Exemple #1
0
def ssh_handler(args, config):
    if config.dig('inventory', 'update_at_start') or args['-u']:
        update_handler(args, config)

    if args ['--tmux'] or config.dig('ssh', 'tmux'):
        question = "What instances would you like to ssh into?"
        targets = prompt_targets(question, targets=args['<host>'], config=config)
    else:
        question = "What instance would you like to ssh into?"
        targets = prompt_targets(question, targets=args['<host>'], config=config, multiple=False)

    if len(targets) == 0:
        logger.info("No matching instances found")
        sys.exit(1)

    commands = collections.OrderedDict()
    for idx, instance in enumerate(targets):
        name = '{}-{}'.format(instance.name, idx)
        commands[name] = Ssh(config, instance).command

    layout = None
    if args['--layout']:
        layout = args['--layout']

    if args['--tmux'] or config.dig('ssh', 'tmux'):
        tmux.run(config, commands, args['-w'], layout, args['-d'], args['-s'])
    else:
        cmd = list(commands.values())[0]
        if args['-d']:
            logger.debug(cmd)
        else:
            os.system(cmd)
Exemple #2
0
def test_ssh_command_bastion_missing_address():
    config = Config({
        'bastion': {}
    })
    with pytest.raises(MissingBastionHost):
        sshObj = Ssh(config, instance)
        sshObj.command
Exemple #3
0
def test_ssh_command_no_user():
    config = Config({
        'ssh': {
            'options': '-C -o ServerAliveInterval=255'
        }
    })
    sshObj = Ssh(config, instance)
    assert_command_results(sshObj.command, 'ssh -C -o ServerAliveInterval=255 address.com')
Exemple #4
0
def test_ssh_command_user():
    config = Config({
        'ssh': {
            'user': '******'
        }
    })
    sshObj = Ssh(config, instance)
    assert_command_results(sshObj.command, 'ssh [email protected]')
Exemple #5
0
def test_ssh_command_bastion_user():
    config = Config({
        'bastion': {
            'address': 'bastion.com',
            'user': '******'
        }
    })
    sshObj = Ssh(config, instance)
    assert_command_results(sshObj.command, "ssh -o ProxyCommand='ssh -W %h:%p [email protected]' address.com")
Exemple #6
0
def test_ssh_command_bastion_options():
    config = Config({
        'bastion': {
            'address': 'bastion.com',
            'options': '-C -o ServerAliveInterval=255'
        }
    })
    sshObj = Ssh(config, instance)
    assert_command_results(sshObj.command, "ssh -o ProxyCommand='ssh -C -o ServerAliveInterval=255 -W %h:%p bastion.com' address.com")
Exemple #7
0
def test_ssh_command_options():
    config = Config({
        'ssh': {
            'user': '******',
            'options': '-C -o ServerAliveInterval=255'
        }
    })
    sshObj = Ssh(config, instance)
    assert_command_results(
        sshObj.command,
        'ssh -C -o ServerAliveInterval=255 -t [email protected]')
Exemple #8
0
def exec_handler(args, config):
    if config.dig('inventory', 'update_at_start') or args['-u']:
        update_handler(args, config)

    if args['--tmux'] or config.dig('ssh', 'tmux'):
        question = "What containers would you like to exec into?"
        targets = prompt_targets(question,
                                 targets=args['<container>'],
                                 config=config,
                                 type=InstanceType.ECS,
                                 filter_sources=args['--source'])
    else:
        question = "What containers would you like to exec into?"
        targets = prompt_targets(question,
                                 targets=args['<container>'],
                                 config=config,
                                 type=InstanceType.ECS,
                                 filter_sources=args['--source'],
                                 multiple=False)

    if len(targets) == 0:
        logger.info("No matching instances found")
        sys.exit(1)

    for instance in targets:
        if instance.container_id == None:
            logger.info("Could not find container id for instance: %s" %
                        instance)
            sys.exit(1)

    commands = collections.OrderedDict()
    for idx, instance in enumerate(targets):
        name = '{}-{}'.format(instance.name, idx)
        commands[name] = Ssh(config,
                             instance,
                             command="sudo -i docker exec -ti %s bash" %
                             instance.container_id).command

    layout = None
    if args['--layout']:
        layout = args['--layout']

    if args['--tmux'] or config.dig('ssh', 'tmux'):
        tmux.run(config, commands, args['-w'], layout, args['-d'], args['-s'])
    else:
        cmd = list(commands.values())[0]
        if args['-d']:
            logger.debug(cmd)
        else:
            os.system(cmd)
Exemple #9
0
    def _build_host_file_contents(self):
        inventory_str = ''
        for instance in self.instances:
            ssh_obj = Ssh(self.config, instance)
            instance_str = "{} ansible_host={}".format(instance.name, instance.address)

            options = ssh_obj.options
            if len(options) > 0:
                instance_str += " ansible_ssh_common_args=\"{}\"".format(options)

            user = self.config.dig('ssh', 'user')
            if user:
                instance_str += " ansible_user=\"{}\"".format(user)

            inventory_str += instance_str + '\n'
        return inventory_str
Exemple #10
0
def test_ssh_command_go_case_no_options():
    config = Config({})
    sshObj = Ssh(config, instance)
    assert_command_results(sshObj.command, 'ssh -t address.com')
Exemple #11
0
def test_ssh_command_go_case():
    config = Config({'ssh': {}})
    sshObj = Ssh(config, instance)
    assert_command_results(sshObj.command, 'ssh -t address.com')
Exemple #12
0
def test_ssh_command_null_config():
    with pytest.raises(BadConfigError):
        sshObj = Ssh(None, instance)
        sshObj.command
Exemple #13
0
def test_ssh_command_null_instance():
    config = Config({})
    with pytest.raises(BadInstanceError):
        sshObj = Ssh(config, None)
        sshObj.command