-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOInterface.py
More file actions
69 lines (55 loc) · 1.5 KB
/
DOInterface.py
File metadata and controls
69 lines (55 loc) · 1.5 KB
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
import skiff
import threading
import logging
import json
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-10s) %(message)s',
)
class DOInterface(object):
def __init__(self, doKey, SSHKeyName, n):
self.DOApi = skiff.rig(doKey)
self.SSHKey = self.DOApi.Key.get(SSHKeyName)
self.n = n
self.droplets = []
def createDroplet(self, i):
nd = self.DOApi.Droplet.create(name='test'+str(i),
region='nyc3',
size='512mb',
image='ubuntu-14-04-x64',
ssh_keys=[self.SSHKey])
nd.wait_till_done()
nd = nd.refresh()
self.droplets.append(nd)
def destroyDroplet(self, droplet):
droplet.destroy()
def createDroplets(self):
threads = []
for i in range(0, self.n):
t = threading.Thread(target=self.createDroplet, args=([i]))
threads.append(t)
t.start()
main_thread = threading.currentThread()
for t in threading.enumerate():
if t is main_thread:
continue
logging.debug('joining %s', t.getName())
t.join()
return self.droplets
def destroyDroplets(self):
for i in range(0, len(self.droplets)):
self.destroyDroplet(self.droplets[i])
def importDroplets(self, f):
json_data = open(f)
data = json.load(json_data)
self.droplets = []
for x in data:
d = self.DOApi.Droplet.get(x)
self.droplets.append(d)
return self.droplets
def exportDroplets(self):
data = []
for d in self.droplets:
data.append(d.id)
f = open('droplets.json', 'w')
f.write(json.dumps(data))
f.close()