def clean(image_name):
        Image.clean(image_name)

        IMAGES_DIRECTORY = LivenetImage.IMAGES_DIRECTORY + image_name + '/'
        WORKING_DIRECTORY = LivenetImage.WORKING_DIRECTORY + image_name + '/'
        MOUNT_DIRECTORY = WORKING_DIRECTORY + 'mnt/'

        # Cleanings for mount directories
        if os.path.isdir(MOUNT_DIRECTORY):
            logging.debug(MOUNT_DIRECTORY + ' is a directory')
            logging.debug('Executing \'umount ' + MOUNT_DIRECTORY + '*\'')
            os.system('umount ' + MOUNT_DIRECTORY + '*')

            if os.path.ismount(MOUNT_DIRECTORY):
                logging.debug(MOUNT_DIRECTORY + ' is a mount point')
                logging.debug('Executing \'umount ' + MOUNT_DIRECTORY + '\'')
                os.system('umount ' + MOUNT_DIRECTORY)

            logging.debug('Executing \'rm -rf ' + MOUNT_DIRECTORY + '\'')
            shutil.rmtree(MOUNT_DIRECTORY)

        # Try logging.debuging image working directory
        if os.path.isdir(WORKING_DIRECTORY):
            logging.debug(WORKING_DIRECTORY + ' is a directory')
            logging.debug('Executing \'rm -rf ' + WORKING_DIRECTORY + '\'')
            shutil.rmtree(WORKING_DIRECTORY)

        # Try logging.debuging image base directory
        if os.path.isdir(IMAGES_DIRECTORY):
            logging.debug(IMAGES_DIRECTORY + ' is a directory')
            logging.debug('Executing \'rm -rf ' + IMAGES_DIRECTORY + '\'')
            shutil.rmtree(IMAGES_DIRECTORY)
Beispiel #2
0
    def clean(image_name):
        Image.clean(image_name)

        # Cleaning image base directory
        if os.path.isdir(Image.IMAGES_DIRECTORY + image_name):
            logging.debug(Image.IMAGES_DIRECTORY + image_name +
                          ' is a directory')
            logging.debug('Executing \'rm -rf ' + Image.IMAGES_DIRECTORY +
                          image_name + '\'')
            shutil.rmtree(Image.IMAGES_DIRECTORY + image_name)

        # Cleaning all other related files and directories...
        if os.path.isdir('/diskless/demo_directory_' + image_name):
            logging.debug('/diskless/demo_directory_' + image_name +
                          ' is a directory')
            logging.debug('Executing \'rm -rf /diskless/demo_directory_' +
                          image_name + '\'')
            shutil.rmtree('/diskless/demo_directory_' + image_name)

        # We need to try to delete the demo_file.txt file in the clean method.
        # In fact the generation of the demo image can be halted before that the
        # normal process of DemoImage image creation removed demo_file.txt.
        if os.path.isfile('/diskless/demo_file_to_remove_' + image_name +
                          '.txt'):
            logging.debug('/diskless/demo_file_to_remove_' + image_name +
                          '.txt is a file')
            logging.debug('Executing \'rm -f /diskless/demo_file_to_remove_' +
                          image_name + '.txt\'')
            os.remove('/diskless/demo_file_to_remove_' + image_name + '.txt')
Beispiel #3
0
    def clean(image_name):
        Image.clean(image_name)

        if os.path.isdir(Image.IMAGES_DIRECTORY + image_name):
            logging.debug(Image.IMAGES_DIRECTORY + image_name +
                          ' is a directory')
            logging.debug('Executing \'rm -rf ' + Image.IMAGES_DIRECTORY +
                          image_name + '\'')
            shutil.rmtree(Image.IMAGES_DIRECTORY + image_name)

        if os.path.isdir(NfsGoldenImage.NFS_DIRECTORY + image_name):
            logging.debug(NfsGoldenImage.NFS_DIRECTORY + image_name +
                          ' is a directory')
            logging.debug('Executing \'rm -rf ' +
                          NfsGoldenImage.NFS_DIRECTORY + image_name + '\'')
            shutil.rmtree(NfsGoldenImage.NFS_DIRECTORY + image_name)
