| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| 15 | |
| 16 | function usage(){ |
| 17 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 18 | echo -e "usage: $0 [OPTIONS]" |
| 19 | echo -e "Install Juju for OSM" |
| 20 | echo -e " OPTIONS" |
| 21 | echo -e " -h / --help: print this help" |
| 22 | echo -e " -D <devops path> use local devops installation path" |
| 23 | echo -e " -s <stack name> or <namespace> user defined stack name when installed using swarm or namespace when installed using k8s, default is osm" |
| 24 | echo -e " -H <VCA host> use specific juju host controller IP" |
| 25 | echo -e " -S <VCA secret> use VCA/juju secret key" |
| 26 | echo -e " -P <VCA pubkey> use VCA/juju public key file" |
| 27 | echo -e " -l: LXD cloud yaml file" |
| 28 | echo -e " -L: LXD credentials yaml file" |
| 29 | echo -e " -K: Specifies the name of the controller to use - The controller must be already bootstrapped" |
| 30 | echo -e " --debug: debug mode" |
| 31 | echo -e " --cachelxdimages: cache local lxd images, create cronjob for that cache (will make installation longer)" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 32 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 33 | } |
| 34 | |
| 35 | function update_juju_images(){ |
| 36 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 37 | crontab -l | grep update-juju-lxc-images || (crontab -l 2>/dev/null; echo "0 4 * * 6 $USER ${OSM_DEVOPS}/installers/update-juju-lxc-images --xenial --bionic") | crontab - |
| 38 | ${OSM_DEVOPS}/installers/update-juju-lxc-images --xenial --bionic |
| 39 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 40 | } |
| 41 | |
| garciadeblas | ccb2237 | 2023-06-07 23:50:00 +0200 | [diff] [blame] | 42 | function install_juju_client() { |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 43 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| garciadeblas | ccb2237 | 2023-06-07 23:50:00 +0200 | [diff] [blame] | 44 | echo "Installing juju client" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 45 | sudo snap install juju --classic --channel=$JUJU_VERSION/stable |
| 46 | [[ ":$PATH": != *":/snap/bin:"* ]] && PATH="/snap/bin:${PATH}" |
| 47 | [ -n "$INSTALL_CACHELXDIMAGES" ] && update_juju_images |
| garciadeblas | ccb2237 | 2023-06-07 23:50:00 +0200 | [diff] [blame] | 48 | echo "Finished installation of juju client" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 49 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 50 | return 0 |
| 51 | } |
| 52 | |
| 53 | function juju_createcontroller_k8s(){ |
| 54 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 55 | cat $HOME/.kube/config | juju add-k8s $OSM_VCA_K8S_CLOUDNAME --client \ |
| garciadeblas | 4290a68 | 2023-06-02 10:29:23 +0200 | [diff] [blame] | 56 | || FATAL_TRACK juju "Failed to add K8s endpoint and credential for client in cloud $OSM_VCA_K8S_CLOUDNAME" |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 57 | |
| 58 | JUJU_BOOTSTRAP_OPTS="" |
| 59 | if [ -n "${OSM_BEHIND_PROXY}" ] ; then |
| 60 | K8S_SVC_CLUSTER_IP=$(kubectl get svc/kubernetes -o jsonpath='{.spec.clusterIP}') |
| 61 | NO_PROXY="${NO_PROXY},${K8S_SVC_CLUSTER_IP},.svc,.cluster.local" |
| 62 | mkdir -p /tmp/.osm |
| 63 | JUJU_MODEL_CONFIG_FILE=/tmp/.osm/model-config.yaml |
| 64 | cat << EOF > $JUJU_MODEL_CONFIG_FILE |
| 65 | apt-http-proxy: ${HTTP_PROXY} |
| 66 | apt-https-proxy: ${HTTPS_PROXY} |
| 67 | juju-http-proxy: ${HTTP_PROXY} |
| 68 | juju-https-proxy: ${HTTPS_PROXY} |
| 69 | juju-no-proxy: ${NO_PROXY} |
| 70 | snap-http-proxy: ${HTTP_PROXY} |
| 71 | snap-https-proxy: ${HTTPS_PROXY} |
| 72 | EOF |
| 73 | JUJU_BOOTSTRAP_OPTS="--model-default /tmp/.osm/model-config.yaml" |
| 74 | fi |
| garciadeblas | 325032a | 2023-04-13 18:07:44 +0200 | [diff] [blame] | 75 | juju bootstrap -v --debug $OSM_VCA_K8S_CLOUDNAME $OSM_NAMESPACE \ |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 76 | --config controller-service-type=loadbalancer \ |
| 77 | --agent-version=$JUJU_AGENT_VERSION \ |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 78 | ${JUJU_BOOTSTRAP_OPTS} \ |
| garciadeblas | 4290a68 | 2023-06-02 10:29:23 +0200 | [diff] [blame] | 79 | || FATAL_TRACK juju "Failed to bootstrap controller $OSM_NAMESPACE in cloud $OSM_VCA_K8S_CLOUDNAME" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 80 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 81 | } |
| 82 | |
| 83 | function juju_addlxd_cloud(){ |
| 84 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 85 | mkdir -p /tmp/.osm |
| 86 | OSM_VCA_CLOUDNAME="lxd-cloud" |
| 87 | LXDENDPOINT=$DEFAULT_IP |
| 88 | LXD_CLOUD=/tmp/.osm/lxd-cloud.yaml |
| 89 | LXD_CREDENTIALS=/tmp/.osm/lxd-credentials.yaml |
| 90 | |
| 91 | cat << EOF > $LXD_CLOUD |
| 92 | clouds: |
| 93 | $OSM_VCA_CLOUDNAME: |
| 94 | type: lxd |
| 95 | auth-types: [certificate] |
| 96 | endpoint: "https://$LXDENDPOINT:8443" |
| 97 | config: |
| 98 | ssl-hostname-verification: false |
| 99 | EOF |
| 100 | openssl req -nodes -new -x509 -keyout /tmp/.osm/client.key -out /tmp/.osm/client.crt -days 365 -subj "/C=FR/ST=Nice/L=Nice/O=ETSI/OU=OSM/CN=osm.etsi.org" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 101 | cat << EOF > $LXD_CREDENTIALS |
| 102 | credentials: |
| 103 | $OSM_VCA_CLOUDNAME: |
| 104 | lxd-cloud: |
| 105 | auth-type: certificate |
| David Garcia | f82897c | 2022-06-01 13:01:43 +0200 | [diff] [blame] | 106 | server-cert: /var/snap/lxd/common/lxd/server.crt |
| 107 | client-cert: /tmp/.osm/client.crt |
| 108 | client-key: /tmp/.osm/client.key |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 109 | EOF |
| 110 | lxc config trust add local: /tmp/.osm/client.crt |
| garciadeblas | 325032a | 2023-04-13 18:07:44 +0200 | [diff] [blame] | 111 | juju add-cloud -c $OSM_NAMESPACE $OSM_VCA_CLOUDNAME $LXD_CLOUD --force |
| 112 | juju add-credential -c $OSM_NAMESPACE $OSM_VCA_CLOUDNAME -f $LXD_CREDENTIALS |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 113 | sg lxd -c "lxd waitready" |
| 114 | juju controller-config features=[k8s-operators] |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 115 | if [ -n "${OSM_BEHIND_PROXY}" ] ; then |
| 116 | if [ -n "${HTTP_PROXY}" ]; then |
| 117 | juju model-default lxd-cloud apt-http-proxy="$HTTP_PROXY" |
| 118 | juju model-default lxd-cloud juju-http-proxy="$HTTP_PROXY" |
| 119 | juju model-default lxd-cloud snap-http-proxy="$HTTP_PROXY" |
| 120 | fi |
| 121 | if [ -n "${HTTPS_PROXY}" ]; then |
| 122 | juju model-default lxd-cloud apt-https-proxy="$HTTPS_PROXY" |
| 123 | juju model-default lxd-cloud juju-https-proxy="$HTTPS_PROXY" |
| 124 | juju model-default lxd-cloud snap-https-proxy="$HTTPS_PROXY" |
| 125 | fi |
| 126 | [ -n "${NO_PROXY}" ] && juju model-default lxd-cloud juju-no-proxy="$NO_PROXY" |
| 127 | fi |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 128 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 129 | } |
| 130 | |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 131 | #Safe unattended install of iptables-persistent |
| 132 | function check_install_iptables_persistent(){ |
| 133 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 134 | echo -e "\nChecking required packages: iptables-persistent" |
| 135 | if ! dpkg -l iptables-persistent &>/dev/null; then |
| 136 | echo -e " Not installed.\nInstalling iptables-persistent requires root privileges" |
| 137 | echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo debconf-set-selections |
| 138 | echo iptables-persistent iptables-persistent/autosave_v6 boolean true | sudo debconf-set-selections |
| 139 | sudo apt-get -yq install iptables-persistent |
| 140 | fi |
| 141 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 142 | } |
| 143 | |
| 144 | function juju_createproxy() { |
| 145 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 146 | check_install_iptables_persistent |
| 147 | |
| 148 | if ! sudo iptables -t nat -C PREROUTING -p tcp -m tcp -d $DEFAULT_IP --dport 17070 -j DNAT --to-destination $OSM_VCA_HOST; then |
| 149 | sudo iptables -t nat -A PREROUTING -p tcp -m tcp -d $DEFAULT_IP --dport 17070 -j DNAT --to-destination $OSM_VCA_HOST |
| 150 | sudo netfilter-persistent save |
| 151 | fi |
| 152 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 153 | } |
| 154 | |
| 155 | DEBUG_INSTALL="" |
| 156 | INSTALL_CACHELXDIMAGES="" |
| 157 | INSTALL_NOJUJU="" |
| Guillermo Calvino | 58d34a8 | 2023-06-26 13:41:57 +0200 | [diff] [blame] | 158 | JUJU_AGENT_VERSION=2.9.43 |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 159 | JUJU_VERSION=2.9 |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 160 | OSM_BEHIND_PROXY="" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 161 | OSM_DEVOPS= |
| garciadeblas | 325032a | 2023-04-13 18:07:44 +0200 | [diff] [blame] | 162 | OSM_NAMESPACE=osm |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 163 | OSM_VCA_HOST= |
| 164 | OSM_VCA_CLOUDNAME="localhost" |
| 165 | OSM_VCA_K8S_CLOUDNAME="k8scloud" |
| 166 | RE_CHECK='^[a-z0-9]([-a-z0-9]*[a-z0-9])?$' |
| 167 | |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 168 | while getopts ":D:i:s:H:l:L:K:-: hP" o; do |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 169 | case "${o}" in |
| 170 | D) |
| 171 | OSM_DEVOPS="${OPTARG}" |
| 172 | ;; |
| 173 | i) |
| 174 | DEFAULT_IP="${OPTARG}" |
| 175 | ;; |
| 176 | s) |
| garciadeblas | 325032a | 2023-04-13 18:07:44 +0200 | [diff] [blame] | 177 | OSM_NAMESPACE="${OPTARG}" && [[ ! "${OPTARG}" =~ $RE_CHECK ]] && echo "Namespace $OPTARG is invalid. Regex used for validation is $RE_CHECK" && exit 0 |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 178 | ;; |
| 179 | H) |
| 180 | OSM_VCA_HOST="${OPTARG}" |
| 181 | ;; |
| 182 | l) |
| 183 | LXD_CLOUD_FILE="${OPTARG}" |
| 184 | ;; |
| 185 | L) |
| 186 | LXD_CRED_FILE="${OPTARG}" |
| 187 | ;; |
| 188 | K) |
| 189 | CONTROLLER_NAME="${OPTARG}" |
| 190 | ;; |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 191 | P) |
| 192 | OSM_BEHIND_PROXY="y" |
| 193 | ;; |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 194 | -) |
| 195 | [ "${OPTARG}" == "help" ] && usage && exit 0 |
| 196 | [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="--debug" && continue |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 197 | [ "${OPTARG}" == "cachelxdimages" ] && INSTALL_CACHELXDIMAGES="y" && continue |
| 198 | echo -e "Invalid option: '--$OPTARG'\n" >&2 |
| 199 | usage && exit 1 |
| 200 | ;; |
| 201 | :) |
| 202 | echo "Option -$OPTARG requires an argument" >&2 |
| 203 | usage && exit 1 |
| 204 | ;; |
| 205 | \?) |
| 206 | echo -e "Invalid option: '-$OPTARG'\n" >&2 |
| 207 | usage && exit 1 |
| 208 | ;; |
| 209 | h) |
| 210 | usage && exit 0 |
| 211 | ;; |
| 212 | *) |
| 213 | usage && exit 1 |
| 214 | ;; |
| 215 | esac |
| 216 | done |
| 217 | |
| 218 | source $OSM_DEVOPS/common/logging |
| 219 | source $OSM_DEVOPS/common/track |
| 220 | |
| 221 | echo "DEBUG_INSTALL=$DEBUG_INSTALL" |
| 222 | echo "DEFAULT_IP=$DEFAULT_IP" |
| garciadeblas | fa3eb33 | 2022-11-15 14:11:56 +0100 | [diff] [blame] | 223 | echo "OSM_BEHIND_PROXY=$OSM_BEHIND_PROXY" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 224 | echo "OSM_DEVOPS=$OSM_DEVOPS" |
| 225 | echo "HOME=$HOME" |
| 226 | |
| garciadeblas | ccb2237 | 2023-06-07 23:50:00 +0200 | [diff] [blame] | 227 | [ -z "$INSTALL_NOJUJU" ] && install_juju_client |
| garciadeblas | 5671dce | 2023-05-18 11:28:16 +0200 | [diff] [blame] | 228 | track juju juju_client_ok |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 229 | |
| 230 | if [ -z "$OSM_VCA_HOST" ]; then |
| 231 | if [ -z "$CONTROLLER_NAME" ]; then |
| 232 | juju_createcontroller_k8s |
| 233 | juju_addlxd_cloud |
| 234 | if [ -n "$LXD_CLOUD_FILE" ]; then |
| garciadeblas | 4290a68 | 2023-06-02 10:29:23 +0200 | [diff] [blame] | 235 | [ -z "$LXD_CRED_FILE" ] && FATAL_TRACK juju "The installer needs the LXD credential yaml if the LXD is external" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 236 | OSM_VCA_CLOUDNAME="lxd-cloud" |
| 237 | juju add-cloud $OSM_VCA_CLOUDNAME $LXD_CLOUD_FILE --force || juju update-cloud $OSM_VCA_CLOUDNAME --client -f $LXD_CLOUD_FILE |
| 238 | juju add-credential $OSM_VCA_CLOUDNAME -f $LXD_CRED_FILE || juju update-credential $OSM_VCA_CLOUDNAME lxd-cloud-creds -f $LXD_CRED_FILE |
| 239 | fi |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 240 | juju_createproxy |
| 241 | else |
| 242 | OSM_VCA_CLOUDNAME="lxd-cloud" |
| 243 | if [ -n "$LXD_CLOUD_FILE" ]; then |
| garciadeblas | 4290a68 | 2023-06-02 10:29:23 +0200 | [diff] [blame] | 244 | [ -z "$LXD_CRED_FILE" ] && FATAL_TRACK juju "The installer needs the LXD credential yaml if the LXD is external" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 245 | juju add-cloud -c $CONTROLLER_NAME $OSM_VCA_CLOUDNAME $LXD_CLOUD_FILE --force || juju update-cloud lxd-cloud -c $CONTROLLER_NAME -f $LXD_CLOUD_FILE |
| 246 | juju add-credential -c $CONTROLLER_NAME $OSM_VCA_CLOUDNAME -f $LXD_CRED_FILE || juju update-credential lxd-cloud -c $CONTROLLER_NAME -f $LXD_CRED_FILE |
| 247 | else |
| 248 | mkdir -p ~/.osm |
| 249 | cat << EOF > ~/.osm/lxd-cloud.yaml |
| 250 | clouds: |
| 251 | lxd-cloud: |
| 252 | type: lxd |
| 253 | auth-types: [certificate] |
| 254 | endpoint: "https://$DEFAULT_IP:8443" |
| 255 | config: |
| 256 | ssl-hostname-verification: false |
| 257 | EOF |
| 258 | openssl req -nodes -new -x509 -keyout ~/.osm/client.key -out ~/.osm/client.crt -days 365 -subj "/C=FR/ST=Nice/L=Nice/O=ETSI/OU=OSM/CN=osm.etsi.org" |
| 259 | local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'` |
| 260 | local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'` |
| 261 | local client_key=`cat ~/.osm/client.key | sed 's/^/ /'` |
| 262 | cat << EOF > ~/.osm/lxd-credentials.yaml |
| 263 | credentials: |
| 264 | lxd-cloud: |
| 265 | lxd-cloud: |
| 266 | auth-type: certificate |
| 267 | server-cert: | |
| 268 | $server_cert |
| 269 | client-cert: | |
| 270 | $client_cert |
| 271 | client-key: | |
| 272 | $client_key |
| 273 | EOF |
| 274 | lxc config trust add local: ~/.osm/client.crt |
| 275 | juju add-cloud -c $CONTROLLER_NAME $OSM_VCA_CLOUDNAME ~/.osm/lxd-cloud.yaml --force || juju update-cloud lxd-cloud -c $CONTROLLER_NAME -f ~/.osm/lxd-cloud.yaml |
| 276 | juju add-credential -c $CONTROLLER_NAME $OSM_VCA_CLOUDNAME -f ~/.osm/lxd-credentials.yaml || juju update-credential lxd-cloud -c $CONTROLLER_NAME -f ~/.osm/lxd-credentials.yaml |
| 277 | fi |
| 278 | fi |
| garciadeblas | 325032a | 2023-04-13 18:07:44 +0200 | [diff] [blame] | 279 | [ -z "$CONTROLLER_NAME" ] && OSM_VCA_HOST=`sg lxd -c "juju show-controller $OSM_NAMESPACE"|grep api-endpoints|awk -F\' '{print $2}'|awk -F\: '{print $1}'` |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 280 | [ -n "$CONTROLLER_NAME" ] && OSM_VCA_HOST=`juju show-controller $CONTROLLER_NAME |grep api-endpoints|awk -F\' '{print $2}'|awk -F\: '{print $1}'` |
| garciadeblas | 4290a68 | 2023-06-02 10:29:23 +0200 | [diff] [blame] | 281 | [ -z "$OSM_VCA_HOST" ] && FATAL_TRACK juju "Cannot obtain juju controller IP address" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 282 | fi |
| garciadeblas | 4d89c37 | 2021-11-25 11:57:18 +0100 | [diff] [blame] | 283 | track juju juju_controller_ok |