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

« back to all changes in this revision

Viewing changes to setup_lcm.sh

  • Committer: bbaqar at plumgrid
  • Date: 2016-05-26 19:19:01 UTC
  • mfrom: (20.1.14 single-controller)
  • Revision ID: bbaqar@plumgrid.com-20160526191901-gfcm3vifr1trz1p6
ChangesĀ forĀ 4.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
set -e
 
3
 
 
4
if [ "$EUID" -ne 0 ]
 
5
  then echo "Please run script with sudo"
 
6
  exit
 
7
fi
 
8
 
 
9
function usage() {
 
10
  cat <<DELIM__
 
11
usage: $(basename $0) [options]
 
12
 
 
13
Options:
 
14
  --clobber             Will clear stored configurations and do a fresh run
 
15
DELIM__
 
16
}
 
17
 
 
18
TEMP=$(getopt -o c --long ,clobber -- "$@")
 
19
if [[ $? -ne 0 ]]; then
 
20
  usage
 
21
  exit 1
 
22
fi
 
23
 
 
24
eval set -- "$TEMP"
 
25
 
 
26
while true; do
 
27
  case "$1" in
 
28
    --clobber) clobber=1; shift ;;
 
29
    --) shift; break ;;
 
30
    *) usage; exit 1
 
31
      ;;
 
32
  esac
 
33
done
 
34
 
 
35
mkdir -p $HOME/.pginstall
 
36
 
 
37
function read_raw() {
 
38
  eval "local=\$${2}"
 
39
  if [[ -n "$local" && "$local" =~ $3 ]]; then
 
40
    ## the value stored in the destination variable is already OK
 
41
    echo "Using provided value [${local}] for: $1"
 
42
    return 0
 
43
  fi
 
44
 
 
45
  if [[ $# -eq 5 ]]; then
 
46
    echo -n "${1} (default [${5}])"
 
47
    dfl="$5"
 
48
  else
 
49
    echo -n "${1}"
 
50
    dfl=""
 
51
  fi
 
52
 
 
53
  while true; do
 
54
    local=""
 
55
    echo -n ": "
 
56
    read local
 
57
    [[ $local == "" ]] && local="$dfl"
 
58
    if [[ "$local" =~ $3 ]]; then
 
59
      break
 
60
    fi
 
61
    echo "  Please try again.  [${local}] is not a valid value: $4"
 
62
  done
 
63
  eval $2="\"$local\""
 
64
}
 
65
 
 
66
function read_nonempty() {
 
67
  if [[ $# -eq 3 ]]; then
 
68
    read_raw "$1" "$2" "[^ ]+" "must be non-empty" "$3"
 
69
  else
 
70
    read_raw "$1" "$2" "[^ ]+" "must be non-empty"
 
71
  fi
 
72
}
 
73
 
 
74
function read_alnum() {
 
75
  if [[ $# -eq 3 ]]; then
 
76
    read_raw "$1" "$2" "^[0-9a-zA-Z]+$" "must use alphanumeric characters only" "$3"
 
77
  else
 
78
    read_raw "$1" "$2" "^[0-9a-zA-Z]+$" "must use alphanumeric characters only"
 
79
  fi
 
80
}
 
81
 
 
82
function read_url() {
 
83
  read_raw "$1" "$2" "^(ftp|http|https)://[^ ]+" "invalid schema (e.g. http://IP)"
 
84
}
 
85
 
 
86
function write_default_file() {
 
87
  cat > $HOME/.pginstall/config <<DELIM__
 
88
MAAS_CONTROLLER_REGION_ADMIN_USERNAME="${MAAS_CONTROLLER_REGION_ADMIN_USERNAME}"
 
89
plumgrid_repo_url="${plumgrid_repo_url}"
 
90
lvm_keypath_content="${lvm_keypath_content}"
 
91
zone_name="${zone_name}"
 
92
DELIM__
 
93
  return 0
 
94
}
 
95
 
 
96
function lcm_key_check() {
 
97
  zone_name_=${zone_name}
 
98
  while true; do
 
99
    zone_name="${zone_name_}"
 
100
    read_alnum 'Enter LCM Canonical zone name' zone_name
 
101
    read_url "Enter LCM repository base url" plumgrid_repo_url
 
102
    echo -n "Checking ssh key on LCM for your deployment..."
 
103
    ret=$(curl -Lks ${plumgrid_repo_url}/files/ssh_keys/zones/$zone_name/id_rsa.pub -o /tmp/id_rsa.pub -w '%{http_code}' || true)
 
104
    [ $ret -eq 200 ] && break
 
105
    echo "Unable to get ssh key from LCM for your deployment."
 
106
    echo "Check zone name or retry after running canonical-pg-refresh.sh --key-only script on LCM and make sure your deployment shows up."
 
107
    zone_name_=""
 
108
  done
 
109
  echo "OK"
 
110
  mkdir -p /var/lib/plumgrid/zones/$zone_name
 
111
  mv /tmp/id_rsa.pub /var/lib/plumgrid/zones/$zone_name/id_rsa.pub
 
112
  lvm_keypath_content="/var/lib/plumgrid/zones/${zone_name}/id_rsa.pub"
 
113
}
 
114
 
 
115
[[ -r $HOME/.pginstall/config && $clobber != 1 ]] && . $HOME/.pginstall/config
 
116
read_nonempty "Username of MAAS Region Admin User" MAAS_CONTROLLER_REGION_ADMIN_USERNAME "root"
 
117
lcm_key_check
 
118
write_default_file