forked from rethinkdb/rethinkdb_rebirth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_remote_test.py
154 lines (123 loc) · 4.87 KB
/
prepare_remote_test.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import sys
import uuid
import paramiko
import digitalocean
from time import sleep
from datetime import datetime
from subprocess import check_call
DROPLET_NAME = 'test-{uuid}'.format(uuid=str(uuid.uuid4()))
SSH_KEY_NAME = 'key-{name}'.format(name=DROPLET_NAME)
DROPLET_STATUS_COMPLETED = 'completed'
BINTRAY_USERNAME = os.getenv('BINTRAY_USERNAME')
class DropletSetup(object):
def __init__(self, token, size, region):
super(DropletSetup, self).__init__()
self.token = token
self.size = size
self.region = region
self.ssh_client = paramiko.SSHClient()
self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh_key = None
self.digital_ocean_ssh_key = None
self._generate_ssh_key()
self.droplet = digitalocean.Droplet(
token=self.token,
name=DROPLET_NAME,
region=self.region,
image='ubuntu-16-04-x64',
size_slug=self.size,
ssh_keys=[self.digital_ocean_ssh_key.id]
)
@staticmethod
def _print_info(message):
print('[{timestamp}]\t{message}'.format(timestamp=datetime.now().isoformat(), message=message))
def _execute_command(self, command):
self._print_info('executing {command}'.format(command=command))
std_in, _, std_err = self.ssh_client.exec_command(command)
std_in.close()
#for line in std_out.readlines():
# print(line.replace('\n', ''))
has_err = False
for line in std_err.readlines():
has_err = True
print(line.replace('\n', ''))
if has_err:
raise Exception('Script execution failed')
def _generate_ssh_key(self):
self._print_info('generating ssh key')
self.ssh_key = paramiko.rsakey.RSAKey.generate(2048, str(uuid.uuid4()))
self._print_info('create ssh key on DigitalOcean')
self.digital_ocean_ssh_key = digitalocean.SSHKey(
token=self.token,
name=SSH_KEY_NAME,
public_key='ssh-rsa {key}'.format(key=str(self.ssh_key.get_base64()))
)
self.digital_ocean_ssh_key.create()
def create_droplet(self):
self._print_info('creating droplet')
self.droplet.create()
self._print_info('waiting for droplet to be ready')
self._wait_for_droplet()
def _wait_for_droplet(self):
actions = self.droplet.get_actions()
for action in actions:
if action.status == DROPLET_STATUS_COMPLETED:
self.droplet.load()
return
self._wait_for_droplet()
def __enter__(self):
"""
Connect to DigitalOcean instance with forever retry.
"""
self._print_info('connecting to droplet')
try:
self.ssh_client.connect(
hostname=self.droplet.ip_address,
username='root',
allow_agent=True,
pkey=self.ssh_key
)
except Exception as exc:
self._print_info(str(exc))
self._print_info('reconnecting')
sleep(3)
return self.__enter__()
return self
def install_rebirthdb(self):
self._print_info('getting rebirthdb')
self._execute_command('source /etc/lsb-release && echo "deb https://dl.bintray.com/{username}/apt $DISTRIB_CODENAME main" | tee /etc/apt/sources.list.d/rebirthdb.list'.format(username=BINTRAY_USERNAME))
self._execute_command('wget -qO- https://dl.bintray.com/{username}/keys/pubkey.gpg | apt-key add -'.format(username=BINTRAY_USERNAME))
self._print_info('installing rebirthdb')
self._execute_command('apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y rebirthdb')
self._execute_command('echo "bind=all" > /etc/rebirthdb/instances.d/default.conf')
def start_rebirthdb(self):
self._print_info('restarting rebirthdb')
self._execute_command('/etc/init.d/rebirthdb restart')
def run_script(self, script, script_arguments):
self._print_info('executing script')
os.environ["REBIRTHDB_HOST"] = self.droplet.ip_address
check_call([script, ' '.join(script_arguments)])
def __exit__(self, *args):
"""
Cleanup DigitalOcean instance connection.
"""
self._print_info('destroying droplet')
self.droplet.destroy()
self._print_info('removing ssh key')
self.digital_ocean_ssh_key.destroy()
def main():
script = sys.argv[1]
script_arguments = sys.argv[2:]
setup = DropletSetup(
token=os.getenv('DO_TOKEN'),
size=os.getenv('DO_SIZE', '512MB'),
region=os.getenv('DO_REGION', 'sfo2')
)
setup.create_droplet()
with setup:
setup.install_rebirthdb()
setup.start_rebirthdb()
setup.run_script(script, script_arguments)
if __name__ == '__main__':
main()