コード例 #1
0
def _attach_monitoring_policy_server(module, oneandone_conn, monitoring_policy_id, servers):
    """
    Attaches servers to a monitoring policy.
    """
    try:
        attach_servers = []

        for _server_id in servers:
            server_id = get_server(oneandone_conn, _server_id)
            attach_server = oneandone.client.AttachServer(
                server_id=server_id
            )
            attach_servers.append(attach_server)

        if module.check_mode:
            if attach_servers:
                return True
            return False

        monitoring_policy = oneandone_conn.attach_monitoring_policy_server(
            monitoring_policy_id=monitoring_policy_id,
            servers=attach_servers)
        return monitoring_policy
    except Exception as ex:
        module.fail_json(msg=str(ex))
コード例 #2
0
def _add_server_ips(module, oneandone_conn, load_balancer_id, server_ids):
    """
    Assigns servers to a load balancer.
    """
    try:
        attach_servers = []

        for server_id in server_ids:
            server = get_server(oneandone_conn, server_id, True)
            attach_server = oneandone.client.AttachServer(
                server_id=server['id'],
                server_ip_id=next(iter(server['ips'] or []), None)['id']
            )
            attach_servers.append(attach_server)

        if module.check_mode:
            if attach_servers:
                return True
            return False

        load_balancer = oneandone_conn.attach_load_balancer_server(
            load_balancer_id=load_balancer_id,
            server_ips=attach_servers)
        return load_balancer
    except Exception as ex:
        module.fail_json(msg=str(ex))
コード例 #3
0
def _add_server_ips(module, oneandone_conn, firewall_id, server_ids):
    """
    Assigns servers to a firewall policy.
    """
    try:
        attach_servers = []

        for _server_id in server_ids:
            server = get_server(oneandone_conn, _server_id, True)
            attach_server = oneandone.client.AttachServer(
                server_id=server['id'],
                server_ip_id=next(iter(server['ips'] or []), None)['id']
            )
            attach_servers.append(attach_server)

        if module.check_mode:
            if attach_servers:
                return True
            return False

        firewall_policy = oneandone_conn.attach_server_firewall_policy(
            firewall_id=firewall_id,
            server_ips=attach_servers)
        return firewall_policy
    except Exception as e:
        module.fail_json(msg=str(e))
コード例 #4
0
def _create_server(module, oneandone_conn, hostname, description,
                   fixed_instance_size_id, vcore, cores_per_processor, ram,
                   hdds, datacenter_id, appliance_id, ssh_key,
                   private_network_id, firewall_policy_id, load_balancer_id,
                   monitoring_policy_id, server_type, wait, wait_timeout,
                   wait_interval):

    try:
        existing_server = get_server(oneandone_conn, hostname)

        if existing_server:
            if module.check_mode:
                return False
            return None

        if module.check_mode:
            return True

        server = oneandone_conn.create_server(
            oneandone.client.Server(
                name=hostname,
                description=description,
                fixed_instance_size_id=fixed_instance_size_id,
                vcore=vcore,
                cores_per_processor=cores_per_processor,
                ram=ram,
                appliance_id=appliance_id,
                datacenter_id=datacenter_id,
                rsa_key=ssh_key,
                private_network_id=private_network_id,
                firewall_policy_id=firewall_policy_id,
                load_balancer_id=load_balancer_id,
                monitoring_policy_id=monitoring_policy_id,
                server_type=server_type,), hdds)

        if wait:
            wait_for_resource_creation_completion(
                oneandone_conn,
                OneAndOneResources.server,
                server['id'],
                wait_timeout,
                wait_interval)
            server = oneandone_conn.get_server(server['id'])  # refresh

        return server
    except Exception as ex:
        module.fail_json(msg=str(ex))
コード例 #5
0
def remove_server(module, oneandone_conn):
    """
    Removes a server.

    module : AnsibleModule object
    oneandone_conn: authenticated oneandone object.

    Returns a dictionary containing a 'changed' attribute indicating whether
    the server was removed, and a 'removed_server' attribute with
    the removed server's hostname and id.
    """
    server_id = module.params.get('server')
    wait = module.params.get('wait')
    wait_timeout = module.params.get('wait_timeout')
    wait_interval = module.params.get('wait_interval')

    changed = False
    removed_server = None

    server = get_server(oneandone_conn, server_id, True)
    if server:
        _check_mode(module, True)
        try:
            oneandone_conn.delete_server(server_id=server['id'])
            if wait:
                wait_for_resource_deletion_completion(oneandone_conn,
                                                      OneAndOneResources.server,
                                                      server['id'],
                                                      wait_timeout,
                                                      wait_interval)
            changed = True
        except Exception as ex:
            module.fail_json(
                msg="failed to terminate the server: %s" % str(ex))

        removed_server = {
            'id': server['id'],
            'hostname': server['name']
        }
    _check_mode(module, False)

    return (changed, removed_server)
