]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/commitdiff
Enhance install script, and add ip6 address
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 17 Sep 2017 17:05:41 +0000 (19:05 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 17 Sep 2017 22:22:19 +0000 (00:22 +0200)
bin/install_script.sh
environments/production/hiera.yaml
modules/base_installation/files/services/en-dhcp.network [deleted file]
modules/base_installation/lib/facter/ldapvar.rb
modules/base_installation/manifests/services.pp
modules/base_installation/templates/services/en-dhcp.network.erb [new file with mode: 0644]
python/get_initial_configuration.py [new file with mode: 0644]
python/reboot_vps_server.py

index f46ab29618e1421ca8d20f1da11e59caf1ed7fb2..3a5900689b6f0b74947656103e3de593d14115d0 100755 (executable)
@@ -1,20 +1,69 @@
 #!/bin/bash
 
+set -e
+
+host_user=root
+git_branch=master
+
+while [ -n "$1" ]; do
+  case "$1" in
+    --vps)
+      vps_name="$2"
+      shift
+      ;;
+    --reinstall-first)
+      reinstall_first=1
+      ;;
+    --password)
+      password="$2"
+      shift
+      ;;
+    --host-user)
+      host_user="$2"
+      shift
+      ;;
+    --no-reboot)
+      no_reboot=1
+      ;;
+    --no-reboot-start)
+      no_reboot_start=1
+      ;;
+    --no-reboot-end)
+      no_reboot_end=1
+      ;;
+    --git-branch)
+      git_branch="$2"
+      shift
+      ;;
+  esac
+
+  shift
+done
+
 DIRECTORY=$(cd `dirname $0` && pwd)
 PYTHON_DIRECTORY="$DIRECTORY/../python"
 
-if [ -n "$1" ]; then
-  vps_name="$1"
-else
+if [ -z "$vps_name" ]; then
   read -p "Nom du vps : " vps_name
 fi
 
-echo "Patienter le temps du reboot"
-python $PYTHON_DIRECTORY/reboot_vps_server.py --rescue "$vps_name"
+if [ -n "$reinstall_first" ]; then
+  echo "Réinstallation du système"
+  python $PYTHON_DIRECTORY/reinstall_vps_server.py --use-current "$vps_name"
+
+  read -p "Appuyer sur une touche quand le serveur est prêt" ready
+fi
+
+if [ -z "$no_reboot" -a -z "$no_reboot_start" ]; then
+  echo "Patienter le temps du reboot"
+  python $PYTHON_DIRECTORY/reboot_vps_server.py --rescue "$vps_name"
+fi
 
-stty -echo
-read -p "Mot de passe reçu par e-mail : " password; echo
-stty echo
+if [ -z "$password" ]; then
+  stty -echo
+  read -p "Mot de passe reçu par e-mail : " password; echo
+  stty echo
+fi
 
 ARCH_DIR=`mktemp -d`
 ARCH_HOST_SCRIPT="$ARCH_DIR/arch_host_script.sh"
@@ -22,10 +71,13 @@ ARCH_CHROOT_SCRIPT="$ARCH_DIR/arch_chroot_script.sh"
 ARCH_INSTALL_SCRIPT="$ARCH_DIR/arch_install_script.sh"
 ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT="$ARCH_DIR/arch_host_puppet_configuration_script.sh"
 ARCH_PUPPET_CONFIGURATION_SCRIPT="$ARCH_DIR/arch_puppet_configuration_script.sh"
+ARCH_PUPPET_INITIAL_CONFIGURATION="$ARCH_DIR/puppet_variables.json"
 
 trap "rm -rf $ARCH_DIR" EXIT
 
 #### Base installation stage
+python $PYTHON_DIRECTORY/get_initial_configuration.py $vps_name > $ARCH_PUPPET_INITIAL_CONFIGURATION
+
 cat > $ARCH_HOST_SCRIPT <<EOF
 #!/bin/bash
 
@@ -61,6 +113,7 @@ cp /tmp/arch_chroot_script.sh /tmp/root.x86_64/
 mount "\$DEVICE"
 
 cp /tmp/arch_install_script.sh "\$MOUNTPOINT/root/"
+cp /tmp/puppet_variables.json "\$MOUNTPOINT/root/"
 
 /tmp/root.x86_64/bin/arch-chroot "\$MOUNTPOINT" /root/arch_install_script.sh
 EOF
@@ -100,7 +153,7 @@ EOF
 cat > $ARCH_INSTALL_SCRIPT <<EOF
 CODE_PATH="/etc/puppetlabs/code"
 rm -rf \$CODE_PATH
