diff --git a/.gitignore b/.gitignore index 8dc1ec5..d7e7aa1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,10 @@ *.so # Artifacts -utils/packer/*/packer_cache/* -utils/packer/*/output* +utils/packer/packer_cache/* +utils/packer/output* +*.box +*.qcow2 # Packages *.egg* diff --git a/doc/source/packer.rst b/doc/source/packer.rst new file mode 100644 index 0000000..6eb74eb --- /dev/null +++ b/doc/source/packer.rst @@ -0,0 +1,65 @@ +`Packer `_ Templates +=========================================== + +Custom build examples +--------------------- + +Ubuntu build for libvrit +~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: sh + + PACKER_LOG=1 \ + UBUNTU_MAJOR_VERSION=16.04 \ + UBUNTU_MINOR_VERSION=.1 \ + UBUNTU_TYPE=server \ + ARCH=amd64 \ + HEADLESS=true \ + packer build -var 'cpus=2' -var 'memory=2048' -only=qemu ubuntu.json + +Debian build for virtualbox +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: sh + + DEBIAN_MAJOR_VERSION=8 \ + DEBIAN_MINOR_VERSION=5 \ + ARCH=amd64 \ + HEADLESS=true \ + packer build -only=virtualbox-iso debian.json + +Login Credentials +----------------- + +(root password is "vagrant" or is not set ) + +- Username: vagrant +- Password: vagrant + +Manual steps +------------ + +To add a local box into Vagrant, run from the repo root dir: + +.. code:: sh + + vagrant box add --name debian \ + utils/packer/debian-8.5.0-amd64-libvirt.box + + vagrant box add --name ubuntu \ + utils/packer/ubuntu-16.04.1-server-amd64-libvirt.box + +To upload a local box into `Atlas `_, +run from the `./utils/packer` dir: + +.. code:: sh + + VERSION=0.1.0 DEBIAN_MAJOR_VERSION=8 DEBIAN_MINOR_VERSION=5 ARCH=amd64 \ + OSTYPE=debian TYPE=libvirt ATLAS_USER=john NAME=foobox ./deploy.sh + + UBUNTU_MAJOR_VERSION=16.04 UBUNTU_MINOR_VERSION=.1 UBUNTU_TYPE=server \ + ARCH=amd64 OSTYPE=ubuntu TYPE=virtualbox ATLAS_USER=doe ./deploy.sh + +The first command creates a box named `john/foobox` which has the version +`0.1.0` and the libvirt provider. The second one uses the version autoincrement +and puts the box as `john/ubuntu-16.04.1-server-amd64` and virtualbox provider. diff --git a/utils/packer/deploy.sh b/utils/packer/deploy.sh index 56b57a9..b85f0d0 100755 --- a/utils/packer/deploy.sh +++ b/utils/packer/deploy.sh @@ -1,47 +1,74 @@ #!/bin/bash -x +# VERSION=0.1.0 DEBIAN_MAJOR_VERSION=8 DEBIAN_MINOR_VERSION=5 ARCH=amd64 OSTYPE=debian TYPE=libvirt ATLAS_USER=john NAME=foobox ./deploy.sh +# UBUNTU_MAJOR_VERSION=16.04 UBUNTU_MINOR_VERSION=.1 UBUNTU_TYPE=server ARCH=amd64 OSTYPE=ubuntu TYPE=virtualbox ATLAS_USER=doe ./deploy.sh USER=${ATLAS_USER:-mirantis} DEBIAN_MAJOR_VERSION=${DEBIAN_MAJOR_VERSION:-8} DEBIAN_MINOR_VERSION=${DEBIAN_MINOR_VERSION:-5} +UBUNTU_MAJOR_VERSION=${UBUNTU_MAJOR_VERSION:-16.04} +UBUNTU_MINOR_VERSION=${UBUNTU_MINOR_VERSION:-.1} +UBUNTU_TYPE=${UBUNTU_TYPE:-server} ARCH=${ARCH:-amd64} TYPE=${TYPE:-libvirt} -NAME="debian-${DEBIAN_MAJOR_VERSION}.${DEBIAN_MINOR_VERSION}.0-${ARCH}" +VERSION=${VERSION:-0.0.0} +case ${OSTYPE} in + ubuntu) NAME="ubuntu-${UBUNTU_MAJOR_VERSION}${UBUNTU_MINOR_VERSION}-${UBUNTU_TYPE}-${ARCH}" ;; + debian) NAME="debian-${DEBIAN_MAJOR_VERSION}.${DEBIAN_MINOR_VERSION}.0-${ARCH}" ;; + *) echo "Unsupported OSTYPE" >&2; exit 1;; +esac +BOXNAME="${BOXNAME:-${NAME}-${TYPE}.box}" create_atlas_box() { if curl -sSL https://atlas.hashicorp.com/api/v1/box/${USER}/${NAME} | grep -q "Resource not found"; then #Create box, because it doesn't exists echo "*** Creating box: ${NAME}, Short Description: ${SHORT_DESCRIPTION}" + set +x curl -s https://atlas.hashicorp.com/api/v1/boxes -X POST -d box[name]="${NAME}" -d box[short_description]="${SHORT_DESCRIPTION}" -d box[is_private]=false -d access_token="${ATLAS_TOKEN}" + set -x fi } remove_atlas_box() { echo "*** Removing box: ${USER}/${NAME}" + set +x curl -sSL https://atlas.hashicorp.com/api/v1/box/${USER}/${NAME} -X DELETE -d access_token="${ATLAS_TOKEN}" + set -x } remove_atlas_box_version() { echo "*** Removing previous version: https://atlas.hashicorp.com/api/v1/box/$USER/$NAME/version/$1" + set +x curl -s https://atlas.hashicorp.com/api/v1/box/$USER/$NAME/version/$1 -X DELETE -d access_token="$ATLAS_TOKEN" > /dev/null + set -x } upload_boxfile_to_atlas() { echo "*** Getting current version of the box (if exists)" + local VER + set +x local CURRENT_VERSION=$(curl -sS -L https://atlas.hashicorp.com/api/v1/box/${USER}/${NAME} -X GET -d access_token="${ATLAS_TOKEN}" | jq 'if .current_version.version == null then "0" else .current_version.version end | tonumber') - local VERSION=$(echo "${CURRENT_VERSION} + 0.1" | bc | sed 's/^\./0./') - echo "*** Uploading a version: ${VERSION}" - curl -sSL https://atlas.hashicorp.com/api/v1/box/${USER}/${NAME}/versions -X POST -d version[version]="${VERSION}" -d access_token="${ATLAS_TOKEN}" > /dev/null - curl -sSL https://atlas.hashicorp.com/api/v1/box/${USER}/${NAME}/version/${VERSION} -X PUT -d version[description]="${DESCRIPTION}" -d access_token="${ATLAS_TOKEN}" > /dev/null - curl -sSL https://atlas.hashicorp.com/api/v1/box/${USER}/${NAME}/version/${VERSION}/providers -X POST -d provider[name]="${TYPE}" -d access_token="${ATLAS_TOKEN}" > /dev/null - UPLOAD_PATH=$(curl -sS https://atlas.hashicorp.com/api/v1/box/${USER}/${NAME}/version/${VERSION}/provider/${TYPE}/upload?access_token=${ATLAS_TOKEN} | jq -r '.upload_path') - echo "*** Uploding \"${NAME}-${TYPE}.box\" to ${UPLOAD_PATH}" - curl -sSL -X PUT --upload-file ${NAME}-${TYPE}.box ${UPLOAD_PATH} + set -x + if [ "${VERSION}" == "0.0.0" ]; then + VER=$(echo "${CURRENT_VERSION} + 0.1" | bc | sed 's/^\./0./') + else + VER=${VERSION} + fi + echo "*** Uploading a version: ${VER}" + set +x + curl -sSL https://atlas.hashicorp.com/api/v1/box/${USER}/${NAME}/versions -X POST -d version[version]="${VER}" -d access_token="${ATLAS_TOKEN}" > /dev/null + curl -sSL https://atlas.hashicorp.com/api/v1/box/${USER}/${NAME}/version/${VER} -X PUT -d version[description]="${DESCRIPTION}" -d access_token="${ATLAS_TOKEN}" > /dev/null + curl -sSL https://atlas.hashicorp.com/api/v1/box/${USER}/${NAME}/version/${VER}/providers -X POST -d provider[name]="${TYPE}" -d access_token="${ATLAS_TOKEN}" > /dev/null + UPLOAD_PATH=$(curl -sS https://atlas.hashicorp.com/api/v1/box/${USER}/${NAME}/version/${VER}/provider/${TYPE}/upload?access_token=${ATLAS_TOKEN} | jq -r '.upload_path') + set -x + echo "*** Uploading \"${BOXNAME}\" to ${UPLOAD_PATH}" + curl -sSL -X PUT --upload-file ${BOXNAME} ${UPLOAD_PATH} + set +x curl -sSL https://atlas.hashicorp.com/api/v1/box/${USER}/${NAME}/version/${VERSION}/release -X PUT -d access_token="${ATLAS_TOKEN}" > /dev/null + set -x } export DESCRIPTION=$(cat ../../doc/PACKER.md) export SHORT_DESCRIPTION="${NAME} for ${TYPE}" create_atlas_box upload_boxfile_to_atlas - #remove_atlas_box