Exemplo n.º 1
0
def form_terminate_volume_creator():
    '''
    Terminate de volume creator of the current zone.
    '''

    # initialize the control variable
    OK = True

    # print the header
    clib.clear_screen()
    clib.print_headers_with_environment('Volume operation - Terminate volume creator')

    # confirm the termination of the volume creator
    print(xlib.get_separator())
    OK = clib.confirm_action('The volume creator is going to be terminated.')

    # terminate the volume creator
    if OK:
        devstdout = xlib.DevStdOut(xcluster.terminate_cluster.__name__)
        xcluster.terminate_cluster(xlib.get_volume_creator_name(), True, devstdout, function=None, is_menu_call=False)

    # show continuation message 
    print(xlib.get_separator())
    input('Press [Intro] to continue ...')
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
def terminate_cluster(cluster_name,
                      force,
                      log,
                      function=None,
                      is_menu_call=True):
    '''
    Terminate a cluster.
    '''

    # initialize the control variable
    OK = True

    # get current region
    region_name = xconfiguration.get_current_region_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 a few 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 the cluster is running
    if OK:
        (master_state_code,
         master_state_name) = xec2.get_node_state(cluster_name,
                                                  node_name='master')
        if not force and master_state_code != 16:
            log.write(
                f'*** ERROR: The cluster {cluster_name} is not running. Its state is {master_state_code} ({master_state_name}).\n'
            )
            OK = False

    # warn that the requirements are OK
    if OK:
        log.write('Process requirements are OK.\n')

    # terminate cluster
    if OK:
        log.write(f'{xlib.get_separator()}\n')
        if cluster_name == xlib.get_volume_creator_name():
            log.write('Terminating the volume creator using StarCluster ...\n')
        else:
            log.write(
                'Terminating the cluster {0} using StarCluster ...\n'.format(
                    cluster_name))
        log.write('\n')
        if not force:
            command = '{0} --region={1} terminate --confirm {2}'.format(
                xlib.get_starcluster(), region_name, cluster_name)
        else:
            command = '{0} --region={1} terminate --force --confirm {2}'.format(
                xlib.get_starcluster(), region_name, cluster_name)
        rc = xlib.run_command(command, log)
        log.write('\n')
        if rc == 0:
            if cluster_name == xlib.get_volume_creator_name():
                log.write('The volume creator is terminated.\n')
            else:
                log.write('The cluster is terminated.\n')
        else:
            log.write('*** ERROR: Return code {0} in command -> {1}\n'.format(
                rc, command))
            OK = False

    # 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
    return OK
Exemplo n.º 4
0
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