forked from altlinuxteam/alt-packer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
publish_vm
executable file
·117 lines (92 loc) · 2.9 KB
/
publish_vm
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
108
109
110
111
112
113
114
115
116
#! /bin/sh
set -euo pipefail
# This script publishes recently built Vagrant Box located in
# `./results` directory on Vagrant Cloud.
TOKEN=${VAGRANTCLOUD_TOKEN}
orgname=${orgname:-BaseALT}
target=${target:-alt-server}
arch=${arch:-x86_64}
BASE_VERSION=${BASE_VERSION:-9}
TARGET_VERSION=${TARGET_VERSION:-9}
VM_TYPE=${VM_TYPE:-qemu}
# Vagrant Cloud forbade underscores in box names
vagrant_arch="${arch}"
if test "${vagrant_arch}" == "x86_64"; then
vagrant_arch="amd64"
fi
# Path to Vagrant box configuration file
box_config="./config/${target}-${BASE_VERSION}-${arch}.json"
vagrantcloud_path="${orgname}/${target}-${TARGET_VERSION}-${vagrant_arch}"
# Check that the variable is set to correct value
# $1 - variable name
# $2 - variable value
# $* - list of all possible values
assert_value() {
variable_name="${1}"
variable_value="${2}"
shift 2
possible_values="$*"
comparison_success=false
echo "CHECKING: ${variable_name}, must be one of: ${possible_values}"
for pval in ${possible_values}; do
if test "${variable_value}" == "${pval}"; then
comparison_success=true
break
fi
done
if test "${comparison_success}" == "true"; then
echo "CHECKING: ${variable_name}, SUCCESS"
return 0
else
echo "CHECKING: ${variable_name}, FAIL"
return 1
fi
}
# Free any used resources on errors
cleanup() {
echo "Logging out of Vagrant cloud"
vagrant cloud auth logout
}
# Check if specific image version is already present on Vagrantup so
# we won't overwrite it.
check_version_present() {
repo_version=$(jq -r '.image_version' ${box_config})
box_info=$(vagrant cloud box show "${vagrantcloud_path}")
remote_version=$(echo "${box_info}" | awk '/current_version/ {print $2} /Current Version/ { print $3 }')
if test "${repo_version}" == "${remote_version}"; then
echo "Box version ${remote_version} is already present on Vagrantup"
exit 2
fi
}
main() {
assert_value target ${target} alt-server alt-workstation alt-kworkstation
assert_value arch ${arch} i586 x86_64
assert_value BASE_VERSION ${BASE_VERSION} '8.2' '8.3' '8sp' '8.1sp' '9' '9.1' 'sisyphus'
assert_value TARGET_VERSION ${TARGET_VERSION} '8.2' '8.3' '8sp' '8.1sp' '9' '9.1' 'sisyphus'
assert_value VM_TYPE ${VM_TYPE} qemu onebula vbox
if test "${BASE_VERSION}" == "sisyphus"; then
TARGET_VERSION="sisyphus"
fi
box_file=results/${VM_TYPE}-${target}-${TARGET_VERSION}-${arch}.box
provider="${VM_TYPE}"
if test "${VM_TYPE}" == "vbox"; then
provider="virtualbox"
fi
if test "${VM_TYPE}" == "onebula"; then
echo "OpenNebula provider is not supported by Vagrant Cloud"
exit 1
fi
assert_value provider ${provider} virtualbox qemu libvirt
vagrant cloud auth login --token "${TOKEN}"
# Check that publish won't overwrite the box.
check_version_present
vagrant cloud publish \
${vagrantcloud_path} \
`jq -r '.image_version' ${box_config}` \
${provider} \
${box_file} \
-f
}
# Resource cleanup handler
trap cleanup EXIT INT HUP
main