-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmaak8-push-all-artifacts.sh
165 lines (154 loc) · 6.62 KB
/
maak8-push-all-artifacts.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
#!/bin/bash
## maak8-push-all-artifacts.sh script version 1.0.
##
## Copyright (c) 2022 Oracle and/or its affiliates
## Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
##
### This script will apply all resources in a tar as created by the maak8-get-all-artifacts.sh script
### The namespaces list is obtained from the directory structure in the TAR
### Usage:
###
### ./maak8-push-all-artifacts.sh [K8s TAR] [DIRECTORY]
### Where:
### K8s TAR
### The tarball created with maak8-get-all-artifacts.sh that will be applied
### DIRECTORY:
### The working directory where the backup tar will be expanded
export basedir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [[ $# -eq 2 ]];
then
export artifacts_tar=$1
export root_dir=$2
else
echo ""
echo "ERROR: Incorrect number of parameters used: Expected 2, got $#"
echo ""
echo "Usage:"
echo " $0 [TARBALL_FILE] [WORKING_DIRECTORY]"
echo ""
echo "Example: "
echo " $0 /tmp/k8lbr.paasmaaoracle.com-6443.22-12-30-11-21-55.gz /tmp/test1/"
exit 1
fi
echo "**** RESTORE OF K8s CLUSTER BASED ON YAML EXTRACTION AND APPLY ****"
dt=`date +%y-%m-%d-%H-%M-%S`
export root_dated_dir=${root_dir}/$dt
export working_dir=${root_dated_dir}/work
mkdir -p $working_dir
export backup_dir=${root_dated_dir}/before
mkdir -p $backup_dir
export oplog=$root_dated_dir/restore-operations.log
export images_log_name=images_reguired.log
cd $working_dir
echo "*******************STARTING RESTORE FOR $artifacts_tar *******************"
tar -xzf $artifacts_tar
echo ""
echo ""
echo "*********************************IMPORTANT********************************"
echo "The restore will create Kubernetes artifacts that will reference the "
echo "following images":
echo ""
echo ""
cat $working_dir/$images_log_name
echo ""
echo ""
echo "Make sure they are available in the target Kubernetes cluster's worker"
echo "nodes before starting this operation. Otherwise, restore will fail!!"
echo "**************************************************************************"
sleep 5
export nonnamespaces=`for i in $(ls *.yaml); do echo ${i%%/}; done`
echo "The non-namespaced artifacts are: $nonnamespaces" >> $oplog
export namespaces=`for i in $(ls -drt */); do echo ${i%%/}; done`
#export namespaces=$(echo "${namespaces//[$'\t\r\n']}")
export namespaces=$(echo "${namespaces}" | tr '\n' ' ')
echo "The namespaces that will be restored are: $namespaces"
echo "A log of restore operations can be found at $oplog"
echo "Restoring first the related non-namespaced artifacts in root..."
for artifact in ${nonnamespaces}; do
for namespace in ${namespaces}; do
#Only apply non-namespace artifacts that reference the selected namespaces
if grep -q "namespace: $namespace" $artifact; then
$basedir/apply-artifacts.sh $artifact $oplog
elif grep -q "group: $namespace" $artifact; then
$basedir/apply-artifacts.sh $artifact $oplog
elif grep -q "maak8sapply: $namespace" $artifact; then
$basedir/apply-artifacts.sh $artifact $oplog
fi
done
done
echo "Namespaces to restore are: $namespaces" >> $oplog
export pv_list=`kubectl get pv -A | grep -vw NAME | awk '{print $1}' | awk -v RS= '{$1=$1}1'`
for namespace in ${namespaces}; do
echo "Restoring namespace $namespace ..."
#May cause redundant ns creation, can be handled better
kubectl create -f $working_dir/$namespace/$namespace.yaml >> $oplog
#kubectl create namespace $namespace >> $oplog
cd $working_dir/$namespace
#Firstly apply service accounts
append="-I"
serviceaccountartifacts=`grep -w "kind: ServiceAccount" * | awk -F':' '{print $1}'`
echo "Original serviceaccountartifacts is : $serviceaccountartifacts" >> $oplog
serviceaccountartifacts=`echo ${serviceaccountartifacts} | tr -d '\n'`
for serviceaccountartifact in ${serviceaccountartifacts}; do
$basedir/apply-artifacts.sh $serviceaccountartifact $oplog
contructignoresa+=" $append ${serviceaccountartifact}"
done
#Secondly apply services
serviceartifacts=`grep -w "kind: Service" * | awk -F':' '{print $1}'`
echo "Original serviceartifacts is : $serviceartifacts" >> $oplog
export serviceartifacts=`echo ${serviceartifacts} | tr -d '\n'`
for serviceartifact in ${serviceartifacts}; do
$basedir/apply-artifacts.sh $serviceartifact $oplog
contructignoresvc+=" $append ${serviceartifact}"
done
pendingartifacts=`ls $contructignoresa $contructignoresvc -I $namespace.yaml`
echo "The list of pending artifacts to apply is: $pendingartifacts" >> $oplog
for artifact in ${pendingartifacts}; do
#Check if this is a PVC
artifactkind=$(cat $artifact | grep kind| awk -F'kind:' '{print $2}')
echo "Let's go with artifact of type $artifactkind " >> $oplog
if [[ "$artifactkind" == *"PersistentVolumeClaim"* ]]; then
echo "Special PVC case: " >> $oplog
pvcinartifact=`cat $artifact | grep name| awk -F'name: ' '{print $2}' | awk -v RS= '{$1=$1}1'`
volume=`cat $artifact | grep volumeName| awk -F'volumeName: ' '{print $2}' | awk -v RS= '{$1=$1}1'`
for pv in ${pv_list}; do
if [[ "$volume" == *"$pv"* ]]; then
pvc=`kubectl get pv $pv -o yaml | grep claimRef: -A 3 | grep name | awk -F'name: ' '{print $2}'`
if [[ "$pvc" == *"$pvcinartifact"* ]]; then
echo "*****PVC was already there, so need to remove it first!****" >> $oplog
#Need to improve to manage more claims than the one being added
kubectl patch pv $pv -p '{"spec":{"claimRef":null}}'
fi
fi
done
elif [[ "$artifactkind" == *"Secret"* ]]; then
echo "Cleaning up token and ca for artifact $artifact ..." >> $oplog
sed -i '/token: /d' $artifact
sed -i '/ca.crt: /d' $artifact
elif [[ "$artifactkind" == *"Job"* ]]; then
job_type=$(grep 'type:' $artifact | awk -F'type: ' '{print $2}')
if [[ "$job_type" == *"Complete"* ]]; then
sed -i 's/suspend: false/suspend: true/g' $artifact
fi
fi
$basedir/apply-artifacts.sh $artifact $oplog
done
listofdeploy=`kubectl get deployments -n $namespace | grep -v NAME | awk '{print $1}'`
for deploy in ${listofdeploy}; do
kubectl rollout restart -n $namespace deployment/$deploy
attempts=0
rollout_status_command="kubectl rollout status deployment/$deploy -n $namespace"
until $rollout_status_command || [ $attempts -eq 6 ]; do
$rollout_status_command
attempts=$((attempts + 1))
sleep 10
done
done
#sleep 60
done
echo "Restore complete!"
echo "Final status of cluster is: "
kubectl get all -A | tee -a $oplog
kubectl get pv -A | tee -a $oplog
kubectl get pvc -A | tee -a $oplog
echo "************ WARNING: PODS MAY NOT HAVE REACHED RUNNING STATE YET. PLEASE CHECK POD SATUS ************"