<commit_before>
# Getting Started

Getting started with Chef and Vagrant.

## Prerequisites

Make sure that VirtualBox is installed as described in the [README](file://W:/_README.html). 

Internet connectivity is a must as we will bootstrap new VMs from scratch which requires downloading packages etc...

We assume you mounted Bill's Kitchen to the `W:\` drive by double-clicking the `mount-w-drive.bat` file and that the environment has been set up via `W:\set-env.bat`. 


## The Scenario

We are going to set up a Chef server locally on our laptop, and then set up a web server node from scratch using `knife bootstrap`. Thus the next steps are:

1. Start the Chef Server VM
2. Setup a Web Server using Chef

 
### Start the Chef Server VM

Within our chef repository located at `W:\repo\my-chef-repo` there is a `Vagrantfile` for setting up a Chef Server VM. Go to that directory and fire up our chef server using `vagrant up chef_server`:

```
W:\repo\my-chef-repo>vagrant up chef_server
[chef_server] Importing base box 'pre-baked-chef-server'...
[chef_server] Matching MAC address for NAT networking...
[chef_server] Clearing any previously set forwarded ports...
[chef_server] Forwarding ports...
[chef_server] -- 22 => 22310 (adapter 1)
[chef_server] Creating shared folders metadata...
[chef_server] Clearing any previously set network interfaces...
[chef_server] Preparing network interfaces based on configuration...
[chef_server] Booting VM...
[chef_server] Waiting for VM to boot. This can take a few minutes.
[chef_server] VM booted and ready for use!
[chef_server] Configuring and enabling network interfaces...
[chef_server] Setting host name...
[chef_server] Mounting shared folders...
[chef_server] -- v-root: /vagrant
```

Verify that the Chef Server VM is running using `vagrant status chef_server`:

```
W:\repo\my-chef-repo>vagrant status chef_server
Current VM states:

chef_server              running

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
```

Ok, server is running. Now let's see if we can talk to it using `knife` (yes, this is the commandline tool of the chefs). Note that the URL of the chef server (along with your client and validation certificate) are already pre-configured in `.chef\knife.rb`. For example, let's query for the clients the chef server knows about: 

```
W:\repo\my-chef-repo>knife client list
  admin
  chef-validator
  chef-webui
  cheffe

W:\repo\my-chef-repo>knife client show cheffe
_rev:        1-479aeb04e51bb4111239e93c5752c457
admin:       true
chef_type:   client
json_class:  Chef::ApiClient
name:        cheffe
public_key:  -----BEGIN RSA PUBLIC KEY-----
             MIIBCgKCAQEAq4EnCkRw3MZRVUhiyd/IqrtcXOoavLFBZ682QGGJc3gHE8XhPyqD
             Cy2r92DCV+lzx4tFAzcK+CF0e8ovQGQR505PjAUWdcODbrx+WgNI8H3k+366eG9s
             U1i0gPdiw31d/mcaiNBe7CltzyK6Eot/BHtgjCEVv9zSRwMPvKCOAZN8PjVKQQAT
             usvVEJWrbBg00x9uYUMFWHYUbVEMPxNCq0xA1e8x0EFry0FhtIBD69lfM+xw04s5
             SQoEE565Qv+eCxfgaP4bXiZ+rkoBtHI9NrL4QONCwdQzqbHsL/xyVK4tuQ4Sq8Za
             XmG/8IuhB8DrbY/9GFlvDe8x9D57aATYywIDAQAB
             -----END RSA PUBLIC KEY-----
```  

Now that our chef server is running we have to upload all the cookbooks, roles, data bags and environments that we have in our chef repository. The first step is to get all the cookbooks defined in `Cheffile` from their remote resources using [librarian](https://github.com/applicationsonline/librarian):

```
W:\repo\my-chef-repo>librarian-chef clean

W:\repo\my-chef-repo>librarian-chef install

W:\repo\my-chef-repo>dir cookbooks
 Volume in drive W is ZUEHLKE
 Volume Serial Number is CE7B-106D

 Directory of W:\repo\my-chef-repo\cookbooks

11.05.2012  10:11    &lt;DIR&gt;          .
11.05.2012  10:11    &lt;DIR&gt;          ..
11.05.2012  10:11    &lt;DIR&gt;          apache2
11.05.2012  10:11    &lt;DIR&gt;          build-essential
11.05.2012  10:11    &lt;DIR&gt;          graphite
11.05.2012  10:11    &lt;DIR&gt;          munin
11.05.2012  10:11    &lt;DIR&gt;          ntp
11.05.2012  10:11    &lt;DIR&gt;          python
11.05.2012  10:11    &lt;DIR&gt;          runit
11.05.2012  10:11    &lt;DIR&gt;          vagrant-ohai
```

As you can see from the output above, `librarian-chef` has now pulled the dependencies defined in `Cheffile` to the cookbooks directory in `W:\repo\my-chef-repo\cookbooks`. Don't edit your cookbooks in here as this directory is managed by librarian! 

Ok, now let's upload everything from our chef repository to the chef server. The easiest way to do this by running `rake install` inside `my-chef-repo`:

```
W:\repo\my-chef-repo>rake install
** Updating your repository
git pull
Already up-to-date.
Updated Role webserver!
Uploading apache2                     [1.1.8]
Uploading build-essential             [1.0.0]
Uploading graphite                    [0.3.0]
Uploading munin                       [1.0.2]
Uploading ntp                         [1.1.8]
Uploading python                      [1.0.6]
Uploading runit                       [0.15.0]
Uploading vagrant-ohai                [1.0.0]
upload complete
```

Great! Our chef server now knows all our cookbooks, roles, environments etc.

You can also use the chef server webui to explore any data on the chef server. Just point your browser to http://192.168.127.12:4040/cookbooks (log in with admin/p@ssw0rd1) and explore the cookbooks we have just uploaded:

![Chef Server - Explore Cookbooks](https://raw.github.com/tknerr/bills-kitchen/master/doc/chef-server_cookbooks.png) <commit_msg>GH-1: update getting started guide for setting up the chef server via knife-server plugin<commit_after>
# Getting Started

Getting started with Chef and Vagrant.

## Prerequisites

Make sure that VirtualBox is installed as described in the [README](file://W:/_README.html). 

Internet connectivity is a must as we will bootstrap new VMs from scratch which requires downloading packages etc...

We assume you mounted Bill's Kitchen to the `W:\` drive by double-clicking the `mount-w-drive.bat` file and that the environment has been set up via `W:\set-env.bat`. 


## The Scenario

We are going to set up a Chef server locally on our laptop, and then set up a web server node from scratch using `knife bootstrap`. Thus the next steps are:

1. Setup a Chef Server
2. Verify the Chef Server works
3. Setup a Web Server using Chef

### Setup a Chef Server

Within our chef repository located at `W:\repo\my-chef-repo` there is a `Vagrantfile` which we will use for setting up a Chef Server VM using [knife-server](https://github.com/fnichol/knife-server). Go to that directory and run `vagrant up chef_server`. This will fire up a Vagrant VM with a bare os Ubuntu basebox:

```
W:\repo\my-chef-repo>vagrant up
[chef_server] VM already created. Booting if it's not already running...
[chef_server] Clearing any previously set forwarded ports...
[chef_server] Forwarding ports...
[chef_server] -- 22 => 22311 (adapter 1)
[chef_server] Creating shared folders metadata...
[chef_server] Clearing any previously set network interfaces...
[chef_server] Preparing network interfaces based on configuration...
[chef_server] Running any VM customizations...
[chef_server] Booting VM...
[chef_server] Waiting for VM to boot. This can take a few minutes.
[chef_server] VM booted and ready for use!
[chef_server] Configuring and enabling network interfaces...
[chef_server] Setting host name...
[chef_server] Mounting shared folders...
[chef_server] -- v-root: /vagrant
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` v-root /vagrant
```

You can ignore the error message at the bottom. This just means that it couldn't mount the 

You can verify that the Chef Server VM is running using `vagrant status chef_server`:

```
W:\repo\my-chef-repo>vagrant status chef_server
Current VM states:

chef_server              running

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
```

Now use `knife-server` to bootstrap a new Chef Server installation on that empty Vagrant VM. When you look at the `Vagrantfile` you will notice the IP address of the `chef_server` VM we just started is `192.168.127.12`, so that is what we will use for bootstrapping the Chef Server installation:

```
W:\repo\my-chef-repo>knife server bootstrap standalone --ssh-user vagrant --ssh-password vagrant --node-name chef-server --host 192.168.127.12 --bootstrap-version 10.14.4
Bootstrapping Chef on 192.168.127.12
192.168.127.12 + setup
192.168.127.12 + apt-get update
0% [Working]
Ign http://security.ubuntu.com precise-security InRelease
10% [Connecting to us.archive.ubuntu.com (91.189.91.30)]
Get:1 http://security.ubuntu.com precise-security Release.gpg [198 B]
...
```

This might take a while... and finally end up like this: 

```
...
192.168.127.12 [Fri, 28 Sep 2012 18:05:22 +0000] INFO: Processing service[apache2] action restart (apache2::default line 219)
192.168.127.12 [Fri, 28 Sep 2012 18:05:25 +0000] INFO: service[apache2] restarted
192.168.127.12 [Fri, 28 Sep 2012 18:05:25 +0000] INFO: Chef Run complete in 26.269591 seconds
192.168.127.12 [Fri, 28 Sep 2012 18:05:25 +0000] INFO: Running report handlers
192.168.127.12 [Fri, 28 Sep 2012 18:05:25 +0000] INFO: Report handlers complete
192.168.127.12 + rm -rf /tmp/chef-solo
192.168.127.12 + printf -- '-----> Bootstraping Chef Server on chef-server is complete.\n'
192.168.127.12 -----> Bootstraping Chef Server on chef-server is complete.
WARNING: No knife configuration file found
Creating initial API user...
Created client[root]
Configuration file written to /root/.chef/knife.rb
```

What happened so far?
1. knife-server installed chef-server with all it's prerequisites (ruby, rubygems, etc...)
2. it created the admin key `cheffe.pem` and validation key `validation.pem` and copied it to `W:\repo\my-chef-repo\.chef` 
3. it set up apache with ssl so that you can access the REST API via `https://192.168.127.12`

For more details check out the [knife-server README](https://github.com/fnichol/knife-server/blob/master/README.md)


### Verify that the Chef Server works

Ok, server is running. Now let's see if we can talk to it using `knife` (yes, this is the commandline tool of the chefs). Note that the URL of the chef server (along with your client and validation certificate) are already pre-configured in `.chef\knife.rb`. For example, let's query for the clients the chef server knows about: 

```
W:\repo\my-chef-repo>knife client list
  chef-validator
  chef-webui
  cheffe
  root

W:\repo\my-chef-repo>knife client show cheffe
_rev:        1-d5e582804a1ca7a7cc91c9ef6c0932ed
admin:       true
chef_type:   client
json_class:  Chef::ApiClient
name:        cheffe
public_key:  -----BEGIN RSA PUBLIC KEY-----
             MIIBCgKCAQEA<KEY>
             <KEY>
             -----END RSA PUBLIC KEY-----
```  

Now that our chef server is running we have to upload all the cookbooks, roles, data bags and environments that we have in our chef repository. The first step is to get all the cookbooks defined in `Cheffile` from their remote resources using [librarian](https://github.com/applicationsonline/librarian):

```
W:\repo\my-chef-repo>librarian-chef clean

W:\repo\my-chef-repo>librarian-chef install

W:\repo\my-chef-repo>dir cookbooks
 Volume in drive W is ZUEHLKE
 Volume Serial Number is CE7B-106D

 Directory of W:\repo\my-chef-repo\cookbooks

11.05.2012  10:11    &lt;DIR&gt;          .
11.05.2012  10:11    &lt;DIR&gt;          ..
11.05.2012  10:11    &lt;DIR&gt;          apache2
11.05.2012  10:11    &lt;DIR&gt;          build-essential
11.05.2012  10:11    &lt;DIR&gt;          graphite
11.05.2012  10:11    &lt;DIR&gt;          munin
11.05.2012  10:11    &lt;DIR&gt;          ntp
11.05.2012  10:11    &lt;DIR&gt;          python
11.05.2012  10:11    &lt;DIR&gt;          runit
11.05.2012  10:11    &lt;DIR&gt;          vagrant-ohai
```

As you can see from the output above, `librarian-chef` has now pulled the dependencies defined in `Cheffile` to the cookbooks directory in `W:\repo\my-chef-repo\cookbooks`. Don't edit your cookbooks in here as this directory is managed by librarian! 

Ok, now let's upload everything from our chef repository to the chef server. The easiest way to do this by running `rake install` inside `my-chef-repo`:

```
W:\repo\my-chef-repo>rake install
** Updating your repository
git pull
Already up-to-date.
Updated Role webserver!
Uploading apache2                     [1.1.8]
Uploading build-essential             [1.0.0]
Uploading graphite                    [0.3.0]
Uploading munin                       [1.0.2]
Uploading ntp                         [1.1.8]
Uploading python                      [1.0.6]
Uploading runit                       [0.15.0]
Uploading vagrant-ohai                [1.0.0]
upload complete
```

Great! Our chef server now knows all our cookbooks, roles, environments etc.

You can also use the chef server webui to explore any data on the chef server. Just point your browser to http://192.168.127.12:4040/cookbooks (log in with admin/chefchef) and explore the cookbooks we have just uploaded:

![Chef Server - Explore Cookbooks](https://raw.github.com/tknerr/bills-kitchen/master/doc/chef-server_cookbooks.png) 
{'fraction_non_alphanumeric': 0.09611572068433806, 'fraction_numerical': 0.053601586019531536, 'mean_word_length': 3.273611546909319, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 17, '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': '7512758', 'n_tokens_mistral': 4961, 'n_tokens_neox': 4351, 'n_words': 1616}