def cli_create_livenet_image():

    # Get available kernels
    kernel_list = KernelManager.get_available_kernels()

    # If there are no kernels aise an exception
    if not kernel_list:
        raise UserWarning('No kernel available')

    # Condition to test if image name is compliant
    while True:

        printc('[+] Give a name for your image', Color.GREEN)
        # Get new image name
        selected_image_name = input('-->: ').replace(" ", "")

        if selected_image_name == '':
            raise UserWarning('Image name cannot be empty !')

        if not ImageManager.is_image(selected_image_name):
            break

        # Else
        print('Image ' + selected_image_name +
              ' already exist, use another image name.')

    # Select the kernel to use
    printc('\n[+] Select your kernel:', Color.GREEN)
    selected_kernel = select_from_list(kernel_list)

    # Manage password
    printc('\n[+] Give a password for your image', Color.GREEN)
    selected_password = input(
        'Enter clear root password of the new image: ').replace(" ", "")

    # Select livenet type
    types_list = [
        'Standard: core (~1.3Gb)',
        'Small: openssh, dnf and NetworkManager (~300Mb)',
        'Minimal: openssh only (~270Mb)'
    ]
    get_type = select_from_list(types_list)

    if get_type == 'Standard: core (~1.3Gb)':
        selected_type = LivenetImage.Type.STANDARD
    elif get_type == 'Small: openssh, dnf and NetworkManager (~300Mb)':
        selected_type = LivenetImage.Type.SMALL
    elif get_type == 'Minimal: openssh only (~270Mb)':
        selected_type = LivenetImage.Type.CORE
    else:
        raise UserWarning('Not a valid choice !')

    # Select livenet size
    printc(
        '\nPlease choose image size:\n(supported units: M=1024*1024, G=1024*1024*1024)\n(Examples: 5120M or 5G)',
        Color.GREEN)
    selected_size = input('-->: ')

    # Check and convert the size
    image_size = cli_get_size(selected_size)

    # Check size compliance with livenet image expected size limits
    if int(image_size) < (LivenetImage.MIN_LIVENET_SIZE) or int(image_size) > (
            LivenetImage.MAX_LIVENET_SIZE):
        raise UserWarning('\nSize out of limits !')

    # Inject ssh key or not
    printc(
        '\nEnter path to SSH public key (left empty to disable key injection)',
        Color.GREEN)
    selected_ssh_pub_key = input('-->: ')
    if selected_ssh_pub_key != '' and not os.path.exists(selected_ssh_pub_key):
        raise UserWarning('\nSSH public key not found ' + selected_ssh_pub_key)
    if selected_ssh_pub_key == '':
        selected_ssh_pub_key = None

    # Activate SELinux or not
    printc('\nActivate SELinux inside the image (yes/no) ?', Color.GREEN)
    answer_selinux = input('-->: ')
    if answer_selinux == 'yes':
        selinux = True
    elif answer_selinux == 'no':
        selinux = False
    else:
        raise UserWarning('\nInvalid input !')

    # Propose to user to install additional packages
    printc(
        '\nDo you want to customize your image with additional packages (yes/no) ? ',
        Color.GREEN)
    choice = input('-->: ')
    # Install additional packages
    if choice == 'yes':
        # Get package list from user
        additional_packages = Image.cli_add_packages()
    # Don't install additional packages
    elif choice == 'no':
        additional_packages = None
    else:
        raise UserWarning('\nInvalid entry !')

    # Propose to user to specify a release version
    printc(
        '\nSpecify a release version for installation (left empty to not use the --relasever option)',
        Color.GREEN)
    release_version = input('-->: ')
    if release_version == '':
        release_version = None

    # Propose to optimize image packages
    printc(
        '\nDo you wish tool try to optimize image by using aggressive packages dependencies parameters ? ',
        Color.GREEN)
    printc(
        'Note that this may collide with additional packages if asked for. (yes/no) ? ',
        Color.GREEN)
    answer_optimize = input('-->: ')
    if answer_optimize == 'yes':
        optimize = True
    elif answer_optimize == 'no':
        optimize = False
    else:
        raise UserWarning('\nInvalid input !')

    # Confirm image creation
    printc(
        '\n[+] Would you like to create a new livenet image with the following attributes: (yes/no)',
        Color.GREEN)
    print('  ├── Image name: \t\t' + selected_image_name)
    print('  ├── Image password: \t\t' + selected_password)
    print('  ├── Image kernel: \t\t' + selected_kernel)
    print('  ├── Image type: \t\t' + get_type)
    print('  ├── Image size: \t\t' + selected_size)
    print('  ├── Optimize packages: \t' + str(optimize))

    # Print ssh pub key packages if there is one
    if selected_ssh_pub_key is not None:
        print('  ├── SSH pubkey: \t\t' + selected_ssh_pub_key)

    # Print additional packages if there is
    if additional_packages is not None:
        print('  ├── Additional packages: \t' + str(additional_packages))

    # Print release version if there is one
    if release_version is not None:
        print('  ├── Release version: \t\t' + release_version)

    print('  └── Enable SELinux: \t\t' + str(selinux))

    confirmation = input('-->: ').replace(" ", "")

    if confirmation == 'yes':
        # Create the image object
        LivenetImage(selected_image_name, selected_password, selected_kernel,
                     selected_type, image_size, additional_packages,
                     selected_ssh_pub_key, selinux, release_version, optimize)
        printc('\n[OK] Done.', Color.GREEN)

    elif confirmation == 'no':
        printc('\n[+] Image creation cancelled, return to main menu.',
               Color.YELLOW)
        return

    else:
        raise UserWarning('\nInvalid confirmation !')
