-
Notifications
You must be signed in to change notification settings - Fork 19
/
push-and-release
executable file
·231 lines (196 loc) · 6.88 KB
/
push-and-release
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/bin/bash -e
#
# Push and release a SINGLE MULTISERIES CHARM to the charm store,
# from a local checkout dir.
# - Charm series metadata is required.
# - Take no action if env var CHARM_PUSH_DRY_RUN is True.
# - Push to channel if 3rd param channel is passed
# - Listen for Jenkins $WORKSPACE env var and use it if found.
channel="$3"
branch="$2"
charm="$1"
usage="usage: push-and-release charmname <master||stable||stable/nn.nn> <channel>
resource_string=""
NOTE:
- Now enforcing series declarations in metadata.yaml. All charms must
declare at least one series.
- channel must either be missing, or one of stable, candidate, beta, edge.
If missing, then the channel is stable.
"
function add_repo_info () {
local charm_dir="${1}"
echo " + Generating repo info and adding as $charm_dir/repo-info"
./generate-repo-info $charm_dir > $charm_dir/repo-info
echo "$(cat $charm_dir/repo-info | sed -e 's/^/ /')"
}
if [ -z "$charm" ] || [ -z "$branch" ]; then
echo -e "The charmname and branch params must be present."
echo -e "$usage"
exit 1
fi
# Override charm dir when driven by Jenkins.
# Otherwise, use cmd param provided.
if [ -n "$WORKSPACE" ]; then
# Driven by Jenkins.
charm_dir="$WORKSPACE/$charm"
else
# Expect a local checkout in the current dir.
charm_dir="charms/$charm"
fi
if [ ! -d $charm_dir ]; then
echo "$charm_dir dir does not exist, cannot push."
exit 1
fi
# Build if necessary, use build dir if already built.
if [[ -n "$BUILT_ASSET_DIR" ]]; then
echo " . $charm is a built charm asset"
charm_dir="$BUILT_ASSET_DIR"
built_charm="$BUILT_ASSET_DIR"
elif grep "^\[testenv:build\]$" $charm_dir/tox.ini &> /dev/null &&\
[[ ! -f "$charm_dir/.build.manifest" ]] &&\
[[ ! -f "$charm_dir/src/.build.manifest" ]]; then
# Build!
echo " . $charm ($charm_dir) needs to build before pushing or releasing"
echo -e "\n*$charm $branch*"
add_repo_info $charm_dir
# Source it to preserve BUILT_ASSET_DIR, etc.
. build-charm $charm_dir
if [[ -n "$BUILT_ASSET_FILE" ]]; then
# The post build checks inspect the charm so explode the built charm
# so checks can inspect it.
charm_dir=$(mktemp -d)
unzip -qq $BUILT_ASSET_FILE -d $charm_dir
# When we switch to using 'charmcraft upload' built_charm should be
# switched to point at BUILT_ASSET_FILE but until then need to point
# the pusher at the exploded charm as 'charm push' does not support
# .charm files.
built_charm=$charm_dir
elif [[ -n "$BUILT_ASSET_DIR" ]]; then
mv $charm_dir/repo-info $BUILT_ASSET_DIR
built_charm=$BUILT_ASSET_DIR
charm_dir=$BUILT_ASSET_DIR
else
echo " ! Built charm asset dir invalid"
exit 1
fi
else
echo " . $charm ($charm_dir) does not need to build before pushing or releasing"
add_repo_info $charm_dir
built_charm="$charm_dir"
fi
# Locate metadata.yaml for charm
metadata=$(find $charm_dir -name "metadata.yaml")
echo " . charm metadata file: $metadata"
# Check that series metadata exists
if ! grep '^\"\?series\"\?:$' $metadata &> /dev/null; then
echo " ! $charm does not declare series in metadata (required)."
exit 1
fi
# Set charm store URL based on branch value passed
case "$branch" in
master)
charm_store_url="~openstack-charmers-next/${charm}"
;;
stable|stable/*)
charm_store_url="~openstack-charmers/${charm}"
;;
*)
echo " ! Unable to determine master or stable when setting charm store URL"
echo $usage
exit 1
;;
esac
# Check the channel parameter; if blank then no channel, spec, otherwise match
# it to stable, candidate, beta, or edge.
channel_lc="${channel,,}"
case "$channel_lc" in
stable|"")
channel_spec=""
channel_lc="stable"
;;
candidate|beta|edge)
channel_spec="--channel=$channel_lc"
;;
*)
echo " ! channel "$channel" is not one of stable, candidate, beta, edge."
echo $usage
exit 1
;;
esac
# Wrapper to deal with potential charm store timeouts
retry_command() {
command=$@
if [ "${CHARM_PUSH_DRY_RUN^^}" == "TRUE" ]; then
echo "url $charm DRY RUN for: $command"
command=":"
fi
i=0
attempts=10
while [ $i -lt $attempts ]; do
set -x
$command && break
set +x
let "i+=1"
[ $i -ge $attempts ] && exit 1
done
set +x
}
echo " + Pushing $charm_store_url from path: $built_charm"
charm_push="$(retry_command charm push $built_charm $charm_store_url)"
echo $charm_push
echo " . Checking charm ref"
charm_ref="$(echo $charm_push | grep -m 1 url | awk '{ print $2 }')"
echo $charm_ref
if [ -z "$charm_ref" ]; then
echo " ! Failed to push charm to charm-store"
exit 1
fi
# Collect charm_ref(s) in tmp file if available. This is used by OSCI notifications.
cs_refs_released="$(mktemp)"
echo $charm_ref >> $cs_refs_released
# Optionally attach an existing resource prior to charm release
if [[ -n $(awk '$0~c' c="$charm:$branch" resources.txt) ]]; then
resource=$(awk '$0~c' c="$charm:$branch" resources.txt | awk -F':' '{ print $4 }')
resource_string=""
for res in $resource; do
resource_string="$resource_string --resource $res"
done
mkdir -p resources/
resource_ext=$(awk '$0~c' c="$charm:$branch" resources.txt | awk -F':' '{ print $5 }')
resource_file="resources/resource.${resource_ext}"
touch "${resource_file}"
resource_raw=$(awk '$0~c' c="$charm:$branch" resources.txt | awk -F':' '{ print $3 }')
for res_raw in $resource_raw; do
if ! charm pull-resource ${charm_ref} ${res_raw}; then
retry_command charm attach ${charm_ref} ${res_raw}=${resource_file}
fi
done
echo " . Using resource $resource (${resource_raw})"
fi
echo " . Releasing charm $charm_ref to channel $channel_lc"
retry_command charm release $charm_ref $resource_string $channel_spec
echo " . Granting global read acl"
retry_command charm grant $charm_store_url $channel_spec --acl read everyone
echo " . Setting charm homepage and bugs-url options"
# Handle bug url overrides if present
if grep "^$charm|" bug-link-overrides.txt; then
BUGS_URL=$(grep "^$charm|" bug-link-overrides.txt | cut -f 2 -d "|")
else
BUGS_URL="https://bugs.launchpad.net/charm-${charm}/+filebug"
fi
# Handle repo url overrides if present
if grep "^$charm|" repo-link-overrides.txt > /dev/null; then
REPO_URL=$(grep "^$charm|" repo-link-overrides.txt | cut -f 2 -d "|")
else
REPO_URL="https://opendev.org/openstack/charm-${charm}"
fi
retry_command charm set $charm_store_url bugs-url=$BUGS_URL \
homepage=$REPO_URL
echo " . Charm store refs released:"
cat $cs_refs_released
# Save cs refs artifact if driven by Jenkins. Otherwise, clean up cs refs temp file.
if [[ -n "$WORKSPACE" ]]; then
mv -v $cs_refs_released $WORKSPACE/cs_refs_released.txt
else
rm -fv $cs_refs_released
fi