-
Notifications
You must be signed in to change notification settings - Fork 2
/
osyb
executable file
·216 lines (185 loc) · 5.95 KB
/
osyb
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
#!/bin/bash -e
echo "GROUPS: $(groups)"
echo "HOME: ${HOME}"
echo "ID: $(id)"
echo "GIT_SSH_COMMAND: ${GIT_SSH_COMMAND}"
echo "PASSWD FILE: $(grep osyb /etc/passwd)"
echo "GROUP FILE: $(grep osyb /etc/group)"
echo "OSYB_GIT_REPO: ${OSYB_GIT_REPO}"
echo "OSYB_GIT_URL: ${OSYB_GIT_URL}"
echo "OSYB_DEBUG: ${OSYB_DEBUG}"
echo
validation() {
if [[ -z "${OSYB_GIT_REPO}" || -z "${OSYB_GIT_URL}" ]]; then
echo "OSYB_GIT_REPO or OSYB_GIT_URL is empty"
exit 1
fi
if [ -z "${OSYB_GIT_BRANCH}" ]; then
echo "OSYB_GIT_BRANCH should not be empty"
exit 1
fi
}
helper_sort_uniq() {
sed -e '1d' | awk '{ print $1 }' | sort | uniq
}
resources() {
oc get "${1}" -o=jsonpath="{.items[*]['metadata.name']}"
}
variables() {
readonly OSYB_START_TIME=$(date +"%T")
echo "Getting projects..."
readonly PROJECTS=$(resources projects)
echo "PROJECTS: ${PROJECTS}"
echo
echo "Getting namespaced resources..."
readonly NAMESPACED_RESOURCES=$(oc api-resources --namespaced=true |
awk '{print $1}' |
grep -v ^events$ |
grep -v ^images$ |
grep -v ^jobs$ |
grep -v ^packagemanifests$ |
grep -v ^pods$ |
helper_sort_uniq)
echo "NAMESPACED_RESOURCES ${NAMESPACED_RESOURCES}"
echo
echo "Getting non-namespaced resources..."
readonly NON_NAMESPACED_RESOURCES=$(oc api-resources --namespaced=false |
awk '{print $1}' |
grep -v ^certificatesigningrequests$ |
grep -v ^images$ |
helper_sort_uniq)
echo "NON_NAMESPACED_RESOURCES ${NON_NAMESPACED_RESOURCES}"
echo
}
cdgitrepo() {
echo -e "Navigating to OSYB_GIT_REPO: ${OSYB_GIT_REPO}\n"
cd "${OSYB_GIT_REPO}"
git checkout $OSYB_GIT_BRANCH
echo -e "Current Working Directory: ${PWD} and branch: ${OSYB_GIT_BRANCH}\n"
}
gitrepo() {
echo -e "Checking whether git repository: ${OSYB_GIT_REPO} has been cloned\n"
[ -d "${OSYB_GIT_REPO}" ] || git clone "${OSYB_GIT_URL}" "${OSYB_GIT_REPO}"
ls "${OSYB_GIT_REPO}"
cdgitrepo
git config user.name osyb
git config user.email [email protected]
git rm -r '*.yaml' --ignore-unmatch
}
nonnamespaced() {
echo "Backing up all nonnamespaced..."
for nnr in ${NON_NAMESPACED_RESOURCES}; do
if [ "${OSYB_DEBUG}" = true ]; then echo "Creating '${nnr}' dir if it does not exist..."; fi
[ -d "${nnr}" ] || mkdir -p "${nnr}"
for r in $(resources "${nnr}"); do
echo -e "---" >"${nnr}/${r}.yaml"
oc get "${nnr}" "${r}" -o=yaml >>"${nnr}/${r}.yaml" || true
done
done
}
projects() {
echo -e "\nBacking up all projects..."
for p in ${PROJECTS}; do
if [ "${OSYB_DEBUG}" = true ]; then echo "Creating 'projects/${p}' dir if it does not exist..."; fi
[ -d "projects/${p}" ] || mkdir -p "projects/${p}"
oc project "${p}"
for nr in ${NAMESPACED_RESOURCES}; do
if [ "${OSYB_DEBUG}" = true ]; then echo "Creating 'projects/${p}/${nr}' dir if it does not exist..."; fi
[ -d "projects/${p}/${nr}" ] || mkdir -p "projects/${p}/${nr}"
for r in $(resources "${nr}"); do
echo -e "---" >"projects/${p}/${nr}/${r}.yaml"
oc get "${nr}" "${r}" -o=yaml >>"projects/${p}/${nr}/${r}.yaml" || true
done
done
done
}
helper_falsepositives() {
if [ "${OSYB_DEBUG}" = true ]; then echo "Removing '${1}' false positives in: '${2}'..."; fi
if grep -q "${3}" "${2}"; then
if [ "${OSYB_DEBUG}" = true ]; then grep -c "${3}" "${2}"; fi
if [ "${OSYB_DEBUG}" = true ]; then echo "eval -i \"del(${1})\" \"${2}\""; fi
yq eval -i "del(${1})" "${2}"
if [ "${OSYB_DEBUG}" = true ]; then echo -e "Done false-positives: ${2}"; fi
fi
}
falsepositives() {
cdgitrepo
local files
files=$(find "${OSYB_GIT_REPO}" -name '*.yaml' -type f ! -empty)
for f in $files; do
helper_falsepositives ".. | select(has(\"time\")).time" "${f}" "time:"
helper_falsepositives ".metadata.creationTimestamp" "${f}" "creationTimestamp:"
helper_falsepositives ".metadata.generation" "${f}" "generation:"
helper_falsepositives ".metadata.resourceVersion" "${f}" "resourceVersion:"
helper_falsepositives ".. | select(has(\"resourceVersion\")).resourceVersion" "${f}" "resourceVersion:"
helper_falsepositives ".metadata.selfLink" "${f}" "selfLink:"
helper_falsepositives ".metadata.uid" "${f}" "uid:"
helper_falsepositives ".spec.renewTime" "${f}" "renewTime:"
helper_falsepositives ".status" "${f}" "status:"
if yq -e 'has(.aggregationRule' "${f}" >/dev/null 2>&1; then
yq eval -i '.aggregationRule.clusterRoleSelectors |= sort_by(.matchLabels | keys| .[0])' ${f}
fi
if [ "${OSYB_DEBUG}" = true ]; then echo "False-positives stage completed for: ${f}"; fi
done
}
gitsecret() {
if test -n "$(git status --porcelain projects/*/secrets/*.yaml)"; then
echo "Git-secret changes found. Adding them to git..."
git secret add projects/*/secrets/*.yaml
git secret hide -m -d -F
git add projects/*/secrets/*.yaml.secret
git add .gitignore .gitsecret/
else
echo "No git-secret changes"
fi
}
preserve() {
cdgitrepo
git pull origin "${OSYB_GIT_BRANCH}"
gitsecret
git add .
if test -n "$(git status --porcelain)"; then
echo "Changes found. Adding and pushing them to git..."
git commit -m "Backup OpenShift YAML. Started: ${OSYB_START_TIME}. Duration: $((${SECONDS} / 60)) minutes."
RETRIES=10
COUNT=0
while [ $COUNT -lt $RETRIES ]; do
if git push origin "${OSYB_GIT_BRANCH}"; then
echo "git push completed"
break
else
echo "git push failed"
fi
let COUNT=$COUNT+1
sleep 60
done
else
echo "No changes"
fi
}
emptycheck() {
cdgitrepo
if [[ $(find . -name '*.yaml' -type f -empty | wc -l) -ne 0 ]]; then
echo "Some YAML files are empty"
exit 1
fi
}
yamlcheck() {
local conf=/tmp/yamllintconf
cdgitrepo
echo -e "\nRunning yamllint..."
echo -e "rules:\n line-length: disable\n indentation: disable" >$conf
yamllint -c $conf .
}
main() {
validation
variables
gitrepo
projects
nonnamespaced
falsepositives
emptycheck
yamlcheck
preserve
}
main