cdrive: master_config: maas-part refactor

  * Misc: remove apt part - not relevant for releases 2018.5+
  * Increase salt pkg install timeout
  * With new day01|cfg01
    - maas more automated via formula
    - drop apt.conf changes - they integrated into image

Change-Id: Ib91bba6aa725191f169027773f4c48399980333d
diff --git a/config-drive/master_config.sh b/config-drive/master_config.sh
index c423cf6..3731b41 100644
--- a/config-drive/master_config.sh
+++ b/config-drive/master_config.sh
@@ -1,5 +1,9 @@
 #!/bin/bash -xe
 
+#==============================================================================
+# Required packages:
+#   apt-get install -y jq
+#==============================================================================
 export SALT_MASTER_DEPLOY_IP=${SALT_MASTER_DEPLOY_IP:-"172.16.164.15"}
 export SALT_MASTER_MINION_ID=${SALT_MASTER_MINION_ID:-"cfg01.deploy-name.local"}
 export DEPLOY_NETWORK_GW=${DEPLOY_NETWORK_GW:-"172.16.164.1"}
@@ -14,88 +18,86 @@
 export MCP_SALT_REPO_URL=${MCP_SALT_REPO_URL:-"http://apt.mirantis.com/xenial"}
 export MCP_SALT_REPO="deb [arch=amd64] $MCP_SALT_REPO_URL $MCP_VERSION salt"
 export FORMULAS="salt-formula-*"
-# Not avaible in 2018.4 and pre.
-export LOCAL_REPOS=false
-#for cloning from aptly image use port 8088
+# for cloning from aptly image use port 8088
 #export PIPELINE_REPO_URL=http://172.16.47.182:8088
+#
+SALT_OPTS="-l debug -t 10 --retcode-passthrough --no-color"
 
-function _apt_cfg(){
-  # TODO remove those function after 2018.4 release
-  echo "Acquire::CompressionTypes::Order gz;" >/etc/apt/apt.conf.d/99compression-workaround-salt
-  echo "Acquire::EnableSrvRecords false;" >/etc/apt/apt.conf.d/99enablesrvrecords-false
-  echo "Acquire::http::Pipeline-Depth 0;" > /etc/apt/apt.conf.d/99aws-s3-mirrors-workaround-salt
-  echo "APT::Install-Recommends false;" > /etc/apt/apt.conf.d/99dont_install_recommends-salt
-  echo "APT::Install-Suggests false;" > /etc/apt/apt.conf.d/99dont_install_suggests-salt
-  echo "Acquire::Languages none;" > /etc/apt/apt.conf.d/99dont_acquire_all_languages-salt
-  echo "APT::Periodic::Update-Package-Lists 0;" > /etc/apt/apt.conf.d/99dont_update_package_list-salt
-  echo "APT::Periodic::Download-Upgradeable-Packages 0;" > /etc/apt/apt.conf.d/99dont_update_download_upg_packages-salt
-  echo "APT::Periodic::Unattended-Upgrade 0;" > /etc/apt/apt.conf.d/99disable_unattended_upgrade-salt
-  echo "INFO: cleaning sources lists"
-  find /etc/apt/sources.list.d/ -type f -delete -print
-  echo > /etc/apt/sources.list  || true
-}
-
+# Funcs =======================================================================
 function _post_maas_cfg(){
-  local PROFILE=mirantis
-  # TODO: remove those check, and use only new version, adfter 2018.4 release
-  if [[ -f /var/lib/maas/.maas_login.sh ]]; then
-    /var/lib/maas/.maas_login.sh
-  else
-    echo "WARNING: Attempt to use old maas login schema.."
-    TOKEN=$(cat /var/lib/maas/.maas_credentials);
-    maas list | cut -d' ' -f1 | xargs -I{} maas logout {}
-    maas login $PROFILE http://127.0.0.1:5240/MAAS/api/2.0/ "${TOKEN}"
-  fi
-  # disable backports for maas enlist pkg repo
-  maas ${PROFILE} package-repository update 1 "disabled_pockets=backports"
-  maas ${PROFILE} package-repository update 1 "arches=amd64"
-  # Download ubuntu image from MAAS local mirror
-  if [[ "$LOCAL_REPOS" == "true" ]] ; then
-    maas ${PROFILE} boot-source-selections create 2 os="ubuntu" release="xenial" arches="amd64" subarches="*" labels="*"
-    echo "WARNING: Removing default MAAS stream:"
-    maas ${PROFILE} boot-source read 1
-    maas ${PROFILE} boot-source delete 1
-    maas ${PROFILE} boot-resources import
-    # TODO wait for finish,and stop import.
-  else
-    maas ${PROFILE} boot-source-selections create 1 os="ubuntu" release="xenial" arches="amd64" subarches="*" labels="*"
-    maas ${PROFILE} boot-resources import
-  fi
-  while [ ! -d /var/lib/maas/boot-resources/current/ubuntu/amd64/generic/xenial ]
-  do
-    sleep 10
-    echo "WARNING: Image is still not ready"
+  chmod 0755 /var/lib/maas/.maas_login.sh
+  source /var/lib/maas/.maas_login.sh
+  # disable backports for maas enlist pkg repo. Those operation enforce maas
+  # to re-create sources.list and drop [source] fetch-definition from it.
+  main_arch_id=$(maas ${PROFILE} package-repositories read | jq -r '.[] | select(.name=="main_archive") | .id')
+  maas ${PROFILE} package-repository update ${main_arch_id} "disabled_pockets=backports" || true
+  maas ${PROFILE} package-repository update ${main_arch_id} "disabled_components=multiverse" || true
+  maas ${PROFILE} package-repository update ${main_arch_id} "arches=amd64" || true
+  # Remove stale notifications, which appear during sources configuration.
+  for i in $(maas ${PROFILE} notifications read | jq '.[]| .id'); do
+    maas ${PROFILE} notification delete ${i} || true
   done
 }
 
