From 442a3ba3e8c489d406eae1758bedc9ed915afd07 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Diemer?= <stephane.diemer@ubicast.eu>
Date: Thu, 9 Feb 2017 17:42:36 +0100
Subject: [PATCH] Changed APT step for Ubuntu upgrade.

---
 2.Common_services/2.NTP/0_setup.py            |  2 +-
 2.Common_services/2.NTP/ntp.conf              |  4 +-
 .../1.Apply_client_configuration/0_setup.sh   | 11 +---
 .../3.Initialize_APT/0_setup.sh               | 48 --------------
 .../3.Proxy_settings}/0_setup.py              |  0
 .../4.APT_upgrade/0_setup.sh                  | 65 +++++++++++++++++++
 .../sources16.list                            |  0
 .../0_setup.sh                                |  0
 global-conf.sh                                |  6 +-
 launcher.sh                                   |  1 +
 tests/test_ntp.py                             |  4 +-
 11 files changed, 75 insertions(+), 66 deletions(-)
 delete mode 100755 3.New_server_deployment/3.Initialize_APT/0_setup.sh
 rename {1.Base/4.Proxy_settings => 3.New_server_deployment/3.Proxy_settings}/0_setup.py (100%)
 create mode 100755 3.New_server_deployment/4.APT_upgrade/0_setup.sh
 rename 3.New_server_deployment/{3.Initialize_APT => 4.APT_upgrade}/sources16.list (100%)
 rename 3.New_server_deployment/{4.Dell_openmanage => 5.Dell_openmanage}/0_setup.sh (100%)

diff --git a/2.Common_services/2.NTP/0_setup.py b/2.Common_services/2.NTP/0_setup.py
index b5c15bb3..c562acfc 100644
--- a/2.Common_services/2.NTP/0_setup.py
+++ b/2.Common_services/2.NTP/0_setup.py
@@ -9,7 +9,7 @@ def setup(interactive=True):
         # NTP
         'echo "Replacing /etc/ntp.conf"',
         dict(line='write', template='%s/ntp.conf' % dir_path, target='/etc/ntp.conf', params=(
-            ('{{ NTP_SERVER1 }}', utils.get_conf('NTP_SERVER1') or 'ntp.ubuntu.com'),
+            ('{{ NTP_SERVER }}', utils.get_conf('NTP_SERVER') or utils.get_conf('NTP_SERVER1') or 'ntp.ubuntu.com'),
         )),
         'service ntp restart',
     ]
diff --git a/2.Common_services/2.NTP/ntp.conf b/2.Common_services/2.NTP/ntp.conf
index 4a0077d8..b37943fb 100644
--- a/2.Common_services/2.NTP/ntp.conf
+++ b/2.Common_services/2.NTP/ntp.conf
@@ -21,8 +21,8 @@ server 1.ubuntu.pool.ntp.org
 server 2.ubuntu.pool.ntp.org
 server 3.ubuntu.pool.ntp.org
 
-# Use Ubuntu's ntp server as a fallback.
-server {{ NTP_SERVER1 }}
+# Fallback server.
+server {{ NTP_SERVER }}
 
 # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
 # details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
diff --git a/20.Client_configuration/1.Apply_client_configuration/0_setup.sh b/20.Client_configuration/1.Apply_client_configuration/0_setup.sh
index 6317dc12..7fa75fd1 100755
--- a/20.Client_configuration/1.Apply_client_configuration/0_setup.sh
+++ b/20.Client_configuration/1.Apply_client_configuration/0_setup.sh
@@ -25,16 +25,9 @@ echo "nameserver ${NETWORK_DNS2}" >> /etc/resolv.conf
 # set customer NTP
 sed -i "s@^server .*@@" /etc/ntp.conf
 echo "" >> /etc/ntp.conf
-echo "server ${NTP_SERVER1}" >> /etc/ntp.conf
-
-if ( ! test -z ${NTP_SERVER2} )
-then
-echo "server ${NTP_SERVER2}" >> /etc/ntp.conf
-fi
-
-if ( ! test -z ${NTP_SERVER3} )
+if ( ! test -z ${NTP_SERVER} )
 then
-echo "server ${NTP_SERVER3}" >> /etc/ntp.conf
+echo "server ${NTP_SERVER}" >> /etc/ntp.conf
 fi
 service ntp restart
 
