forked from cloudmesh/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.txt
219 lines (177 loc) · 7.14 KB
/
launcher.txt
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
def launcher_import(self, d, launcher_name, username):
'''
insert a launcher/recipe into db_clouds
'''
d['cm_launcher'] = launcher_name
d['cm_kind'] = 'launcher'
d['cm_user_id'] = username
self.db_clouds.insert(d)
def launcher_get(self, username, launcher_name=None):
if launcher_name:
return self.db_clouds.find_one({'cm_kind': 'launcher',
'cm_user_id': username,
'cm_cloud': launcher_name})
else:
return self.db_clouds.find({'cm_kind': 'launcher',
'cm_user_id': username})
def launcher_remove(self, username, launcher_name):
self.db_clouds.remove({'cm_kind': 'launcher',
'cm_user_id': username,
'cm_launcher': launcher_name})
---------------
def stack_create(self, name, template_url, parameters, timeout_mins=60):
"""
Create a stack by OpenStack Heat Orchestration
ref: http://developer.openstack.org/api-ref-orchestration-v1.html
"""
url = self._get_service_endpoint("orchestration")[self.service_url_type]
posturl = "%s/stacks" % url
try:
param = ast.literal_eval(parameters)
except ValueError:
param = parameters
params = {
"stack_name": "%s" % name,
"template_url": "%s" % template_url,
"parameters": param,
"timeout_mins": "%s" % timeout_mins
}
log.debug(str(lineno()) + ":POST PARAMS {0}".format(params))
return self._post(posturl, params)
def stack_delete(self, stack_name):
"""
delete a specified stack and returns the id
ref: http://developer.openstack.org/api-ref-orchestration-v1.html
"""
conf = self._get_service_endpoint("orchestration")
url = conf[self.service_url_type]
headers = {'content-type': 'application/json',
'X-Auth-Token': '%s' % conf['token']}
# Find stacks
msg = "stacks/%s" % stack_name
service = "orchestration"
r1 = self._get(msg, service=service,
urltype=self.service_url_type)
try:
stack_id = r1['stack']['id']
except KeyError:
log.warning("stack does not exist ({0})".format(stack_name))
ret = {"msg": "failed"}
return ret
url = "%s/stacks/%s/%s" % (url, stack_name, stack_id)
# no return from http delete via rest api
r = requests.delete(url, headers=headers, verify=self._get_cacert())
ret = {"msg": "success"}
if r.text:
try:
ret = r.json()
except:
pass
return ret
-------------------
from __future__ import print_function
import os
import sys
from cmd3.shell import command
from cloudmesh_base.logger import LOGGER
from cloudmesh_common.tables import row_table
from cmd3.console import Console
from pprint import pprint
from cloudmesh.cm_mongo import cm_mongo
from cloudmesh.config.cm_config import cm_config
from cloudmesh.user.cm_user import cm_user
from cloudmesh.shell.shellutil import shell_commands_dict_output
log = LOGGER(__file__)
class cm_shell_stack:
"""opt_example class"""
name = "stack"
_id = "t_stacks"
def activate_cm_shell_stack(self):
self.register_command_topic('cloud','stack')
pass
def get_cloud_name(self, cm_user_id):
"""Returns a default cloud name if exists
"""
try:
return self.cm_user.get_defaults(cm_user_id)['cloud']
except KeyError:
log.error('set a default cloud with openstack. "stack" works on'
' openstack platform only')
return None
@command
def do_stack(self, args, arguments):
"""
::
Usage:
stack start NAME [--template=TEMPLATE] [--param=PARAM]
stack stop NAME
stack show NAME
stack list [--refresh] [--column=COLUMN] [--format=FORMAT]
stack help | -h
An orchestration tool (OpenStack Heat)
Arguments:
NAME stack name
help Prints this message
Options:
-v verbose mode
"""
self.cm_mongo = cm_mongo()
self.cm_config = cm_config()
self.cm_user = cm_user()
log.info(arguments)
if arguments["help"] or arguments["-h"]:
print(self.do_stack.__doc__)
elif arguments['show'] and arguments['NAME']:
print ("NOT IMPLEMENTED")
return
elif arguments['start'] and arguments['NAME']:
userid = self.cm_config.username()
def_cloud = self.get_cloud_name(userid)
t_url = arguments['--template']
param = arguments['--param']
s_name = arguments['NAME']
self.cm_mongo.activate(userid)
res = self.cm_mongo.stack_create(cloud=def_cloud, cm_user_id=userid,
servername=s_name,
template_url=t_url,
parameters=param)
print(res)
return res
elif arguments['stop'] and arguments['NAME']:
userid = self.cm_config.username()
def_cloud = self.get_cloud_name(userid)
s_name = arguments['NAME']
self.cm_mongo.activate(userid)
res = self.cm_mongo.stack_delete(cloud=def_cloud, cm_user_id=userid,
server=s_name)
return res
elif arguments['list']:
userid = self.cm_config.username()
if arguments['--refresh']:
self.cm_mongo.activate(userid)
self.cm_mongo.refresh(cm_user_id=userid, types=[self._id])
d = self.cm_mongo.stacks(cm_user_id=userid)
columns = None
if arguments['--column']:
if arguments['--column'] != "all":
columns = [x.strip() for x in arguments['--column'].split(',')]
else:
columns = ['stack_name','description', 'stack_status',
'creation_time', 'cm_cloud']
if arguments['--format']:
if arguments['--format'] not in ['table', 'json', 'csv']:
Console.error("please select printing format among table, json and csv")
return
else:
p_format = arguments['--format']
else:
p_format = None
for k, v in d.iteritems():
for k0, v0 in v.iteritems():
if '_id' in v0:
del v0['_id']
shell_commands_dict_output(userid,
v,
print_format=p_format,
firstheader="stack_id",
header=columns)