~ubuntu-on-ec2/vmbuilder/jenkins_kvm-add-azure-cc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash

set -eu

API_ROOT="https://vagrantcloud.com/api/v1"
BOX_OWNER=${BOX_OWNER:-"ubuntu"}

SUITE="$1"
SERIAL="$2"
ARCH="$3"
IMAGE_TYPE="$4"
ACCESS_TOKEN="$5"

# Always point to the current image, so we don't run in to a problem with
# versions being removed in the future
case $SUITE in
    trusty)
        BOX_URL_PREFIX="https://cloud-images.ubuntu.com/vagrant/${SUITE}/current/${SUITE}-server-cloudimg-${ARCH}"
        BOX_URL_POSTFIX="-vagrant-disk1.box"
        ;;
    *)
        BOX_URL_PREFIX="https://cloud-images.ubuntu.com/${SUITE}/current/${SUITE}-server-cloudimg-${ARCH}"
        BOX_URL_POSTFIX="-vagrant.box"
        if [ "${IMAGE_TYPE}" = "juju" ]; then
            echo "This script does not (yet) support Juju boxes on Xenial and later"
            exit 1
        fi
        ;;
esac

if [ "${ARCH}" = "amd64" ]; then
    BOX_NAME="${SUITE}64"
elif [ "${ARCH}" = "i386" ]; then
    BOX_NAME="${SUITE}32"
else
    echo "Unknown arch: ${ARCH}"
    exit 1
fi

if [ "${IMAGE_TYPE}" = "juju" ]; then
    BOX_URL="${BOX_URL_PREFIX}-juju${BOX_URL_POSTFIX}"
    BOX_NAME="${BOX_NAME}-juju"
elif [ "${IMAGE_TYPE}" = "default" ]; then
    BOX_URL="${BOX_URL_PREFIX}${BOX_URL_POSTFIX}"
else
    echo "Unknown image type: ${IMAGE_TYPE}"
    exit 1
fi

# Atlas requires versions of the form X.Y.Z where X, Y and Z are all integers
if [[ ${SERIAL} == *"."* ]]; then
    VERSION="${SERIAL}.0"
else
    VERSION="${SERIAL}.0.0"
fi

make_api_call () {
    echo "Making API call:" $@
    curl -L -f $@ -d "access_token=${ACCESS_TOKEN}" | python -m json.tool
}

if curl -L -f "${API_ROOT}/box/${BOX_OWNER}/${BOX_NAME}"; then
    echo "Box ${BOX_NAME} already registered..."
else
    echo "Registering ${BOX_NAME}..."
    make_api_call \
        "${API_ROOT}/boxes" \
        -X POST \
        -d "box[name]=${BOX_NAME}" \
        -d "box[username]=${BOX_OWNER}"
    make_api_call \
        "${API_ROOT}/box/${BOX_OWNER}/${BOX_NAME}" \
        -X PUT \
        -d "box[is_private]=false"
fi

echo "Check if the version already registered..."
while true; do
    if curl -L -f "${API_ROOT}/box/${BOX_OWNER}/${BOX_NAME}/version/${VERSION}"; then
        echo "Version ${VERSION} already registered; trying a higher revision."
        VERSION="$(echo "$VERSION" | awk -F. -v OFS=. '{printf("%s.%s.%s\n", $1, $2, ++$3)}')"
    else
        break
    fi
done
echo "Using version: ${VERSION}"

echo "Create the version..."
make_api_call \
    "${API_ROOT}/box/${BOX_OWNER}/${BOX_NAME}/versions" \
    -X POST \
    -d "version[version]=${VERSION}"
echo "Version created."

echo "Add the virtualbox provider to the version..."
make_api_call \
    "${API_ROOT}/box/${BOX_OWNER}/${BOX_NAME}/version/${VERSION}/providers" \
    -X POST \
    -d "provider[name]=virtualbox" \
    -d "provider[url]=${BOX_URL}"
echo "Virtualbox provider added."

echo "Release the version..."
make_api_call \
    "${API_ROOT}/box/${BOX_OWNER}/${BOX_NAME}/version/${VERSION}/release" \
    -X PUT
echo "Version released."