Projet

Général

Profil

0002-cook-add-prechecks-on-recipe-16599.patch

Christophe Siraut, 17 septembre 2018 13:21

Télécharger (4,88 ko)

Voir les différences:

Subject: [PATCH 2/2] cook: add prechecks on recipe (#16599)

 hobo/environment/management/commands/cook.py | 51 +++++++++++++++++++++-------
 1 file changed, 38 insertions(+), 13 deletions(-)
hobo/environment/management/commands/cook.py
35 35
from hobo.multitenant.middleware import TenantMiddleware
36 36
from hobo.environment.models import (AVAILABLE_SERVICES, Authentic, Wcs, Hobo,
37 37
        Passerelle, Combo, Fargo, Welco, MandayeJS, Chrono, Corbo, BiJoe,
38
        Variable)
38
        Variable, ServiceBase)
39 39
from hobo.deploy.signals import notify_agents
40 40
from hobo.theme.utils import set_theme
41 41
from hobo.profile.models import AttributeDefinition
......
53 53
        parser.add_argument(
54 54
                '--timeout', type=int, action='store', default=120,
55 55
                 help='set the timeout for the wait_operationals method')
56
        parser.add_argument('--permissive', action='store_true', help='ignore integrity checks result')
56 57

  
57 58
    def handle(self, recipe, *args, **kwargs):
58 59
        self.verbosity = kwargs.get('verbosity')
59 60
        self.timeout = kwargs.get('timeout')
61
        self.permissive = kwargs.get('permissive')
60 62
        self.run_cook(recipe)
61 63
        if self.verbosity:
62
            print 'All steps executed successfully.  Your environment should now be ready.'
64
            print('All steps executed successfully.  Your environment should now be ready.')
63 65

  
64 66
    def run_cook(self, filename):
65 67
        recipe = json.load(open(filename))
66
        variables = {}
68
        steps = self.get_steps(recipe, os.path.dirname(filename))
69
        self.perform_checks(steps)
70
        for action, action_args in steps:
71
            getattr(self, action)(**action_args)
72
            if self.must_notify:
73
                notify_agents(None)
74
                self.wait_operationals(timeout=self.timeout)
75
                self.must_notify = False
76
        notify_agents(None)
77

  
78
    def get_steps(self, recipe, path):
79
        variables, steps = {}, []
67 80
        if 'load-variables-from' in recipe:
68 81
            variables.update(json.load(open(
69
                os.path.join(os.path.dirname(filename), recipe['load-variables-from']))))
82
                os.path.join(path, recipe['load-variables-from']))))
70 83
        variables.update(recipe.get('variables', {}))
71 84
        for step in recipe.get('steps', []):
72 85
            action, action_args = step.items()[0]
73 86
            for arg in action_args:
74 87
                if not isinstance(action_args[arg], basestring):
75 88
                    continue
76
                action_args[arg] = string.Template(
77
                        action_args[arg]).substitute(variables)
78
            getattr(self, action.replace('-', '_'))(**action_args)
79
            if self.must_notify:
80
                notify_agents(None)
81
                self.wait_operationals(timeout=self.timeout)
82
                self.must_notify = False
83
        notify_agents(None)
89
                action_args[arg] = string.Template(action_args[arg]).substitute(variables)
90
            steps.append((action.replace('-', '_'), action_args))
91
        return steps
84 92

  
85 93
    def wait_operationals(self, timeout):
86 94
        services = []
......
148 156
            user.set_password(password)
149 157
            user.save()
150 158
        if created and self.verbosity:
151
            print 'superuser account: %s / %s' % (username, password)
159
            print('superuser account: %s / %s' % (username, password))
152 160

  
153 161
    def create_site(self, klass, base_url, title, slug, template_name, variables):
154 162
        if slug is None:
......
281 289
        current_tenant = connection.get_tenant()
282 290
        self.run_cook(filename)
283 291
        connection.set_tenant(current_tenant)
292

  
293
    def perform_checks(self, steps):
294
        errors = []
295
        for action, action_args in steps:
296
            if not hasattr(self, action):
297
                errors.append('Error: Unknown action %s' % action)
298
            if 'url' in action_args.keys():
299
                url = action_args['url']
300
                service = ServiceBase(title='dummy', base_url=url)
301
                if not service.is_resolvable():
302
                    errors.append('Error: %s is not resolvable' % url)
303
                if not service.has_valid_certificate():
304
                    errors.append('Error: %s has no valid certificate' % url)
305

  
306
        if not self.permissive:
307
            if len(errors) > 0:
308
                raise CommandError('Please fix the following issues:\n %s' % '\n '.join(errors))
284
-