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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 JUJU_AGENT_VERSION
=2.9.42
21 K8S_CLOUD_NAME
="k8s-cloud"
22 KUBECTL
="microk8s.kubectl"
24 OSMCLIENT_VERSION
=10.0
25 IMAGES_OVERLAY_FILE
=~
/.osm
/images-overlay.yaml
26 PATH
=/snap
/bin
:${PATH}
27 OSM_DEVOPS
="$( cd "$
( dirname "${BASH_SOURCE[0]}" )"/.. &> /dev/null && pwd )"
28 if [ -f ${OSM_DEVOPS}/common
/all_funcs
] ; then
29 source ${OSM_DEVOPS}/common
/all_funcs
34 function FATAL_TRACK
(){
41 duration
=$
((ctime
- SESSION_ID
))
42 url
="http://www.woopra.com/track/ce?project=osm.etsi.org&cookie=${SESSION_ID}"
44 event_name
="${event_name}_$1"
45 url
="${url}&event=${event_name}&ce_duration=${duration}"
46 wget
-q -O /dev
/null
$url
49 function FATAL_TRACK
(){
58 OSM_HA_BUNDLE
=ch
:osm-ha
59 CHARMHUB_CHANNEL
=10.0/candidate
62 function check_arguments
(){
63 while [ $# -gt 0 ] ; do
65 --bundle) BUNDLE
="$2" ;;
66 --overlay) OVERLAY
="$2" ;;
67 --k8s) KUBECFG
="$2" ;;
68 --vca) CONTROLLER
="$2" ;;
69 --lxd) LXD_CLOUD
="$2" ;;
70 --lxd-cred) LXD_CREDENTIALS
="$2" ;;
71 --microstack) MICROSTACK
=y
;;
72 --ha) BUNDLE
=$OSM_HA_BUNDLE ;;
74 --registry) REGISTRY_INFO
="$2" ;;
75 --only-vca) ONLY_VCA
=y
;;
80 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
83 function install_snaps
(){
84 if [ ! -v KUBECFG
]; then
86 sudo snap
install microk8s
--classic --channel=${MICROK8S_VERSION}/stable ||
87 FATAL_TRACK k8scluster
"snap install microk8s ${MICROK8S_VERSION}/stable failed"
88 sudo usermod
-a -G microk8s
`whoami`
89 sudo
cat /var
/snap
/microk8s
/current
/args
/kube-apiserver |
grep advertise-address ||
(
90 echo "--advertise-address $DEFAULT_IP" | sudo
tee -a /var
/snap
/microk8s
/current
/args
/kube-apiserver
91 sg
${KUBEGRP} -c microk8s.stop
92 sg
${KUBEGRP} -c microk8s.start
95 sudo chown
-f -R `whoami` ~
/.kube
96 sg
${KUBEGRP} -c "microk8s status --wait-ready"
97 KUBECONFIG
=~
/.osm
/microk8s-config.yaml
98 sg
${KUBEGRP} -c "microk8s config" |
tee ${KUBECONFIG}
99 track k8scluster k8scluster_ok
102 sudo snap
install kubectl
--classic
103 export KUBECONFIG
=${KUBECFG}
106 sudo snap
install juju
--classic --channel=$JUJU_VERSION/stable ||
107 FATAL_TRACK juju
"snap install juju ${JUJU_VERSION}/stable failed"
111 function bootstrap_k8s_lxd
(){
112 [ -v CONTROLLER
] && ADD_K8S_OPTS
="--controller ${CONTROLLER}" && CONTROLLER_NAME
=$CONTROLLER
113 [ ! -v CONTROLLER
] && ADD_K8S_OPTS
="--client" && BOOTSTRAP_NEEDED
="yes" && CONTROLLER_NAME
="osm-vca"
115 if [ -v BOOTSTRAP_NEEDED
]; then
116 CONTROLLER_PRESENT
=$
(juju controllers
2>/dev
/null|
grep ${CONTROLLER_NAME} |
wc -l)
117 if [ $CONTROLLER_PRESENT -ge 1 ]; then
119 Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
120 You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
123 juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
125 Please retry the installation once this conflict has been resolved.
127 FATAL_TRACK bootstrap_k8s
"VCA already present"
130 CONTROLLER_PRESENT
=$
(juju controllers
2>/dev
/null|
grep ${CONTROLLER_NAME} |
wc -l)
131 if [ $CONTROLLER_PRESENT -le 0 ]; then
133 Threre is no VCA present with the name "${CONTROLLER_NAME}". Please specify a VCA
134 that exists, or remove the --vca ${CONTROLLER_NAME} option.
136 Please retry the installation with one of the solutions applied.
138 FATAL_TRACK bootstrap_k8s
"Requested VCA not present"
142 if [ -v KUBECFG
]; then
143 cat $KUBECFG | juju add-k8s
$K8S_CLOUD_NAME $ADD_K8S_OPTS
144 [ -v BOOTSTRAP_NEEDED
] && juju bootstrap
$K8S_CLOUD_NAME $CONTROLLER_NAME \
145 --config controller-service-type
=loadbalancer \
146 --agent-version=$JUJU_AGENT_VERSION
148 sg
${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.
enable metallb
"
149 sg ${KUBEGRP} -c "microk8s.
enable ingress
"
150 sg ${KUBEGRP} -c "microk8s.
enable hostpath-storage dns
"
152 start_time="$
(date -u +%s
)"
156 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
157 echo "Microk8s storage failed to
enable"
158 sg ${KUBEGRP} -c "microk8s.status
"
159 FATAL_TRACK bootstrap_k8s "Microk8s storage failed to
enable"
161 storage_status=`sg ${KUBEGRP} -c "microk8s.status
-a storage
"`
162 if [[ $storage_status == "enabled
" ]]; then
168 [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config
" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
169 [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \
170 "juju bootstrap microk8s
$CONTROLLER_NAME --config controller-service-type
=loadbalancer
--agent-version=$JUJU_AGENT_VERSION" \
171 && K8S_CLOUD_NAME=microk8s
173 track bootstrap_k8s bootstrap_k8s_ok
175 if [ -v LXD_CLOUD ]; then
176 if [ ! -v LXD_CREDENTIALS ]; then
177 echo "The installer needs the LXD server certificate
if the LXD is external
"
178 FATAL_TRACK bootstrap_lxd "No LXD certificate supplied
"
181 LXDENDPOINT=$DEFAULT_IP
182 LXD_CLOUD=~/.osm/lxd-cloud.yaml
183 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
184 # Apply sysctl production values for optimal performance
185 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
188 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
189 sudo snap install lxd --channel $LXD_VERSION/stable
191 sudo usermod -a -G lxd `whoami`
192 cat /usr/share/osm-devops/installers/lxd-preseed.conf | sed 's/^config: {}/config:\n core.https_address: '$LXDENDPOINT':8443/' | sg lxd -c "lxd init
--preseed"
193 sg lxd -c "lxd waitready
"
194 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
195 sg lxd -c "lxc profile device
set default eth0 mtu
$DEFAULT_MTU"
196 sg lxd -c "lxc network
set lxdbr0 bridge.mtu
$DEFAULT_MTU"
198 cat << EOF > $LXD_CLOUD
202 auth-types: [certificate]
203 endpoint: "https
://$LXDENDPOINT:8443"
205 ssl-hostname-verification: false
207 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
"
208 cat << EOF > $LXD_CREDENTIALS
212 auth-type: certificate
213 server-cert: /var/snap/lxd/common/lxd/server.crt
214 client-cert: ~/.osm/client.crt
215 client-key: ~/.osm/client.key
217 lxc config trust add local: ~/.osm/client.crt
220 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
221 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
222 sg lxd -c "lxd waitready
"
223 juju controller-config features=[k8s-operators]
224 track bootstrap_lxd bootstrap_lxd_ok
227 function deploy_charmed_osm(){
228 if [ -v REGISTRY_INFO ] ; then
229 registry_parts=(${REGISTRY_INFO//@/ })
230 if [ ${#registry_parts[@]} -eq 1 ] ; then
231 # No credentials supplied
234 REGISTRY_URL=${registry_parts[0]}
236 credentials=${registry_parts[0]}
237 credential_parts=(${credentials//:/ })
238 REGISTRY_USERNAME=${credential_parts[0]}
239 REGISTRY_PASSWORD=${credential_parts[1]}
240 REGISTRY_URL=${registry_parts[1]}
242 # Ensure the URL ends with a /
243 case $REGISTRY_URL in
245 *) REGISTRY_URL=${REGISTRY_URL}/
249 echo "Creating OSM model
"
250 if [ -v KUBECFG ]; then
251 juju add-model $MODEL_NAME $K8S_CLOUD_NAME
253 sg ${KUBEGRP} -c "juju add-model
$MODEL_NAME $K8S_CLOUD_NAME"
255 echo "Deploying OSM with charms
"
257 if [ -v REGISTRY_URL ]; then
258 [ ! -v TAG ] && TAG='latest'
260 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
262 if [ -v OVERLAY ]; then
263 extra_overlay="--overlay $OVERLAY"
266 if [ -v BUNDLE ]; then
267 juju deploy --trust --channel $CHARMHUB_CHANNEL -m $MODEL_NAME $BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_overlay
269 juju deploy --trust --channel $CHARMHUB_CHANNEL -m $MODEL_NAME $OSM_BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_overlay
272 if [ ! -v KUBECFG ]; then
273 API_SERVER=${DEFAULT_IP}
275 API_SERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
276 proto="$
(echo $API_SERVER |
grep :// |
sed -e's,^\(.*://\).*,\1,g')"
277 url="$
(echo ${API_SERVER/$proto/})"
278 user="$
(echo $url |
grep @ | cut
-d@
-f1)"
279 hostport="$
(echo ${url/$user@/} | cut
-d/ -f1)"
280 API_SERVER="$
(echo $hostport |
sed -e 's,:.*,,g')"
282 # Expose OSM services
283 juju config -m $MODEL_NAME nbi site_url=https://nbi.${API_SERVER}.nip.io
284 juju config -m $MODEL_NAME ng-ui site_url=https://ui.${API_SERVER}.nip.io
285 juju config -m $MODEL_NAME grafana site_url=https://grafana.${API_SERVER}.nip.io
286 juju config -m $MODEL_NAME prometheus site_url=https://prometheus.${API_SERVER}.nip.io
288 echo "Waiting
for deployment to finish...
"
290 grafana_leader=`juju status -m $MODEL_NAME grafana | grep "*" | cut -d "*" -f 1`
291 grafana_admin_password=`juju run -m $MODEL_NAME --unit $grafana_leader "echo \\$GF_SECURITY_ADMIN_PASSWORD"`
292 juju config -m $MODEL_NAME mon grafana_password=$grafana_admin_password
293 echo "OSM with charms deployed
"
296 function check_osm_deployed() {
298 start_time="$
(date -u +%s
)"
299 total_service_count=14
303 service_count=$(juju status --format json -m $MODEL_NAME | jq '.applications[]."application-status
".current' | grep active | wc -l)
304 echo "$service_count / $total_service_count services active
"
305 if [ $service_count -eq $total_service_count ]; then
308 if [ $service_count -ne $previous_count ]; then
309 previous_count=$service_count
310 start_time="$
(date -u +%s
)"
313 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
314 echo "Timed out waiting
for OSM services to become ready
"
315 FATAL_TRACK deploy_osm "Timed out waiting
for services to become ready
"
321 function create_overlay() {
324 local HOME=/home/$USER
325 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
326 local vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
327 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
328 local vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
329 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
330 local vca_cloud="lxd-cloud
"
331 # Get the VCA Certificate
332 local vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
334 # Calculate the default route of this machine
335 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
337 # Generate a new overlay.yaml, overriding any existing one
338 cat << EOF > /tmp/vca-overlay.yaml
343 vca_secret: $vca_secret
346 vca_pubkey: $vca_pubkey
347 vca_cacert: $vca_cacert
348 vca_cloud: $vca_cloud
349 vca_k8s_cloud: $K8S_CLOUD_NAME
353 vca_secret: $vca_secret
355 vca_cacert: $vca_cacert
357 mv /tmp/vca-overlay.yaml ~/.osm/
358 OSM_VCA_HOST=$vca_host
361 function generate_images_overlay(){
362 echo "applications
:" > /tmp/images-overlay.yaml
364 charms_with_resources="nbi lcm mon pol ng-ui ro pla
"
365 for charm in $charms_with_resources; do
366 cat << EOF > /tmp/${charm}_registry.yaml
367 registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
369 if [ ! -z "$REGISTRY_USERNAME" ] ; then
370 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
371 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
374 cat << EOF >> /tmp/images-overlay.yaml
377 image: /tmp/${charm}_registry.yaml
381 ch_charms_with_resources="keystone
"
382 for charm in $ch_charms_with_resources; do
383 cat << EOF > /tmp/${charm}_registry.yaml
384 registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
386 if [ ! -z "$REGISTRY_USERNAME" ] ; then
387 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
388 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
391 cat << EOF >> /tmp/images-overlay.yaml
394 ${charm}-image: /tmp/${charm}_registry.yaml
399 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
402 function refresh_osmclient_snap() {
403 osmclient_snap_install_refresh refresh
406 function install_osm_client_snap() {
407 osmclient_snap_install_refresh install
410 function osmclient_snap_install_refresh() {
411 channel_preference="stable candidate beta edge
"
412 for channel in $channel_preference; do
413 echo "Trying to
install osmclient from channel
$OSMCLIENT_VERSION/$channel"
414 sudo snap $1 osmclient --channel $OSMCLIENT_VERSION/$channel 2> /dev/null && echo osmclient snap installed && break
417 function install_osmclient() {
418 snap info osmclient | grep -E ^installed: && refresh_osmclient_snap || install_osm_client_snap
421 function add_local_k8scluster() {
422 osm --all-projects vim-create \
423 --name _system-osm-vim \
424 --account_type dummy \
425 --auth_url http://dummy \
426 --user osm --password osm --tenant osm \
427 --description "dummy
" \
428 --config '{management_network_name: mgmt}'
429 tmpfile=$(mktemp --tmpdir=${HOME})
430 cp ${KUBECONFIG} ${tmpfile}
431 osm --all-projects k8scluster-add \
433 --vim _system-osm-vim \
434 --k8s-nets '{"net1
": null}' \
436 --description "OSM Internal Cluster
" \
441 function install_microstack() {
442 sudo snap install microstack --beta --devmode
444 CHECK=$(microstack.openstack server list)
445 if [ $? -ne 0 ] ; then
446 if [[ $CHECK == *"not initialized
"* ]]; then
447 echo "Setting MicroStack dashboard to listen to port
8080"
448 sudo snap set microstack config.network.ports.dashboard=8080
449 echo "Initializing MicroStack. This can take several minutes
"
450 sudo microstack.init --auto --control
454 sudo snap alias microstack.openstack openstack
456 echo "Updating default security group
in MicroStack to allow all access
"
458 for i in $(microstack.openstack security group list | awk '/default/{ print $2 }'); do
459 for PROTO in icmp tcp udp ; do
460 echo " $PROTO ingress
"
461 CHECK=$(microstack.openstack security group rule create $i --protocol $PROTO --remote-ip 0.0.0.0/0 2>&1)
462 if [ $? -ne 0 ] ; then
463 if [[ $CHECK != *"409"* ]]; then
464 echo "Error creating ingress rule
for $PROTO"
471 microstack.openstack network show osm-ext &>/dev/null
472 if [ $? -ne 0 ]; then
473 echo "Creating osm-ext network with router to bridge to MicroStack external network
"
474 microstack.openstack network create --enable --no-share osm-ext
475 microstack.openstack subnet create osm-ext-subnet --network osm-ext --dns-nameserver 8.8.8.8 \
476 --subnet-range 172.30.0.0/24
477 microstack.openstack router create external-router
478 microstack.openstack router add subnet external-router osm-ext-subnet
479 microstack.openstack router set --external-gateway external external-router
482 microstack.openstack image list | grep ubuntu20.04 &> /dev/null
483 if [ $? -ne 0 ] ; then
484 echo "Fetching Ubuntu
20.04 image and upLoading to MicroStack
"
485 wget -q -O- https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img \
486 | microstack.openstack image create --public --container-format=bare \
487 --disk-format=qcow2 ubuntu20.04 | grep status
490 if [ ! -f ~/.ssh/microstack ]; then
491 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
492 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
495 echo "Creating VIM microstack-site
in OSM
"
496 . /var/snap/microstack/common/etc/microstack.rc
499 --name microstack-site \
500 --user "$OS_USERNAME" \
501 --password "$OS_PASSWORD" \
502 --auth_url "$OS_AUTH_URL" \
503 --tenant "$OS_USERNAME" \
504 --account_type openstack \
505 --config='{use_floating_ip: True,
508 management_network_name: osm-ext}'
511 DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
512 DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
519 if [ -v ONLY_VCA ]; then
521 vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
522 vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
523 vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
524 vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
525 vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
526 vca_cloud="lxd-cloud
"
527 vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
528 hostname=`cat /etc/hostname`
530 echo "Use the following
command to register the installed VCA to your OSM
:"
531 echo -e " osm vca-add
--endpoints $vca_host:$vca_port \\\n --user $vca_user \\\n --secret $vca_secret \\\n --cacert $vca_cacert \\\n --lxd-cloud lxd-cloud
\\\n --lxd-credentials lxd-cloud
\\\n --k8s-cloud microk8s
\\\n --k8s-credentials microk8s
\\\n $hostname-vca"
532 track deploy_osm deploy_vca_only_ok
535 track deploy_osm deploy_osm_services_k8s_ok
537 track osmclient osmclient_ok
538 export OSM_HOSTNAME=$(juju config -m $MODEL_NAME nbi site_url | sed "s
/http.
*\?:\
/\
///"):443
541 track final_ops add_local_k8scluster_ok
542 if [ -v MICROSTACK ]; then
544 track final_ops install_microstack_ok
547 echo "Your installation is now complete
, follow these steps
for configuring the osmclient
:"
549 echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP
"
551 echo "export OSM_HOSTNAME
=$OSM_HOSTNAME"
553 echo "2. Add the previous
command to your .bashrc
for other Shell sessions
"
555 echo "echo \"export OSM_HOSTNAME
=$OSM_HOSTNAME\" >> ~
/.bashrc
"