Working with Vagrant

Now, we are going to createVagrantfile configuration file in the root of our application to create a simple environment. We will provide a Linux distribution environment, which will be Ubuntu. The content of the Vagrantfile is as follows:

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

config.vm.box = "hashicorp/precise32"

config.vm.network :forwarded_port, guest: 8090, host: 8090
config.vm.network "public_network", ip: "192.168.1.121"
#config.vm.synced_folder "target","/opt"

config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048"]
end

# provision
config.vm.provision "shell", path:"entrypoint.sh"

end

Pay attention to line 6 of Vagrantfile:

config.vm.box = "hashicorp/precise32"

We are creating our Linux environment from an already built VM box from hashicorp/precise32.

Before continuing with the provision of the environment using Vagrant, we are going to create an ssh file that will install JDK 8 for us. At the root of the project, create an entrypoint.sh file with the following content:

#!/usr/bin/env bash
sudo apt-get update


echo "Install Java 8.."
sudo apt-get install -y software-properties-common python-software-properties


echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | sudo /usr/bin/debconf-set-selections
sudo add-apt-repository ppa:webupd8team/java -y

sudo apt-get update

sudo apt-get install oracle-java8-installer
echo "Set env variables for Java 8.."
sudo apt-get install -y oracle-java8-set-default


# Start our simple web application with specific JVM_ARGS and SPRING_PROFILE
echo "Run our springboot application."
java -Dspring.profiles.active=dev -jar /vagrant/target/infra-as-code-0.0.1-SNAPSHOT.jar

Then, to create the box and provision the VM, we are going to run the following on the Console:

vagrant up

On the first attempt, it will take some minutes to download the box and provision the server. Between these processes, you will be asked which network interface you will use to provision your server with the question Which interface should the network bridge to?You can then choose what is more convenient for your machine.

At the end of the whole output of our execution, we will see our Spring application running on the provisioned server, as shown in the following screenshot:

Now we can check our application is running on port 8090 (http://localhost:8090/) in the browser. You can check the Java process running inside Vagrant by accessing ssh with the following command:

vagrant ssh

This will open an ssh session on our provision server, allowing us to see the process already created in the console:

vagrant@precise32:~$ ps aux | grep java

The output of the result will be our running Java process, as shown in the following screenshot:

To stop the VM, we can use the vagrant halt command in the console:

vagrant halt

To destroy the created VM you can enter the following:

vagrant destroy

We just learned to express our infrastructure as code using Vagrant. We can create an environment or server needed for the different stages using different tools; we can review this in the previous chapter. In the next section, we are going to create an example of the process of release management.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.178.237