Beispiel #5
0
def cli_create_staging_image():

    # Get available kernels
    kernel_list = KernelManager.get_available_kernels()

    # If there are no kernels aise an exception
    if not kernel_list:
        raise UserWarning('No kernel available')

    # Condition to test if image name is compliant
    while True:

        printc('[+] Give a name for your image', Color.GREEN)
        # Get new image name
        selected_image_name = input('-->: ').replace(" ", "")

        if selected_image_name == '':
            raise UserWarning('Image name cannot be empty !')

        if not ImageManager.is_image(selected_image_name):
            break

        # Else
        print('Image ' + selected_image_name +
              ' already exist, use another image name.')

    # Select the kernel to use
    printc('\n[+] Select your kernel:', Color.GREEN)
    selected_kernel = select_from_list(kernel_list)

    # Manage password
    printc('\n[+] Give a password for your image', Color.GREEN)
    selected_password = input(
        'Please enter clear root password of the new image: ').replace(
            " ", "")

    # Propose to user to install additional packages
    printc(
        '\nDo you want to customize your image with additional packages? (yes/no)',
        Color.GREEN)
    choice = input('-->: ')
    # Install addictional packages
    if choice == 'yes':
        # Get package list from user
        additional_packages = Image.cli_add_packages()
    # Don't install additional packages
    elif choice == 'no':
        additional_packages = None
    else:
        raise UserWarning('\nInvalid entry !')

    # Propose to user to specify a release version
    printc(
        '\nSpecify a release version for installation (left empty to not use the --relasever option)',
        Color.GREEN)
    release_version = input('-->: ')
    if release_version == '':
        release_version = None

    # Confirm image creation
    printc(
        '\n[+] Would you like to create a new nfs staging image with the following attributes: (yes/no)',
        Color.GREEN)
    print('  ├── Image name: \t\t' + selected_image_name)
    print('  ├── Image password : \t\t' + selected_password)

    # Print additional packages if there is
    if additional_packages is not None:
        print('  ├── Additional packages: \t' + str(additional_packages))

    # Print release version if there is one
    if release_version is not None:
        print('  ├── Release version: \t\t' + release_version)

    print('  └── Image kernel: \t\t' + selected_kernel)

    confirmation = input('-->: ').replace(" ", "")

    if confirmation == 'yes':
        # Create the image object
        NfsStagingImage(selected_image_name, selected_password,
                        selected_kernel, additional_packages, release_version)
        printc('\n[OK] Done.', Color.GREEN)

    elif confirmation == 'no':
        printc('\n[+] Image creation cancelled, return to main menu.',
               Color.YELLOW)
        return

    else:
        raise UserWarning('\nInvalid confirmation !')