diff --git a/3.New_server_deployment/3.Initialize_APT/0_setup.sh b/3.New_server_deployment/3.Initialize_APT/0_setup.sh
deleted file mode 100755
index f47a48d4..00000000
--- a/3.New_server_deployment/3.Initialize_APT/0_setup.sh
+++ /dev/null
@@ -1,48 +0,0 @@
-#!/bin/bash
-source /root/envsetup/global-conf.sh
-
-# modify sources.list to use ubicast cache
-if ( ! test -z ${APT_CACHE_HOST} ); then
-	if ( ! grep "${APT_CACHE_HOST}" /etc/apt/sources.list >/dev/null ); then
-		echo "Updating sources.list to use cache ${APT_CACHE_HOST}."
-		sed -i "s@http://@http://${APT_CACHE_HOST}/@" /etc/apt/sources.list
-	fi
-fi
-
-# migrate to Ubuntu 16.04 / apply sources.list
-if ( rgrep '14.04' /etc/lsb-release >/dev/null ); then
-	echo "Upgrading to Ubuntu 16.04."
-	apt-get update
-	apt-get dist-upgrade -y
-	cp sources16.list /etc/apt/sources.list
-	apt-get update
-	DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade -o Dpkg::Options::="--force-confold" --force-yes -y
-	apt-get install -f -y
-	apt-get dist-upgrade -y
-else
-	echo "Updating Ubuntu 16.04 sources.list file."
-	cp sources16.list /etc/apt/sources.list
-	apt-get update
-	apt-get dist-upgrade -y
-fi
-
-# APT https support
-apt-get install -y apt-transport-https
-
-# APT panel
-if ( ! test -z ${SKYREACH_API_KEY} ); then
-	echo "Adding skyreach.list to APT sources."
-	wget -q "https://${SKYREACH_HOST}/media/public.gpg" -O- | sudo apt-key add -
-	echo "deb https://${SKYREACH_HOST} packaging/apt/${SKYREACH_API_KEY}/" > /etc/apt/sources.list.d/skyreach.list
-fi
-
-# update
-apt-get update
-apt-get dist-upgrade -y
-
-# unattended-upgrades
-echo "Installing and configuring unattended-upgrades."
-apt-get install -y unattended-upgrades
-sed -i 's@//Unattended-Upgrade::Mail "root";@Unattended-Upgrade::Mail "root";@' /etc/apt/apt.conf.d/50unattended-upgrades
-sed -i 's@//*.*"vim";@"mysql-server";@' /etc/apt/apt.conf.d/50unattended-upgrades
-sed -i 's@//*.*"libc6";@"mysql-client";@' /etc/apt/apt.conf.d/50unattended-upgrades
diff --git a/1.Base/4.Proxy_settings/0_setup.py b/3.New_server_deployment/3.Proxy_settings/0_setup.py
similarity index 100%
rename from 1.Base/4.Proxy_settings/0_setup.py
rename to 3.New_server_deployment/3.Proxy_settings/0_setup.py
diff --git a/3.New_server_deployment/4.APT_upgrade/0_setup.sh b/3.New_server_deployment/4.APT_upgrade/0_setup.sh
new file mode 100755
index 00000000..793ab100
--- /dev/null
+++ b/3.New_server_deployment/4.APT_upgrade/0_setup.sh
@@ -0,0 +1,65 @@
+#!/bin/bash
+source /root/envsetup/global-conf.sh
+
+# move 50unattended-upgrades conf if any
+if ( ls /etc/apt/apt.conf.d/50unattended-upgrades.ucf-dist 2>&1 >/dev/null ); then
+	mv /etc/apt/apt.conf.d/50unattended-upgrades.ucf-dist /etc/apt/apt.conf.d/50unattended-upgrades
+fi
+
+# update sources.list
+rm -f /etc/apt/sources.list~
+echo "Updating sources.list file."
+cp sources16.list /etc/apt/sources.list
+if ( rgrep '14.04' /etc/lsb-release >/dev/null ); then
+	sed -i 's@xenial@trusty@' /etc/apt/sources.list
+fi
+
+# modify sources.list to use ubicast cache
+if ( ! test -z ${APT_CACHE_HOST} ); then
+	if ( ! grep "${APT_CACHE_HOST}" /etc/apt/sources.list >/dev/null ); then
+		echo "Updating sources.list to use cache ${APT_CACHE_HOST}."
+		sed -i "s@http://@http://${APT_CACHE_HOST}/@" /etc/apt/sources.list
+	fi
+fi
+
+# update packages
+DEBIAN_FRONTEND=noninteractive
+export DEBIAN_FRONTEND
+apt-get update
+apt-get install -f -y -o Dpkg::Options::="--force-confold"
+apt-get dist-upgrade -y -o Dpkg::Options::="--force-confold"
+apt-get install -y apt-transport-https
+apt-get autoremove -y
+
+# migrate to Ubuntu 16.04 if needed
+if ( rgrep '14.04' /etc/lsb-release >/dev/null ); then
+	echo "Upgrading to Ubuntu 16.04."
+	sed -i 's@trusty@xenial@' /etc/apt/sources.list
+	apt-get update
+	apt-get dist-upgrade -y -o Dpkg::Options::="--force-confold" || apt-get install -f -y -o Dpkg::Options::="--force-confold"
+	apt-get dist-upgrade -y -o Dpkg::Options::="--force-confold" || apt-get install -f -y -o Dpkg::Options::="--force-confold"
+	apt-get dist-upgrade -y -o Dpkg::Options::="--force-confold"
+	apt-get autoremove -y
+	echo "16.04 migration ok."
+	echo "The server is rebooting, please wait at least 2 minutes"
+	echo " before restarting the script to continue the configuration."
+	echo "Date: $(date)."
+	echo "Ignore the following message about reboot problem if any."
+	reboot
+	exit 1
+fi
+systemctl unmask nginx || true
+
+# APT panel
+if ( ! test -z ${SKYREACH_API_KEY} ); then
+	echo "Adding skyreach.list to APT sources."
+	wget -q "https://${SKYREACH_HOST}/media/public.gpg" -O- | sudo apt-key add -
+	echo "deb https://${SKYREACH_HOST} packaging/apt/${SKYREACH_API_KEY}/" > /etc/apt/sources.list.d/skyreach.list
+fi
+
+# update
+apt-get update
+apt-get dist-upgrade -y -o Dpkg::Options::="--force-confold"
+
+# unattended-upgrades
+apt-get install -y unattended-upgrades
diff --git a/3.New_server_deployment/3.Initialize_APT/sources16.list b/3.New_server_deployment/4.APT_upgrade/sources16.list
similarity index 100%
rename from 3.New_server_deployment/3.Initialize_APT/sources16.list
rename to 3.New_server_deployment/4.APT_upgrade/sources16.list
diff --git a/3.New_server_deployment/4.Dell_openmanage/0_setup.sh b/3.New_server_deployment/5.Dell_openmanage/0_setup.sh
similarity index 100%
rename from 3.New_server_deployment/4.Dell_openmanage/0_setup.sh
rename to 3.New_server_deployment/5.Dell_openmanage/0_setup.sh
diff --git a/global-conf.sh b/global-conf.sh
index da2822ab..36fe5195 100644
--- a/global-conf.sh
+++ b/global-conf.sh
@@ -17,14 +17,12 @@ NC='\033[0m'
 SKYREACH_HOST='panel.ubicast.eu'
 SKYREACH_API_KEY=
 # NTP
