|
| 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] etcd-endpoints" |
| 15 | +usage() { |
| 16 | + echo "${USAGE}" |
| 17 | + exit 1 |
| 18 | +} |
| 19 | + |
| 20 | +APPLY=false |
| 21 | +OS_MASTER_CONFIG_DIR="/etc/origin/master" |
| 22 | +OS_ETCD_PREFIX="/openshift.io" |
| 23 | + |
| 24 | +while getopts ":ac:p:" opt; do |
| 25 | + case $opt in |
| 26 | + a) |
| 27 | + APPLY=true |
| 28 | + ;; |
| 29 | + c) |
| 30 | + OS_MASTER_CONFIG_DIR="${OPTARG}" |
| 31 | + ;; |
| 32 | + p) |
| 33 | + OS_ETCD_PREFIX="${OPTARG}" |
| 34 | + ;; |
| 35 | + \?) |
| 36 | + usage |
| 37 | + ;; |
| 38 | + :) |
| 39 | + echo "Option -$OPTARG requires an argument" |
| 40 | + usage |
| 41 | + ;; |
| 42 | + esac |
| 43 | +done |
| 44 | +shift $((OPTIND-1)) |
| 45 | + |
| 46 | +export ETCDCTL_ENDPOINT=${1:-""} |
| 47 | +export ETCDCTL_CA_FILE=${ETCDCTL_CA_FILE:-"${OS_MASTER_CONFIG_DIR}/master.etcd-ca.crt"} |
| 48 | +export ETCDCTL_CERT_FILE=${ETCDCTL_CERT_FILE:-"${OS_MASTER_CONFIG_DIR}/master.etcd-client.crt"} |
| 49 | +export ETCDCTL_KEY_FILE=${ETCDCTL_KEY_FILE:-"${OS_MASTER_CONFIG_DIR}/master.etcd-client.key"} |
| 50 | + |
| 51 | +if [[ ! -e "${ETCDCTL_CA_FILE}" ]]; then |
| 52 | + ETCDCTL_CA_FILE="${OS_MASTER_CONFIG_DIR}/ca.crt" |
| 53 | + if [[ ! -e "${ETCDCTL_CA_FILE}" ]]; then |
| 54 | + echo "Default CA files not found. Please specify correct ETCDCTL_CA_FILE." |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | +fi |
| 58 | + |
| 59 | +if [[ ! -e "${ETCDCTL_CERT_FILE}" ]]; then |
| 60 | + echo "Default client cert file not found. Please specify correct ETCDCTL_CERT_FILE." |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +if [[ ! -e "${ETCDCTL_KEY_FILE}" ]]; then |
| 65 | + echo "Default client key file not found. Please specify correct ETCDCTL_KEY_FILE." |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | + |
| 69 | +if [[ -z "${ETCDCTL_ENDPOINT}" ]]; then |
| 70 | + echo "etcd-endpoints required" |
| 71 | + usage |
| 72 | +fi |
| 73 | + |
| 74 | +if [[ "$APPLY" != "true" ]]; then |
| 75 | + echo "Running in dry-run mode. Use -a option to apply changes." |
| 76 | +fi |
| 77 | + |
| 78 | +if ! command -v etcdctl &>/dev/null; then |
| 79 | + echo "This utility requires etcdctl to be installed" |
| 80 | + exit 1 |
| 81 | +fi |
| 82 | + |
| 83 | +echo_mode() { |
| 84 | + if [[ "$APPLY" != "true" ]]; then |
| 85 | + echo "dry-run:" "$@" |
| 86 | + else |
| 87 | + echo "$@" |
| 88 | + fi |
| 89 | +} |
| 90 | + |
| 91 | +copy_key() { |
| 92 | + echo_mode "copying ${1} to ${2}" |
| 93 | + if ! value="$(etcdctl get "${1}")"; then |
| 94 | + echo "failed to get key ${1}" |
| 95 | + return 1 |
| 96 | + fi |
| 97 | + if etcdctl get "${2}" &>/dev/null; then |
| 98 | + echo_mode "overwriting existing key ${2}" |
| 99 | + fi |
| 100 | + if [[ "$APPLY" = "true" ]]; then |
| 101 | + if ! etcdctl set "${2}" "$value" >/dev/null; then |
| 102 | + echo "failed to set key ${2}" |
| 103 | + return 1 |
| 104 | + fi |
| 105 | + fi |
| 106 | + return 0 |
| 107 | +} |
| 108 | + |
| 109 | +copy_keys() { |
| 110 | + for key in $(etcdctl ls "${1}"); do |
| 111 | + newkey="${2}/$(basename "${key}")" |
| 112 | + copy_key "${key}" "${newkey}" |
| 113 | + done |
| 114 | +} |
| 115 | + |
| 116 | +echo "Migrating Users" |
| 117 | +copy_keys "${OS_ETCD_PREFIX}/identities" "${OS_ETCD_PREFIX}/useridentities" |
| 118 | + |
| 119 | +echo "Migrating Egress Policies" |
| 120 | +for project in $(etcdctl ls "${OS_ETCD_PREFIX}/egressnetworkpolicies"); do |
| 121 | + projectname="$(basename "${project}")" |
| 122 | + copy_keys "${OS_ETCD_PREFIX}/egressnetworkpolicies/${projectname}" "${OS_ETCD_PREFIX}/registry/egressnetworkpolicy/${projectname}" |
| 123 | +done |
0 commit comments