Exemplo n.º 1
0
    def main(self,argv):
        methodName = "main"
        self.rc = 0
        try:
            beginTime = Utilities.currentTimeMillis()
            cmdLineArgs = Utilities.getInputArgs(self.ArgsSignature,argv[1:])
            trace, logFile = self._configureTraceAndLogging(cmdLineArgs)
            self.region = cmdLineArgs.get('region')
            if (logFile):
                TR.appendTraceLog(logFile)   
            if (trace):
                TR.info(methodName,"Tracing with specification: '%s' to log file: '%s'" % (trace,logFile))

            logFilePath = os.path.join(self.logsHome,"icp4i_install.log")
    
            with open(logFilePath,"a+") as icp4iInstallLogFile:
                self.stackId = cmdLineArgs.get('stackid')
                self.stackName = cmdLineArgs.get('stack-name')
                self.amiID = environ.get('AMI_ID')
                self.cp4iSecret = environ.get('CP4I_SECRET')
                self.ocpSecret = environ.get('OCP_SECRET')
                self.pnSecret = environ.get('PN_SECRET')
                # self.cp4ibucketName = environ.get('ICP4IArchiveBucket')
                self.ICP4IInstallationCompletedURL = environ.get('ICP4IInstallationCompletedURL')
                TR.info(methodName, "amiID %s "% self.amiID)
                # TR.info(methodName, "cp4ibucketName %s "% self.cp4ibucketName)
                TR.info(methodName, "ICP4IInstallationCompletedURL %s "% self.ICP4IInstallationCompletedURL)
                TR.info(methodName, "cp4iSecret %s "% self.cp4iSecret)
                TR.info(methodName, "ocpSecret %s "% self.ocpSecret)
                TR.info(methodName, "pnSecret %s "% self.pnSecret)
                self.__init(self.stackId,self.stackName, icp4iInstallLogFile)
                self.zones = Utilities.splitString(self.AvailabilityZones)
                TR.info(methodName," AZ values %s" % self.zones)

                TR.info(methodName,"RedhatPullSecret %s" %self.RedhatPullSecret)
                secret = self.RedhatPullSecret.split('/',1)
                TR.info(methodName,"Pull secret  %s" %secret)  
                self.pullSecret = "/ibm/pull-secret"
                s3_cp_cmd = "aws s3 cp "+self.RedhatPullSecret+" "+self.pullSecret
                TR.info(methodName,"s3 cp cmd %s"%s3_cp_cmd)
                call(s3_cp_cmd, shell=True,stdout=icp4iInstallLogFile)
                self.getSecret(icp4iInstallLogFile)
                
                ocpstart = Utilities.currentTimeMillis()
                self.installOCP(icp4iInstallLogFile)
                ocpend = Utilities.currentTimeMillis()
                self.printTime(ocpstart, ocpend, "Installing OCP")

                storagestart = Utilities.currentTimeMillis()
                self.installDedicatedOCS = Utilities.toBoolean(self.DedicatedOCS)
                self.configureOCS(icp4iInstallLogFile)
                storageend = Utilities.currentTimeMillis()
                self.printTime(storagestart, storageend, "Installing storage")

                if(self.password=="NotProvided"):
                    install_cp4i = ("sudo ./cp4i-deployment/cp4i-install.sh  -n " +  self.Namespace + " -k " + self.apiKey +
                                " -1 " + self.APILM + " -2 " + self.AIDB + " -3 " + self.AIDE + " -4 " + self.OD + " -5 " + self.AR +
                                " -6 " + self.MQ + " -7 " + self.ES + " -8 " + self.GW + " -9 " + self.HST + " | tee -a cp4i-logs.txt")
                else:
                    install_cp4i = ("sudo ./cp4i-deployment/cp4i-install.sh  -n " +  self.Namespace + " -k " + self.apiKey +
                                    " -1 " + self.APILM + " -2 " + self.AIDB + " -3 " + self.AIDE + " -4 " + self.OD + " -5 " + self.AR +
                                    " -6 " + self.MQ + " -7 " + self.ES + " -8 " + self.GW + " -9 " + self.HST + " -p " + self.password + " | tee -a cp4i-logs.txt")
                
                try:
                    process = Popen(install_cp4i,shell=True,stdout=icp4iInstallLogFile,stderr=icp4iInstallLogFile,close_fds=True)
                    stdoutdata,stderrdata=process.communicate()
                except CalledProcessError as e:
                    TR.error(methodName, "ERROR return code: %s, Exception: %s" % (e.returncode, e), e)
                    raise e    
                TR.info(methodName,"Installation of CP4I %s %s" %(stdoutdata,stderrdata))
                time.sleep(30)

                self.exportResults(self.stackName+"-OpenshiftURL", "https://"+self.openshiftURL, icp4iInstallLogFile)

                get_cp4i_route_cmd = "oc get route -n " + self.Namespace + " | grep 'navigator-pn' | awk '{print $2}'"
                TR.info(methodName, "Get CP4I URL")
                try:
                    self.cp4iURL = check_output(['bash','-c', get_cp4i_route_cmd])
                    TR.info(methodName, "CP4I URL retrieved %s"%self.cp4iURL)
                except CalledProcessError as e:
                    TR.error(methodName,"command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

                self.exportResults(self.stackName+"-CP4IURL", "https://"+self.cp4iURL, icp4iInstallLogFile)

                if(self.password=="NotProvided"):
                    get_cp4i_password_cmd = "oc get secrets -n ibm-common-services platform-auth-idp-credentials -ojsonpath='{.data.admin_password}' | base64 --decode && echo """
                    TR.info(methodName, "Get CP4I Password")
                    try:
                        self.cp4iPassword = check_output(['bash','-c', get_cp4i_password_cmd])
                        TR.info(methodName, "CP4I Password retrieved %s"%self.cp4iPassword)
                    except CalledProcessError as e:
                        TR.error(methodName,"command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
                else:
                    self.cp4iPassword = self.password

                self.updateSecret(icp4iInstallLogFile)
            #endWith    
            
        except Exception as e:
            TR.error(methodName,"Exception with message %s" %e)
            self.rc = 1
        finally:
            try:
            # Copy icpHome/logs to the S3 bucket for logs.
                self.logExporter.exportLogs("/var/log/")
                self.logExporter.exportLogs("/ibm/cp4i-linux-workspace/Logs")
                self.logExporter.exportLogs("%s" % self.logsHome)
            except Exception as  e:
                TR.error(methodName,"ERROR: %s" % e, e)
                self.rc = 1
            #endTry          
        endTime = Utilities.currentTimeMillis()
        elapsedTime = (endTime - beginTime)/1000
        etm, ets = divmod(elapsedTime,60)
        eth, etm = divmod(etm,60) 

        if (self.rc == 0):
            success = 'true'
            status = 'SUCCESS'
            TR.info(methodName,"SUCCESS END CP4I Install AWS ICP4I Quickstart.  Elapsed time (hh:mm:ss): %d:%02d:%02d" % (eth,etm,ets))
            # TODO update this later
            self.updateStatus(status)
        else:
            success = 'false'
            status = 'FAILURE: Check logs in S3 log bucket or on the Boot node EC2 instance in /ibm/logs/icp4i_install.log and /ibm/logs/post_install.log'
            TR.info(methodName,"FAILED END CP4I Install AWS ICP4I Quickstart.  Elapsed time (hh:mm:ss): %d:%02d:%02d" % (eth,etm,ets))
            # # TODO update this later
            self.updateStatus(status)
           
        #endIf 
        try:
            data = "%s: IBM Cloud Pak installation elapsed time: %d:%02d:%02d" % (status,eth,etm,ets)    
            check_call(['cfn-signal', 
                            '--success', success, 
                            '--id', self.stackId, 
                            '--reason', status, 
                            '--data', data, 
                            self.ICP4IInstallationCompletedURL
                            ])     
        except CalledProcessError as e:
            TR.error(methodName, "ERROR return code: %s, Exception: %s" % (e.returncode, e), e)
            raise e                                                
Exemplo n.º 2
0
    def main(self, argv):
        methodName = "main"
        self.rc = 0
        params = {}
        try:
            beginTime = Utilities.currentTimeMillis()
            cmdLineArgs = Utilities.getInputArgs(self.ArgsSignature, argv[1:])
            trace, logFile = self._configureTraceAndLogging(cmdLineArgs)
            self.region = cmdLineArgs.get('region')
            if (logFile):
                TR.appendTraceLog(logFile)
            if (trace):
                TR.info(
                    methodName,
                    "Tracing with specification: '%s' to log file: '%s'" %
                    (trace, logFile))

            logFilePath = os.path.join(self.logsHome, "icpd_install.log")

            with open(logFilePath, "a+") as icpdInstallLogFile:
                self.stackId = cmdLineArgs.get('stackid')
                self.stackName = cmdLineArgs.get('stack-name')
                self.cfnResource = boto3.resource('cloudformation',
                                                  region_name=self.region)
                self.cf = boto3.client('cloudformation',
                                       region_name=self.region)
                self.ec2 = boto3.resource('ec2', region_name=self.region)
                self.s3 = boto3.client('s3', region_name=self.region)
                self.elb = boto3.client('elb', region_name=self.region)
                self.secretsmanager = boto3.client('secretsmanager',
                                                   region_name=self.region)
                self.autoScaling = boto3.client('autoscaling',
                                                region_name=self.region)
                self.stackParameters = self.getStackParameters(self.stackId)
                self.stackParameterNames = self.stackParameters.keys()

                list = self.stackParameters.get(
                    "AnsibleAdditionalEnvironmentVariables").split(",")
                params.update((dict(item.split("=", 1) for item in list)))
                self.namespace = params.get('Namespace')
                self.cpdbucketName = params.get('ICPDArchiveBucket')
                self.ICPDInstallationCompletedURL = params.get(
                    'ICPDInstallationCompletedURL')
                self.installWKC = Utilities.toBoolean(params.get('WKC'))
                self.installWSL = Utilities.toBoolean(params.get('WSL'))
                self.installDV = Utilities.toBoolean(params.get('DV'))
                self.installWML = Utilities.toBoolean(params.get('WML'))
                self.installOSWML = Utilities.toBoolean(params.get('OSWML'))
                if (self.installOSWML):
                    self.installWML = True
                #endIf
                self.apikey = params.get('APIKey')
                TR.info(
                    methodName,
                    "Retrieve namespace value from Env Variables %s" %
                    self.namespace)
                self.logExporter = LogExporter(region=self.region,
                                               bucket=self.getOutputBucket(),
                                               keyPrefix='logs/%s' %
                                               self.stackName,
                                               fqdn=socket.getfqdn())
                self.configureEFS()
                self.getS3Object(bucket=self.cpdbucketName,
                                 s3Path="2.5/cpd-linux",
                                 destPath="/ibm/cpd-linux")
                os.chmod("/ibm/cpd-linux", stat.S_IEXEC)
                if not self.apikey:
                    TR.info(methodName, "Downloading repo.yaml from S3 bucket")
                    os.remove("/ibm/repo.yaml")
                    self.getS3Object(bucket=self.cpdbucketName,
                                     s3Path="2.5/repo.yaml",
                                     destPath="/ibm/repo.yaml")
                else:
                    TR.info(methodName,
                            "updating repo.yaml with apikey value provided")
                    self.updateTemplateFile('/ibm/repo.yaml', '<APIKEY>',
                                            self.apikey)
                self.installCPD(icpdInstallLogFile)
                self.validateInstall(icpdInstallLogFile)
                self.manageUser(icpdInstallLogFile)
                if not self.apikey:
                    self.activateLicense(icpdInstallLogFile)
            #endWith

        except Exception as e:
            TR.error(methodName, "Exception with message %s" % e)
            self.rc = 1
        finally:
            try:
                # Copy icpHome/logs to the S3 bucket for logs.
                self.logExporter.exportLogs("/var/log/")
                self.logExporter.exportLogs("/ibm/cpd-linux-workspace/Logs")
                self.logExporter.exportLogs("%s" % self.logsHome)
            except Exception as e:
                TR.error(methodName, "ERROR: %s" % e, e)
                self.rc = 1
            #endTry
        endTime = Utilities.currentTimeMillis()
        elapsedTime = (endTime - beginTime) / 1000
        etm, ets = divmod(elapsedTime, 60)
        eth, etm = divmod(etm, 60)

        if (self.rc == 0):
            success = 'true'
            status = 'SUCCESS'
            TR.info(
                methodName,
                "SUCCESS END CPD Install AWS ICPD Quickstart.  Elapsed time (hh:mm:ss): %d:%02d:%02d"
                % (eth, etm, ets))
            self.updateStatus(status)
            os.remove("/ibm/trial.lic")
            #os.remove("/ibm/repo.yaml")
        else:
            success = 'false'
            status = 'FAILURE: Check logs in S3 log bucket or on the AnsibleConfigServer node EC2 instance in /ibm/logs/icpd_install.log and /ibm/logs/post_install.log'
            TR.info(
                methodName,
                "FAILED END CPD Install AWS ICPD Quickstart.  Elapsed time (hh:mm:ss): %d:%02d:%02d"
                % (eth, etm, ets))
            self.updateStatus(status)
            os.remove("/ibm/trial.lic")
            #os.remove("/ibm/repo.yaml")
            #endIf
        try:
            data = "%s: IBM Cloud Pak installation elapsed time: %d:%02d:%02d" % (
                status, eth, etm, ets)
            check_call([
                'cfn-signal', '--success', success, '--id', self.stackId,
                '--reason', status, '--data', data,
                self.ICPDInstallationCompletedURL
            ])
        except CalledProcessError as e:
            TR.error(
                methodName,
                "ERROR return code: %s, Exception: %s" % (e.returncode, e), e)
            raise e