-git clone -b master --recursive https://git.immae.eu/perso/Immae/Projets/Puppet.git \$CODE_PATH
+git clone -b $git_branch --recursive https://git.immae.eu/perso/Immae/Projets/Puppet.git \$CODE_PATH
 puppet apply --test \$CODE_PATH/manifests/site.pp
 # The password seed requires puppet to be run twice
 puppet apply --test \$CODE_PATH/manifests/site.pp
@@ -110,11 +163,11 @@ chmod a+x $ARCH_HOST_SCRIPT $ARCH_CHROOT_SCRIPT $ARCH_INSTALL_SCRIPT
 
 expect -f - <<EOF
 set timeout -1
-spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $ARCH_HOST_SCRIPT $ARCH_CHROOT_SCRIPT $ARCH_INSTALL_SCRIPT root@$vps_name:/tmp
+spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $ARCH_PUPPET_INITIAL_CONFIGURATION $ARCH_HOST_SCRIPT $ARCH_CHROOT_SCRIPT $ARCH_INSTALL_SCRIPT $host_user@$vps_name:/tmp
 expect "assword:"
 send "$password\n"
 expect eof
-spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no root@$vps_name /tmp/arch_host_script.sh
+spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $host_user@$vps_name /tmp/arch_host_script.sh
 expect "assword:"
 send "$password\r"
 expect eof
@@ -142,19 +195,18 @@ chmod a+x $ARCH_PUPPET_CONFIGURATION_SCRIPT $ARCH_HOST_PUPPET_CONFIGURATION_SCRI
 
 expect -f - <<EOF
 set timeout -1
-spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $ARCH_PUPPET_CONFIGURATION_SCRIPT $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT root@$vps_name:/tmp
+spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $ARCH_PUPPET_CONFIGURATION_SCRIPT $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT $host_user@$vps_name:/tmp
 expect "assword:"
 send "$password\n"
 expect eof
-spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no root@$vps_name /tmp/arch_host_puppet_configuration_script.sh
+spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $host_user@$vps_name /tmp/arch_host_puppet_configuration_script.sh
 expect "assword:"
 send "$password\r"
 expect eof
 EOF
 
 ### Installation finished
-read -p "Reboot to normal? [Y/n]" reboot
-if [ "x$reboot" != "xn" ]; then
+if [ -z "$no_reboot" -a -z "$no_reboot_end" ]; then
   echo "Rebooting"
   python $PYTHON_DIRECTORY/reboot_vps_server.py --local "$vps_name"
 fi
index 44c7ecdb060fa55532464ec1cd98ca3edbcd16fe..9cedf4782183e0fda9246de2d00e2f292cfb536b 100644 (file)
@@ -15,6 +15,9 @@ hierarchy:
     path: "nodes/%{facts.ec2_metadata.hostname}.yaml"
 ### /FIXME
 
+  - name: "Initialization variables"
+    path: "/root/puppet_variables.json"
+
   - name: "Per-role data"
     mapped_paths: [ldapvar.self.vars.roles, role, "roles/%{role}.yaml"]
 
diff --git a/modules/base_installation/files/services/en-dhcp.network b/modules/base_installation/files/services/en-dhcp.network
deleted file mode 100644 (file)
index 6eef0e9..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-[Match]
-Name=en*
-
-[Network]
-DHCP=yes
-
-[DHCP]
-UseMTU=true
index ff8e898271c53352b33c763b67bc52831e5ee26a..3ee6623080709c013f1281ab3f8c39a52eac21da 100644 (file)
@@ -1,46 +1,49 @@
-require 'ldap'
-require 'puppet/util/ldap/connection'
-
-Facter.add("ldapvar") do
-  setcode do
-    if Puppet[:node_terminus].to_sym != :ldap
-      data = []
-    else
-      begin
-        conn = Puppet::Util::Ldap::Connection.instance
-        conn.start
-        connection = conn.connection
-      rescue ::LDAP::ResultError => e
-        raise Puppet::ParseError, ("ldapquery(): LDAP ResultError - #{e.message}")
-      end
-
-      host = Facter.value('ec2_metadata')["hostname"]
-      base = Puppet[:ldapbase]
-      scope  = ::LDAP::LDAP_SCOPE_SUBTREE
-      filter = "(objectclass=*)"
-
-      data = {
-        :self  => {},
-        :other => [],
-      }
-
-      connection.search(base, scope, filter) do |entry|
-        data_ = entry.to_hash
-        data_['vars'] = (data_[Puppet[:ldapstackedattrs]] || [])
-          .map { |var| var.split("=", 2) }
-          .group_by { |(key, value)| key }
-          .map { |key, value| [key, value.map(&:last)] }
-          .to_h
-
-        data[:other] << data_
+begin
+  require 'ldap'
+  require 'puppet/util/ldap/connection'
+
+  Facter.add("ldapvar") do
+    setcode do
+      if Puppet[:node_terminus].to_sym != :ldap
+        data = []
+      else
+        begin
+          conn = Puppet::Util::Ldap::Connection.instance
+          conn.start
+          connection = conn.connection
+        rescue ::LDAP::ResultError => e
+          raise Puppet::ParseError, ("ldapquery(): LDAP ResultError - #{e.message}")
+        end
 
