-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
83 lines (73 loc) · 2.83 KB
/
__init__.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
from cloudservers import CloudServers, ServerManager, ImageManager, CloudServersException, Server
from time import sleep
class chef_vpc:
def __init__(self):
filename = '/tmp/.rackspace_cloud_account'
self._setup_cloud_instances(filename)
server = self._create_server()
print server.id
self._get_server_status(server.id)
print "Finished"
def _throw_exception(self, exception):
print "Cloud Servers had an exception."
print exception
def _setup_cloud_instances(self, filename):
_creds = self._read_to_dict(filename)
self.cloudservers = CloudServers(_creds['userid'], _creds['api_key'])
self.imageManager = ImageManager(self.cloudservers)
self.serverManager = ServerManager(self.cloudservers)
def _get_list_of_current_servers(self):
cur_servers = {}
try:
cur_servers = self.cloudservers.servers.list()
except CloudServersException as exception:
self._throw_exception(exception)
return cur_servers
def _get_list_of_server_images(self):
server_images = {}
try:
server_images = ImageManager(self.cloudservers).list()
except CloudServersException as exception:
self._throw_exception(exception)
return server_images
def _read_to_dict(self, filename):
fileHandle = file(filename, 'r')
line = fileHandle.readline()
dictName = {}
keycounter = 1
while line:
l = line.split(': ')
if len(l) > 1:
key = l[0]
l[1] = l[1].replace("\n","")
dictName[key] = l[1]
line = fileHandle.readline()
return dictName
def _create_server(self):
print "creating server"
server = ""
try:
server = self.serverManager.create("firsttest4", 51, 2)
except CloudServersException as exception:
self._throw_exception(exception)
return server
def _get_server_status(self, server_id):
server = self.serverManager.get(server_id)
status = ""
while status != "ACTIVE":
try:
print "Progress of server %s is %d"%(server.name, progress)
print "Status is: '%s'"%server.status
sleep(15)
server = self.serverManager.get(server_id)
status = server.status
progress = server.progress
except:
sleep(15)
server = self.serverManager.get(server_id)
status = server.status
progress = server.progress
print "Progress of server %s is %d"%(server.name, progress)
print "Status is: %s"%server.status
if __name__ == '__main__':
chef = chef_vpc()