コード例 #6
0
def update_network(module, oneandone_conn):
    """
    Modifies a private network.

    module : AnsibleModule object
    oneandone_conn: authenticated oneandone object
    """
    try:
        _private_network_id = module.params.get('private_network')
        _name = module.params.get('name')
        _description = module.params.get('description')
        _network_address = module.params.get('network_address')
        _subnet_mask = module.params.get('subnet_mask')
        _add_members = module.params.get('add_members')
        _remove_members = module.params.get('remove_members')

        changed = False

        private_network = get_private_network(oneandone_conn,
                                              _private_network_id, True)
        if private_network is None:
            _check_mode(module, False)

        if _name or _description or _network_address or _subnet_mask:
            _check_mode(module, True)
            private_network = oneandone_conn.modify_private_network(
                private_network_id=private_network['id'],
                name=_name,
                description=_description,
                network_address=_network_address,
                subnet_mask=_subnet_mask)
            changed = True

        if _add_members:
            instances = []

            for member in _add_members:
                instance_id = get_server(oneandone_conn, member)
                instance_obj = oneandone.client.AttachServer(
                    server_id=instance_id)

                instances.extend([instance_obj])
            private_network = _add_servers(module, oneandone_conn,
                                           private_network['id'], instances)
            _check_mode(module, private_network)
            changed = True

        if _remove_members:
            chk_changed = False
            for member in _remove_members:
                instance = get_server(oneandone_conn, member, True)

                if module.check_mode:
                    chk_changed |= _remove_member(module, oneandone_conn,
                                                  private_network['id'],
                                                  instance['id'])
                _check_mode(module, instance and chk_changed)

                _remove_member(module, oneandone_conn, private_network['id'],
                               instance['id'])
            private_network = get_private_network(oneandone_conn,
                                                  private_network['id'], True)
            changed = True

        return (changed, private_network)
    except Exception as ex:
        module.fail_json(msg=str(ex))
コード例 #7
0
def update_monitoring_policy(module, oneandone_conn):
    """
    Updates a monitoring_policy based on input arguments.
    Monitoring policy ports, processes and servers can be added/removed to/from
    a monitoring policy. Monitoring policy name, description, email,
    thresholds for cpu, ram, disk, transfer and internal_ping
    can be updated as well.

    module : AnsibleModule object
    oneandone_conn: authenticated oneandone object
    """
    try:
        monitoring_policy_id = module.params.get('monitoring_policy')
        name = module.params.get('name')
        description = module.params.get('description')
        email = module.params.get('email')
        thresholds = module.params.get('thresholds')
        add_ports = module.params.get('add_ports')
        update_ports = module.params.get('update_ports')
        remove_ports = module.params.get('remove_ports')
        add_processes = module.params.get('add_processes')
        update_processes = module.params.get('update_processes')
        remove_processes = module.params.get('remove_processes')
        add_servers = module.params.get('add_servers')
        remove_servers = module.params.get('remove_servers')

        changed = False

        monitoring_policy = get_monitoring_policy(oneandone_conn,
                                                  monitoring_policy_id, True)
        if monitoring_policy is None:
            _check_mode(module, False)

        _monitoring_policy = oneandone.client.MonitoringPolicy(
            name=name, description=description, email=email)

        _thresholds = None

        if thresholds:
            threshold_entities = [
                'cpu', 'ram', 'disk', 'internal_ping', 'transfer'
            ]

            _thresholds = []
            for treshold in thresholds:
                key = treshold.keys()[0]
                if key in threshold_entities:
                    _threshold = oneandone.client.Threshold(
                        entity=key,
                        warning_value=treshold[key]['warning']['value'],
                        warning_alert=str(
                            treshold[key]['warning']['alert']).lower(),
                        critical_value=treshold[key]['critical']['value'],
                        critical_alert=str(
                            treshold[key]['critical']['alert']).lower())
                    _thresholds.append(_threshold)

        if name or description or email or thresholds:
            _check_mode(module, True)
            monitoring_policy = oneandone_conn.modify_monitoring_policy(
                monitoring_policy_id=monitoring_policy['id'],
                monitoring_policy=_monitoring_policy,
                thresholds=_thresholds)
            changed = True

        if add_ports:
            if module.check_mode:
                _check_mode(
                    module,
                    _add_ports(module, oneandone_conn, monitoring_policy['id'],
                               add_ports))

            monitoring_policy = _add_ports(module, oneandone_conn,
                                           monitoring_policy['id'], add_ports)
            changed = True

        if update_ports:
            chk_changed = False
            for update_port in update_ports:
                if module.check_mode:
                    chk_changed |= _modify_port(module, oneandone_conn,
                                                monitoring_policy['id'],
                                                update_port['id'], update_port)

                _modify_port(module, oneandone_conn, monitoring_policy['id'],
                             update_port['id'], update_port)
            monitoring_policy = get_monitoring_policy(oneandone_conn,
                                                      monitoring_policy['id'],
                                                      True)
            changed = True

        if remove_ports:
            chk_changed = False
            for port_id in remove_ports:
                if module.check_mode:
                    chk_changed |= _delete_monitoring_policy_port(
                        module, oneandone_conn, monitoring_policy['id'],
                        port_id)

                _delete_monitoring_policy_port(module, oneandone_conn,
                                               monitoring_policy['id'],
                                               port_id)
            _check_mode(module, chk_changed)
            monitoring_policy = get_monitoring_policy(oneandone_conn,
                                                      monitoring_policy['id'],
                                                      True)
            changed = True

        if add_processes:
            monitoring_policy = _add_processes(module, oneandone_conn,
                                               monitoring_policy['id'],
                                               add_processes)
            _check_mode(module, monitoring_policy)
            changed = True

        if update_processes:
            chk_changed = False
            for update_process in update_processes:
                if module.check_mode:
                    chk_changed |= _modify_process(module, oneandone_conn,
                                                   monitoring_policy['id'],
                                                   update_process['id'],
                                                   update_process)

                _modify_process(module, oneandone_conn,
                                monitoring_policy['id'], update_process['id'],
                                update_process)
            _check_mode(module, chk_changed)
            monitoring_policy = get_monitoring_policy(oneandone_conn,
                                                      monitoring_policy['id'],
                                                      True)
            changed = True

        if remove_processes:
            chk_changed = False
            for process_id in remove_processes:
                if module.check_mode:
                    chk_changed |= _delete_monitoring_policy_process(
                        module, oneandone_conn, monitoring_policy['id'],
                        process_id)

                _delete_monitoring_policy_process(module, oneandone_conn,
                                                  monitoring_policy['id'],
                                                  process_id)
            _check_mode(module, chk_changed)
            monitoring_policy = get_monitoring_policy(oneandone_conn,
                                                      monitoring_policy['id'],
                                                      True)
            changed = True

        if add_servers:
            monitoring_policy = _attach_monitoring_policy_server(
                module, oneandone_conn, monitoring_policy['id'], add_servers)
            _check_mode(module, monitoring_policy)
            changed = True

        if remove_servers:
            chk_changed = False
            for _server_id in remove_servers:
                server_id = get_server(oneandone_conn, _server_id)

                if module.check_mode:
                    chk_changed |= _detach_monitoring_policy_server(
                        module, oneandone_conn, monitoring_policy['id'],
                        server_id)

                _detach_monitoring_policy_server(module, oneandone_conn,
                                                 monitoring_policy['id'],
                                                 server_id)
            _check_mode(module, chk_changed)
            monitoring_policy = get_monitoring_policy(oneandone_conn,
                                                      monitoring_policy['id'],
                                                      True)
            changed = True

        return (changed, monitoring_policy)
    except Exception as ex:
        module.fail_json(msg=str(ex))
