Cleaned up output from host check and host install. Set paramiko log level to WARNING.

This commit is contained in:
Borne Mace 2015-07-28 13:16:28 -07:00
parent 431f4e2d20
commit 78c85dcfc6
4 changed files with 82 additions and 81 deletions

View File

@ -21,3 +21,11 @@ If you make changes to the i18n strings (denoted by methods like
_("message")) make sure to re-generate the i18n files with the
>python setup.py extract_messages command and check in the files
generated in openstack-kollaclient.
===============
Troubleshooting
===============
If you get an error about missing python.h install the python-dev
package via apt-get or yum or whatever mechanism is appropriate
for your platform.

View File

@ -40,10 +40,10 @@ class HostAdd(Command):
hostname = parsed_args.hostname.rstrip()
netAddr = parsed_args.networkaddress.rstrip()
contents = load_etc_yaml('hosts.yml')
for host, hostdata in contents.items():
for host in contents:
if host == hostname:
# TODO(bmace) fix message
self.log.info(_("host already exists"))
self.log.info(_('host already exists (%s)') % hostname)
return
hostEntry = {hostname: {'Services': '', 'NetworkAddress':
netAddr, 'Zone': ''}}
@ -66,7 +66,7 @@ class HostRemove(Command):
hostname = parsed_args.hostname.rstrip()
contents = load_etc_yaml('hosts.yml')
foundHost = False
for host, hostdata in contents.items():
for host, _ in contents.items():
if host == hostname:
foundHost = True
if foundHost:
@ -133,10 +133,14 @@ class HostCheck(Command):
return parser
def take_action(self, parsed_args):
self.log.info(_("host check"))
sshKeysExist = ssh_check_keys()
if not sshKeysExist:
ssh_keygen()
try:
ssh_keygen()
except Exception as e:
self.log.error("Error generating ssh keys: %s" % str(e))
return False
hostname = parsed_args.hostname.rstrip()
netAddr = None
contents = load_etc_yaml('hosts.yml')
@ -146,16 +150,21 @@ class HostCheck(Command):
# TODO(bmace) fix message
hostFound = True
netAddr = hostdata['NetworkAddress']
self.log.info(netAddr)
break
if hostFound is False:
self.log.info("no host by name (" + hostname + ") found")
self.log.error("No host by name (%s) found" % hostname)
return False
sshCheck = ssh_check_host(netAddr)
if sshCheck is False:
self.log.error("ssh access to host failed")
return
try:
self.log.info('host check (%s) at address (%s)' %
(hostname, netAddr))
ssh_check_host(netAddr)
self.log.info('host check (%s) passed' % hostname)
return True
except Exception as e:
self.log.error("host check (%s) failed (%s)" % (hostname, str(e)))
return False
class HostInstall(Command):
@ -171,10 +180,14 @@ class HostInstall(Command):
return parser
def take_action(self, parsed_args):
self.log.info(_("host install"))
sshKeysExist = ssh_check_keys()
if not sshKeysExist:
ssh_keygen()
try:
ssh_keygen()
except Exception as e:
self.log.error("Error generating ssh keys: %s" % str(e))
return False
hostname = parsed_args.hostname.rstrip()
# TODO(bmace) get this password in another way?
password = parsed_args.password.rstrip()
@ -186,23 +199,28 @@ class HostInstall(Command):
# TODO(bmace) fix message
hostFound = True
netAddr = hostdata['NetworkAddress']
self.log.info(netAddr)
break
if hostFound is False:
self.log.info("no host by name (" + hostname + ") found")
self.log.info("no host by name (%s) found" % hostname)
return False
sshCheck = ssh_check_host(netAddr)
# Don't bother doing all the install stuff is the check looks ok
sshCheck = False
try:
ssh_check_host(netAddr)
self.log.info('host install skipped for (%s) as check passed' % hostname)
return True
except Exception as e:
pass
# If sshCheck fails we need to set up the user / remote ssh keys
# using root and the available password
if sshCheck is False:
self.log.info("configuring admin user and ssh keys")
self.log.info("calling ssh_install_host with: " + netAddr +
" : " + password)
installHost = ssh_install_host(netAddr, password)
if installHost is False:
# TODO(bmace) probably throw out of ssh_install_host and
# log the error here, or maybe log it in ssh_install_host?
self.log.info("host install failed for host: " + hostname)
else:
self.log.info("host install succeeded")
try:
self.log.info('host install (%s) at address (%s)' %
(hostname, netAddr))
ssh_install_host(netAddr, password)
self.log.info('host install (%s) passed' % hostname)
except Exception as e:
self.log.info('host install (%s) failed (%s)' % (hostname, str(e)))

View File

