~plumgrid-team/charms/bundles/plumgrid-ons/single-controller

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
#!/bin/bash
set -e

if [ "$EUID" -ne 0 ]
  then echo "Please run script with sudo"
  exit
fi

function usage() {
  cat <<DELIM__
usage: $(basename $0) [options]

Options:
  --clobber             Will clear stored configurations and do a fresh run
DELIM__
}

TEMP=$(getopt -o c --long ,clobber -- "$@")
if [[ $? -ne 0 ]]; then
  usage
  exit 1
fi

eval set -- "$TEMP"

while true; do
  case "$1" in
    --clobber) clobber=1; shift ;;
    --) shift; break ;;
    *) usage; exit 1
      ;;
  esac
done

mkdir -p $HOME/.pginstall

function read_raw() {
  eval "local=\$${2}"
  if [[ -n "$local" && "$local" =~ $3 ]]; then
    ## the value stored in the destination variable is already OK
    echo "Using provided value [${local}] for: $1"
    return 0
  fi

  if [[ $# -eq 5 ]]; then
    echo -n "${1} (default [${5}])"
    dfl="$5"
  else
    echo -n "${1}"
    dfl=""
  fi

  while true; do
    local=""
    echo -n ": "
    read local
    [[ $local == "" ]] && local="$dfl"
    if [[ "$local" =~ $3 ]]; then
      break
    fi
    echo "  Please try again.  [${local}] is not a valid value: $4"
  done
  eval $2="\"$local\""
}

function read_nonempty() {
  if [[ $# -eq 3 ]]; then
    read_raw "$1" "$2" "[^ ]+" "must be non-empty" "$3"
  else
    read_raw "$1" "$2" "[^ ]+" "must be non-empty"
  fi
}

function read_alnum() {
  if [[ $# -eq 3 ]]; then
    read_raw "$1" "$2" "^[0-9a-zA-Z]+$" "must use alphanumeric characters only" "$3"
  else
    read_raw "$1" "$2" "^[0-9a-zA-Z]+$" "must use alphanumeric characters only"
  fi
}

function read_url() {
  read_raw "$1" "$2" "^(ftp|http|https)://[^ ]+" "invalid schema (e.g. http://IP)"
}

function write_default_file() {
  cat > $HOME/.pginstall/config <<DELIM__
MAAS_CONTROLLER_REGION_ADMIN_USERNAME="${MAAS_CONTROLLER_REGION_ADMIN_USERNAME}"
plumgrid_repo_url="${plumgrid_repo_url}"
lvm_keypath_content="${lvm_keypath_content}"
zone_name="${zone_name}"
DELIM__
  return 0
}

function lcm_key_check() {
  zone_name_=${zone_name}
  while true; do
    zone_name="${zone_name_}"
    read_alnum 'Enter LCM Canonical zone name' zone_name
    read_url "Enter LCM repository base url" plumgrid_repo_url
    echo -n "Checking ssh key on LCM for your deployment..."
    ret=$(curl -Lks ${plumgrid_repo_url}/files/ssh_keys/zones/$zone_name/id_rsa.pub -o /tmp/id_rsa.pub -w '%{http_code}' || true)
    [ $ret -eq 200 ] && break
    echo "Unable to get ssh key from LCM for your deployment."
    echo "Check zone name or retry after running canonical-pg-refresh.sh --key-only script on LCM and make sure your deployment shows up."
    zone_name_=""
  done
  echo "OK"
  mkdir -p /var/lib/plumgrid/zones/$zone_name
  mv /tmp/id_rsa.pub /var/lib/plumgrid/zones/$zone_name/id_rsa.pub
  lvm_keypath_content="/var/lib/plumgrid/zones/${zone_name}/id_rsa.pub"
}

[[ -r $HOME/.pginstall/config && $clobber != 1 ]] && . $HOME/.pginstall/config
read_nonempty "Username of MAAS Region Admin User" MAAS_CONTROLLER_REGION_ADMIN_USERNAME "root"
lcm_key_check
write_default_file