-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy_to_amazon_ec2
executable file
·94 lines (72 loc) · 2.88 KB
/
deploy_to_amazon_ec2
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
#!/usr/bin/python
# A simple script for building and deploying artifacts to Amazon EC2.
# EC2 instancc must be 'Ubuntu 16.04.1 x64'.
import os
import sys
import os
import subprocess
if len(sys.argv) < 4:
print "Not enough parameters."
print "Example: ./deploy_into_amazon_ec2 <address> <port> <private key>"
quit()
credentials = sys.argv[1]
port = sys.argv[2]
pem_file = sys.argv[3]
swiftc_path = subprocess.check_output("which swiftc", shell=True)
head, tail = os.path.split(swiftc_path.rstrip())
root, tail = os.path.split(head)
shared = os.path.join(root, "lib", "swift", "linux")
if not os.path.isdir(shared):
print "Shared objects directory not found at: " + shared
quit()
print "Found shared objects directory: " + shared
print "Building project..."
try:
subprocess.check_output("swift build -v -c release -Xlinker -rpath -Xlinker .", shell=True)
except:
quit()
print "Build ok."
print 'Checking SSH connection...'
try:
subprocess.check_output('ssh -i "' + pem_file + '" ' + credentials + " 'pwd'", shell=True)
except:
print "Unexpected error:", sys.exc_info()
quit()
print 'Installing libcurl4-openssl-dev (it can take some time)...'
try:
subprocess.check_output('ssh -i "' + pem_file + '" ' + credentials + " 'sudo apt-get -y install libcurl4-openssl-dev'", shell=True)
except:
print "Unexpected error:", sys.exc_info()
quit()
print 'Checking if service is on...'
try:
pid = subprocess.check_output('ssh -i "' + pem_file + '" ' + credentials + " 'lsof -t -i:" + port + "'" , stderr=subprocess.STDOUT, shell=True).rstrip()
print "Service found PID = " + pid
subprocess.check_output('ssh -i "' + pem_file + '" ' + credentials + " 'kill -9 $(lsof -t -i:" + port + ")'", stderr=subprocess.STDOUT, shell=True).rstrip()
print "Service killed."
except subprocess.CalledProcessError, e:
if e.returncode == 1:
print "Service not found on port: " + str(port) + ". No restart needed."
else:
print "Unexpected error:", sys.exc_info()
print e.output
quit()
print "Syncing Swift core shared objects..."
try:
subprocess.check_output('rsync -PazSHAX --rsh "ssh -i ' + pem_file + '" --rsync-path "sudo rsync" ' + os.path.join(shared, "*") + " " + credentials +":/home/ubuntu/swiftx", stderr=subprocess.STDOUT, shell=True)
except:
print "Unexpected error:", sys.exc_info()
quit()
print "Sending release artifact..."
try:
subprocess.check_output('rsync --rsh "ssh -i ' + pem_file + '" --rsync-path "sudo rsync" ./.build/release/swiftX ' + credentials + ":/home/ubuntu/swiftx/release", shell=True)
except:
print "Failed to rsync executable: ", sys.exc_info()
quit()
print "Starting server..."
try:
subprocess.check_output('ssh -i "' + pem_file + '" ' + credentials + " '/home/ubuntu/swiftx/release < /dev/null > /tmp/swiftx_log 2>&1 &'", shell=True)
except:
print "Failed to run executable: ", sys.exc_info()
quit()
print "Server is running. Make sure you enabled " + port + " at Security Grups settings."