-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdtrctl.sh
executable file
·350 lines (275 loc) · 9.65 KB
/
dtrctl.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/bash
main() {
echo "Starting dtrctl ..."
authenticate
if [ "$PULL" ]; then
if [ ! $SRC_DTR_URL ]; then
echo "error: No source DTR specified"
exit 1
fi
getOrgs
getRepos
getTeams
getTeamMembers
getTeamRepoAccess
echo "Sync from source DTR to local copy complete"
fi
if [ "$PUSH" ]; then
if [ ! $DEST_DTR_URL ]; then
echo "error: No destination DTR specified"
exit 1
fi
putOrgs
putRepos
putTeams
putTeamMembers
putTeamRepoAccess
echo "Sync from local copy to destination DTR complete"
fi
if [ "$SYNC_IMAGES" ]; then
migrateImages
echo "Image migration from source DTR to destination DTR complete"
fi
if [ "$PRINT_ACCESS" ]; then
printAccessMap
fi
}
authenticate() {
if [ $SRC_DTR_URL ]; then
echo "Authenticating and logging in to $SRC_DTR_URL"
if [ ! -d "/etc/docker/certs.d/${SRC_DTR_URL}" ]; then
mkdir -p /etc/docker/certs.d/"${SRC_DTR_URL}"
fi
curl -ksf https://"${SRC_DTR_URL}"/ca > /etc/docker/certs.d/"${SRC_DTR_URL}"/ca.crt
openssl s_client -host "${SRC_DTR_URL}" -port 443 </dev/null 2>/dev/null | openssl x509 -outform PEM > /etc/docker/certs.d/"${SRC_DTR_URL}"/ca.crt
docker login "$SRC_DTR_URL" -u "$SRC_DTR_USER" -p "$SRC_DTR_PASSWORD"
SRC_DTR_TOKEN=$(cat ~/.docker/config.json | jq -r ".auths[\"$SRC_DTR_URL\"].identitytoken")
fi
if [ $DEST_DTR_URL ]; then
echo "Authenticating and logging in to $DEST_DTR_URL"
if [ ! -d "/etc/docker/certs.d/${DEST_DTR_URL}" ]; then
mkdir -p /etc/docker/certs.d/"${DEST_DTR_URL}"
fi
curl -ksf https://"${DEST_DTR_URL}"/ca > /etc/docker/certs.d/"${DEST_DTR_URL}"/ca.crt
openssl s_client -host "${DEST_DTR_URL}" -port 443 </dev/null 2>/dev/null | openssl x509 -outform PEM > /etc/docker/certs.d/"${DEST_DTR_URL}"/ca.crt
docker login "$DEST_DTR_URL" -u "$DEST_DTR_USER" -p "$DEST_DTR_PASSWORD"
DEST_DTR_TOKEN=$(cat ~/.docker/config.json | jq -r ".auths[\"$DEST_DTR_URL\"].identitytoken")
fi
}
###########################
# GET #
###########################
getOrgs() {
curl -s --insecure \
https://"$SRC_DTR_URL"/enzi/v0/accounts?refresh_token="$SRC_DTR_TOKEN" | \
jq -c '.accounts[] | select(.isOrg==true) | {name: .name, fullName: .fullName, isOrg: .isOrg}' \
> orgConfig
cat orgConfig | jq -r '.name' > orgList
cat orgList | while IFS= read -r i;
do
if [ ! -d ./$i ]; then
mkdir ./$i
fi
done
}
getRepos() {
cat orgList | sort -u | while IFS= read -r i;
do
curl -s --insecure \
https://"$SRC_DTR_URL"/api/v0/repositories/$i?refresh_token="$SRC_DTR_TOKEN" | \
jq '.repositories[] | {name: .name, shortDescription: .shortDescription, longDescription: "", visibility: .visibility}' \
> ./$i/repoConfig
done
}
getTeams() {
cat orgList | sort -u | while IFS= read -r i;
do
curl -s --insecure \
https://"$SRC_DTR_URL"/enzi/v0/accounts/$i/teams?refresh_token="$SRC_DTR_TOKEN" | jq -c '.teams[] | {name: .name, description: .description}' > ./$i/teamConfig
cat ./$i/teamConfig | while IFS= read -r j;
do
if [ ! -d ./$i/$(echo $j | jq -r '.name') ]; then
mkdir ./$i/$(echo $j | jq -r '.name')
fi
done
done
}
getTeamMembers() {
cat orgList | sort -u | while IFS= read -r i;
do
cat ./$i/teamConfig | jq -r '.name' | while IFS= read -r j;
do
curl -s --insecure \
https://"$SRC_DTR_URL"/enzi/v0/accounts/${i}/teams/${j}/members?refresh_token="$SRC_DTR_TOKEN" | jq -c '.members[] | {name: .member.name, isAdmin: .isAdmin, isPublic: .isPublic}' \
> ./$i/$j/members
done
done
}
getTeamRepoAccess() {
cat orgList | sort -u | while IFS= read -r i;
do
cat ./$i/teamConfig | jq -r '.name' | while IFS= read -r j;
do
curl -s --insecure \
https://"$SRC_DTR_URL"/api/v0/accounts/${i}/teams/${j}/repositoryAccess?refresh_token="$SRC_DTR_TOKEN" | jq -c '.repositoryAccessList[]' > ./$i/$j/repoAccess
done
done
}
###########################
# PUT #
###########################
putOrgs() {
cat orgConfig | while IFS= read -r i;
do
curl --insecure -X POST --header "Content-Type: application/json" \
--header "Accept: application/json" -d "$i" https://"$DEST_DTR_URL"/enzi/v0/accounts?refresh_token="$DEST_DTR_TOKEN"
done
}
putRepos() {
cat orgList | sort -u | while IFS= read -r i;
do
cat ./$i/repoConfig | jq -c '.' | while IFS= read -r j;
do
curl --insecure -X POST --header "Content-Type: application/json" \
--header "Accept: application/json" -d "$j" https://"$DEST_DTR_URL"/api/v0/repositories/${i}?refresh_token="$DEST_DTR_TOKEN"
done
done
}
putTeams() {
cat orgList | sort -u | while IFS= read -r i;
do
curl -s --insecure \
https://"$SRC_DTR_URL"/enzi/v0/accounts/$i/teams?refresh_token="$SRC_DTR_TOKEN" | jq -c '.teams[] | {name: .name, description: .description}' > ./$i/teamConfig
cat ./$i/teamConfig | while IFS= read -r j;
do
curl --insecure -X POST --header "Content-Type: application/json" \
--header "Accept: application/json" -d "$j" https://"$DEST_DTR_URL"/enzi/v0/accounts/${i}/teams?refresh_token="$DEST_DTR_TOKEN"
done
done
}
putTeamMembers() {
#Responds with 200 even though team members already exist (I guess this is because of PUT)
cat orgList | sort -u | while IFS= read -r i;
do
cat ./$i/teamConfig | jq -r '.name' | while IFS= read -r j;
do
cat ./$i/$j/members | while IFS= read -r k;
do
teamMemberName=$(echo $k | jq -c -r .name)
curl --insecure -X PUT --header "Content-Type: application/json" \
--header "Accept: application/json" -d "$k" https://"$DEST_DTR_URL"/enzi/v0/accounts/${i}/teams/${j}/members/${teamMemberName}?refresh_token="$DEST_DTR_TOKEN"
done
done
done
}
## Needs to be finished
putTeamRepoAccess() {
echo "putTeamRepoAccess"
}
###########################
# PUSH IMAGES #
###########################
migrateImages() {
echo "Image sync initiating"
cat orgList | sort -u | while IFS= read -r i;
do
echo "Migrating images for $i"
cat ./$i/repoConfig | jq -c -r '.name' | while IFS= read -r j;
do
TAGS=$(curl -s --insecure \
https://"$SRC_DTR_URL"/api/v0/repositories/${i}/${j}/tags?refresh_token="$SRC_DTR_TOKEN" | jq -c -r '.[].name')
for k in $TAGS;
do
echo "Pulling $SRC_DTR_URL/$i/$j:$k"
docker pull "$SRC_DTR_URL/$i/$j:$k"
docker tag "$SRC_DTR_URL/$i/$j:$k" "$DEST_DTR_URL/$i/$j:$k"
echo "Pushing $DEST_DTR_URL/$i/$j:$k"
docker push "$DEST_DTR_URL/$i/$j:$k"
done
#Clean up images after each repo
#echo "Repo $i complete. Pruning local images."
#docker image prune -af
done
#Clean up images after each Org
echo "Org $i complete. Pruning local images."
docker image prune -af
done
}
printAccessMap() {
echo "Printing Team and Repo Access"
cat orgList | sort -u | while IFS= read -r i;
do
echo "$i"
echo "-------------------------"
cat ./$i/teamConfig | jq -r '.name' | while IFS= read -r j;
do
echo " $j Members"
cat ./$i/$j/members | while IFS= read -r member;
do
if [ $(echo $member | jq '.isAdmin') == 'true' ]
then
access="Admin"
else
access="Member"
fi
echo " " $(echo $member | jq -r .name) "-" "$access"
done
echo " $j Repository Access"
cat ./$i/$j/repoAccess | while IFS= read -r access;
do
repoName=$(echo $access | jq -r '.repository.name')
accessLevel=$(echo $access | jq -r '.accessLevel')
echo " $i/$repoName - $accessLevel"
done
echo ""
done
echo ""
done
}
usage() {
echo ""
echo "Usage: dtrctl -c [confguration file] COMMAND"
echo "Pronounced: dtr-control"
echo ""
echo "Options"
echo ""
echo "-s, --source-metadata Pull org, repo, team, and team access metadata from source DTR and store locally"
echo "-p, --push-metadata Push org, repo, team, and team access metadata from local source to dest DTR"
echo "-i, --image-sync Pull images from source DTR and push to dest DTR"
echo "-a, --print-access Print mapping of access rights between teams and repos"
echo "--help Print usage"
echo ""
}
## Parse arguments
while [[ $# -gt 0 ]]
do
case "$1" in
-p|--push-metadata)
PUSH=1
shift 1
;;
-i|--image-sync)
SYNC_IMAGES=1
shift 1
;;
-a|--print-access)
PRINT_ACCESS=1
shift 1
;;
-s|--source-metadata)
PULL=1
shift 1
;;
-h|--help)
usage
exit 1
;;
*)
echo "Unknown argument: $1"
usage
exit 1
;;
esac
done
#Entrypoint for program
main