Deploying a Rails project as a Fly.io Machine
config/routes.rb:
fly.io-rails gem:
fly:app generator, the files necessary to deploy your application will be generated on your first deploy using default options.
Deploy your project
fly status.
You can also run commands like fly apps open to bring your application up in the
browser.
Now lets make that application launch more machines.
Installing fly on the Rails Machine
Since we will be using fly services from within our Rails application, we will need to install the fly executable. We do that by adding the following lines to our Dockerfile:
# Deploy your application comment.
Next we need to make a Fly token available to our application:
Add a controller
Lets generate a controller with three actions:job/startwill be the URL you will load that will kick off a job.job/completewill be the URL that job will fetch once it is complete.job/statuswill be the URL you will load once the job is complete to see the results.
start method provides the complete URL of the complete action
as a parameter to the machine job.
Before moving on, lets make sure that the file exists:
Add a Job
We start by generating a job:- Specify a machine configuration. For simplicity we will use the
same Fly application name and the same Fly image as our Rails
application. The server command will be
curlspecifying the URL that was passed as an argument to the job. - Start a machine using this configuration, and check for errors, and log the results.
- Query the status of the machine every 10 seconds for a maximum of 5 minutes, checking to see if the machine has exited.
- Extract the exit code and log the state. If the machine has exited successfully we delete the machine, otherwise we leave it around so that further forensics can be performed.
Fly::Machines:
Each of the lines in the list above is a link to the documentation for that API.
The key difference is that instead of passing in and receiving back a JSON object, you pass in and receive back a Ruby hash. And all of the URLs and HTTP headers are taken care of for you by the Fly::Machine module.
For those interested in the inner workings, the source to Fly::Machine is on GitHub. Again, all this is beta, and subject to change.
Trying it out
We are now ready to deploy, but before we do in a separate window start watching the log::fly apps open you will arrive at your application’s welcome page.
Take a note of the URL. Either in the browser or in a command window add
/job/start. As a response (either in your browser or terminal window
you will see something like:
/job/status to see the results. Durations will vary, but I’m currently seeing a total elapsed time of anywhere from about two and a half seconds to five seconds.
Recap
While not exactly a realistic application, this application demonstrates a number of important features. Scheduling a job. Launching, monitoring, and removing a machine. Inter-machine communications. With the right parameters, you can start machines in remote geographic locations or with volumes attached. These machines will have access to your application’s secrets so they can access databases or other cloud services. And if you go back and look at themain.tf file in your application directory you can get an indication of what steps are required, and what options are required for each step, to launch a Rails application.
The possibilities are unlimited.
At the moment, this is only a preview, so API names and options may change.
But do try this out, and if you have questions or come up with an exciting
usage of this, let us know on community.fly.io.