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

637 by Daniel Watkins
Add script to register Vagrant versions with Hashicorp's Atlas.
1
#!/bin/bash
2
3
set -eu
4
786.1.2 by Daniel Watkins
Update API_ROOT in register-vagrant-version.sh
5
API_ROOT="https://vagrantcloud.com/api/v1"
637 by Daniel Watkins
Add script to register Vagrant versions with Hashicorp's Atlas.
6
BOX_OWNER=${BOX_OWNER:-"ubuntu"}
7
8
SUITE="$1"
9
SERIAL="$2"
10
ARCH="$3"
11
IMAGE_TYPE="$4"
12
ACCESS_TOKEN="$5"
13
14
# Always point to the current image, so we don't run in to a problem with
15
# versions being removed in the future
743.1.1 by Daniel Watkins
Handle changed Vagrant prefix for xenial and later
16
case $SUITE in
786.1.1 by Daniel Watkins
Drop references to no-longer-supported releases in register-vagrant-version.sh
17
    trusty)
795.1.1 by Daniel Watkins
Use HTTPS for Vagrant box redirects (LP: #1754948)
18
        BOX_URL_PREFIX="https://cloud-images.ubuntu.com/vagrant/${SUITE}/current/${SUITE}-server-cloudimg-${ARCH}"
743.1.1 by Daniel Watkins
Handle changed Vagrant prefix for xenial and later
19
        BOX_URL_POSTFIX="-vagrant-disk1.box"
20
        ;;
21
    *)
795.1.1 by Daniel Watkins
Use HTTPS for Vagrant box redirects (LP: #1754948)
22
        BOX_URL_PREFIX="https://cloud-images.ubuntu.com/${SUITE}/current/${SUITE}-server-cloudimg-${ARCH}"
743.1.1 by Daniel Watkins
Handle changed Vagrant prefix for xenial and later
23
        BOX_URL_POSTFIX="-vagrant.box"
743.1.2 by Daniel Watkins
Fail obviously when trying to publish a Juju box on 16.04 (and later)
24
        if [ "${IMAGE_TYPE}" = "juju" ]; then
25
            echo "This script does not (yet) support Juju boxes on Xenial and later"
26
            exit 1
27
        fi
743.1.1 by Daniel Watkins
Handle changed Vagrant prefix for xenial and later
28
        ;;
29
esac
637 by Daniel Watkins
Add script to register Vagrant versions with Hashicorp's Atlas.
30
31
if [ "${ARCH}" = "amd64" ]; then
32
    BOX_NAME="${SUITE}64"
33
elif [ "${ARCH}" = "i386" ]; then
34
    BOX_NAME="${SUITE}32"
35
else
36
    echo "Unknown arch: ${ARCH}"
37
    exit 1
38
fi
39
40
if [ "${IMAGE_TYPE}" = "juju" ]; then
41
    BOX_URL="${BOX_URL_PREFIX}-juju${BOX_URL_POSTFIX}"
42
    BOX_NAME="${BOX_NAME}-juju"
43
elif [ "${IMAGE_TYPE}" = "default" ]; then
44
    BOX_URL="${BOX_URL_PREFIX}${BOX_URL_POSTFIX}"
45
else
46
    echo "Unknown image type: ${IMAGE_TYPE}"
47
    exit 1
48
fi
49
50
# Atlas requires versions of the form X.Y.Z where X, Y and Z are all integers
51
if [[ ${SERIAL} == *"."* ]]; then
52
    VERSION="${SERIAL}.0"
53
else
54
    VERSION="${SERIAL}.0.0"
55
fi
56
57
make_api_call () {
58
    echo "Making API call:" $@
786.1.3 by Daniel Watkins
register-vagrant-version.sh curl calls should follow redirects
59
    curl -L -f $@ -d "access_token=${ACCESS_TOKEN}" | python -m json.tool
637 by Daniel Watkins
Add script to register Vagrant versions with Hashicorp's Atlas.
60
}
61
786.1.3 by Daniel Watkins
register-vagrant-version.sh curl calls should follow redirects
62
if curl -L -f "${API_ROOT}/box/${BOX_OWNER}/${BOX_NAME}"; then
741.1.1 by Daniel Watkins
Register a box if it doesn't already exist
63
    echo "Box ${BOX_NAME} already registered..."
64
else
65
    echo "Registering ${BOX_NAME}..."
66
    make_api_call \
67
        "${API_ROOT}/boxes" \
68
        -X POST \
744.1.1 by Daniel Watkins
Create new Vagrant boxes with explicit owner
69
        -d "box[name]=${BOX_NAME}" \
70
        -d "box[username]=${BOX_OWNER}"
741.1.1 by Daniel Watkins
Register a box if it doesn't already exist
71
    make_api_call \
72
        "${API_ROOT}/box/${BOX_OWNER}/${BOX_NAME}" \
73
        -X PUT \
74
        -d "box[is_private]=false"
75
fi
76
637 by Daniel Watkins
Add script to register Vagrant versions with Hashicorp's Atlas.
77
echo "Check if the version already registered..."
641 by Daniel Watkins
Use the last part of the version as a Vagrant revision.
78
while true; do
786.1.3 by Daniel Watkins
register-vagrant-version.sh curl calls should follow redirects
79
    if curl -L -f "${API_ROOT}/box/${BOX_OWNER}/${BOX_NAME}/version/${VERSION}"; then
641 by Daniel Watkins
Use the last part of the version as a Vagrant revision.
80
        echo "Version ${VERSION} already registered; trying a higher revision."
81
        VERSION="$(echo "$VERSION" | awk -F. -v OFS=. '{printf("%s.%s.%s\n", $1, $2, ++$3)}')"
82
    else
83
        break
84
    fi
85
done
86
echo "Using version: ${VERSION}"
637 by Daniel Watkins
Add script to register Vagrant versions with Hashicorp's Atlas.
87
88
echo "Create the version..."
89
make_api_call \
90
    "${API_ROOT}/box/${BOX_OWNER}/${BOX_NAME}/versions" \
91
    -X POST \
92
    -d "version[version]=${VERSION}"
93
echo "Version created."
94
95
echo "Add the virtualbox provider to the version..."
96
make_api_call \
97
    "${API_ROOT}/box/${BOX_OWNER}/${BOX_NAME}/version/${VERSION}/providers" \
98
    -X POST \
99
    -d "provider[name]=virtualbox" \
100
    -d "provider[url]=${BOX_URL}"
101
echo "Virtualbox provider added."
102
103
echo "Release the version..."
104
make_api_call \
105
    "${API_ROOT}/box/${BOX_OWNER}/${BOX_NAME}/version/${VERSION}/release" \
106
    -X PUT
107
echo "Version released."