-        if data_["cn"].any? { |cn| cn == host }
-          data[:self] = data_
+        host = Facter.value('ec2_metadata')["hostname"]
+        base = Puppet[:ldapbase]
+        scope  = ::LDAP::LDAP_SCOPE_SUBTREE
+        filter = "(objectclass=*)"
+
+        data = {
+          :self  => {},
+          :other => [],
+        }
+
+        connection.search(base, scope, filter) do |entry|
+          data_ = entry.to_hash
+          data_['vars'] = (data_[Puppet[:ldapstackedattrs]] || [])
+            .map { |var| var.split("=", 2) }
+            .group_by { |(key, value)| key }
+            .map { |key, value| [key, value.map(&:last)] }
+            .to_h
+
+          data[:other] << data_
+
+          if data_["cn"].any? { |cn| cn == host }
+            data[:self] = data_
+          end
         end
-      end
 
-      data
+        data
+      end
     end
   end
+rescue LoadError
+  # No facts
 end
-
index b48c3b530e4b72b3f9e9b1fd33a6772500a06301..c641f4b26105496e6deadf4f57f6c13cd71893f3 100644 (file)
@@ -38,13 +38,14 @@ class base_installation::services inherits base_installation {
      group   => "root"
   }
 
+  $ip6 = lookup("ips.v6") |$key| { {} }
   file { '/etc/systemd/network/en-dhcp.network':
-     ensure => "present",
-     path   => "/etc/systemd/network/en-dhcp.network",
-     source => 'puppet:///modules/base_installation/services/en-dhcp.network',
-     mode   => "0644",
-     owner  => "root",
-     group  => "root"
+    ensure  => "present",
+    path    => "/etc/systemd/network/en-dhcp.network",
+    content => template('base_installation/services/en-dhcp.network.erb'),
+    mode    => "0644",
+    owner   => "root",
+    group   => "root"
   }
 
 }
diff --git a/modules/base_installation/templates/services/en-dhcp.network.erb b/modules/base_installation/templates/services/en-dhcp.network.erb
new file mode 100644 (file)
index 0000000..4f55582
--- /dev/null
@@ -0,0 +1,13 @@
+[Match]
+Name=en*
+
+[Network]
+DHCP=ipv4
+
+<%- unless @ip6.empty? -%>
+Address=<%= @ip6["ipAddress"] %>/<%= @ip6["mask"] %>
+Gateway=<%= @ip6["gateway"] %>
+<%- end -%>
+
+[DHCP]
+UseMTU=true
diff --git a/python/get_initial_configuration.py b/python/get_initial_configuration.py
new file mode 100644 (file)
index 0000000..0c6f698
--- /dev/null
@@ -0,0 +1,37 @@
+# -*- encoding: utf-8 -*-
+import json
+try:
+    from ovh import ovh
+except ImportError:
+    # In case it's installed globally
+    import ovh
+import sys
+
+infos = {}
+
+# Credentials are stored in ~/.ovh.conf
+# See ovh/README.rst
+client = ovh.Client()
+
+vps_list = client.get('/vps/')
+if sys.argv[-1] in vps_list:
+    vps = sys.argv[-1]
+else:
+    print("VPS not in list:")
+    for vps in vps_list:
+        print(vps)
+    sys.exit(1)
+
+ips = client.get('/vps/{}/ips'.format(vps))
+
+infos["ips"] = {}
+for ip in ips:
+    ip_infos = client.get('/vps/{}/ips/{}'.format(vps, ip))
+
+    if ip_infos["version"] == "v4":
+        infos["ips"]["v4"] = ip_infos
+    else:
+        infos["ips"]["v6"] = ip_infos
+        infos["ips"]["v6"]["mask"] = 128
+
+print(json.dumps(infos))
index 7ea301a3445b5d9e6a2573ca510ebb84944f56d9..71c52273b83e5c4836daefc0620ae1e413717892 100644 (file)
@@ -1,6 +1,10 @@
 # -*- encoding: utf-8 -*-
 import json
-from ovh import ovh
+try:
+    from ovh import ovh
+except ImportError:
+    # In case it's installed globally
+    import ovh
 import sys
 import ovh_helper