コード例 #8
0
def startstop_server(module, oneandone_conn):
    """
    Starts or Stops a server.

    module : AnsibleModule object
    oneandone_conn: authenticated oneandone object.

    Returns a dictionary with a 'changed' attribute indicating whether
    anything has changed for the server as a result of this function
    being run, and a 'server' attribute with basic information for
    the server.
    """
    state = module.params.get('state')
    server_id = module.params.get('server')
    wait = module.params.get('wait')
    wait_timeout = module.params.get('wait_timeout')
    wait_interval = module.params.get('wait_interval')

    changed = False

    # Resolve server
    server = get_server(oneandone_conn, server_id, True)
    if server:
        # Attempt to change the server state, only if it's not already there
        # or on its way.
        try:
            if state == 'stopped' and server['status']['state'] == 'POWERED_ON':
                _check_mode(module, True)
                oneandone_conn.modify_server_status(server_id=server['id'],
                                                    action='POWER_OFF',
                                                    method='SOFTWARE')
            elif state == 'running' and server['status'][
                    'state'] == 'POWERED_OFF':
                _check_mode(module, True)
                oneandone_conn.modify_server_status(server_id=server['id'],
                                                    action='POWER_ON',
                                                    method='SOFTWARE')
        except Exception as ex:
            module.fail_json(msg="failed to set server %s to state %s: %s" %
                             (server_id, state, str(ex)))

        _check_mode(module, False)

        # Make sure the server has reached the desired state
        if wait:
            operation_completed = False
            wait_timeout = time.time() + wait_timeout
            while wait_timeout > time.time():
                time.sleep(wait_interval)
                server = oneandone_conn.get_server(server['id'])  # refresh
                server_state = server['status']['state']
                if state == 'stopped' and server_state == 'POWERED_OFF':
                    operation_completed = True
                    break
                if state == 'running' and server_state == 'POWERED_ON':
                    operation_completed = True
                    break
            if not operation_completed:
                module.fail_json(
                    msg="Timeout waiting for server %s to get to state %s" %
                    (server_id, state))

        changed = True
        server = _insert_network_data(server)

    _check_mode(module, False)

    return (changed, server)