-
Notifications
You must be signed in to change notification settings - Fork 25
/
copy-application.sh
executable file
·197 lines (177 loc) · 5.93 KB
/
copy-application.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
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
#!/usr/bin/env bash
#
# Perform a copy of applications and components to a target namespace.
SHORT_OPTS=a:,h
LONG_OPTS=applications:,all,help
OPTS=$(getopt --alternative --name copy-application --options $SHORT_OPTS --longoptions $LONG_OPTS -- "$@")
CLEANUP_ANNOTATIONS=(
"\"appstudio.openshift.io/component-initial-build\"": "\"true\""
)
CLEANUP_KEYS=(
".metadata.finalizers",
".metadata.namespace",
".metadata.ownerReferences",
".metadata.resourceVersion",
".metadata.uid",
".spec.gitOpsRepository",
".status"
)
CLEANUP_COMMAND="jq 'del(${CLEANUP_KEYS[@]})' | jq '.metadata.annotations += {${CLEANUP_ANNOTATIONS[@]}}'"
NAMESPACED_NAME_REGEX="^[a-z0-9]([-a-z0-9]*[a-z0-9])?\/[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
QUERY_ARGS="--no-headers -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name"
REQUIREMENTS="kubectl jq oc"
#######################################
# Check if all required tools are installed
# Globals:
# REQUIREMENTS
# Arguments:
# None
# Outputs:
# Writes errors messages to stderr
#######################################
check_requirements() {
for tool in $REQUIREMENTS; do
if ! [ -x "$(command -v $tool)" ]; then
echo "Error: $tool is not installed" >&2
exit 3
fi
done
oc status &>/dev/null || (echo "Error: Not logged in into a cluster" >&2 && exit 4)
}
#######################################
# Copy a given application to a the target workspace.
# Globals:
# CLEANUP_COMMAND
# NAMESPACED_NAME_REGEX
# WORKSPACE
# Arguments:
# Namespaced name of the application to copy
#######################################
copy_application() {
if [[ ! "$1" =~ $NAMESPACED_NAME_REGEX ]]; then
echo "Ignoring application '$1' as it's not a valid namespaced name"
fi
namespace=$(echo "$1" | cut -d '/' -f 1)
application=$(echo "$1" | cut -d '/' -f 2)
echo "Copying application '$application' to namespace '$WORKSPACE'"
eval "kubectl get application $application --namespace=$namespace -o json | $CLEANUP_COMMAND | kubectl apply -n $WORKSPACE -f -"
sleep 5
echo "Copying application '$application' components"
copy_components "$namespace" "$application"
}
#######################################
# Copy a list of applications to a target workspace. If no applications are
# supplied on the command line all the applications on the current namespace
# will get copied. If the --all option is used, all applications in all
# namespaces will be copied to the target namespace.
# Globals:
# ALL
# APPLICATIONS
# QUERY_ARGS
# Arguments:
# None
#######################################
copy_applications() {
if [ -z "$APPLICATIONS" ]; then
if [ "$ALL" = true ]; then
# Create array with namespaced names of all applications in all namespaces
readarray -t applications < <(kubectl get applications -A $QUERY_ARGS | awk '{print $1"/"$2}')
else
# Create array with namespaced names of all applications in the current namespace
readarray -t applications < <(kubectl get applications $QUERY_ARGS | awk '{print $1"/"$2}')
fi
else
# Create array with the list of namespaced names supplied on the command line
IFS=',' read -ra applications <<<"$APPLICATIONS"
fi
for application in "${applications[@]}"; do
copy_application "$application"
done
}
#######################################
# Copy all the components associated with a given application to the
# target workspace.
# Globals:
# CLEANUP_COMMAND
# WORKSPACE
# Arguments:
# Namespace of the application owning the components
# Name of the application owning the components
#######################################
copy_components() {
# Read all components into an array
readarray -t components < <(kubectl get component -n "$1" -o jsonpath="{range .items[?(@.spec.application==\"$2\")]}{.metadata.name}{\"\n\"}{end}")
for component in "${components[@]}"; do
echo "Copying component '$component' to namespace '$WORKSPACE'"
eval "kubectl get component $component --namespace=$1 -o json | $CLEANUP_COMMAND | kubectl apply -n $WORKSPACE -f -"
done
}
#######################################
# Parse command line arguments.
# Globals:
# ALL
# APPLICATIONS
# Arguments:
# None
# Outputs:
# Writes errors messages to stderr
#######################################
parse_arguments() {
eval set -- "$OPTS"
while :; do
case "$1" in
-a | --applications)
APPLICATIONS="$2"
shift 2
;;
-h | --help)
show_help
exit
;;
--all)
ALL=true
shift
;;
--)
shift
break
;;
*) echo "Error: Unexpected option: $1" % >2 ;;
esac
done
if [ "$ALL" = true ] && [ -n "$APPLICATIONS" ]; then
echo "Error: Options '--all' and '-a' are mutually exclusive" >&2
exit 2
fi
if [ "$#" -lt 1 ]; then
echo "Error: Target workpace has to be specified as a positional argument" >&2
exit 2
fi
if [ "$#" -gt 1 ]; then
echo "Error: Only one positional argument (target workspace) is accepted" >&2
exit 2
fi
WORKSPACE=${@:OPTIND:1}
}
#######################################
# Show help message.
# Globals:
# None
# Arguments:
# None
# Outputs:
# Writes help message to stdout
#######################################
show_help() {
echo "Usage:"
echo " copy-application [OPTION]... WORKSPACE"
echo -e "\nCopy all applications and components from one workspace to another.\n"
echo "Options:"
echo " -a,--applications if specified, only the given applications will be targeted"
echo " --all copy all applications and components from all namespaces"
echo " to the target one"
echo -e "\n -h, --help display this help"
}
parse_arguments
check_requirements
copy_applications