# Samrat Man Singh

Email: mail@samrat.me

Hi! I’m Samrat, a programmer from Nepal.

# A first look at provisioning with Puppet(on a Vagrant box)

### 2012-06-04

In my previous post, I talked about deploying a Flask app on a Vagrant box using Gunicorn and Nginx. The response I got was mind-blowing, so I’ve decided to write about another neat tool that’s awesome for deploying web apps- Puppet. Vagrant actually encourages its users to use it, and you should use it. There’s also an alternative to Puppet called Chef; some people prefer that over Puppet, so you might want to check it out.

Hopefully I’ll be able to demonstrate what Puppet does and why its awesome in this post. But please note that this isn’t meant as a comprehensive tutorial, you should check out Puppet’s docs for that. Also even though the Puppet docs asks you to get the Learning Puppet VM, I found it much more comfortable to use vagrant ssh for learning Puppet, so if you already have Vagraant installed, you might want to try that out too- just try running puppet inside the virtual machine.

What Puppet does is something called provisioning- that means that it makes computers do what they are supposed to do. In other words, it does the configuring for you. To understand what that means, let’s see it in action.

First create a Vagrant box,

::shell
mkdir codebase_with_puppet
cd codebase_with_puppet

vagrant init


You should now see Vagrantfile. Open it with a text editor, then uncomment the lines

::puppet
config.vm.provision :puppet do |puppet|
puppet.manifests_path = "manifests"
puppet.manifest_file  = "base.pp"
end


Now, create base.pp inside a folder called manifests, and add the following to it.

::puppet
package {"nginx":
ensure => present,
}


Now, run vagrant up. You’ll notice that Vagrant automatically installs nginx after it boots the VM. You should get a message like

::shell
notice: /Stage[main]//Package[nginx]/ensure: ensure changed 'purged' to 'present'


This can become a real treasure as this way, you won’t have to memorize what you need to install, to get the app running. All the system needs is to have puppet installed, after that puppet with the right manifests will handle everything.

Now, let’s do something different with Puppet- instead of installing another package, we’ll use it to configure nginx.

First, create a file in your local machine, inside codebase_with_puppet called codebase_nginx. To that file add the following

::nginx
server {
location / {
proxy_pass http://127.0.0.1:8000;
}
}


If you’ve gone through the previous post you’ll notice that it’s the same configuration that we had used.

Now, we’ll use Puppet to make sure that the configuration file is placed where its supposed to be. To your base.pp file, add

::puppet
file {"rm-nginx-default":
path => '/etc/nginx/sites-enabled/default',
ensure => absent,
require => Package['nginx'],
}

file {"setup-nginx-codebase":
path => '/etc/nginx/sites-enabled/codebase_nginx',
ensure => present,
require => Package['nginx'],
source => "/vagrant/codebase_nginx",
}


Run vagrant reload and you’re done with the nginx configuration. Besides removing the repetitiveness for you, Puppet is also wonderful when you’re working on a team or on an open-source project. Now, all you need to do is write the manifests and once you share them you can rest assured that the entire team has the exact same environment.

# Flask + Nginx + Gunicorn(on a Vagrant box)

### 2012-05-27

I had some difficulty in grasping how exactly to set up a server when I tried to do so recently, so I decided to write a tutorial that will guide you through the process. Hopefully, this post will help you avoid at least some of the confusion that I encountered.

We’ll be using Nginx + Gunicorn to host a simple Flask app. Many of you may not have access to a server but don’t worry, we’ll use Vagrant, which makes use of a VirtualBox VM to emulate a server.

Because this is a post about deployment more than development, we’ll make the web app super-simple. If you’re not familiar with Flask, please check it out, its awesome and really easy to learn. You’ll also probably want to develop the app inside virtualenv- it makes things a lot neater. Make a folder in your local machine(we’re not working with the virtual-machine yet) for your app, I’ll call it codebase. Create two folders called static and templates, and a Python file called app.py. codebase should now look like this:

.
├── app.py
├── static
└── templates


Now, open app.py with a text editor and add the following:

from flask import Flask

@app.route('/')
def index():
return "Hello world!"

if __name__ == '__main__':
app.run()

At this point, if you run app.py with python app.py, you should be able to open http://localhost:5000/ and see a “Hello World!” printed. Now, freeze your requirements with

pip freeze > requirements.txt

Great, now we’ll start working on the actual server.

### Vagrant

As I said before, Vagrant allows you to work with server-like environments on your local machine. It’s absolutely great. To get Vagrant up and running:

# first make sure Virtualbox is installed, then,
gem install vagrant
vagrant init
vagrant up

If nothing went wrong, you should now see a file called Vagrantfile inside codebase- that’s Vagrant’s configuration file. Open the file, we’ll need to make a few changes to the file.

First, uncomment the line:

config.vm.network :hostonly, "192.168.33.10"
If nothing went wrong, you should now see a file called Vagrantfile inside codebase- that's Vagrant's configuration file. Open the file, we'll need to make a few changes to the file.

and change “192.168.33.10” to “33.33.33.33”. This will enable the host-machine(that is your computer) to access the webserver running on the VM.

That way we should be able to access a web app running in the VM’s localhost, on our machine.

Because, we did a vagrant up the Vagrant box should already be running. Now, run

vagrant reload

so that the changes we made to the Vagrantfile take place.

After the VM restarts, run

vagrant ssh

This allows you to run commands into the VM. Once inside the VM, we’ll need to get some things installed. Run

apt-get install nginx
pip install virtualenv

Now let’s create a folder inside the VM where we’ll keep the application

cd /home/vagrant
mkdir www
cd www
virtualenv --no-site-packages .
mkdir codebase

And let’s grab the application from our local machine

cp /vagrant/* /home/vagrant/www/codebase/

Note that while I used cp, its always a better idea to use git or some other version-control system. For more on that, I recommend that you read this post.

Then, activate the virtualenv we created.

cd /home/vagrant/www
source bin/activate

Install gunicorn with pip

pip install gunicorn

Also install the other Python dependencies your app has with

pip install -r requirements.txt

Now, if you run

gunicorn /home/vagrant/www/codebase/app.py:app -b 127.0.0.1:8000

you’ll have your app running but if you try opening it from your browser you’ll find that you can’t actually see the “Hello World” message that we were expecting. That’s where nginx comes in.

### Nginx

First of all, you need to start nginx with

/etc/init.d/nginx start

Then

rm /etc/nginx/sites-enabled/default
touch /etc/nginx/sites-available/codebase
ln -s /etc/nginx/sites-available/codebase  /etc/nginx/sites-enabled/codebase

To /etc/nginx/sites-enabled/codebase add

server {
location / {
proxy_pass http://127.0.0.1:8000;
}
}

And restart nginx with

/etc/init.d/nginx restart

Now, from inside codebase run

gunicorn app:app -b localhost:8000

If everything went right, if you visit http://33.33.33.33/ you should now see the “Hello World!” message. Congratulations! You’ve successfully set up your own server.

Update- I’ve written a follow-up to this post which covers Puppet, a really handy tool that’s comes packaged with Vagrant- you can find the post here.