- Vagrant repackaging box from existing one #vagrant
Vagrant repackaging is a hacky way to create a new vagrant box from an existing one, which is already configured and well tested. In this article, scope is not to explain the benefits of using virtualization in your development workflows. As well, I’m assuming that you:
- already have the latest vagrant installed, hostupdater plugin and virtualbox .
- already have a box already up (I used scotch box from scotch.io), configured to your dev needs (i.e. a proper vagrant file, services like apache, mysql etc).
Also assuming that your colleagues have:
- Installed virtualbox, vagrant and hostupdater plugin
- Have a … Instant messenger and … a torrent client????
In my case, I like to work with a team and ask peoples’ opinion around me about the current state of dev progress or collaborate with my colleagues in LAN or WAN :D . I also like the torrent way of (thinking and) distributing large files like the one we’re about to create.
So, in this tutorial we will learn to repackage an existing vagrant box into another, new, box. Distribute the .box file, share it with all our colleagues, via torrent protocol, with the help of an open bittorrent tracker, privately, in no time! Well … not one bittorrent tracker. Actually, some more of them …
openbittorrent.com
coppersurfer.tk
leechers-paradise.org/
My virtual machine’s settings
As a mentioned earlier, I used scotch box from scotch.io. The purpose of this vm is for web development so the shared folders, apache, php5 and mysql are configured to my needs which are:
My folder structure:
1 2 3 4 5 6 7 8 9 10 11 |
project's name [dir] |--------- Vagrantfile [file] |--------- www [dir] |------------ files {1...n}.php [file] |--------- project's-name.sublime-project [file] |--------- project's-name.sublime-workspace [file] |
Apache 2.4 with:
- Server Location set to /var/www
- Allow override all option to honour .htaccess
Mysql server:
- with the user accounts created and their permissions set.
Proper vagrant file with:
- neworking set (port forwards, static address(es), bridged interafaces etc)
- hostname too (to relief from web app’s URL issues)
- synced folder set
a minimal file like that one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config.vm.box = "scotch/box" config.vm.network "public_network", ip: "10.10.0.204" config.vm.hostname = "myapp.local" config.vm.synced_folder "www/", "/var/www", :mount_options => ["dmode=777", "fmode=666"] # Optional NFS. Make sure to remove other synced_folder line too #config.vm.synced_folder "www/", "/var/www", :nfs => { :mount_options => ["dmode=777","fmode=666"] } end |
Cleaning your box
We are going to clean up your messy virtual machine! Like your sysadmin-mom!
1 |
sudo apt-get clean |
We need to make our box as small as possible.
1 |
sudo dd if=/dev/zero of=/EMPTY bs=1M |
Repackage existing vagrant box into an other new box
1 |
vagrant package --base the_vm_name_for_virtualbox --output /mypath/tosave/project_name.box |
Once the vagrant is done with the packaging, we’re going to create the private torrent , put three or more trackers to distribute the .box file to our team.
Distribution : Creating private torrent
Transmission is the default torrent client comes with Ubuntu and other GNU/Linux distros, mine is Ubuntu Gnome 15.10. Using transmission we’ll create the private torrent file and send it via some IM (Jabber/XMPP, skype etc) to our fellow devs.
- Open transmission
- File -> New …
- Select the save destination of the .torrent file
- Select the .box file you want to share
- Put trackers announce addresses (make sure there are enough of them, MPAA and the judges think that torrents are made for the pirating movies)
- Write some sort of a comment about the file, I prefer to use project’s name here too.
- Mark torrent as private (sometimes we need some privacy :P)
Now send the file to anyone who need it. If you prefer, create a small seedbox (seedboxing-like-a-boss guide is here) for two months for free (at digital ocean. no! Seriously… Do the math by yourselves!) to speed-up seeding. We are geeks, right?
Adding the .box to colleagues host machine …
1 |
vagrant box add the_vm_name_for_virtualbox /mypath/todownloaded/project_name.box |
1 |
vagrant init the_vm_name_for_virtualbox |
Update the new Vagrantfile and change the static ip address or use an entirely different configuration for your team mate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config.vm.box = "scotch/box" config.vm.network "public_network", ip: "192.168.1.15" config.vm.hostname = "myapp.local" config.vm.synced_folder "www/", "/var/www", :mount_options => ["dmode=777", "fmode=666"] # Optional NFS. Make sure to remove other synced_folder line too #config.vm.synced_folder "www/", "/var/www", :nfs => { :mount_options => ["dmode=777","fmode=666"] } end |
Thereafter run
1 |
vagrant up |
ENJOY!
More resources you may need:
- http://cloud-images.ubuntu.com/
- https://docs.vagrantup.com/v2/
- https://atlas.hashicorp.com/help
- https://www.virtualbox.org/
- https://www.vagrantup.com/