From 32266947c7c1056821e1d810a5938bd6dcaa6a30 Mon Sep 17 00:00:00 2001 From: Christophe Siraut Date: Thu, 13 Sep 2018 11:17:56 +0200 Subject: [PATCH 2/2] cook: add prechecks on recipe (#16599) --- hobo/environment/management/commands/cook.py | 51 +++++++++++++++++++++------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/hobo/environment/management/commands/cook.py b/hobo/environment/management/commands/cook.py index 913e39f..8a88f03 100644 --- a/hobo/environment/management/commands/cook.py +++ b/hobo/environment/management/commands/cook.py @@ -35,7 +35,7 @@ from hobo.agent.common.management.commands.hobo_deploy import ( from hobo.multitenant.middleware import TenantMiddleware from hobo.environment.models import (AVAILABLE_SERVICES, Authentic, Wcs, Hobo, Passerelle, Combo, Fargo, Welco, MandayeJS, Chrono, Corbo, BiJoe, - Variable) + Variable, ServiceBase) from hobo.deploy.signals import notify_agents from hobo.theme.utils import set_theme from hobo.profile.models import AttributeDefinition @@ -53,34 +53,42 @@ class Command(BaseCommand): parser.add_argument( '--timeout', type=int, action='store', default=120, help='set the timeout for the wait_operationals method') + parser.add_argument('--permissive', action='store_true', help='ignore integrity checks result') def handle(self, recipe, *args, **kwargs): self.verbosity = kwargs.get('verbosity') self.timeout = kwargs.get('timeout') + self.permissive = kwargs.get('permissive') self.run_cook(recipe) if self.verbosity: - print 'All steps executed successfully. Your environment should now be ready.' + print('All steps executed successfully. Your environment should now be ready.') def run_cook(self, filename): recipe = json.load(open(filename)) - variables = {} + steps = self.get_steps(recipe, os.path.dirname(filename)) + self.perform_checks(steps) + for action, action_args in steps: + getattr(self, action)(**action_args) + if self.must_notify: + notify_agents(None) + self.wait_operationals(timeout=self.timeout) + self.must_notify = False + notify_agents(None) + + def get_steps(self, recipe, path): + variables, steps = {}, [] if 'load-variables-from' in recipe: variables.update(json.load(open( - os.path.join(os.path.dirname(filename), recipe['load-variables-from'])))) + os.path.join(path, recipe['load-variables-from'])))) variables.update(recipe.get('variables', {})) for step in recipe.get('steps', []): action, action_args = step.items()[0] for arg in action_args: if not isinstance(action_args[arg], basestring): continue - action_args[arg] = string.Template( - action_args[arg]).substitute(variables) - getattr(self, action.replace('-', '_'))(**action_args) - if self.must_notify: - notify_agents(None) - self.wait_operationals(timeout=self.timeout) - self.must_notify = False - notify_agents(None) + action_args[arg] = string.Template(action_args[arg]).substitute(variables) + steps.append((action.replace('-', '_'), action_args)) + return steps def wait_operationals(self, timeout): services = [] @@ -148,7 +156,7 @@ class Command(BaseCommand): user.set_password(password) user.save() if created and self.verbosity: - print 'superuser account: %s / %s' % (username, password) + print('superuser account: %s / %s' % (username, password)) def create_site(self, klass, base_url, title, slug, template_name, variables): if slug is None: @@ -281,3 +289,20 @@ class Command(BaseCommand): current_tenant = connection.get_tenant() self.run_cook(filename) connection.set_tenant(current_tenant) + + def perform_checks(self, steps): + errors = [] + for action, action_args in steps: + if not hasattr(self, action): + errors.append('Error: Unknown action %s' % action) + if 'url' in action_args.keys(): + url = action_args['url'] + service = ServiceBase(title='dummy', base_url=url) + if not service.is_resolvable(): + errors.append('Error: %s is not resolvable' % url) + if not service.has_valid_certificate(): + errors.append('Error: %s has no valid certificate' % url) + + if not self.permissive: + if len(errors) > 0: + raise CommandError('Please fix the following issues:\n %s' % '\n '.join(errors)) -- 2.11.0