<commit_before>= Lab 02 IT Infrastructure

In this lab you will learn more about IT infrastructure by running Vagrant and learning basic "infrastructure as code" principles.

This is a long lab, with the following learning objectives:

* Creation of a basic virtual machine using Vagrant
* Using your Linux skills for simple server configuration
* Learning the basics of Git source control
* Combining the three previous skills into an automated "infrastructure as code" use case

==  Running Vagrant on the Course Server

Vagrant is already installed on the serverXXX course server, but you need to take a few steps before you can use it.
Assuming you have .ssh configured and have logged in, you should find yourself in your default directory.

IMPORTANT: In the following instructions when you see *YourStudentID*, substitute your user id. Where you see "serverXXX," substitute the course servername.

Do not type the "$" symbol - this represents the command prompt you see when you log into Linux. It may also be a "#" or have some directory information in front of it, e.g.

	YourStudentID@serverXXX:~$

First, always confirm where you are by issuing the "pwd" (print working directory) command:

....
YourStudentID@serverXXX:~$ pwd
/home/student/YourStudentID
....

and examine what is in your directory:

....
YourStudentID@serverXXX:~$ ls -al
drwxr-xr-x  5 YourStudentID student 4096 Sep 19 11:35 .
drwxr-xr-x 32 root  root    4096 Sep 19 09:19 ..
-rw-------  1 YourStudentID student 1040 Sep 19 10:51 .bash_history
drwx------  2 YourStudentID student 4096 Sep 19 09:26 .cache
drwx------  3 YourStudentID student 4096 Sep 18 20:08 .config
drwxr-xr-x  2 YourStudentID student 4096 Sep 19 09:50 .ssh
....

In any command line environment, it is good to run these commands (pwd and ls, or their equivalents) frequently so you maintain awareness of where you are and what might be affected by any commands you run.

First, you need to do some setup work, by copying the virtual machine file to your home directory. This prevents a lengthy download process.

....
YourStudentID@serverXXX:~$ mkdir -p .vagrant.d/boxes
YourStudentID@serverXXX:~$ cp -R /var/vagrant/.vagrant.d/boxes/precise64 .vagrant.d/boxes
....

Now, make a "vagrant" directory:

....
YourStudentID@serverXXX:~$ mkdir vagrant
YourStudentID@serverXXX:~$
....

Instead of "ls -al," try "tree -al":

....
YourStudentID@serverXXX:~$ tree -al
.

├── .ssh
│   └── authorized_keys
├── vagrant
└── .vagrant.d
    └── boxes
        └── precise64
            └── 0
                └── virtualbox
                    ├── box-disk1.vmdk
                    ├── box.ovf
                    ├── metadata.json
                    └── Vagrantfile

10 directories, 13 files
....

