Node-exporter idempotent installation
[osm/devops.git] / installers / full_install_osm.sh
index 4725287..e759c56 100755 (executable)
@@ -53,6 +53,7 @@ function usage(){
     echo -e "     --soui:         install classic build of OSM (Rel THREE v3.1, based on LXD containers, with SO and UI)"
     echo -e "     --lxdimages:    (only for Rel THREE with --soui) download lxd images from OSM repository instead of creating them from scratch"
     echo -e "     --pullimages:   pull/run osm images from docker.io/opensourcemano"
+    echo -e "     --k8s_monitor:  install the OSM kubernetes moitoring with prometheus and grafana"
     echo -e "     -l <lxd_repo>:  (only for Rel THREE with --soui) use specified repository url for lxd images"
     echo -e "     -p <path>:      (only for Rel THREE with --soui) use specified repository path for lxd images"
 #    echo -e "     --reconfigure:  reconfigure the modules (DO NOT change NAT rules)"
@@ -185,8 +186,13 @@ function uninstall_lightweight() {
     else
         echo -e "\nUninstalling OSM"
         if [ -n "$KUBERNETES" ]; then
+            if [ -n "$K8S_MONITOR" ]; then
+                # uninstall OSM MONITORING
+                uninstall_k8s_monitoring
+            fi
             remove_k8s_namespace $OSM_STACK_NAME
         else
+
             remove_stack $OSM_STACK_NAME
             remove_stack osm_elk
         fi
@@ -597,16 +603,38 @@ function install_osmclient(){
 }
 
 function install_prometheus_nodeexporter(){
-    sudo useradd --no-create-home --shell /bin/false node_exporter
-    sudo wget -q https://github.com/prometheus/node_exporter/releases/download/v$PROMETHEUS_NODE_EXPORTER_TAG/node_exporter-$PROMETHEUS_NODE_EXPORTER_TAG.linux-amd64.tar.gz  -P /tmp/
-    sudo tar -C /tmp -xf /tmp/node_exporter-$PROMETHEUS_NODE_EXPORTER_TAG.linux-amd64.tar.gz
-    sudo cp /tmp/node_exporter-$PROMETHEUS_NODE_EXPORTER_TAG.linux-amd64/node_exporter /usr/local/bin
-    sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter
-    sudo rm -rf node_exporter-$PROMETHEUS_NODE_EXPORTER_TAG.linux-amd64*
-    sudo cp ${OSM_DEVOPS}/installers/docker/files/node_exporter.service /etc/systemd/system/node_exporter.service
+    if (systemctl -q is-active node_exporter)
+        then
+            echo "Node Exporter is already running."
+        else
+            echo "Node Exporter is not active, installing..."
+            if getent passwd node_exporter > /dev/null 2>&1; then
+                echo "node_exporter user exists"
+            else
+                echo "Creating user node_exporter"
+                sudo useradd --no-create-home --shell /bin/false node_exporter
+            fi
+            sudo wget -q https://github.com/prometheus/node_exporter/releases/download/v$PROMETHEUS_NODE_EXPORTER_TAG/node_exporter-$PROMETHEUS_NODE_EXPORTER_TAG.linux-amd64.tar.gz  -P /tmp/
+            sudo tar -C /tmp -xf /tmp/node_exporter-$PROMETHEUS_NODE_EXPORTER_TAG.linux-amd64.tar.gz
+            sudo cp /tmp/node_exporter-$PROMETHEUS_NODE_EXPORTER_TAG.linux-amd64/node_exporter /usr/local/bin
+            sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter
+            sudo rm -rf node_exporter-$PROMETHEUS_NODE_EXPORTER_TAG.linux-amd64*
+            sudo cp ${OSM_DEVOPS}/installers/docker/files/node_exporter.service /etc/systemd/system/node_exporter.service
+            sudo systemctl daemon-reload
+            sudo systemctl restart node_exporter
+            sudo systemctl enable node_exporter
+            echo "Node Exporter has been activated in this host."
+    fi
+    return 0
+}
+
+function uninstall_prometheus_nodeexporter(){
+    sudo systemctl stop node_exporter
+    sudo systemctl disable node_exporter
+    sudo rm /etc/systemd/system/node_exporter.service
     sudo systemctl daemon-reload
-    sudo systemctl restart node_exporter
-    sudo systemctl enable node_exporter
+    sudo userdel node_exporter
+    sudo rm /usr/local/bin/node_exporter
     return 0
 }
 
@@ -677,6 +705,7 @@ function install_juju() {
     echo "Installing juju"
     sudo snap install juju --classic
     [ -z "$INSTALL_NOLXD" ] && sudo dpkg-reconfigure -p medium lxd
+    [[ ":$PATH": != *":/snap/bin:"* ]] && PATH="/snap/bin:${PATH}"
     echo "Finished installation of juju"
     return 0
 }
@@ -926,6 +955,13 @@ function generate_docker_env_files() {
         $WORKDIR_SUDO sed -i "s|OSMMON_VCA_SECRET.*|OSMMON_VCA_SECRET=$OSM_VCA_SECRET|g" $OSM_DOCKER_WORK_DIR/mon.env
     fi
 
+    if ! grep -Fq "OSMMON_VCA_CACERT" $OSM_DOCKER_WORK_DIR/mon.env; then
+       echo "OSMMON_VCA_CACERT=${OSM_VCA_CACERT}" | $WORKDIR_SUDO tee -a $OSM_DOCKER_WORK_DIR/mon.env
+    else
+       $WORKDIR_SUDO sed -i "s|OSMMON_VCA_CACERT.*|OSMMON_VCA_CACERT=${OSM_VCA_CACERT}|g" $OSM_DOCKER_WORK_DIR/mon.env
+    fi
+
+
     # POL
     if [ ! -f $OSM_DOCKER_WORK_DIR/pol.env ]; then
         echo "OSMPOL_SQL_DATABASE_URI=mysql://root:${MYSQL_ROOT_PASSWORD}@mysql:3306/pol" | $WORKDIR_SUDO tee -a $OSM_DOCKER_WORK_DIR/pol.env
@@ -1039,6 +1075,7 @@ function deploy_lightweight() {
     OSM_UI_PORT=80
     OSM_MON_PORT=8662
     OSM_PROM_PORT=9090
+    OSM_PROM_CADVISOR_PORT=8080
     OSM_PROM_HOSTPORT=9091
     OSM_GRAFANA_PORT=3000
     [ -n "$INSTALL_ELK" ] && OSM_ELK_PORT=5601
@@ -1051,6 +1088,7 @@ function deploy_lightweight() {
         OSM_PORTS+=(OSM_UI_PORTS=$OSM_UI_PORT)
         OSM_PORTS+=(OSM_MON_PORTS=$OSM_MON_PORT)
         OSM_PORTS+=(OSM_PROM_PORTS=$OSM_PROM_PORT)
+        OSM_PORTS+=(OSM_PROM_CADVISOR_PORTS=$OSM_PROM_CADVISOR_PORT)
         OSM_PORTS+=(OSM_GRAFANA_PORTS=$OSM_GRAFANA_PORT)
         #[ -n "$INSTALL_PERFMON" ] && OSM_PORTS+=(OSM_PM_PORTS=$OSM_PM_PORT)
         [ -n "$INSTALL_ELK" ] && OSM_PORTS+=(OSM_ELK_PORTS=$OSM_ELK_PORT)
@@ -1061,6 +1099,7 @@ function deploy_lightweight() {
         OSM_PORTS+=(OSM_UI_PORTS=$OSM_UI_PORT:$OSM_UI_PORT)
         OSM_PORTS+=(OSM_MON_PORTS=$OSM_MON_PORT:$OSM_MON_PORT)
         OSM_PORTS+=(OSM_PROM_PORTS=$OSM_PROM_HOSTPORT:$OSM_PROM_PORT)
+        OSM_PORTS+=(OSM_PROM_CADVISOR_PORTS=$OSM_PROM_CADVISOR_PORT:$OSM_PROM_CADVISOR_PORT)
         OSM_PORTS+=(OSM_GRAFANA_PORTS=$OSM_GRAFANA_PORT:$OSM_GRAFANA_PORT)
         #[ -n "$INSTALL_PERFMON" ] && OSM_PORTS+=(OSM_PM_PORTS=$OSM_PM_PORT:$OSM_PM_PORT)
         [ -n "$INSTALL_ELK" ] && OSM_PORTS+=(OSM_ELK_PORTS=$OSM_ELK_PORT:$OSM_ELK_PORT)
@@ -1237,6 +1276,10 @@ function install_lightweight() {
     generate_docker_env_files
 
     if [ -n "$KUBERNETES" ]; then
+        if [ -n "$K8S_MONITOR" ]; then
+            # uninstall OSM MONITORING
+            uninstall_k8s_monitoring
+        fi
         #remove old namespace
         remove_k8s_namespace $OSM_STACK_NAME
         deploy_cni_provider
@@ -1257,6 +1300,12 @@ function install_lightweight() {
         [ -n "$INSTALL_ELK" ] && deploy_elk && track elk
     fi
 
+    if [ -n "$KUBERNETES" ] && [ -n "$K8S_MONITOR" ]; then
+        # install OSM MONITORING
+        install_k8s_monitoring
+        track install_k8s_monitoring
+    fi
+
     [ -z "$INSTALL_NOHOSTCLIENT" ] && install_osmclient
     track osmclient
 
@@ -1296,6 +1345,18 @@ function install_vimemu() {
     echo "     osm vim-create --name emu-vim1 --user username --password password --auth_url http://${VIMEMU_HOSTNAME}:6001/v2.0 --tenant tenantName --account_type openstack"
 }
 
+function install_k8s_monitoring() {
+    # install OSM monitoring
+    chmod +x $WORKDIR_SUDO $OSM_DEVOPS/installers/k8s/*.sh
+    $WORKDIR_SUDO $OSM_DEVOPS/installers/k8s/install_osm_k8s_monitoring.sh
+}
+
+function uninstall_k8s_monitoring() {
+    # install OSM monitoring
+    chmod +x $WORKDIR_SUDO $OSM_DEVOPS/installers/k8s/*.sh
+    $WORKDIR_SUDO $OSM_DEVOPS/installers/k8s/uninstall_osm_k8s_monitoring.sh
+}
+
 function dump_vars(){
     echo "DEVELOP=$DEVELOP"
     echo "INSTALL_FROM_SOURCE=$INSTALL_FROM_SOURCE"
@@ -1379,6 +1440,7 @@ INSTALL_NOLXD=""
 INSTALL_NODOCKER=""
 INSTALL_NOJUJU=""
 KUBERNETES=""
+K8S_MONITOR=""
 INSTALL_NOHOSTCLIENT=""
 NOCONFIGURE=""
 RELEASE_DAILY=""
@@ -1526,6 +1588,7 @@ while getopts ":hy-:b:r:c:k:u:R:l:p:D:o:m:H:S:s:w:t:U:P:A:" o; do
             [ "${OPTARG}" == "nodockerbuild" ] && DOCKER_NOBUILD="y" && continue
             [ "${OPTARG}" == "nohostclient" ] && INSTALL_NOHOSTCLIENT="y" && continue
             [ "${OPTARG}" == "pullimages" ] && continue
+            [ "${OPTARG}" == "k8s_monitor" ] && K8S_MONITOR="y" && continue
             echo -e "Invalid option: '--$OPTARG'\n" >&2
             usage && exit 1
             ;;