Ejemplo n.º 1
0
def remove_universe(universe_name, verbose):
    """
    Removes a universe from the local machine.
    :param universe_name: The name of the universe which should be removed.
    :param verbose: True, for more verbose output.
    """
    print(_('remove-intro').format(universe_name))

    installation_configuration_path = installation_file_path(universe_name)

    if not os.path.exists(installation_configuration_path):
        print(_('universe-does-not-exist'))
        exit(1)

    installation_path = configuration.read_configuration(
        installation_configuration_path)['location']
    schroot_path = schroot_config_file_path(universe_name)

    if mount.contains_active_mount_point(session_mount_point(universe_name)) \
            or mount.contains_active_mount_point(installation_path):
        print(_('still-mounted-error'))
        exit(1)

    if os.path.exists(installation_path):
        run_command(['sudo', 'rm', '-rf', installation_path], 'deletion-phase',
                    verbose)
        print(_('installation-path-removed'))
    else:
        print(_('installation-path-does-not-exist'))

    if os.path.exists(schroot_path):
        run_command(['sudo', 'rm', schroot_path], 'deletion-phase', verbose)
        print(_('schroot-config-removed'))
    else:
        print(_('schroot-config-does-not-exist'))

    local_universe_path = local_universe_dir(universe_name)
    if os.path.exists(local_universe_path):
        run_command(['rm', '-rf', local_universe_path], 'deletion-phase',
                    verbose)
        print(_('local-cache-removed'))
    else:
        print(_('local-cache-does-not-exist'))
Ejemplo n.º 2
0
def update_universe(universe_name, verbose):
    """"
    Re-runs the Ansible playbook saved in the local Slingring home
    for the given universe on its chroot.
    :param universe_name: The name of the universe
    :param verbose: True, for more verbose output.
    """
    local_installation_path = local_universe_dir(universe_name)

    if not os.path.exists(local_installation_path):
        print(_('universe-does-not-exist'))
        exit(1)

    installation_configuration_path = installation_file_path(universe_name)
    universe_path = configuration.read_configuration(installation_configuration_path)['location']

    seed_universe_file_path = universe_file_path(universe_name)
    seed_dictionary = configuration.read_seed_file(seed_universe_file_path)

    print(_('update-start').format(universe_name))

    # retrieve ansible variable files from the user
    user_vars, user_secrets = gather_variables_from_user(seed_dictionary)

    print_spaced(_('coffee-time'))

    user_name = user.get_user()
    user_group = user.get_user_group()
    user_home = user_home_in_chroot(user_name)

    slingring_vars = create_slingring_vars_dict(user_name, user_group, user_home, seed_dictionary['mirror'],
                                                universe_name,
                                                seed_dictionary['version'])

    chroot.run_ansible(universe_name, universe_path, user_vars, user_secrets,
                       slingring_vars, verbose)

    if ' ' in universe_name:
        quote = '"'
    else:
        quote = ''

    print_spaced(_('update-done').format(quote, universe_name, quote))
Ejemplo n.º 3
0
def create_seed(args):
    """"
    Creates a new universe seed from a template.
    :param args: The command line arguments as parsed by argparse.
                 This is expected to contain the following information:
                    - name: The intended name for the seed.
                    - directory: The root directory in which the seed should be created.
                                 The complete resulting directory is root + name.
                    - template: The template name (e.g. 'default'). The template will be
                                loaded from the local home. If it cannot be found in the
                                local home, it will be taken from the global share.
    """
    universe_name = args.name
    root_dir = args.directory
    template_name = args.template

    seed_dir = os.path.join(args.directory, args.name)

    _validate_template(template_name)

    if os.path.exists(seed_dir):
        print(_('directory-exists').format(universe_name, root_dir))
        exit(1)

    _copy_base_image(universe_name, template_name, root_dir)

    # Fill in templates, if filters are defined.
    template_path = template_file_location(template_name)
    if os.path.exists(template_path):
        template_config = read_configuration(template_path)
        template_filter_expressions = template_config.get(
            'template_filter', [])
        if template_filter_expressions:
            template_blacklist_expressions = template_config.get(
                'template_blacklist', [])
            result_dir = os.path.join(root_dir, universe_name)
            template_files = _find_template_files(
                result_dir, template_filter_expressions,
                template_blacklist_expressions)
            variables = _get_variable_dict(args.name)
            for file in template_files:
                _fill_in_template(file, variables)
Ejemplo n.º 4
0
def list_universes(verbose):
    """
    Lists all universes on the local machine.
    """
    multiverse_directory = _get_directories_in_multiverse()
    universe_list = []
    for universe in multiverse_directory:
        installation_file_path = paths.installation_file_path(universe)
        if os.path.exists(installation_file_path):
            install_path = configuration.read_configuration(
                installation_file_path)['location']
            if verbose:
                output = '   - {} ({})'.format(universe, install_path)
            else:
                output = '   - {}'.format(universe)
            universe_list.append(output)

    if universe_list:
        print(_('list-start'))
        for universe in universe_list:
            print(universe)
    else:
        print(_('no-universes-found'))
Ejemplo n.º 5
0
def upgrade_universe(seed_path, universe_name, verbose):
    """
    Replaces the local seed of an existing universe with a newer
    version and runs the Ansible playbook on the given universe's chroot.
    :param seed_path: The path to the universe seed directory as string.
    :param universe_name: The name of the universe
    :param verbose: True, for more verbose output.
    """
    old_universe_name = universe_name

    local_installation_path = local_universe_dir(old_universe_name)

    if not os.path.exists(local_installation_path):
        print(_('universe-does-not-exist'))
        exit(1)

    # read current information
    old_installation_configuration_path = installation_file_path(
        old_universe_name)
    universe_path = configuration.read_configuration(
        old_installation_configuration_path)['location']

    seed_universe_file_path = universe_file_path(old_universe_name)
    old_seed_dictionary = configuration.read_seed_file(seed_universe_file_path)

    # read new information
    source_seed_directory = get_seed_directory_from_argument(seed_path)
    source_seed_universe_path = source_universe_file_path(
        source_seed_directory)
    new_seed_dictionary = configuration.read_seed_file(
        source_seed_universe_path)
    new_universe_name = new_seed_dictionary['name']

    if not yes_no_prompt(
            _('upgrade-warning').format(new_seed_dictionary['version'],
                                        old_seed_dictionary['version'])):
        exit(1)
    else:
        print()

    # validate
    _validate_seed_dictionaries(old_seed_dictionary, new_seed_dictionary)
    if old_universe_name != new_universe_name:
        _validate_paths_for_collision(new_universe_name)

    # replace old seed with new seed
    run_command(['rm', '-rf', local_installation_path],
                'remove-local-seed-phase', verbose)
    copy_seed_to_local_home(source_seed_directory, new_universe_name)
    new_installation_configuration_path = installation_file_path(
        new_universe_name)
    configuration.write_installation_configuration(
        new_installation_configuration_path, universe_path)

    # take care of the schroot config in case of a universe rename
    if old_universe_name != new_universe_name:
        change_schroot_name(old_universe_name, new_universe_name,
                            schroot_config_file_path(old_universe_name),
                            schroot_config_file_path(new_universe_name),
                            'rename-schroot-phase', verbose)

    # run the new Ansible playbook on the universe as if we just updated
    update_universe(new_universe_name, verbose)