Beispiel #1
0
def get_gcelb_driver(gce_driver=None):
    # The GCE Load Balancer driver uses the GCE Compute driver for all of its
    # API calls.  You can either provide the driver directly, or provide the
    # same authentication information so the LB driver can get its own
    # Compute driver.
    if gce_driver:
        driver = get_driver_lb(Provider_lb.GCE)(gce_driver=gce_driver)
    else:
        driver = get_driver_lb(Provider_lb.GCE)(*args, **kwargs)
    return driver
Beispiel #2
0
def get_gcelb_driver(gce_driver=None):
    # The GCE Load Balancer driver uses the GCE Compute driver for all of its
    # API calls.  You can either provide the driver directly, or provide the
    # same authentication information so the LB driver can get its own
    # Compute driver.
    if gce_driver:
        driver = get_driver_lb(Provider_lb.GCE)(gce_driver=gce_driver)
    else:
        driver = get_driver_lb(Provider_lb.GCE)(*args, **kwargs)
    return driver
def main():
    module = AnsibleModule(argument_spec=dict(
        httphealthcheck_name=dict(),
        httphealthcheck_port=dict(default=80),
        httphealthcheck_path=dict(default='/'),
        httphealthcheck_interval=dict(default=5),
        httphealthcheck_timeout=dict(default=5),
        httphealthcheck_unhealthy_count=dict(default=2),
        httphealthcheck_healthy_count=dict(default=2),
        httphealthcheck_host=dict(),
        name=dict(),
        protocol=dict(default='tcp'),
        region=dict(),
        external_ip=dict(),
        port_range=dict(),
        members=dict(type='list'),
        state=dict(default='present'),
        service_account_email=dict(),
        pem_file=dict(),
        credentials_file=dict(),
        project_id=dict(),
    ))

    if not HAS_LIBCLOUD:
        module.fail_json(
            msg='libcloud with GCE support (0.13.3+) required for this module.'
        )

    gce = gce_connect(module)

    httphealthcheck_name = module.params.get('httphealthcheck_name')
    httphealthcheck_port = module.params.get('httphealthcheck_port')
    httphealthcheck_path = module.params.get('httphealthcheck_path')
    httphealthcheck_interval = module.params.get('httphealthcheck_interval')
    httphealthcheck_timeout = module.params.get('httphealthcheck_timeout')
    httphealthcheck_unhealthy_count = \
            module.params.get('httphealthcheck_unhealthy_count')
    httphealthcheck_healthy_count = \
            module.params.get('httphealthcheck_healthy_count')
    httphealthcheck_host = module.params.get('httphealthcheck_host')
    name = module.params.get('name')
    protocol = module.params.get('protocol')
    region = module.params.get('region')
    external_ip = module.params.get('external_ip')
    port_range = module.params.get('port_range')
    members = module.params.get('members')
    state = module.params.get('state')

    try:
        gcelb = get_driver_lb(Provider_lb.GCE)(gce_driver=gce)
        gcelb.connection.user_agent_append(
            "%s/%s" % (USER_AGENT_PRODUCT, USER_AGENT_VERSION))
    except Exception, e:
        module.fail_json(msg=unexpected_error_msg(e), changed=False)
def main():
    module = AnsibleModule(
        argument_spec = dict(
            httphealthcheck_name = dict(),
            httphealthcheck_port = dict(default=80),
            httphealthcheck_path = dict(default='/'),
            httphealthcheck_interval = dict(default=5),
            httphealthcheck_timeout = dict(default=5),
            httphealthcheck_unhealthy_count = dict(default=2),
            httphealthcheck_healthy_count = dict(default=2),
            httphealthcheck_host = dict(),
            name = dict(),
            protocol = dict(default='tcp'),
            region = dict(),
            external_ip = dict(),
            port_range = dict(),
            members = dict(type='list'),
            state = dict(default='present'),
            service_account_email = dict(),
            pem_file = dict(),
            project_id = dict(),
        )
    )

    if not HAS_LIBCLOUD:
        module.fail_json(msg='libcloud with GCE support (0.13.3+) required for this module.')

    gce = gce_connect(module)

    httphealthcheck_name = module.params.get('httphealthcheck_name')
    httphealthcheck_port = module.params.get('httphealthcheck_port')
    httphealthcheck_path = module.params.get('httphealthcheck_path')
    httphealthcheck_interval = module.params.get('httphealthcheck_interval')
    httphealthcheck_timeout = module.params.get('httphealthcheck_timeout')
    httphealthcheck_unhealthy_count = \
            module.params.get('httphealthcheck_unhealthy_count')
    httphealthcheck_healthy_count = \
            module.params.get('httphealthcheck_healthy_count')
    httphealthcheck_host = module.params.get('httphealthcheck_host')
    name = module.params.get('name')
    protocol = module.params.get('protocol')
    region = module.params.get('region')
    external_ip = module.params.get('external_ip')
    port_range = module.params.get('port_range')
    members = module.params.get('members')
    state = module.params.get('state')

    try:
        gcelb = get_driver_lb(Provider_lb.GCE)(gce_driver=gce)
        gcelb.connection.user_agent_append("%s/%s" % (
                USER_AGENT_PRODUCT, USER_AGENT_VERSION))
    except Exception, e:
        module.fail_json(msg=unexpected_error_msg(e), changed=False)
