Beispiel #1
0
def run_workflow(tenant, deployment, workflow_id, parameters,
                 allow_custom_parameters, force, logger):
    logger.info(
        'Running workflow {workflow} on deployment {deployment} for tenant '
        '{tenant}'.format(
            workflow=workflow_id,
            deployment=deployment,
            tenant=tenant,
        ))
    request_data = json.dumps({
        'deployment_id': deployment,
        'workflow_id': workflow_id,
        'parameters': parameters,
        'allow_custom_parameters': allow_custom_parameters,
        'force': force,
        'dry_run': False,
    })
    logger.debug('Workflow details: {details}'.format(details=request_data, ))

    try:
        result = make_request(
            path='/api/v3.1/executions',
            tenant=tenant,
            request_data=request_data,
            request_parameters=None,
            method=requests.post,
            logger=logger,
        )
    except ManagerRequestFailed as err:
        logger.error('Starting workflow failed: {error}'.format(
            error=text_type(err), ))
        raise StartWorkflowFailed(text_type(err))

    logger.debug('Retrieving ID from result {result}'.format(result=result, ))
    return tenant, result['id']
Beispiel #2
0
    def get_actions_list(self):

        actions = []
        actions_confirmation = {}

        for act in self._actions:
            name, text = act
            
            if self.is_action_allowed(name):
                actions.append((name, text_type(text)))
                
                confirmation = self._actions_data[name][2]
                if confirmation:
                    actions_confirmation[name] = text_type(confirmation)
        return actions, actions_confirmation
Beispiel #3
0
def soft_unicode(s):
    """Make a string unicode if it isn't already.  That way a markup
    string is not converted back to unicode.
    """
    if not isinstance(s, text_type):
        s = text_type(s)
    return s
Beispiel #4
0
def soft_unicode(s):
    """Make a string unicode if it isn't already.  That way a markup
    string is not converted back to unicode.
    """
    if not isinstance(s, text_type):
        s = text_type(s)
    return s
Beispiel #5
0
def remove_configuration_file(logger,
                              configuration_path,
                              reload_service=True,
                              sudo=False,
                              ignore_missing=False):
    conf_file_name = os.path.split(configuration_path)[-1]
    configuration_path = os.path.join(BASE_OBJECTS_DIR, configuration_path)

    tmpdir = tempfile.mkdtemp()
    temp_location = os.path.join(tmpdir, conf_file_name)

    validate = True
    try:
        run(['mv', configuration_path, temp_location], sudo=sudo)
    except subprocess.CalledProcessError as err:
        if 'No such file or directory' in text_type(err) and ignore_missing:
            validate = False

    if validate:
        validate_configuration(
            logger,
            rollback=['mv', temp_location, configuration_path],
            sudo=sudo,
        )

    # If the configuration is still healthy, we can finalise the deletion
    run(['rm', '-rf', tmpdir], sudo=sudo)

    if reload_service:
        trigger_nagios_reload(set_group=sudo)
Beispiel #6
0
def escape(s):
    """Convert the characters &, <, >, ' and " in string s to HTML-safe
    sequences.  Use this if you need to display text that might contain
    such characters in HTML.  Marks return value as markup string.
    """
    if hasattr(s, '__html__'):
        return s.__html__()
    return Markup(
        text_type(s).replace('&', '&amp;').replace('>', '&gt;').replace(
            '<', '&lt;').replace("'", '&#39;').replace('"', '&#34;'))
Beispiel #7
0
def get_execution(tenant, execution_id, logger):
    logger.debug('Retrieving execution {execution} for tenant {tenant}'.format(
        execution=execution_id,
        tenant=tenant,
    ))
    try:
        return make_request(
            path='/api/v3.1/executions/{execution_id}'.format(
                execution_id=execution_id, ),
            tenant=tenant,
            request_data=None,
            request_parameters=None,
            method=requests.get,
            logger=logger,
        )
    except ManagerRequestFailed as err:
        logger.error('Failed to retrieve execution: {error}'.format(
            error=text_type(err), ))
        raise GetExecutionError(text_type(err))
Beispiel #8
0
class _MarkupEscapeHelper(object):
    """Helper for Markup.__mod__"""
    def __init__(self, obj, escape):
        self.obj = obj
        self.escape = escape

    __getitem__ = lambda s, x: _MarkupEscapeHelper(s.obj[x], s.escape)
    __unicode__ = __str__ = lambda s: text_type(s.escape(s.obj))
    __repr__ = lambda s: str(s.escape(repr(s.obj)))
    __int__ = lambda s: int(s.obj)
    __float__ = lambda s: float(s.obj)
