Ejemplo n.º 1
0
def basic_local_ipmi_runner(SCRIPT_TEMPLATE, stdout=False, stderr=False, regex=False):
    """
    :Return Code Description:

        0: PING SUCCESS(SUCCESS)
        1: PING FAIL(TIMEOUT)

        0: SSH SUCCESS(SUCCESS)
        1: SSH FAIL(TIMEOUT, RESET, NO_ROUTE)
        2: SSH AUTHENTICATE FAIL(验证错误, 密钥格式错误, 密钥无法找到)
        3: COMMAND EXECUTE TIMEOUT(脚本执行超时)
        4: COMMAND EXECUTE FAIL(脚本中途失败)

        10: NETWORK ERROR(ADDRESS ERROR)

        20: OTHER ERROR
        100: DEFAULT

        SSH认证是先看private_key,后看password.

    """

    ipmi_address = generate_ipmi_address(env.host)

    template = Template(SCRIPT_TEMPLATE)
    script = template.render(dict(ipmi_address=ipmi_address))

    # TODO: 统计其它异常情况

    try:
        data = local(script, capture=True)
    except Exception, e:
        output = dict(code=20,
                      error_message='Base Exception: %s' % e if stderr else '',
                      message='')
Ejemplo n.º 2
0
def remote_control(operation, config):
    """
    :Return:

        0: 执行中
        1: 已完成
        2: 内部错误

    """

    ID = operation.get('OPT_ID', 0)
    API_URL = '%s/operation/%s' % (config.get('SETTINGS_API_BASIC_URL',
                                              None), ID)

    ipmi_address_list = list()
    for address in operation.get('OPT_SERVER_LIST', '').split():
        ipmi_address_list.append(generate_ipmi_address(address))

    specifies = '-I lanplus' if operation.get('IPMI_INTERFACE', False) else ''
    COMMAND = 'ipmitool %s -H {{ ipmi_address }} -U %s -P %s chassis power %s' % \
              (specifies, operation.get('IPMI_USERNAME', 'root'),
               operation.get('IPMI_PASSWORD', 'calvin'), operation.get('OPT_SCRIPT_TEMPLATE', 'status'))

    if API_URL is not None:

        with hide('everything'):

            result = execute(basic_local_ipmi_runner,
                             COMMAND,
                             stdout=True,
                             stderr=True,
                             hosts=operation.get('OPT_SERVER_LIST',
                                                 '').split())

        data = json.dumps(dict(id=ID, status=1, result=result),
                          ensure_ascii=False)

        response = requests.put(API_URL,
                                data=data,
                                headers={'content-type': 'application/json'})

        if response.status_code != requests.codes.ok:
            message = response.json.get('message', 'unknown errors')
            logger.error(
                u'UPDATE OPERATION FAILS| Operation ID is %s, Message is %s' %
                (ID, message))

    else:
        logger.error(u'CONFIG FAILS|Message is can\'t get API url from config')
Ejemplo n.º 3
0
def remote_control(operation, config):
    """
    :Return:

        0: 执行中
        1: 已完成
        2: 内部错误

    """

    ID = operation.get('OPT_ID', 0)
    API_URL = '%s/operation/%s' % (config.get('SETTINGS_API_BASIC_URL', None), ID)

    ipmi_address_list = list()
    for address in operation.get('OPT_SERVER_LIST', '').split():
        ipmi_address_list.append(generate_ipmi_address(address))

    specifies = '-I lanplus' if operation.get('IPMI_INTERFACE', False) else ''
    COMMAND = 'ipmitool %s -H {{ ipmi_address }} -U %s -P %s chassis power %s' % \
              (specifies, operation.get('IPMI_USERNAME', 'root'),
               operation.get('IPMI_PASSWORD', 'calvin'), operation.get('OPT_SCRIPT_TEMPLATE', 'status'))

    if API_URL is not None:

        with hide('everything'):

            result = execute(basic_local_ipmi_runner, COMMAND,
                             stdout=True, stderr=True,
                             hosts=operation.get('OPT_SERVER_LIST', '').split())

        data = json.dumps(dict(id=ID, status=1, result=result),  ensure_ascii=False)

        response = requests.put(API_URL, data=data, headers={'content-type': 'application/json'})

        if response.status_code != requests.codes.ok:
            message = response.json.get('message', 'unknown errors')
            logger.error(u'UPDATE OPERATION FAILS| Operation ID is %s, Message is %s' % (ID, message))

    else:
        logger.error(u'CONFIG FAILS|Message is can\'t get API url from config')