forked from OpsMx/spin2argo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspin2argo.sh
103 lines (93 loc) · 2.71 KB
/
spin2argo.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
#! /bin/bash
validate_clone() {
if [ $? == 0 ]
then
echo "INFO: Cloning done $SOURCE_REPO"
else
echo "ERROR: Cloning failed with repo $SOURCE_REPO, Please check credentials and repo access...."
exit 5
fi
}
tokenclone() {
clone_result=$(git clone https://$git_user:${git_token}@$SOURCE_API/$SOURCE_ORG/$SOURCE_REPO_PATH.git /tmp/$SOURCE_REPO_PATH -b "$SOURCE_BRANCH" 2> /dev/null)
validate_clone
}
sshclone() {
apk add openssh > /dev/null
mkdir -p ~/.ssh/
echo "$GIT_SSH_KEY" | tr -d '\r' > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
clone_result=$(git clone git@$SOURCE_API:$SOURCE_ORG/$SOURCE_REPO_PATH.git /tmp/$SOURCE_REPO_PATH -b "$SOURCE_BRANCH" 2> /dev/null)
validate_clone
}
repochanges() {
cd "$SOURCE_REPO_PATH"
IFS="|"
for i in $VALUES
do
echo value is $i
yq e -i "$VALUES" "$MANIFEST_PATH"
if [ $? != 0 ]; then
echo "Error occured in YAML processing, please check the logs"
exit 1
fi
done
}
gitcommitpush() {
git config --global user.email "[email protected]"
git config --global user.name "$git_user"
git commit -am "Autocommit to add ${VALUES[*]}"
git push
}
##############################
## script starts from here ###
##############################
## source the env
env > /tmp/source.txt
source /tmp/source.txt 2> /dev/null
## Input variables from the Configmap
cd /tmp/
SOURCE_REPO="$source"
SOURCE_BRANCH="$sourcebranch"
TARGET_REPO="$source"
MANIFEST_PATH="$filePath"
VALUES="$value"
SOURCE_REPO_PATH=$(echo $SOURCE_REPO | awk -F// '{print $2}' | awk -F/ '{print $3}' | awk -F. '{print $1}')
SOURCE_ORG=$(echo $SOURCE_REPO | awk -F// '{print $2}' | awk -F/ '{print $2}')
SOURCE_API=$(echo $SOURCE_REPO | awk -F// '{print $2}' | awk -F/ '{print $1}')
if [[ -z "$SOURCE_REPO" || -z "$TARGET_REPO" ]]; then
echo "ERROR: Source repo, target repo, path, and values must all be defined."
exit 1
fi
if [[ -z "$SOURCE_BRANCH" ]]; then
echo "ERROR: Not defined the branch, Please specify branch."
exit 1
fi
if [[ -z "$MANIFEST_PATH" ]]; then
echo "ERROR: YAML to be modified must be specified"
exit 1
fi
if [[ -z "$VALUES" ]]; then
echo "ERROR: Not defined vaules to be replaced in the manifest "
exit 1
fi
if [[ -z "$GIT_SSH_KEY" && -z "$git_token" ]]; then
echo "ERROR: Not defined github authendication token or SSH key ."
exit 1
elif [[ ! -z "$GIT_SSH_KEY" && ! -z "$git_token" ]]; then
echo "INFO: Defined both token and SSH, considering the token to clone ..."
tokenclone
else
if [[ ! -z "$git_token" ]]; then
echo "INFO: cloning using token..."
tokenclone
fi
if [[ ! -z "$GIT_SSH_KEY" ]]; then
echo "INFO: cloning using ssh..."
sshclone
fi
fi
cd /tmp/
repochanges
gitcommitpush