Skip to content

Commit 5a16b10

Browse files
author
Clark Perkins
committed
Beginnings of stacks
1 parent ac94959 commit 5a16b10

File tree

3 files changed

+78
-76
lines changed

3 files changed

+78
-76
lines changed

stackdio/cli/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def server_version(obj):
4949

5050
# Add all our other commands
5151
stackdio.add_command(blueprints.blueprints)
52+
stackdio.add_command(stacks.stacks)
5253

5354

5455
class StackdioShell(Cmd, bootstrap.BootstrapMixin, stacks.StackMixin,

stackdio/cli/mixins/blueprints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def create_all_blueprints(obj):
178178
click.secho('Blueprint {0} NOT created\n'.format(name), fg='magenta')
179179

180180

181-
def _get_blueprint_id(client, blueprint_title):
181+
def get_blueprint_id(client, blueprint_title):
182182
found_blueprints = client.search_blueprints(title=blueprint_title)
183183

184184
if len(found_blueprints) == 0:
@@ -198,7 +198,7 @@ def delete_blueprint(obj, title):
198198
"""
199199
client = obj['client']
200200

201-
blueprint_id = _get_blueprint_id(client, title)
201+
blueprint_id = get_blueprint_id(client, title)
202202

203203
click.confirm('Really delete blueprint {0}?'.format(title), abort=True)
204204

stackdio/cli/mixins/stacks.py

Lines changed: 75 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,86 @@
22

33
import json
44

5+
import click
56
from cmd2 import Cmd
67

8+
from stackdio.cli.mixins.blueprints import get_blueprint_id
9+
from stackdio.cli.utils import print_summary
710
from stackdio.client.exceptions import StackException
811

912

13+
@click.group()
14+
def stacks():
15+
"""
16+
Perform actions on stacks
17+
"""
18+
pass
19+
20+
21+
@stacks.command(name='list')
22+
@click.pass_obj
23+
def list_stacks(obj):
24+
"""
25+
List all stacks
26+
"""
27+
client = obj['client']
28+
29+
click.echo('Getting stacks ... ')
30+
print_summary('Stack', client.list_stacks())
31+
32+
33+
@stacks.command(name='launch')
34+
@click.pass_obj
35+
@click.argument('blueprint_title')
36+
@click.argument('stack_title')
37+
def launch_stack(obj, blueprint_title, stack_title):
38+
"""
39+
Launch a stack from a blueprint
40+
"""
41+
client = obj['client']
42+
43+
blueprint_id = get_blueprint_id(client, blueprint_title)
44+
45+
click.echo('Launching stack "{0}" from blueprint "{1}"'.format(stack_title,
46+
blueprint_title))
47+
48+
stack_data = {
49+
'blueprint': blueprint_id,
50+
'title': stack_title,
51+
'description': 'Launched from blueprint %s' % (blueprint_title),
52+
'namespace': stack_title,
53+
}
54+
results = client.create_stack(stack_data)
55+
click.echo('Stack launch results:\n{0}'.format(results))
56+
57+
58+
def get_stack_id(client, stack_title):
59+
found_stacks = client.search_stacks(title=stack_title)
60+
61+
if len(found_stacks) == 0:
62+
raise click.Abort('Stack "{0}" does not exist'.format(stack_title))
63+
elif len(found_stacks) > 1:
64+
raise click.Abort('Multiple stacks matching "{0}" were found'.format(stack_title))
65+
else:
66+
return found_stacks[0]['id']
67+
68+
69+
@stacks.command(name='history')
70+
@click.pass_obj
71+
@click.argument('stack_title')
72+
@click.option('-l', '--length', type=click.INT, default=20, help='The number of entries to show')
73+
def stack_history(obj, stack_title, length):
74+
"""
75+
Print recent history for a stack
76+
"""
77+
client = obj['client']
78+
79+
stack_id = get_stack_id(client, stack_title)
80+
history = client.get_stack_history(stack_id)
81+
for event in history[0:min(length, len(history))]:
82+
click.echo('[{created}] {level} // {event} // {status}'.format(**event))
83+
84+
1085
class StackMixin(Cmd):
1186

1287
STACK_ACTIONS = ["start", "stop", "launch_existing", "terminate", "provision", "custom"]
@@ -57,67 +132,6 @@ def do_stacks(self, arg):
57132
else:
58133
print(USAGE)
59134

60-
def complete_stacks(self, text, line, begidx, endidx):
61-
# not using line, begidx, or endidx, thus the following pylint disable
62-
# pylint: disable=W0613
63-
return [i for i in self.STACK_COMMANDS if i.startswith(text)]
64-
65-
def help_stacks(self):
66-
print("Manage stacks.")
67-
print("Sub-commands can be one of:\n\t{0}".format(
68-
", ".join(self.STACK_COMMANDS)))
69-
print("Try 'stacks COMMAND' to get help on (most) sub-commands")
70-
71-
def _list_stacks(self):
72-
"""List all running stacks"""
73-
74-
print("Getting running stacks ... ")
75-
stacks = self.stacks.list_stacks()
76-
self._print_summary("Stack", stacks)
77-
78-
def _launch_stack(self, args):
79-
"""Launch a stack from a blueprint.
80-
Must provide blueprint name and stack name"""
81-
82-
if len(args) != 2:
83-
print("Usage: stacks launch BLUEPRINT_NAME STACK_NAME")
84-
return
85-
86-
blueprint_name = args[0]
87-
stack_name = args[1]
88-
89-
try:
90-
blueprint_id = self.stacks.get_blueprint_id(blueprint_name)
91-
except StackException:
92-
print(self.colorize(
93-
"Blueprint [{0}] does not exist".format(blueprint_name),
94-
"red"))
95-
return
96-
97-
print("Launching stack [{0}] from blueprint [{1}]".format(
98-
stack_name, blueprint_name))
99-
100-
stack_data = {
101-
"blueprint": blueprint_id,
102-
"title": stack_name,
103-
"description": "Launched from blueprint %s" % (blueprint_name),
104-
"namespace": stack_name,
105-
"max_retries": 1,
106-
}
107-
results = self.stacks.create_stack(stack_data)
108-
print("Stack launch results:\n{0}".format(results))
109-
110-
def _get_stack_id(self, stack_name):
111-
"""Validate that a stack exists"""
112-
113-
try:
114-
return self.stacks.get_stack_id(stack_name)
115-
except StackException:
116-
print(self.colorize(
117-
"Stack [{0}] does not exist".format(stack_name),
118-
"red"))
119-
raise
120-
121135
def _stack_action(self, args):
122136
"""Perform an action on a stack."""
123137

@@ -165,19 +179,6 @@ def _stack_action(self, args):
165179
results = self.stacks.do_stack_action(stack_id, action)
166180
print("Stack action results:\n{0}".format(json.dumps(results, indent=3)))
167181

168-
def _stack_history(self, args):
169-
"""Print recent history for a stack"""
170-
171-
NUM_EVENTS = 20
172-
if len(args) < 1:
173-
print("Usage: stacks history STACK_NAME")
174-
return
175-
176-
stack_id = self._get_stack_id(args[0])
177-
history = self.stacks.get_stack_history(stack_id).get("results")
178-
for event in history[0:min(NUM_EVENTS, len(history))]:
179-
print("[{created}] {level} // {event} // {status}".format(**event))
180-
181182
def _stack_hostnames(self, args):
182183
"""Print hostnames for a stack"""
183184

0 commit comments

Comments
 (0)