def stoplxc(host): """ Stop lxc in proxmox using given hostresource configuration """ #check if container is running and ask user to stop it if not exists(host): raise ValueError("Host template is missing. Please create host template") container = Container.getContainer(HOST_CONTAINER) hostresource = container.loadResource(host) #get proxmox user and hypervisor userresource = proxmoxutil.listuser() if userresource is None: raise ValueError("No proxmox user found!! Please use proxmoxutil command to update user credentials") user = userresource.properties[PROPERTIES_USER] password = userresource.properties[PROPERTIES_PASSWORD] authrealm = userresource.properties[PROPERTIES_AUTHREALM] puser = user+'@'+authrealm primary = proxmoxutil.listprimary() if primary is None: raise ValueError("Primary proxmox hypervisor not found!! Please use proxmoxutil command to update primary hypervisor") hypervisor = primary.properties[PROPERTIES_HYPERVISOR] print "Authenticating "+puser +" on "+ hypervisor proxmox = ProxmoxAPI(hypervisor, user=puser, password=password, verify_ssl=False) node = proxmox.nodes(hostresource.properties[HYPERVISOR]) vmid = int(hostresource.properties[HOSTID]) print "Stopping container" node.lxc(vmid).status.stop.post() time.sleep(30) print "Stopped container"
def __init__(self): # Read args. parser = argparse.ArgumentParser() parser.add_argument('-d', '--dry', action='store_true') args = parser.parse_args() self.dry = args.dry # Read config, sanitize, fire up the API. with open( os.path.dirname(os.path.abspath(__file__)) + '/config.yaml', 'r') as stream: try: config = yaml.safe_load(stream) if 'method' not in config: config['method'] = 'current' if 'allowed_disparity' not in config: config['allowed_disparity'] = 20 if 'rules' not in config: config['rules'] = {} if 'async' not in config: config['async'] = True if 'separate' not in config['rules']: config['rules']['separate'] = {} except yaml.YAMLError as exc: print(exc) sys.exit(1) self.config = config self.proxmox = ProxmoxAPI(config['host'], user=config['user'], password=config['password'], backend='https', verify_ssl=False)
class Vmbr: """ Vmbr class defining an Proxmox vmbr """ def __init__(self, hostname, user, token_name, token_value): self.proxmox = ProxmoxAPI( hostname, user=user, token_name=token_name, token_value=token_value, verify_ssl=False, ) def create_vmbr(self, number): for i in self.proxmox.nodes.get(): self.proxmox.nodes(i["node"]).network.create( iface="vmbr" + str(number), type="bridge" ) try: self.proxmox.nodes(i["node"]).network.put() except ResourceException as e: print(str(e) + "===> Processing without reloading configuration, you will have to reboot your nodes") print("Vmbr created on " + i["node"])
def __init__(self, px_config): self.px_config = px_config self.px = ProxmoxAPI(px_config['fqdn'], user=px_config['user'], password=px_config['passwd'], verify_ssl=px_config['tls']) self.ceph_storage = self.__get_ceph_storage()
def __init__(self, vms): self.vms = vms self.proxmox = ProxmoxAPI('172.18.13.36', user='******', password='******', verify_ssl=False) self.run()
class HVInfo(): def __init__(self, host, password): self.proxmox=ProxmoxAPI(host, user='******', password=password, verify_ssl=False) def getUsers(self): """ Получить список пользователей """ result = self.proxmox.access.users.get() return result def ACL(self): result = self.proxmox.access.acl.get() return result def getNodes(self): result = self.proxmox.nodes.get() return result def getKVM(self, node): result = self.proxmox.nodes(node).qemu.get() return result def getOpenVZ(self, node): result = self.proxmox.nodes(node).openvz.get() return result
def find_vm(self, label): """Find a VM in the Proxmox cluster and return its node and vm proxy objects for extraction of additional data by other methods. @param label: the label of the VM to be compared to the VM's name in Proxmox. @raise CuckooMachineError: if the VM cannot be found.""" proxmox = ProxmoxAPI(self.options.proxmox.hostname, user=self.options.proxmox.username, password=self.options.proxmox.password, verify_ssl=False) # /cluster/resources[type=vm] will give us all VMs no matter which node # they reside on try: vms = proxmox.cluster.resources.get(type="vm") except ResourceException as e: raise CuckooMachineError("Error enumerating VMs: %s" % e) for vm in vms: if vm["name"] == label: # dynamically address # /nodes/<node>/{qemu,lxc,openvz,...}/<vmid> to get handle on # VM node = proxmox.nodes(vm["node"]) hv = node.__getattr__(vm["type"]) vm = hv.__getattr__(str(vm["vmid"])) # remember various request proxies for subsequent actions return vm, node raise CuckooMachineError("Not found")
def connect(server_url: str, username: str, *, password: Optional[str] = None, token_name: Optional[str] = None, token_value: Optional[str] = None, verify_ssl: bool = False, use_ssh: bool = False): api = ProxmoxAPI(server_url, user=username, password=password, token_name=token_name, token_value=token_value, verify_ssl=verify_ssl, backend=('ssh_paramiko' if use_ssh else 'https')) # check if API is working. a, b = api.get_tokens() if not (a or b): raise Exception( f"Failed to connect to Proxmox server '{server_url}@{username}', verify credentials" ) return api
def __init__(self, env_info, args): buttlib.common.ButtBuilder.__init__(self, env_info, args) self.__ssl_helper = buttlib.helpers.SSLHelper( self._env_info['clusterDomain'], "%s/ssl" % self._cluster_info['buttdir']) self.__net_device = "eth0" with open("butt-templates/network/network_config.yaml", 'r') as file: self.__network_config = file.read() self._cluster_info['kube_masters'] = self.get_kube_masters() self._cluster_info['etcd_hosts'] = self.get_master_hosts() self._cluster_info['etcd_initial_cluster'] = self.get_initial_cluster() self._cluster_info['master_port'] = 10443 self._cluster_info['kube_master'] = "127.0.0.1" self.__proxmox_host = self._env_info['proxmoxHost'] self.__proxmox_user = self._env_info['proxmoxUser'] self.__proxmox_storage_path = self._env_info['storagePath'] self.__proxmox_storage_name = "local" self.__proxmox_instances = {} self.__proxmox_tmpl_name = "coreosbase" self.__butt_net = env_info['bridge'] self.__max_vmid = 0 self.__proxmox = ProxmoxAPI('127.0.0.1', user='******', password='******', verify_ssl=False)
def get_openvz_hosts(ct_node): isc = ProxmoxAPI(host=ct_node, port=app.config["PROXMOX_PORT"], user=app.config["PROXMOX_USERNAME"] + '@' + app.config["PROXMOX_REALM"], password=app.config["PROXMOX_PASSWORD"], verify_ssl=False) openvz_host_details = {} try: nodes = [] for node in isc.nodes.get(): for vm in isc.nodes(node['node']).openvz.get(): pve_iface = [] for vmcfg in isc.nodes(node['node']).openvz(vm['vmid']).config.get(): if 'net' in vmcfg: pve_iface_name = vmcfg pve_iface_details = isc.nodes(node['node']).openvz(vm['vmid']).config.get()[pve_iface_name] pve_iface.append({ "pve_iface_name": pve_iface_name, "pve_iface_details": pve_iface_details }) nodes.append({ "node": node['node'], "vmid": vm['vmid'], "type": vm['type'], "name": vm['name'], "status": vm['status'], "network_info": pve_iface}) openvz_host_details.update({"status": "Host details", "vms": nodes}) return openvz_host_details except: pass
def proxmox_pci_switcher(name, config=False): """Switcher virtual machine to use one pci resource like GPU""" if config == False and os.name == 'nt': config = '~\\AppData\\Local\\proxmox-pci-switcher\\config.yaml' else: config = '~/.config/proxmox-pci-switcher/config.yaml' with open(os.path.expanduser(config)) as file: proxmox_config = yaml.load(file, Loader=yaml.FullLoader) proxmox = ProxmoxAPI(proxmox_config['proxmox']['host'], user=proxmox_config['proxmox']['user'], password=proxmox_config['proxmox']['password'], verify_ssl=proxmox_config['proxmox']['verify_ssl']) # use first node node = proxmox.nodes.get()[0] target = False for t in proxmox_config['targets']: if name == t['name']: target = t break if target: if proxmox.nodes(node['node']).qemu( target['vmid']).status('current').get()['status'] == "stopped": print(f"power on vm '{name}', see you later!") proxmox.nodes(node['node']).qemu( target['vmid']).status('start').post() else: print(f"target vm '{name}' is running.") else: print(f"vm '{name}', not found in '{config}' file.")
def ProxmoxMain(request): if not request.user.is_authenticated(): return redirect('/Login?next=%s' % request.path) proxmoxIpAddress = settings.PROXMOX_SERVER_IP_ADDRESS proxmoxUsername = settings.PROXMOX_USER proxmoxPassword = settings.PROXMOX_PASSWORD proxmoxVerifySsl = settings.PROXMOX_VERIFY_SSL proxmox = ProxmoxAPI(proxmoxIpAddress, user=proxmoxUsername, password=proxmoxPassword, verify_ssl=proxmoxVerifySsl) command = request.GET.get('command', 'none') currentNode = request.GET.get('node', 'none') currentVz = request.GET.get('openvz', 'none') currentVm = request.GET.get('qemu', 'none') if command != 'none': return runcommand(request, proxmox) elif currentVz != 'none': vms = getVmDict(proxmox) tasks = getAllServerTasks(proxmox) return render(request, 'OctaHomeProxmox/CT.html', {'links': getSideBar(request, proxmox), 'serverInfo':addWizardVeriables(request, proxmox), 'node':currentNode, 'Vz':vms[currentNode][currentVz], 'StorageDevices':getStorageDetails(proxmox), 'ServerStatuses':getServerStatuses(proxmox), 'tasks':tasks}) elif currentVm != 'none': raise Http404 elif currentNode != 'none': tasks = proxmox.nodes(currentNode).get('tasks') return render(request, 'OctaHomeProxmox/Node.html', {'links': getSideBar(request, proxmox), 'serverInfo':addWizardVeriables(request, proxmox), 'Node':currentNode, 'StorageDevices':getStorageDetails(proxmox), 'ServerStatuses':getServerStatuses(proxmox), 'tasks':tasks}) else: tasks = getAllServerTasks(proxmox) return render(request, 'OctaHomeProxmox/AllNodes.html', {'links': getSideBar(request, proxmox), 'serverInfo':addWizardVeriables(request, proxmox), 'StorageDevices':getStorageDetails(proxmox), 'ServerStatuses':getServerStatuses(proxmox), 'tasks':tasks}) raise Http404
def __init__(self, *args, **kwargs): super(CD_DVD, self).__init__(*args,**kwargs) isos = [] proxmox = ProxmoxAPI(secrets.PROXMOX_HOST,user=secrets.PROXMOX_USER,password=secrets.PROXMOX_PASS,verify_ssl=False) for item in proxmox.nodes('proxmox01').storage('NFS-ISOs').content.get(): isos.append(item['volid']) self.fields['iso'] = forms.ChoiceField(choices=[(iso,iso) for iso in isos])
def get_kvm_hosts(ct_node): isc = ProxmoxAPI(host=ct_node, port=app.config["PROXMOX_PORT"], user=app.config["PROXMOX_USERNAME"] + '@' + app.config["PROXMOX_REALM"], password=app.config["PROXMOX_PASSWORD"], verify_ssl=False) for node in isc.nodes.get(): for vm in isc.nodes(node['node']).qemu.get(): print("{0}. {1} => {2}".format(vm['vmid'], vm['name'], vm['status'])) return None
def test_https_api_token(): p = ProxmoxAPI('proxmox', user='******', token_name='test', token_value='ab27beeb-9ac4-4df1-aa19-62639f27031e', verify_ssl=False) eq_(p.get_tokens()[0], None) eq_(p.get_tokens()[1], None)
def _connect_proxmox( self ): self.proxmox = ProxmoxAPI( self.host, user=self.username, password=self.password, verify_ssl=False) self.nodes = self.proxmox.nodes() if len(self.nodes.get()) == 0: raise CuckooCriticalError( "Proxmox found no Nodes.") self._verifyNodes() self.vm_label_id = self._getVMID()
def __init__(self, hostname, user, token_name, token_value): self.proxmox = ProxmoxAPI( hostname, user=user, token_name=token_name, token_value=token_value, verify_ssl=False, )
def __init__(self) -> None: super().__init__() log.info(f"Proxmox host: {PROXMOX_API_HOST}") log.info(f"Proxmox user: {PROXMOX_API_USER}") self.pve = ProxmoxAPI(host=PROXMOX_API_HOST, user=PROXMOX_API_USER, password=PROXMOX_API_PASSWORD, verify_ssl=PROXMOX_API_VERIFY_SSL)
def setUp(self, session): response = {'ticket': 'ticket', 'CSRFPreventionToken': 'CSRFPreventionToken'} session.request.return_value = response self.proxmox = ProxmoxAPI('proxmox', user='******', password='******', port=123, verify_ssl=False) self.serializer = MagicMock() self.session = MagicMock() self.session.request.return_value.status_code = 200 self.proxmox._store['session'] = self.session self.proxmox._store['serializer'] = self.serializer
class ProxmoxKvmChaotic(Chaotic): def __init__(self) -> None: super().__init__() log.info(f"Proxmox host: {PROXMOX_API_HOST}") log.info(f"Proxmox user: {PROXMOX_API_USER}") self.pve = ProxmoxAPI(host=PROXMOX_API_HOST, user=PROXMOX_API_USER, password=PROXMOX_API_PASSWORD, verify_ssl=PROXMOX_API_VERIFY_SSL) def action(self) -> None: vms = self.pve.cluster.resources.get(type='vm') denylist = self.configs.get('denylist') or [] vms = [ vm for vm in vms if vm['status'] == "running" and vm['name'] not in denylist ] if vms: vm = random.choice(vms) log.info( f"Choose VM ID={vm['vmid']}, name={vm['name']} on node={vm['node']}" ) min_uptime = self.configs.get('min_uptime') if min_uptime is not None: current = self.pve.nodes(vm['node']).qemu( vm['vmid']).status.current.get() required_uptime = min_uptime * 60 if current['uptime'] < required_uptime: log.info( f"VM {vm['name']} required uptime lower then {min_uptime} min: {current['uptime'] / 60:.2f}, skipping" ) log.info(f"done") return if not self.dry_run: log.info(f"Stopping VM {vm['name']}") self.pve.nodes(vm['node']).qemu( vm['vmid']).status.shutdown.post(forceStop=1) wait_before_restart = int( self.configs.get('wait_before_restart', 60)) log.info(f"Sleeping for {wait_before_restart} seconds") time.sleep(wait_before_restart) log.info(f"Starting VM {vm['name']}") self.pve.nodes(vm['node']).qemu(vm['vmid']).status.start.post() else: log.info("No VMs found") log.info(f"done")
def __init__(self, host, user, password, backend='https', verify_ssl=False): self.client = ProxmoxAPI(host=host, user=user, password=password, backend=backend, verify_ssl=verify_ssl)
def __init__(self, proxmox_host, password, user: str = 'root@pam', verify_ssl=True, port=443): self.prox = ProxmoxAPI(proxmox_host, user=user, password=password, port=port, verify_ssl=verify_ssl)
def setup_conn(self): if self._conn is None: try: self._conn = ProxmoxAPI(host=PROXMOX_HOST, user='******'.format(PROXMOX_USER), password=PROXMOX_PASS, verify_ssl=False) logger.debug('connected to the "{}"'.format(PROXMOX_HOST)) except ConnectionError as e: logger.critical( '"{}" while connecting to the proxmox'.format(e))
def get_nodes(host, *args, **kwargs): """Get and returns a list of all nodes on the PVE cluster. """ pve = ProxmoxAPI(host, *args, **kwargs) nodes = pve.nodes.get() # Add to this list to get more info on node. properties = ['network', 'services'] for node in nodes: for p in properties: node[p] = pve.nodes(node['node']).get(p) return nodes
def __init__(self, *args, **kwargs): super(CD_DVD, self).__init__(*args, **kwargs) isos = [] proxmox = ProxmoxAPI(secrets.PROXMOX_HOST, user=secrets.PROXMOX_USER, password=secrets.PROXMOX_PASS, verify_ssl=False) for item in proxmox.nodes('proxmox01').storage( 'NFS-ISOs').content.get(): isos.append(item['volid']) self.fields['iso'] = forms.ChoiceField(choices=[(iso, iso) for iso in isos])
def rebuildlxc(host): """ Creates lxc in proxmox using given hostresource configuration """ #check if container is running and ask user to stop it if not exists(host): raise ValueError("Host template is missing. Please create host template") container = Container.getContainer(HOST_CONTAINER) hostresource = container.loadResource(host) #get proxmox user and hypervisor userresource = proxmoxutil.listuser() if userresource is None: raise ValueError("No proxmox user found!! Please use proxmoxutil command to update user credentials") user = userresource.properties[PROPERTIES_USER] password = userresource.properties[PROPERTIES_PASSWORD] authrealm = userresource.properties[PROPERTIES_AUTHREALM] puser = user+'@'+authrealm primary = proxmoxutil.listprimary() if primary is None: raise ValueError("Primary proxmox hypervisor not found!! Please use proxmoxutil command to update primary hypervisor") hypervisor = primary.properties[PROPERTIES_HYPERVISOR] print "Authenticating "+puser +" on "+ hypervisor proxmox = ProxmoxAPI(hypervisor, user=puser, password=password, verify_ssl=False) node = proxmox.nodes(hostresource.properties[HYPERVISOR]) hostname = hostresource.properties[HOSTNAME] vmid = int(hostresource.properties[HOSTID]) memory = int(hostresource.properties[PROPERTIES_MEMORY]) swap = int(hostresource.properties[PROPERTIES_SWAP]) interfaces = hostresource.properties[INTERFACES] i=0 netconfig = dict() for interface in interfaces: print "Configuring %s" %interface netconfig["net"+str(i)] = hostresource.properties[interface] i=i+1 print "Reconfiguring LXC with the following parameters:" print "Vmid: %d" %vmid print "Memory: %d" %memory print "Swap: %d" %swap node.lxc(vmid).config.put(memory=memory, swap=swap, **netconfig) print "Reconfiguring LXC....." time.sleep(30)
def main(): module = AnsibleModule( argument_spec=dict(args=dict(type='str', default=None), api_host=dict(required=True), api_user=dict(required=True), api_password=dict(no_log=True), validate_certs=dict(type='bool', default='no'), node=dict(type='str', default='no'))) api_user = module.params['api_user'] api_host = module.params['api_host'] api_password = module.params['api_password'] validate_certs = module.params['validate_certs'] node = module.params['node'] try: proxmox = ProxmoxAPI(api_host, user=api_user, password=api_password, verify_ssl=validate_certs) global VZ_TYPE global PVE_MAJOR_VERSION PVE_MAJOR_VERSION = 3 if proxmox_version(proxmox) < LooseVersion( '4.0') else 4 except Exception as e: module.fail_json( msg='authorization on proxmox cluster failed with exception: %s' % e) try: node = proxmox.nodes(node) vms = node.qemu.get() except Exception as e: module.fail_json( msg='Getting information for VMs failed with exception: %s' % e) vm_list = [] vm_dict = {} for vm in vms: try: vm_dict[vm['name']] = node.qemu(vm['vmid']).config.get() except Exception as e: module.fail_json( msg= 'Getting information for VM with vmid = %s failed with exception: %s' % (vmid, e)) vm_dict[vm['name']]['vmid'] = vm['vmid'] response = vm_dict module.exit_json(changed=False, vm_configs=response)
def __init__(self, backend, host, user, **kwargs): """A wrapper object for ProxmoxAPI. :param backend: the backend of proxmoxer :param host: the host of proxmox server :param user: the api user name of proxmox server (default 'root') :param kwargs: other args ProxmoxAPI accepted :returns: ProxmoxBroker -- ProxmoxBroker object for module usage. """ # here timeout is task timeout, not connection timeout self.timeout = kwargs.pop('timeout', 30) self.backend = backend self.proxmox = ProxmoxAPI(host, backend=backend, user=user, **kwargs)
def main(): module = AnsibleModule( argument_spec=dict(args=dict(type='str', default=None), api_host=dict(required=True), api_user=dict(required=True), api_password=dict(no_log=True), validate_certs=dict(type='bool', default='no'), node=dict(type='str', default='no'), vm_id=dict(type='int', required=True), vm_ip_type=dict(type='str', default='ipv4'), vm_interface=dict(type='str', default='eth0'))) api_user = module.params['api_user'] api_host = module.params['api_host'] api_password = module.params['api_password'] validate_certs = module.params['validate_certs'] node = module.params['node'] vm_id = module.params['vm_id'] vm_ip_type = module.params['vm_ip_type'] vm_interface = module.params['vm_interface'] try: proxmox = ProxmoxAPI(api_host, user=api_user, password=api_password, verify_ssl=validate_certs) global VZ_TYPE global PVE_MAJOR_VERSION PVE_MAJOR_VERSION = 3 if proxmox_version(proxmox) < LooseVersion( '4.0') else 4 except Exception as e: module.fail_json( msg='authorization on proxmox cluster failed with exception: %s' % e) try: vm_ip = proxmox.nodes(node).qemu(vm_id).agent( 'network-get-interfaces').get() except Exception as e: module.fail_json( msg='Getting IP for VM with vmid %s failed with exception: %s' % (vm_id, e)) vm_ip_info = [i for i in vm_ip['result'] if i['name'] == vm_interface] vm_ip4_addr = [ i for i in vm_ip_info[0]['ip-addresses'] if i['ip-address-type'] == vm_ip_type ][0]['ip-address'] response = vm_ip4_addr module.exit_json(changed=False, ip=response)
class Proxmox(App): """ Skeleton example app to build other apps off of Args: app_name (str): Name of the app device (list[str]): List of associated device names context (dict): Information about the context in which the App is operating """ def __init__(self, app_name, device, context): App.__init__(self, app_name, device, context) # Required to call superconstructor self.proxmox = ProxmoxAPI(self.host, user=self.device_fields["username"], password=self.device.get_encrypted_field("password"), verify_ssl=False) @action def get_all_nodes(self): return self.proxmox.nodes.get() @action def get_all_vms(self): nodes_vms = [] for node in self.proxmox.nodes.get(): node['vms'] = [] for vm in self.proxmox.nodes(node['node']).openvz.get(): node['vms'].append(vm) nodes_vms.append(node) @action def get_all_vms_for_node(self, node_name): for node in self.proxmox.nodes.get():
def _connnect(self, module): api_host = module.params['api_host'] api_user = module.params['api_user'] api_password = module.params['api_password'] validate_certs = module.params['validate_certs'] if not api_host: module.fail_json(msg='The api_host paramater is missing.' 'Please specify this paramter in the task or' 'use the environment variable PROXMOX_HOST') if not api_user: module.fail_json(msg='The api_user paramater is missing.' 'Please specify this paramter in the task or' 'use the environment variable PROXMOX_USER') if not api_password: module.fail_json(msg='The api_password paramater is missing.' 'Please specify this paramter in the task or' 'use the environment variable PROXMOX_PASSWORD') try: return ProxmoxAPI(api_host, user=api_user, password=api_password, verify_ssl=validate_certs) except Exception as e: module.fail_json(msg='%s' % e, exception=traceback.format_exc())
def __proxmox_login(self,system_id,context): """Login to proxmox server.""" api_host = context.get('power_address') api_user = context.get('power_user') api_password = context.get('power_pass') vm_id = context.get('power_vm_name') api_ssl_val = (context.get('power_validate_ssl')==PROXMOX_YES) try: api = ProxmoxAPI(api_host, user=api_user, password=api_password, verify_ssl=api_ssl_val) con_vm=None for vm in api.cluster.resources.get(type="vm"): if (str(vm['vmid'])==vm_id) or (vm['name']==vm_id): con_vm=vm break except Exception: raise ProxmoxError( "Can't connect to proxmox cluster %s" % (api_host)) if con_vm is None: """vm not found""" raise ProxmoxError( "Virtual machine %s not found on proxmox cluster %s" % (vm_id, api_host)) #extract node object vm_obj=getattr(getattr(getattr(api.nodes,con_vm['node']), con_vm['type']), str(con_vm['vmid'])) return vm_obj
def setUp(self, _): super(TestParamikoSuiteWithSudo, self).__init__(sudo=True) self.proxmox = ProxmoxAPI('proxmox', user='******', backend='ssh_paramiko', port=123, sudo=True) self.client = self.proxmox._store['session'].ssh_client self.session = self.client.get_transport().open_session() self._set_stderr('200 OK') self._set_stdout('')
def call_function(f: Callable): parser = argh.helpers.ArghParser() parser.add_argument( 'config_file', type=argparse.FileType('r'), help='path to config file with PVE credentials', ) parser.add_argument( '--section', help='config section to use', action='append', ) parser.set_default_command( argh.arg( '--pve', help=argparse.SUPPRESS, default=argparse.SUPPRESS, )(f)) args = parser.parse_args() var_args = dict(vars(args)) del var_args['config_file'] del var_args['section'] del var_args['_functions_stack'] import configparser config = configparser.ConfigParser() config.read_file(args.config_file) problems = tuple( itertools.chain.from_iterable( f(ProxmoxAPI(**config[section]), **var_args) for section in args.section or config.sections())) handle_result(merge_results(problems))
def test_paramiko_invalid_backend(_): with assert_raises(NotImplementedError): ProxmoxAPI('proxmox', user='******', backend='ssh_paramiko', port=123, service='PBS')
def setUp(self, session): response = {"ticket": "ticket", "CSRFPreventionToken": "CSRFPreventionToken"} session.request.return_value = response self.proxmox = ProxmoxAPI("proxmox", user="******", password="******", port=123, verify_ssl=False) self.serializer = MagicMock() self.session = MagicMock() self.session.request.return_value.status_code = 200 self.proxmox._store["session"] = self.session self.proxmox._store["serializer"] = self.serializer
def create_vm(request): vm_form = VM_Form(data=request.POST or None) drive_form = CD_DVD(data=request.POST or None) disk_form = Disk(data=request.POST or None) cpu_form = CPU(data=request.POST or None) net_form = Network(data=request.POST or None) if request.method == 'POST': if vm_form.is_valid() and drive_form.is_valid() and disk_form.is_valid() and cpu_form.is_valid() and net_form.is_valid(): if '_request' in request.POST: # Request VM use = User.objects.get_or_create(username='******') request_vm(vm_form, drive_form, disk_form, cpu_form, net_form,use[0]) return redirect('/') if check_limits(vm_form.cleaned_data['memory'],cpu_form.cleaned_data['cores'],disk_form.cleaned_data['size']): return render(request, 'create.html',{'vm_form': vm_form,'drive_form': drive_form,'disk_form': disk_form,'cpu_form': cpu_form,'net_form': net_form,'request_vm': True}) proxmox = ProxmoxAPI(secrets.PROXMOX_HOST,user=secrets.PROXMOX_USER,password=secrets.PROXMOX_PASS,verify_ssl=False) node = proxmox.nodes(vm_form.cleaned_data['node']) vm_id = int(proxmox.cluster.nextid.get()) testdata = node.qemu.create(vmid=vm_id, name=vm_form.cleaned_data['name'], ostype=vm_form.cleaned_data['ostype'], ide2=drive_form.cleaned_data['iso']+',media=cdrom', ide0='ceph_pool:'+str(disk_form.cleaned_data['size'])+',format='+disk_form.cleaned_data['disk_format'], sockets=1, cores=cpu_form.cleaned_data['cores'], numa=0, pool=secrets.PROXMOX_POOL, memory=vm_form.cleaned_data['memory'], net0=net_form.cleaned_data['model']+',bridge='+net_form.cleaned_data['bridge']) # Testing use = User.objects.get_or_create(username='******') vm = VM(user=use[0],vmid=vm_id,name=vm_form.cleaned_data['name'],nodename=vm_form.cleaned_data['node']) vm.save() return redirect('/manage/') return render(request, 'create.html',{'vm_form': vm_form,'drive_form': drive_form,'disk_form': disk_form,'cpu_form': cpu_form,'net_form': net_form})
class TestSuite: proxmox = None serializer = None session = None # noinspection PyMethodOverriding @patch("requests.sessions.Session") def setUp(self, session): response = {"ticket": "ticket", "CSRFPreventionToken": "CSRFPreventionToken"} session.request.return_value = response self.proxmox = ProxmoxAPI("proxmox", user="******", password="******", port=123, verify_ssl=False) self.serializer = MagicMock() self.session = MagicMock() self.session.request.return_value.status_code = 200 self.proxmox._store["session"] = self.session self.proxmox._store["serializer"] = self.serializer def test_get(self): self.proxmox.nodes("proxmox").storage("local").get() eq_(self.session.request.call_args[0], ("GET", "https://proxmox:123/api2/json/nodes/proxmox/storage/local")) def test_delete(self): self.proxmox.nodes("proxmox").openvz(100).delete() eq_(self.session.request.call_args[0], ("DELETE", "https://proxmox:123/api2/json/nodes/proxmox/openvz/100")) self.proxmox.nodes("proxmox").openvz("101").delete() eq_(self.session.request.call_args[0], ("DELETE", "https://proxmox:123/api2/json/nodes/proxmox/openvz/101")) def test_post(self): node = self.proxmox.nodes("proxmox") node.openvz.create( vmid=800, ostemplate="local:vztmpl/debian-6-turnkey-core_12.0-1_i386.tar.gz", hostname="test", storage="local", memory=512, swap=512, cpus=1, disk=4, password="******", ip_address="10.0.100.222", ) eq_(self.session.request.call_args[0], ("POST", "https://proxmox:123/api2/json/nodes/proxmox/openvz")) ok_("data" in self.session.request.call_args[1]) data = self.session.request.call_args[1]["data"] eq_(data["cpus"], 1) eq_(data["disk"], 4) eq_(data["hostname"], "test") eq_(data["ip_address"], "10.0.100.222") eq_(data["memory"], 512) eq_(data["ostemplate"], "local:vztmpl/debian-6-turnkey-core_12.0-1_i386.tar.gz") eq_(data["password"], "secret") eq_(data["storage"], "local") eq_(data["swap"], 512) eq_(data["vmid"], 800) node = self.proxmox.nodes("proxmox1") node.openvz.post( vmid=900, ostemplate="local:vztmpl/debian-7-turnkey-core_12.0-1_i386.tar.gz", hostname="test1", storage="local1", memory=1024, swap=1024, cpus=2, disk=8, password="******", ip_address="10.0.100.111", ) eq_(self.session.request.call_args[0], ("POST", "https://proxmox:123/api2/json/nodes/proxmox1/openvz")) ok_("data" in self.session.request.call_args[1]) data = self.session.request.call_args[1]["data"] eq_(data["cpus"], 2) eq_(data["disk"], 8) eq_(data["hostname"], "test1") eq_(data["ip_address"], "10.0.100.111") eq_(data["memory"], 1024) eq_(data["ostemplate"], "local:vztmpl/debian-7-turnkey-core_12.0-1_i386.tar.gz") eq_(data["password"], "secret1") eq_(data["storage"], "local1") eq_(data["swap"], 1024) eq_(data["vmid"], 900) def test_put(self): node = self.proxmox.nodes("proxmox") node.openvz(101).config.set(cpus=4, memory=1024, ip_address="10.0.100.100", onboot=True) eq_(self.session.request.call_args[0], ("PUT", "https://proxmox:123/api2/json/nodes/proxmox/openvz/101/config")) data = self.session.request.call_args[1]["data"] eq_(data["cpus"], 4) eq_(data["memory"], 1024) eq_(data["ip_address"], "10.0.100.100") eq_(data["onboot"], True) node = self.proxmox.nodes("proxmox1") node.openvz(102).config.put(cpus=2, memory=512, ip_address="10.0.100.200", onboot=False) eq_( self.session.request.call_args[0], ("PUT", "https://proxmox:123/api2/json/nodes/proxmox1/openvz/102/config") ) data = self.session.request.call_args[1]["data"] eq_(data["cpus"], 2) eq_(data["memory"], 512) eq_(data["ip_address"], "10.0.100.200") eq_(data["onboot"], False)
from proxmoxer import ProxmoxAPI # apiuser: "******" # apipass: "******" # cluster: # - "cluster1" # - "cluster2" pve_config = yaml.load(open('/root/proxmox/proxmox.yml', 'r')) pve_apiuser = pve_config['apiuser'] pve_apipass = pve_config['apipass'] pve_cluster = pve_config['cluster'] print('{cluster:<16} {node:<16} {id:<4} {name:<16} {status:<12}'.format( cluster="CLUSTER", node="NODE", id="ID", name="NAME", status="STATUS")) for cluster in pve_cluster: pve = ProxmoxAPI(cluster, user=pve_apiuser, password=pve_apipass, verify_ssl=False) for node in pve.nodes.get(): for vm in pve.nodes(node['node']).qemu.get(): print('{cluster:<16} {node:<16} {id:<4} {name:<16} {status:<12}'.format( cluster=cluster, node=node['node'], id=vm['vmid'], name=vm['name'], status=vm['status']))
def main(): module = AnsibleModule( argument_spec=dict( acpi=dict(type='bool', default='yes'), agent=dict(type='bool'), args=dict(type='str', default=None), api_host=dict(required=True), api_user=dict(required=True), api_password=dict(no_log=True), autostart=dict(type='bool', default='no'), balloon=dict(type='int', default=0), bios=dict(choices=['seabios', 'ovmf']), boot=dict(type='str', default='cnd'), bootdisk=dict(type='str'), clone=dict(type='str', default=None), cores=dict(type='int', default=1), cpu=dict(type='str', default='kvm64'), cpulimit=dict(type='int'), cpuunits=dict(type='int', default=1000), delete=dict(type='str', default=None), description=dict(type='str'), digest=dict(type='str'), force=dict(type='bool', default=None), format=dict(type='str', default='qcow2', choices=['cloop', 'cow', 'qcow', 'qcow2', 'qed', 'raw', 'vmdk']), freeze=dict(type='bool'), full=dict(type='bool', default='yes'), hostpci=dict(type='dict'), hotplug=dict(type='str'), hugepages=dict(choices=['any', '2', '1024']), ide=dict(type='dict', default=None), keyboard=dict(type='str'), kvm=dict(type='bool', default='yes'), localtime=dict(type='bool'), lock=dict(choices=['migrate', 'backup', 'snapshot', 'rollback']), machine=dict(type='str'), memory=dict(type='int', default=512), migrate_downtime=dict(type='int'), migrate_speed=dict(type='int'), name=dict(type='str'), net=dict(type='dict'), newid=dict(type='int', default=None), node=dict(), numa=dict(type='dict'), numa_enabled=dict(type='bool'), onboot=dict(type='bool', default='yes'), ostype=dict(default='l26', choices=['other', 'wxp', 'w2k', 'w2k3', 'w2k8', 'wvista', 'win7', 'win8', 'l24', 'l26', 'solaris']), parallel=dict(type='dict'), pool=dict(type='str'), protection=dict(type='bool'), reboot=dict(type='bool'), revert=dict(type='str', default=None), sata=dict(type='dict'), scsi=dict(type='dict'), scsihw=dict(choices=['lsi', 'lsi53c810', 'virtio-scsi-pci', 'virtio-scsi-single', 'megasas', 'pvscsi']), serial=dict(type='dict'), shares=dict(type='int'), skiplock=dict(type='bool'), smbios=dict(type='str'), snapname=dict(type='str'), sockets=dict(type='int', default=1), startdate=dict(type='str'), startup=dict(), state=dict(default='present', choices=['present', 'absent', 'stopped', 'started', 'restarted', 'current']), storage=dict(type='str'), tablet=dict(type='bool', default='no'), target=dict(type='str'), tdf=dict(type='bool'), template=dict(type='bool', default='no'), timeout=dict(type='int', default=30), update=dict(type='bool', default='no'), validate_certs=dict(type='bool', default='no'), vcpus=dict(type='int', default=None), vga=dict(default='std', choices=['std', 'cirrus', 'vmware', 'qxl', 'serial0', 'serial1', 'serial2', 'serial3', 'qxl2', 'qxl3', 'qxl4']), virtio=dict(type='dict', default=None), vmid=dict(type='int', default=None), watchdog=dict(), ), mutually_exclusive=[('delete', 'revert'), ('delete', 'update'), ('revert', 'update'), ('clone', 'update'), ('clone', 'delete'), ('clone', 'revert')], required_one_of=[('name', 'vmid',)], required_if=[('state', 'present', ['node'])] ) if not HAS_PROXMOXER: module.fail_json(msg='proxmoxer required for this module') api_user = module.params['api_user'] api_host = module.params['api_host'] api_password = module.params['api_password'] clone = module.params['clone'] cpu = module.params['cpu'] cores = module.params['cores'] delete = module.params['delete'] memory = module.params['memory'] name = module.params['name'] newid = module.params['newid'] node = module.params['node'] revert = module.params['revert'] sockets = module.params['sockets'] state = module.params['state'] timeout = module.params['timeout'] update = bool(module.params['update']) vmid = module.params['vmid'] validate_certs = module.params['validate_certs'] # If password not set get it from PROXMOX_PASSWORD env if not api_password: try: api_password = os.environ['PROXMOX_PASSWORD'] except KeyError as e: module.fail_json(msg='You should set api_password param or use PROXMOX_PASSWORD environment variable') try: proxmox = ProxmoxAPI(api_host, user=api_user, password=api_password, verify_ssl=validate_certs) global VZ_TYPE global PVE_MAJOR_VERSION PVE_MAJOR_VERSION = 3 if float(proxmox.version.get()['version']) < 4.0 else 4 except Exception as e: module.fail_json(msg='authorization on proxmox cluster failed with exception: %s' % e) # If vmid not set get the Next VM id from ProxmoxAPI # If vm name is set get the VM id from ProxmoxAPI if not vmid: if state == 'present' and (not update and not clone) and (not delete and not revert): try: vmid = get_nextvmid(module, proxmox) except Exception as e: module.fail_json(msg="Can't get the next vimd for VM {} automatically. Ensure your cluster state is good".format(name)) else: try: if not clone: vmid = get_vmid(proxmox, name)[0] else: vmid = get_vmid(proxmox, clone)[0] except Exception as e: if not clone: module.fail_json(msg="VM {} does not exist in cluster.".format(name)) else: module.fail_json(msg="VM {} does not exist in cluster.".format(clone)) if clone is not None: if get_vmid(proxmox, name): module.exit_json(changed=False, msg="VM with name <%s> already exists" % name) if vmid is not None: vm = get_vm(proxmox, vmid) if not vm: module.fail_json(msg='VM with vmid = %s does not exist in cluster' % vmid) if not newid: try: newid = get_nextvmid(module, proxmox) except Exception as e: module.fail_json(msg="Can't get the next vimd for VM {} automatically. Ensure your cluster state is good".format(name)) else: vm = get_vm(proxmox, newid) if vm: module.exit_json(changed=False, msg="vmid %s with VM name %s already exists" % (newid, name)) if delete is not None: try: settings(module, proxmox, vmid, node, name, timeout, delete=delete) module.exit_json(changed=True, msg="Settings has deleted on VM {} with vmid {}".format(name, vmid)) except Exception as e: module.fail_json(msg='Unable to delete settings on VM {} with vimd {}: '.format(name, vmid) + str(e)) elif revert is not None: try: settings(module, proxmox, vmid, node, name, timeout, revert=revert) module.exit_json(changed=True, msg="Settings has reverted on VM {} with vmid {}".format(name, vmid)) except Exception as e: module.fail_json(msg='Unable to revert settings on VM {} with vimd {}: Maybe is not a pending task... '.format(name, vmid) + str(e)) if state == 'present': try: if get_vm(proxmox, vmid) and not (update or clone): module.exit_json(changed=False, msg="VM with vmid <%s> already exists" % vmid) elif get_vmid(proxmox, name) and not (update or clone): module.exit_json(changed=False, msg="VM with name <%s> already exists" % name) elif not (node, name): module.fail_json(msg='node, name is mandatory for creating/updating vm') elif not node_check(proxmox, node): module.fail_json(msg="node '%s' does not exist in cluster" % node) create_vm(module, proxmox, vmid, newid, node, name, memory, cpu, cores, sockets, timeout, update, acpi=module.params['acpi'], agent=module.params['agent'], autostart=module.params['autostart'], balloon=module.params['balloon'], bios=module.params['bios'], boot=module.params['boot'], bootdisk=module.params['bootdisk'], cpulimit=module.params['cpulimit'], cpuunits=module.params['cpuunits'], description=module.params['description'], digest=module.params['digest'], force=module.params['force'], freeze=module.params['freeze'], hostpci=module.params['hostpci'], hotplug=module.params['hotplug'], hugepages=module.params['hugepages'], ide=module.params['ide'], keyboard=module.params['keyboard'], kvm=module.params['kvm'], localtime=module.params['localtime'], lock=module.params['lock'], machine=module.params['machine'], migrate_downtime=module.params['migrate_downtime'], migrate_speed=module.params['migrate_speed'], net=module.params['net'], numa=module.params['numa'], numa_enabled=module.params['numa_enabled'], onboot=module.params['onboot'], ostype=module.params['ostype'], parallel=module.params['parallel'], pool=module.params['pool'], protection=module.params['protection'], reboot=module.params['reboot'], sata=module.params['sata'], scsi=module.params['scsi'], scsihw=module.params['scsihw'], serial=module.params['serial'], shares=module.params['shares'], skiplock=module.params['skiplock'], smbios1=module.params['smbios'], snapname=module.params['snapname'], startdate=module.params['startdate'], startup=module.params['startup'], tablet=module.params['tablet'], target=module.params['target'], tdf=module.params['tdf'], template=module.params['template'], vcpus=module.params['vcpus'], vga=module.params['vga'], virtio=module.params['virtio'], watchdog=module.params['watchdog']) if not clone: get_vminfo(module, proxmox, node, vmid, ide=module.params['ide'], net=module.params['net'], sata=module.params['sata'], scsi=module.params['scsi'], virtio=module.params['virtio']) if update: module.exit_json(changed=True, msg="VM %s with vmid %s updated" % (name, vmid)) elif clone is not None: module.exit_json(changed=True, msg="VM %s with newid %s cloned from vm with vmid %s" % (name, newid, vmid)) else: module.exit_json(changed=True, msg="VM %s with vmid %s deployed" % (name, vmid), **results) except Exception as e: if update: module.fail_json(msg="Unable to update vm {} with vimd {}=".format(name, vmid) + str(e)) elif clone is not None: module.fail_json(msg="Unable to clone vm {} from vimd {}=".format(name, vmid) + str(e)) else: module.fail_json(msg="creation of %s VM %s with vmid %s failed with exception=%s" % (VZ_TYPE, name, vmid, e)) elif state == 'started': try: vm = get_vm(proxmox, vmid) if not vm: module.fail_json(msg='VM with vmid <%s> does not exist in cluster' % vmid) if getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'running': module.exit_json(changed=False, msg="VM %s is already running" % vmid) if start_vm(module, proxmox, vm, vmid, timeout): module.exit_json(changed=True, msg="VM %s started" % vmid) except Exception as e: module.fail_json(msg="starting of VM %s failed with exception: %s" % (vmid, e)) elif state == 'stopped': try: vm = get_vm(proxmox, vmid) if not vm: module.fail_json(msg='VM with vmid = %s does not exist in cluster' % vmid) if getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'stopped': module.exit_json(changed=False, msg="VM %s is already stopped" % vmid) if stop_vm(module, proxmox, vm, vmid, timeout, force=module.params['force']): module.exit_json(changed=True, msg="VM %s is shutting down" % vmid) except Exception as e: module.fail_json(msg="stopping of VM %s failed with exception: %s" % (vmid, e)) elif state == 'restarted': try: vm = get_vm(proxmox, vmid) if not vm: module.fail_json(msg='VM with vmid = %s does not exist in cluster' % vmid) if getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'stopped': module.exit_json(changed=False, msg="VM %s is not running" % vmid) if stop_vm(module, proxmox, vm, vmid, timeout, force=module.params['force']) and start_vm(module, proxmox, vm, vmid, timeout): module.exit_json(changed=True, msg="VM %s is restarted" % vmid) except Exception as e: module.fail_json(msg="restarting of VM %s failed with exception: %s" % (vmid, e)) elif state == 'absent': try: vm = get_vm(proxmox, vmid) if not vm: module.exit_json(changed=False, msg="VM %s does not exist" % vmid) if getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'running': module.exit_json(changed=False, msg="VM %s is running. Stop it before deletion." % vmid) taskid = getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE).delete(vmid) while timeout: if (proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['status'] == 'stopped' and proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['exitstatus'] == 'OK'): module.exit_json(changed=True, msg="VM %s removed" % vmid) timeout -= 1 if timeout == 0: module.fail_json(msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s' % proxmox.nodes(vm[0]['node']).tasks(taskid).log.get()[:1]) time.sleep(1) except Exception as e: module.fail_json(msg="deletion of VM %s failed with exception: %s" % (vmid, e)) elif state == 'current': status = {} try: vm = get_vm(proxmox, vmid) if not vm: module.fail_json(msg='VM with vmid = %s does not exist in cluster' % vmid) current = getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] status['status'] = current if status: module.exit_json(changed=False, msg="VM %s with vmid = %s is %s" % (name, vmid, current), **status) except Exception as e: module.fail_json(msg="Unable to get vm {} with vmid = {} status: ".format(name, vmid) + str(e))
def connect(self, hostname, password, nodename): self.proxmox = ProxmoxAPI(hostname, user='******', password=password, verify_ssl=False) self.node = self.proxmox.nodes(nodename) self.password = pwgen()
def main(): ''' Example configuration file: apiuser: "******" apipass: "******" cluster: - "cluster1" - "cluster2" ''' conf_path = os.path.dirname(os.path.abspath(__file__)) + r'/../conf/proxmox.yml' try: pve_config = yaml.load(open(conf_path, 'r')) pve_apiuser = pve_config['apiuser'] pve_apipass = pve_config['apipass'] pve_cluster = pve_config['cluster'] except: print('{0} ERROR: Unable to load configuration from {1}'.format( str(datetime.now()), conf_path)) return 1 parser = argparse.ArgumentParser(description='Backup a VM by name') parser.add_argument('vmname', nargs='+', help='VM to Backup') parser.add_argument('--target', required=True, help='Backup Location') parser.add_argument('--notify', required=True, help='Notification Email Address') args = parser.parse_args() # Track all VMs that are backed up backup_list = set() for cluster in pve_cluster: try: pve = ProxmoxAPI(cluster, user=pve_apiuser, password=pve_apipass, verify_ssl=False) except: print('{0} ERROR: Unable to access cluster {1}. Skipping!'.format( str(datetime.now()), cluster)) continue for node in pve.nodes.get(): target_exists = False # Track all VMs backed up on a node backup_list_node = set() backup_list_node_vmid = set() for target in pve.nodes(node['node']).storage.get(): if target['storage'] == args.target: target_exists = True if target_exists == True: for vm in pve.nodes(node['node']).qemu.get(): if vm['name'] in args.vmname: backup_list.add(vm['name']) backup_list_node.add(vm['name']) backup_list_node_vmid.add(vm['vmid']) if len(backup_list_node) >= 1: backup_vmname = str() backup_vmid = str() for vmid in backup_list_node_vmid: backup_vmid = backup_vmid + '{0},'.format(vmid) for name in backup_list_node: backup_vmname = backup_vmname + ' {0}'.format(name) try: print('{0} INFO: Starting backup(s) for {1} on node {2}'.format( str(datetime.now()), backup_vmname.strip().upper(), node['node'])) pve.nodes(node['node']).vzdump.create( vmid=backup_vmid.rstrip(','), storage=args.target, mode='snapshot', compress='lzo', mailto=args.notify, mailnotification='always') except: print('{0} ERROR: Failed to submit backup job to node {1}. Skipping!'.format( str(datetime.now()), node['node'])) else: print('{0} WARN: Backup target not found on node {1}'.format( str(datetime.now()), node['node'])) for vm in args.vmname: if vm not in backup_list: print('{0} ERROR: VM {1} backup request failed'.format( str(datetime.now()), vm))
# cluster: # - "cluster1" # - "cluster2" pve_config = yaml.load(open('/root/proxmox/proxmox.yml', 'r')) pve_apiuser = pve_config['apiuser'] pve_apipass = pve_config['apipass'] pve_cluster = pve_config['cluster'] parser = argparse.ArgumentParser(description='Display VM Configuration') parser.add_argument('vmname', nargs='+', help='VM to look up') args = parser.parse_args() vm_found = None for cluster in pve_cluster: pve = ProxmoxAPI(cluster, user=pve_apiuser, password=pve_apipass, verify_ssl=False) for node in pve.nodes.get(): for vm in pve.nodes(node['node']).qemu.get(): if vm['name'] in args.vmname: # Print a blank line between matching VMs if vm_found == True: print() vm_found = True # Print VM configuration vmconfig = pve.nodes(node['node']).qemu(vm['vmid']).get('config') vmconfig.update({'cluster' : cluster, 'node' : node['node']}) for k, v in sorted(vmconfig.items()): print('{0:>10}: {1}'.format(k, v))
disk = module.params['disk'] cpus = module.params['cpus'] memory = module.params['memory'] swap = module.params['swap'] storage = module.params['storage'] timeout = module.params['timeout'] # If password not set get it from PROXMOX_PASSWORD env if not api_password: try: api_password = os.environ['PROXMOX_PASSWORD'] except KeyError, e: module.fail_json(msg='You should set api_password param or use PROXMOX_PASSWORD environment variable') try: proxmox = ProxmoxAPI(api_host, user=api_user, password=api_password, verify_ssl=validate_certs) except Exception, e: module.fail_json(msg='authorization on proxmox cluster failed with exception: %s' % e) if state == 'present': try: if get_instance(proxmox, vmid) and not module.params['force']: module.exit_json(changed=False, msg="VM with vmid = %s is already exists" % vmid) elif not (node, module.params['hostname'] and module.params['password'] and module.params['ostemplate']): module.fail_json(msg='node, hostname, password and ostemplate are mandatory for creating vm') elif not node_check(proxmox, node): module.fail_json(msg="node '%s' not exists in cluster" % node) elif not content_check(proxmox, node, module.params['ostemplate'], storage): module.fail_json(msg="ostemplate '%s' not exists on node %s and storage %s" % (module.params['ostemplate'], node, storage))
class ProxmoxBroker(object): def __init__(self, backend, host, user, **kwargs): """A wrapper object for ProxmoxAPI. :param backend: the backend of proxmoxer :param host: the host of proxmox server :param user: the api user name of proxmox server (default 'root') :param kwargs: other args ProxmoxAPI accepted :returns: ProxmoxBroker -- ProxmoxBroker object for module usage. """ # here timeout is task timeout, not connection timeout self.timeout = kwargs.pop('timeout', 30) self.backend = backend self.proxmox = ProxmoxAPI(host, backend=backend, user=user, **kwargs) def _proxmox_node(self, node): """Get ProxmoxResource object for given node. :param node: the node name :returns: ProxmoxResource -- ProxmoxResource object for given node. """ return self.proxmox.nodes(node) def _is_int(self, string): """Verify if given string could be convert to integer. :param string: the given string :returns: bool -- if given string could be convert to integer. """ try: int(string) return True except ValueError: return False def _is_node_valid(self, node): """Verify if given node could be found in proxmox cluster. :param string: the node name :returns: bool -- if given node could be found in proxmox cluster. """ for nd in self.proxmox.nodes.get(): if nd['node'] == node: return True return False def _get_snapshot(self, node, vmid, snapname): """Get snapshot resource of given instance. :param node: the node name of instance :param vmid: the vmid of instance :param snapname: the snapshot name :returns: ProxmoxResource -- snampshot resource of the instance. """ for snap in self._proxmox_node(node).qemu(vmid).snapshot.get(): if snap.get('name') == snapname: return snap return None def _wait_until_timeout(self, node, taskid, vmstatus={}): """Wait until a task completed and meet expected vm status. :param node: the node name of the task :param taskid: the taskid :param vmstatus: expected vm status, e.g. {"vmid": "foo", "status": "bar"} :returns: (bool, string): task result, message """ if self.backend is not 'https': taskid = [data for data in taskid.split('\n') if 'UPID' in data][0] proxmox_node = self._proxmox_node(node) timeout = self.timeout while timeout >= 0: task_status = proxmox_node.tasks(taskid).status.get() if (task_status['status'] == 'stopped' and task_status['exitstatus'] == 'OK'): if not vmstatus: return True, "OK" else: if vmstatus['status'] in ('absent', 'present'): vms = [vm for vm in proxmox_node.qemu().get() if vm['vmid'] == vmstatus['vmid']] if vmstatus['status'] == 'absent' and len(vms) == 0: return True, "OK" elif vmstatus['status'] == 'present' and len(vms) == 1: return True, vms[0] else: vm = proxmox_node.qemu(vmstatus['vmid'] ).status.current.get() if vm['status'] == vmstatus['status']: return True, vm timeout = timeout - 1 if timeout == 0: msg = proxmox_node.tasks(taskid).log.get()[:1] return False, msg time.sleep(1) return False, msg def get_instance(self, vmid_or_name, node=None): """Get info of an instance. :param vmid_or_name: the instance name or vmid :param node: node name of the instance (default None) :returns: (bool, string) -- (result of the task, vm info if success or error message if fail) """ if self._is_int(vmid_or_name): field = 'vmid' vmid_or_name = int(vmid_or_name) else: field = 'name' vmid_or_name = str(vmid_or_name) vms = [] for vm in self.proxmox.cluster.resources.get(type='vm'): if vm.get(field) == vmid_or_name and (not node or vm.get('node') == node): vms.append(vm) if len(vms) == 1: return True, vms[0] elif len(vms) == 0: return False, "No instance with name or id %s found" % vmid_or_name else: return (False, "More than one instance with name or id %s found" % vmid_or_name) def clone_instance(self, name, template, node): """Create new instance based on a template. Create a new instance based on given template, it will use the max_id+1 as vmid, and will wait until the task done and the vm is present on the node. :param name: the new instance name :param template: the template name or vmid :param node: node name for the new instance :returns: (bool, bool,string) -- (result of the task, changed or not on proxmox, message for user) """ existed, _ = self.get_instance(name, node) if existed: return False, False, "VM with name = %s already exists" % name if not self._is_node_valid(node): return False, False, "node '%s' not exists in cluster" % node template_existed, t_vm = self.get_instance(template, node=node) if not template_existed: return False, False, "%s is not existed" % template elif template_existed and t_vm['template'] == 0: return False, False, "%s is not a valid template" % template else: proxmox_node = self._proxmox_node(node) next_id = max([vm['vmid'] for vm in proxmox_node.qemu.get()]) + 1 taskid = proxmox_node.qemu(t_vm['vmid']).clone.post(newid=next_id, name=name) expected_status = {"vmid": next_id, "status": 'present'} result, log = self._wait_until_timeout(node, taskid, expected_status) if not result: return (False, True, "Reached timeout while waiting for clone " "VM, last line in task before timeout %s" % log) else: return True, True, "cloned" def start_instance(self, name, node=None): """Start an instance. Start an instance with given name or id, it will wait until the task done and vm in 'running' status. :param name: the instance name or id :param node: node name for the instance, (default None) :returns: (bool, bool,string) -- (result of the task, changed or not on proxmox, message for user) """ rc, msg = self.get_instance(name, node) if not rc: return (rc, False, msg) elif msg['status'] == 'running': msg = "VM %s is already running" % name return True, False, msg else: vm = msg proxmox_node = self._proxmox_node(vm['node']) taskid = proxmox_node.qemu(vm['vmid']).status.start.post() expected_status = {"vmid": vm['vmid'], "status": 'running'} success, log = self._wait_until_timeout(vm['node'], taskid, expected_status) if not success: return (False, True, "Reached timeout while waiting for " "starting VM, last line in task before " "timeout %s" % log) else: return True, True, "started" def stop_instance(self, name, node=None, force=False): """Stop an instance. Stop an instance with given name or id, it will wait until the task done and vm in 'stopped' status. if force=True, it will call stop for the instance, otherwise will call (ACPI) shutdown. :param name: the instance name or id :param node: node name for the instance, (default None) :param force: stop (True) or shutdown (False), (default False) :returns: (bool, bool,string) -- (result of the task, changed or not on proxmox, message for user) """ rc, msg = self.get_instance(name, node) if not rc: return rc, False, msg elif msg['status'] == 'stopped': msg = "VM %s is already stopped" % name return True, False, msg else: vm = msg proxmox_node = self._proxmox_node(vm['node']) if force: taskid = proxmox_node.qemu(vm['vmid']).status.stop.post() else: taskid = proxmox_node.qemu(vm['vmid']).status.shutdown.post() expected_status = {"vmid": vm['vmid'], "status": 'stopped'} success, log = self._wait_until_timeout(vm['node'], taskid, expected_status) if not success: return (False, True, "Reached timeout while waiting for " "stopping VM, last line in task before " "timeout %s" % log) else: return True, True, "stopped" def delete_instance(self, name, node=None, force=False): """Delete an instance. Stop an instance with given name or id, it will wait until the task done and vm in absent on node. if force=True, it will stop the instance first if it's still in 'running' status, otherwise the task will fail. :param name: the instance name or id :param node: node name for the instance, (default None) :param force: force delete or not, (default False) :returns: (bool, bool,string) -- (result of the task, changed or not on proxmox, message for user) """ rc, msg = self.get_instance(name, node) if not rc: msg = "VM %s is already absent" % name return True, False, msg elif msg['status'] != 'stopped' and not force: msg = "VM %s is not stopped" % name return False, False, msg else: vm = msg proxmox_node = self._proxmox_node(vm['node']) if msg['status'] != 'stopped' and force: self.stop_instance(name, node, force=True) taskid = proxmox_node.qemu(vm['vmid']).delete() expected_status = {"vmid": vm['vmid'], "status": 'absent'} success, log = self._wait_until_timeout(vm['node'], taskid, expected_status) if not success: return (False, True, "Reached timeout while waiting for " "deleting VM, last line in task before " "timeout %s" % log) else: return True, True, "deleted" def snapshot_instance(self, name, snapname, node=None): """Take a snapshot for an instance. :param name: the instance name or id :param snapname: the name of the snapshot :param node: node name for the instance, (default None) :returns: (bool, bool,string) -- (result of the task, changed or not on proxmox, message for user) """ rc, msg = self.get_instance(name, node) if not rc: return rc, False, msg else: vm = msg snap = self._get_snapshot(vm['node'], vm['vmid'], snapname) if snap: return True, False, "Snapshot %s exists" % snapname proxmox_node = self._proxmox_node(vm['node']) taskid = proxmox_node.qemu(vm['vmid']).snapshot().post( snapname=snapname, vmstate="1") success, log = self._wait_until_timeout(vm['node'], taskid) if not success: return (False, True, "Reached timeout while waiting for " "snapshot VM, last line in task before " "timeout %s" % log) else: return True, True, "snapshotted" def restore_instance(self, name, snapname, node=None): """Restore an instance from a snapshot. :param name: the instance name or id :param snapname: the name of the snapshot to restore from :param node: node name for the instance, (default None) :returns: (bool, bool,string) -- (result of the task, changed or not on proxmox, message for user) """ rc, msg = self.get_instance(name, node) if not rc: return rc, False, msg else: vm = msg snap = self._get_snapshot(vm['node'], vm['vmid'], snapname) if not snap: return False, False, "Snapshot %s not found" % snapname proxmox_node = self._proxmox_node(vm['node']) taskid = proxmox_node.qemu(vm['vmid']).snapshot( snapname).rollback.post() success, log = self._wait_until_timeout(vm['node'], taskid) if not success: return (False, True, "Reached timeout while waiting for " "restore VM, last line in task before " "timeout %s" % log) else: return True, True, "restored"
class KVM(): def __init__(self, uuid=None): self.proxmox = None self.node = None self.password = None self.uuid = uuid self.log = [] self.result = [] def connect(self, hostname, password, nodename): self.proxmox = ProxmoxAPI(hostname, user='******', password=password, verify_ssl=False) self.node = self.proxmox.nodes(nodename) self.password = pwgen() def create(self, vmid=100, hostname='newvm', mem=512, cpus=1): try: self.node.qemu.create(vmid=vmid, ostype='l26', name=hostname, storage='local', memory=mem, sockets=1, cores=cpus, net0='rtl8139,rate=50,bridge=vmbr0', virtio0='local:' + str(vmid) + '/vm-' + str(vmid) + '-disk-1.qcow2,cache=writeback,mbps_rd=5,mbps_wr=5', cdrom='none') except: return False finally: self.result.append('<result action="create" state="ready"/>') # Время на создание VM time.sleep(15) #self.node.qemu(vmid).config.post(onboot=1) return True def createstorage(self, vmid=100, size=5): """ Создать диск для ВМ """ try: self.node.storage.local.content.post( filename='vm-' + str(vmid) + '-disk-1.qcow2', format='qcow2', size=str(size) + 'G', vmid=vmid) except BaseException as err: self.log.append(err) return False finally: self.log.append('<result action="createstorage" state="ready"/>') return True def startvm(self, vmid=100, args=None): """ Запустить ВМ """ self.node.qemu(vmid).config.post(delete="args") #self.node.qemu(vmid).config.set(args='') if args: if len(args)>0: self.node.qemu(vmid).config.post(args=args) else: args = '-kernel /root/ipxe.lkrn -append \"dhcp && chain http://pxe.justhost.ru/uuid/' + self.uuid + '.boot.php\"' self.node.qemu(vmid).config.set(args=args) #self.node.qemu(int(vmid)).config.post(args=arguments) self.node.qemu(vmid).status.start.post() def stopvm(self, vmid=100): """ Остановить ВМ """ if self.isVMExist(vmid): self.node.qemu(vmid).status.stop.post() def deletevm(self, vmid=100): """ Удалить ВМ """ if self.isVMExist(vmid): self.node.qemu(vmid).delete() time.sleep(15) def isVMExist(self, vmid=100): """ Проверка на существование ВМ с указанным ID """ result = False for item in self.node.openvz.get(): if item['vmid'] == str(vmid): result=True for item in self.node.qemu.get(): if item['vmid'] == str(vmid): result=True return result def task(self, tasks): """ Пакетный режим """ result = [] flagerror = False for item in tasks: self.log.append(item) if item['action'] == 'connect': if self.connect(item['hostname'], item['password'], item['node']): result.append({'action': 'connect', 'status': 'error'}) else: result.append({'action': 'connect', 'status': 'error'}) elif item['action'] == 'create': self.createstorage(vmid=int(item['vmid']), size=item['hdd']) self.create(vmid=int(item['vmid']), hostname=item['hostname'], mem=item['mem'], cpus=item['cpu']) time.sleep(10) result.append({'action': 'create', 'status': 'ready'}) elif item['action'] == 'delete': self.log.append('Удаление KVM ' + str(item['vmid'])) self.deletevm(vmid=item['vmid']) time.sleep(10) self.log.append('Удалена KVM ' + str(item['vmid'])) result.append({'action': 'delete', 'status': 'ready'}) elif item['action'] == 'start': if 'args' in item.keys(): self.startvm(vmid=item['vmid'], args=item['args']) else: self.startvm(vmid=item['vmid']) result.append({'action': 'start', 'status': 'ready'}) elif item['action'] == 'stop': self.stopvm(vmid=item['vmid']) result.append({'action': 'stop', 'status': 'ready'}) elif item['action'] == 'wait': time.sleep(300) if flagerror: return {'result': 'kvm', 'status': 'ready', 'actions': result} else: return {'result': 'kvm', 'status': 'error', 'actions': result}
default='dir', choices=['dir', 'lvm'], help='Virtual machine storage backend') args = parser.parse_args() vmid = str(args.vmid) name = args.name flavor_type = args.flavor storage_type = args.storage # customize flavor instance, flavor = instance_customize(instance, vmid, name, flavor_type) # proxmoxer initialize proxmox_api = ProxmoxAPI(proxmox['host'], user=proxmox['user'], password=proxmox['password'], verify_ssl=proxmox['verify_ssl']) node = proxmox_api.nodes(proxmox['node']) # create kvm machine node.qemu.create(vmid=vmid, name=name, sockets=flavor['sockets'], cores=flavor['cores'], balloon=flavor['balloon'], memory=flavor['memory'], net0=instance['net']) # seeding seed(vmid, instance, proxmox) # set seed iso node.qemu(vmid).config.set(virtio1=instance['hd_seed'])
def buildlxc(host): """ Creates lxc in proxmox using given hostresource configuration """ if not exists(host): raise ValueError("Host template is missing. Please create host template") container = Container.getContainer(HOST_CONTAINER) hostresource = container.loadResource(host) #get proxmox user and hypervisor userresource = proxmoxutil.listuser() if userresource is None: raise ValueError("No proxmox user found!! Please use proxmoxutil command to update user credentials") user = userresource.properties[PROPERTIES_USER] password = userresource.properties[PROPERTIES_PASSWORD] authrealm = userresource.properties[PROPERTIES_AUTHREALM] puser = user+'@'+authrealm primary = proxmoxutil.listprimary() if primary is None: raise ValueError("Primary proxmox hypervisor not found!! Please use proxmoxutil command to update primary hypervisor") hypervisor = primary.properties[PROPERTIES_HYPERVISOR] print "Authenticating "+puser +" on "+ hypervisor proxmox = ProxmoxAPI(hypervisor, user=puser, password=password, verify_ssl=False) node = proxmox.nodes(hostresource.properties[HYPERVISOR]) hostname = hostresource.properties[HOSTNAME] vmid = int(hostresource.properties[HOSTID]) ostemplate = str(hostresource.properties[PROPERTIES_OSTEMPLATE]) cpulimit = int(hostresource.properties[PROPERTIES_CPULIMIT]) cpuunits = int(hostresource.properties[PROPERTIES_CPUUNITS]) memory = int(hostresource.properties[PROPERTIES_MEMORY]) swap = int(hostresource.properties[PROPERTIES_SWAP]) storage = hostresource.properties[PROPERTIES_STORAGE] disk = int(hostresource.properties[PROPERTIES_DISK]) disksize="%dG"%(disk) interfaces = hostresource.properties[INTERFACES] i=0 netconfig = dict() for interface in interfaces: print "Configuring %s" %interface netconfig["net"+str(i)] = hostresource.properties[interface] i=i+1 print "Building LXC with the following parameters:" print "Vmid: %d" %vmid print "Template: %s" %ostemplate print "Cpu Limit: %d" %cpulimit print "Cpu Units: %d" %cpuunits print "Memory: %d" %memory print "Swap: %d" %swap print "Storage: %s" %storage print "Disk: %d" %disk node.lxc.create(vmid=vmid, hostname=hostname, ostemplate=ostemplate, password=DEFAULT_PASSWORD, cpuunits=cpuunits, cpulimit=cpulimit, memory=memory, swap=swap, **netconfig) print "Creating LXC....." time.sleep(30) print "Resizing rootfs" node.lxc(vmid).resize.put(disk='rootfs', size=disksize) time.sleep(30) print "LXC created"
disk = module.params['disk'] cpus = module.params['cpus'] memory = module.params['memory'] swap = module.params['swap'] storage = module.params['storage'] timeout = module.params['timeout'] # If password not set get it from PROXMOX_PASSWORD env if not api_password: try: api_password = os.environ['PROXMOX_PASSWORD'] except KeyError, e: module.fail_json(msg='You should set api_password param or use PROXMOX_PASSWORD environment variable') try: proxmox = ProxmoxAPI(api_host, user=api_user, password=api_password, verify_ssl=validate_certs) global VZ_TYPE VZ_TYPE = 'openvz' if float(proxmox.version.get()['version']) < 4.0 else 'lxc' except Exception, e: module.fail_json(msg='authorization on proxmox cluster failed with exception: %s' % e) if state == 'present': try: if get_instance(proxmox, vmid) and not module.params['force']: module.exit_json(changed=False, msg="VM with vmid = %s is already exists" % vmid) elif not (node, module.params['hostname'] and module.params['password'] and module.params['ostemplate']): module.fail_json(msg='node, hostname, password and ostemplate are mandatory for creating vm') elif not node_check(proxmox, node): module.fail_json(msg="node '%s' not exists in cluster" % node) elif not content_check(proxmox, node, module.params['ostemplate'], storage):
## Determine variables online_api_token = "YOUR_ONLINE_API_TOKEN" proxmox_api_server = "127.0.0.1" proxmox_api_login = "******" proxmox_api_password = "******" ## Initialize Online API online_api_session = requests.session() online_api_session.headers['Authorization'] = "Bearer " + online_api_token online_api = slumber.API('https://api.online.net/api/v1', session=online_api_session) ## Retrieve IPFAILOVER informations failover_info = online_api.server.failover.get() ## Initialize Proxmox API proxmox_api = ProxmoxAPI(proxmox_api_server, user=proxmox_api_login, password=proxmox_api_password, verify_ssl=False) ## Check Cluster Status proxmox_cluster = proxmox_api.cluster.status.get() proxmox_members = proxmox_api.nodes.get() checked_mac=[] ## For each node in cluster for node_member in proxmox_members: # Check if member is node for member in proxmox_cluster: if member["type"]=="node" and node_member["node"] == member["name"]: cluster_member=member # If host is alive (if is not alive, don't ask the node)
userID='' vmid=100 hvname='node1' nodes={'node1': {'name': 'IPorHOSTNAME', 'password': '******'}, 'node2': {'name': 'IPorHOSTNAME', 'password': '******'}} ostemplates=['local:vztmpl/debian-7.0-x86.tar.gz', 'local:vztmpl/ubuntu-14.04-x86.tar.gz', 'local:vztmpl/ubuntu-14.04-x86_64.tar.gz', 'local:vztmpl/centos-6-x86.tar.gz', 'local:vztmpl/centos-6-x86_64.tar.gz', 'local:vztmpl/centos-7-x86_64.tar.gz'] proxmox = ProxmoxAPI(nodes[hvname]['name'], user='******', password=nodes[hvname]['password'], verify_ssl=False) def isUserExist(proxmox, user): result = False for item in proxmox.access.users.get(): if item['userid'] == user: result = True return result if not isUserExist(proxmox, 'u' + userID + '@pve'): proxmox.access.users.create(userid='u' + userID + '@pve', password='******') node = proxmox.nodes(hvname) node.openvz.create(vmid=vmid, ostemplate=ostemplates[4],
def __init__(self, host, password): self.proxmox=ProxmoxAPI(host, user='******', password=password, verify_ssl=False)
class OVZ(): def __init__(self): self.proxmox = None self.node = None self.password = None self.log = [] def connect(self, hostname, password, nodename): self.proxmox = ProxmoxAPI(hostname, user='******', password=password, verify_ssl=False) self.node = self.proxmox.nodes(nodename) self.password = pwgen() def create(self, vmid=100, template='local:vztmpl/centos-6-x86_64.tar.gz', hostname='newvm', mem=512, hdd=5, cpus=1, iplist=[], nameserver='8.8.8.8'): if not self.isVMExist(vmid): try: self.node.openvz.create(vmid=vmid, ostemplate=template, hostname=hostname, storage='local', memory=mem, swap=0, cpus=cpus, disk=hdd, password=self.password, ip_address=iplist, nameserver=nameserver) except: return False # Время на распаковку архива контейнера time.sleep(30) self.node.openvz(vmid).config.set(onboot=1) return True def startvm(self, vmid=100): if self.isVMExist(vmid): self.node.openvz(vmid).status.start.post() def stopvm(self, vmid=100): if self.isVMExist(vmid): self.node.openvz(vmid).status.stop.post() def deletevm(self, vmid=100): if self.isVMExist(vmid): self.node.openvz(vmid).delete() def isVMExist(self, vmid=100): result = False for item in self.node.openvz.get(): if item['vmid'] == vmid: result=True for item in self.node.qemu.get(): if item['vmid'] == vmid: result=True return result def machines(self): result = self.node.openvz.get() return result def task(self, xml): """ Пакетный режим """ result=[] self.log.append('OVZ task begin') for item in xml: self.log.append(item.tag) if item.tag == 'connect': self.connect(item.attrib['hostname'], item.attrib['password'], item.attrib['node']) elif item.tag == 'create': iplist=[] for ip in item: iplist.append(ip.text) if self.create(vmid=item.attrib['vmid'], template='local:vztmpl/' + item.attrib['template'], hostname=item.attrib['hostname'], mem=item.attrib['mem'], hdd=item.attrib['hdd'], cpus=item.attrib['cpu'], iplist=iplist, nameserver='46.17.40.200 46.17.46.200'): result.append('<result type="openvz" status="error" description="Ошибка создания"/>') else: time.sleep(10) result.append('<result type="openvz" status="ready" password="******"/>') elif item.tag == 'delete': self.deletevm(vmid=xml.attrib['vmid']) elif item.tag == 'start': self.startvm(vmid=item.attrib['vmid']) elif item.tag == 'wait': time.sleep(300) self.log.append('OVZ task end') return result
def main(): module = AnsibleModule( argument_spec = dict( api_host = dict(required=True), api_user = dict(required=True), api_password = dict(no_log=True), vmid = dict(required=True), validate_certs = dict(type='bool', default='no'), node = dict(), password = dict(no_log=True), hostname = dict(), ostemplate = dict(), disk = dict(type='str', default='3'), cpus = dict(type='int', default=1), memory = dict(type='int', default=512), swap = dict(type='int', default=0), netif = dict(type='dict'), mounts = dict(type='dict'), ip_address = dict(), onboot = dict(type='bool', default='no'), storage = dict(default='local'), cpuunits = dict(type='int', default=1000), nameserver = dict(), searchdomain = dict(), timeout = dict(type='int', default=30), force = dict(type='bool', default='no'), state = dict(default='present', choices=['present', 'absent', 'stopped', 'started', 'restarted']), ) ) if not HAS_PROXMOXER: module.fail_json(msg='proxmoxer required for this module') state = module.params['state'] api_user = module.params['api_user'] api_host = module.params['api_host'] api_password = module.params['api_password'] vmid = module.params['vmid'] validate_certs = module.params['validate_certs'] node = module.params['node'] disk = module.params['disk'] cpus = module.params['cpus'] memory = module.params['memory'] swap = module.params['swap'] storage = module.params['storage'] if module.params['ostemplate'] is not None: template_store = module.params['ostemplate'].split(":")[0] timeout = module.params['timeout'] # If password not set get it from PROXMOX_PASSWORD env if not api_password: try: api_password = os.environ['PROXMOX_PASSWORD'] except KeyError as e: module.fail_json(msg='You should set api_password param or use PROXMOX_PASSWORD environment variable') try: proxmox = ProxmoxAPI(api_host, user=api_user, password=api_password, verify_ssl=validate_certs) global VZ_TYPE VZ_TYPE = 'openvz' if float(proxmox.version.get()['version']) < 4.0 else 'lxc' except Exception as e: module.fail_json(msg='authorization on proxmox cluster failed with exception: %s' % e) if state == 'present': try: if get_instance(proxmox, vmid) and not module.params['force']: module.exit_json(changed=False, msg="VM with vmid = %s is already exists" % vmid) elif not (node, module.params['hostname'] and module.params['password'] and module.params['ostemplate']): module.fail_json(msg='node, hostname, password and ostemplate are mandatory for creating vm') elif not node_check(proxmox, node): module.fail_json(msg="node '%s' not exists in cluster" % node) elif not content_check(proxmox, node, module.params['ostemplate'], template_store): module.fail_json(msg="ostemplate '%s' not exists on node %s and storage %s" % (module.params['ostemplate'], node, template_store)) create_instance(module, proxmox, vmid, node, disk, storage, cpus, memory, swap, timeout, password = module.params['password'], hostname = module.params['hostname'], ostemplate = module.params['ostemplate'], netif = module.params['netif'], mounts = module.params['mounts'], ip_address = module.params['ip_address'], onboot = int(module.params['onboot']), cpuunits = module.params['cpuunits'], nameserver = module.params['nameserver'], searchdomain = module.params['searchdomain'], force = int(module.params['force'])) module.exit_json(changed=True, msg="deployed VM %s from template %s" % (vmid, module.params['ostemplate'])) except Exception as e: module.fail_json(msg="creation of %s VM %s failed with exception: %s" % ( VZ_TYPE, vmid, e )) elif state == 'started': try: vm = get_instance(proxmox, vmid) if not vm: module.fail_json(msg='VM with vmid = %s not exists in cluster' % vmid) if getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'running': module.exit_json(changed=False, msg="VM %s is already running" % vmid) if start_instance(module, proxmox, vm, vmid, timeout): module.exit_json(changed=True, msg="VM %s started" % vmid) except Exception as e: module.fail_json(msg="starting of VM %s failed with exception: %s" % ( vmid, e )) elif state == 'stopped': try: vm = get_instance(proxmox, vmid) if not vm: module.fail_json(msg='VM with vmid = %s not exists in cluster' % vmid) if getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'mounted': if module.params['force']: if umount_instance(module, proxmox, vm, vmid, timeout): module.exit_json(changed=True, msg="VM %s is shutting down" % vmid) else: module.exit_json(changed=False, msg=("VM %s is already shutdown, but mounted. " "You can use force option to umount it.") % vmid) if getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'stopped': module.exit_json(changed=False, msg="VM %s is already shutdown" % vmid) if stop_instance(module, proxmox, vm, vmid, timeout, force = module.params['force']): module.exit_json(changed=True, msg="VM %s is shutting down" % vmid) except Exception as e: module.fail_json(msg="stopping of VM %s failed with exception: %s" % ( vmid, e )) elif state == 'restarted': try: vm = get_instance(proxmox, vmid) if not vm: module.fail_json(msg='VM with vmid = %s not exists in cluster' % vmid) if ( getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'stopped' or getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'mounted' ): module.exit_json(changed=False, msg="VM %s is not running" % vmid) if ( stop_instance(module, proxmox, vm, vmid, timeout, force = module.params['force']) and start_instance(module, proxmox, vm, vmid, timeout) ): module.exit_json(changed=True, msg="VM %s is restarted" % vmid) except Exception as e: module.fail_json(msg="restarting of VM %s failed with exception: %s" % ( vmid, e )) elif state == 'absent': try: vm = get_instance(proxmox, vmid) if not vm: module.exit_json(changed=False, msg="VM %s does not exist" % vmid) if getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'running': module.exit_json(changed=False, msg="VM %s is running. Stop it before deletion." % vmid) if getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'mounted': module.exit_json(changed=False, msg="VM %s is mounted. Stop it with force option before deletion." % vmid) taskid = getattr(proxmox.nodes(vm[0]['node']), VZ_TYPE).delete(vmid) while timeout: if ( proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['status'] == 'stopped' and proxmox.nodes(vm[0]['node']).tasks(taskid).status.get()['exitstatus'] == 'OK' ): module.exit_json(changed=True, msg="VM %s removed" % vmid) timeout = timeout - 1 if timeout == 0: module.fail_json(msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s' % proxmox_node.tasks(taskid).log.get()[:1]) time.sleep(1) except Exception as e: module.fail_json(msg="deletion of VM %s failed with exception: %s" % ( vmid, e ))
class TestSuite(): proxmox = None serializer = None session = None # noinspection PyMethodOverriding @patch('requests.sessions.Session') def setUp(self, session): response = {'ticket': 'ticket', 'CSRFPreventionToken': 'CSRFPreventionToken'} session.request.return_value = response self.proxmox = ProxmoxAPI('proxmox', user='******', password='******', port=123, verify_ssl=False) self.serializer = MagicMock() self.session = MagicMock() self.session.request.return_value.status_code = 200 self.proxmox._store['session'] = self.session self.proxmox._store['serializer'] = self.serializer def test_get(self): self.proxmox.nodes('proxmox').storage('local').get() eq_(self.session.request.call_args[0], ('GET', 'https://proxmox:123/api2/json/nodes/proxmox/storage/local')) def test_delete(self): self.proxmox.nodes('proxmox').openvz(100).delete() eq_(self.session.request.call_args[0], ('DELETE', 'https://proxmox:123/api2/json/nodes/proxmox/openvz/100')) self.proxmox.nodes('proxmox').openvz('101').delete() eq_(self.session.request.call_args[0], ('DELETE', 'https://proxmox:123/api2/json/nodes/proxmox/openvz/101')) def test_post(self): node = self.proxmox.nodes('proxmox') node.openvz.create(vmid=800, ostemplate='local:vztmpl/debian-6-turnkey-core_12.0-1_i386.tar.gz', hostname='test', storage='local', memory=512, swap=512, cpus=1, disk=4, password='******', ip_address='10.0.100.222') eq_(self.session.request.call_args[0], ('POST', 'https://proxmox:123/api2/json/nodes/proxmox/openvz')) ok_('data' in self.session.request.call_args[1]) data = self.session.request.call_args[1]['data'] eq_(data['cpus'], 1) eq_(data['disk'], 4) eq_(data['hostname'], 'test') eq_(data['ip_address'], '10.0.100.222') eq_(data['memory'], 512) eq_(data['ostemplate'], 'local:vztmpl/debian-6-turnkey-core_12.0-1_i386.tar.gz') eq_(data['password'], 'secret') eq_(data['storage'], 'local') eq_(data['swap'], 512) eq_(data['vmid'], 800) node = self.proxmox.nodes('proxmox1') node.openvz.post(vmid=900, ostemplate='local:vztmpl/debian-7-turnkey-core_12.0-1_i386.tar.gz', hostname='test1', storage='local1', memory=1024, swap=1024, cpus=2, disk=8, password='******', ip_address='10.0.100.111') eq_(self.session.request.call_args[0], ('POST', 'https://proxmox:123/api2/json/nodes/proxmox1/openvz')) ok_('data' in self.session.request.call_args[1]) data = self.session.request.call_args[1]['data'] eq_(data['cpus'], 2) eq_(data['disk'], 8) eq_(data['hostname'], 'test1') eq_(data['ip_address'], '10.0.100.111') eq_(data['memory'], 1024) eq_(data['ostemplate'], 'local:vztmpl/debian-7-turnkey-core_12.0-1_i386.tar.gz') eq_(data['password'], 'secret1') eq_(data['storage'], 'local1') eq_(data['swap'], 1024) eq_(data['vmid'], 900) def test_put(self): node = self.proxmox.nodes('proxmox') node.openvz(101).config.set(cpus=4, memory=1024, ip_address='10.0.100.100', onboot=True) eq_(self.session.request.call_args[0], ('PUT', 'https://proxmox:123/api2/json/nodes/proxmox/openvz/101/config')) data = self.session.request.call_args[1]['data'] eq_(data['cpus'], 4) eq_(data['memory'], 1024) eq_(data['ip_address'], '10.0.100.100') eq_(data['onboot'], True) node = self.proxmox.nodes('proxmox1') node.openvz(102).config.put(cpus=2, memory=512, ip_address='10.0.100.200', onboot=False) eq_(self.session.request.call_args[0], ('PUT', 'https://proxmox:123/api2/json/nodes/proxmox1/openvz/102/config')) data = self.session.request.call_args[1]['data'] eq_(data['cpus'], 2) eq_(data['memory'], 512) eq_(data['ip_address'], '10.0.100.200') eq_(data['onboot'], False)