Merge pull request #27336 from rallytime/cloud-logging-five

[2015.5] Fixup salt-cloud logging
This commit is contained in:
C. R. Oldham 2015-09-24 09:02:52 -06:00
commit 3746085587
3 changed files with 122 additions and 41 deletions

View file

@ -1993,13 +1993,20 @@ def wait_for_instance(
},
call='action',
)
log.debug(password_data)
win_passwd = password_data.get('password', None)
if win_passwd is None:
log.debug(password_data)
# This wait is so high, because the password is unlikely to
# be generated for at least 4 minutes
time.sleep(60)
else:
logging_data = password_data
logging_data['password'] = 'XXX-REDACTED-XXX'
logging_data['passwordData'] = 'XXX-REDACTED-XXX'
log.debug(logging_data)
vm_['win_password'] = win_passwd
break

View file

@ -695,17 +695,29 @@ def wait_for_winexesvc(host, port, username, password, timeout=900):
start = time.time()
log.debug(
'Attempting winexe connection to host {0} on port {1}'.format(
host, port
host,
port
)
)
creds = "-U '{0}%{1}' //{2}".format(
username, password, host)
username,
password,
host
)
logging_creds = "-U '{0}%XXX-REDACTED-XXX' //{1}".format(
username,
host
)
trycount = 0
while True:
trycount += 1
try:
# Shell out to winexe to check %TEMP%
ret_code = win_cmd('winexe {0} "sc query winexesvc"'.format(creds))
ret_code = win_cmd(
'winexe {0} "sc query winexesvc"'.format(creds),
logging_command=logging_creds
)
if ret_code == 0:
log.debug('winexe connected...')
return True
@ -730,14 +742,24 @@ def validate_windows_cred(host, username='Administrator', password=None, retries
'''
Check if the windows credentials are valid
'''
cmd = "winexe -U '{0}%{1}' //{2} \"hostname\"".format(
username,
password,
host
)
logging_cmd = "winexe -U '{0}%XXX-REDACTED-XXX' //{1} \"hostname\"".format(
username,
host
)
for i in xrange(retries):
retcode = win_cmd("winexe -U '{0}%{1}' //{2} \"hostname\"".format(
username, password, host
))
if retcode == 0:
ret_code = win_cmd(
cmd,
logging_command=logging_cmd
)
if ret_code == 0:
break
time.sleep(retry_delay)
return retcode == 0
return ret_code == 0
def wait_for_passwd(host, port=22, ssh_timeout=15, username='root',
@ -857,7 +879,14 @@ def deploy_windows(host,
return False
creds = "-U '{0}%{1}' //{2}".format(
username, password, host)
username,
password,
host
)
logging_creds = "-U '{0}%XXX-REDACTED-XXX' //{1}".format(
username,
host
)
salt.utils.smb.mkdirs('salttemp', conn=smb_conn)
salt.utils.smb.mkdirs('salt/conf/pki/minion', conn=smb_conn)
@ -892,9 +921,19 @@ def deploy_windows(host,
# Shell out to winexe to execute win_installer
# We don't actually need to set the master and the minion here since
# the minion config file will be set next via impacket
win_cmd('winexe {0} "c:\\salttemp\\{1} /S /master={2} /minion-name={3}"'.format(
creds, installer, master, name
))
cmd = 'winexe {0} "c:\\salttemp\\{1} /S /master={2} /minion-name={3}"'.format(
creds,
installer,
master,
name
)
logging_cmd = 'winexe {0} "c:\\salttemp\\{1} /S /master={2} /minion-name={3}"'.format(
logging_creds,
installer,
master,
name
)
win_cmd(cmd, logging_command=logging_cmd)
# Copy over minion_conf
if minion_conf:
@ -933,13 +972,21 @@ def deploy_windows(host,
smb_conn.deleteFile('C$', 'salttemp/{0}'.format(installer))
smb_conn.deleteDirectory('C$', 'salttemp')
# Shell out to winexe to ensure salt-minion service started
win_cmd('winexe {0} "sc stop salt-minion"'.format(
stop_cmd = 'winexe {0} "sc stop salt-minion"'.format(
creds,
))
)
logging_stop_cmd = 'winexe {0} "sc stop salt-minion"'.format(
logging_creds
)
win_cmd(stop_cmd, logging_command=logging_stop_cmd)
time.sleep(5)
win_cmd('winexe {0} "sc start salt-minion"'.format(
creds,
))
start_cmd = 'winexe {0} "sc start salt-minion"'.format(creds)
logging_start_cmd = 'winexe {0} "sc start salt-minion"'.format(
logging_creds
)
win_cmd(start_cmd, logging_command=logging_start_cmd)
# Fire deploy action
fire_event(
@ -1049,7 +1096,6 @@ def deploy_script(host,
log.debug('Using {0} as the key_filename'.format(key_filename))
ssh_kwargs['key_filename'] = key_filename
elif password and kwargs.get('has_ssh_agent', False) is False:
log.debug('Using {0} as the password'.format(password))
ssh_kwargs['password'] = password
if root_cmd('test -e \'{0}\''.format(tmp_dir), tty, sudo,
@ -1479,10 +1525,7 @@ def _exec_ssh_cmd(cmd, error_msg=None, allow_failure=False, **kwargs):
if ('key_filename' in kwargs and kwargs['key_filename']
and SSH_PASSWORD_PROMP_SUDO_RE.search(stdout)
):
# do nothing, as command already has adjustments to
# echo out the sudo password as part of the ssh command
# keep waiting for proc output
continue
proc.sendline(kwargs['sudo_password'])
# elif authenticating via password and haven't exhausted our
# password_retires
elif (
@ -1703,6 +1746,7 @@ def win_cmd(command, **kwargs):
'''
Wrapper for commands to be run against Windows boxes
'''
logging_command = kwargs.get('logging_command', None)
try:
proc = NonBlockingPopen(
command,
@ -1710,19 +1754,31 @@ def win_cmd(command, **kwargs):
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
stream_stds=kwargs.get('display_ssh_output', True),
logging_command=logging_command,
)
log.debug(
'Executing command(PID {0}): {1!r}'.format(
proc.pid, command
if logging_command is None:
log.debug(
'Executing command(PID {0}): {1!r}'.format(
proc.pid,
command
)
)
)
else:
log.debug(
'Executing command(PID {0}): {1!r}'.format(
proc.pid,
logging_command
)
)
proc.poll_and_read_until_finish()
proc.communicate()
return proc.returncode
except Exception as err:
log.error(
'Failed to execute command {0!r}: {1}\n'.format(
command, err
logging_command, err
),
exc_info=True
)
@ -1734,15 +1790,17 @@ def root_cmd(command, tty, sudo, allow_failure=False, **kwargs):
'''
Wrapper for commands to be run as root
'''
logging_command = command
sudo_password = kwargs.get('sudo_password', None)
if sudo:
if 'sudo_password' in kwargs and kwargs['sudo_password'] is not None:
command = 'echo "{1}" | sudo -S {0}'.format(
command,
kwargs['sudo_password'],
)
else:
if sudo_password is None:
command = 'sudo {0}'.format(command)
log.debug('Using sudo to run command {0}'.format(command))
logging_command = command
else:
logging_command = 'sudo -S "XXX-REDACTED-XXX" {0}'.format(command)
command = 'sudo -S {0}'.format(command)
log.debug('Using sudo to run command {0}'.format(logging_command))
ssh_args = []
@ -1817,10 +1875,14 @@ def root_cmd(command, tty, sudo, allow_failure=False, **kwargs):
if 'port' in kwargs:
ssh_args.extend(['-p {0}'.format(kwargs['port'])])
cmd = 'ssh {0} {1[username]}@{1[hostname]} {2}'.format(
' '.join(ssh_args), kwargs, pipes.quote(command)
cmd = 'ssh {0} {1[username]}@{1[hostname]} '.format(
' '.join(ssh_args),
kwargs
)
log.debug('SSH command: {0!r}'.format(cmd))
logging_command = cmd + logging_command
cmd = cmd + pipes.quote(command)
log.debug('SSH command: {0!r}'.format(logging_command))
retcode = _exec_ssh_cmd(cmd, allow_failure=allow_failure, **kwargs)
return retcode

View file

@ -59,6 +59,7 @@ class NonBlockingPopen(subprocess.Popen):
'stderr_logger_name', self._stderr_logger_name_
)
logging_command = kwargs.pop('logging_command', None)
stderr = kwargs.get('stderr', None)
super(NonBlockingPopen, self).__init__(*args, **kwargs)
@ -87,9 +88,20 @@ class NonBlockingPopen(subprocess.Popen):
self._stderr_logger_name_.format(pid=self.pid)
)
log.info(
'Running command under pid {0}: {1!r}'.format(self.pid, *args)
)
if logging_command is None:
log.info(
'Running command under pid {0}: {1!r}'.format(
self.pid,
*args
)
)
else:
log.info(
'Running command under pid {0}: {1!r}'.format(
self.pid,
logging_command
)
)
def recv(self, maxsize=None):
return self._recv('stdout', maxsize)