def form_remove_volume(): ''' Remove a volume in the current zone. ''' # initialize the control variable OK = True # print the header clib.clear_screen() clib.print_headers_with_environment('Volume operation - Remove volume') # get current zone name zone_name = xconfiguration.get_current_zone_name() # get the volume name print(xlib.get_separator()) volume_name = cinputs.input_volume_name(zone_name, template_name='', help=True, help_type='created') # confirm the removal of the volume print(xlib.get_separator()) OK = clib.confirm_action('The volume is going to be removed.') # remove the volume if OK: devstdout = xlib.DevStdOut(xvolume.remove_volume.__name__) OK = xvolume.remove_volume(volume_name, devstdout, function=None) # show continuation message print(xlib.get_separator()) input('Press [Intro] to continue ...')
def form_delink_volume_from_template(): ''' Delink a volume from a cluster template ''' # initialize the control variable OK = True # print the header clib.clear_screen() clib.print_headers_with_environment('Configuration - Delink volume in a cluster template') # get current zone name zone_name = xconfiguration.get_current_zone_name() # get the template name and the volume name print(xlib.get_separator()) template_name = cinputs.input_template_name(volume_creator_included=False, help=True, is_all_possible=True) volume_name = cinputs.input_volume_name(zone_name, template_name, help=True, help_type='linked') # verify there is some volume linked to the cluster template if volume_name == '': print(xlib.get_separator()) print('*** WARNING: There is not any volume linked to the cluster template.') OK = False # confirm the exclusion of the volume if OK: print(xlib.get_separator()) if template_name == 'all': OK = clib.confirm_action('The volume {0} is going to be delinked from every template.'.format(volume_name)) else: OK = clib.confirm_action('The volume {0} is going to be delinked from the template {0}.'.format(volume_name, template_name)) # delink a volume in a cluster template if OK: print(xlib.get_separator()) print('The volume {0} is being delinked from the cluster template {1}.'.format(volume_name, template_name)) devstdout = xlib.DevStdOut(xconfiguration.delink_volume_from_template.__name__) (OK, error_list) = xconfiguration.delink_volume_from_template(template_name, volume_name, devstdout, function=None) # show continuation message print(xlib.get_separator()) input('Press [Intro] to continue ...')
def form_mount_volume(): ''' Mount a volume in a node. ''' # initialize the control variable OK = True # print the header clib.clear_screen() clib.print_headers_with_environment('Volume operation - Mount volume in a node') # get current zone name zone_name = xconfiguration.get_current_zone_name() # get the cluster name and node name print(xlib.get_separator()) if xec2.get_running_cluster_list(volume_creator_included=False) != []: cluster_name = cinputs.input_cluster_name(volume_creator_included=False, help=True) node_name = cinputs.input_node_name(cluster_name, new=False, is_master_valid=True, help=True) else: print('WARNING: There is not any running cluster.') OK = False # get the volume name, AWS device file and directory path if OK: volume_name = cinputs.input_volume_name(zone_name, template_name='', help=True, help_type='created') aws_device_file = cinputs.input_device_file(node_name, volume_name) mounting_path = cinputs.input_mounting_path(node_name, aws_device_file) # confirm the mounting of the volume if OK: print(xlib.get_separator()) OK = clib.confirm_action('The volume is going to be mounted.') # mount the volume in the node if OK: devstdout = xlib.DevStdOut(xvolume.mount_volume.__name__) xvolume.mount_volume(cluster_name, node_name, volume_name, aws_device_file, mounting_path, devstdout, function=None, is_menu_call=True) # show continuation message print(xlib.get_separator()) input('Press [Intro] to continue ...')
def form_review_volume_links(): ''' Review linked volumes of cluster templates in order to remove linked volumes that do not currently exist. ''' # initialize the control variable and the error list OK = True error_list = [] # print the header clib.clear_screen() clib.print_headers_with_environment('Configuration - Review volumes linked to cluster templates') # get current zone name zone_name = xconfiguration.get_current_zone_name() # get the NGScloud confign file ngscloud_config_file = xconfiguration.get_ngscloud_config_file() # verify if there are any volumes linked if xconfiguration.get_volumes_dict() == {}: print(xlib.get_separator()) print('WARNING: There is not any volume linked.') OK = False # confirm the review of volumes links if OK: print(xlib.get_separator()) OK = clib.confirm_action('The file {0} is going to be reviewed in order to remove volumes linked which are not currently created in the zone {1}.'.format(ngscloud_config_file, zone_name)) # review volumen link if OK: devstdout = xlib.DevStdOut(xconfiguration.review_volume_links.__name__) (OK, error_list) = xconfiguration.review_volume_links(zone_name, devstdout, function=None) # show continuation message or exit of application print(xlib.get_separator()) if not OK and error_list != []: raise xlib.ProgramException('C001') else: input('Press [Intro] to continue ...')
def form_create_volume(): ''' Create a volume in the current zone. ''' # initialize the control variable OK = True # print the header clib.clear_screen() clib.print_headers_with_environment('Volume operation - Create volume') # get current zone name zone_name = xconfiguration.get_current_zone_name() # show sites related to EBS volumes print(xlib.get_separator()) print('You can consult the characteristics of the EBS volumes in:') print(' https://aws.amazon.com/ebs/details/') print('and the EBS pricing is detailed in:') print(' https://aws.amazon.com/ebs/pricing/') # get the cluster name, node name, volume name, volume type and volume size print(xlib.get_separator()) volume_name = cinputs.input_volume_name(zone_name, template_name='', help=False, help_type='created') volume_type = cinputs.input_volume_type() volume_size = cinputs.input_volume_size(volume_type) terminate_indicator = cinputs.input_terminate_indicator() # confirm the creation of the volume if OK: print(xlib.get_separator()) OK = clib.confirm_action('The volume is going to be created.') # create the volume if OK: devstdout = xlib.DevStdOut(xvolume.create_volume.__name__) OK = xvolume.create_volume(volume_name, volume_type, volume_size, terminate_indicator, devstdout, function=None) # show continuation message print(xlib.get_separator()) input('Press [Intro] to continue ...')
def print_headers_with_environment(process_name): ''' Print the headers of a screen with environmen information. ''' # print the project name, version and the process name title = f'{xlib.get_project_name()} v {xlib.get_project_version()} - {process_name}' line = '-' * len(title) print(f'+-{line}-+') print(f'| {title} |') print(f'+-{line}-+') print() # get current region and zone names region_name = xconfiguration.get_current_region_name() zone_name = xconfiguration.get_current_zone_name() # print the environment and the current region and zone names print( f'Environment: {xconfiguration.environment} - Region: {region_name} - Zone: {zone_name}' ) print()
def print_headers_with_environment(process_name): ''' Print the headers of a screen with environmen information. ''' # print the project name, version and the process name title = '{0} v {1} - {2}'.format(xlib.get_project_name(), xlib.get_project_version(), process_name) line = '-' * len(title) print('+-{0}-+'.format(line)) print('| {0} |'.format(title)) print('+-{0}-+'.format(line)) print() # get current region and zone names region_name = xconfiguration.get_current_region_name() zone_name = xconfiguration.get_current_zone_name() # print the environment and the current region and zone names print('Environment: {0} - Region: {1} - Zone: {2}'.format( xconfiguration.environment, region_name, zone_name)) print()
def form_create_ngscloud_config_file(is_menu_call): ''' Create the NGScloud config file corresponding to the environment. ''' # initialize the control variable OK = True # print the header if is_menu_call: clib.clear_screen() clib.print_headers_with_environment('Configuration - Recreate TransciptomeCloud config file') # get current region and zone names region_name = xconfiguration.get_current_region_name() zone_name = xconfiguration.get_current_zone_name() # get basic AWS data and contact e-mail address from NGScloud config file (user_id, access_key_id, secret_access_key) = xconfiguration.get_basic_aws_data() email = xconfiguration.get_contact_data() # confirm or change the AWS data and contact e-mail address print(xlib.get_separator()) user_id = cinputs.input_user_id(user_id) access_key_id = cinputs.input_access_key_id(access_key_id) secret_access_key = cinputs.input_secret_access_key(secret_access_key) email = cinputs.input_email(email) # verify the AWS access key identification and the AWS secret access key print(xlib.get_separator()) print('Verifying the AWS access key identification and the AWS secret access key') OK = xec2.verify_aws_credentials(access_key_id, secret_access_key) if OK: print('The credentials are OK.') else: print('ERROR: The credentials are wrong. Please review your access key identification and secret access key in the AWS web.') if not is_menu_call: raise xlib.ProgramException('EXIT') # confirm the creation of the NGScloud config file if OK: if is_menu_call: print(xlib.get_separator()) OK = clib.confirm_action('The {0} config file is going to be created. The previous files will be lost.'.format(xlib.get_project_name())) # create the NGScloud config file corresponding to the environment if OK: print(xlib.get_separator()) print('The file {0} is being created ...'.format(xconfiguration.get_ngscloud_config_file())) (OK, error_list) = xconfiguration.create_ngscloud_config_file(user_id, access_key_id, secret_access_key, email) if OK: print('The config file is created with default values.') print() print('You can modify the conection data and contact e-mail address in:') print(' "Cloud control" -> "Configuration" -> "Update connection data and contact e-mail"') print('The assigned region and zone are {0} and {1}, respectively. You can modify them in:'.format(xconfiguration.get_default_region_name(), xconfiguration.get_default_zone_name())) print(' "Cloud control" -> "Configuration" -> "Update region and zone data"') else: for error in error_list: print(error) raise xlib.ProgramException('C001') # show continuation message print(xlib.get_separator()) input('Press [Intro] to continue ...')
def create_cluster(template_name, cluster_name, log, function=None, is_menu_call=True): ''' Create a cluster from a template name. ''' # initialize the control variable OK = True # initialize the state variables master_state_code = '' master_state_name = '' # get current region and zone names region_name = xconfiguration.get_current_region_name() zone_name = xconfiguration.get_current_zone_name() # warn that the log window does not have to be closed if not isinstance(log, xlib.DevStdOut) and is_menu_call: log.write( 'This process might take several minutes. Do not close this window, please wait!\n' ) # warn that the requirements are being verified log.write(f'{xlib.get_separator()}\n') log.write('Checking process requirements ...\n') # check that the cluster is defined in the NGScloud config file if OK: if not xconfiguration.is_template_defined(template_name): log.write( '*** ERROR: The cluster {0} is not defined in the {1} config file.\n' .format(cluster_name, xlib.get_project_name())) OK = False # check that the cluster mode is None if OK: if xec2.get_cluster_mode(cluster_name) is not None: log.write('*** ERROR: There is a cluster or a instance running.\n') OK = False # check that the zone is available if OK: if not xec2.is_zone_available(region_name, zone_name): log.write( '*** ERROR: The zone name {0} is not available.\n'.format( zone_name)) OK = False # warn that the requirements are OK if OK: log.write('Process requirements are OK.\n') # create the cluster if OK: log.write(f'{xlib.get_separator()}\n') if cluster_name == xlib.get_volume_creator_name(): log.write('Creating the volume creator using StarCluster ...\n') else: log.write( 'Creating the cluster {0} using StarCluster ...\n'.format( cluster_name)) log.write('\n') if template_name == xlib.get_volume_creator_name(): command = '{0} --region={1} start --availability-zone={2} --cluster-template={3} --disable-queue {4}'.format( xlib.get_starcluster(), region_name, zone_name, template_name, cluster_name) else: command = '{0} --region={1} start --availability-zone={2} --cluster-template={3} {4}'.format( xlib.get_starcluster(), region_name, zone_name, template_name, cluster_name) rc = xlib.run_command(command, log) log.write('\n') if rc == 0: (master_state_code, master_state_name) = xec2.get_node_state(cluster_name, node_name='master') if cluster_name == xlib.get_volume_creator_name(): log.write('The volume creator is created.\n') else: log.write('The cluster is created.\n') else: log.write('*** ERROR: Return code {0} in command -> {1}\n'.format( rc, command)) log.write('***') log.write( '*** You have to terminate {0} (option "Force termination of a cluster")\n' .format(cluster_name)) log.write('*** and create it again.\n') OK = False # install infrastructure software in every node of the cluster if OK: if cluster_name != xlib.get_volume_creator_name(): cluster_node_list = xec2.get_cluster_node_list(cluster_name) for node_name in cluster_node_list: OK = xnode.install_node_infrastructure_software( cluster_name, node_name, log) # warn that the log window can be closed if not isinstance(log, xlib.DevStdOut) and is_menu_call: log.write(f'{xlib.get_separator()}\n') log.write('You can close this window now.\n') # execute final function if function is not None: function() # return the control variable and the state return (OK, master_state_code, master_state_name)
def create_volume(volume_name, volume_type, volume_size, terminate_indicator, log, function=None): ''' Create a volume in the current zone. ''' # initialize the control variable OK = True # get the volume creator name volume_creator_name = xlib.get_volume_creator_name() # warn that the log window must not be closed if not isinstance(log, xlib.DevStdOut): log.write( 'This process might take a few minutes. Do not close this window, please wait!\n' ) # verify the volume creator is running if OK: log.write('{0}\n'.format(xlib.get_separator())) log.write('Verifing if volume creator is running ...\n') (master_state_code, master_state_name) = xec2.get_node_state(volume_creator_name, 'master') if master_state_code == 16: log.write('The volume creator is running.\n') else: log.write( '*** WARNING: The volume creator is not running. It will be created.\n' ) (OK, master_state_code, master_state_name) = xcluster.create_cluster(volume_creator_name, volume_creator_name, log, function=None, is_menu_call=False) # get the master node identification if OK: node_id = xec2.get_node_id(volume_creator_name, 'master') if node_id == '': log.write( '*** ERROR: The master identification of the volume creator not has been got.\n' ) OK = False # create the SSH client connection log.write('{0}\n'.format(xlib.get_separator())) log.write('Connecting SSH client ...\n') (OK, error_list, ssh_client) = xssh.create_ssh_client_connection(volume_creator_name, 'master') if OK: log.write('The SSH client is connected.\n') else: for error in error_list: log.write('{0}\n'.format(error)) # warn that the requirements are being verified log.write('{0}\n'.format(xlib.get_separator())) log.write('Verifying process requirements ...\n') # get current region and zone names region_name = xconfiguration.get_current_region_name() zone_name = xconfiguration.get_current_zone_name() # verify that the zone is available if OK: if not xec2.is_zone_available(region_name, zone_name): log.write( '*** ERROR: The zone {0} is not available in the region {1}.\n' .format(zone_name, region_name)) OK = False # verify that the volume is not created if OK: if xec2.is_volume_created(volume_name, zone_name): log.write( '*** WARNING: The volume {0} is already created.\n'.format( volume_name)) OK = False # warn that the requirements are OK if OK: log.write('Process requirements are OK.\n') # create the volume if OK: log.write('{0}\n'.format(xlib.get_separator())) log.write('Creating volume {0} ...\n'.format(volume_name)) (OK, volume_id) = xec2.create_volume(volume_name, volume_type, volume_size) if OK: log.write( 'The volume is created with the identification {0}.\n'.format( volume_id)) else: log.write('*** ERROR: The volume is not created.\n') # wait for the volume status to be available if OK: log.write('{0}\n'.format(xlib.get_separator())) log.write('Waiting for the volume state to be available ...\n') i = 0 available_state_timeout = 120 while True: i += 1 volume_status = xec2.get_volume_state(volume_name, zone_name) time.sleep(1) if volume_status == 'available': log.write('The volume is now available.\n') break elif i > available_state_timeout: log.write( '*** The volume is not available after {0} s.\n'.format( available_state_timeout)) Ok = False break # set the aws device and get de machine device if OK: aws_device = '/dev/sdp' machine_device = xlib.get_machine_device_file(aws_device) # attach the volume to the cluster if OK: log.write('{0}\n'.format(xlib.get_separator())) log.write( 'Attaching volume {0} to node master of volume creator ...\n'. format(volume_name)) OK = xec2.attach_volume(node_id, volume_id, aws_device) if OK: log.write('The volume is attached.\n') else: log.write('*** ERROR: The volume is not attached.\n') # wait for the volume attachment to be available if OK: log.write('{0}\n'.format(xlib.get_separator())) log.write('Waiting for the volume attachment to be available ...\n') i = 0 attachment_timeout = 120 while True: i += 1 volume_attachments = xec2.get_volume_attachments( volume_name, zone_name) if volume_attachments != []: log.write('The volume attachment is now available.\n') break elif i > attachment_timeout: log.write( '*** ERROR: The volume attachment is not available after {0} s.\n' .format(attachment_timeout)) Ok = False break time.sleep(1) # wait for the device availability if OK: log.write('{0}\n'.format(xlib.get_separator())) log.write('Waiting for the availabity of the device {0} ...\n'.format( machine_device)) i = 0 format_timeout = 120 try: while True: i += 1 command = 'hdparm -z {0}'.format(machine_device) (OK, stdout, stderr) = xssh.execute_cluster_command(ssh_client, command) command = 'lsblk --list --noheadings --output NAME' (OK, stdout, stderr) = xssh.execute_cluster_command(ssh_client, command) for line in stdout: if line == os.path.basename(machine_device): log.write('The device is available.\n') raise xlib.BreakAllLoops if i > format_timeout: log.write( '*** ERROR: The device is not available after {0} s.\n' .format(format_timeout)) OK = False break time.sleep(1) except xlib.BreakAllLoops: pass # format the volume if OK: log.write('{0}\n'.format(xlib.get_separator())) log.write('Formating volume {0} to ext4 file system type ...\n'.format( volume_name)) command = 'mkfs -t ext4 {0} 2>&1; RC=$?; echo "RC=$RC"'.format( machine_device) (OK, stdout, stderr) = xssh.execute_cluster_command(ssh_client, command) if stdout[len(stdout) - 1] == 'RC=0': log.write('The volume is formatted.\n') OK = True else: log.write('*** ERROR: Wrong command ---> {0}\n'.format(command)) OK = False # detach the volume to the cluster if OK: log.write('{0}\n'.format(xlib.get_separator())) log.write( 'Detaching volume {0} from node master of volume creator ...\n'. format(volume_name)) OK = xec2.detach_volume(node_id, volume_id, aws_device) if OK: log.write('The volume is detached.\n') else: log.write('*** ERROR: The volume is not detached.\n') # terminate volume creator if OK: if terminate_indicator: OK = xcluster.terminate_cluster(volume_creator_name, True, log, function=None, is_menu_call=False) else: log.write('{0}\n'.format(xlib.get_separator())) log.write('You do not indicate to terminate the volume creator.\n') log.write( 'Remember to terminate it when you finish to create the volumes!!!\n' ) # close the SSH client connection if OK: log.write('{0}\n'.format(xlib.get_separator())) log.write('Closing the SSH client connection ...\n') xssh.close_ssh_client_connection(ssh_client) log.write('The connection is closed.\n') # warn that the log window can be closed if not isinstance(log, xlib.DevStdOut): log.write('{0}\n'.format(xlib.get_separator())) log.write('You can close this window now.\n') # execute final function if function is not None: function() # return the control variable return OK
def remove_volume(volume_name, log, function=None): ''' Delete a volume in the current zone. ''' # initialize the control variable OK = True # warn that the log window must not be closed if not isinstance(log, xlib.DevStdOut): log.write( 'This process might take a few minutes. Do not close this window, please wait!\n' ) # warn that the requirements are being verified log.write('{0}\n'.format(xlib.get_separator())) log.write('Verifying process requirements ...\n') # get current region and zone names region_name = xconfiguration.get_current_region_name() zone_name = xconfiguration.get_current_zone_name() # verify that the zone is available if OK: if not xec2.is_zone_available(region_name, zone_name): log.write( '*** ERROR: The zone {0} is not available in the region {1}.\n' .format(zone_name, region_name)) OK = False # verify that the volume is available if OK: volume_status = xec2.get_volume_state(volume_name, zone_name) if volume_status != 'available': log.write( '*** ERROR: The volume {0} is not available in the zone {1}.\n' .format(volume_name, zone_name)) OK = False # verify that the volume is not linked to any cluster templates if OK: if volume_name in xconfiguration.get_volume_names_list(): log.write( '*** ERROR: The volume is linked to some cluster templates.\n') OK = False # warn that the requirements are OK if OK: log.write('Process requirements are OK.\n') # delete the volume if OK: log.write('{0}\n'.format(xlib.get_separator())) log.write('Removing volume {0} ...\n'.format(volume_name)) OK = xec2.delete_volume(volume_name) if OK: log.write( 'The volume is been deleted. It may remain in the deleting state for several minutes.\n' ) else: log.write('*** ERROR: The volume is not deleted.\n') # warn that the log window can be closed if not isinstance(log, xlib.DevStdOut): log.write('{0}\n'.format(xlib.get_separator())) log.write('You can close this window now.\n') # execute final function if function is not None: function() # return the control variable return OK