def find_compartment_id(compartment, compartment_dict): """ Find the compartment ocid for compartment in the compartment dictinary. Parameters ---------- compartment: str The compartment name. compartment_dict: dict The dictionary containing data for all? compartments in this tenancy. Returns ------- str: the ocid of the compartment on success, raises an exception on failure. """ _logger.debug('__ Looking for the ocid of compartment %s', compartment) for _, v in list(compartment_dict.items()): for x in v: if x['name'] == compartment: compartment_data = x console_msg(msg='Compartment: %s' % compartment_data['name']) compartment_id = compartment_data['id'] return compartment_id raise OciMigrateException('Failed to find the ocid for %s' % compartment)
def os_banner(): """ Show OS banner. Returns ------- No return value. """ console_msg('OS is one of %s' % _os_type_tag_csl_tag_type_os_)
def unmount_part(devname): """ Unmount a partition from mountpoint from /mnt/<last part of device specification> and remove the mountpoint. Parameters ---------- devname: str The full path of the device. Returns ------- bool True on success, False otherwise. """ mntpoint = migrate_data.loopback_root + '/' + devname.rsplit('/')[-1] cmd = ['umount', mntpoint] _logger.debug('__ Running %s' % cmd) pause_msg(cmd) while True: try: _logger.debug('command: %s' % cmd) cmdret = system_tools.run_call_cmd(cmd) if cmdret == 0: _logger.debug('%s unmounted from %s' % (devname, mntpoint)) # # remove mountpoint if system_tools.exec_rmdir(mntpoint): _logger.debug('%s removed' % mntpoint) return True else: _logger.critical(' Failed to remove mountpoint %s' % mntpoint) raise OciMigrateException( 'Failed to remove mountpoint %s' % mntpoint) else: _logger.critical(' Failed to unmount %s: %d' % (devname, cmdret)) console_msg('Failed to unmount %s, error code %d.\n ' 'Please verify before continuing.' % (devname, cmdret)) retry = read_yn( 'Something prevented to complete %s, please ' 'verify and correct if possible. ' 'Press Y to retry, N to ignore.', waitenter=True) if not retry: break except Exception as e: _logger.critical(' Failed to unmount %s: %s' % (devname, str(e))) return False
def main(): """ Upload an image from on-premise to an object storage in the Oracle Cloud Infrastructure. Returns ------- int: 0 on success, raises exception on failure. """ # # set locale lc_all_to_set = get_config_data('lc_all') os.environ['LC_ALL'] = "%s" % lc_all_to_set _logger.debug('Locale set to %s', lc_all_to_set) # # command line cmdline_args = parse_args() # # input image if cmdline_args.input_image: image_path = cmdline_args.input_image.name console_msg(msg='\n Uploading %s at %s\n' % (os.path.basename(' '.join(sys.argv)), time.ctime())) else: raise OciMigrateException('Missing argument: input image path.') # # object storage bucket_name = cmdline_args.bucket_name # # output image if cmdline_args.output_name: output_name = cmdline_args.output_name else: output_name = os.path.splitext(os.path.basename(image_path))[0] # # yes migrate_data.yes_flag = cmdline_args.yes_flag # # verbose migrate_data.verbose_flag = cmdline_args.verbose_flag # # message console_msg(msg='Uploading %s to object storage %s in the Oracle Cloud ' 'Infrastructure as %s.' % (image_path, bucket_name, output_name)) # # collect data try: # # The object storage exist and data. object_storage_data = oci_cli_tools.bucket_exists(bucket_name) console_msg(msg='Object storage %s present.' % bucket_name) # # The object is present in object storage. if oci_cli_tools.object_exists(object_storage_data, output_name): raise OciMigrateException('Object %s already exists object ' 'storage %s.' % (output_name, bucket_name)) _logger.debug('Object %s does not yet exists in object storage %s', output_name, bucket_name) except Exception as e: exit_with_msg('Unable to upload %s to %s: %s' % (image_path, bucket_name, str(e))) # # Ask for confirmation to proceed. if not read_yn('\n Agree to proceed uploading %s to %s as %s?' % (image_path, bucket_name, output_name), waitenter=True, suppose_yes=migrate_data.yes_flag): exit_with_msg('\n Exiting.') # # Uploading the image to the Oracle Cloud Infrastructure. _, nb_columns = terminal_dimension() try: upload_progress = ProgressBar(nb_columns, 0.2, progress_chars=['uploading %s' % output_name]) # # Verify if object already exists. if oci_cli_tools.object_exists(object_storage_data, output_name): raise OciMigrateException('Object %s already exists object storage %s.' % (output_name, bucket_name)) _logger.debug('Object %s does not yet exists in object storage %s', output_name, bucket_name) # # Upload the image. console_msg(msg='\n Uploading %s, this might take a while....' % image_path) upload_progress.start() upload_result = oci_cli_tools.upload_image(image_path, bucket_name, output_name) _logger.debug('Upload result: %s', upload_result) console_msg(msg=' Finished....\n') upload_progress.stop() except Exception as e: _logger.error(' Error while uploading %s to %s: %s.', image_path, bucket_name, str(e)) exit_with_msg('*** ERROR *** Error while uploading %s to %s: %s.' % (image_path, bucket_name, str(e))) finally: # if progress thread was started, needs to be terminated. if system_tools.is_thread_running(upload_progress): upload_progress.stop()
def main(): """ Import image from object storage into custom images repository. Returns ------- int: 0 on success, raises exception on failure. """ # # set locale lc_all_to_set = get_config_data('lc_all') os.environ['LC_ALL'] = "%s" % lc_all_to_set _logger.debug('Locale set to %s', lc_all_to_set) # # command line cmdline_args = parse_args() console_msg(msg='Importing %s from %s into %s as %s and setting ' 'launch_mode to %s.' % (cmdline_args.image_name, cmdline_args.bucket_name, cmdline_args.bucket_name, cmdline_args.display_name, cmdline_args.launch_mode)) compartment = cmdline_args.compartment_name bucket = cmdline_args.bucket_name object_name = cmdline_args.image_name display_name = cmdline_args.display_name launch_mode = cmdline_args.launch_mode migrate_data.verbose_flag = cmdline_args.verbose_flag migrate_data.yes_flag = cmdline_args.yes_flag # # collect data try: # # oci configuration oci_config = migrate_tools.get_oci_config() _logger.debug('Found oci config file') # # compartment data for tenancy tenancy = oci_config['tenancy'] _logger.debug('Tenancy: %s', tenancy) compartment_dict = oci_cli_tools.get_tenancy_data(tenancy) # # object storage namespace os_namespace = oci_cli_tools.get_os_namespace() console_msg(msg='Object storage namespace: %s' % os_namespace) # # compartment ocid compartment_id = oci_cli_tools.find_compartment_id( compartment, compartment_dict) # # object storage exist and data object_storage_data = oci_cli_tools.bucket_exists(bucket) console_msg(msg='Object storage %s present.' % bucket) # # object present in object storage if oci_cli_tools.object_exists(object_storage_data, object_name): _logger.debug('Object %s present in object_storage %s', object_name, bucket) else: raise OciMigrateException( 'Object %s does not exist in the object ' 'storage %s.' % (object_name, bucket)) # # display name present if oci_cli_tools.display_name_exists(display_name, compartment_id): raise OciMigrateException('Image with name %s already exists.' % display_name) _logger.debug('%s does not exist', display_name) except Exception as e: exit_with_msg('Error while importing %s data: %s' % (object_name, str(e))) # # Ask for confirmation to proceed with upload. if not read_yn('\n Import %s to %s as %s' % (object_name, compartment, display_name), waitenter=True, suppose_yes=migrate_data.yes_flag): exit_with_msg('Exiting.\n') # # Start the import. try: _ = oci_cli_tools.import_image(object_name, display_name, compartment_id, os_namespace, bucket, launch_mode) except Exception as e: exit_with_msg('Failed to import %s: %s' % (object_name, str(e))) # # Follow up the import. finished = False _, nb_columns = terminal_dimension() import_progress = ProgressBar( nb_columns, 0.2, progress_chars=['importing %s' % display_name]) import_progress.start() try: while not finished: if oci_cli_tools.get_lifecycle_state(display_name, compartment_id) \ == 'AVAILABLE': finished = True except Exception as e: _logger.error('Failed to follow up on the import of %s, giving up: %s', display_name, str(e)) if system_tools.is_thread_running(import_progress): import_progress.stop() console_msg(msg='Done') return 0