forked from cloudfoundry/cf-deployment-concourse-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask
executable file
·137 lines (117 loc) · 3.8 KB
/
task
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
set -eux
# shellcheck disable=SC1091
source cf-deployment-concourse-tasks/shared-functions
check_upload_stemcell_params() {
if [ -z "$INFRASTRUCTURE" ]; then
echo "INFRASTRUCTURE has not been set"
exit 1
fi
local supported_infrastructures
supported_infrastructures=("aws" "google" "boshlite" "bosh-lite" "vsphere" "azure")
any_matched=false
for iaas in ${supported_infrastructures[*]}; do
if [ "${INFRASTRUCTURE}" == "${iaas}" ]; then
any_matched=true
break
fi
done
if [ "$any_matched" = false ]; then
echo "${INFRASTRUCTURE} is not supported; please choose a value from ${supported_infrastructures[*]}"
exit 1
fi
}
verify_ops_file_modifies_stemcell_section() {
local ops_file
ops_file=$1
set +e
if ! grep 'path: /stemcells/' ops-files/${ops_file} > /dev/null; then
echo "Error: Could not find any stemcells to be uploaded from ${ops_file} ops file."
exit 1
fi
set -e
}
upload_stemcell() {
# Read potentially variable stemcell paramaters out of cf-deployment with bosh
local os
os=$1
local version
version=$2
# Hardcode a couple of stable stemcell paramaters
local stemcells_url
stemcells_url="https://bosh.io/d/stemcells"
# Ask bosh if it already has our OS / version stemcell combination
# As of this writing, the new bosh cli doesn't have --skip-if-exists
set +e
local existing_stemcell
existing_stemcell=$(bosh stemcells | grep "${os}" | awk '{print $2}' | tr -d "\*" | grep ^"${version}"$ )
set -e
local stemcell_name
if [ "$INFRASTRUCTURE" = "aws" ]; then
stemcell_name="bosh-aws-xen-hvm"
elif [ "$INFRASTRUCTURE" = "google" ]; then
stemcell_name="bosh-google-kvm"
elif [ "$INFRASTRUCTURE" = "boshlite" ]; then
stemcell_name="bosh-warden-boshlite"
elif [ "$INFRASTRUCTURE" = "bosh-lite" ]; then
stemcell_name="bosh-warden-boshlite"
elif [ "$INFRASTRUCTURE" = "vsphere" ]; then
stemcell_name="bosh-vsphere-esxi"
elif [ "$INFRASTRUCTURE" = "azure" ]; then
stemcell_name="bosh-azure-hyperv"
fi
stemcell_name="${stemcell_name}-${os}-go_agent"
if [ "$version" = "latest" ]; then
full_stemcell_url="${stemcells_url}/${stemcell_name}"
else
full_stemcell_url="${stemcells_url}/${stemcell_name}?v=${version}"
fi
# If bosh already has our stemcell, exit 0
if [ "${existing_stemcell}" ]; then
echo "Task bosh-upload-stemcell-from-cf-deployment:"
echo "Stemcell '${stemcell_name}/${version}' already exists. Skipping..."
return
fi
# If bosh.io doesn't have our stemcell, exit 2
set +e
local stemcell_exists
wget -S --spider "${full_stemcell_url}" > /dev/null 2>&1
stemcell_exists=$?
if [ ${stemcell_exists} -ne 0 ]; then
echo "Error: Could not find a '$os' stemcell for IaaS '$INFRASTRUCTURE' on bosh.io. Please double-check that the IaaS/OS combination is supported."
exit 2 #POSIX 'No such file or directory'
fi
set -e
# ... otherwise, begin the upload process
bosh \
-n \
upload-stemcell \
"${full_stemcell_url}"
}
function upload_stemcells() {
local arguments=''
for op in ${OPS_FILES}
do
verify_ops_file_modifies_stemcell_section ${op}
arguments="${arguments} -o ops-files/${op}"
done
bosh interpolate ${arguments} cf-deployment/${MANIFEST_FILE} > /tmp/cf.yml
local stemcells_json=$(ruby -rjson -ryaml -e "puts YAML.load_file('/tmp/cf.yml').to_json" | jq .stemcells)
local size=$(echo ${stemcells_json} | jq 'length')
if [ ${size} -eq 0 ]; then
echo "Error: Did not find any stemcells to upload."
exit 1
fi
for i in `seq 0 $((size - 1))`
do
local os=$(echo ${stemcells_json} | jq -r .[$i].os)
local version=$(echo ${stemcells_json} | jq -r .[$i].version)
upload_stemcell ${os} ${version}
done
}
main() {
check_upload_stemcell_params
setup_bosh_env_vars
upload_stemcells
}
main