blob: 7be5f99cbaa4a231bbe9fbaf8564cb8392b81503 [file] [log] [blame]
garciadeblas0bc87522021-10-20 22:16:17 +02001#!/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
16function 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)"
garciadeblas0bc87522021-10-20 22:16:17 +020032 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
33}
34
35function 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
garciadeblasccb22372023-06-07 23:50:00 +020042function install_juju_client() {
garciadeblas0bc87522021-10-20 22:16:17 +020043 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblasccb22372023-06-07 23:50:00 +020044 echo "Installing juju client"
garciadeblas0bc87522021-10-20 22:16:17 +020045 sudo snap install juju --classic --channel=$JUJU_VERSION/stable
46 [[ ":$PATH": != *":/snap/bin:"* ]] && PATH="/snap/bin:${PATH}"
47 [ -n "$INSTALL_CACHELXDIMAGES" ] && update_juju_images
garciadeblasccb22372023-06-07 23:50:00 +020048 echo "Finished installation of juju client"
garciadeblas0bc87522021-10-20 22:16:17 +020049 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
50 return 0
51}
52
53function juju_createcontroller_k8s(){
54 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
55 cat $HOME/.kube/config | juju add-k8s $OSM_VCA_K8S_CLOUDNAME --client \
garciadeblas4290a682023-06-02 10:29:23 +020056 || FATAL_TRACK juju "Failed to add K8s endpoint and credential for client in cloud $OSM_VCA_K8S_CLOUDNAME"
garciadeblasfa3eb332022-11-15 14:11:56 +010057
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
65apt-http-proxy: ${HTTP_PROXY}
66apt-https-proxy: ${HTTPS_PROXY}
67juju-http-proxy: ${HTTP_PROXY}
68juju-https-proxy: ${HTTPS_PROXY}
69juju-no-proxy: ${NO_PROXY}
70snap-http-proxy: ${HTTP_PROXY}
71snap-https-proxy: ${HTTPS_PROXY}
72EOF
73 JUJU_BOOTSTRAP_OPTS="--model-default /tmp/.osm/model-config.yaml"
74 fi
garciadeblas325032a2023-04-13 18:07:44 +020075 juju bootstrap -v --debug $OSM_VCA_K8S_CLOUDNAME $OSM_NAMESPACE \
garciadeblas0bc87522021-10-20 22:16:17 +020076 --config controller-service-type=loadbalancer \
77 --agent-version=$JUJU_AGENT_VERSION \
garciadeblasfa3eb332022-11-15 14:11:56 +010078 ${JUJU_BOOTSTRAP_OPTS} \
garciadeblas4290a682023-06-02 10:29:23 +020079 || FATAL_TRACK juju "Failed to bootstrap controller $OSM_NAMESPACE in cloud $OSM_VCA_K8S_CLOUDNAME"
garciadeblas0bc87522021-10-20 22:16:17 +020080 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
81}
82
83function 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
92clouds:
93 $OSM_VCA_CLOUDNAME:
94 type: lxd
95 auth-types: [certificate]
96 endpoint: "https://$LXDENDPOINT:8443"
97 config:
98 ssl-hostname-verification: false
99EOF
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"
garciadeblas0bc87522021-10-20 22:16:17 +0200101 cat << EOF > $LXD_CREDENTIALS
102credentials:
103 $OSM_VCA_CLOUDNAME:
104 lxd-cloud:
105 auth-type: certificate
David Garciaf82897c2022-06-01 13:01:43 +0200106 server-cert: /var/snap/lxd/common/lxd/server.crt
107 client-cert: /tmp/.osm/client.crt
108 client-key: /tmp/.osm/client.key
garciadeblas0bc87522021-10-20 22:16:17 +0200109EOF
110 lxc config trust add local: /tmp/.osm/client.crt
garciadeblas325032a2023-04-13 18:07:44 +0200111 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
garciadeblas0bc87522021-10-20 22:16:17 +0200113 sg lxd -c "lxd waitready"
114 juju controller-config features=[k8s-operators]
garciadeblasfa3eb332022-11-15 14:11:56 +0100115 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
garciadeblas0bc87522021-10-20 22:16:17 +0200128 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
129}
130
garciadeblas0bc87522021-10-20 22:16:17 +0200131#Safe unattended install of iptables-persistent
132function 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
144function 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
155DEBUG_INSTALL=""
156INSTALL_CACHELXDIMAGES=""
157INSTALL_NOJUJU=""
Guillermo Calvino58d34a82023-06-26 13:41:57 +0200158JUJU_AGENT_VERSION=2.9.43
garciadeblas0bc87522021-10-20 22:16:17 +0200159JUJU_VERSION=2.9
garciadeblasfa3eb332022-11-15 14:11:56 +0100160OSM_BEHIND_PROXY=""
garciadeblas0bc87522021-10-20 22:16:17 +0200161OSM_DEVOPS=
garciadeblas325032a2023-04-13 18:07:44 +0200162OSM_NAMESPACE=osm
garciadeblas0bc87522021-10-20 22:16:17 +0200163OSM_VCA_HOST=
164OSM_VCA_CLOUDNAME="localhost"
165OSM_VCA_K8S_CLOUDNAME="k8scloud"
166RE_CHECK='^[a-z0-9]([-a-z0-9]*[a-z0-9])?$'
167
garciadeblasfa3eb332022-11-15 14:11:56 +0100168while getopts ":D:i:s:H:l:L:K:-: hP" o; do
garciadeblas0bc87522021-10-20 22:16:17 +0200169 case "${o}" in
170 D)
171 OSM_DEVOPS="${OPTARG}"
172 ;;
173 i)
174 DEFAULT_IP="${OPTARG}"
175 ;;
176 s)
garciadeblas325032a2023-04-13 18:07:44 +0200177 OSM_NAMESPACE="${OPTARG}" && [[ ! "${OPTARG}" =~ $RE_CHECK ]] && echo "Namespace $OPTARG is invalid. Regex used for validation is $RE_CHECK" && exit 0
garciadeblas0bc87522021-10-20 22:16:17 +0200178 ;;
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 ;;
garciadeblasfa3eb332022-11-15 14:11:56 +0100191 P)
192 OSM_BEHIND_PROXY="y"
193 ;;
garciadeblas0bc87522021-10-20 22:16:17 +0200194 -)
195 [ "${OPTARG}" == "help" ] && usage && exit 0
196 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="--debug" && continue
garciadeblas0bc87522021-10-20 22:16:17 +0200197 [ "${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
216done
217
218source $OSM_DEVOPS/common/logging
219source $OSM_DEVOPS/common/track
220
221echo "DEBUG_INSTALL=$DEBUG_INSTALL"
222echo "DEFAULT_IP=$DEFAULT_IP"
garciadeblasfa3eb332022-11-15 14:11:56 +0100223echo "OSM_BEHIND_PROXY=$OSM_BEHIND_PROXY"
garciadeblas0bc87522021-10-20 22:16:17 +0200224echo "OSM_DEVOPS=$OSM_DEVOPS"
225echo "HOME=$HOME"
226
garciadeblasccb22372023-06-07 23:50:00 +0200227[ -z "$INSTALL_NOJUJU" ] && install_juju_client
garciadeblas5671dce2023-05-18 11:28:16 +0200228track juju juju_client_ok
garciadeblas0bc87522021-10-20 22:16:17 +0200229
230if [ -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
garciadeblas4290a682023-06-02 10:29:23 +0200235 [ -z "$LXD_CRED_FILE" ] && FATAL_TRACK juju "The installer needs the LXD credential yaml if the LXD is external"
garciadeblas0bc87522021-10-20 22:16:17 +0200236 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
garciadeblas0bc87522021-10-20 22:16:17 +0200240 juju_createproxy
241 else
242 OSM_VCA_CLOUDNAME="lxd-cloud"
243 if [ -n "$LXD_CLOUD_FILE" ]; then
garciadeblas4290a682023-06-02 10:29:23 +0200244 [ -z "$LXD_CRED_FILE" ] && FATAL_TRACK juju "The installer needs the LXD credential yaml if the LXD is external"
garciadeblas0bc87522021-10-20 22:16:17 +0200245 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
250clouds:
251 lxd-cloud:
252 type: lxd
253 auth-types: [certificate]
254 endpoint: "https://$DEFAULT_IP:8443"
255 config:
256 ssl-hostname-verification: false
257EOF
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
263credentials:
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
273EOF
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
garciadeblas325032a2023-04-13 18:07:44 +0200279 [ -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}'`
garciadeblas0bc87522021-10-20 22:16:17 +0200280 [ -n "$CONTROLLER_NAME" ] && OSM_VCA_HOST=`juju show-controller $CONTROLLER_NAME |grep api-endpoints|awk -F\' '{print $2}'|awk -F\: '{print $1}'`
garciadeblas4290a682023-06-02 10:29:23 +0200281 [ -z "$OSM_VCA_HOST" ] && FATAL_TRACK juju "Cannot obtain juju controller IP address"
garciadeblas0bc87522021-10-20 22:16:17 +0200282fi
garciadeblas4d89c372021-11-25 11:57:18 +0100283track juju juju_controller_ok