-### Body
-echo "Configuring network interfaces"
-find /etc/network/interfaces.d/ -type f -delete
-kill $(pidof /sbin/dhclient) || /bin/true
-envsubst < /root/interfaces > /etc/network/interfaces
-ip a flush dev ens3
-rm -f /var/run/network/ifstate.ens3
-if [[ $(grep -E '^\ *gateway\ ' /etc/network/interfaces) ]]; then
-(ip r s | grep ^default) && ip r d default || /bin/true
-fi;
-ifup ens3
+function process_formulas(){
+    local RECLASS_ROOT=${RECLASS_ROOT:-/srv/salt/reclass/}
+    local FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas}
+
+    echo "Configuring formulas ..."
+    curl -s $MCP_SALT_REPO_KEY | apt-key add -
+    echo $MCP_SALT_REPO > /etc/apt/sources.list.d/mcp_salt.list
+    apt-get update
+    apt-get install -y $FORMULAS
+
+    [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
+    for formula_service in $(ls /usr/share/salt-formulas/reclass/service/); do
+        #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
+        formula_service=${formula_service//-/$'_'}
+        [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
+            ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
+    done
+}
+
+function enable_services(){
+  local services="postgresql.service salt-api salt-master salt-minion jenkins"
+  for s in ${services} ; do
+    systemctl enable ${s} || true
+    systemctl restart ${s} || true
+  done
+}
+
+function process_network(){
+  echo "Configuring network interfaces"
+  find /etc/network/interfaces.d/ -type f -delete
+  kill $(pidof /sbin/dhclient) || /bin/true
+  envsubst < /root/interfaces > /etc/network/interfaces
+  ip a flush dev ens3
+  rm -f /var/run/network/ifstate.ens3
+  if [[ $(grep -E '^\ *gateway\ ' /etc/network/interfaces) ]]; then
+  (ip r s | grep ^default) && ip r d default || /bin/true
+  fi;
+  ifup ens3
+}
+
+# Body ========================================================================
+process_network
 
 echo "Preparing metadata model"
 mount /dev/cdrom /mnt/
 cp -rT /mnt/model/model /srv/salt/reclass
-chown -R root:root /srv/salt/reclass/*
+chown -R root:root /srv/salt/reclass/* || true
 chown -R root:root /srv/salt/reclass/.git* || true
 chmod -R 644 /srv/salt/reclass/classes/cluster/* || true
 chmod -R 644 /srv/salt/reclass/classes/system/*  || true
 
 echo "Configuring salt"
-#service salt-master restart
 envsubst < /root/minion.conf > /etc/salt/minion.d/minion.conf
-service salt-minion restart
+enable_services
 while true; do
     salt-key | grep "$SALT_MASTER_MINION_ID" && break
     sleep 5
 done
+
 sleep 5
 for i in $(salt-key -l accepted | grep -v Accepted | grep -v "$SALT_MASTER_MINION_ID"); do
     salt-key -d $i -y
@@ -103,7 +105,7 @@
 
 find /var/lib/jenkins/jenkins.model.JenkinsLocationConfiguration.xml -type f -print0 | xargs -0 sed -i -e 's/10.167.4.15/'$SALT_MASTER_DEPLOY_IP'/g'
 
-echo "updating git repos"
+echo "updating local git repos"
 if [[ "$PIPELINES_FROM_ISO" == "true" ]] ; then
   cp -r /mnt/mk-pipelines/* /home/repo/mk/mk-pipelines/
   cp -r /mnt/pipeline-library/* /home/repo/mcp-ci/pipeline-library/
@@ -118,14 +120,7 @@
   chown -R git:www-data /home/repo/mcp-ci/pipeline-library/*
 fi
 
-echo "installing formulas"
-_apt_cfg
-curl -s $MCP_SALT_REPO_KEY | sudo apt-key add -
-echo $MCP_SALT_REPO > /etc/apt/sources.list.d/mcp_salt.list
-apt-get update
-apt-get install -y $FORMULAS
-rm -rf /srv/salt/reclass/classes/service/*
-cd /srv/salt/reclass/classes/service/;ls /usr/share/salt-formulas/reclass/service/ -1 | xargs -I{} ln -s /usr/share/salt-formulas/reclass/service/{};cd /root
+process_formulas
 
 salt-call saltutil.refresh_pillar
 salt-call saltutil.sync_all
@@ -134,23 +129,27 @@
   exit 1
 fi
 
-salt-call state.sls linux.network,linux,openssh,salt
-salt-call -t5 pkg.install salt-master,salt-minion
+salt-call ${SALT_OPTS} state.sls linux.network,linux,openssh,salt
+salt-call ${SALT_OPTS} pkg.install salt-master,salt-minion
 sleep 5
-salt-call state.sls salt
-# Sometimes, maas can stuck :(
-salt-call state.sls maas.cluster,maas.region || salt-call state.sls maas.cluster,maas.region
-salt-call state.sls reclass,ntp
+salt-call ${SALT_OPTS} state.sls salt
+salt-call ${SALT_OPTS} state.sls maas.cluster,maas.region
+salt-call ${SALT_OPTS} state.sls reclass
 
 _post_maas_cfg
-salt-call state.sls maas.cluster,maas.region || salt-call state.sls maas.cluster,maas.region
 
 ssh-keyscan cfg01 > /var/lib/jenkins/.ssh/known_hosts || true
 
 pillar=$(salt-call pillar.data jenkins:client)
 
 if [[ $pillar == *"job"* ]]; then
-  salt-call state.sls jenkins.client
+  salt-call ${SALT_OPTS} state.sls jenkins.client
 fi
 
+stop_services="salt-api salt-master salt-minion jenkins maas-rackd.service maas-regiond.service postgresql.service"
+for s in ${stop_services} ; do
+  systemctl stop ${s} || true
+  sleep 1
+done
+sync
 reboot