aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/install_script_ovh_cloud_instance.sh182
-rwxr-xr-xbin/install_script_ovh_vps_ssd.sh (renamed from bin/install_script.sh)0
-rw-r--r--python/get_initial_configuration_cloud_instance.py41
-rw-r--r--python/reboot_cloud_instance.py39
-rw-r--r--python/reinstall_cloud_instance.py57
5 files changed, 319 insertions, 0 deletions
diff --git a/bin/install_script_ovh_cloud_instance.sh b/bin/install_script_ovh_cloud_instance.sh
new file mode 100755
index 0000000..26e410e
--- /dev/null
+++ b/bin/install_script_ovh_cloud_instance.sh
@@ -0,0 +1,182 @@
1#!/bin/bash
2
3usage() {
4cat <<EOF
5 $0 [options]
6 --help,-h This help
7 --instance-id id Id of the instance
8 --reinstall-first Start with reinstalling the vps
9 --host-user user Use another user (default: arch)
10 --no-reboot Don't reboot
11 --no-reboot-start Don't reboot to rescue at the beginning
12 --no-reboot-end Don't reboot to normal at the end
13 --git-branch Use another puppet branch (default: master)
14 --environment Environment to use for the installl (default: production)
15EOF
16}
17
18set -e
19
20host_user=arch
21git_branch=master
22environment=production
23
24while [ -n "$1" ]; do
25 case "$1" in
26 --instance-id)
27 instance_id="$2"
28 shift
29 ;;
30 --reinstall-first)
31 reinstall_first=1
32 ;;
33 --host-user)
34 host_user="$2"
35 shift
36 ;;
37 --no-reboot)
38 no_reboot=1
39 ;;
40 --no-reboot-start)
41 no_reboot_start=1
42 ;;
43 --no-reboot-end)
44 no_reboot_end=1
45 ;;
46 --git-branch)
47 git_branch="$2"
48 shift
49 ;;
50 --environment)
51 environment="$2"
52 shift
53 ;;
54 --help|-h)
55 usage
56 exit 0
57 ;;
58 esac
59
60 shift
61done
62
63DIRECTORY=$(cd `dirname $0` && pwd)
64PYTHON_DIRECTORY="$DIRECTORY/../python"
65
66if [ -z "$instance_id" ]; then
67 read -p "Id de l'instance : " instance_id
68fi
69
70if [ -n "$reinstall_first" ]; then
71 echo "Réinstallation du système"
72 python $PYTHON_DIRECTORY/reinstall_cloud_instance.py --use-current "$instance_id"
73
74 read -p "Appuyer sur une touche quand le serveur est prêt" ready
75fi
76
77if [ -z "$no_reboot" -a -z "$no_reboot_start" ]; then
78 echo "Patienter le temps du reboot"
79 python $PYTHON_DIRECTORY/reboot_cloud_instance.py --rescue "$instance_id"
80
81 read -p "Appuyer sur une touche quand l'instance a redémarré" ready
82fi
83
84ARCH_DIR=`mktemp -d`
85ARCH_HOST_SCRIPT="$ARCH_DIR/arch_host_script.sh"
86ARCH_INSTALL_SCRIPT="$ARCH_DIR/arch_install_script.sh"
87ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT="$ARCH_DIR/arch_host_puppet_configuration_script.sh"
88ARCH_PUPPET_CONFIGURATION_SCRIPT="$ARCH_DIR/arch_puppet_configuration_script.sh"
89ARCH_PUPPET_INITIAL_CONFIGURATION="$ARCH_DIR/puppet_variables.json"
90
91trap "rm -rf $ARCH_DIR" EXIT
92
93#### Base installation stage
94python $PYTHON_DIRECTORY/get_initial_configuration_cloud_instance.py $instance_id > $ARCH_PUPPET_INITIAL_CONFIGURATION
95host_address=$(python $PYTHON_DIRECTORY/get_initial_configuration_cloud_instance.py $instance_id | jq -r '.ips.v4.ipAddress')
96
97cat > $ARCH_HOST_SCRIPT <<EOF
98#!/bin/bash
99
100sudo haveged &
101sudo pacman -Sy --noconfirm arch-install-scripts
102
103DEVICE=/dev/vdb1
104MOUNTPOINT=/mnt
105
106UUID=\$(lsblk -rno UUID "\$DEVICE")
107PART="/dev/disk/by-uuid/\$UUID"
108
109# mkfs.ext4 -F -U "\$UUID" "\$DEVICE"
110sudo mount "\$DEVICE" /mnt
111
112##### FIXME: mkfs.ext4 would be better ####
113for i in /mnt/*; do
114 if [ "\$i" = "/mnt/boot" ]; then
115 # keep /boot/grub
116 sudo rm -f \$i/*
117 else
118 sudo rm -rf \$i
119 fi
120done
121##### /FIXME ####
122
123sudo pacstrap /mnt base git puppet
124
125echo "\$PART / auto defaults 0 1" | sudo tee /mnt/etc/fstab
126
127sudo cp /tmp/arch_install_script.sh "\$MOUNTPOINT/root/"
128sudo cp /tmp/puppet_variables.json "\$MOUNTPOINT/root/"
129
130sudo arch-chroot "\$MOUNTPOINT" /root/arch_install_script.sh
131EOF
132
133cat > $ARCH_INSTALL_SCRIPT <<EOF
134CODE_PATH="/etc/puppetlabs/code"
135rm -rf \$CODE_PATH
136git clone -b $git_branch --recursive https://git.immae.eu/perso/Immae/Projets/Puppet.git \$CODE_PATH
137puppet apply --environment $environment --tags base_installation --test \$CODE_PATH/manifests/site.pp
138# The password seed requires puppet to be run twice
139puppet apply --environment $environment --tags base_installation --test \$CODE_PATH/manifests/site.pp
140EOF
141
142chmod a+x $ARCH_HOST_SCRIPT $ARCH_INSTALL_SCRIPT
143
144expect -f - <<EOF
145set timeout -1
146spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $ARCH_PUPPET_INITIAL_CONFIGURATION $ARCH_HOST_SCRIPT $ARCH_INSTALL_SCRIPT $host_user@$host_address:/tmp
147expect eof
148spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $host_user@$host_address /tmp/arch_host_script.sh
149expect eof
150EOF
151
152### Role specific stage
153read -p "Press key when LDAP is configured" i
154
155cat > $ARCH_PUPPET_CONFIGURATION_SCRIPT <<EOF
156CODE_PATH="/etc/puppetlabs/code"
157puppet apply --environment $environment --tags base_installation --test \$CODE_PATH/manifests/site.pp
158EOF
159
160cat > $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT <<EOF
161MOUNTPOINT=/mnt
162
163sudo cp /tmp/arch_puppet_configuration_script.sh "\$MOUNTPOINT/root/"
164
165sudo arch-chroot "\$MOUNTPOINT" /root/arch_puppet_configuration_script.sh
166EOF
167
168chmod a+x $ARCH_PUPPET_CONFIGURATION_SCRIPT $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT
169
170expect -f - <<EOF
171set timeout -1
172spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $ARCH_PUPPET_CONFIGURATION_SCRIPT $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT $host_user@$host_address:/tmp
173expect eof
174spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $host_user@$host_address /tmp/arch_host_puppet_configuration_script.sh
175expect eof
176EOF
177
178### Installation finished
179if [ -z "$no_reboot" -a -z "$no_reboot_end" ]; then
180 echo "Rebooting"
181 python $PYTHON_DIRECTORY/reboot_cloud_instance.py --local "$instance_id"
182fi
diff --git a/bin/install_script.sh b/bin/install_script_ovh_vps_ssd.sh
index 6b1aa39..6b1aa39 100755
--- a/bin/install_script.sh
+++ b/bin/install_script_ovh_vps_ssd.sh
diff --git a/python/get_initial_configuration_cloud_instance.py b/python/get_initial_configuration_cloud_instance.py
new file mode 100644
index 0000000..4157716
--- /dev/null
+++ b/python/get_initial_configuration_cloud_instance.py
@@ -0,0 +1,41 @@
1# -*- encoding: utf-8 -*-
2import json
3try:
4 from ovh import ovh
5except ImportError:
6 # In case it's installed globally
7 import ovh
8import sys
9
10infos = {}
11
12# Credentials are stored in ~/.ovh.conf
13# See ovh/README.rst
14client = ovh.Client()
15
16projects_list = client.get('/cloud/project/')
17if len(projects_list) > 1:
18 print("More than one project is not supported, taking the first one")
19project = projects_list[0]
20instances_list = client.get('/cloud/project/{}/instance'.format(project))
21instances = dict(map(lambda x: (x["id"], x), instances_list))
22if sys.argv[-1] in instances:
23 instance = instances[sys.argv[-1]]
24else:
25 print("Instance not in list:")
26 for instance in instances_list:
27 print("{}: {}".format(instance["name"], instance["id"]))
28 sys.exit(1)
29
30infos["ips"] = {}
31for ip_infos in instance["ipAddresses"]:
32 ip_infos["ipAddress"] = ip_infos.pop("ip")
33 ip_infos["gateway"] = ip_infos.pop("gatewayIp")
34
35 if ip_infos["version"] == 4:
36 infos["ips"]["v4"] = ip_infos
37 else:
38 infos["ips"]["v6"] = ip_infos
39 infos["ips"]["v6"]["mask"] = 128
40
41print(json.dumps(infos))
diff --git a/python/reboot_cloud_instance.py b/python/reboot_cloud_instance.py
new file mode 100644
index 0000000..b90f488
--- /dev/null
+++ b/python/reboot_cloud_instance.py
@@ -0,0 +1,39 @@
1# -*- encoding: utf-8 -*-
2import json
3try:
4 from ovh import ovh
5except ImportError:
6 # In case it's installed globally
7 import ovh
8import sys
9
10# Credentials are stored in ~/.ovh.conf
11# See ovh/README.rst
12client = ovh.Client()
13
14projects_list = client.get('/cloud/project/')
15if len(projects_list) > 1:
16 print("More than one project is not supported, taking the first one")
17project = projects_list[0]
18instances_list = client.get('/cloud/project/{}/instance'.format(project))
19instances = dict(map(lambda x: (x["id"], x), instances_list))
20if sys.argv[-1] in instances:
21 instance = instances[sys.argv[-1]]
22else:
23 print("Instance not in list:")
24 for instance in instances_list:
25 print("{}: {}".format(instance["name"], instance["id"]))
26 sys.exit(1)
27
28if "--rescue" in sys.argv:
29 netboot_mode="rescue"
30elif "--local" in sys.argv:
31 netboot_mode="local"
32else:
33 netboot_mode=None
34
35if netboot_mode is not None:
36 result = client.post("/cloud/project/{}/instance/{}/rescueMode".format(project,
37 instance["id"]), imageId=instance["imageId"], rescue=(netboot_mode == "rescue"))
38 print(result)
39
diff --git a/python/reinstall_cloud_instance.py b/python/reinstall_cloud_instance.py
new file mode 100644
index 0000000..c488fda
--- /dev/null
+++ b/python/reinstall_cloud_instance.py
@@ -0,0 +1,57 @@
1# -*- encoding: utf-8 -*-
2import json
3try:
4 from ovh import ovh
5except ImportError:
6 # In case it's installed globally
7 import ovh
8import sys
9
10# Credentials are stored in ~/.ovh.conf
11# See ovh/README.rst
12client = ovh.Client()
13
14projects_list = client.get('/cloud/project/')
15if len(projects_list) > 1:
16 print("More than one project is not supported, taking the first one")
17project = projects_list[0]
18instances_list = client.get('/cloud/project/{}/instance'.format(project))
19instances = dict(map(lambda x: (x["id"], x), instances_list))
20if sys.argv[-1] in instances:
21 instance = instances[sys.argv[-1]]
22else:
23 print("Instance not in list:")
24 for instance in instances_list:
25 print("{}: {}".format(instance["name"], instance["id"]))
26 sys.exit(1)
27
28current_image = instance["imageId"]
29available_images = client.get('/cloud/project/{}/image'.format(project),
30 osType="linux",
31 region=instance["region"])
32available_images_ids = list(map(lambda x: x["id"], available_images))
33
34def print_images(available_images):
35 for image in available_images:
36 print("{}: {}".format(image["name"], image["id"]))
37
38def reinstall(image_id):
39 return client.post('/cloud/project/{}/instance/{}/reinstall'.format(project, instance["id"]),
40 imageId=image_id)
41
42if "--get-state" in sys.argv:
43 print(instance["status"])
44elif "--use-current" in sys.argv:
45 if current_image in available_images_ids:
46 print("Current image still available, using it")
47 print(reinstall(current_image))
48 else:
49 print("Current image no more available. Chose among:")
50 print_images(available_images)
51elif sys.argv[-1] in available_templates:
52 print("Chosen image available, using it")
53 print(reinstall(current_image))
54else:
55 print("Chosen image not available. Chose among:")
56 print_images(available_images)
57