@ -33,12 +33,13 @@ def ssh_check_keys():
def ssh_connect(netAddr, username, password, useKeys):
log = logging.getLogger(__name__)
try:
logging.getLogger("paramiko").setLevel(logging.WARNING)
sshClient = paramiko.SSHClient()
privateKey = ssh_get_private_key()
sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
log.info("connecting to addr: " + netAddr +
" user: " + username + " password: " + password +
" useKeys: " + str(useKeys))
log.debug("connecting to addr: " + netAddr +
" user: " + username + " password: " + password +
" useKeys: " + str(useKeys))
if useKeys:
sshClient.connect(hostname=netAddr, username=username,
password=password, pkey=privateKey)
@ -48,51 +49,30 @@ def ssh_connect(netAddr, username, password, useKeys):
return sshClient
except Exception as e:
# TODO(bmace) better failure behavior here
log.error("connect exception: " + str(e))
log.error("connect exception: " + str(type(e)))
log.error("connect exception: " + str(e.args))
try:
sshClient.close()
except Exception:
pass
return None
raise e
def ssh_check_host(netAddr):
log = logging.getLogger(__name__)
log.info("ssh check connect " + netAddr)
try:
sshClient = ssh_connect(netAddr, get_admin_user(), '', True)
if sshClient is None:
log.info("check host failed as connect failed")
return False
try:
log.info("about to exec 'ls'")
sshClient.exec_command("ls")
return True
# TODO(bmace) do whatever other checks are needed
except paramiko.SSHException as sshException:
log.error("exec failed" + str(sshException))
log.error("exec failed" + type(sshException))
log.error("exec failed" + sshException.args)
sshClient.exec_command("ls")
# TODO(bmace) do whatever other checks are needed
except Exception as e:
# TODO(bmace) better failure behavior here
log.error(str(e))
log.error(str(type(e)))
log.error(str(e.args))
raise e
finally:
try:
sshClient.close()
except Exception:
pass
return False
def ssh_install_host(netAddr, password):
log = logging.getLogger(__name__)
log.info("ssh install host " + netAddr)
adminUser = get_admin_user()
publicKey = ssh_get_public_key()
@ -101,56 +81,54 @@ def ssh_install_host(netAddr, password):
# add user
sshClient = ssh_connect(netAddr, "root", password, False)
commandStr = "useradd -m " + adminUser
log.info(commandStr)
stdin, stdout, stderr = sshClient.exec_command(commandStr)
log.info(str(stdout.read()) + " : " + str(stderr.read()))
log.debug(commandStr)
_, stdout, stderr = sshClient.exec_command(commandStr)
log.debug(str(stdout.read()) + " : " + str(stderr.read()))
# create ssh dir
commandStr = ("su - " + adminUser +
" -c \"mkdir /home/" + adminUser +
"/.ssh\"")
log.info(commandStr)
stdin, stdout, stderr = sshClient.exec_command(commandStr)
log.info(str(stdout.read()) + " : " + str(stderr.read()))
log.debug(commandStr)
_, stdout, stderr = sshClient.exec_command(commandStr)
log.debug(str(stdout.read()) + " : " + str(stderr.read()))
# create authorized_keys file
commandStr = ("su - " + adminUser +
" -c \"touch /home/" + adminUser +
"/.ssh/authorized_keys\"")
log.info(commandStr)
stdin, stdout, stderr = sshClient.exec_command(commandStr)
log.info(str(stdout.read()) + " : " + str(stderr.read()))
log.debug(commandStr)
_, stdout, stderr = sshClient.exec_command(commandStr)
log.debug(str(stdout.read()) + " : " + str(stderr.read()))
# populate authorized keys file w/ public key
commandStr = ("su - " + adminUser +
" -c \"echo '" + publicKey +
"' > /home/" + adminUser +
"/.ssh/authorized_keys\"")
log.info(commandStr)
stdin, stdout, stderr = sshClient.exec_command(commandStr)
log.info(str(stdout.read()) + " : " + str(stderr.read()))
log.debug(commandStr)
_, stdout, stderr = sshClient.exec_command(commandStr)
log.debug(str(stdout.read()) + " : " + str(stderr.read()))
# set appropriate permissions for ssh dir
commandStr = ("su - " + adminUser +
" -c \"chmod 0700 /home/" + adminUser +
"/.ssh\"")
log.info(commandStr)
stdin, stdout, stderr = sshClient.exec_command(commandStr)
log.info(str(stdout.read()) + " : " + str(stderr.read()))
log.debug(commandStr)
_, stdout, stderr = sshClient.exec_command(commandStr)
log.debug(str(stdout.read()) + " : " + str(stderr.read()))
# set appropriate permissions for authorized_keys file
commandStr = ("su - " + adminUser +
" -c \"chmod 0740 /home/" + adminUser +
"/.ssh/authorized_keys\"")
log.info(commandStr)
stdin, stdout, stderr = sshClient.exec_command(commandStr)
log.info(str(stdout.read()) + " : " + str(stderr.read()))
log.debug(commandStr)
_, stdout, stderr = sshClient.exec_command(commandStr)
log.debug(str(stdout.read()) + " : " + str(stderr.read()))
# TODO(bmace) do whatever else needs to be done at install time
except Exception as e:
log.error(str(e))
log.error(str(type(e)))
log.error(str(e.args()))
raise e
finally:
try:
sshClient.close()
@ -183,10 +161,7 @@ def ssh_keygen():
publicKey.get_base64()))
log.info("generated public key at: " + publicKeyPath)
except Exception as e:
# TODO(bmace) better failure behavior here
log.error(str(e))
log.error(str(type(e)))
log.error(str(e.args))
raise e
def ssh_get_private_key():

View File

@ -14,8 +14,8 @@
import logging
from kollaclient.i18n import _
from kollaclient.util import load_etc_yaml
from kollaclient.util import save_etc_yaml
from kollaclient.utils import load_etc_yaml
from kollaclient.utils import save_etc_yaml
from cliff.command import Command