Lessons: 12Length: 40 minutes

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.1 Exporting and Sharing Your Vagrant Box

The Vagrant instance is now fully set up. However, if anything happened to it, we would have to start configuring it again from scratch. To avoid this and to make it easier to start new Node.js projects, in this lesson we are going to use the shell provisioner to automatically perform our installation steps. Afterwards, we’ll export the completed Vagrant box to Atlas so we can share it with others.

Related Links

3.1 Exporting and Sharing Your Vagrant Box

Hi. Welcome back to Easy No Chas Development Environment for Vagrant. In this lesson we are going to export and share our Vagrantbox with the world. Before we can do that, it is probably a good idea to make what we have done until now repeatable. To do that, we're going to use a provisioner with Vagrant. Let's edit the vagrant file and scroll down to the end. Remember the provision section I mentioned in one of the earlier lessons? This is what we need. Vagrant supports a variety of provisioners, like Ansible, Chef, Docker, or Puppet. It can also use a simple shell script. This is what we are going to use because it's easy to understand and completely sufficient for our purpose. First, uncomment the sample code. Then add the privileged fals flag, so that we don't run as root, but as the Vagrant user with it all the time. Now I'm going to paste all the steps we went through in the previous lessons. To refresh your memory, I'm going to explain them quickly as we go. First, we are going to update the apt repository and install build-essential and curl for later. Then, we need to get NVM. I'm using the cold version of the install command here. Install the latest node version and set it to the default. Then we add the Mongol DP public key and the Mongol DP repository to our app configuration. After another update, we install Mongol DP dash org. Now comes the compilation and configuration of redis. We first download the archive, and extract it. Then we move into the deps directory, and compile the four dependencies. After this is completed We compile redis itself and then install it. For a configuration, we first create the necessary directories. Copy the init script and update the run levels. Then we copy the configuration file for redis and use sed to change all the properties that need changing. After everything is complete you start to redis service. After all dependencies are set up we change into our slash Vagrant folder and run RPM install. To install all packages in case the user already hasn't done so. Then we globally install nodemon. Finally, we need to install the Ruby package, and the foreman gem. After this is done, we just need to export the upstart configuration, and start the service. Now that the provision is set up, let's put it to the test. Destroy the Vagrant instance with Vagrant Destroy and confirm the action. We can now recreate the box with Vagrant up. After the initialization and boot, Vagrant runs our shared script. This can take quite a bit so grab a coffee or stretch a leg while you wait. After provisioning finished you should try if everything worked. Open up your browser and go to local host port 4000. The web site works, and so does our circuit communication. Now that we have a working provisioner, it's time to share our fresh box with the world. Start by going to the atlas website and log in or sign up if you haven't created an account already. It's free. Use the Create Vagrant Box feature to publish our work. First we need to choose a name space and name file box. I'm going to use my user name, and easy node here. Boxes can also be made private, restricting access to a few collaborators. You can add a short description which explains what the box includes. After you entered all the data, you can create that box. It's time to redirect you to the version creation form. As I mentioned before, Vagrant boxes are version and can be updated. Let's add version 1.0.0. Now that you've created a version, we need to add some data to it. Start by creating a new provider. We are going to choose virtual box. You then have the option to use either a URL, for instance the drop box for MSMS3, or a direct file upload. I'm going to upload the Vagrant box directory. I have to choose a file to upload, but what do I put here? We need to go back to our shell for a bit to create ourselves a Vagrant box file. Use Vagrant package to do that. This will shut down your Vagrant instance and export the current state of it. When the packaging is complete we have a newly created package.box file in our folder. This is the one we want to upload. After uploading is complete we can see the info about our provider on the version page. There is also command to import and boot a new instance of this box. Let's crop the command. Before we can use it, though, we have to release the version. This can be done through editing it. The box is released. Now everyone has access to our box. I copied our project's source code to another folder to use the newly created Vagrant base box. Paste in the command for fetching our new box, and execute it. Vagrant will download and import the box, and finally boot it up. There is no provisioning, since it is already done inside the virtual machine. Give it a try. When we open the browser, it can't connect to our node app. If you have a look at Vagrant's output, you can see that the port for wedding wasn't set up. Right. It is a fresh Vagrant file that doesn't come with that. Change the port for wedding to 4000 again and reload Vagrant. After this is done, you can revisit the app again and see it running. It also stored the sample contacts we created at the beginning of the lesson. This is a good point to tell you that you might not want to share Vagrant base boxes without completely set up. Leave all custom actions for setting up the app itself to the user, or provided in the Vagrant file, not the box itself. In the next lesson, I'm going to show you how to share your running Vagrant instance with others. See you there.

Back to the top