-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy_to_digital_ocean
executable file
·89 lines (67 loc) · 2.36 KB
/
deploy_to_digital_ocean
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
#!/usr/bin/python
# A simple script for building and deploying artifacts to Digital Ocean.
# Droplet must be 'Ubuntu 16.04.1 x64'.
import os
import sys
import subprocess
if len(sys.argv) < 3:
print "Not enough parameters."
print "Example usage: ./deploy_into_digital_ocean <address> <port>"
quit()
credentials = sys.argv[1]
port = sys.argv[2]
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 " + credentials + " 'pwd'", shell=True)
except:
print "Unexpected error:", sys.exc_info()
quit()
print 'Installing libcurl4-openssl-dev...'
try:
subprocess.check_output("ssh " + 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 ' + credentials + " 'lsof -t -i:" + port + "'" , stderr=subprocess.STDOUT, shell=True).rstrip()
print "Service found PID = " + pid
subprocess.check_output('ssh ' + 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 -avz " + os.path.join(shared, "*") + " " + credentials +":/home/swiftx", stderr=subprocess.STDOUT, shell=True)
except:
quit()
print "Sending release artifact..."
try:
subprocess.check_output("scp .build/release/swiftX " + credentials +":/home/swiftx/release", shell=True)
except:
quit()
print "Starting server..."
try:
subprocess.check_output("ssh " + credentials + " /home/swiftx/release < /dev/null > /tmp/swiftx_log 2>&1 &", shell=True)
except:
quit()
print "Server is running."