/
loadResources
executable file
·140 lines (110 loc) · 6.53 KB
/
loadResources
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
#!/bin/bash
# Load all of 1 type of resource into the Horizon Exchange, which had previously been dumped with a command like: hzn exchange <resource-type> list -l
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
cat << EndOfMessage
Usage: $(basename "$0") <json-file>
Publishes the Horizon Exchange resources contained in <json-file>, or from stdin if omitted. Typically the json comes from a command like 'hzn exchange <resource-type> list -l'. This enables you to dump 1 or all of the Exchange resources of a given type, and then load them into either another Exchange org, or into a different instance of Horizon. This can also be used to essentially copy resources from one version of Horizon to another when doing a full reinstall, instead of an upgrade. Before publishing, $(basename "$0") will process the json to: remove fields that are included in the listing, but can not be included when publishing (e.g. the lastUpdated field), and to remove the orginal org so it will be published in the org specfied by HZN_ORG_ID.
Limitations: currently only supports loading services (but not service policies yet), patterns, and deployment policies.
Environment variables: because $(basename "$0") uses the 'hzn exchange ...' command to publish the resources, all of the same environment variables are supported.
Examples:
Copy 1 pattern from the IBM org to your org:
hzn exchange pattern list IBM/pattern-ibm.helloworld | $(basename "$0")
Copy all patterns from the IBM org to your org:
hzn exchange pattern list IBM/ -l | $(basename "$0")
Copy all services from the IBM org to your org:
hzn exchange service list IBM/ -l | $(basename "$0")
Copy all of your deployment policies from 1 instance of Horizon to another:
hzn exchange deployment listpolicy -l > ourPolicies.json
# move to (or install) another instance of Horizon, then
$(basename "$0") ourPolicies.json
EndOfMessage
exit
fi
#TODOS:
# - we can't currently support loading service policies because the exchange and hzn don't support listing the policies
# for all services at once.
# - we can't currently support loading nodes, because they each need a token. If you are exchange root when listing
# the nodes, the bcrypt of the token is returned. But we would have to modify the exchange PUT /orgs/{orgid}/nodes/{id}
# to optionally accept already bcrypt'd tokens.
# Confirm they have hzn and jq installed
if ! which hzn >/dev/null || ! which jq >/dev/null; then
echo "Error: the hzn and jq commands are required. Install them before continuing."
exit 2
fi
checkexitcode() { # check the previous cmds exit code. Invoke like this: checkexitcode $? 'reading the specified json'
if [[ $1 == 0 ]]; then return; fi
echo "Error: exit code $1 when $2"
exit $1
}
jsonfile="$1" # if omitted, we read from stdin
json=$(jq -c . $jsonfile) # get the json and check its syntax
checkexitcode $? 'reading the specified json'
if [[ -z "$json" ]]; then
echo "Error: json input is empty"
exit 2
fi
# Try to auto-detect which type of resource these are
if jq -e '.[].services' <<< "$json" >/dev/null; then
resType='pattern'
elif jq -e '.[].service' <<< "$json" >/dev/null; then
resType='deployment-policy'
elif jq -e '.[].sharable' <<< "$json" >/dev/null; then
resType='service'
else
echo "Error: can not determine resource type in the specified json. Allowable types: services, patterns, deployment policies"
exit 2
fi
echo "Determined the json contains resources of type $resType"
# Add the keys to an array since the key names may contain spaces
keys_list=$(jq -r 'to_entries[] | .key' <<< $json)
keys=()
while read key; do
keys+=( "${key}" )
done <<< $keys_list
for k_tmp in "${keys[@]}"; do
# Have to strip off the " characters
k=$(echo "${k_tmp}" | sed 's/"//g')
name=${k#*/} # strip the org from the beginning of the key
resourceJson=$(jq -c ".[\"$k\"] | del(.owner) | del(.lastUpdated) | .public = false" <<< $json)
checkexitcode $? "getting resource $k from the specified json"
oldOrgId=${k%%/*}
## Some fields of the resource might have a reference to the source org id. If the org id has changed and if the org name is not IBM, change the reference
if [ "${oldOrgId}" != "IBM" ] && [ "${oldOrgId}" != "${HZN_ORG_ID}" ]; then
if [ "$resType" == "pattern" ] || [ "$resType" == "deployment-policy" ]; then
# userInput field is not always present; check to see if it exists first
if jq -e '.userInput' <<< "$resourceJson" > /dev/null; then
resourceJson=$(echo "${resourceJson}" | jq "(.userInput[] | select (.serviceOrgid == \"${oldOrgId}\") | .serviceOrgid ) = \"\${HZN_ORG_ID}\"")
checkexitcode $? "updating userInput[].serviceOrgid field"
fi
if [ "$resType" == "deployment-policy" ]; then
resourceJson=$(echo "${resourceJson}" | jq "(select(.service.org == \"${oldOrgId}\") | .service.org ) = \"\${HZN_ORG_ID}\"")
checkexitcode $? "updating service.org field"
else
resourceJson=$(echo "${resourceJson}" | jq "(.services[] | select(.serviceOrgid == \"${oldOrgId}\") | .serviceOrgid ) = \"\${HZN_ORG_ID}\"")
checkexitcode $? "updating services[].serviceOrgid field"
fi
if jq -e '.secretBinding' <<< "$resourceJson" > /dev/null; then
resourceJson=$(echo "${resourceJson}" | jq "(.secretBinding[] | select(.serviceOrgid == \"${oldOrgId}\") | .serviceOrgid ) = \"\${HZN_ORG_ID}\"")
checkexitcode $? "updating secretBinding[].org field"
fi
else # resType==service
resourceJson=$(echo "${resourceJson}" | jq "(.requiredServices[] | select(.org == \"${oldOrgId}\") | .org ) = \"\${HZN_ORG_ID}\"")
checkexitcode $? "updating requiredServices[].org field"
fi
fi
# Pipe our massaged json in the appropriate hzn publish command
case $resType in
service) # Have to 1st inflate the deployment field from a string to real json
deployment=$(jq -r '.deployment' <<< $resourceJson)
if [ ! -z "${deployment}" ]; then
resourceJson=$(jq ".deployment = $deployment" <<< $resourceJson)
fi
echo "$resourceJson" | hzn exchange service publish -I -O -f-
;;
pattern) echo "$resourceJson" | hzn exchange pattern publish -p "$name" -f-
;;
deployment-policy) echo "$resourceJson" | hzn exchange deployment addpolicy -f- "$name"
;;
esac
checkexitcode $? "publishing \"$name\" to the exchange"
done