Beispiel #5
0
	def __init__(self, region):

		cfgfile = "inventory.cfg"
		# Confs
		self.config = ConfigParser.RawConfigParser()
		self.config.read(cfgfile)

		# Auth confs
		username = self.config.get("rackspace", "username")
		apikey = self.config.get("rackspace", "apikey")

		lb = get_driver_lb(Provider.RACKSPACE)
		self.loadbalancer = lb(username, apikey, region=region)
def get_lb_conn(dd_driver=None):
    """
    Return a load-balancer conn object
    """
    vm_ = get_configured_provider()

    region = config.get_cloud_config_value("region", vm_, __opts__)

    user_id = config.get_cloud_config_value("user_id", vm_, __opts__)
    key = config.get_cloud_config_value("key", vm_, __opts__)
    if not dd_driver:
        raise SaltCloudSystemExit(
            "Missing dimensiondata_driver for get_lb_conn method."
        )
    return get_driver_lb(Provider_lb.DIMENSIONDATA)(user_id, key, region=region)
Beispiel #7
0
def get_lb_conn(dd_driver=None):
    '''
    Return a load-balancer conn object
    '''
    vm_ = get_configured_provider()

    region = config.get_cloud_config_value('region', vm_, __opts__)

    user_id = config.get_cloud_config_value('user_id', vm_, __opts__)
    key = config.get_cloud_config_value('key', vm_, __opts__)
    if not dd_driver:
        raise SaltCloudSystemExit(
            'Missing dimensiondata_driver for get_lb_conn method.')
    return get_driver_lb(Provider_lb.DIMENSIONDATA)(user_id,
                                                    key,
                                                    region=region)
Beispiel #8
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            httphealthcheck_name=dict(),
            httphealthcheck_port=dict(default=80),
            httphealthcheck_path=dict(default="/"),
            httphealthcheck_interval=dict(default=5),
            httphealthcheck_timeout=dict(default=5),
            httphealthcheck_unhealthy_count=dict(default=2),
            httphealthcheck_healthy_count=dict(default=2),
            httphealthcheck_host=dict(),
            name=dict(),
            protocol=dict(default="tcp"),
            region=dict(),
            external_ip=dict(),
            port_range=dict(),
            members=dict(type="list"),
            state=dict(default="present"),
            service_account_email=dict(),
            pem_file=dict(),
            project_id=dict(),
        )
    )

    gce = gce_connect(module)

    httphealthcheck_name = module.params.get("httphealthcheck_name")
    httphealthcheck_port = module.params.get("httphealthcheck_port")
    httphealthcheck_path = module.params.get("httphealthcheck_path")
    httphealthcheck_interval = module.params.get("httphealthcheck_interval")
    httphealthcheck_timeout = module.params.get("httphealthcheck_timeout")
    httphealthcheck_unhealthy_count = module.params.get("httphealthcheck_unhealthy_count")
    httphealthcheck_healthy_count = module.params.get("httphealthcheck_healthy_count")
    httphealthcheck_host = module.params.get("httphealthcheck_host")
    name = module.params.get("name")
    protocol = module.params.get("protocol")
    region = module.params.get("region")
    external_ip = module.params.get("external_ip")
    port_range = module.params.get("port_range")
    members = module.params.get("members")
    state = module.params.get("state")

    try:
        gcelb = get_driver_lb(Provider_lb.GCE)(gce_driver=gce)
        gcelb.connection.user_agent_append("%s/%s" % (USER_AGENT_PRODUCT, USER_AGENT_VERSION))
    except Exception, e:
        module.fail_json(msg=unexpected_error_msg(e), changed=False)
