<commit_before># mesostack
Deploy [Mesosphere] (https://mesosphere.com)  on [Apache CloudStack] (http://www.cloudstack.org)

## Prerequisites
- CloudStack Advanced Zone
- Ubuntu 1404 template (see http://dl.openvm.eu/cloudstack/ubuntu/vanilla/14.04/x86_64/)

## Approach
First we build a Mesos image (CloudStack template) that contains the necessary mesosphere packages using [Packer](https://www.packer.io). For both master and slave nodes we use the same CloudStack template, although the slave requires only a subset of the master's software. To do this, we start with a base Ubuntu1404 image and install Mesosphere packages on it.

Once Packer creates a Mesos base template for us, we will use Terraform to create the Mesosphere cluster using  this base template to create the master and slave Mesos VMs

## Packer install
Create an Ubuntu base VM in a network in CloudStack. Install Packer on this VM. We'll call this VM the Packer VM.
Packer can be installed from https://www.packer.io. The CloudStack plugin can be installed from https://github.com/schubergphilis/packer-cloudstack.  Note that if you are building from scratch, as of end of June 2015, the build fails. To build successfully
```bash
export GOPATH=$HOME/go
mkdir -p $GOPATH
export PATH=$PATH:$GOPATH/bin
#assuming that Packer 0.7.5 has been installed in $GOPATH/bin
sudo apt-get -y install mercurial git bzr 
go get -u github.com/mitchellh/gox
go get -u github.com/schubergphilis/packer-cloudstack
cd $GOPATH/src/github.com/mitchellh/packer
make -C $GOPATH/src/github.com/schubergphilis/packer-cloudstack updatedeps dev
git checkout tags/v0.7.5
cd $GOPATH/src/github.com/schubergphilis/packer-cloudstack
[edit scripts/build.sh to comment out go get -u]
make dev
```

## Build Mesos Image using Packer
Copy `mesostack.json` from this repository to the Packer VM.


Edit the `builders` part of the Packer template (`mesostack.json`). Fill in the values for :


      `hypervisor`: xenserver has been tested

      `service_offering_id` : this is the service offering used by Packer to instantiate a new (Mesos) VM.

      `template_id` : the CloudStack template id of the base Ubuntu template.

      `zone_id` : zone where Packer will create the Mesos VM.

      `network_ids` : Network where Packer will create the Mesos VM. This has to be the same network as the Packer VM.


We need the credentials to the CloudStack cloud.
```bash
export CLOUDSTACK_API_URL="http://cloudstack.local:8080/client/api"
export CLOUDSTACK_API_KEY="<KEY>"
export CLOUDSTACK_SECRET_KEY="<KEY>"
```
Execute on Packer VM
```bash
packer validate mesostack.json
packer build mesostack.json
```

If this works, you will have a brand new template called 'Ubuntu_mesos'.

## Build the Mesosphere cluster in an isolated network
We will use  [Terraform] (https://www.terraform.io) to deploy the Mesos template and create a Mesosphere cluster<commit_msg>Add terraform details<commit_after># mesostack
Deploy [Mesosphere] (https://mesosphere.com)  on [Apache CloudStack] (http://www.cloudstack.org)

## Prerequisites
- CloudStack Advanced Zone
- Ubuntu 1404 template with cloud-init(e.g., install from http://dl.openvm.eu/cloudstack/ubuntu/vanilla/14.04/x86_64/)

## Approach
First we build a Mesos image (CloudStack template) that contains the necessary mesosphere packages using [Packer](https://www.packer.io). For both master and slave nodes we use the same CloudStack template, although the slave requires only a subset of the master's software. To do this, we start with a base Ubuntu1404 image and install Mesosphere packages on it.

Once Packer creates a Mesos base template for us, we will use Terraform to create the Mesosphere cluster using  this base template to create the master and slave Mesos VMs

## Packer install
Create an Ubuntu base VM in a guest network in CloudStack. Install Packer on this VM. We'll call this VM the Packer VM.
Packer can be installed from https://www.packer.io. The CloudStack plugin can be installed from https://github.com/schubergphilis/packer-cloudstack.  Note that if you are building from scratch, as of end of June 2015, the build fails. To build successfully
```bash
export GOPATH=$HOME/go
mkdir -p $GOPATH
export PATH=$PATH:$GOPATH/bin
#assuming that Packer 0.7.5 has been installed in $GOPATH/bin
sudo apt-get -y install mercurial git bzr 
go get -u github.com/mitchellh/gox
go get -u github.com/schubergphilis/packer-cloudstack
cd $GOPATH/src/github.com/mitchellh/packer
make -C $GOPATH/src/github.com/schubergphilis/packer-cloudstack updatedeps dev
git checkout tags/v0.7.5
cd $GOPATH/src/github.com/schubergphilis/packer-cloudstack
make dev
```

## Build Mesos Image using Packer
Copy `mesostack.json` from this repository to the Packer VM.


Edit the `builders` part of the Packer template (`mesostack.json`). Fill in the values for :


      `hypervisor`: xenserver has been tested
      `service_offering_id` : this is the service offering used by Packer to instantiate a new (Mesos) VM.
      `template_id` : the CloudStack template id of the base Ubuntu template.
      `zone_id` : zone where Packer will create the Mesos VM.
      `network_ids` : Network where Packer will create the Mesos VM. This has to be the same network as the Packer VM.

We need to let Packer know the credentials to the CloudStack cloud. In the Packer VM:
```bash
export CLOUDSTACK_API_URL="http://cloudstack.local:8080/client/api"
export CLOUDSTACK_API_KEY="<KEY>"
export CLOUDSTACK_SECRET_KEY="<KEY>"
```
Execute on Packer VM
```bash
packer validate mesostack.json
packer build mesostack.json
```

If this works, you will have a brand new template called 'Ubuntu1404_mesos'.

## Build the Mesosphere cluster in an isolated network
We will use  [Terraform] (https://www.terraform.io) to deploy the Mesos template and create a Mesosphere cluster
We will use this [excellent tutorial] (https://www.digitalocean.com/community/tutorials/how-to-configure-a-production-ready-mesosphere-cluster-on-ubuntu-14-04) as our guide.

### Prerequisites
* Any  hypervisor (tested : XenServer 6.5)
* terraform >= v0.6.1
* API and secret keys for your CloudStack
* ssh keypair (using `cloudmonkey`: `cloudmonkey create sshkeypair name=ubuntu`)
* Ubuntu1404_mesos template built by Packer as above

### Configure
You can execute terraform from anywhere, as long as the CloudStack API is reachable. Check out (or copy) the contents of the terraform folder in this repository to where you will be working with terraform.

Update the `mesostack.tf` by filling in the values for the variables.  Details for the values of those variables are below.

The available variables that can be configured are:

* `cs_access_key`: CloudStack API key
* `cs_secret_key`: CloudStack secret key
* `cs_key_name`: The SSH key name to use for the instances
* `cs_ssh_private_key_file`: Path to the SSH private key file. This should have been generated by `create sshkeypair`.
* `cs_ssh_user`: SSH user (default `ubuntu`)
* `num_masters`: The number of Mesos Masters. Zookeeper and Marathon will run on these as well to launch (default `3`)
* `num_slaves`: The number of Mesos Slaves. 

Other variables may be configured in cloudstack/variables.tf . Of particular interest are the instance types (service offerings) for the master and slave nodes (default : t1.medium), and the ip address defaults (172.16.0.x/24)

### Deploy

Here are some step-by-step instructions for deploying the Mesos cluster via Terraform:

1. Run the following commands in the folder containing the `mesostack.tf` file

  ```bash
  terraform get -update
  terraform apply
  ```

  This will deploy the cluster.

Upon success, terraform will print the public IP of the cluster

```
Outputs:

  public_ip = 10.31.229.150
```

This public ip has port forwards to ssh into the masters and slaves as well as the Web UI for Mesos and Marathon.

- Ports 1222, 1223, 1224, etc are forwarded to ssh port of master nodes
- Ports 2222, 2223, 2224, etc are forwarded to ssh port of slave nodes
- Port 5050 and 8080 are forwarded respectively to the first master node

Unfortunately it isn't guaranteed that the first master node will be the 'leading master' of the cluster. Therefore the Web UI (e.g., `http://10.31.229.150:5050/`) will try to redirect to the *private* IP of the leading master. This will be unreachable from outside the mesos cluster network, so you will have to play around with the port forwarding to get to the right master.

You can add additional slave nodes to grow your cluster by changing the variable `num_slaves` in `mesostack.tf` and 
```bash
  terraform plan
  terraform apply
```
{'fraction_non_alphanumeric': 0.07608316877686143, 'fraction_numerical': 0.016726681379951213, 'mean_word_length': 4.391358797745774, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 11, 'lorem ipsum': 0, 'www.': 9, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2665088', 'n_tokens_mistral': 2764, 'n_tokens_neox': 2518, 'n_words': 1127}