Ejemplo n.º 1
0
def upgrade_vt(target, ret=None, output=True):
    '''Upgrade LXC hosts
    This will reboot all containers upon lxc upgrade
    Containers are marked as being rebooted, and unmarked
    as soon as this script unmark explicitly them to be
    done.
    '''
    func_name = 'mc_cloud_lxc.upgrade_vt {0}'.format(target)
    __salt__['mc_api.time_log']('start {0}'.format(func_name))
    if not ret:
        ret = result()
    ret['comment'] += yellow('Upgrading lxc on {0}\n'.format(target))
    version = cli('cmd.run', 'lxc-info --version', salt_target=target)
    # run the install SLS which should take care of upgrading
    for step in [configure_install_lxc]:
        try:
            step(target, ret=ret, output=False)
        except FailedStepError:
            ret['result'] = False
            ret['comment'] += red('Failed to upgrade lxc\n')
            return ret
    # after upgrading
    nversion = cli('cmd.run', 'lxc-info --version', salt_target=target)
    if nversion != version:
        containers = cli('lxc.list', salt_target=target)
        reg = cli('mc_macros.update_local_registry', 'lxc_to_restart',
                  {'todo': containers.get('running', [])},
                  salt_target=target)
        ret['comment'] += red('Upgraded lxc\n')
    else:
        ret['comment'] += red('lxc was already at the last version\n')
    reg = cli('mc_macros.get_local_registry',
              'lxc_to_restart', salt_target=target)
    todo = reg.get('todo', [])
    done = []
    for lxc in todo:
        try:
            stopret = cli('lxc.stop', lxc, salt_target=target)
            if not stopret['result']:
                raise ValueError('wont stop')
            startret = cli('lxc.start', lxc, salt_target=target)
            if not startret['result']:
                raise ValueError('wont start')
            ret['comment'] += yellow('Rebooted {0}\n'.format(lxc))
            done.append(lxc)
        except Exception, ex:
            ret['result'] = False
            ret['comment'] += yellow(
                'lxc {0} failed to'
                ' reboot: {1}\n'.format(lxc, ex.message))
Ejemplo n.º 2
0
def register_configuration(target, ret=None, output=True):
    '''
    drop the compute node configuration
    '''
    func_name = 'mc_compute_node.register_configuration {0}'.format(target)
    if ret is None:
        ret = result()
    __salt__['mc_api.time_log']('start {0}'.format(func_name))
    settings = __salt__['mc_cloud_compute_node.cn_sls_pillar'](target)
    cret = cli(
        'mc_macros.update_local_registry',
        'cloud_compute_node_settings', settings, registry_format='pack',
        salt_target=target)
    if (
        isinstance(cret, dict)
        and(
            'makina-states.local.'
            'cloud_compute_node_settings.cnSettings' in cret
        )
    ):
        ret['result'] = True
        ret['comment'] += yellow('Configuration stored'
                                 ' on {0}\n'.format(target))
    else:
        ret['result'] = False
        ret['comment'] += red('Configuration failed to store'
                              ' on {0}\n'.format(target))
    salt_output(ret, __opts__, output=output)
    __salt__['mc_api.time_log']('end {0}'.format(func_name))
    return ret
Ejemplo n.º 3
0
def sync_images(target, output=True, ret=None):
    '''sync images on target'''
    func_name = 'mc_cloud_lxc.sync_images {0}'.format(
        target)
    __salt__['mc_api.time_log']('start {0}'.format(func_name))
    if ret is None:
        ret = result()
    iret = __salt__['mc_lxc.sync_images'](only=[target])
    if iret['result']:
        ret['comment'] += yellow(
            'LXC: images synchronnised on {0}\n'.format(target))
    else:
        merge_results(ret, iret)
        ret['comment'] += yellow(
            'LXC: images failed to synchronnise on {0}\n'.format(target))
    salt_output(ret, __opts__, output=output)
    __salt__['mc_api.time_log']('end {0}'.format(func_name))
    return ret
