def host_add(args): assert isinstance(args, dict) # remove this assert after adding more type and htype assert (args['type'].lower() in ['vm', 'server', '1'] and args['htype'].lower() == 'kvm') host_info = models.HostAdd() host_info.ip = args['ip'] host_info.user_name = args['user_name'] host_info.user_passwd = args['user_passwd'] if args['type'].lower() in ['vm', 'server', '1']: host_info.instance_type = host_info.instance_types.VM if args['htype'].lower() in ['xen', 'xenserver', 'xcp', '1']: host_info.hypervisor_type = host_info.hypervisor_types.XEN elif args['htype'].lower() in ['vmware', 'esxi', '2']: host_info.hypervisor_type = host_info.hypervisor_types.VMWARE elif args['htype'].lower() in ['kvm', '3']: host_info.hypervisor_type = host_info.hypervisor_types.KVM elif args['type'].lower() in ['nspgateway', 'nsp', '3']: host_info.instance_type = host_info.instance_types.NSP host_info.hypervisor_type = host_info.hypervisor_types.KVM host_info.rack_name = args['rack_name'] if args['nic_type'].lower() == 'gigabit': host_info.nic_type = host_info.nic_types.GIGABIT elif args['nic_type'].lower() == '10gigabit': host_info.nic_type = host_info.nic_types.XGIGABIT host_info.uplink_ip = args.get('uplink_ip') host_info.uplink_netmask = args.get('uplink_netmask') host_info.uplink_gateway = args.get('uplink_gateway') host_info.misc = args.get('misc') host_info.brand = args.get('brand') host_info.model = args.get('model') host_info.storage_link_ip = args.get('storage_link_ip') if (host_info.storage_link_ip is None and host_info.hypervisor_type == host_info.hypervisor_types.KVM and host_info.instance_type == host_info.instance_types.VM): print >> sys.stderr, 'Error: No storage_link_ip specified' return -1 resp = call_post_with_callback(TALKER_URL + 'hosts/', data=host_info.to_primitive()) if 'OPT_STATUS' not in resp or 'DESCRIPTION' not in resp: print >> sys.stderr, 'Error: response %s corrupted' % resp return -1 if resp['OPT_STATUS'] != 'SUCCESS': print >> sys.stderr, 'Error (%s): %s' % (resp['OPT_STATUS'], resp['DESCRIPTION']) return -1 print resp['DATA']['LCUUID'] if host_info.instance_type != host_info.instance_types.NSP: print >> sys.stderr, 'Please activate storage of this host before use' return 0
def host_add(args): assert isinstance(args, dict) # remove this assert after adding more type and htype assert (args['type'].lower() in ['vm', 'server', '1'] and args['htype'].lower() == 'kvm') host_info = models.HostAdd() host_info.ip = args['ip'] host_info.user_name = args['user_name'] host_info.user_passwd = args['user_passwd'] if args['type'].lower() in ['vm', 'server', '1']: host_info.instance_type = host_info.instance_types.VM if args['htype'].lower() in ['xen', 'xenserver', 'xcp', '1']: host_info.hypervisor_type = host_info.hypervisor_types.XEN elif args['htype'].lower() in ['vmware', 'esxi', '2']: host_info.hypervisor_type = host_info.hypervisor_types.VMWARE elif args['htype'].lower() in ['kvm', '3']: host_info.hypervisor_type = host_info.hypervisor_types.KVM elif args['type'].lower() in ['nspgateway', 'nsp', '3']: host_info.instance_type = host_info.instance_types.NSP host_info.hypervisor_type = host_info.hypervisor_types.KVM host_info.rack_name = args['rack_name'] if args['nic_type'].lower() == 'gigabit': host_info.nic_type = host_info.nic_types.GIGABIT elif args['nic_type'].lower() == '10gigabit': host_info.nic_type = host_info.nic_types.XGIGABIT host_info.uplink_ip = args.get('uplink_ip') host_info.uplink_netmask = args.get('uplink_netmask') host_info.uplink_gateway = args.get('uplink_gateway') host_info.misc = args.get('misc') host_info.brand = args.get('brand') host_info.model = args.get('model') host_info.storage_link_ip = args.get('storage_link_ip') if (host_info.storage_link_ip is None and host_info.hypervisor_type == host_info.hypervisor_types.KVM and host_info.instance_type == host_info.instance_types.VM): print >>sys.stderr, 'Error: No storage_link_ip specified' return -1 resp = call_post_with_callback(TALKER_URL + 'hosts/', data=host_info.to_primitive()) if 'OPT_STATUS' not in resp or 'DESCRIPTION' not in resp: print >>sys.stderr, 'Error: response %s corrupted' % resp return -1 if resp['OPT_STATUS'] != 'SUCCESS': print >>sys.stderr, 'Error (%s): %s' % (resp['OPT_STATUS'], resp['DESCRIPTION']) return -1 print resp['DATA']['LCUUID'] if host_info.instance_type != host_info.instance_types.NSP: print >>sys.stderr, 'Please activate storage of this host before use' return 0
def vm_import(args): assert isinstance(args, dict) url = TALKER_URL + 'hosts/' try: resp = requests.get(url) except requests.exceptions.ConnectionError: print >> sys.stderr, 'Error: unable to connet to talker' return -1 if resp.status_code != 200: print >> sys.stderr, 'Error: unable to get hosts' return -1 r_data = resp.json() if 'DATA' not in r_data: print >> sys.stderr, 'Error: unable to get hosts' return -1 launch_server_htype = None for it in r_data['DATA']: if args['launch-server'] == it.get('IP', None): launch_server_htype = it.get('HTYPE', None) break if launch_server_htype is None: print >>sys.stderr, 'Error: launch_server %s not found' % \ args['launch-server'] return -1 if launch_server_htype != HOST_HTYPE_KVM: call_args = [] for key, value in args.items(): call_args.append('%s=%s' % (key, value)) if os.path.isfile('/usr/local/bin/mntnct'): call_cmd = 'mntnct' else: call_cmd = (os.path.dirname(os.path.realpath(__file__)) + '/mntnct') subprocess.call([call_cmd, 'vm.import'] + call_args) return url = TALKER_URL + 'domains/' try: resp = requests.get(url) except requests.exceptions.ConnectionError: print >> sys.stderr, 'Error: unable to connet to talker' return -1 if resp.status_code != 200: print >> sys.stderr, 'Error: unable to get domains' return -1 r_data = resp.json() if 'DATA' not in r_data: print >> sys.stderr, 'Error: unable to get domains' return -1 domain_uuid = None for it in r_data['DATA']: if args['domain'] == it.get('NAME', None): domain_uuid = it.get('LCUUID', None) break if domain_uuid is None: print >> sys.stderr, 'Error: domain %s not found' % args['domain'] return -1 try: user = data.User.get(data.User.email == args['user-email']) userid = user.id except Exception: print >>sys.stderr, 'Error: user %s not found' % \ args['user-email'] return -1 try: ps = data.OssProductSpec.get( data.OssProductSpec.plan_name == args['product-specification'], data.OssProductSpec.domain == domain_uuid) ps_lcuuid = ps.lcuuid except Exception: print >>sys.stderr, 'Error: product-specification %s not found' % \ args['product-specification'] return -1 vm_info = models.VMImport() vm_info.name = args['label'] vm_info.os = args['os'] vm_info.launch_server = args['launch-server'] vm_info.userid = userid vm_info.product_specification_lcuuid = ps_lcuuid vm_info.domain = domain_uuid resp = call_post_with_callback(TALKER_URL + 'vms/import/', data=vm_info.to_primitive()) if 'OPT_STATUS' not in resp or 'DESCRIPTION' not in resp: print >> sys.stderr, 'Error: response %s corrupted' % resp return -1 if resp['OPT_STATUS'] != 'SUCCESS': print >> sys.stderr, 'Error (%s): %s' % (resp['OPT_STATUS'], resp['DESCRIPTION']) return -1 print >> sys.stdout, '' print >> sys.stdout, 'NAME: %s' % resp['DATA']['NAME'] print >> sys.stdout, 'LCUUID: %s' % resp['DATA']['LCUUID'] print >> sys.stdout, 'STATE: %d' % resp['DATA']['STATE'] print >> sys.stdout, 'CPU: %d' % resp['DATA']['VCPU_NUM'] print >> sys.stdout, 'MEM_SIZE: %d M' % resp['DATA']['MEM_SIZE'] print >> sys.stdout, 'SYS_DISK_SIZE: %d G' % resp['DATA']['VDI_SYS_SIZE'] print >> sys.stdout, 'SYS_SR_NAME: %s' % resp['DATA']['VDI_SYS_SR_NAME'] return 0
def vm_import(args): assert isinstance(args, dict) url = TALKER_URL + 'hosts/' try: resp = requests.get(url) except requests.exceptions.ConnectionError: print >>sys.stderr, 'Error: unable to connet to talker' return -1 if resp.status_code != 200: print >>sys.stderr, 'Error: unable to get hosts' return -1 r_data = resp.json() if 'DATA' not in r_data: print >>sys.stderr, 'Error: unable to get hosts' return -1 launch_server_htype = None for it in r_data['DATA']: if args['launch-server'] == it.get('IP', None): launch_server_htype = it.get('HTYPE', None) break if launch_server_htype is None: print >>sys.stderr, 'Error: launch_server %s not found' % \ args['launch-server'] return -1 if launch_server_htype != HOST_HTYPE_KVM: call_args = [] for key, value in args.items(): call_args.append('%s=%s' % (key, value)) if os.path.isfile('/usr/local/bin/mntnct'): call_cmd = 'mntnct' else: call_cmd = (os.path.dirname(os.path.realpath(__file__)) + '/mntnct') subprocess.call([call_cmd, 'vm.import'] + call_args) return url = TALKER_URL + 'domains/' try: resp = requests.get(url) except requests.exceptions.ConnectionError: print >>sys.stderr, 'Error: unable to connet to talker' return -1 if resp.status_code != 200: print >>sys.stderr, 'Error: unable to get domains' return -1 r_data = resp.json() if 'DATA' not in r_data: print >>sys.stderr, 'Error: unable to get domains' return -1 domain_uuid = None for it in r_data['DATA']: if args['domain'] == it.get('NAME', None): domain_uuid = it.get('LCUUID', None) break if domain_uuid is None: print >>sys.stderr, 'Error: domain %s not found' % args['domain'] return -1 try: user = data.User.get(data.User.email == args['user-email']) userid = user.id except Exception: print >>sys.stderr, 'Error: user %s not found' % \ args['user-email'] return -1 try: ps = data.OssProductSpec.get( data.OssProductSpec.plan_name == args['product-specification'], data.OssProductSpec.domain == domain_uuid) ps_lcuuid = ps.lcuuid except Exception: print >>sys.stderr, 'Error: product-specification %s not found' % \ args['product-specification'] return -1 vm_info = models.VMImport() vm_info.name = args['label'] vm_info.os = args['os'] vm_info.launch_server = args['launch-server'] vm_info.userid = userid vm_info.product_specification_lcuuid = ps_lcuuid vm_info.domain = domain_uuid resp = call_post_with_callback(TALKER_URL + 'vms/import/', data=vm_info.to_primitive()) if 'OPT_STATUS' not in resp or 'DESCRIPTION' not in resp: print >>sys.stderr, 'Error: response %s corrupted' % resp return -1 if resp['OPT_STATUS'] != 'SUCCESS': print >>sys.stderr, 'Error (%s): %s' % (resp['OPT_STATUS'], resp['DESCRIPTION']) return -1 print >>sys.stdout, '' print >>sys.stdout, 'NAME: %s' % resp['DATA']['NAME'] print >>sys.stdout, 'LCUUID: %s' % resp['DATA']['LCUUID'] print >>sys.stdout, 'STATE: %d' % resp['DATA']['STATE'] print >>sys.stdout, 'CPU: %d' % resp['DATA']['VCPU_NUM'] print >>sys.stdout, 'MEM_SIZE: %d M' % resp['DATA']['MEM_SIZE'] print >>sys.stdout, 'SYS_DISK_SIZE: %d G' % resp['DATA']['VDI_SYS_SIZE'] print >>sys.stdout, 'SYS_SR_NAME: %s' % resp['DATA']['VDI_SYS_SR_NAME'] return 0