Пример #1
0
def get_upgrade_status(module, manager_url, mgr_username, mgr_password,
                       validate_certs):
    '''
  Get the current status of upgrade at the start.
  Doesn't upgrade if any component is in progress 
  or system is already upgraded.
  '''
    no_of_checks = 0
    while True:
        upgrade_status = get_attribute_from_endpoint(
            module, manager_url, '/upgrade/status-summary', mgr_username,
            mgr_password, validate_certs, 'overall_upgrade_status', False)
        no_of_checks = no_of_checks + 1
        if upgrade_status == 'IN_PROGRESS' or upgrade_status == 'PAUSING':
            if no_of_checks > 2:
                module.fail_json(
                    msg='Upgrade is in state: %s, can\'t continue' %
                    upgrade_status)
        elif upgrade_status == 'SUCCESS':
            module.exit_json(changed=False,
                             message='Upgrade state is SUCCESS. No need to'
                             ' continue.')
        else:
            return upgrade_status
        time.sleep(20)
Пример #2
0
def check_continuity(module, manager_url, mgr_username, mgr_password,
                     validate_certs):
    '''
  Returns:
  Based on the output of upgrade status summary API, gets the
  checks and returns if upgrade can be continued and
  if there is any component fail in the upgrade
  '''
    try:
        component_status_list = get_attribute_from_endpoint(
            module, manager_url, '/upgrade/status-summary', mgr_username,
            mgr_password, validate_certs, 'component_status', False)
    except Exception as err:
        can_continue = True
        is_failed = True
        return can_continue, is_failed
    try:
        can_continue = True
        for component_status in component_status_list:
            if component_status['status'] == 'IN_PROGRESS' or \
               component_status['status'] == 'PAUSING':
                can_continue = False
                break
        if not can_continue:
            return can_continue, False
        else:
            is_failed = False
            found_not_started = False
            for component_status in component_status_list[::-1]:
                if component_status['status'] == 'NOT_STARTED':
                    found_not_started = True
                elif component_status['status'] == 'PAUSED':
                    can_continue = True
                    is_failed = False
                    return can_continue, is_failed
                elif component_status['status'] == 'SUCCESS':
                    if not found_not_started:
                        can_continue = False
                        is_failed = False
                        return can_continue, is_failed
                    else:
                        can_continue = True
                        is_failed = False
                        return can_continue, is_failed
                elif component_status['status'] == 'FAILED':
                    can_continue = False
                    is_failed = True
                    return can_continue, is_failed
                elif component_status['status'] == 'IN_PROGRESS' or \
                component_status['status'] == 'PAUSING':
                    can_continue = False
                    is_failed = False
                    return can_continue, is_failed
                else:
                    return True, True
    except Exception as err:
        can_continue = True
        is_failed = True
        return can_continue, is_failed
Пример #3
0
def decide_next_step(module, manager_url, mgr_username, mgr_password,
                     validate_certs, can_continue, is_failed):
    '''
  params:
  - can_continue: if upgrade can be continued 
  - is_failed: Is there any component Failure
  return:
  - Decides the next operation to be done based on 
    can_continue and is_failed values 
  '''
    if can_continue and is_failed:
        return
    elif can_continue and not is_failed:
        return
    elif not can_continue and is_failed:
        raise Exception('Upgrade failed. Please run upgrade status summary'
                        ' to see the reason of upgrade failure.')
    else:
        time.sleep(15)
        try:
            upgrade_status = get_attribute_from_endpoint(
                module, manager_url, '/upgrade/summary', mgr_username,
                mgr_password, validate_certs, 'upgrade_status', False)
        except Exception as err:
            return
        if upgrade_status == 'SUCCESS':
            module.exit_json(
                changed=True,
                message='System has been upgraded successfully!!!')
        elif upgrade_status == 'IN_PROGRESS' or upgrade_status == 'PAUSING' or upgrade_status == 'PAUSED':
            return
        else:
            module.fail_json(
                msg=
                'All components till last one are upgraded. Still upgrade status'
                ' is %s. Please run upgrade status summary to see the reason.'
                % upgrade_status)
Пример #4
0
def main():
    argument_spec = vmware_argument_spec()
    argument_spec.update(virtual_ip_address=dict(required=True, type='str'),
                         state=dict(required=True,
                                    choices=['present', 'absent']))

    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)
    virtual_ip_params = get_virtual_ip_params(module.params.copy())
    state = module.params['state']
    mgr_hostname = module.params['hostname']
    mgr_username = module.params['username']
    mgr_password = module.params['password']
    validate_certs = module.params['validate_certs']

    manager_url = 'https://{}/api/v1'.format(mgr_hostname)

    headers = dict(Accept="application/json")
    headers['Content-Type'] = 'application/json'

    if state == 'present':
        # add virtual IP address
        if not virtual_ip_params.__contains__('virtual_ip_address'):
            module.fail_json(msg="Field virtual_ip_address is not provided")
        else:
            virtual_ip_address = virtual_ip_params['virtual_ip_address']
            if not check_if_valid_ip(virtual_ip_address):
                module.fail_json(msg="Virtual IP provided is invalid.")

        if module.check_mode:
            module.exit_json(
                changed=False,
                debug_out="Cluster virtual IP would have been updated to %s" %
                module.params['virtual_ip_address'],
                id=module.params['virtual_ip_address'])
        try:
            (rc, resp) = request(
                manager_url +
                '/cluster/api-virtual-ip?action=set_virtual_ip&ip_address=%s' %
                virtual_ip_address,
                data='',
                headers=headers,
                method='POST',
                url_username=mgr_username,
                url_password=mgr_password,
                validate_certs=validate_certs,
                ignore_errors=True)
        except Exception as err:
            module.fail_json(
                msg="Failed to add virtual IP address. Error[%s]." %
                to_native(err))

        time.sleep(5)
        module.exit_json(
            changed=True,
            result=resp,
            message="Virtual IP address is set with ip address: %s " %
            virtual_ip_address)

    elif state == 'absent':
        # delete virtual IP address
        is_virtual_ip_set = True
        virtual_ip_address = get_attribute_from_endpoint(
            module, manager_url, '/cluster/api-virtual-ip', mgr_username,
            mgr_password, validate_certs, 'ip_address')
        if virtual_ip_address is None or virtual_ip_address == '0.0.0.0':
            virtual_ip_address = "Virtual IP address is not set"
            is_virtual_ip_set = False
        if module.check_mode:
            if not is_virtual_ip_set:
                module.exit_json(changed=True,
                                 debug_out='Virtual IP address is not set',
                                 id=virtual_ip_address)
            else:
                module.exit_json(
                    changed=True,
                    debug_out=
                    'Virtual IP address is set to %s. Will be removed.' %
                    virtual_ip_address,
                    id=virtual_ip_address)
        try:
            (rc,
             resp) = request(manager_url +
                             '/cluster/api-virtual-ip?action=clear_virtual_ip',
                             data='',
                             headers=headers,
                             method='POST',
                             url_username=mgr_username,
                             url_password=mgr_password,
                             validate_certs=validate_certs,
                             ignore_errors=True)
        except Exception as err:
            module.fail_json(
                msg="Failed to clear virtual IP address. Error[%s]." %
                to_native(err))

        time.sleep(5)
        if is_virtual_ip_set:
            module.exit_json(changed=True,
                             object_name=virtual_ip_address,
                             message="Cleared cluster virtual IP address.")
        else:
            module.exit_json(changed=False,
                             object_name="Virtual IP was not set before.",
                             message="Cleared cluster virtual IP address.")