allstufftech September 13, 2017

Without deploying an app, which is created in your machine, you can never be sure of whether it will be working same in the LIVE environment or not. And for making anything live on the web, you need to pay and buy a sunscription service. We do have the options of Heroku which gets well integrated with the Git but still we loose upon the control on the app.

A good solution is AppFog which provides FREE account with 2GB RAM for your apps. You get better control of the services associated with the apps and as well can control the start / stop and restart of the same through a web interface. You can even increase the memory need for your app. Also, it has an automated deployment gem called “af” which automates the process of deployment in an easy fashion as simple as some simple commands.

Since I was able to successfully deploy my Rails app in appfog, I am just mentioning the steps which needs to be followed for the same. Ensure that your app has mysql2 gem already present in the Gemfile.

Rails App in Appfog

1. Create a new rails app in local machine

2. Install appfog gem

sudo gem install af

3. Configure Appfog credentials

af login

Provide your email address and password as the login credentials.

4. Go to the rails app directory and push

af push --runtime=ruby193

The ruby runtime is mentioned in the above command because without this, my app did not work.

Logs:

$ af push --runtime=ruby193
Would you like to deploy from the current directory? [Yn]: Y
Application Name: appfogtestapp
Detected a Rails Application, is this correct? [Yn]: Y
1: AWS US East - Virginia
2: AWS EU West - Ireland
3: AWS Asia SE - Singapore
4: Rackspace AZ 1 - Dallas
5: HP AZ 2 - Las Vegas
Select Infrastructure: 1
Application Deployed URL [appfogtestapp.aws.af.cm]:
Memory reservation (128M, 256M, 512M, 1G, 2G) [256M]:
How many instances? [1]:
Bind existing services to 'appfogtestapp'? [yN]: N
Create services to bind to 'appfogtestapp'? [yN]: N
Would you like to save this configuration? [yN]: N
Creating Application: OK
Uploading Application:
 Checking for available resources: OK
 Processing resources: OK
 Packing application: OK
 Uploading (655K): OK
Push Status: OK
Staging Application 'appfogtestapp': OK
Starting Application 'appfogtestapp': OK

5. Go to your AppFog Dashboard and Visit the app link.
In my case, this was the link: appfogtestapp.aws.af.cm

6. PrePackage Rails App and Update

bundle package
bundle install
bundle exec rake assets:precompile
af update appfogtestapp

Logs:

$ af update appfogtestapp
Uploading Application:
 Checking for available resources: OK
 Processing resources: OK
 Packing application: OK
 Uploading (114K): OK
Push Status: OK
Stopping Application 'appfogtestapp': OK
Staging Application 'appfogtestapp': OK
Starting Application 'appfogtestapp': OK

After this your all assets like css, etc will come to life in production environment.

Use “push” only for creating new applications in appfog. For all other cases to update the changes, use “update”.

6. Appfog Tunnel and DB credentials

gem install caldecott
af services

Logs:

$ af services
============== System Services ==============
+------------+---------+-------------------------------+
| Service | Version | Description |
+------------+---------+-------------------------------+
| mongodb | 1.8 | MongoDB NoSQL store |
| mysql | 5.1 | MySQL database service |
| postgresql | 9.1 | PostgreSQL database service |
| rabbitmq | 2.4 | RabbitMQ message queue |
| redis | 2.2 | Redis key-value store service |
+------------+---------+-------------------------------+
=========== Provisioned Services ============
+---------------------------+---------+-----+
| Name | Service | In |
+---------------------------+---------+-----+
| appfogtestapp-mysql-40241 | mysql | rs |
+---------------------------+---------+-----+
af tunnel appfogtestapp-mysql-40241

Logs:

Deploying tunnel application 'caldecott-rs'.
Uploading Application:
 Checking for available resources: OK
 Packing application: OK
 Uploading (1K): OK
Push Status: OK
Binding Service [appfogtestapp-mysql-40241]: OK
Staging Application 'caldecott-rs': OK
Starting Application 'caldecott-rs': OK
Getting tunnel connection info: OK
Service connection info:
 username : uFY9S0laI85UX
 password : pcOCChewIK7HS
 name : d420c81250e9f475ca24a46b4ee417964
 infra : rs
Starting tunnel to appfogtestapp-mysql-40241 on port 10000.
1: none
2: mysql
3: mysqldump
Which client would you like to start?: 1
Open another shell to run command-line clients or
use a UI tool to connect using the displayed information.
Press Ctrl-C to exit...

In another terminal window, modify and update the database.yml configuration for the production environment.

production:
 adapter: mysql2
 database: d420c81250e9f475ca24a46b4ee417964
 username: uFY9S0laI85UX
 password: pcOCChewIK7HS
 port : 10000
 host : 127.0.0.1
af update appfogtestapp

If you note, the above command has created a new service and binded to our Rails app and that acts as a mySql DB for our app.

7. Rails Rake DB migrate

Keep the other terminal running where you created the af tunnel and in another terminal run these commands for DB tables creation and data additions in your appfog app.

RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake db:seed
af update appfogtestapp