Beispiel #9
0
def get_lb_conn(dd_driver=None):
    '''
    Return a load-balancer conn object
    '''
    vm_ = get_configured_provider()

    region = config.get_cloud_config_value(
        'region', vm_, __opts__
    )

    user_id = config.get_cloud_config_value(
       'user_id', vm_, __opts__
    )
    key = config.get_cloud_config_value(
       'key', vm_, __opts__
    )
    if not dd_driver:
        raise SaltCloudSystemExit(
            'Missing dimensiondata_driver for get_lb_conn method.'
        )
    return get_driver_lb(Provider_lb.DIMENSIONDATA)(user_id, key, region=region)
Beispiel #10
0
def main():
    module = AnsibleModule(argument_spec=dict(
        httphealthcheck_name=dict(),
        httphealthcheck_port=dict(default=80),
        httphealthcheck_path=dict(default='/'),
        httphealthcheck_interval=dict(default=5),
        httphealthcheck_timeout=dict(default=5),
        httphealthcheck_unhealthy_count=dict(default=2),
        httphealthcheck_healthy_count=dict(default=2),
        httphealthcheck_host=dict(),
        name=dict(),
        protocol=dict(default='tcp'),
        region=dict(),
        external_ip=dict(),
        port_range=dict(),
        members=dict(type='list'),
        state=dict(default='present'),
        service_account_email=dict(),
        pem_file=dict(),
        credentials_file=dict(),
        project_id=dict(),
    ))

    if not HAS_LIBCLOUD:
        module.fail_json(
            msg='libcloud with GCE support (0.13.3+) required for this module.'
        )

    gce = gce_connect(module)

    httphealthcheck_name = module.params.get('httphealthcheck_name')
    httphealthcheck_port = module.params.get('httphealthcheck_port')
    httphealthcheck_path = module.params.get('httphealthcheck_path')
    httphealthcheck_interval = module.params.get('httphealthcheck_interval')
    httphealthcheck_timeout = module.params.get('httphealthcheck_timeout')
    httphealthcheck_unhealthy_count = \
            module.params.get('httphealthcheck_unhealthy_count')
    httphealthcheck_healthy_count = \
            module.params.get('httphealthcheck_healthy_count')
    httphealthcheck_host = module.params.get('httphealthcheck_host')
    name = module.params.get('name')
    protocol = module.params.get('protocol')
    region = module.params.get('region')
    external_ip = module.params.get('external_ip')
    port_range = module.params.get('port_range')
    members = module.params.get('members')
    state = module.params.get('state')

    try:
        gcelb = get_driver_lb(Provider_lb.GCE)(gce_driver=gce)
        gcelb.connection.user_agent_append(
            "%s/%s" % (USER_AGENT_PRODUCT, USER_AGENT_VERSION))
    except Exception as e:
        module.fail_json(msg=unexpected_error_msg(e), changed=False)

    changed = False
    json_output = {'name': name, 'state': state}

    if not name and not httphealthcheck_name:
        module.fail_json(msg='Nothing to do, please specify a "name" ' + \
                'or "httphealthcheck_name" parameter', changed=False)

    if state in ['active', 'present']:
        # first, create the httphealthcheck if requested
        hc = None
        if httphealthcheck_name:
            json_output['httphealthcheck_name'] = httphealthcheck_name
            try:
                hc = gcelb.ex_create_healthcheck(
                    httphealthcheck_name,
                    host=httphealthcheck_host,
                    path=httphealthcheck_path,
                    port=httphealthcheck_port,
                    interval=httphealthcheck_interval,
                    timeout=httphealthcheck_timeout,
                    unhealthy_threshold=httphealthcheck_unhealthy_count,
                    healthy_threshold=httphealthcheck_healthy_count)
                changed = True
            except ResourceExistsError:
                hc = gce.ex_get_healthcheck(httphealthcheck_name)
            except Exception as e:
                module.fail_json(msg=unexpected_error_msg(e), changed=False)

            if hc is not None:
                json_output['httphealthcheck_host'] = hc.extra['host']
                json_output['httphealthcheck_path'] = hc.path
                json_output['httphealthcheck_port'] = hc.port
                json_output['httphealthcheck_interval'] = hc.interval
                json_output['httphealthcheck_timeout'] = hc.timeout
                json_output['httphealthcheck_unhealthy_count'] = \
                        hc.unhealthy_threshold
                json_output['httphealthcheck_healthy_count'] = \
                        hc.healthy_threshold

        # create the forwarding rule (and target pool under the hood)
        lb = None
        if name:
            if not region:
                module.fail_json(msg='Missing required region name',
                                 changed=False)
            nodes = []
            output_nodes = []
            json_output['name'] = name
            # members is a python list of 'zone/inst' strings
            if members:
                for node in members:
                    try:
                        zone, node_name = node.split('/')
                        nodes.append(gce.ex_get_node(node_name, zone))
                        output_nodes.append(node)
                    except:
                        # skip nodes that are badly formatted or don't exist
                        pass
            try:
                if hc is not None:
                    lb = gcelb.create_balancer(name,
                                               port_range,
                                               protocol,
                                               None,
                                               nodes,
                                               ex_region=region,
                                               ex_healthchecks=[hc],
                                               ex_address=external_ip)
                else:
                    lb = gcelb.create_balancer(name,
                                               port_range,
                                               protocol,
                                               None,
                                               nodes,
                                               ex_region=region,
                                               ex_address=external_ip)
                changed = True
            except ResourceExistsError:
                lb = gcelb.get_balancer(name)
            except Exception as e:
                module.fail_json(msg=unexpected_error_msg(e), changed=False)

            if lb is not None:
                json_output['members'] = output_nodes
                json_output['protocol'] = protocol
                json_output['region'] = region
                json_output['external_ip'] = lb.ip
                json_output['port_range'] = lb.port
                hc_names = []
                if 'healthchecks' in lb.extra:
                    for hc in lb.extra['healthchecks']:
                        hc_names.append(hc.name)
                json_output['httphealthchecks'] = hc_names

    if state in ['absent', 'deleted']:
        # first, delete the load balancer (forwarding rule and target pool)
        # if specified.
        if name:
            json_output['name'] = name
            try:
                lb = gcelb.get_balancer(name)
                gcelb.destroy_balancer(lb)
                changed = True
            except ResourceNotFoundError:
                pass
            except Exception as e:
                module.fail_json(msg=unexpected_error_msg(e), changed=False)

        # destroy the health check if specified
        if httphealthcheck_name:
            json_output['httphealthcheck_name'] = httphealthcheck_name
            try:
                hc = gce.ex_get_healthcheck(httphealthcheck_name)
                gce.ex_destroy_healthcheck(hc)
                changed = True
            except ResourceNotFoundError:
                pass
            except Exception as e:
                module.fail_json(msg=unexpected_error_msg(e), changed=False)

    json_output['changed'] = changed
    module.exit_json(**json_output)
