{"id":171,"date":"2017-11-02T10:27:54","date_gmt":"2017-11-02T09:27:54","guid":{"rendered":"http:\/\/brgeek.com.br\/wordpress\/?p=171"},"modified":"2017-11-02T10:27:54","modified_gmt":"2017-11-02T09:27:54","slug":"building-a-vagrant-box-quick-start","status":"publish","type":"post","link":"http:\/\/brgeek.com.br\/wordpress\/2017\/11\/02\/building-a-vagrant-box-quick-start\/","title":{"rendered":"Building a Vagrant box &#8211; Quick Start"},"content":{"rendered":"<p>The goal of Vagrant is to make it so simple to create a local development environment, you\u2019d never want to do it another way again. With two simple commands you can quickly setup your first vagrant environment and with a third command, be connected into your first vagrant box in under a few minutes.<\/p>\n<p>vagrant init precise32 http:\/\/files.vagrantup.com\/precise32.box<br \/>\nvagrant up<br \/>\nvagrant ssh # you&#8217;re in!<\/p>\n<p>This power is amazing and so useful for freelancers who work on many different projects, or companies that need to bring new members of a team up to speed as quickly as possible. With the use of Vagrant, the configuration of the development and the production environment can be mirrored as closely as possible. And errors like \u201cworks on my machine\u201d can become a thing of the past.<\/p>\n<p>If you\u2019re already a fan a Vagrant, I\u2019m probably preaching to the choir. For more about what Vagrant can do check out the amazing documentation. Or do what I did and pick up the O\u2019Reilly book by Vagrant\u2019s author, Mitchell Hashimoto, Vagrant: Up and Running and read it. It\u2019s a short but jam-packed review of the details you need to get to know Vagrant, inside and out.<\/p>\n<p>Why Build A Box?<br \/>\nThere are a bunch of amazing boxes out there available on sites like vagrentbox.es and vagrantcloud.com so why would you want to build your own box?<\/p>\n<p>Maybe you want to add a few extra things to your base (a runtime or two like Julia, Erlang, JVM or Python, etc.) then start this as your new \u201cbase\u201d.<\/p>\n<p>Maybe you want your box to have more ram or you need your boxes to more closely mirror production and you are building a ram enriched, multiple server cluster with multiple provisioners, we get it\u2026 you\u2019ve got a mountain to climb, you just need your gear.<\/p>\n<p>Definitions<br \/>\nWhat is a package.box file? When using the VirtualBox provider, it\u2019s a tarred, gzip file containing the following:<\/p>\n<p>Vagrantfile<br \/>\nbox-disk.vmdk<br \/>\nbox.ovf<br \/>\nmetadata.json<br \/>\nThe Vagrantfile has some information that will be merged into your Vagrantfile that is created when you run vagrant init boxname in a folder.<\/p>\n<p>The box-disk.vmdk is the virtual hard disk drive.<\/p>\n<p>The box.ovf defines the virtual hardware for the box.<\/p>\n<p>The metadata.json tells vagrant what provider the box works with.<\/p>\n<p>NOTE: These contents would be different for the VMWare provider, etc. For more about that please refer to the vagrant docs on boxes.<\/p>\n<p>Shout Out<br \/>\nI\u2019m a fan of Vagrant and all things automation, they make my life a lot easier. <\/p>\n<p>Getting Prepared<br \/>\nIf you don\u2019t already have Vagrant and VirtualBox, grab those.<\/p>\n<p>Download Vagrant installer for your operating system.<\/p>\n<p>Download VirtualBox installer for your operating system.<\/p>\n<p>NOTE: Grab the VirtualBox Extension Pack as well. For more about the functionality, it provides read here.<\/p>\n<p>Prior to the installation packages, Vagrant was distributed as a RubyGem. Installation packages are now the preferred way to install Vagrant, so you should uninstall the RubyGem version and follow the instructions for your platform. The RubyGem-based installation is still supported for Vagrant 1.0.x, but is deprecated and will not be supported in any future versions.<\/p>\n<p>Page 8, \u201cVagrant: Up and Running by Mitchell Hashimoto (O\u2019Reilly). Copyright 2013 Mitchell Hashimoto, 978-1-449-33583-0.\u201d<\/p>\n<p>RUBY USERS: If you had Vagrant installed as a rubygem, uninstall it before you install vagrant:<\/p>\n<p>gem uninstall vagrant<br \/>\nRun the Vagrant installer and the VirtualBox and VirtualBox Extension Pack installers, once they are installed, if you feel like a reboot would help, go ahead.<\/p>\n<p>Download ubuntu server, the rest of the instructions will be for ubuntu, but if you\u2019re choosing another distro you\u2019re probably fine to adapt the commands that follow.<\/p>\n<p>Build a Box<br \/>\nWe\u2019re going to use VirtualBox to build an ubuntu server from scratch. The reason for this is because Vagrant has native support for VirtualBox. There are more plugins out there for other providers like VMWare, Parallels or Vagrant-LXC, etc. We\u2019ll stick to VirtualBox for this guide.<\/p>\n<p>When we setup our ubuntu server, it prompts us to setup a default user. We\u2019re going to name that user vagrant so it\u2019s the default user as well. This will make it the default SSH user and streamline the process.<\/p>\n<p>Configure the Virtual Hardware<\/p>\n<p>Create a new Virtual Machine with the following settings:<\/p>\n<p>Name: vagrant-ubuntu64<br \/>\nType: Linux<br \/>\nVersion: Ubuntu64<br \/>\nMemory Size: 512MB (to taste)<br \/>\nNew Virtual Disk: [Type: VMDK, Size: 40 GB]<br \/>\nModify the hardware settings of the virtual machine for performance and because SSH needs port-forwarding enabled for the vagrant user:<\/p>\n<p>Disable audio<br \/>\nDisable USB<br \/>\nEnsure Network Adapter 1 is set to NAT<br \/>\nAdd this port-forwarding rule: [Name: SSH, Protocol: TCP, Host IP: blank, Host Port: 2222, Guest IP: blank, Guest Port: 22]<br \/>\nMount the Linux Distro ISO and boot up the server.<\/p>\n<p>Install The Operating System<\/p>\n<p>Setting up ubuntu is very simple. Follow the on-screen prompts. And when prompted to setup a user, set the user to vagrant and the password to vagrant. It will give you a passive-aggressive guilt trip about it being a weak sauce password. Don\u2019t let that shake you, be strong and soldier through.<\/p>\n<p>Set Root Password<\/p>\n<p>In order to setup the Super User, aka root user, you\u2019ll need to be able to sign in as that user. Since I asked you to make vagrant the default user while installing the Operating System in the last step, these commands should help you set the root password and then sign in as root in order to make the next configuration changes below.<\/p>\n<p>sudo passwd root<br \/>\nThis will prompt you to type the password twice, where I\u2019d suggest the password \u201cvagrant\u201d. Now sign in as the root user in order to Setup the Super User next.<\/p>\n<p>su &#8211;<br \/>\nSetup the Super User<\/p>\n<p>Vagrant must be able run sudo commands without a password prompt and if you\u2019re not configuring ubuntu at this point, just make sure that require tty is disabled for the vagrant user.<\/p>\n<p>The most efficient way I\u2019ve found to setup the vagrant user so it\u2019s able to use sudo without being prompted for a password is to add it to the sudoers list like this:<\/p>\n<p>sudo visudo -f \/etc\/sudoers.d\/vagrant<br \/>\nAnything in the \/etc\/sudoers.d\/* folder is included in the \u201csudoers\u201d privileges when created by the root user. So that\u2019s why we created this as the root user and in that folder.<\/p>\n<p>With that file open add this to the file then save it and exit.<\/p>\n<p># add vagrant user<br \/>\nvagrant ALL=(ALL) NOPASSWD:ALL<br \/>\nNow you can test that it works by running a simple command:<\/p>\n<p>sudo pwd<br \/>\nIt will return the home folder without prompting you for a password if everything is setup correctly. If you are prompted for a password, something\u2019s out of wack and things won\u2019t work right. This step was CRUCIAL for me, so please ensure this test passes for you.<\/p>\n<p>Updating The Operating System<\/p>\n<p>One of the reasons we are building this box is so we can save time by already being as up to date as the box was built. So let\u2019s get up-to-date first.<\/p>\n<p>sudo apt-get update -y<br \/>\nsudo apt-get upgrade -y<br \/>\nUsually, if there are kernel updates you\u2019ll want to reboot the server. So do that.<\/p>\n<p>sudo shutdown -r now<br \/>\nInstall the Vagrant Key<\/p>\n<p>The only way that all the vagrant commands will be able to communicate over ssh from the host machine to the guest server is if the guest server has this \u201cinsecure vagrant key\u201d installed. It\u2019s called \u201cinsecure\u201d because essentially everyone has this same key and anyone can hack into everyone\u2019s vagrant box if you use it.<\/p>\n<p>But at the same time, we\u2019re hoping you\u2019re not running around with all your most valuable company data on your vagrant boxes, right? RIGHT? OK. Good.<\/p>\n<p>mkdir -p \/home\/vagrant\/.ssh<br \/>\nchmod 0700 \/home\/vagrant\/.ssh<br \/>\nwget &#8211;no-check-certificate \\<br \/>\n    https:\/\/raw.github.com\/mitchellh\/vagrant\/master\/keys\/vagrant.pub \\<br \/>\n    -O \/home\/vagrant\/.ssh\/authorized_keys<br \/>\nchmod 0600 \/home\/vagrant\/.ssh\/authorized_keys<br \/>\nchown -R vagrant \/home\/vagrant\/.ssh<br \/>\nInstall and Configure OpenSSH Server<\/p>\n<p>If you didn\u2019t install SSH while installing the operating system you can do it now: sudo apt-get install -y openssh-server<\/p>\n<p>We need to edit the \/etc\/ssh\/sshd_config file:<\/p>\n<p>sudo nano \/etc\/ssh\/sshd_config<br \/>\nFind and uncomment the following line because we added the Vagrant key above to the authorized_keys file:<\/p>\n<p>AuthorizedKeysFile %h\/.ssh\/authorized_keys<br \/>\nThen restart ssh:<\/p>\n<p>sudo service ssh restart<br \/>\nInstalling Guest Tools<\/p>\n<p>Guest Tools help the operating system handle shared folders and \u201coptimize the guest operating system for better performance and usability.\u201d 2<\/p>\n<p>A compiler is required to install the Guest Tools, use this command:<\/p>\n<p>sudo apt-get install -y gcc build-essential linux-headers-server<br \/>\nIn VirtualBox browse to the Devices menu at the top, then in the drop-down list at the bottom, click on Insert Guest Additions CD Image.<\/p>\n<p>This will add an ISO image to the virtual CDROM running in your server. Run these commands to mount your cdrom and then run the script. NOTE: The message about the cdrom being read-only is fine.<\/p>\n<p>sudo mount \/dev\/cdrom \/mnt<br \/>\ncd \/mnt<br \/>\nsudo .\/VBoxLinuxAdditions.run<br \/>\nPackage the Box<\/p>\n<p>Before you package the box you\u2019ll want to \u201czero out\u201d the drive. \u201cThis fixes fragmentation issues with the underlying disk, which allows it to compress much more efficiently later.\u201d 3<\/p>\n<p>sudo dd if=\/dev\/zero of=\/EMPTY bs=1M<br \/>\nsudo rm -f \/EMPTY<br \/>\nNow we\u2019re ready to package the box. I usually make a folder to hold my boxes like so:<\/p>\n<p>mkdir ~\/code\/personal\/vagrant_boxes<br \/>\ncd ~\/code\/personal\/vagrant_boxes<br \/>\nThis is the command that finally packages up the box for you as we defined above into the compressed gzip tarball file, it also generates and includes the Vagrantfile and the metadata.json file.<\/p>\n<p>vagrant package &#8211;base vagrant-ubuntu64<br \/>\nVagrant will then check VirtualBox for any instances of the name vagrant-ubuntu64 and attempt to ssh into them and control them.<\/p>\n<p>\u2192 vagrant package &#8211;base vagrant-ubuntu64<br \/>\n[vagrant-ubuntu64] Attempting graceful shutdown of VM&#8230;<br \/>\n[vagrant-ubuntu64] Forcing shutdown of VM&#8230;<br \/>\n[vagrant-ubuntu64] Clearing any previously set forwarded ports&#8230;<br \/>\n[vagrant-ubuntu64] Exporting VM&#8230;<br \/>\n[vagrant-ubuntu64] Compressing package to: \/Users\/tbird\/code\/personal\/virtual_boxes\/package.box<br \/>\nYou are left with the package.box file in your ~\/code\/personal\/vagrant_boxes folder.<\/p>\n<p>Test Your Box<\/p>\n<p>From your same vagrant_boxes folder you can run these final test commands. All the heavy lifting is really done at this point. If you\u2019ve screwed up something it\u2019s probably in a step up above.<\/p>\n<p>You should be in ~\/code\/personal\/vagrant_boxes\/ and type:<\/p>\n<p>vagrant box add ubuntu64 package.box<br \/>\nvagrant init ubuntu64<br \/>\nvagrant up<br \/>\nConnect to the server you created from start to finish!<\/p>\n<p>vagrant ssh<br \/>\nYou\u2019ve won, you deserve to get your high five on.<\/p>\n<p>Packer<br \/>\nJust when you thought it was safe to do things manually at the beginning \u2026 in comes Packer. What is Packer, you say? Well, Packer automates everything we just did. \/FACEPALM<\/p>\n<p>Anyway\u2026 Once you have Vagrant, VirtualBox, and Packer installed you can define a quick-start.json file like:<\/p>\n<p><code class=\"\" data-line=\"\">{<br \/>\n  &quot;builders&quot;: [{<br \/>\n    &quot;type&quot;: &quot;amazon-ebs&quot;,<br \/>\n    &quot;access_key&quot;: &quot;YOUR KEY HERE&quot;,<br \/>\n    &quot;secret_key&quot;: &quot;YOUR SECRET KEY HERE&quot;,<br \/>\n    &quot;region&quot;: &quot;us-east-1&quot;,<br \/>\n    &quot;source_ami&quot;: &quot;ami-de0d9eb7&quot;,<br \/>\n    &quot;instance_type&quot;: &quot;t1.micro&quot;,<br \/>\n    &quot;ssh_username&quot;: &quot;ubuntu&quot;,<br \/>\n    &quot;ami_name&quot;: &quot;packer-example &quot;<br \/>\n  }]<br \/>\n}<\/code><\/p>\n<p>After changing the Access and Secret Key you could run the packer command with the quick-start.json file:<\/p>\n<p>packer build quick-start.json<br \/>\nPacker will then automate the creation of that quick-start.json machine image for Amazon EC2. That can optionally include the creation of a Vagrant box.<\/p>\n<p>What Packer aims to do is to make their images compatible with all these providers: Amazon EC2 (AMI), DigitalOcean, Docker, Google Compute Engine, OpenStack, QEMU for KVM or Xen instances, or even VirtualBox or VMWare software. (See platforms for the entire list.)<\/p>\n<p>What Have We Learned<br \/>\nWhat have we learned from this process of building boxes for Vagrant?<\/p>\n<p>The process of automation is the process of taking yourself out of the equation. \u201cAuto\u201d is defined as self, and the suffix \u201caction\u201d is defined as action. So if we think of the job of automating your development environment or production environment so it runs without your intervention, then your actions are doing it right!<\/p>\n<p>And even if we want to do it by hand or use Packer, knowing how things work is a lot better than guessing how they work. And I like to experience things a few times before I start to automate those boring parts away, and then focus on new more interesting challenges.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The goal of Vagrant is to make it so simple to create a local development environment, you\u2019d never want to do it another way again. With two simple commands you can quickly setup your first vagrant environment and with a third command, be connected into your first vagrant box in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":172,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-171","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"jetpack_featured_media_url":"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2017\/11\/devops-100314993-primary.idge_.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/comments?post=171"}],"version-history":[{"count":1,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":173,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/171\/revisions\/173"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media\/172"}],"wp:attachment":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}