示例#1
0
def tower_send(asset):
    """
    Upload assets to Tower.

    Arguments:
        asset -- asset object or list of assets objects
    """
    source = asset if isinstance(asset, list) else [asset]

    sender = Sender(False)
    sender.send(source, None, None, 'default')
示例#2
0
文件: misc.py 项目: ansible/tower-cli
def send(source=None, prevent=None, exclude=None, secret_management='default', no_color=False):
    """Import assets into Tower.

    'tower send' imports one or more assets into a Tower instance

    The import can take either JSON or YAML.
    Data can be sent on stdin (i.e. from tower-cli receive pipe) and/or from files
    or directories passed as parameters.

    If a directory is specified only files that end in .json, .yaml or .yml will be
    imported. Other files will be ignored.
    """

    from tower_cli.cli.transfer.send import Sender
    sender = Sender(no_color)
    sender.send(source, prevent, exclude, secret_management)
示例#3
0
def send(source=None,
         prevent=None,
         secret_management='default',
         no_color=False):
    """Import assets into Tower.

    'tower send' imports one or more assets into a Tower instance

    The import can take either JSON or YAML.
    Data can be sent on stdin (i.e. from tower-cli receive pipe) and/or from files
    or directories passed as parameters.

    If a directory is specified only files that end in .json, .yaml or .yml will be
    imported. Other files will be ignored.
    """

    from tower_cli.cli.transfer.send import Sender
    sender = Sender(no_color)
    sender.send(source, prevent, secret_management)
示例#4
0
def main():
    argument_spec = dict(
        assets=dict(required=False),
        files=dict(required=False, default=[], type='list'),
        prevent=dict(required=False, default=[], type='list'),
        password_management=dict(required=False,
                                 default='default',
                                 choices=['default', 'random']),
    )

    module = TowerModule(argument_spec=argument_spec,
                         supports_check_mode=False)

    if not HAS_TOWER_CLI:
        module.fail_json(msg='ansible-tower-cli required for this module')

    if not TOWER_CLI_HAS_EXPORT:
        module.fail_json(
            msg='ansible-tower-cli version does not support export')

    assets = module.params.get('assets')
    prevent = module.params.get('prevent')
    password_management = module.params.get('password_management')
    files = module.params.get('files')

    result = dict(
        changed=False,
        msg='',
        output='',
    )

    if not assets and not files:
        result['msg'] = "Assets or files must be specified"
        module.fail_json(**result)

    path = None
    if assets:
        # We got assets so we need to dump this out to a temp file and append that to files
        handle, path = mkstemp(prefix='', suffix='', dir='')
        with open(path, 'w') as f:
            f.write(assets)
        files.append(path)

    tower_auth = tower_auth_config(module)
    failed = False
    with settings.runtime_values(**tower_auth):
        try:
            sender = Sender(no_color=False)
            old_stdout = sys.stdout
            sys.stdout = captured_stdout = StringIO()
            try:
                sender.send(files, prevent, password_management)
            except TypeError as e:
                # Newer versions of TowerCLI require 4 parameters
                sender.send(files, prevent, [], password_management)

            if sender.error_messages > 0:
                failed = True
                result[
                    'msg'] = "Transfer Failed with %d errors" % sender.error_messages
            if sender.changed_messages > 0:
                result['changed'] = True
        except TowerCLIError as e:
            result['msg'] = e.message
            failed = True
        finally:
            if path is not None:
                os.remove(path)
            result['output'] = captured_stdout.getvalue().split("\n")
            sys.stdout = old_stdout

    # Return stdout so that module returns will work
    if failed:
        module.fail_json(**result)
    else:
        module.exit_json(**result)