def get_config_id(kwargs=None, call=None): ''' Returns a config_id for a given linode. .. versionadded:: 2015.8.0 name The name of the Linode for which to get the config_id. Can be used instead of ``linode_id``.h linode_id The ID of the Linode for which to get the config_id. Can be used instead of ``name``. CLI Example: .. code-block:: bash salt-cloud -f get_config_id my-linode-config name=my-linode salt-cloud -f get_config_id my-linode-config linode_id=1234567 ''' if call == 'action': raise SaltCloudException( 'The get_config_id function must be called with -f or --function.') if kwargs is None: kwargs = {} name = kwargs.get('name', None) linode_id = kwargs.get('linode_id', None) if name is None and linode_id is None: raise SaltCloudSystemExit( 'The get_config_id function requires either a \'name\' or a \'linode_id\' ' 'to be provided.') if linode_id is None: linode_id = get_linode_id_from_name(name) response = _query('linode', 'config.list', args={'LinodeID': linode_id})['DATA'] config_id = {'config_id': response[0]['ConfigID']} return config_id
def show_instance(name, call=None): ''' Displays details about a particular Linode VM. Either a name or a linode_id must be provided. .. versionadded:: 2015.8.0 name The name of the VM for which to display details. CLI Example: .. code-block:: bash salt-cloud -a show_instance vm_name .. note:: The ``image`` label only displays information about the VM's distribution vendor, such as "Debian" or "RHEL" and does not display the actual image name. This is due to a limitation of the Linode API. ''' if call != 'action': raise SaltCloudException( 'The show_instance action must be called with -a or --action.') node_id = get_linode_id_from_name(name) node_data = get_linode(kwargs={'linode_id': node_id}) ips = get_ips(node_id) state = int(node_data['STATUS']) ret = { 'id': node_data['LINODEID'], 'image': node_data['DISTRIBUTIONVENDOR'], 'name': node_data['LABEL'], 'size': node_data['TOTALRAM'], 'state': _get_status_descr_by_id(state), 'private_ips': ips['private_ips'], 'public_ips': ips['public_ips'] } return ret
def shutdown(name, call=None, session=None): ''' Shutdown a vm .. code-block:: bash salt-cloud -a shutdown xenvm01 ''' if call == 'function': raise SaltCloudException( 'The show_instnce function must be called with -a or --action.' ) if session is None: session = _get_session() log.info('Starting VM {0}'.format(name)) vm = _get_vm(name, session) task = session.xenapi.Async.VM.shutdown(vm) _run_async_task(task, session) return show_instance(name)
def resume(name, call=None, session=None): ''' Resume a vm from disk .. code-block:: bash salt-cloud -a resume xenvm01 ''' if call == 'function': raise SaltCloudException( 'The show_instnce function must be called with -a or --action.' ) if session is None: session = _get_session() log.info('Resuming VM {0}'.format(name)) vm = _get_vm(name, session) task = session.xenapi.Async.VM.resume(vm, False, True) _run_async_task(task, session) return show_instance(name)
def get_pv_args(name, session=None, call=None): ''' Get PV arguments for a VM .. code-block:: bash salt-cloud -a get_pv_args xenvm01 ''' if call == 'function': raise SaltCloudException( 'This function must be called with -a or --action.') if session is None: log.debug('New session being created') session = _get_session() vm = _get_vm(name, session=session) pv_args = session.xenapi.VM.get_PV_args(vm) if len(pv_args) > 0: return pv_args return None
def get_pv_args(name, session=None, call=None): """ Get PV arguments for a VM .. code-block:: bash salt-cloud -a get_pv_args xenvm01 """ if call == "function": raise SaltCloudException( "This function must be called with -a or --action.") if session is None: log.debug("New session being created") session = _get_session() vm = _get_vm(name, session=session) pv_args = session.xenapi.VM.get_PV_args(vm) if pv_args: return pv_args return None
def reboot(name, call=None): """ Reboot a Kamatera server. .. versionadded:: 2015.8.0 name The name of the server to reboot. CLI Example: .. code-block:: bash salt-cloud -a reboot server_name """ if call != "action": raise SaltCloudException( "The show_instance action must be called with -a or --action." ) return _server_operation(name, "reboot")
def suspend(name, call=None, session=None): ''' Suspend a vm to disk .. code-block:: bash salt-cloud -a suspend xenvm01 ''' if call == 'function': raise SaltCloudException( 'The show_instnce function must be called with -a or --action.' ) if session is None: session = _get_session() log.info('Suspending VM %s', name) vm = _get_vm(name, session) task = session.xenapi.Async.VM.suspend(vm) _run_async_task(task, session) return show_instance(name)
def stop(name, call=None): """ Stop a Kamatera server. .. versionadded:: 2015.8.0 name The name of the server to stop. CLI Example: .. code-block:: bash salt-cloud -a stop server_name """ if call != "action": raise SaltCloudException( "The show_instance action must be called with -a or --action." ) return _server_operation(name, "poweroff")
def shutdown(name, call=None, session=None): """ Shutdown a vm .. code-block:: bash salt-cloud -a shutdown xenvm01 """ if call == "function": raise SaltCloudException( "The show_instnce function must be called with -a or --action." ) if session is None: session = _get_session() log.info("Starting VM %s", name) vm = _get_vm(name, session) task = session.xenapi.Async.VM.shutdown(vm) _run_async_task(task, session) return show_instance(name)
def resume(name, call=None, session=None): """ Resume a vm from disk .. code-block:: bash salt-cloud -a resume xenvm01 """ if call == "function": raise SaltCloudException( "The show_instnce function must be called with -a or --action." ) if session is None: session = _get_session() log.info("Resuming VM %s", name) vm = _get_vm(name, session) task = session.xenapi.Async.VM.resume(vm, False, True) _run_async_task(task, session) return show_instance(name)
def show_instance(name, call=None): """ Displays details about a specific Kamatera server .. versionadded:: 2015.8.0 name Server name CLI Example: .. code-block:: bash salt-cloud -a show_instance server_name """ if call != "action": raise SaltCloudException( "The show_instance action must be called with -a or --action." ) return list_nodes(full=True, name_regex=name)[name]
def show_instance(name, session=None, call=None): """ Show information about a specific VM or template .. code-block:: bash salt-cloud -a show_instance xenvm01 .. note:: memory is memory_dynamic_max """ if call == "function": raise SaltCloudException( "The show_instnce function must be called with -a or --action.") log.debug("show_instance-> name: %s session: %s", name, session) if session is None: session = _get_session() vm = _get_vm(name, session=session) record = session.xenapi.VM.get_record(vm) if not record["is_a_template"] and not record["is_control_domain"]: try: base_template_name = record["other_config"]["base_template_name"] except Exception: # pylint: disable=broad-except base_template_name = None log.debug( "VM %s, does not have base_template_name attribute", record["name_label"], ) ret = { "id": record["uuid"], "image": base_template_name, "name": record["name_label"], "size": record["memory_dynamic_max"], "state": record["power_state"], "private_ips": get_vm_ip(name, session), "public_ips": None, } __utils__["cloud.cache_node"](ret, _get_active_provider_name(), __opts__) return ret
def list_nodes(call=None): ''' Returns a list of devices, keeping only a brief listing. CLI Example: .. code-block:: bash salt-cloud -Q salt-cloud --query salt-cloud -f list_nodes packet-provider .. ''' if call == 'action': raise SaltCloudException( 'The list_nodes function must be called with -f or --function.') ret = {} for device in get_devices_by_token(): ret[device.hostname] = device.__dict__ return ret
def list_nodes(call=None): ''' Returns a list of linodes, keeping only a brief listing. CLI Example: .. code-block:: bash salt-cloud -Q salt-cloud --query .. note:: The ``image`` label only displays information about the VM's distribution vendor, such as "Debian" or "RHEL" and does not display the actual image name. This is due to a limitation of the Linode API. ''' if call == 'action': raise SaltCloudException( 'The list_nodes function must be called with -f or --function.') return _list_linodes(full=False)
def set_vm_ip(name=None, ipv4_cidr=None, ipv4_gw=None, session=None, call=None): ''' Set the IP address on a virtual interface (vif) ''' mode = 'static' # TODO: Need to add support for IPv6 if call == 'function': raise SaltCloudException( 'The function must be called with -a or --action.') log.debug( 'Setting name: %s ipv4_cidr: %s ipv4_gw: %s mode: %s', name, ipv4_cidr, ipv4_gw, mode ) if session is None: log.debug('New session being created') session = _get_session() vm = _get_vm(name, session) # -- try to get ip from vif # TODO: for now will take first interface # addition consideration needed for # multiple interface(vif) VMs vifs = session.xenapi.VM.get_VIFs(vm) if vifs is not None: log.debug('There are %s vifs.', len(vifs)) for vif in vifs: record = session.xenapi.VIF.get_record(vif) log.debug(record) try: session.xenapi.VIF.configure_ipv4( vif, mode, ipv4_cidr, ipv4_gw) except XenAPI.Failure: log.info('Static IP assignment could not be performed.') return True
def get_vm_ip(name=None, session=None, call=None): """ Get the IP address of the VM .. code-block:: bash salt-cloud -a get_vm_ip xenvm01 .. note:: Requires xen guest tools to be installed in VM """ if call == "function": raise SaltCloudException( "This function must be called with -a or --action.") if session is None: log.debug("New session being created") session = _get_session() vm = _get_vm(name, session=session) ret = None # -- try to get ip from vif vifs = session.xenapi.VM.get_VIFs(vm) if vifs is not None: for vif in vifs: if session.xenapi.VIF.get_ipv4_addresses(vif): cidr = session.xenapi.VIF.get_ipv4_addresses(vif).pop() ret, subnet = cidr.split("/") log.debug("VM vif returned for instance: %s ip: %s", name, ret) return ret # -- try to get ip from get tools metrics vgm = session.xenapi.VM.get_guest_metrics(vm) try: net = session.xenapi.VM_guest_metrics.get_networks(vgm) if "0/ip" in net.keys(): log.debug("VM guest metrics returned for instance: %s 0/ip: %s", name, net["0/ip"]) ret = net["0/ip"] # except Exception as ex: # pylint: disable=broad-except except XenAPI.Failure: log.info("Could not get vm metrics at this time") return ret
def get_vm_ip(name=None, session=None, call=None): ''' Get the IP address of the VM .. code-block:: bash salt-cloud -a get_vm_ip xenvm01 .. note:: Requires xen guest tools to be installed in VM ''' if call == 'function': raise SaltCloudException( 'This function must be called with -a or --action.') if session is None: log.debug('New session being created') session = _get_session() vm = _get_vm(name, session=session) ret = None # -- try to get ip from vif vifs = session.xenapi.VM.get_VIFs(vm) if vifs is not None: for vif in vifs: if len(session.xenapi.VIF.get_ipv4_addresses(vif)) != 0: cidr = session.xenapi.VIF.get_ipv4_addresses(vif).pop() ret, subnet = cidr.split('/') log.debug('VM vif returned for instance: %s ip: %s', name, ret) return ret # -- try to get ip from get tools metrics vgm = session.xenapi.VM.get_guest_metrics(vm) try: net = session.xenapi.VM_guest_metrics.get_networks(vgm) if "0/ip" in net.keys(): log.debug('VM guest metrics returned for instance: %s 0/ip: %s', name, net["0/ip"]) ret = net["0/ip"] # except Exception as ex: except XenAPI.Failure: log.info('Could not get vm metrics at this time') return ret
def show_instance(name, call=None): ''' Displays details about a particular Linode VM. Either a name or a linode_id must be provided. .. versionadded:: 2015.8.0 name The name of the VM for which to display details. CLI Example: .. code-block:: bash salt-cloud -a show_instance vm_name .. note:: The ``image`` label only displays information about the VM's distribution vendor, such as "Debian" or "RHEL" and does not display the actual image name. This is due to a limitation of the Linode API. ''' if call != 'action': raise SaltCloudException( 'The show_instance action must be called with -a or --action.') node_id = get_linode_id_from_name(name) node_data = get_linode(kwargs={'linode_id': node_id}) state = node_data['status'] ret = { 'id': node_data['id'], 'image': node_data['image'], 'name': node_data['label'], 'size': node_data['specs']['memory'], 'state': node_data['status'], 'ips': node_data['ipv4'] } return ret
def reboot(name, call=None): """ Reboot a vagrant minion. name The name of the VM to reboot. CLI Example: .. code-block:: bash salt-cloud -a reboot vm_name """ if call != "action": raise SaltCloudException( "The reboot action must be called with -a or --action.") my_info = _get_my_info(name) profile_name = my_info[name]["profile"] profile = __opts__["profiles"][profile_name] host = profile["host"] with salt.client.LocalClient() as local: return local.cmd(host, "vagrant.reboot", [name])
def get_conn(**kwargs): ''' Return a conn object for the passed VM data ''' if 'location' in kwargs: location = kwargs['location'] if location not in EC2_LOCATIONS: raise SaltCloudException( 'The specified location does not seem to be valid: ' '{0}\n'.format(location)) else: location = DEFAULT_LOCATION driver = get_driver(EC2_LOCATIONS[location]) vm_ = get_configured_provider() return driver( config.get_cloud_config_value('id', vm_, __opts__, search_global=False), config.get_cloud_config_value('key', vm_, __opts__, search_global=False))
def vdi_list(call=None, kwargs=None): """ Return available Xen VDI images If this function is called with the ``-f`` or ``--function`` then it can return a list with minimal deatil using the ``terse=True`` keyword argument. .. code-block:: bash salt-cloud -f vdi_list myxen terse=True """ if call == "action": raise SaltCloudException("This function must be called with -f or --function.") log.debug("kwargs is %s", kwargs) if kwargs is not None: if "terse" in kwargs: if kwargs["terse"] == "True": terse = True else: terse = False else: terse = False else: kwargs = {} terse = False session = _get_session() vdis = session.xenapi.VDI.get_all() ret = {} for vdi in vdis: data = session.xenapi.VDI.get_record(vdi) log.debug(type(terse)) if terse is True: ret[data.get("name_label")] = {"uuid": data.get("uuid"), "OpqueRef": vdi} else: data.update({"OpaqueRef": vdi}) ret[data.get("name_label")] = data return ret
def list_nodes(call=None): ''' Return a list of VMs on OpenNebubla. call Optional type of call to use with this function such as ``function``. CLI Example: .. code-block:: bash salt-cloud -Q salt-cloud --query salt-cloud --fuction list_nodes opennebula salt-cloud -f list_nodes opennebula ''' if call == 'action': raise SaltCloudException( 'The list_nodes function must be called with -f or --function.') return _list_nodes(full=False)
def avail_locations(call=None): """ Return available Kamatera datacenter locations. CLI Example: .. code-block:: bash salt-cloud --list-locations my-kamatera-config salt-cloud -f avail_locations my-kamatera-config """ if call == "action": raise SaltCloudException( "The avail_locations function must be called with -f or --function." ) else: return { datacenter.pop("id"): "{0}, {1} ({2})".format( datacenter["subCategory"], datacenter["name"], datacenter["category"] ) for datacenter in _request("service/server?datacenter=1") }
def reboot(name, call=None): ''' Reboot a saltify minion. ..versionadded:: Oxygen name The name of the VM to reboot. CLI Example: .. code-block:: bash salt-cloud -a reboot vm_name ''' if call != 'action': raise SaltCloudException( 'The reboot action must be called with -a or --action.') local = salt.client.LocalClient() return local.cmd(name, 'system.reboot')
def reboot(name, call=None): ''' Reboot a vagrant minion. name The name of the VM to reboot. CLI Example: .. code-block:: bash salt-cloud -a reboot vm_name ''' if call != 'action': raise SaltCloudException( 'The reboot action must be called with -a or --action.') my_info = _get_my_info(name) profile_name = my_info[name]['profile'] profile = __opts__['profiles'][profile_name] host = profile['host'] local = salt.client.LocalClient() return local.cmd(host, 'vagrant.reboot', [name])
def avail_images(call=None): ''' Return available Linode images. CLI Example: .. code-block:: bash salt-cloud --list-images my-linode-config salt-cloud -f avail_images my-linode-config ''' if call == 'action': raise SaltCloudException( 'The avail_images function must be called with -f or --function.') response = _query('images') ret = {} for item in response['data']: name = item['label'] ret[name] = item return ret
def reboot(name, call=None): """ Reboot a saltify minion. .. versionadded:: 2018.3.0 name The name of the VM to reboot. CLI Example: .. code-block:: bash salt-cloud -a reboot vm_name """ if call != "action": raise SaltCloudException( "The reboot action must be called with -a or --action.") with salt.client.LocalClient() as local: return local.cmd(name, "system.reboot")
def stop(name=None, linode_id=None, call=None): ''' Stop a VM in Linode. Either a name or a linode_id must be provided. name The name of the VM to stop. linode_id The Linode ID of the VM to stop. Can be provided instead of name. CLI Example: .. code-block:: bash salt-cloud -a stop vm_name salt-cloud -a stop linode_id ''' if not name and not linode_id: raise SaltCloudException( 'Either a name or a linode_id must be specified.') node_id = _check_and_set_node_id(name, linode_id) node = get_linode(node_id) if node['STATUS'] == 2: return { 'success': True, 'state': 'Stopped', 'msg': 'Machine already stopped' } response = _query('linode', 'shutdown', args={'LinodeID': node_id})['DATA'] if _wait_for_job(node_id, response['JobID']): return {'state': 'Stopped', 'action': 'stop', 'success': True} else: return {'action': 'stop', 'success': False}
def show_instance(name, session=None, call=None): ''' Show information about a specific VM or template .. code-block:: bash salt-cloud -a show_instance xenvm01 .. note:: memory is memory_dynamic_max ''' if call == 'function': raise SaltCloudException( 'The show_instnce function must be called with -a or --action.') log.debug('show_instance-> name: %s session: %s', name, session) if session is None: session = _get_session() vm = _get_vm(name, session=session) record = session.xenapi.VM.get_record(vm) if not record['is_a_template'] and not record['is_control_domain']: try: base_template_name = record['other_config']['base_template_name'] except Exception: base_template_name = None log.debug('VM %s, doesnt have base_template_name attribute', record['name_label']) ret = { 'id': record['uuid'], 'image': base_template_name, 'name': record['name_label'], 'size': record['memory_dynamic_max'], 'state': record['power_state'], 'private_ips': get_vm_ip(name, session), 'public_ips': None } __utils__['cloud.cache_node'](ret, __active_provider_name__, __opts__) return ret