Beispiel #9
0
    def non_lazy(self):
        filters = []

        for item in self.filters:
            copy = dict(item)
            copy['operation'] = as_unicode(copy['operation'])
            options = copy['options']
            if options:
                copy['options'] =[(k, text_type(v)) for k,v in options]
            filters.append(copy)
        return as_unicode(self.label), filters
Beispiel #10
0
 def format_field(self, value, format_spec):
     if hasattr(value, '__html_format__'):
         rv = value.__html_format__(format_spec)
     elif hasattr(value, '__html__'):
         if format_spec:
             raise ValueError('No format specification allowed '
                              'when formatting an object with '
                              'its __html__ method.')
         rv = value.__html__()
     else:
         rv = string.Formatter.format_field(self, value, format_spec)
     return text_type(self.escape(rv))
Beispiel #11
0
def trigger_nagios_reload(set_group=False):
    # We have the trigger file to avoid reloading too quickly when there are a
    # large amount of changes being made at once, as this can upset nagios.
    reload_trigger_file = '/tmp/nagios_reload_triggered'
    current_time = time.time()
    try:
        with open(reload_trigger_file) as trigger_handle:
            reload_time = float(trigger_handle.read().strip())
    except IOError:
        # The file doesn't exist, that's fine- we can reload nagios then!
        pass
    else:
        # In case the file is lingering from a killed process or similar we
        # check that the reload time is actually in the future
        delay_until_reload = reload_time - current_time
        if delay_until_reload >= 0:
            time.sleep(delay_until_reload + 1)
            # At this point, the other process should've restarted nagios
            # and cleaned up after itself. If it didn't, we'll assume it
            # died.
            if not os.path.exists(reload_trigger_file):
                # The other process cleaned up after itself so it is likely
                # that it also restarted nagios
                return
    # If we reach here then either something else wasn't already restarting
    # nagios, or something else was intending to but didn't
    delay = 15
    if os.path.exists(reload_trigger_file) and set_group:
        # We may not be able to open it if nagiosrest created it but died
        # before removing it
        run(['rm', reload_trigger_file], sudo=True)
    with open(reload_trigger_file, 'w') as trigger_handle:
        trigger_handle.write(text_type(current_time + delay))
    if set_group:
        # Allow nagios rest to delete this file
        run(['chgrp', 'nagios', reload_trigger_file], sudo=True)
        run(['chmod', '660', reload_trigger_file])
    time.sleep(delay)
    try:
        # try reload nagios service
        run(['systemctl', 'reload', 'nagios'], sudo=True)
    except subprocess.CalledProcessError:
        # this means reload failed so we try restart for 3 times
        for _ in range(3):
            try:
                run(['systemctl', 'restart', 'nagios'], sudo=True)
                # if it succeed no need for other trials
                break
            except subprocess.CalledProcessError:
                time.sleep(delay)
    # If we had to set the group then we may also not own the file
    run(['rm', reload_trigger_file], sudo=set_group)
Beispiel #12
0
def escape(s):
    """Convert the characters &, <, >, ' and " in string s to HTML-safe
    sequences.  Use this if you need to display text that might contain
    such characters in HTML.  Marks return value as markup string.
    """
    if hasattr(s, '__html__'):
        return s.__html__()
    return Markup(text_type(s)
        .replace('&', '&amp;')
        .replace('>', '&gt;')
        .replace('<', '&lt;')
        .replace("'", '&#39;')
        .replace('"', '&#34;')
    )
Beispiel #13
0
    def unescape(self):
        r"""Unescape markup again into an text_type string.  This also resolves
        known HTML4 and XHTML entities:

        >>> Markup("Main &raquo; <em>About</em>").unescape()
        u'Main \xbb <em>About</em>'
        """
        from markupsafe._constants import HTML_ENTITIES
        def handle_match(m):
            name = m.group(1)
            if name in HTML_ENTITIES:
                return unichr(HTML_ENTITIES[name])
            try:
                if name[:2] in ('#x', '#X'):
                    return unichr(int(name[2:], 16))
                elif name.startswith('#'):
                    return unichr(int(name[1:]))
            except ValueError:
                pass
            return u''
        return _entity_re.sub(handle_match, text_type(self))
