Skip to content

Commit 8834b94

Browse files
author
Gabriel Monroy
committed
check for valid flavor on formations:create, add time-based done output #16
1 parent abbc10a commit 8834b94

1 file changed

Lines changed: 27 additions & 32 deletions

File tree

client/deis.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
create create a new container formation
1414
info print a representation of the formation
1515
scale scale container types (web=2, worker=1)
16-
balance rebalance the container formation
1716
converge force-converge all nodes in the formation
1817
calculate recalculate and update the formation databag
1918
destroy destroy a container formation
@@ -50,6 +49,7 @@
5049
from docopt import docopt
5150
from docopt import DocoptExit
5251
import requests
52+
import time
5353
import yaml
5454

5555

@@ -525,11 +525,12 @@ def containers_scale(self, args):
525525
typ, count = type_num.split('=')
526526
body.update({typ: int(count)})
527527
print('Scaling containers... but first, coffee!')
528+
before = time.time()
528529
response = self._dispatch('post',
529530
"/api/formations/{}/scale/containers".format(formation),
530531
json.dumps(body))
531532
if response.status_code == requests.codes.ok: # @UndefinedVariable
532-
print '...done'
533+
print('done in {}s\n'.format(int(time.time() - before)))
533534
self.containers_list({})
534535
else:
535536
print('Error!', response.text)
@@ -651,6 +652,13 @@ def formations_create(self, args):
651652
o = args.get(opt)
652653
if o:
653654
body.update({opt.strip('-'): o})
655+
# if a flavor was passed, make sure its valid
656+
flavor = args.get('--flavor')
657+
if flavor:
658+
response = self._dispatch('get', '/api/flavors/{}'.format(flavor))
659+
if response.status_code != 200:
660+
print 'Flavor not found'
661+
return
654662
sys.stdout.write('Creating formation... ')
655663
sys.stdout.flush()
656664
response = self._dispatch('post', '/api/formations',
@@ -670,7 +678,6 @@ def formations_create(self, args):
670678
sys.exit(1)
671679
print('Git remote deis added')
672680
# create default layers if a flavor was provided
673-
flavor = args.get('--flavor')
674681
if flavor:
675682
print
676683
self.layers_create({'<id>': 'runtime', '<flavor>': flavor})
@@ -712,7 +719,7 @@ def formations_list(self, args):
712719
if response.status_code == requests.codes.ok: # @UndefinedVariable
713720
data = response.json()
714721
if data['count'] == 0:
715-
print 'No flavors found'
722+
print 'No formations found'
716723
return
717724
print("=== {owner} Formations".format(**data['results'][0]))
718725
for item in data['results']:
@@ -748,10 +755,11 @@ def formations_destroy(self, args):
748755
return
749756
sys.stdout.write("Destroying {}... ".format(formation))
750757
sys.stdout.flush()
758+
before = time.time()
751759
response = self._dispatch('delete', "/api/formations/{}".format(formation))
752760
if response.status_code in (requests.codes.no_content, # @UndefinedVariable
753761
requests.codes.not_found): # @UndefinedVariable
754-
print('done')
762+
print('done in {}s'.format(int(time.time() - before)))
755763
try:
756764
subprocess.check_call(
757765
['git', 'remote', 'rm', 'deis'],
@@ -784,27 +792,6 @@ def formations_calculate(self, args, quiet=False):
784792
else:
785793
print('Error!', response.text)
786794

787-
def formations_balance(self, args):
788-
"""
789-
Rebalance containers across a formation
790-
791-
In the event nodes are added or removed outside of a
792-
containers:scale command, balancing the formation will
793-
ensure containers are spread evenly across nodes.
794-
795-
Usage: deis formations:balance
796-
"""
797-
formation = args.get('--formation')
798-
if not formation:
799-
formation = self._session.formation
800-
response = self._dispatch('post',
801-
"/api/formations/{}/balance".format(formation))
802-
if response.status_code == requests.codes.ok: # @UndefinedVariable
803-
databag = json.loads(response.content)
804-
print(json.dumps(databag, indent=2))
805-
else:
806-
print('Error!', response.text)
807-
808795
def formations_converge(self, args):
809796
"""
810797
Force converge a formation
@@ -818,9 +805,13 @@ def formations_converge(self, args):
818805
formation = args.get('--formation')
819806
if not formation:
820807
formation = self._session.formation
808+
sys.stdout.write('Converging {}... '.format(formation))
809+
sys.stdout.flush()
810+
before = time.time()
821811
response = self._dispatch('post',
822812
"/api/formations/{}/converge".format(formation))
823813
if response.status_code == requests.codes.ok: # @UndefinedVariable
814+
print('done in {}s'.format(int(time.time() - before)))
824815
databag = json.loads(response.content)
825816
print(json.dumps(databag, indent=2))
826817
else:
@@ -959,10 +950,11 @@ def layers_create(self, args):
959950
body['run_list'] = 'recipe[deis],recipe[deis::proxy]'
960951
sys.stdout.write("Creating {} layer... ".format(args['<id>']))
961952
sys.stdout.flush()
953+
before = time.time()
962954
response = self._dispatch('post', "/api/formations/{}/layers".format(formation),
963955
json.dumps(body))
964956
if response.status_code == requests.codes.created: # @UndefinedVariable
965-
print('done')
957+
print('done in {}s'.format(int(time.time() - before)))
966958
else:
967959
print('Error!', response.text)
968960

@@ -978,10 +970,11 @@ def layers_destroy(self, args):
978970
layer = args['<id>'] # noqa
979971
sys.stdout.write("Destroying {layer} layer... ".format(**locals()))
980972
sys.stdout.flush()
973+
before = time.time()
981974
response = self._dispatch(
982975
'delete', "/api/formations/{formation}/layers/{layer}".format(**locals()))
983976
if response.status_code == requests.codes.no_content: # @UndefinedVariable
984-
print('done')
977+
print('done in {}s'.format(int(time.time() - before)))
985978
else:
986979
print('Error!', response.text)
987980

@@ -1022,12 +1015,13 @@ def layers_scale(self, args):
10221015
typ, count = type_num.split('=')
10231016
body.update({typ: int(count)})
10241017
print('Scaling layers... but first, coffee!')
1018+
before = time.time()
10251019
# TODO: add threaded spinner to print dots
10261020
response = self._dispatch('post',
10271021
"/api/formations/{}/scale/layers".format(formation),
10281022
json.dumps(body))
10291023
if response.status_code == requests.codes.ok: # @UndefinedVariable
1030-
print('...done\n')
1024+
print('done in {}s\n'.format(int(time.time() - before)))
10311025
print('Use `git push deis master` to deploy to your formation')
10321026
else:
10331027
print('Error!', response.text)
@@ -1096,12 +1090,13 @@ def nodes_destroy(self, args):
10961090
if not formation:
10971091
formation = self._session.formation
10981092
node = args['<id>']
1099-
response = self._dispatch('delete',
1100-
"/api/formations/{formation}/nodes/{node}".format(**locals()))
11011093
sys.stdout.write("Destroying {}... ".format(node))
11021094
sys.stdout.flush()
1095+
before = time.time()
1096+
response = self._dispatch('delete',
1097+
"/api/formations/{formation}/nodes/{node}".format(**locals()))
11031098
if response.status_code == requests.codes.no_content: # @UndefinedVariable
1104-
print('done')
1099+
print('done in {}s\n'.format(int(time.time() - before)))
11051100
else:
11061101
print('Error!', response.status_code, response.text)
11071102

0 commit comments

Comments
 (0)