Skip to content

Commit 867561e

Browse files
AIFBR-12310: skill restore in active active setup (#199)
* skill restore in active active setup * skill restore script * skill restore script * skill restore script * skill restore script * review comments * review comments * review comments
1 parent 4b3d77f commit 867561e

File tree

3 files changed

+272
-0
lines changed

3 files changed

+272
-0
lines changed

activeactive/skills/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Script for manual backup and restore of skills in OnPrem
2+
3+
## Purpose
4+
With skills being present in more than one cluster in Active-Active setup, there is a need to manually backup and
5+
restore skills when needed.
6+
7+
## Requirements
8+
The Machine where script runs needs the following:
9+
* Minimum AIC version 22.10
10+
* jq, curl to be installed.
11+
* User logged in with permission to run the script and access to above tools
12+
13+
### NOTE
14+
* Please note port forwarding will be done by the script to create tenant secret on secondary cluster
15+
16+
## Steps to restore skills on secondary
17+
18+
* Find the skill id to restore. <skill-id>
19+
* Run script skill-backup.sh in the primary and give skill-id as input.
20+
* Example:
21+
[root@server0 testadmin]# sh skill-backup.sh
22+
Enter skill id:
23+
083a05ac-1c1e-4db5-850b-ca1d9d4e56cd
24+
* This will generate a dir with all the artifacts of that skill.
25+
* Copy the dir in secondary.
26+
* Run script skill-restore.sh and give path of the copied folder as input.
27+
* Example:
28+
[root@server0 testadmin]# sh skill-restore.sh
29+
Enter backup dir path:
30+
083a05ac-skill-backup
31+
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/bin/bash
2+
3+
red=$(tput setaf 1)
4+
green=$(tput setaf 2)
5+
yellow=$(tput setaf 3)
6+
default=$(tput sgr0)
7+
8+
9+
echo "$green Enter skill id:"
10+
read skillId
11+
#echo "Skill Id is: $skillId"
12+
13+
echo "$green $(date) Taking backup of Skill with Id: $skillId"
14+
15+
uuidArray=(${skillId//-/ })
16+
uuid=${uuidArray[0]}
17+
#echo "uuid: $uuid"
18+
mkdir $uuid-skill-backup
19+
20+
function backup_deployment() {
21+
22+
getDeployment=`kubectl get deployment -n uipath | grep -i $skillId`
23+
if [ -z "$getDeployment" ]; then
24+
echo "$red $(date) No deployment present with provided skill id, Please check ... Exiting $default"
25+
exit 1
26+
else
27+
deploymentArray=(${getDeployment// / })
28+
deployFile=${deploymentArray[0]}
29+
#echo "deploy file name: $deployFile"
30+
kubectl get deployment -n uipath $deployFile -o yaml > $uuid-skill-backup/$uuid-deployment.yaml
31+
echo "$green $(date) Backup of deployment is success for Skill with Id: $skillId"
32+
fi
33+
}
34+
35+
function backup_service() {
36+
37+
getService=`kubectl get svc -n uipath | grep -i $skillId`
38+
if [ -z "$getService" ]; then
39+
echo "$red $(date) No SVC found for Skill id : $skillId"
40+
else
41+
serviceArray=(${getService// / })
42+
serviceFile=${serviceArray[0]}
43+
#echo "service file name: $serviceFile"
44+
kubectl get svc -n uipath $serviceFile -o yaml > $uuid-skill-backup/$uuid-service.yaml
45+
echo "$green $(date) Backup of service is success for Skill with Id: $skillId"
46+
fi
47+
}
48+
49+
function backup_virtual_service() {
50+
51+
getVirtualService=`kubectl get virtualservices -n uipath | grep -i $skillId`
52+
if [ -z "$getVirtualService" ]; then
53+
echo "$red $(date) No Virtual Service found for Skill id : $skillId"
54+
else
55+
virtualServiceArray=(${getVirtualService// / })
56+
virtualServiceFile=${virtualServiceArray[0]}
57+
kubectl get virtualservices -n uipath $virtualServiceFile -o yaml > $uuid-skill-backup/$uuid-virtual-service.yaml
58+
echo "$green $(date) Backup of virtual service is success for Skill with Id: $skillId"
59+
fi
60+
}
61+
62+
function backup_skill_secret() {
63+
64+
getSkillSecret=`kubectl get secret -n uipath | grep -i $skillId`
65+
if [ -z "$getSkillSecret" ]; then
66+
echo "$red $(date) No Secret found for Skill id : $skillId"
67+
else
68+
secretArray=(${getSkillSecret// / })
69+
secretFile=${secretArray[0]}
70+
#echo "secretFile file name: $secretFile"
71+
kubectl get secret -n uipath $secretFile -o yaml > $uuid-skill-backup/$uuid-secret.yaml
72+
echo "$green $(date) Backup of Secret is success for Skill with Id: $skillId"
73+
fi
74+
}
75+
76+
function backup_configmap() {
77+
78+
getConfigmap=`kubectl get configmap -n uipath | grep -i $skillId`
79+
if [ -z "$getConfigmap" ]; then
80+
echo "$red $(date) No Configmap found for Skill id : $skillId"
81+
else
82+
configmapArray=(${getConfigmap// / })
83+
configmapFile=${configmapArray[0]}
84+
kubectl get configmap -n uipath $configmapFile -o yaml > $uuid-skill-backup/$uuid-configmap.yaml
85+
echo "$green $(date) Backup of Configmap is success for Skill with Id: $skillId"
86+
fi
87+
}
88+
89+
function backup_hpa() {
90+
91+
getHpa=`kubectl get hpa -n uipath | grep -i $skillId`
92+
if [ -z "$getHpa" ]; then
93+
echo "$yellow $(date) No HPA found for Skill id : $skillId"
94+
else
95+
hpaArray=(${getHpa// / })
96+
hpaFile=${hpaArray[0]}
97+
kubectl get hpa -n uipath $hpaFile -o yaml > $uuid-skill-backup/$uuid-hpa.yaml
98+
echo "$green $(date) Backup of HPA is success for Skill with Id: $skillId"
99+
fi
100+
}
101+
102+
function backup_pdb() {
103+
104+
getPdb=`kubectl get pdb -n uipath | grep -i $skillId`
105+
if [ -z "$getPdb" ]; then
106+
echo "$yellow $(date) No PDB found for Skill id : $skillId"
107+
else
108+
pdbArray=(${getPdb// / })
109+
pdbFile=${pdbArray[0]}
110+
kubectl get pdb -n uipath $pdbFile -o yaml > $uuid-skill-backup/$uuid-pdb.yaml
111+
echo "$green $(date) Backup of PDB is success for Skill with Id: $skillId"
112+
fi
113+
114+
}
115+
116+
backup_deployment
117+
118+
backup_service
119+
120+
backup_virtual_service
121+
122+
backup_skill_secret
123+
124+
backup_configmap
125+
126+
backup_hpa
127+
128+
backup_pdb
129+
130+
echo "$green $(date) Skill backup complete in dir: $uuid-skill-backup"
131+
132+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/bin/bash
2+
3+
4+
red=$(tput setaf 1)
5+
green=$(tput setaf 2)
6+
yellow=$(tput setaf 3)
7+
default=$(tput sgr0)
8+
9+
PORT_FRWD=80
10+
11+
echo "$green Enter backup dir path:"
12+
read backupDir
13+
echo "$green Enter skill id:"
14+
read skillId
15+
16+
uuidArray=(${skillId//-/ })
17+
uuid=${uuidArray[0]}
18+
19+
echo -e "$green $(date) Backup started \n"
20+
21+
# Validate dependency module
22+
# $1 - Name of the dependency module
23+
# $2 - Command to validate module
24+
function validate_dependency() {
25+
eval $2
26+
# Next statement is checking last command success
27+
if [ $? -ne 0 ]; then
28+
echo -e "$red $(date) Please install ******** $1 *********** ... Exiting $default"
29+
exit 1
30+
fi
31+
}
32+
33+
# Validate required modules exits in target setup
34+
function validate_setup() {
35+
validate_dependency jq "jq --version"
36+
echo -e "$(date) Successfully validated required dependencies"
37+
}
38+
39+
40+
function create_tenant_secret() {
41+
TENANT_SECRET_EXISTS=$(kubectl -n uipath get secret | grep deployment-storage-credentials)
42+
if [ -z "$TENANT_SECRET_EXISTS" ]; then
43+
echo -e "$green Starting port forwarding for ai-deployer to create tenant secret \n"
44+
45+
IDENTITY_SERVER_ENDPOINT=$(kubectl -n uipath get deployment ai-app-deployment -o json | jq -r '.spec.template.spec.containers[0].env[] | select(.name == "IDENTITY_SERVER_ENDPOINT").value')'/connect/token'
46+
if [[ -z $IDENTITY_SERVER_ENDPOINT ]]; then
47+
echo "$red $(date) IDENTITY_SERVER_ENDPOINT is invalid or missing, Please check ... Exiting $default"
48+
exit 1
49+
fi
50+
51+
S2S_CLIENT_ID=$(kubectl -n uipath get secret identity-client-aifabric -o json | jq '.data."Aicenter.Recovery.S2S.ClientId"' | sed -e 's/^"//' -e 's/"$//' | base64 -d)
52+
S2S_CLIENT_SECRET=$(kubectl -n uipath get secret identity-client-aifabric -o json | jq '.data."Aicenter.Recovery.S2S.ClientSecret"' | sed -e 's/^"//' -e 's/"$//' | base64 -d)
53+
if [[ -z $S2S_CLIENT_ID || -z $S2S_CLIENT_SECRET ]]; then
54+
echo "$red $(date) S2S_CLIENT_ID or S2S_CLIENT_SECRET is invalid or missing, Please check ... Exiting $default"
55+
exit 1
56+
fi
57+
58+
ACCESS_TOKEN=$(curl --location --request POST $IDENTITY_SERVER_ENDPOINT \
59+
--header 'Content-Type: application/x-www-form-urlencoded' \
60+
--data-urlencode 'client_id='$S2S_CLIENT_ID \
61+
--data-urlencode 'client_secret='$S2S_CLIENT_SECRET \
62+
--data-urlencode 'grant_type=client_credentials' \
63+
--data-urlencode 'audience=AiFabricRecovery' | jq '.access_token' | sed -e 's/^"//' -e 's/"$//')
64+
if [[ -z $ACCESS_TOKEN ]]; then
65+
echo "$red $(date) ACCESS_TOKEN is invalid or missing, Please check ... Exiting $default"
66+
exit 1
67+
fi
68+
69+
kubectl -n uipath port-forward service/ai-deployer-svc $PORT_FRWD:80 --address 0.0.0.0 &
70+
bkg_pid=$!
71+
sleep 5
72+
echo -e "$green Calling create tenant secret API \n"
73+
curl --silent --fail -i -k --show-error -X POST 'http://localhost:'$PORT_FRWD'/ai-deployer/v1/system/namespace/recover' -H 'authorization: Bearer '"$ACCESS_TOKEN"
74+
echo -e "$yellow Waiting for tenant secret creation to complete \n"
75+
sleep 5
76+
test -f /proc/$bkg_pid/cmdline && kill $bkg_pid
77+
78+
echo -e "$green Tenant Secret creation complete. Restoring backup dir \n"
79+
echo $default
80+
else
81+
echo -e "$green Tenant Secret already exists. Restoring backup dir \n"
82+
echo $default
83+
fi
84+
85+
}
86+
87+
function restore_skill() {
88+
89+
kubectl apply -f $backupDir/$uuid-secret.yaml
90+
kubectl apply -f $backupDir/$uuid-configmap.yaml
91+
kubectl apply -f $backupDir/$uuid-deployment.yaml
92+
kubectl apply -f $backupDir/$uuid-service.yaml
93+
if [ -f "$backupDir/$uuid-hpa.yaml" ]; then
94+
kubectl apply -f $backupDir/$uuid-hpa.yaml
95+
fi
96+
if [ -f "$backupDir/$uuid-pdb.yaml" ]; then
97+
kubectl apply -f $backupDir/$uuid-pdb.yaml
98+
fi
99+
kubectl apply -f $backupDir/$uuid-virtual-service.yaml
100+
101+
echo -e "$green $(date) Backup completed \n"
102+
echo $default
103+
}
104+
105+
validate_setup
106+
107+
create_tenant_secret
108+
109+
restore_skill

0 commit comments

Comments
 (0)