Beispiel #11
0
def main():
    module = AnsibleModule(
        argument_spec = dict(
            httphealthcheck_name = dict(),
            httphealthcheck_port = dict(default=80),
            httphealthcheck_path = dict(default='/'),
            httphealthcheck_interval = dict(default=5),
            httphealthcheck_timeout = dict(default=5),
            httphealthcheck_unhealthy_count = dict(default=2),
            httphealthcheck_healthy_count = dict(default=2),
            httphealthcheck_host = dict(),
            name = dict(),
            protocol = dict(default='tcp'),
            region = dict(),
            external_ip = dict(),
            port_range = dict(),
            members = dict(type='list'),
            state = dict(default='present'),
            service_account_email = dict(),
            pem_file = dict(),
            credentials_file = dict(),
            project_id = dict(),
        )
    )

    if not HAS_LIBCLOUD:
        module.fail_json(msg='libcloud with GCE support (0.13.3+) required for this module.')

    gce = gce_connect(module)

    httphealthcheck_name = module.params.get('httphealthcheck_name')
    httphealthcheck_port = module.params.get('httphealthcheck_port')
    httphealthcheck_path = module.params.get('httphealthcheck_path')
    httphealthcheck_interval = module.params.get('httphealthcheck_interval')
    httphealthcheck_timeout = module.params.get('httphealthcheck_timeout')
    httphealthcheck_unhealthy_count = \
            module.params.get('httphealthcheck_unhealthy_count')
    httphealthcheck_healthy_count = \
            module.params.get('httphealthcheck_healthy_count')
    httphealthcheck_host = module.params.get('httphealthcheck_host')
    name = module.params.get('name')
    protocol = module.params.get('protocol')
    region = module.params.get('region')
    external_ip = module.params.get('external_ip')
    port_range = module.params.get('port_range')
    members = module.params.get('members')
    state = module.params.get('state')

    try:
        gcelb = get_driver_lb(Provider_lb.GCE)(gce_driver=gce)
        gcelb.connection.user_agent_append("%s/%s" % (
                USER_AGENT_PRODUCT, USER_AGENT_VERSION))
    except Exception as e:
        module.fail_json(msg=unexpected_error_msg(e), changed=False)

    changed = False
    json_output = {'name': name, 'state': state}

    if not name and not httphealthcheck_name:
        module.fail_json(msg='Nothing to do, please specify a "name" ' + \
                'or "httphealthcheck_name" parameter', changed=False)

    if state in ['active', 'present']:
        # first, create the httphealthcheck if requested
        hc = None
        if httphealthcheck_name:
            json_output['httphealthcheck_name'] = httphealthcheck_name
            try:
                hc = gcelb.ex_create_healthcheck(httphealthcheck_name,
                    host=httphealthcheck_host, path=httphealthcheck_path,
                    port=httphealthcheck_port,
                    interval=httphealthcheck_interval,
                    timeout=httphealthcheck_timeout,
                    unhealthy_threshold=httphealthcheck_unhealthy_count,
                    healthy_threshold=httphealthcheck_healthy_count)
                changed = True
            except ResourceExistsError:
                hc = gce.ex_get_healthcheck(httphealthcheck_name)
            except Exception as e:
                module.fail_json(msg=unexpected_error_msg(e), changed=False)

            if hc is not None:
                json_output['httphealthcheck_host'] = hc.extra['host']
                json_output['httphealthcheck_path'] = hc.path
                json_output['httphealthcheck_port'] = hc.port
                json_output['httphealthcheck_interval'] = hc.interval
                json_output['httphealthcheck_timeout'] = hc.timeout
                json_output['httphealthcheck_unhealthy_count'] = \
                        hc.unhealthy_threshold
                json_output['httphealthcheck_healthy_count'] = \
                        hc.healthy_threshold

        # create the forwarding rule (and target pool under the hood)
        lb = None
        if name:
            if not region:
                module.fail_json(msg='Missing required region name',
                        changed=False)
            nodes = []
            output_nodes = []
            json_output['name'] = name
            # members is a python list of 'zone/inst' strings
            if members:
                for node in members:
                    try:
                        zone, node_name = node.split('/')
                        nodes.append(gce.ex_get_node(node_name, zone))
                        output_nodes.append(node)
                    except:
                        # skip nodes that are badly formatted or don't exist
                        pass
            try:
                if hc is not None:
                    lb = gcelb.create_balancer(name, port_range, protocol,
                        None, nodes, ex_region=region, ex_healthchecks=[hc],
                        ex_address=external_ip)
                else:
                    lb = gcelb.create_balancer(name, port_range, protocol,
                        None, nodes, ex_region=region, ex_address=external_ip)
                changed = True
            except ResourceExistsError:
                lb = gcelb.get_balancer(name)
            except Exception as e:
                module.fail_json(msg=unexpected_error_msg(e), changed=False)

            if lb is not None:
                json_output['members'] = output_nodes
                json_output['protocol'] = protocol
                json_output['region'] = region
                json_output['external_ip'] = lb.ip
                json_output['port_range'] = lb.port
                hc_names = []
                if 'healthchecks' in lb.extra:
                    for hc in lb.extra['healthchecks']:
                        hc_names.append(hc.name)
                json_output['httphealthchecks'] = hc_names

    if state in ['absent', 'deleted']:
        # first, delete the load balancer (forwarding rule and target pool)
        # if specified.
        if name:
            json_output['name'] = name
            try:
                lb = gcelb.get_balancer(name)
                gcelb.destroy_balancer(lb)
                changed = True
            except ResourceNotFoundError:
                pass
            except Exception as e:
                module.fail_json(msg=unexpected_error_msg(e), changed=False)

        # destroy the health check if specified
        if httphealthcheck_name:
            json_output['httphealthcheck_name'] = httphealthcheck_name
            try:
                hc = gce.ex_get_healthcheck(httphealthcheck_name)
                gce.ex_destroy_healthcheck(hc)
                changed = True
            except ResourceNotFoundError:
                pass
            except Exception as e:
                module.fail_json(msg=unexpected_error_msg(e), changed=False)


    json_output['changed'] = changed
    module.exit_json(**json_output)