Skip to content

Commit

Permalink
Added bastion-mode config initialization logic to the init subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Tejeda, Engelbert committed Oct 1, 2019
1 parent 2310870 commit 39b332e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
37 changes: 29 additions & 8 deletions ansible_taskrunner/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def cli(**kwargs):
@click.version_option(version=__version__)
@click.option('--show-samples', '-m', is_flag=True,
help='Only show a sample task manifest, don\'t write it')
@extend_cli.bastion_mode
def init(**kwargs):
logger.info('Initializing ...')
if kwargs['show_samples']:
Expand Down Expand Up @@ -268,14 +269,34 @@ def init(**kwargs):
else:
logger.info(
'File exists, not writing manifest %s' % tasks_file)
if not os.path.exists(sftp_config_file):
logger.info(
'Existing manifest not found, writing %s' % sftp_config_file)
with open(sftp_config_file, 'w') as f:
f.write(SAMPLE_SFTP_CONFIG)
else:
logger.info(
'File exists, not writing manifest %s' % sftp_config_file)
if is_windows:
bastion_remote_path = kwargs.get('bastion_remote_path')
bastion_host = kwargs['bastion_host']
bastion_user = kwargs.get('bastion_user')
bastion_ssh_key_file = kwargs.get('bastion_ssh_key_file')
if not bastion_user:
import getpass
bastion_user = getpass.getuser()
if not bastion_remote_path:
cur_dir = os.path.basename(os.getcwd())
bastion_remote_path = '/home/{}/{}'.format(bastion_user, cur_dir)
if not bastion_ssh_key_file:
home_dir = os.path.expanduser('~')
bastion_ssh_key_file = os.path.join(home_dir, '.ssh', 'id_rsa')
settings_vars = {
'bastion_remote_path': bastion_remote_path,
'bastion_host': bastion_host,
'bastion_user': bastion_user,
'bastion_ssh_key_file': bastion_ssh_key_file.replace('\\', '\\\\')
}
if not os.path.exists(sftp_config_file):
logger.info(
'Existing sftp config not found, writing %s' % sftp_config_file)
with open(sftp_config_file, 'w') as f:
f.write(Template(SAMPLE_SFTP_CONFIG).safe_substitute(**settings_vars))
else:
logger.info(
'File exists, not writing sftp config %s' % sftp_config_file)

# Run command
# Parse help documentation
Expand Down
15 changes: 14 additions & 1 deletion ansible_taskrunner/lib/click_extras/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,20 @@ def process_options(self, parameters, func, is_required=False):
numargs = 1
return func

def bastion_mode(self, func):
if sys.platform in ['win32', 'cygwin']:
option = click.option('--bastion-host', '-h',
help='Specify host for bastion mode',
required=True)
func = option(func)
option = click.option('--bastion-user', '-u',
help='Specify username for initializing bastion mode settings file')
func = option(func)
option = click.option('--bastion-remote-path', '-r',
help='Specify remote workspace for bastion mode')
func = option(func)
return func

def options(self, func):
"""
Read dictionary of parameters, append these
Expand Down Expand Up @@ -244,7 +258,6 @@ def options(self, func):
if param[0] != 'or' and param[1] is None:
logger.warning("Removing invalid option key '%s'" % param[0])
optional_parameters.pop(param[0])
# Filter out any None values
extended_cli_func = self.process_options(
optional_parameters, extended_cli_func_required)
return extended_cli_func
8 changes: 4 additions & 4 deletions ansible_taskrunner/lib/help/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"confirm_downloads": false,
"confirm_sync": true,
"confirm_overwrite_newer": false,
"host": "CHANGEME",
"user": "CHANGEME",
"ssh_key_file": "CHANGEME",
"host": "$bastion_host",
"user": "$bastion_user",
"ssh_key_file": "$bastion_ssh_key_file",
"port": "22",
"remote_path": "CHANGEME",
"remote_path": "$bastion_remote_path",
"ignore_regexes": [
"\\\\.sublime-(project|workspace)", "sftp-config(-alt\\\\d?)?\\\\.json",
"sftp-settings\\\\.json", "/venv/", "\\\\.svn/", "\\\\.hg/", "\\\\.git/",
Expand Down

0 comments on commit 39b332e

Please sign in to comment.