# coding=utf-8
import os
+import sys
import re
import http.client
import base64
req = conn.request("POST", apiCall, json_file, headers=headers)
res = conn.getresponse()
res.read()
- if res.status != 200:
+ if res.status != 200 and res.status != 204:
logging.error("Error here, response back wasnt 200: Response was : %d , %s" % (res.status, res.reason))
+ writeCertInstallStatus("NOTOK")
else:
logging.debug("Response :%s Reason :%s ",res.status, res.reason)
def extractZipFiles(zipFileList, count):
for zipFolder in zipFileList:
- with zipfile.ZipFile(Path + "/" + zipFolder.strip(),"r") as zip_ref:
- zip_ref.extractall(Path)
- folder = zipFolder.rsplit(".")[0]
- processFiles(folder, count)
+ try:
+ with zipfile.ZipFile(Path + "/" + zipFolder.strip(),"r") as zip_ref:
+ zip_ref.extractall(Path)
+ folder = zipFolder.rsplit(".")[0]
+ processFiles(folder, count)
+ except Exception as e:
+ logging.error("Error while extracting zip file(s). Exiting Certificate Installation.")
+ logging.info("Error details : %s" % e)
+ writeCertInstallStatus("NOTOK")
def processFiles(folder, count):
logging.info('Process folder: %d %s', count, folder)
clientCrt = readFile(folder, file.strip())
else:
logging.error("Could not find file %s" % file.strip())
+ writeCertInstallStatus("NOTOK")
shutil.rmtree(Path + "/" + folder)
post_content(clientKey, clientCrt, certList, count)
if timePassed > TIMEOUT:
logging.error("TIME OUT: Healthcheck not passed in %d seconds... Could cause problems for testing activities..." %TIMEOUT)
+ writeCertInstallStatus("NOTOK")
return connected
return "'{}'".format(password)
except Exception as e:
logging.error("Error occurred while fetching password : %s", e)
- exit()
+ writeCertInstallStatus("NOTOK")
def cleanup():
for file in os.listdir(Path):
return file
except Exception as e:
logging.error("Error occurred while converting jks to p12 format : %s", e)
+ writeCertInstallStatus("NOTOK")
def make_cert_chain(cert_chain, pattern):
logging.debug("No JKS files found in %s directory" % Path)
except subprocess.CalledProcessError as err:
print("CalledProcessError Execution of OpenSSL command failed: %s" % err)
+ writeCertInstallStatus("NOTOK")
except Exception as e:
logging.error("UnExpected Error while processing JKS files at {0}, Caused by: {1}".format(Path, e))
+ writeCertInstallStatus("NOTOK")
def replaceAdminPassword(username, password, newpassword):
if newpassword is None:
logging.debug("Password change was not possible. Problem code was: %d", httpStatus)
except:
logging.error("Cannot execute REST call to set password.")
+ writeCertInstallStatus("NOTOK")
def readCertProperties():
logging.debug("No certs.properties/zip files exist at: " + Path)
logging.info("Processing any available jks/p12 files under cert directory")
process_jks_files(count)
-
+ else:
+ logging.info('Connected status: %s', connected)
+ logging.info('Stopping SDNR due to inability to install certificates')
+ writeCertInstallStatus("NOTOK")
+
+def writeCertInstallStatus(installStatus):
+ if installStatus == "NOTOK":
+ with open(os.path.join(log_directory, 'INSTALLCERTSFAIL'), 'w') as fp:
+ pass
+ sys.exit(1)
+ elif installStatus == "OK":
+ with open(os.path.join(log_directory, 'INSTALLCERTSPASS'), 'w') as fp:
+ pass
+ sys.exit(0)
readCertProperties()
logging.info('Cert installation ending')
+writeCertInstallStatus("OK")
+
--- /dev/null
+#!/bin/sh
+
+OKFILE=${ODL_HOME}/data/log/INSTALLCERTSPASS
+NOTOKFILE=${ODL_HOME}/data/log/INSTALLCERTSFAIL
+INSTALLCOMPLETE=false
+elapsedTime=0
+
+printInstallCertsLog() {
+ printf "################ Contents of ${ODL_HOME}/data/log/installCerts.log ################ \n"
+ cat ${ODL_HOME}/data/log/installCerts.log
+}
+
+while [[ $INSTALLCOMPLETE != true ]]; do
+ printf "Certificate installation in progress. Elapsed time - $elapsedTime secs. Waiting for 10 secs before checking the status.. \n"
+ sleep 10
+ elapsedTime=$((elapsedTime + 10))
+ pid=$(pgrep -f installCerts.py)
+ if [[ $? != 0 ]]; then
+ INSTALLCOMPLETE=true
+ fi
+done
+
+printf "Certificate installation script completed execution \n"
+if [ -f $OKFILE ]; then
+ #do nothing
+ printf "Everything OK in Certificate Installation \n"
+elif [ -f $NOTOKFILE ]; then
+ # Terminate SDNR container
+ printf "Problems encountered in Certificate Installation \n"
+ printInstallCertsLog
+ printf "Stoppping SDNR container due to failure in installing Certificates \n"
+ pid=`pgrep java`
+ kill -SIGKILL $pid
+fi
+