## Build Basic Image Vagrant Box locally
```
curl -L https://github.com/Alfresco/alfresco-spk/raw/vagrant-plugin/plugin/pkg/spk-0.1.0.gem > spk.gem
vagrant plugin install spk.gem

# Used by Vagrant commandline
export SPK_STACK_TEMPLATE=file://$PWD/spk/build-vagrantbox.json.example

vagrant spk build-images -s $SPK_STACK_TEMPLATE
```

To fetch databags:
```
export ENV_VARS=https://raw.githubusercontent.com/Alfresco/alfresco-spk/master/packer/commands/common-env-vars.json

export PRE_COMMANDS=https://raw.githubusercontent.com/Alfresco/alfresco-spk/master/packer/commands/fetch-databags.json

vagrant spk build-images -s $SPK_STACK_TEMPLATE --env-vars $ENV_VARS --pre-commands $PRE_COMMANDS
```

To add local environment variables
```
ENV_VARS=file://$PWD/spk/env-vars.json,$ENV_VARS
```

To add Git/Nexus-specific release/deployment steps:
```
PRE_COMMANDS=$PRE_COMMANDS,https://raw.githubusercontent.com/Alfresco/alfresco-spk/master/packer/commands/prepare-git-release.json
POST_COMMANDS=$POST_COMMANDS,https://raw.githubusercontent.com/Alfresco/alfresco-spk/master/packer/commands/perform-git-release.json,https://raw.githubusercontent.com/Alfresco/alfresco-spk/master/packer/commands/nexus-upload.json
```

To run a release: (used by alfresco-spk/commands/common-env-vars.json):
```
MVN_REPO_ID=internal-releases
CURRENT_VERSION_SUFFIX=
```

## Build Basic Image AMI with Bamboo (Script Task)
```
# Bamboo environment variables
set -e
export HOME=${system.user.home}
export PATH=/usr/local/packer:/opt/apache-maven/bin:/Users/Shared/apache-maven/3.2.3/bin:$HOME/.chefdk/gem/ruby/2.1.0/bin:/opt/chefdk/bin:/opt/chefdk/embedded/bin:$PATH

GIT_REPO="${bamboo.planRepository.repositoryUrl}"
GIT_TOKEN="${bamboo.git_token}"
AWS_ACCESS_KEY_ID="${bamboo.aws_key_id}"
AWS_SECRET_ACCESS_KEY="${bamboo.aws_key_secret}"
SPK_AMI_FILE="${bamboo.spk_ami_file}"
SPK_VAGRANTBOX_FILE="${bamboo.spk_vagrantbox_file}"

rm -rf ~/.alfresco-spk/commands
mkdir -p ~/.alfresco-spk/commands

# Define private SPK items
declare -a arr=("https://api.github.com/repos/Alfresco/chalf-img-basic/contents/spk/bamboo-env-vars.json?ref=using-spk" "https://api.github.com/repos/Alfresco/chalf-img-basic/contents/spk/env-vars.json?ref=using-spk")

LOCAL_ENV_VARS=""
# Download private SPK items
for URL in "${arr[@]}"
do
  FILE_NAME=`echo $URL | rev | cut -d / -f 1 | rev | cut -f1 -d"?"`
  echo "curl --header 'Authorization: token $GIT_TOKEN' --header 'Accept: application/vnd.github.v3.raw' $URL > $HOME/.alfresco-spk/commands/$FILE_NAME"
  curl --header "Authorization: token $GIT_TOKEN" --header 'Accept: application/vnd.github.v3.raw' $URL > $HOME/.alfresco-spk/commands/$FILE_NAME
  LOCAL_ENV_VARS="$LOCAL_ENV_VARS,file://$HOME/.alfresco-spk/commands/$FILE_NAME"
done
LOCAL_ENV_VARS=`echo $LOCAL_ENV_VARS | cut -c 2-`
echo "LOCAL ENV VARS: $LOCAL_ENV_VARS"

ENV_VARS=$LOCAL_ENV_VARS,https://raw.githubusercontent.com/Alfresco/alfresco-spk/master/packer/commands/common-env-vars.json
PRE_COMMANDS=https://raw.githubusercontent.com/Alfresco/alfresco-spk/master/packer/commands/prepare-git-release.json$GIT_TOKEN_PARAM
POST_COMMANDS=https://raw.githubusercontent.com/Alfresco/alfresco-spk/master/packer/commands/perform-git-release.json$GIT_TOKEN_PARAM,https://raw.githubusercontent.com/Alfresco/alfresco-spk/master/packer/commands/nexus-upload.json$GIT_TOKEN_PARAM

curl -L --no-sessionid https://github.com/Alfresco/alfresco-spk/raw/vagrant-plugin/plugin/pkg/spk-0.1.0.gem$GIT_TOKEN_PARAM > ~/.alfresco-spk/spk.gem
vagrant plugin install ~/.alfresco-spk/spk.gem

if [[ -n "$SPK_AMI_FILE" && -f "$SPK_AMI_FILE" ]]
then
  echo "Run SPK to build AMI"
  vagrant spk build-images -s file://$PWD/$SPK_AMI_FILE --env-vars $ENV_VARS --pre-commands $PRE_COMMANDS --post-commands $POST_COMMANDS
  # tail -f ~/.alfresco-spk/vagrant/packer/packer.log
fi

if [[ -n "$SPK_VAGRANTBOX_FILE" && -f "$SPK_VAGRANTBOX_FILE" ]]
then
  # Run SPK to build a Vagrant Box (.box)
  vagrant spk build-images -s file://$PWD/$SPK_VAGRANTBOX_FILE --env-vars $ENV_VARS --pre-commands $PRE_COMMANDS --post-commands $POST_COMMANDS
  # tail -f ~/.alfresco-spk/vagrant/packer/packer.log
fi

# Allow to tail packer.log
chmod 644 ~/.alfresco-spk/vagrant/packer/packer.log
```

