Ejemplo n.º 1
0
def ext_pillar(minion_id, pillar, account_name, token=None):
    client = linode_api4.LinodeClient(token)
    matched_linodes = client.linode.instances(
        linode_api4.Instance.label == minion_id)
    if (matched_linodes):
        linode = matched_linodes[0]
        return {"tags": {account_name: linode.tags}}
    else:
        return {"tags": {account_name: []}}
Ejemplo n.º 2
0
def linode_domain():
    """Add or remove a Linode domain."""
    module_args = {
        'domain': {
            'type': 'str'
        },
        'name': {
            'type': 'str',
            'required': True,
        },
        'state': {
            'type': 'str',
            'required': True,
            'choices': ['absent', 'present']
        },
        'target': {
            'type': 'str'
        }
    }

    execute = {'absent': del_subdomain, 'present': add_subdomain}

    result = {'changed': False, 'subdomain': '', 'domain': '', 'target': ''}

    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)

    # state with no modifications
    if module.check_mode:
        module.exit_json(**result)

    client = linode_api4.LinodeClient(os.environ.get('LINODE_TOKEN'))

    result = execute.get(module.params.get('state'))(module, client)

    # use whatever logic you need to determine whether or not this module
    # made any modifications to your target
    # if module.params['new']:
    #    result['changed'] = True

    # during the execution of the module, if there is an exception or a
    # conditional state that effectively causes a failure, run
    # AnsibleModule.fail_json() to pass in the message and the result
    if module.params['name'] == 'fail me':
        module.fail_json(msg='You requested this to fail', **result)

    # in the event of a successful module execution, you will want to
    # simple AnsibleModule.exit_json(), passing the key/value results
    module.exit_json(**result)
Ejemplo n.º 3
0
def regions_list():
    """
    list all the valid Linode regions

    :param: None
    :return: returns sorted list of region strings

    requires
        LINODE_TOKEN must be defined
    """
    client = linode_api4.LinodeClient(TOKEN)

    region_list = []
    for region in client.regions():
        region_list.append(region.id)
    return sorted(region_list)
Ejemplo n.º 4
0
def images_list():
    """
    list all Linode images

    :param: None
    :return: returns sorted list of image strings

    requires
        LINODE_TOKEN must be defined
    """
    client = linode_api4.LinodeClient(TOKEN)

    image_list = []
    for image in client.images():
        image_list.append(image.label + '  (' + image.id+')')
    return sorted(image_list)
Ejemplo n.º 5
0
def types_list():
    """
    list all Linode types

    :param: None
    :return: returns sorted list of type strings

    requires
        LINODE_TOKEN must be defined
    """
    client = linode_api4.LinodeClient(TOKEN)

    type_list = []
    for type in client.linode.types():
        type_list.append( type.id + '  (' + type.label + ')' )
    return sorted(type_list)
Ejemplo n.º 6
0
def descr_instances():
    """
    get the running linodes for the owner of the LINODE_TOKEN

    :param: None
    :return: dict{ImageID} -- all the instance attributes

    requires
        LINODE_TOKEN must be defined
    """
    # this returns list pageinatedItems
    # Images is a list of dicts containing each image's info
    client = linode_api4.LinodeClient(TOKEN)
    response = client.linode.instances()   # - list of paginatedItems
    # PaginatedList
    # for t in res.tags: res[ 'tag_{}'.format(t) ] = t
    # instances = []
    # for res in response:
    #     instances.append(res)  # add instance to list of instances to return
    return response
Ejemplo n.º 7
0
 def __init__(self, linode_account):
     api_token = linode_account.api_token
     self.client = linode_api4.LinodeClient(api_token)
Ejemplo n.º 8
0
else:
    metric = "gigabytes"
    warn = int(args.w)
    crit = int(args.c)

if args.k:
    key = args.k
elif args.f:
    with open(args.f, 'r') as f:
        key = f.read().strip()
else:
    print("UNKNOWN: No api key specified.")
    sys.exit(3)

try:
    lin = linode_api4.LinodeClient(key)
except Exception as e:
    print(f"UNKNOWN: problem with API key. Error: {e}")
    sys.exit(3)

transfer = lin.account.transfer()
quota = transfer.quota
used = transfer.used
free = quota - used
billable = transfer.billable

percused = (used / quota) * 100
percfree = 100 - percused

if args.i:
    perfdata = f'free_perc={percfree};;;0;100 free_gig={free};;;0;{quota}'
Ejemplo n.º 9
0
 def __init__(self, auth_token, sshkey=""):
     self.client = li.LinodeClient(auth_token)
     self.linodes = self._get_linodes()
     self.sshkey = sshkey