-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinstall.sh
executable file
·103 lines (81 loc) · 3.6 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
reset
clear
. PARAMETERS
. libs/common.sh
. libs/digitalocean_api.sh
. libs/cloudflare_api.sh
# install jq to parse json responses
installjq
# install resty to make rest calls from bash
getresty
# given a tag get the vm_id from digital ocean
vm_id=`do_getvmidfromtag "$digitalocen_droplet_tag"`
# if vm was not found exit
if [ -z "$vm_id" ];
then
log "no vm found. creating droplet..."
# create a droplet with the given tag
key_id=`do_getkeyidbyname "$private_key"`
vm_id=`do_createdroplet "$digitalocen_droplet_tag" "$mail_server_host.$postfix_admin_domain" "$key_id"`
else
log "found vm $vm_id."
# give the machine the right name
do_renamedroplet "$vm_id" "$mail_server_host.$postfix_admin_domain"
# 'format and reinstall' image to the vm
do_rebuildimage "$vm_id"
fi
# wait machine to be rebuilt and be available
# the problem here is that even if the machine is available
# that does mean that it`s already acessible
# so we check for the machine every 5 seconds
do_waitmachinetobeready "$vm_id"
# ask for the ip address so we can access the machine using ssh
ip_address=`do_getvmipaddressfromid "$vm_id"`
echo "export server_ip=$ip_address" > IP_ADDRESS
# delete the machine from knowhosts so we can add the new finger print for the newly
# created vm
removemachinefromknownhosts "$ip_address"
# wait until port 22 answer with ssh handshake
waitforssh "$ip_address"
# copy installation files to the remote host
# the docker-compose file
log "copying docker-compose to remote host..."
result=`scp -o "StrictHostKeyChecking no" -i "~/.ssh/$private_key" remote/docker-compose.yml root@$ip_address:~/docker-compose.yml.tpl`
# copy postfixadmin.conf file to the remote host
log "copying postfixadmin.conf file to remote host..."
result=`scp -o "StrictHostKeyChecking no" -i "~/.ssh/$private_key" remote/postfixadmin.conf root@$ip_address:~/postfixadmin.conf`
# copy the installation script
log "copying remote_install.sh to remote host..."
result=`scp -o "StrictHostKeyChecking no" -i "~/.ssh/$private_key" remote/remote_install.sh root@$ip_address:~/remote_install.sh`
# make the installation script runnable
result=`ssh -i "~/.ssh/$private_key" root@$ip_address chmod +x /root/remote_install.sh`
# copy PARAMETERS file to the remote host
log "copying PARAMETERS file to remote host..."
result=`scp -o "StrictHostKeyChecking no" -i "~/.ssh/$private_key" PARAMETERS root@$ip_address:~/PARAMETERS`
# copy IP_ADDRESS to the remote host
log "copying IP_ADDRESS to remote host..."
result=`scp -o "StrictHostKeyChecking no" -i "~/.ssh/$private_key" IP_ADDRESS root@$ip_address:~/IP_ADDRESS`
# copy libs/apis to the remote host
log "copying libs/apis to remote host..."
result=`scp -o "StrictHostKeyChecking no" -i "~/.ssh/$private_key" -r libs root@$ip_address:~/libs`
log "preparing to run remote script on first login"
ssh -i "~/.ssh/$private_key" root@$ip_address 'echo -e "~/remote_install.sh\nmv ~/remote_install.sh ~/remote_install.sh.done" >> ~/.bashrc'
rm -rf IP_ADDRESS
rm -rf resty
log "-----------------------"
if [ "$cloudflare_enabled" = "true" ];
then
cf_update_record "$postfix_admin_domain" "A" "$mail_server_host.$postfix_admin_domain" "$ip_address"
else
log "you server ip address is: "$ip_address"\n"
log "fix/review your dns records before continue.\n"
log "set and A record with value $mail_server_host poinintg to $ip_address.\n"
pause "Going to second stage. Press any key to continue..."
fi
log "-----------------------"
log "-----------------------"
log "entering remote host..."
log "-----------------------"
log "-----------------------"
ssh -i "~/.ssh/$private_key" root@$ip_address