Outils pour utilisateurs

Outils du site


issue101:c_c

Ceci est une ancienne révision du document !


For any reader who has ever developed anything that needed to be tested or run on a system outside of the local development server or workstation, you’ve probably hoped for an easy way to control a test environment, or to emulate the target server. This is essentially what vagrant wants to offer. It’s a tool that creates a configuration file for a server - specifications, OS, what to install, and links to the sources (such as ISOs). This is then run through vagrant, which created a VirtualBox virtual machine running everything outlined in the vagrant file.

Now…you may be asking yourself why you wouldn’t just do this by hand? If you’re going to need the development server on and off (such as web development, where certain combinations of software and OSes, or particular versions are common), or if you need to be able to pass the exact development environment to other people. Such as when working in a team. This way, you can just share the vagrantfile (and, possibly, the source files), and the other team members will be up and running swiftly.

Vagrant also handles certain aspects - such as installing guest additions, enabling SSH access, and various other aspects. It also ensures the networking interface is bridged, so that you can access software running on the server from your local network. For anyone who uses a local Apache install and virtual hosts for their web projects, this is one way of creating virtual machines of your server. Running the actual software you’ll encounter on deployment. If you, like me, tend to delete virtual machines the moment you’re done with them because they’re hogging your hard drive space, Vagrant is a perfect match. Keep the vagrantfile, and delete everything else. Space saved!

How does it work?

Vagrant must be installed (it’s a command-line tool), as well as VirtualBox. In Ubuntu it’s as simple as sudo apt-get install vagrant virtualbox. If you’re using a different host OS, you’ll need to check how best to install them.

Once Vagrant is installed, you can either download a Vagrantfile (there are many posted on github, or on http://www.vagrantbox.es/), or create one yourself. Creating one on your own is something I haven’t yet done. As such, we’ll focus on the more common scenario of using a pre-built Vagrantfile. If there is interest in creating custom Vagrantfiles, send me an email, and I will follow up this article at a later date. A rough scenario is using vagrant init, and then adjusting the default Vagrantfile to suit your needs.

Getting started

For the sake of this article, I’ll be using a prebuilt Vagrantfile called “django-python3-vagrant”. See the Further Reading for a link.

Download the file:

git clone https://github.com/FlipperPA/django-python3-vagrant.git

Then cd into the folder:

cd django-python3-vagrant

Unfortunately, this vagrantfile is geared towards utopic - which is no longer located on the Ubuntu server. So, time to edit the Vagrantfile!

On line 6, you’ll want to change the line from “django_config.vm.box = “utopic64”” to “django_config.vm.box = “trusty64””.

Replace line 10 with the following:

django_config.vm.box_url = “https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box

Here, you need to run:

vagrant up

Once that command finishes running, you can log into the VM using

vagrant ssh

Upon login, you’ll be greeted by some instructions on creating a django project. Follow them. However, on the runserver step, you’ll need to replace 0.0.0.0:8000 with the actual IP of the server. Find this using ipconfig. For example: python manage.py runserver 192.168.1.200:8000

After that, you can access the django instance using the IP address of the server.

To stop the VM:

vagrant halt

In previous versions, this was vagrant shutdown. So if halt doesn’t work for you, try shutdown.

To delete the VM:

vagrant destroy

If you run into issues, make sure of the following things: • You’re using the right IP in both your browser, and the runserver command. • You’re running the vagrant commands from the folder that contains the Vagrantfile

Hopefully, this will be of use to anyone who need to frequently create the same (or similar) virtual machines. If you have questions, comments, or suggestions, feel free to contact me at lswest34+fcm@gmail.com. Have a Vagrantfile you can’t live without? Or a Vagrant tip that saves time? Email them to me, and I’ll compile them into an article.

Further Reading

https://github.com/FlipperPA/django-python3-vagrant - Vagrantfile

http://vagrantbox.es - Prebuilt boxes

https://www.vagrantup.com/ - Vagrant homepage

issue101/c_c.1443477437.txt.gz · Dernière modification : 2015/09/28 23:57 de d52fr