From 248bd83ed797f6e0f385e72a56aa7bafdf23d490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Sun, 17 Sep 2017 19:05:41 +0200 Subject: Enhance install script, and add ip6 address --- bin/install_script.sh | 82 +++++++++++++++++---- environments/production/hiera.yaml | 3 + .../files/services/en-dhcp.network | 8 --- modules/base_installation/lib/facter/ldapvar.rb | 83 +++++++++++----------- modules/base_installation/manifests/services.pp | 13 ++-- .../templates/services/en-dhcp.network.erb | 13 ++++ python/get_initial_configuration.py | 37 ++++++++++ python/reboot_vps_server.py | 6 +- 8 files changed, 175 insertions(+), 70 deletions(-) delete mode 100644 modules/base_installation/files/services/en-dhcp.network create mode 100644 modules/base_installation/templates/services/en-dhcp.network.erb create mode 100644 python/get_initial_configuration.py diff --git a/bin/install_script.sh b/bin/install_script.sh index f46ab29..3a59006 100755 --- a/bin/install_script.sh +++ b/bin/install_script.sh @@ -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 < $ARCH_INSTALL_SCRIPT < 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 - diff --git a/modules/base_installation/manifests/services.pp b/modules/base_installation/manifests/services.pp index b48c3b5..c641f4b 100644 --- a/modules/base_installation/manifests/services.pp +++ b/modules/base_installation/manifests/services.pp @@ -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 index 0000000..4f55582 --- /dev/null +++ b/modules/base_installation/templates/services/en-dhcp.network.erb @@ -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 index 0000000..0c6f698 --- /dev/null +++ b/python/get_initial_configuration.py @@ -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)) diff --git a/python/reboot_vps_server.py b/python/reboot_vps_server.py index 7ea301a..71c5227 100644 --- a/python/reboot_vps_server.py +++ b/python/reboot_vps_server.py @@ -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 -- cgit v1.2.3