-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy-digitalocean.py
82 lines (70 loc) · 2.12 KB
/
deploy-digitalocean.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
#!/usr/bin/env python2.7
''' Deploy Kako to Digital Ocean. '''
import sys
import time
import logging
import click
import digitalocean
@click.command()
@click.option('--api-token', help='Digital Ocean API Token')
@click.option(
'--region',
help='Region to deploy into (default: sfo1)',
default='sfo1'
)
@click.option(
'--size',
help='Size of droplet to deploy (default: 1gb)',
default='1gb'
)
@click.option(
'--cloud-config',
help='The user-data / cloud-config to use (default: cloud-config.yaml)',
default='cloud-config.yaml'
)
def main(api_token, region, size, cloud_config):
''' Attempts to deploy a Kako instance to Digital Ocean. '''
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(process)d - [%(levelname)s] %(message)s'
)
log = logging.getLogger()
# Ensure a token was provided.
if api_token is None:
log.error('No API token provided in call, cannot continue!')
sys.exit(-1)
# Read in user-data.
log.info('Attempting to read user-data from %s', cloud_config)
with open(cloud_config, 'r') as hndl:
user_data = hndl.read()
# Launch.
log.info('Attempting to create %s droplet in %s', size, region)
droplet = digitalocean.Droplet(
name='kako-worker',
token=api_token,
image='ubuntu-16-04-x64',
region=region,
size_slug=size,
user_data=user_data,
)
droplet.create()
log.info('Droplet launched!')
# Start monitoring until built.
running = False
while not running:
actions = droplet.get_actions()
for action in actions:
action.load()
# Check whether the droplet is up.
if action.status == 'completed':
running = True
log.info('Droplet is up!')
break
# Otherwise wait and try again soon.
log.info('Droplet still in state %s', action.status)
time.sleep(10)
# Print IP and exit.
droplet.load()
log.info('New Kako host should be running at %s', droplet.ip_address)
if __name__ == '__main__':
main()