-
Notifications
You must be signed in to change notification settings - Fork 2
/
get-update-list
executable file
·142 lines (120 loc) · 5.19 KB
/
get-update-list
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/sh
#
# This OPTIONAL file returns a list of all of the update scripts
# to run, in the order you want to run them.
#
# This gets executed from the $EZVM_LOCAL_UPDATE_DIR directory
# (by default etc/local/update)
#
# In this example implementation, we simply return the default
# list of all files in sorted order.
#
. "$(dirname "$0")/../lib.sh" || { echo "Cannot include ../lib.sh" 1>&2; exit 1; }
DEBUG=0
# Determine if we're running on GCE
is_gce=$(get_metadata "id")
r=$?
case $r in
22) ;; # This is a 404 error, as in the metadata doesn't exist; no problem.
6) ;; # Couldn't resolve host; assume we're not running in GCE
0) ;; # Success we got the metadata
*) fatal $r "Error querying metadata (code=$r)" ;;
esac
if [ -z "$is_gce" ]; then
is_gce=0
debug_output "NOT running in GCE"
ezvm_updates=""
else
is_gce=1
debug_output "Currently running in GCE"
ezvm_updates="gce-maintenance"
fi
if [ $is_gce = 1 ]; then
# Get the server role
# If this fails get_metadata_attribute prints error info so we just need to exit with the failure code
metadata=$(get_metadata_attribute "ezvm-updates")
r=$?
case $r in
22) ;; # This is a 404 error, as in the metadata doesn't exist; no problem.
0) ;; # Success we got the metadata
*) fatal $r "Error querying metadata (code=$r)" ;;
esac
ezvm_updates="$ezvm_updates $metadata"
debug_output "After querying metadata, ezvm_updates='$ezvm_updates'"
fi
splice_update_before() {
update=$1
before=$2
updateEscaped=$(echo "$update" | sed -e 's,\-,\\-,g')
beforeEscaped=$(echo "$before" | sed -e 's,\-,\\-,g')
# If the $update exists AFTER $before, then remove it so we can add it back in the correct place
if echo " $ezvm_updates " | grep " $beforeEscaped *\(.* \)*$updateEscaped " > /dev/null 2>&1; then
ezvm_updates="$(echo " $ezvm_updates " | sed -e "s,[[:space:]]$updateEscaped[[:space:]], ,")"
debug_output "During splicing $update before $before, removed $update, ezvm_updates='$ezvm_updates'"
fi
# If the $update doesn't exist, add it before $before
if ! echo " $ezvm_updates " | grep " $updateEscaped " > /dev/null 2>&1; then
ezvm_updates=$(echo " $ezvm_updates " | sed -e "s, $beforeEscaped , $update $before ,")
fi
debug_output "After splicing $update before $before, ezvm_updates='$ezvm_updates'"
}
splice_update_after() {
after=$1
update=$2
afterEscaped=$(echo "$after" | sed -e 's,\-,\\-,g')
updateEscaped=$(echo "$update" | sed -e 's,\-,\\-,g')
# If the $update exists BEFORE $after, then remove it so we can add it back in the correct place
if echo " $ezvm_updates " | grep " $updateEscaped *\(.* \)*$afterEscaped " > /dev/null 2>&1; then
ezvm_updates="$(echo " $ezvm_updates " | sed -e "s,[[:space:]]$updateEscaped[[:space:]], ,")"
debug_output "During splicing $update after $after, removed $update, ezvm_updates='$ezvm_updates'"
fi
# If the update doesn't exist, add it after $after
if ! echo " $ezvm_updates " | grep " $updateEscaped " > /dev/null 2>&1; then
ezvm_updates=$(echo " $ezvm_updates " | sed -e "s, $afterEscaped , $after $update ,")
fi
debug_output "After splicing $update after $after, ezvm_updates='$ezvm_updates'"
}
# If we are installing salt-minion, then insert salt-common updates before salt-minion
if echo " $ezvm_updates " | grep ' salt\-minion ' > /dev/null 2>&1; then
# 1) Only insert salt-common if it doesn't already exist in the list
splice_update_before 'salt-common' 'salt-minion'
# 2) If a salt-minion-reset was not explicitly provided, add it after the salt-minion
splice_update_after 'salt-minion' 'salt-minion-reset'
# 3) If a salt-highstate was not explicitly provided, add it after the salt-minion-reset
splice_update_after 'salt-minion-reset' 'salt-highstate'
fi
# If we are installing salt-master, then insert salt-common updates before salt-master
if echo " $ezvm_updates " | grep ' salt\-master ' > /dev/null 2>&1; then
# Only insert it if it doesn't already exist in the list
splice_update_before 'salt-common' 'salt-master'
fi
list_update_files() {
d=$1
# Find all files in directory $d
# If this returns an invalid return code then it has failed and we need to die
tmpf="$(mktemp)"
find "$d" -maxdepth 1 -type f -print > "$tmpf"
r=$?
if [ $r = 0 ]; then
cat "$tmpf" \
| sort \
| xargs -n1 -i{} sh -c 'test -x {} || echo -n "# SKIP NON-EXECUTABLE: "; echo {}'
fi
rm -f "$tmpf"
[ $r = 0 ] || fatal $r "find failed to list files in directory: $(readlink -f "$(pwd)/$d")"
}
# List of update dirs must have associated setup routines
for d in $ezvm_updates; do
[ -d "./$d" ] || fatal 2 "Invalid ezvm-updates dir: $d"
done
# First thing we do is anything that's truly global
# DO NOT include get-update-list (this script) in the output
list_update_files "." | sed -e "s,^\(\(\./\)\?$(basename $0)\)\$,# SKIP RESERVED FILE: \1,"
# Only execute role-specific update procedure
# They are listed in the "$dir/" sub-directory
#
# List these in the same order that they're specified in ezvm-updates
#
for d in $ezvm_updates; do
list_update_files "./$d"
done