Ejemplo n.º 4
0
def register_configuration(vm,
                           compute_node=None,
                           vt=None,
                           ret=None,
                           output=True,
                           salt_target=None):
    '''
    Register the configuration on the 'salt_target' node as a local registry

    salt_target is aimed to be the vm as default but can be
    any other reachable minion.

    Idea is that we copy this configuration on the compute node at first
    to provision the vm with the rights settings.
    '''
    compute_node = __salt__['mc_cloud_vm.get_compute_node'](vm, compute_node)
    func_name = 'mc_cloud_vm.register_configuration {0}'.format(vm)
    suf = ''
    if not salt_target:
        salt_target = vm
    if salt_target != vm:
        suf = '_{0}'.format(vm)
    if ret is None:
        ret = result()
    __salt__['mc_api.time_log']('start {0}'.format(func_name))
    settings = __salt__['mc_cloud_vm.vm_sls_pillar'](compute_node, vm)
    cret = cli(
        'mc_macros.update_local_registry',
        'cloud_vm_settings{0}'.format(suf),
        settings, registry_format='pack',
        salt_target=salt_target)
    if (
        isinstance(cret, dict)
        and(
            (
                'makina-states.local.'
                'cloud_vm_settings{0}.vmSettings'.format(
                    suf
                ) in cret
            )
        )
    ):
        ret['result'] = True
        ret['comment'] += yellow('VM Configuration stored'
                                 ' on {0}\n'.format(salt_target))
    else:
        ret['result'] = False
        ret['comment'] += red('VM Configuration failed to store'
                              ' on {0}\n'.format(salt_target))
    salt_output(ret, __opts__, output=output)
    __salt__['mc_api.time_log']('end {0}'.format(func_name))
    return ret
Ejemplo n.º 5
0
def post_deploy_controller(output=True):
    '''Prepare cloud controller LXC configuration'''
    func_name = 'mc_cloud_lxc.post_deploy_controller'
    __salt__['mc_api.time_log']('start {0}'.format(func_name))
    ret = result()
    ret['comment'] = yellow('Installing controller lxc configuration\n')
    pref = 'makina-states.cloud.lxc.controller'
    ret = __salt__['mc_api.apply_sls'](
        ['{0}.postdeploy'.format(pref)],
        **{'ret': ret})
    salt_output(ret, __opts__, output=output)
    __salt__['mc_api.time_log']('end {0}'.format(func_name))
    return ret
Ejemplo n.º 6
0
def _configure(what, target, ret, output):
    __salt__['mc_cloud_compute_node.lazy_register_configuration'](target)
    func_name = 'mc_compute_node._configure {0} {1}'.format(what, target)
    __salt__['mc_api.time_log']('start {0}'.format(func_name))
    if ret is None:
        ret = result()
    ret['comment'] += yellow('Installing {1} on {0}\n'.format(target, what))
    ret =  __salt__['mc_api.apply_sls'](
        '{0}.{1}'.format(_GPREF, what), **{
            'salt_target': target, 'ret': ret})
    salt_output(ret, __opts__, output=output)
    __salt__['mc_api.time_log']('end {0}'.format(func_name))
    return ret
Ejemplo n.º 7
0
def install_vts(target, ret=None, output=True):
    '''install all virtual types to be ready to host vms'''
    func_name = 'mc_compute_node.install_vts {0}'.format(target)
    __salt__['mc_api.time_log']('start {0}'.format(func_name))
    if ret is None:
        ret = result()
    ret = run_vt_hook('install_vt',
                      ret=ret, target=target, output=output)
    if ret['result']:
        ret['comment'] += yellow(
            '{0} is now ready to host vms\n'.format(target))
    salt_output(ret, __opts__, output=output)
    __salt__['mc_api.time_log']('end {0}'.format(func_name))
    return ret