-NTP_SERVER1='ntp.ubuntu.com'
-NTP_SERVER2=
-NTP_SERVER3=
+NTP_SERVER='ntp.ubuntu.com'
 # SSL certificate
 SSL_CERTIFICATE='/etc/ssl/certs/ssl-cert-snakeoil.pem'
 SSL_CERTIFICATE_KEY='/etc/ssl/private/ssl-cert-snakeoil.key'
 # APT sources
-APT_CACHE_HOST='panel.ubicast.eu'
+APT_CACHE_HOST=
 
 # -- Shell --
 # ubicast shell account
diff --git a/launcher.sh b/launcher.sh
index dc38bc30..880ff184 100755
--- a/launcher.sh
+++ b/launcher.sh
@@ -20,6 +20,7 @@ init() {
     python3 /root/envsetup/envsetup.py 32
     python3 /root/envsetup/envsetup.py 33
     python3 /root/envsetup/envsetup.py 34
+    python3 /root/envsetup/envsetup.py 35
 
     python3 /root/envsetup/envsetup.py 11
     python3 /root/envsetup/envsetup.py 12
diff --git a/tests/test_ntp.py b/tests/test_ntp.py
index 5764e1bb..393a582c 100755
--- a/tests/test_ntp.py
+++ b/tests/test_ntp.py
@@ -2,7 +2,7 @@
 # -*- coding: utf-8 -*-
 # Copyright 2017, Florent Thiery
 '''
-Check that the server is synchronized with the configured NTP server 
+Check that the server is synchronized with the configured NTP server.
 '''
 import os
 import sys
@@ -24,7 +24,7 @@ print('Checking NTP server conforms to conf')
 if os.path.isfile('../utils.py'):
     es_utils = imp.load_source('es_utils', '../utils.py')
     conf = es_utils.load_conf()
-    NTP_SERVER = conf.get('NTP_SERVER1')
+    NTP_SERVER = conf.get('NTP_SERVER') or conf.get('NTP_SERVER1') or 'ntp.ubuntu.com'
 
     with open('/etc/ntp.conf', 'r') as f:
         d = f.read()
-- 
GitLab