Beispiel #14
0
    def unescape(self):
        r"""Unescape markup again into an text_type string.  This also resolves
        known HTML4 and XHTML entities:

        >>> Markup("Main &raquo; <em>About</em>").unescape()
        u'Main \xbb <em>About</em>'
        """
        from _constants import HTML_ENTITIES
        def handle_match(m):
            name = m.group(1)
            if name in HTML_ENTITIES:
                return unichr(HTML_ENTITIES[name])
            try:
                if name[:2] in ('#x', '#X'):
                    return unichr(int(name[2:], 16))
                elif name.startswith('#'):
                    return unichr(int(name[1:]))
            except ValueError:
                pass
            return u''
        return _entity_re.sub(handle_match, text_type(self))
Beispiel #15
0
        def get_kwargs(self, field, kwargs):

            if field.type == 'StringField':
                kwargs['data-type'] == 'text'
            elif field.type == 'TextAreaField':
                kwargs['data-type'] = 'textarea'
                kwargs['data-rows'] = '5'
            elif field.type == 'BooleanField':
                kwargs['data-type'] = 'select2'
                kwargs['data-value'] = '1' if field.data else ''
                kwargs['data-source'] = json.dumps([{
                    'value': '',
                    'text': 'No'
                }, {
                    'value': '1',
                    'text': 'Yes'
                }])
                kwargs['data-role'] = 'x-editable-boolean'
            elif field.type in ['Select2Field', 'SelectField']:
                kwargs['data-type'] = 'select2'
                choices = [{'value': x, 'text': y} for x, y in field.choices]
                if getattr(field, 'allow_blank', False):
                    choices.insert(0, {'value': '__None', 'text': ''})
                kwargs['data-source'] = json.dumps(choices)
            elif field.type == 'DataField':
                kwargs['data-type'] = 'combodate'
                kwargs['data-format'] = 'YYYY-MM-DD'
                kwargs['data-template'] = 'YYYY-MM-DD'
            elif field.type == 'DateTimeField':
                kwargs['data-type'] = 'combodate'
                kwargs['data-format'] = 'YYYY-MM-DD HH:mm:ss'
                kwargs['data-template'] = 'YYYY-MM-DD  HH:mm:ss'
                # x-editable-combodate uses 1 minute increments
                kwargs['data-role'] = 'x-editable-combodate'
            elif field.type == 'TimeField':
                kwargs['data-type'] = 'combodate'
                kwargs['data-format'] = 'HH:mm:ss'
                kwargs['data-template'] = 'HH:mm:ss'
                kwargs['data-role'] = 'x-editable-combodate'
            elif field.type == 'IntegerField':
                kwargs['data-type'] = 'number'
            elif field.type in ['FloatField', 'DecimalField']:
                kwargs['data-type'] = 'number'
                kwargs['data-step'] = 'any'
            elif field.type in [
                    'QuerySelectField', 'ModelSelectField',
                    'QuerySelectMultipleField', 'KeyPropertyField'
            ]:
                kwargs['data-type'] = 'select2'
                choices = []
                selected_ids = []
                for value, label, selected in field.iter_choices():
                    try:
                        label = text_type(label)
                    except TypeError:
                        label = ''
                    choices.append({'value': text_type(value), 'text': label})
                    if selected:
                        selected_ids.append(value)
                kwargs['data-source'] = json.dumps(choices)

                if field.type == 'QuerySelectMultipleField':
                    kwargs['data-role'] = 'x-editable-select2-multiple'
                    separator = getattr(field, 'separator', ',')
                    kwargs['data-value'] = separator.join(selected_ids)
                else:
                    kwargs['data-value'] = text_type(selected_ids[0])
            else:
                raise Exception('Unsupported field type: %s' % (type(field), ))
            return kwargs
Beispiel #16
0
def list_formatter(view, values):
    return u', '.join(text_type(v) for v in values)
