-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.py
339 lines (268 loc) · 9.99 KB
/
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import os
from pprint import pprint
from novaclient import exceptions
from novaclient.client import Client
from cloudmesh_client.common.ConfigDict import ConfigDict
from cloudmesh_client.common.util import path_expand
# from cloudmesh_client.MultiKeyDict import MultiKeyDict
# disable security warning for old certs
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
def get_nova_credentials(kind="yaml", cloud=None):
d = {}
if kind in ["env"]:
d['version'] = '2'
d['username'] = os.environ['OS_USERNAME']
d['api_key'] = os.environ['OS_PASSWORD']
d['auth_url'] = os.environ['OS_AUTH_URL']
d['project_id'] = os.environ['OS_TENANT_NAME']
d['cacert'] = path_expand(os.environ['OS_CACERT'])
elif kind in ["yaml"]:
if cloud is None:
raise Exception("cloud not specified")
config = dict(ConfigDict(filename="~/.cloudmesh/cloudmesh.yaml")["cloudmesh"]["clouds"][cloud])
cred = dict(config["credentials"])
d['version'] = '2'
d['username'] = cred['OS_USERNAME']
d['api_key'] = cred['OS_PASSWORD']
d['auth_url'] = cred['OS_AUTH_URL']
d['project_id'] = cred['OS_TENANT_NAME']
if 'OS_CACERT' in cred:
d['cacert'] = path_expand(cred['OS_CACERT'])
else:
raise Exception("unsupported kind: " + kind)
return d
'''
openstack_client_keys = [
["username", "OS_USERNAME"],
["password", "OS_PASSWORD", "api_key"],
["auth_url", "OS_AUTH_URL"],
["tenant_id", "OS_TENANT_ID"],
["region_name", "OS_REGION_NAME"],
["cacert", "OS_CACERT"],
["version", "OS_COMPUTE_API_VERSION"],
["project_id", "OS_TENANT_NAME"],
["os_version", "OS_VERSION"]
]
m = MultiKeyDict(openstack_client_keys)
m["username"] = "gregor"
print ("U", m["username"])
print ("U", m["OS_USERNAME"])
m["OS_PASSWORD"] = "1234"
print("P", m["password"])
print("P", m["OS_PASSWORD"])
# m.set(get_nova_credentials_v2())
'''
class CloudMesh(object):
def __init__(self):
self.config = dict(ConfigDict(filename="~/.cloudmesh/cloudmesh.yaml")["cloudmesh"]["clouds"])
self.client = {}
def cloud_username(self, cloud):
if self.cloud_type(cloud) == "openstack":
return self.config[cloud]["credentials"]["OS_USERNAME"]
else:
raise Exception("username for this cloud type not defined")
def cloud_project(self, cloud):
if self.cloud_type(cloud) == "openstack":
return self.config[cloud]["credentials"]["OS_TENANT_NAME"]
else:
raise Exception("username for this cloud type not defined")
def cloud_type(self, cloud):
return self.config[cloud]["cm_type"]
def credentials(self, cloud):
if self.cloud_type(cloud) == "openstack":
cred = get_nova_credentials(cloud=cloud)
return dict(cred)
else:
print("not supported cloud type")
def authenticate(self, cloud):
credential = self.credentials(cloud)
self.client[cloud] = Client(**credential)
# #############################################
# KEY LIST
# #############################################
def list(self, kind, cloud=None):
result = {cloud: {}}
def insert(objects):
for i in objects:
result[cloud][i.name] = i.__dict__
if self.cloud_type(cloud) == "openstack":
if kind in ["server"]:
objects = self.client[cloud].servers.list()
insert(objects)
elif kind in ["flavor"]:
objects = self.client[cloud].flavors.list()
insert(objects)
elif kind in ["image"]:
objects = self.client[cloud].images.list()
insert(objects)
elif kind in ["key"]:
objects = self.client[cloud].keypairs.list()
result[cloud]["key"] = {}
for i in objects:
result[cloud]["key"][i.name] = i.__dict__["_info"]["keypair"]
elif kind in ["limit"]:
objects = self.client[cloud].limits.get()
result[cloud]["limits"] = objects.__dict__["_info"]
elif kind in ["quota"]:
# username = self.cloud_username(cloud)
project = self.cloud_project(cloud)
# get is permission denied on india so we use by default the default
# objects = self.client[cloud].quotas.get(username, project)
objects = self.client[cloud].quotas.defaults(project)
result[cloud]["quota"] = objects.__dict__["_info"]
else:
raise Exception("unsupported kind or cloud: " + kind + " " + str(cloud))
return result
# #############################################
# IMAGE MANAGEMENT
# #############################################
def image_get(self, name, cloud=None):
pass
# #############################################
# FLAVOR MANAGEMENT
# #############################################
def flavor_get(self, name, cloud=None):
pass
# #############################################
# SEVER MANAGEMENT
# #############################################
def vm_create(self, name, flavor, image, secgroup, keypair, meta, userdata, cloud=None):
self.client[cloud].servers.create(name, image, flavor, meta=meta,
security_groups=secgroup, key_name=keypair, userdata=userdata)
# TBD
def vm_delete(self, name, cloud):
self.client[cloud].servers.delete()
def vm_set_meta(self, name, cloud=None):
pass
def vm_get_meta(self, name, cloud=None):
pass
def vm_set_userdata(self, name, cloud=None):
pass
def vm_get_usedata(self, name, cloud=None):
pass
# #############################################
# SECURITY MANAGEMENT
# #############################################
def secgroup_create(self):
pass
def secgroup_delete(self):
pass
def secgroup_get(self):
pass
def secgroup_list(self):
pass
def secgroup_list_rule(self):
pass
# #############################################
# KEY MANAGEMENT
# #############################################
def key_add(self, name, filename, cloud=None):
keyfile = path_expand(filename)
if self.cloud_type(cloud) == "openstack":
with open(os.path.expanduser(filename), 'r') as public_key:
try:
self.client[cloud].keypairs.create(name=name, public_key=public_key.read())
except exceptions.Conflict, e:
print("key already exists: {0}".format(str(e)))
else:
raise Exception("unsupported kind or cloud: " + kind + " " + str(cloud))
def key_find(self, name, cloud=None):
"""finds the key with the given name
:param name: name of the key
:param cloud: name of the cloud
:return:
"""
pass
def key_delete(self, name, cloud=None):
"""deletes the key with the given name
:param name: name of the key
:param cloud: name of the cloud
:return:
"""
self.client[cloud].keypairs.delete(name)
# #############################################
# DELETE
# #############################################
def delete(self, kind, name, cloud=None):
if self.cloud_type(cloud) == "openstack":
if kind in ["key"]:
self.key_delete(name, cloud=cloud)
elif kind in ["server"]:
self.vm_delete(name, cloud=cloud)
else:
raise Exception("unsupported kind or cloud: " + kind + " " + str(cloud))
# mesh = Mesh()
# cred = mesh.credentials("india")
# print("CCC", cred)
# credential = get_nova_credentials(cloud="india")
# pprint (credential)
# credential = get_nova_credentials(kind="env")
# pprint (credential)
# nova_client = Client(**credential)
# print(nova_client.servers.list())
# credential = get_nova_credentials(cloud="chameleon")
# pprint (credential)
# credential = get_nova_credentials(kind="env")
# pprint (credential)
# nova_client = Client(**credential)
# print(nova_client.servers.list())
print("===================")
cm = CloudMesh()
clouds = ["india", "chameleon"]
kinds = ["limit", "quota", "server", "flavor", "image"]
clouds = ["india"]
kinds = ["key"]
for cloud in clouds:
cm.authenticate(cloud)
# l = cm.list("key", cloud="india")
# pprint (l)
for cloud in clouds:
for kind in kinds:
l = cm.list(kind, cloud=cloud)
pprint(l)
cm.key_delete("gregor-key", cloud='india')
print("--------------")
pprint(cm.list("key", cloud='india'))
print("--------------")
cm.key_add("gregor-key", "~/.ssh/id_rsa.pub", cloud='india')
pprint(cm.list("key", cloud='india'))
print("--------------")
'''
export OS_USERNAME=username
export OS_PASSWORD=password
export OS_TENANT_NAME=a
export OS_AUTH_URL=https://identityHost:portNumber/v2.0
export OS_TENANT_ID=tenantIDString
export OS_REGION_NAME=regionName
export OS_CACERT=/path/to/caceratFile
export OS_COMPUTE_API_VERSION=2
username (str) - Username
api_key (str) - API Key
project_id (str) - Project ID
auth_url (str) - Auth URL
insecure (bool) - Allow insecure
timeout (float) - API timeout, None or 0 disables
proxy_tenant_id (str) - Tenant ID
proxy_token (str) - Proxy Token
region_name (str) - Region Name
endpoint_type (str) - Endpoint Type
extensions (str) - Exensions
service_type (str) - Service Type
service_name (str) - Service Name
volume_service_name (str) - Volume Service Name
timings (bool) - Timings
bypass_url (str) - Bypass URL
os_cache (bool) - OS cache
no_cache (bool) - No cache
http_log_debug (bool) - Enable debugging for HTTP connections
auth_system (str) - Auth system
auth_plugin (str) - Auth plugin
auth_token (str) - Auth token
cacert (str) - cacert
tenant_id (str) - Tenant ID
user_id (str) - User ID
connection_pool (bool) - Use a connection pool
session (str) - Session
auth (str) - Auth
'''