Skip to content

Commit 22339ab

Browse files
author
OpenShift Bot
authored
Merge pull request #13118 from kargakis/migration-script-15
Merged by openshift-bot
2 parents 770a605 + 9cf1869 commit 22339ab

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

contrib/migration/fix-3.4-paths.sh

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#!/bin/bash
2+
#
3+
# https://bugzilla.redhat.com/show_bug.cgi?id=1415570
4+
#
5+
# In the initial release of OCP 3.4, paths for two objects,
6+
# User and EgressNetworkPolicy, inadvertantly changed.
7+
# This script migrates any of these resources created in
8+
# version of OCP 3.4 without the fix to the proper location
9+
# in etcd. Namely:
10+
#
11+
# identities -> useridentities
12+
# egressnetworkpolicies -> registry/egressnetworkpolicy
13+
14+
USAGE="${0} [-a] [-c os-master-config-dir] [-p os-etcd-prefix] [-b backup-dir] etcd-endpoints"
15+
usage() {
16+
echo "${USAGE}"
17+
exit 1
18+
}
19+
20+
# default values
21+
APPLY=false
22+
OS_MASTER_CONFIG_DIR="/etc/origin/master"
23+
OS_ETCD_PREFIX="/openshift.io"
24+
BACKUP_DIR="$HOME/openshift-3.4-migration-backup"
25+
26+
while getopts ":ac:p:b:" opt; do
27+
case $opt in
28+
a)
29+
APPLY=true
30+
;;
31+
c)
32+
OS_MASTER_CONFIG_DIR="${OPTARG}"
33+
;;
34+
p)
35+
OS_ETCD_PREFIX="${OPTARG}"
36+
;;
37+
b)
38+
BACKUP_DIR="${OPTARG}"
39+
;;
40+
\?)
41+
usage
42+
;;
43+
:)
44+
echo "Option -$OPTARG requires an argument"
45+
usage
46+
;;
47+
esac
48+
done
49+
shift $((OPTIND-1))
50+
51+
export ETCDCTL_ENDPOINT=${1:-""}
52+
export ETCDCTL_CA_FILE=${ETCDCTL_CA_FILE:-"${OS_MASTER_CONFIG_DIR}/master.etcd-ca.crt"}
53+
export ETCDCTL_CERT_FILE=${ETCDCTL_CERT_FILE:-"${OS_MASTER_CONFIG_DIR}/master.etcd-client.crt"}
54+
export ETCDCTL_KEY_FILE=${ETCDCTL_KEY_FILE:-"${OS_MASTER_CONFIG_DIR}/master.etcd-client.key"}
55+
56+
if [[ ! -e "${ETCDCTL_CA_FILE}" ]]; then
57+
ETCDCTL_CA_FILE="${OS_MASTER_CONFIG_DIR}/ca.crt"
58+
if [[ ! -e "${ETCDCTL_CA_FILE}" ]]; then
59+
echo "Default CA files not found. Please specify correct ETCDCTL_CA_FILE."
60+
exit 1
61+
fi
62+
fi
63+
64+
if [[ ! -e "${ETCDCTL_CERT_FILE}" ]]; then
65+
echo "Default client cert file not found. Please specify correct ETCDCTL_CERT_FILE."
66+
exit 1
67+
fi
68+
69+
if [[ ! -e "${ETCDCTL_KEY_FILE}" ]]; then
70+
echo "Default client key file not found. Please specify correct ETCDCTL_KEY_FILE."
71+
exit 1
72+
fi
73+
74+
if [[ -z "${ETCDCTL_ENDPOINT}" ]]; then
75+
echo "etcd-endpoints required"
76+
usage
77+
fi
78+
79+
if [[ "$APPLY" != "true" ]]; then
80+
echo "Running in dry-run mode. Use -a option to apply changes."
81+
else
82+
if ! mkdir -p "${BACKUP_DIR}"; then
83+
echo "Unable to create backup directory ${BACKUP_DIR}"
84+
exit 1
85+
fi
86+
fi
87+
88+
if ! command -v etcdctl &>/dev/null; then
89+
echo "This utility requires etcdctl to be installed"
90+
exit 1
91+
fi
92+
93+
echo_mode() {
94+
if [[ "$APPLY" != "true" ]]; then
95+
echo "dry-run:" "$@"
96+
else
97+
echo "$@"
98+
fi
99+
}
100+
101+
backup_key() {
102+
key="${1}"
103+
value="${2}"
104+
105+
backupfile="${BACKUP_DIR}/${key}"
106+
mkdir -p "$(dirname "${backupfile}")"
107+
echo "$value" > "${backupfile}"
108+
}
109+
110+
copy_key() {
111+
echo_mode "copying ${1} to ${2}"
112+
if ! value="$(etcdctl get "${1}")"; then
113+
echo_mode "failed to get key ${1}"
114+
exit 1
115+
fi
116+
if existing=$(etcdctl get "${2}" 2>/dev/null); then
117+
echo_mode "overwriting existing key ${2}"
118+
fi
119+
if [[ "$APPLY" = "true" ]]; then
120+
backup_key "${1}" "${value}"
121+
if [[ -n "${existing}" ]]; then
122+
backup_key "${2}" "${existing}"
123+
fi
124+
if ! etcdctl set "${2}" "$value" >/dev/null; then
125+
echo "failed to set key ${2}"
126+
exit 1
127+
fi
128+
if ! etcdctl rm "${1}" >/dev/null; then
129+
echo "failed to remove old key ${1}"
130+
exit 1
131+
fi
132+
fi
133+
return 0
134+
}
135+
136+
copy_keys() {
137+
output="$(etcdctl ls "${1}")"
138+
if [[ $? -ne 0 || -z "$output" ]]; then
139+
echo_mode "No keys found to migrate"
140+
return
141+
fi
142+
for key in $output; do
143+
newkey="${2}/$(basename "${key}")"
144+
copy_key "${key}" "${newkey}"
145+
done
146+
}
147+
148+
IFS=$'\n'
149+
150+
echo_mode "Migrating Users"
151+
copy_keys "${OS_ETCD_PREFIX}/identities" "${OS_ETCD_PREFIX}/useridentities"
152+
153+
echo_mode "Migrating Egress Policies"
154+
output="$(etcdctl ls "${OS_ETCD_PREFIX}/egressnetworkpolicies")"
155+
if [[ $? -ne 0 || -z "$output" ]]; then
156+
echo_mode "No keys found to migrate"
157+
else
158+
for project in $output; do
159+
projectname="$(basename "${project}")"
160+
echo_mode "Project $projectname"
161+
copy_keys "${OS_ETCD_PREFIX}/egressnetworkpolicies/${projectname}" "${OS_ETCD_PREFIX}/registry/egressnetworkpolicy/${projectname}"
162+
done
163+
fi

0 commit comments

Comments
 (0)