If your directory layout does not look the same as above (particularly the vagrant and .vagrant.d directories) check your work, or ask for assistance. (Don't worry about extra files you may have, but be sure the minimum above is present.)

Try:
....
YourStudentID@serverXXX:~$ ls -al .vagrant.d/boxes/precise64/0/virtualbox/
total 313172
drwxr-xr-x 2 YourStudentID student      4096 Sep 19 11:37 .
drwxr-xr-x 3 YourStudentID student      4096 Sep 19 11:37 ..
-rwxr-xr-x 1 YourStudentID student 320654336 Sep 19 11:37 box-disk1.vmdk
-rwxr-xr-x 1 YourStudentID student     14108 Sep 19 11:37 box.ovf
-rwxr-xr-x 1 YourStudentID student        25 Sep 19 11:37 metadata.json
-rwxr-xr-x 1 YourStudentID student       505 Sep 19 11:37 Vagrantfile
....

Notice the size (320654336) of "box-disk1.vmdk." This is the virtual machine image.  (I'd like to find a smaller one, but it's OK for now.)

Change to your "vagrant" directory:

....
YourStudentID@serverXXX:~$ cd ~/vagrant
YourStudentID@serverXXX:~/vagrant$
....

Type the following command carefully:

	YourStudentID@serverXXX:~/vagrant$ vagrant init precise64

You should see the following:

	A `Vagrantfile` has been placed in this directory. You are now
	ready to `vagrant up` your first virtual environment! Please read
	the comments in the Vagrantfile as well as documentation on
	`vagrantup.com` for more information on using Vagrant.
	YourStudentID@serverXXX:~/vagrant$

Now, cross your fingers and enter the following command:

	YourStudentID@serverXXX:~/vagrant$ vagrant up

You should see:
....
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'precise64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1442681339461_15180
==> default: Fixed port collision for 22 => 2222. Now on port 2207.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2207 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2207
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 4.3
==> default: Mounting shared folders...
    default: /vagrant => /home/student/YourStudentID/vagrant
....

If you saw the above output, or close to it, try:

....
YourStudentID@serverXXX:~/vagrant$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
New release '14.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2
vagrant@precise64:~$
....

If that worked, congratulations, you did it!

But, what does this all mean?

You now have your own personal Linux machine, operating within the main course server, on which you are the administrator. You have tremendous freedom to experiment with its configuration.

If you damage something, you simply need to exit out to the main server command prompt and run "vagrant destroy." This will erase the machine. You can then run "vagrant up" again and the machine will be restored.

IMPORTANT: If you destroy the VM you will LOSE ALL DATA you have on the machine, unless that data is stored in your ~/vagrant directory (which is mapped to your /vagrant directory in the VM).

DO NOT ATTEMPT to bridge your VM to the external world. It is not hardened and possibly vulnerable. As long as you do not mess with the network configuration, you will be fine. In a future lab, you will see how to run Firefox on the course server in a X11 window over ssh on your local machine, so you can safely browse to your VM.

For further information, see https://docs.vagrantup.com/.

== Infrastructure configuration

In this part, you will get a very simple introduction to the concept of "infrastructure as code" using shell scripting on your virtual machine in conjunction with Git and Github

**Prerequisites**

Lab 00 & 01: You must have configured ssh and run the Linux tutorial (or be comfortable otherwise with Linux command-line tools).

Lab 02: You must have successfully created your own virtual machine (VM) using Vagrant.<commit_msg>lab 02 minor fix<commit_after>= Lab 02 IT Infrastructure

In this lab you will learn more about IT infrastructure by running Vagrant and learning basic "infrastructure as code" principles.

This is a long lab, with the following learning objectives:

* Creation of a basic virtual machine using Vagrant
* Using your Linux skills for simple server configuration
* Learning the basics of Git source control
* Combining the three previous skills into an automated "infrastructure as code" use case

==  Running Vagrant on the Course Server

Vagrant is already installed on the serverXXX course server, but you need to take a few steps before you can use it.
Assuming you have .ssh configured and have logged in, you should find yourself in your default directory.

IMPORTANT: In the following instructions when you see *YourStudentID*, substitute your user id. Where you see "serverXXX," substitute the course servername.

Do not type the "$" symbol - this represents the command prompt you see when you log into Linux. It may also be a "#" or have some directory information in front of it, e.g.

	YourStudentID@serverXXX:~$

First, always confirm where you are by issuing the "pwd" (print working directory) command:

....
YourStudentID@serverXXX:~$ pwd
/home/student/YourStudentID
....

and examine what is in your directory:

....
YourStudentID@serverXXX:~$ ls -al
drwxr-xr-x  5 YourStudentID student 4096 Sep 19 11:35 .
drwxr-xr-x 32 root  root    4096 Sep 19 09:19 ..
-rw-------  1 YourStudentID student 1040 Sep 19 10:51 .bash_history
drwx------  2 YourStudentID student 4096 Sep 19 09:26 .cache
drwx------  3 YourStudentID student 4096 Sep 18 20:08 .config
drwxr-xr-x  2 YourStudentID student 4096 Sep 19 09:50 .ssh
....

In any command line environment, it is good to run these commands (pwd and ls, or their equivalents) frequently so you maintain awareness of where you are and what might be affected by any commands you run.

First, you need to do some setup work, by copying the virtual machine file to your home directory. This prevents a lengthy download process.

....
YourStudentID@serverXXX:~$ mkdir -p .vagrant.d/boxes
YourStudentID@serverXXX:~$ cp -R /var/vagrant/.vagrant.d/boxes/precise64 .vagrant.d/boxes
....

Now, make a "vagrant" directory:

....
YourStudentID@serverXXX:~$ mkdir vagrant
YourStudentID@serverXXX:~$
....

Instead of "ls -al," try "tree -al":

....
YourStudentID@serverXXX:~$ tree -al
.

├── .ssh
│   └── authorized_keys
├── vagrant
└── .vagrant.d
    └── boxes
        └── precise64
            └── 0
                └── virtualbox
                    ├── box-disk1.vmdk
                    ├── box.ovf
                    ├── metadata.json
                    └── Vagrantfile

10 directories, 13 files
....

If your directory layout does not look the same as above (particularly the vagrant and .vagrant.d directories) check your work, or ask for assistance. (Don't worry about extra files you may have, but be sure the minimum above is present.)

Try:
....
YourStudentID@serverXXX:~$ ls -al .vagrant.d/boxes/precise64/0/virtualbox/
total 313172
drwxr-xr-x 2 YourStudentID student      4096 Sep 19 11:37 .
drwxr-xr-x 3 YourStudentID student      4096 Sep 19 11:37 ..
-rwxr-xr-x 1 YourStudentID student 320654336 Sep 19 11:37 box-disk1.vmdk
-rwxr-xr-x 1 YourStudentID student     14108 Sep 19 11:37 box.ovf
-rwxr-xr-x 1 YourStudentID student        25 Sep 19 11:37 metadata.json
-rwxr-xr-x 1 YourStudentID student       505 Sep 19 11:37 Vagrantfile
....

Notice the size (320654336) of "box-disk1.vmdk." This is the virtual machine image.  (I'd like to find a smaller one, but it's OK for now.)

Change to your "vagrant" directory:

....
YourStudentID@serverXXX:~$ cd ~/vagrant
YourStudentID@serverXXX:~/vagrant$
....

Type the following command carefully:

	YourStudentID@serverXXX:~/vagrant$ vagrant init precise64

You should see the following:

	A `Vagrantfile` has been placed in this directory. You are now
	ready to `vagrant up` your first virtual environment! Please read
	the comments in the Vagrantfile as well as documentation on
	`vagrantup.com` for more information on using Vagrant.
	YourStudentID@serverXXX:~/vagrant$

Now, cross your fingers and enter the following command:

	YourStudentID@serverXXX:~/vagrant$ vagrant up

You should see:
....
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'precise64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1442681339461_15180
==> default: Fixed port collision for 22 => 2222. Now on port 2207.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2207 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2207
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 4.3
==> default: Mounting shared folders...
    default: /vagrant => /home/student/YourStudentID/vagrant
....

If you saw the above output, or close to it, try:

....
YourStudentID@serverXXX:~/vagrant$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
New release '14.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2
vagrant@precise64:~$
....

If that worked, congratulations, you did it!

But, what does this all mean?

You now have your own personal Linux machine, operating within the main course server, on which *you are the administrator*. You have tremendous freedom to experiment with its configuration.

If you damage something, you simply need to exit out to the main server command prompt and run "vagrant destroy." This will erase the machine. You can then run "vagrant up" again and the machine will be restored.

IMPORTANT: If you destroy the VM you will LOSE ALL DATA you have on the machine, unless that data is stored in your ~/vagrant directory (https://docs.vagrantup.com/v2/synced-folders/index.html[which is mapped] to your /vagrant directory in the VM).

WARNING: DO NOT ATTEMPT to bridge your VM to the external world. It is not hardened and possibly vulnerable. As long as you do not mess with the network configuration, you will be fine. In a future lab, you will see how to run Firefox on the course server in a X11 window over ssh on your local machine, so you can safely browse to your VM.

For further information, see https://docs.vagrantup.com/.

== Infrastructure configuration

In this part, you will get a very simple introduction to the concept of "infrastructure as code" using shell scripting on your virtual machine in conjunction with Git and Github

**Prerequisites**

Lab 00 & 01: You must have configured ssh and run the Linux tutorial (or be comfortable otherwise with Linux command-line tools).

Lab 02: You must have successfully created your own virtual machine (VM) using Vagrant.
{'fraction_non_alphanumeric': 0.08089983539596415, 'fraction_numerical': 0.03285984271169908, 'mean_word_length': 3.5923852183650617, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3270982', 'n_tokens_mistral': 5135, 'n_tokens_neox': 4602, 'n_words': 2241}