Example #1
0
def customize(**kwargs):
    """Run virt-customize on the remote host

    All keyword arguments to this command are converted to command line
    arguments for virt-customize by converting underscores (_) to hyphens (-)
    and prepending double hyphens (--). This means that single-character flags
    will most probably not work.
    Two of the arguments maintain their original meaning but get special
    treatment:

    :param str add:                Specifying this argument is mandatory as the
                                   command is pretty useless without it
    :param str commands_from_file: This argument is meant to refer to a local
                                   file. The file is parsed, and its content is
                                   converted into arguments for the remote
                                   virt-customize command.

    Any files referred to by the local command file are either uploaded to the
    remote host (With the command arguments adjusted to point to the right
    remote file locations) or parsed locally and converted to arguments in case
    they are referred to by a 'commands-from-file' command. The
    'commands-from-shell' command can also be used in the command files to
    embed some locally-executed shell logic to generate more complex commands.
    See the fabric_ci.lib.virt.command_file.perform_ast_includes for full
    details of inclusion logic.
    """
    remote_files = []

    def file_map(local_file):
        """Upload local file to remote location and calculate remote name"""
        tmpfile = RemoteTempfile(directory=True)
        local_path = find_included_file(local_file.path, local_file.context)
        put(local_path=local_path, remote_path=tmpfile.name)
        remote_files.append(tmpfile)
        return join(tmpfile.name, basename(local_path))

    if 'add' not in kwargs:
        abort("The 'add' parameter must be specified")
    if 'commands_from_file' in kwargs:
        commands_from_file = expanduser(kwargs.pop('commands_from_file'))
        args_from_file = ast_to_command_params(swap_ast_used_files(
            perform_ast_includes(file_to_ast(commands_from_file)),
            file_map
        ))
    else:
        args_from_file = ()
    run(quote_and_join('virt-customize', *tuple(chain(
        dict_to_opts(kwargs), args_from_file
    ))))
def test_ast_to_command_params():
    ast = lines_to_ast((
        '# some comment',
        'run /some/script',
        'copy-in /copyed/dir:/dst/dir',
        'install sopme-pkg',
        'selinux-relabel',
    ))
    expected = (
        '--run', '/some/script',
        '--copy-in', '/copyed/dir:/dst/dir',
        '--install', 'sopme-pkg',
        '--selinux-relabel',
    )
    result = tuple(ast_to_command_params(ast))
    assert expected == result