]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blob - bin/install_script.sh
Merge branch 'dev'
[perso/Immae/Projets/Puppet.git] / bin / install_script.sh
1 #!/bin/bash
2
3 usage() {
4 cat <<EOF
5 $(basename $0) [options]
6 --help,-h This help
7
8 One of the following options is necessary:
9 --instance-id id Id of the cloud instance
10 --vps-id id Id of the vps
11
12 Optional arguments:
13 --password password Password of the host (only useful in case of no reboot and vps)
14 --reinstall-first Start with reinstalling the vps
15 --host-user user Use another user than the default one
16 --no-reboot Don't reboot
17 --no-reboot-start Don't reboot to rescue at the beginning
18 --no-reboot-end Don't reboot to normal at the end
19 --git-branch branch Use another puppet branch (default: master)
20 --environment env Environment to use for the install (default: production)
21 EOF
22 }
23
24 set -e
25
26 git_branch=master
27 environment=production
28 host_user=""
29 password=""
30 T=""
31
32 while [ -n "$1" ]; do
33 case "$1" in
34 --instance-id)
35 host_id="$2"
36 if [ -z "$host_user" ]; then
37 host_user="arch"
38 fi
39 if [ -z "$password" ]; then
40 password="x"
41 fi
42 [ -n "$T" ] && usage && exit 1
43 T="ovh_cloud_instance"
44 shift
45 ;;
46 --vps-id)
47 host_id="$2"
48 if [ -z "$host_user" ]; then
49 host_user="root"
50 fi
51 [ -n "$T" ] && usage && exit 1
52 T="ovh_vps_ssd"
53 shift
54 ;;
55 --password)
56 password="$2"
57 shift
58 ;;
59 --reinstall-first)
60 reinstall_first=1
61 ;;
62 --host-user)
63 host_user="$2"
64 shift
65 ;;
66 --no-reboot)
67 no_reboot=1
68 ;;
69 --no-reboot-start)
70 no_reboot_start=1
71 ;;
72 --no-reboot-end)
73 no_reboot_end=1
74 ;;
75 --git-branch)
76 git_branch="$2"
77 shift
78 ;;
79 --environment)
80 environment="$2"
81 shift
82 ;;
83 --help|-h)
84 usage
85 exit 0
86 ;;
87 esac
88
89 shift
90 done
91
92 if [ -z "$T" -o -z "$host_id" ]; then
93 usage
94 exit 1
95 fi
96
97 DIRECTORY=$(cd `dirname $0` && pwd)
98 PYTHON_DIRECTORY="$DIRECTORY/../python"
99 SCRIPTS="$DIRECTORY/../scripts"
100
101 if [ -n "$reinstall_first" ]; then
102 echo "Réinstallation du système"
103 python $PYTHON_DIRECTORY/reinstall_$T.py --use-current "$host_id"
104
105 read -p "Appuyer sur une touche quand le serveur est prêt" ready
106 fi
107
108 if [ -z "$no_reboot" -a -z "$no_reboot_start" ]; then
109 echo "Patienter le temps du reboot"
110 python $PYTHON_DIRECTORY/reboot_$T.py --rescue "$host_id"
111
112 read -p "Appuyer sur une touche quand l'instance a redémarré" ready
113 fi
114
115 if [ -z "$password" ]; then
116 stty -echo
117 read -p "Mot de passe reçu par e-mail : " password; echo
118 stty echo
119 fi
120
121 ARCH_DIR=`mktemp -d`
122 ARCH_HOST_SCRIPT="$SCRIPTS/$T/arch_host_script.sh"
123 if [ -f "$SCRIPTS/$T/arch_chroot_script.sh" ]; then
124 ARCH_CHROOT_SCRIPT="$SCRIPTS/$T/arch_chroot_script.sh"
125 else
126 ARCH_CHROOT_SCRIPT=""
127 fi
128 ARCH_INSTALL_SCRIPT="$SCRIPTS/arch_install_script.sh"
129 ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT="$SCRIPTS/$T/arch_host_puppet_configuration_script.sh"
130 ARCH_PUPPET_CONFIGURATION_SCRIPT="$SCRIPTS/arch_puppet_configuration_script.sh"
131 ARCH_PUPPET_INITIAL_CONFIGURATION="$ARCH_DIR/puppet_variables.json"
132
133 trap "rm -rf $ARCH_DIR" EXIT
134
135 #### Base installation stage
136 python $PYTHON_DIRECTORY/get_initial_configuration_$T.py $host_id > $ARCH_PUPPET_INITIAL_CONFIGURATION
137 host_address=$(python $PYTHON_DIRECTORY/get_initial_configuration_$T.py $host_id | jq -r '.ips.v4.ipAddress')
138
139 dest="$host_user@$host_address"
140 files="$ARCH_HOST_SCRIPT $ARCH_CHROOT_SCRIPT $ARCH_PUPPET_INITIAL_CONFIGURATION $ARCH_INSTALL_SCRIPT"
141
142 $SCRIPTS/send_and_run.tcl "$dest" "$password" "$git_branch" "$environment" $files
143
144 ### Role specific stage
145 read -p "Press key when LDAP is configured" i
146
147 files="$ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT $ARCH_PUPPET_CONFIGURATION_SCRIPT"
148
149 $SCRIPTS/send_and_run.tcl "$dest" "$password" "$git_branch" "$environment" $files
150
151 ### Installation finished
152 if [ -z "$no_reboot" -a -z "$no_reboot_end" ]; then
153 echo "Rebooting"
154 python $PYTHON_DIRECTORY/reboot_$T.py --local "$host_id"
155 fi