Ejemplo n.º 8
0
def orchestrate(output=True, only=None, skip=None, ret=None, refresh=False):
    '''Parse saltify settings to saltify all targets

        output
            display output
        only
            specify explicitly which hosts to provision among all
            avalaible ones
        skip
            hosts to skip
        refresh
            refresh pillar
    '''
    if skip is None:
        skip = []
    if only is None:
        only = []
    if ret is None:
        ret = result()
    if refresh:
        cli('saltutil.refresh_pillar')
    comment = ''
    settings = cli('mc_cloud_saltify.settings')
    saltified = ret['changes'].setdefault('saltified', [])
    saltified_error = ret['changes'].setdefault('saltified_errors', [])
    targets = [a for a in settings['targets']]
    targets = filter_compute_nodes(targets, skip, only)
    targets.sort()
    for idx, compute_node in enumerate(targets):
        try:
            cret = saltify(compute_node, output=False)
            if cret['result']:
                saltified.append(compute_node)
            else:
                raise SaltyficationError(
                    'Target {0} failed to saltify:\n{1}'.format(
                        compute_node, cret['comment']))
        except Exception, exc:
            trace = traceback.format_exc()
            comment += yellow(
                '\nSaltyfication failed for {0}: {1}'.format(
                    compute_node, exc))
            if not isinstance(exc, SaltyficationError):
                ret['trace'] += '\n'.format(trace)
            log.error(trace)
            saltified_error.append(compute_node)
Ejemplo n.º 9
0
def _vm_configure(what, target, compute_node, vm, ret, output):
    __salt__['mc_cloud_vm.lazy_register_configuration'](vm, compute_node)
    func_name = 'mc_cloud_lxc._vm_configure {0} {1} {2} {3}'.format(
        what, target, compute_node, vm)
    __salt__['mc_api.time_log']('start {0}'.format(func_name))
    if ret is None:
        ret = result()
    ret['comment'] += yellow(
        'LXC: Installing {2} on vm '
        '{0}/{1}\n'.format(compute_node, vm, what))
    pref = 'makina-states.cloud.lxc.vm'
    ret =  __salt__['mc_api.apply_sls'](
        '{0}.{1}'.format(pref, what), **{
            'salt_target': target,
            'ret': ret})
    salt_output(ret, __opts__, output=output)
    __salt__['mc_api.time_log']('end {0}'.format(func_name))
    return ret
Ejemplo n.º 10
0
def install_vt(target, output=True):
    '''install & configure lxc'''
    func_name = 'mc_cloud_lxc.install_vt {0}'.format(
        target)
    __salt__['mc_api.time_log']('start {0}'.format(func_name))
    ret = result()
    ret['comment'] += yellow('Installing lxc on {0}\n'.format(target))
    for step in [configure_grains,
                 configure_install_lxc,
                 configure_images]:
        try:
            step(target, ret=ret, output=False)
        except FailedStepError:
            pass
    __salt__['mc_cloud_lxc.sync_images'](target, output=False, ret=ret)
    salt_output(ret, __opts__, output=output)
    __salt__['mc_api.time_log']('end {0}'.format(func_name))
    return ret
Ejemplo n.º 11
0
def apply_sls_(func, slss,
               salt_output_t='highstate',
               status_msg=None,
               sls_status_msg=None,
               ret=None,
               output=False,
               *a, **kwargs):
    local_target = __grains__.get('id', __opts__.get('id', 'local'))
    target = salt_target = kwargs.pop('salt_target', None)
    if target is None:
        target = local_target
    if ret is None:
        ret = result()
    if isinstance(slss, basestring):
        slss = [slss]
    sls_kw = kwargs.pop('sls_kw', {})
    statuses = []
    salt_ok, salt_ko = 'success', 'failed'
    for sls in slss:
        cret = None
        try:
            cliret = cli(func, sls, salt_target=salt_target, *a, **sls_kw)
            cret = filter_state_return(cliret, target=target,
                                       output=salt_output_t)
            valid_state_return(cliret, sls=sls)
            ret['output'] += cret
            statuses.append((salt_ok, sls))
        except SaltExit, exc:
            trace = traceback.format_exc()
            ret['result'] = False
            ret['output'] += '- {0}\n{1}\n'.format(yellow(sls), exc)
            ret['trace'] += '{0}\n'.format(trace)
            statuses.append((salt_ko, sls))
            if cret:
                ret['trace'] += '{0}\n'.format(pformat(cret))
                ret['output'] += '{0}\n'.format(cret)
