Press enter to see results or esc to cancel.

[EN] Real scenario for deploying a django project with numericube.deploy on ec2 machine

From localhost to EC2

For our projects, we use intensively ansible, fabric, salt, vagrant and other assorted tools for deploying our applications. But we end up always doing the same work…

So we created a tool, numericube.deploy, to help us to deploy our applications. Let’s see how to use it to:

  • Deploy an app on ec2 machine.
  • Update this app on this machine with a well process.

So I will take as exemple a pure and fresh Django project. This project is just a page displaying “Welcome”. This article describes a real process, that have been tested in real condition, that show you that it can be easy to make complicated task with this tool in less time.

Let’s go !

So first create the test django app.

We use a generic cookiecutter : cookiecutter-django template (https://cookiecutter-django.readthedocs.io/en/latest/). This template is overkill for our use case (ie a simple welcome page) but it is generic. So you can see that numericube.deploy can work with a real django professional app.

  • create a virtualenv

virtualenv welcomeapp

  • install cookiecutter

pip install cookiecutter

  • And generate our django app named welcome

cookiercutter https://github.com/pydanny/cookiecutter-django

We answer to question, and after all this, we have a real django app that have a welcome page. Because  cookiecutter-django  is designed for real project, it only use postgres database. We can use, for this demonstration project, sqlite3, enough for our use case (so edit DATABASES in config/settings/base.py and change this settings).

We want that  welcome app be distribuable with pip, so we add to welcome directory a simple setup.py.

We have a wonderfull django app the welcome app !

Now we want to deploy this app, with a generic production stack with nginx, supervisor … So add now an ansible configuration to our fresh new project.

How can I quickly do that ? With cookiecutter of course and our special recipe design for this : cookiecutter-numericube-ansible

Answer question, and after generation do that program tell to you to do.

We create an ssh key and configure a vagrant file (vagrant init + adapt config.vm.provision).

After that, you can test ansible configuration in vagrant.  It’s easy to test if ansible work. After this you can use vagrant to develop in real condition.

It is time to commit our work. So I create a new repository in github: https://github.com/yboussard/welcome.git . And push all things on it.

So now we have a django project, with a good ansible structure, and everything is commit and push in github. I just answer to question for create this.

It’s time to deploy our welcome app to the cloud. So on my virtualenv I install and configure numericube.deploy tool (see readme for that).

I must configure my project for numericube.deploy. Two tasks to achieve that:

  1. Get the public key generated by cookiecutter-numericube-ansible and add it as deploy key on github.
  2. Generate a project configuration with generate_config , utility that comes with numericube.deploy installation. Answer question, and that’s all, if response is correct, you’re ready to create an push app on ec2 machine !

This generates code in the project on fabfile directory.

Now create an ec2 machine. numericube.deploy help us for that with create_instance utility.

create_instance AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY SSH_PUBLIC_KEY

This creates a new instance and create a user as the user that launch the command. After cloud-init, you can ssh to the ec2 machine if your ssh_public_key correspond to your ssh_private_key. The ec2 machine is ready to deploy. With our test, this public amazon machine is ec2-54-75-78-109.eu-west-1.compute.amazonaws.com 

 

To test that just use fab test command with the name of the ec2 machine provided by create_instance

The new machine is created, we must modify ansible configuration host now. So edit ansible host file for deploying to your new instance  (provision/ansible/hosts) in staging section.

[staging]
ec2-54-75-78-109.eu-west-1.compute.amazonaws.com nickname=staging vm=0

You are now ready to release and deploy your app on ec2 machine with numericube.deploy

fab -H ec2-54-75-78-109.eu-west-1.compute.amazonaws.com release

This creates a tag, v20170411 and numericube.deploy say to you if you want deploy it.

This is the very first time that you deploy on this machine. So numericube.deploy advert you about that and say to you that machine will be bootstrap before deployed. Answer yes if you want to deploy.

Deploy process is launch. After some configuration, it calls ansible with your deploy key in order to install and configure your app.

Deployment succeed. I open my browser and I see results:

Normal ! Django 1.10 verify HOST_ALLOWED and we don’t change that. But the results is good. Supervisor work, nginis well configured. Just one little thing is not ok. We must fix it.

As we simulate a real project, I create an issue on git for this project called DisallowedHost.

I fix settings and commit with the #1 number of issue. Normally after that my django should work. So lets go for updating ec2 machine with numericube.deploy and see how it update things.

fab -H ec2-54-75-78-109.eu-west-1.compute.amazonaws.com release

This create a new release tag v20170411a. 

We re-say yes to deploy. 

You see that now numericube.deploy see the remote deployed tag and give to us the change log between the two release. You can see diff color for each commit if you want. If all is correct, type yes for deploying.

After deploy process you should notice that numericube.deploy ask you your git credential. This is for create a git token used for update tracker.

As we prefix comment commit with #1 , numericube.deploy detect that you have deploy the correction of an issue and comment and close it. The tracker is automatically update with those information.

and finally test on browser welcome app see results after deploying

It’s finally looks good.

Voila !

So for synthesize in order to create and deploy a django app on ec2 with numericube.deploy:

  1. Create django app with cookiecutter , one file added (setup.py), one file modified (base.py)
  2. Create an ansible configuration with cookiecutter-numericube-ansible.
  3. Create a numericube.deploy configuration with generate_config utility and configure a ssh git deploy key.
  4. Use create_instance utility to create an ec2 machine ready for deploy.
  5. And use fabric and numericube.deploy command for deploying and updating application over the cloud.

Thanks for reading me!

Regards,

Youenn.