This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathpublishcv.sh
executable file
·142 lines (123 loc) · 5.99 KB
/
publishcv.sh
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
#!/bin/bash -x
# Publish the content view(s) and promote if necessary
#
# This script can be tested /used with a command like:
# PUSH_USER=jenkins SATELLITE=sat6.example.com \
# RSA_ID=/var/lib/jenkins/.ssh/id_rsa TESTVM_ENV=2 CV="cv-acme-soe-demo" \
# CV_PASSIVE_LIST="cv-passive-1,cv-passive-2" ORG=Default_Organization \
# CCV_NAME_PATTERN="ccv-test-*" BUILD_URL=$$ ./publishcv.sh
# Load common parameter variables
. $(dirname "${0}")/common.sh
# has anything changed? If yes, then MODIFIED_CONTENT_FILE is not 0 bytes
if [[ ! -s "${MODIFIED_CONTENT_FILE}" ]]
then
echo "No entries in ${MODIFIED_CONTENT_FILE} no need to continue with $0"
exit 0
fi
# Create an array from all the content view names
oldIFS="${IFS}"
i=0
IFS=','
for cv in ${CV} ${CV_PASSIVE_LIST}
do
CV_LIST[$i]="${cv}"
((i++))
done
IFS="${oldIFS}"
# Get a list of all CV version IDs
for cv in "${CV_LIST[@]}"
do
inform "Publish CV ${cv}"
ssh -q -l ${PUSH_USER} -i ${RSA_ID} ${SATELLITE} \
"hammer content-view publish --name \"${cv}\" --organization \"${ORG}\" --description \"Build ${BUILD_ID} of Job ${JOB_NAME} on ${JENKINS_URL}\"" || \
{ err "Content view '${cv}' couldn't be published."; exit 1; }
# get the latest version of each CV, add it to the array
inform "Get the latest version of CV ${cv}"
VER_ID_LIST+=( "$(ssh -q -l ${PUSH_USER} -i ${RSA_ID} ${SATELLITE} \
"hammer content-view info --name \"${cv}\" --organization \"${ORG}\" \
| sed -n \"/Versions:/,/Components:/p\" | grep \"ID:\" | tr -d ' ' | cut -f2 -d ':' | sort -n | tail -n 1")" )
done
# sleep after publishing content view to give chance for locks to get cleared up
inform "Give Satellite 90 seconds to settle WRT Content View locks"
sleep 90
if [[ -n ${CCV_NAME_PATTERN} ]]
then # we want to update and publish all CCVs containing our CVs
inform "Analysing CCVs"
# Create a sed script to replace old with new version ID
for (( i = 0; i < ${#CV_LIST[@]}; i++ ))
do
cv=${CV_LIST[$i]}
ver_id=${VER_ID_LIST[$i]}
CV_VER_SED+="$(ssh -q -l ${PUSH_USER} -i ${RSA_ID} ${SATELLITE} \
"hammer --csv content-view version list --content-view \"${cv}\" --organization \"${ORG}\"" | awk -F',' -v ver_id="${ver_id}" '
$1 != "ID" && $1 != ver_id {ids="s/," $1 ",/," ver_id ",/;" ids}
END {print ids}')"
done
# Create an array of composite content view IDs matching the given pattern
CCV_TMP_IDS=( $(ssh -q -l ${PUSH_USER} -i ${RSA_ID} ${SATELLITE} \
"hammer --csv content-view list --organization \"${ORG}\" --search \"${CCV_NAME_PATTERN}\"" | awk -F, '$1 ~ /^[0-9]+$/ {print $1}') )
# We need at the same time to find out which of the CCVs use the given CV
# as well as keep the ID of all other used CV versions, but filter out
# the currently used version of our CV, to avoid two versions of the same CV
for ccv_id in "${CCV_TMP_IDS[@]}"
do
cv_tmp_ver_ids=$(ssh -q -l ${PUSH_USER} -i ${RSA_ID} ${SATELLITE} \
"hammer --output yaml content-view info --id ${ccv_id} --organization \"${ORG}\"" \
| awk -F': *' '
$1 == "Components" {cmp = 1; next}
{if (!cmp) next}
/^[A-Z]/ {cmp = 0; next}
$1 ~ / *ID/ { ids=$2 "," ids; }
END {print "," ids}')
cv_used_ver_ids=$(echo "${cv_tmp_ver_ids}" | sed "${CV_VER_SED}")
if [[ "${cv_used_ver_ids}" != "${cv_tmp_ver_ids}" ]]
then # the CCV really uses one of the given CVs in any version
CCV_IDS+=( ${ccv_id} )
CV_USED_VER_IDS+=( "${cv_used_ver_ids}" )
fi
done
fi
# We update the found CCVs with the new version and publish them
if [[ ${#CCV_IDS[*]} -gt 0 ]]
then # there is at least one CCV using the given CVs
for (( i = 0; i < ${#CCV_IDS[@]}; i++ ))
do
ccv_id=${CCV_IDS[$i]}
cv_used_ver_ids=${CV_USED_VER_IDS[$i]}
# we add back the CV under its latest version to the CCV
inform "Update component IDs of CCV ${ccv_id}"
ssh -q -l ${PUSH_USER} -i ${RSA_ID} ${SATELLITE} \
"hammer content-view update --id ${ccv_id} --component-ids \"${cv_used_ver_ids}\" --organization \"${ORG}\"" || \
{ err "CCV '${ccv_id}' couldn't be updated with '${cv_used_ver_ids}'."; exit 1; }
# sleep after updating CV for locks to get cleared up
inform "Give Satellite 10 seconds to settle WRT Content View locks"
sleep 10
# And then we publish the updated CCV
inform "Publish CCV ${ccv_id}"
ssh -q -l ${PUSH_USER} -i ${RSA_ID} ${SATELLITE} \
"hammer content-view publish --id \"${ccv_id}\" --organization \"${ORG}\" --description \"Build ${BUILD_ID} of Job ${JOB_NAME} on ${JENKINS_URL}\"" || \
{ err "CCV '${ccv_id}' couldn't be published."; exit 1; }
done
fi
if [[ -n ${TESTVM_ENV} ]]
then
for (( i = 0; i < ${#CV_LIST[@]}; i++ ))
do # promote the latest version of each CV
cv=${CV_LIST[$i]}
ver_id=${VER_ID_LIST[$i]}
inform "Promoting version ${ver_id} of ${cv} to LCE ${TESTVM_ENV}"
ssh -q -l ${PUSH_USER} -i ${RSA_ID} ${SATELLITE} \
"hammer content-view version promote --content-view \"${cv}\" --organization \"${ORG}\" \
--to-lifecycle-environment-id \"${TESTVM_ENV}\" --force --id ${ver_id}"
done
# we also promote the latest version of each CCV
for ccv_id in ${CCV_IDS[@]}
do
ccv_ver=$(ssh -q -l ${PUSH_USER} -i ${RSA_ID} ${SATELLITE} \
"hammer --csv content-view version list --content-view-id ${ccv_id} --organization \"${ORG}\"" | awk -F',' '$1 ~ /^[0-9]+$/ {if ($3 > maxver) {maxver = $3; maxid = $1} } END {print maxid}')
inform "Promoting version ${ccv_ver} of CCV ID ${ccv_id} to LCE ${TESTVM_ENV}"
ssh -q -l ${PUSH_USER} -i ${RSA_ID} ${SATELLITE} \
"hammer content-view version promote --content-view-id \"${ccv_id}\" --organization \"${ORG}\" \
--to-lifecycle-environment-id \"${TESTVM_ENV}\" --force --id ${ccv_ver}"
done
fi