From 04ca5bdec649ef54bab06083acbc00347099fbd5 Mon Sep 17 00:00:00 2001 From: Luke Hinds Date: Tue, 16 Aug 2016 12:28:17 +0100 Subject: Cleaned up comments and added PEP 257 -- Docstring Conventions Change-Id: Id7e8e627e3e249948181c09deadf2d114b77dbf7 Signed-off-by: Luke Hinds --- connect.py | 14 +++++--------- scripts/internet_check.py | 1 + security_scan.py | 16 +++++++++++++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/connect.py b/connect.py index 18ca96d..546e44d 100644 --- a/connect.py +++ b/connect.py @@ -29,6 +29,7 @@ class SetUp: self.args = args def keystonepass(self): + """ Used to retrieve keystone password """ com = self.args[0] client = paramiko.SSHClient() privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa') @@ -51,6 +52,7 @@ class SetUp: client.close() def getockey(self): + """ Used to retrieve, SSH overcloud keys """ remotekey = self.args[0] localkey = self.args[1] privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa') @@ -82,6 +84,7 @@ class ConnectionManager: self.args = args def remotescript(self): + """ Function to execute remote scripts """ localpath = self.args[0] remotepath = self.args[1] com = self.args[2] @@ -90,7 +93,6 @@ class ConnectionManager: privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa') selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile) client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - # Connection to undercloud try: client.connect(INSTALLER_IP, port=22, username='stack', pkey=selectedkey) @@ -111,7 +113,6 @@ class ConnectionManager: (local_addr)) remote_client = paramiko.SSHClient() remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - # Tunnel to overcloud try: remote_client.connect('127.0.0.1', port=22, username=self.user, key_filename=self.localkey, sock=channel) @@ -130,24 +131,22 @@ class ConnectionManager: output = "" stdin, stdout, stderr = remote_client.exec_command(com) stdout = stdout.readlines() - # remove script sftp.remove(remotepath) remote_client.close() client.close() - # Pipe back stout for line in stdout: output = output + line if output != "": return output def remotecmd(self): + """ Used to execute remote commands """ com = self.args[0] client = paramiko.SSHClient() privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa') selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile) client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - # Connection to undercloud try: client.connect(INSTALLER_IP, port=22, username='stack', pkey=selectedkey) @@ -168,7 +167,6 @@ class ConnectionManager: (local_addr)) remote_client = paramiko.SSHClient() remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - # Tunnel to overcloud try: remote_client.connect('127.0.0.1', port=22, username=self.user, key_filename=self.localkey, sock=channel) @@ -192,6 +190,7 @@ class ConnectionManager: client.close() def download_reports(self): + """ Function to retrieve reports from remote nodes """ dl_folder = self.args[0] reportfile = self.args[1] reportname = self.args[2] @@ -200,7 +199,6 @@ class ConnectionManager: privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa') selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile) client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - # Connection to overcloud try: client.connect(INSTALLER_IP, port=22, username='stack', pkey=selectedkey) @@ -221,7 +219,6 @@ class ConnectionManager: (local_addr)) remote_client = paramiko.SSHClient() remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - # Tunnel to overcloud try: remote_client.connect('127.0.0.1', port=22, username=self.user, key_filename=self.localkey, sock=channel) @@ -234,7 +231,6 @@ class ConnectionManager: except socket.error: logger.error("Socker Connection failed for " "undercloud host: {0}".format(self.host)) - # Download the reports sftp = remote_client.open_sftp() logger.info("Downloading \"{0}\"...".format(reportname)) sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, reportname))) diff --git a/scripts/internet_check.py b/scripts/internet_check.py index 1bed50a..7fd3b1b 100644 --- a/scripts/internet_check.py +++ b/scripts/internet_check.py @@ -16,6 +16,7 @@ TEST_HOST = "google.com" def is_connected(): + """ Perform socket connect to port 80 to verify connectivity """ try: host = socket.gethostbyname(TEST_HOST) socket.create_connection((host, 80), 2) diff --git a/security_scan.py b/security_scan.py index 36b7954..8511b44 100644 --- a/security_scan.py +++ b/security_scan.py @@ -49,7 +49,7 @@ args = parser.parse_args() cfgparse = SafeConfigParser() cfgparse.read(args.cfgfile) -# Grab Undercloud key +# Obtain Undercloud key remotekey = cfgparse.get('undercloud', 'remotekey') localkey = cfgparse.get('undercloud', 'localkey') setup = connect.SetUp(remotekey, localkey) @@ -69,6 +69,7 @@ nova = client.Client(2, session=sess) def run_tests(host, nodetype): + """ Main tool runtime function """ user = cfgparse.get(nodetype, 'user') port = cfgparse.get(nodetype, 'port') connect.logger.info("Host: {0} Selected Profile: {1}".format(host, @@ -97,7 +98,7 @@ def run_tests(host, nodetype): def nova_iterate(): - # Find compute nodes, active with network on ctlplane + """ Iterates over the Nova API to gather a list of node IP's""" for server in nova.servers.list(): if server.status == 'ACTIVE' and 'compute' in server.name: networks = server.networks @@ -113,6 +114,7 @@ def nova_iterate(): def internet_check(host, nodetype): + """ Performs connectivity test using scripts/internet_check.py """ import connect user = cfgparse.get(nodetype, 'user') port = cfgparse.get(nodetype, 'port') @@ -129,6 +131,9 @@ def internet_check(host, nodetype): def createfiles(host, port, user, localkey): + """ Creates required tempfiles needed for OpenSCAP to run. + Executes script file: /scripts/createfiles.py + """ import connect global tmpdir localpath = functest_dir + 'scripts/createfiles.py' @@ -140,6 +145,7 @@ def createfiles(host, port, user, localkey): def install_pkg(host, port, user, localkey): + """ Installs OpenSCAP binarie and main release scap content""" import connect com = 'sudo yum -y install openscap-scanner scap-security-guide' connect = connect.ConnectionManager(host, port, user, localkey, com) @@ -147,6 +153,7 @@ def install_pkg(host, port, user, localkey): def run_scanner(host, port, user, localkey, nodetype): + """ Peforms the actual OpenSCAP scan operation""" import connect scantype = cfgparse.get(nodetype, 'scantype') profile = cfgparse.get(nodetype, 'profile') @@ -179,8 +186,8 @@ def run_scanner(host, port, user, localkey, nodetype): def post_tasks(host, port, user, localkey, nodetype): + """ Create download folder for functest dashboard and download reports """ import connect - # Create the download folder for functest dashboard and download reports reports_dir = cfgparse.get(nodetype, 'reports_dir') dl_folder = os.path.join(reports_dir, host + "_" + datetime.datetime. @@ -195,6 +202,7 @@ def post_tasks(host, port, user, localkey, nodetype): def removepkg(host, port, user, localkey, nodetype): + """ Removes all packages (if Clean = True is used in ini config) """ import connect com = 'sudo yum -y remove openscap-scanner scap-security-guide' connect = connect.ConnectionManager(host, port, user, localkey, com) @@ -202,6 +210,7 @@ def removepkg(host, port, user, localkey, nodetype): def cleandir(host, port, user, localkey, nodetype): + """ Removes all scan files (if Clean = True is used in ini config) """ import connect com = 'sudo rm -r {0}'.format(tmpdir.rstrip()) connect = connect.ConnectionManager(host, port, user, localkey, com) @@ -209,4 +218,5 @@ def cleandir(host, port, user, localkey, nodetype): if __name__ == '__main__': + """ Main entry_point """ nova_iterate() -- cgit 1.2.3-korg