## Manual upload Vagrant Box to S3
This step should be done by Bamboo, but may be useful to perform it locally for testing purposes.

- Install [awscli](https://aws.amazon.com/cli/):
```
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" ; sudo python get-pip.py
sudo pip install awscli
```
- Define your (AWS QA Account, can find a [copy on pcb-data_bags project](https://github.com/Alfresco/pcb-data_bags/blob/master/data_bags/aws/bamboo_qa.json)) key id and secret:
```
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export KEYPAIR_NAME=
export AWS_KEY_PATH=
```
- Upload .box or .ovf file to S3 bucket (using awscli - with credentials properly configured - or Web Console)
```
aws s3 cp ~/.alfresco-spk/vagrant/packer/image-basic-0.0.1.box s3://packer-vms
```

- (Optional) install the Vagrant box locally (to avoid S3 upload)
```
rm -Rf ~/.vagrant.d/boxes/img-basic-0.0.1
mkdir -p ~/.vagrant.d/boxes/img-basic-0.0.1/0/virtualbox
tar xvzf ~/.alfresco-spk/vagrant/packer/image-basic-0.0.1.box -C ~/.vagrant.d/boxes/img-basic-0.0.1/0/virtualbox
```

## Using Images
Install the following Vagrant plugins:
```
vagrant plugin install vagrant-s3auth
vagrant plugin install vagrant-vbguest
```

### Using Vagrant
If the .box is local:
```
vagrant box add ~/.alfresco-spk/vagrant/packer/img-basic-0.4.16.box --name img-basic-0.4.16
```

If the .box is remote:
```
vagrant box add https://s3.amazonaws.com/packer-vms/img-basic-0.4.16.box --name img-basic-0.4.16
```

You can configure your Vagrantfile as follows:
```
Vagrant.configure("2") do |config|
  config.vm.box = "image-basic-0.4.16"
  config.vm.hostname = "myimagebasic"
  config.vm.network :private_network, ip: "192.168.0.42"
end
```

### Using Kitchen
Simply use the following syntax in your .kitchen.yml
```
platforms:
  - name: img-basic-0.4.16
    driver:
      box: img-basic-0.4.16
      box_url: https://s3.amazonaws.com/packer-vms/img-basic-0.4.16.box
```

If local:
```
box_url: file://Users/mau/.alfresco-spk/vagrant/packer/img-basic-0.4.16.box
```

## Enable cache
You may find that most of your kitchen runs are fetching always the same binaries, wasting time and bandwidth; you can provide a web proxy instance by configuring the following environment variables:
```
export http_proxy=http://localhost:3128
export https_proxy=https://localhost:3128
```

For kitchen runs, you can use [http_proxy configuration](https://gist.github.com/fnichol/7551540); for Vagrant runs, you can use [Vagrant proxyconf plugin](https://github.com/tmatilai/vagrant-proxyconf/).

## File locations
```
~/.alfresco-spk/vagrant/vagrant.log
~/.alfresco-spk/vagrant/packer/packer.log
~/.alfresco-spk/vagrant/packer/image-basic-packer.json
```

## Debugging
Adding `export PACKER_LOG=1`, Packer will run in [debug mode](https://www.packer.io/docs/command-line/build.html), will not kill the environment and will keep ec2_amazon.pem file in the `packer-run` folder; this will allow you to connect to the box using the following command (from the right packer-run sub-folder)

```
ssh -i ec2_amazon.pem centos@ec2-bla-bla-bla.compute-1.amazonaws.com
```
