def do_fpga_image_list(args): """Query FPGA images of a tenant""" kwargs = OrderedDict() if args.page is not None and args.size is not None: kwargs['page'] = args.page kwargs['size'] = args.size elif args.page is not None and args.size is None\ or args.page is None and args.size is not None: utils.print_err('Error: argument --page and --size ' 'must exist or not exist at the same time') return utils.check_param(**kwargs) status_code, reason, body = rest.fpga_image_list(*_get_config(), params=kwargs) if status_code != 200 or not isinstance(body, dict): raise FisException(_invalid_resp(status_code, reason, body)) fi_list = body.get('fpgaimages', []) _do_resp(status_code, reason) columns = [ 'id', 'name', 'status', 'protected', 'size', 'createdAt', 'description', 'metadata', 'message' ] utils.print_list(fi_list, columns)
def main(): utils.check_login_user() # parse input option argv = [encode.convert_to_unicode(a) for a in sys.argv[1:]] args = get_parser().parse_args(argv) # read and check args kwargs = {} if args.name is not None: kwargs['name'] = args.name if args.metadata is not None: kwargs['metadata'] = args.metadata if args.description is not None: kwargs['description'] = args.description if args.file_name is not None: kwargs['file_name'] = args.file_name if args.fpga_image_id is not None: kwargs['fpga_image_id'] = args.fpga_image_id if args.image_id is not None: kwargs['image_id'] = args.image_id if args.page is not None: kwargs['page'] = args.page if args.size is not None: kwargs['size'] = args.size try: utils.check_param(**kwargs) except Exception as e: utils.exit('Error: %s' % encode.exception_to_unicode(e)) # read and check config file config.read_config_and_verify() access_key = os.getenv('OS_ACCESS_KEY') secret_key = os.getenv('OS_SECRET_KEY') bucket_name = os.getenv('OS_BUCKET_NAME') region_id = os.getenv('OS_REGION_ID') domain_id = os.getenv('OS_DOMAIN_ID') project_id = os.getenv('OS_PROJECT_ID') obs_endpoint = os.getenv('OS_OBS_ENDPOINT') vpc_endpoint = os.getenv('OS_VPC_ENDPOINT') fis_endpoint = os.getenv('OS_FIS_ENDPOINT') try: # configure intranet dns of ecs config.configure_intranet_dns_ecs(region_id) # check bucket utils._check_bucket_acl_location(bucket_name, access_key, secret_key, obs_endpoint, region_id, domain_id) # check fis rest.fpga_image_relation_list(access_key, secret_key, project_id, region_id, fis_endpoint) except Exception as e: utils.exit('Error: %s' % encode.exception_to_unicode(e)) if kwargs: print('fis argument(s) and config file are OK') else: print('fis config file is OK')
def do_fpga_image_relation_delete(args): """Delete the relation of an FPGA image and an ECS image""" kwargs = OrderedDict() kwargs['fpga_image_id'] = args.fpga_image_id kwargs['image_id'] = args.image_id utils.check_param(**kwargs) status_code, reason = rest.fpga_image_relation_delete( *_get_config(), **kwargs) if status_code != 204: raise FisException(_invalid_resp(status_code, reason)) _do_resp(status_code, reason)
def do_get_log_file(args): """Get the log file of an FPGA image""" access_key = os.getenv('OS_ACCESS_KEY') secret_key = os.getenv('OS_SECRET_KEY') obs_endpoint = os.getenv('OS_OBS_ENDPOINT') kwargs = OrderedDict() kwargs['fpga_image_id'] = args.fpga_image_id utils.check_param(**kwargs) status_code, reason, body = rest.fpga_image_list(*_get_config(), params=kwargs) if status_code != 200 or not isinstance(body, dict): raise FisException(_invalid_resp(status_code, reason, body)) fi_list = body.get('fpgaimages', []) if not fi_list: raise FisException('FPGA Image [%s] does not exist' % args.fpga_image_id) fpga_image = fi_list[0] if fpga_image.get('status') not in ('active', 'error'): raise FisException( 'FPGA Image [%s] status [%s] is not "active" or "error"' % (args.fpga_image_id, fpga_image.get('status'))) log_directory = fpga_image.get('log_directory') if log_directory is None: raise FisException('FPGA Image [%s] has no log file' % args.fpga_image_id) print('Log directory is "%s"' % log_directory) l = log_directory.split(':') if len(l) != 2 or l[0] == '': raise FisException('Log directory [%s] is invalid' % log_directory) file_name = args.fpga_image_id + '_log.tar' if os.path.lexists(file_name): raise FisException( 'The file [%s] already exists in the current directory' % file_name) bucket_name = l[0] if l[1]: object_key = '%s/%s' % (l[1], file_name) else: object_key = file_name print('Downloading Log file from OBS') status_code, reason, filesize, time_diff = rest.get_log_file( access_key, secret_key, file_name, bucket_name, object_key, obs_endpoint) _do_resp(status_code, reason) print('Download %s bytes using %s second(s)' % (filesize, time_diff))
def do_fpga_image_relation_list(args): """Query FPGA image relations visible to a tenant""" kwargs = OrderedDict() if args.image_id is not None: kwargs['image_id'] = args.image_id if args.fpga_image_id is not None: kwargs['fpga_image_id'] = args.fpga_image_id if args.page is not None and args.size is not None: kwargs['page'] = args.page kwargs['size'] = args.size elif args.page is not None and args.size is None\ or args.page is None and args.size is not None: utils.print_err('Error: argument --page and --size ' 'must exist or not exist at the same time') return utils.check_param(**kwargs) status_code, reason, body = rest.fpga_image_relation_list(*_get_config(), params=kwargs) if status_code != 200 or not isinstance(body, dict): raise FisException(_invalid_resp(status_code, reason, body)) _do_resp(status_code, reason) relation_list = [] for relations in body.get('associations', []): image_id = relations.get('image_id', None) for fpga_image in relations.get('fpgaimages', []): relation = {} relation['image_id'] = image_id relation.update(fpga_image) relation['fpga_image_id'] = relation.get('id', None) relation_list.append(relation) columns = [ 'image_id', 'fpga_image_id', 'name', 'status', 'protected', 'size', 'createdAt', 'description', 'metadata', 'message' ] utils.print_list(relation_list, columns) if args.image_id is None and args.fpga_image_id is None: print( 'Tips: The FPGA image relations can only be obtained if at least one of the \033[31m--fpga-image-id\033[0m and \033[31m--image-id\033[0m arguments is specified, otherwise only an empty list is returned.' )
def do_fpga_image_delete(args): """Delete an FPGA image""" kwargs = OrderedDict() kwargs['fpga_image_id'] = args.fpga_image_id utils.check_param(**kwargs) if not args.force: ans = raw_input('Deleted fpga-image cannot be restored! ' 'Are you absolutely sure? (yes/no): ').strip() while ans != 'yes' and ans != 'no': ans = raw_input('please input yes or no: ').strip() if ans == 'no': print('cancel fpga-image-delete') return status_code, reason = rest.fpga_image_delete(*_get_config(), **kwargs) if status_code != 204: raise FisException(_invalid_resp(status_code, reason)) _do_resp(status_code, reason)
def do_fpga_image_create(args): """Create an FPGA image""" access_key = os.getenv('OS_ACCESS_KEY') secret_key = os.getenv('OS_SECRET_KEY') obs_endpoint = os.getenv('OS_OBS_ENDPOINT') bucket_name = os.getenv('OS_BUCKET_NAME') utils.check_dcp_file(args.dcp_file) kwargs = OrderedDict() kwargs['dcp_obs_path'] = args.dcp_obs_path if args.log_obs_directory is not None: kwargs['log_obs_directory'] = args.log_obs_directory kwargs['name'] = args.name if args.description is not None: kwargs['description'] = args.description utils.check_param(**kwargs) print('Uploading DCP file to OBS') _, _, filesize, time_diff = rest.put_dcp_file(access_key, secret_key, args.dcp_file, bucket_name, args.dcp_obs_path, obs_endpoint) print('Upload %s bytes using %s second(s)' % (filesize, time_diff)) print('Creating FPGA image to FIS') fpga_image = { 'dcp_location': '%s:%s' % (bucket_name, args.dcp_obs_path), 'log_directory': args.log_obs_directory, 'name': args.name, 'description': args.description } status_code, reason, body = rest.fpga_image_create(*_get_config(), fpga_image=fpga_image) if status_code != 200 or not isinstance(body, dict): raise FisException(_invalid_resp(status_code, reason, body)) fi = body.get('fpga_image', {}) _do_resp(status_code, reason) print('id: %s\nstatus: %s' % (fi.get('id'), fi.get('status')))
def do_fpga_image_register(args): """Register an FPGA image""" object_key = utils.check_fpga_image_file(args.fpga_image_file) access_key = os.getenv('OS_ACCESS_KEY') secret_key = os.getenv('OS_SECRET_KEY') obs_endpoint = os.getenv('OS_OBS_ENDPOINT') bucket_name = os.getenv('OS_BUCKET_NAME') kwargs = OrderedDict() kwargs['name'] = args.name kwargs['metadata'] = args.metadata if args.description is not None: kwargs['description'] = args.description utils.check_param(**kwargs) kwargs['location'] = '%s:%s' % (bucket_name, object_key) kwargs['metadata'] = json.loads(args.metadata, object_pairs_hook=OrderedDict) print('Uploading FPGA image to OBS') status_code, reason, filesize, time_diff = rest.put_object( access_key, secret_key, args.fpga_image_file, bucket_name, object_key, obs_endpoint) if status_code != 200: raise FisException("Upload FPGA image file to OBS failed: %s %s" % (status_code, reason)) print('Upload %s bytes using %s seconds' % (filesize, time_diff)) print('Registering FPGA image to FIS') status_code, reason, body = rest.fpga_image_register(*_get_config(), fpga_image=kwargs) if status_code != 200 or not isinstance(body, dict): raise FisException(_invalid_resp(status_code, reason, body)) fi = body.get('fpga_image', {}) _do_resp(status_code, reason) print('id: %s\nstatus: %s' % (fi.get('id'), fi.get('status')))