Ejemplo n.º 12
0
            ret['output'] += cret
            statuses.append((salt_ok, sls))
        except SaltExit, exc:
            trace = traceback.format_exc()
            ret['result'] = False
            ret['output'] += '- {0}\n{1}\n'.format(yellow(sls), exc)
            ret['trace'] += '{0}\n'.format(trace)
            statuses.append((salt_ko, sls))
            if cret:
                ret['trace'] += '{0}\n'.format(pformat(cret))
                ret['output'] += '{0}\n'.format(cret)
    clr = ret['result'] and green or red
    status = ret['result'] and salt_ok or salt_ko
    if not status_msg:
        if target:
            status_msg = yellow('  Installation on {0}: ') + clr('{1}\n')
        else:
            status_msg = yellow('  Installation: ') + clr('{1}\n')
    ret['comment'] += status_msg.format(target, status)
    sls_status_msg = yellow('   - {0}:') + ' {2}\n'
    if len(statuses) > 1:
        for status, sls in statuses:
            if status == salt_ko:
                sclr = red
            else:
                sclr = green
            status = sclr(status)
            ret['comment'] += sls_status_msg.format(sls, target, status)
    return ret

Ejemplo n.º 13
0
def provision_compute_nodes(skip=None, only=None,
                            no_compute_node_provision=False,
                            output=True,
                            refresh=True,
                            ret=None):
    '''provision compute nodes

        skip
            list or comma separated string of compute node
            to skip (will skip contained vms too)
        only
            list or comma separated string of compute node
            If set, it will only provision those compute nodes
            and contained vms

    '''
    func_name = 'mc_compute_node.provision_compute_nodes'
    __salt__['mc_api.time_log']('start {0}'.format(func_name))
    only, _, skip, __ = (
        __salt__['mc_cloud_controller.gather_only_skip'](
            only=only, skip=skip))
    if ret is None:
        ret = result()
    if refresh:
        cli('saltutil.refresh_pillar')
    settings = cli('mc_cloud_compute_node.settings')
    provision = ret['changes'].setdefault('cns_provisionned', [])
    provision_error = ret['changes'].setdefault('cns_in_error', [])
    targets = [a for a in settings['targets']]
    #targets += ['foo', 'bar']
    targets = filter_compute_nodes(targets, skip, only)
    for idx, compute_node in enumerate(targets):
        cret = result()
        if no_compute_node_provision:
            cret['comment'] = yellow(
                'Compute node configuration skipped for {0}\n'
            ).format(compute_node)
        else:
            try:
                deploy(compute_node, ret=cret, output=False)
                #if idx == 1:
                #    raise FailedStepError('foo')
                #elif idx > 0:
                #    raise Exception('bar')
            except FailedStepError:
                cret['result'] = False
            except Exception, exc:
                trace = traceback.format_exc()
                cret = {'result': False,
                        'output': 'unknown error on {0}\n{1}'.format(compute_node,
                                                                     exc),
                        'comment': 'unknown error on {0}\n'.format(compute_node),
                        'trace': trace}
        if cret['result']:
            if compute_node not in provision:
                provision.append(compute_node)
            # if everything is well, wipe the unseful output
            cret['output'] = ''
            cret['trace'] = ''
        else:
            ret['result'] = False
            if compute_node not in provision_error:
                provision_error.append(compute_node)
        cret.pop('result', False)
        merge_results(ret, cret)