Beispiel #17
0
def create_meta_group(logger, group_instance_prefix, group_type, tenant,
                      approach, unknown, check_interval, low_warning_threshold,
                      low_critical_threshold, high_warning_threshold,
                      high_critical_threshold, reaction_target, low_reaction,
                      high_reaction):
    logger.info('Creating meta group for prefix {prefix} '
                'for group {group_type}'.format(
                    prefix=group_instance_prefix,
                    group_type=group_type,
                ))

    logger.debug('Creating reaction configuration')
    reaction_conf = {'reactions': {}}
    if low_reaction:
        reaction_conf['reactions']['low'] = {
            'workflow': low_reaction,
        }
    if high_reaction:
        reaction_conf['reactions']['high'] = {
            'workflow': high_reaction,
        }
    reaction_conf_path = get_meta_group_reaction_configuration_path(
        group_type=group_type,
        group_instance_prefix=group_instance_prefix,
        tenant=tenant,
    )
    make_config_subdir(os.path.dirname(reaction_conf_path))
    deploy_file(
        data=json.dumps(reaction_conf),
        destination=reaction_conf_path,
        sudo=False,
    )

    logger.debug('Setting reaction target')
    reaction_target_path = get_meta_group_reaction_target_path(
        group_type=group_type,
        group_instance_prefix=group_instance_prefix,
        tenant=tenant,
    )
    make_config_subdir(os.path.dirname(reaction_target_path))
    deploy_file(
        data=reaction_target,
        destination=reaction_target_path,
        sudo=False,
    )

    logger.debug('Deploying meta group check configuration')
    meta_group_config_destination = get_meta_group_configuration_destination(
        group_type,
        group_instance_prefix,
        tenant,
    )
    make_config_subdir(
        os.path.dirname(
            os.path.join(
                BASE_OBJECTS_DIR,
                meta_group_config_destination,
            )))
    check_config = {
        'group_type': group_type,
        'group_instance_prefix': group_instance_prefix,
        'tenant': tenant,
        'unknown': unknown,
        'approach': approach,
        'low_warning_threshold': low_warning_threshold,
        'low_critical_threshold': low_critical_threshold,
        'high_warning_threshold': high_warning_threshold,
        'high_critical_threshold': high_critical_threshold,
        'check_interval': check_interval,
    }
    logger.debug('Full check configuration: {conf}'.format(
        conf=text_type(check_config)))
    deploy_configuration_file(
        logger,
        source='meta_group_check.template',
        destination=meta_group_config_destination,
        template_params=check_config,
        reload_service=True,
        use_pkg_data=False,
    )
Beispiel #18
0
def create_group_instance(logger, group_name, group_type, tenant,
                          reaction_target):
    logger.info('Creating instance {name} of {group_type}'.format(
        name=group_name,
        group_type=group_type,
    ))

    logger.debug('Loading group check configuration')
    with open(get_group_config_location(group_type)) as config_handle:
        group_config = json.load(config_handle)
    check_config = group_config['check_configuration']
    logger.debug('Check configuration loaded: {conf}'.format(
        conf=text_type(check_config)))

    logger.debug('Setting reaction target to {target}'.format(
        target=reaction_target, ))
    reaction_target_path = get_group_check_reaction_target_path(
        group_type, group_name, tenant)
    make_config_subdir(os.path.dirname(reaction_target_path))
    deploy_file(
        data=reaction_target,
        destination=reaction_target_path,
        sudo=False,
    )

    logger.info('Creating supporting hostgroups')
    configure_tenant_group(logger, tenant)

    logger.debug('Deploying group tenant configuration')
    group_config_destination = get_group_host_configuration_destination(
        group_type,
        tenant,
    )
    make_config_subdir(
        os.path.dirname(
            os.path.join(
                BASE_OBJECTS_DIR,
                group_config_destination,
            )))
    deploy_configuration_file(
        logger,
        source='group.template',
        destination=group_config_destination,
        template_params={
            'group_type': group_type,
            'tenant': tenant,
        },
        reload_service=False,
        use_pkg_data=False,
    )

    logger.debug('Deploying group instance configuration')
    instance_config_destination = get_group_check_configuration_destination(
        group_type,
        group_name,
        tenant,
    )
    make_config_subdir(
        os.path.dirname(
            os.path.join(
                BASE_OBJECTS_DIR,
                instance_config_destination,
            )))
    check_config.update({
        'group_type': group_type,
        'group_name': group_name,
        'tenant': tenant,
    })
    logger.debug('Full check configuration: {conf}'.format(
        conf=text_type(check_config)))
    deploy_configuration_file(
        logger,
        source='group_check.template',
        destination=instance_config_destination,
        template_params=check_config,
        reload_service=True,
        use_pkg_data=False,
    )