diff --git a/charts/mysql/templates/deployment.yaml b/charts/mysql/templates/deployment.yaml index 94f94b0c52ae4c65462e4fb587e0430bea8ffec1..ce5063d6ea93c1ae42bb64fdd514d8d0f784dbfc 100644 --- a/charts/mysql/templates/deployment.yaml +++ b/charts/mysql/templates/deployment.yaml @@ -6,7 +6,7 @@ metadata: labels: app: {{ template "mysql.fullname" . }} chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" - release: "{{ .Release.Name }}" + app.kubernetes.io/instance: "{{ .Release.Name }}" heritage: "{{ .Release.Service }}" {{- with .Values.deploymentAnnotations }} annotations: @@ -19,12 +19,12 @@ spec: selector: matchLabels: app: {{ template "mysql.fullname" . }} - release: {{ .Release.Name }} + app.kubernetes.io/instance: {{ .Release.Name }} template: metadata: labels: app: {{ template "mysql.fullname" . }} - release: {{ .Release.Name }} + app.kubernetes.io/instance: {{ .Release.Name }} {{- with .Values.podLabels }} {{ toYaml . | indent 8 }} {{- end }} diff --git a/charts/mysql/values.yaml b/charts/mysql/values.yaml index 2ddc72d0951fb02bc098b2ea425797844d5be3f6..8c697efb7a19b63836a43466fc8a3aded8e8a568 100644 --- a/charts/mysql/values.yaml +++ b/charts/mysql/values.yaml @@ -1,8 +1,8 @@ # mysql image version ## ref: https://hub.docker.com/r/library/mysql/tags/ ## -image: "mysql" -imageTag: "5.7.30" +image: "image-registry.openshift-image-registry.svc:5000/oaicicd-tmp/mysql-internal" +imageTag: "TAG" strategy: type: Recreate diff --git a/charts/oai-amf/values.yaml b/charts/oai-amf/values.yaml index 48c2e72af6ad4e4cf3003fba7d75477526e2713b..6ab2b5e16f93de37e29734dfbb909b1e4f828725 100644 --- a/charts/oai-amf/values.yaml +++ b/charts/oai-amf/values.yaml @@ -4,12 +4,12 @@ replicaCount: 1 -namespace: "oaicicd-tmp" +namespace: "PROJECT" image: registry: local - repository: image-registry.openshift-image-registry.svc:5000/oaicicd-tmp/oai-amf - version: develop + repository: image-registry.openshift-image-registry.svc:5000/PROJECT/oai-amf + version: "TAG" # pullPolicy: IfNotPresent or Never or Always pullPolicy: Always diff --git a/charts/oai-nrf/values.yaml b/charts/oai-nrf/values.yaml index 547041a24b22c25b7525a43622fa7e0c650585a4..54e5b4d992299046f83294a5d23ae2f42917fbf8 100644 --- a/charts/oai-nrf/values.yaml +++ b/charts/oai-nrf/values.yaml @@ -4,12 +4,12 @@ replicaCount: 1 -namespace: "oaicicd-tmp" +namespace: "PROJECT" image: registry: local - repository: image-registry.openshift-image-registry.svc:5000/oaicicd-tmp/oai-nrf - version: develop + repository: image-registry.openshift-image-registry.svc:5000/PROJECT/oai-nrf + version: TAG # pullPolicy: IfNotPresent or Never or Always pullPolicy: Always diff --git a/charts/oai-smf/values.yaml b/charts/oai-smf/values.yaml index 847fca3e231258148f63b080ecfff14438f54bf0..44b68c6ba50f09adc40d5047a37b076708356636 100644 --- a/charts/oai-smf/values.yaml +++ b/charts/oai-smf/values.yaml @@ -4,12 +4,12 @@ replicaCount: 1 -namespace: "oaicicd-tmp" +namespace: "PROJECT" image: registry: local - repository: oai-smf - version: develop + repository: image-registry.openshift-image-registry.svc:5000/PROJECT/oai-smf + version: TAG #pullPolicy: IfNotPresent or Never or Always pullPolicy: Always diff --git a/charts/oai-spgwu-tiny/values.yaml b/charts/oai-spgwu-tiny/values.yaml index 2862302d73a6bda29479ed71a7864f09966aadb4..4431dd79f831b7b4399b0512c407707918e9d8f3 100644 --- a/charts/oai-spgwu-tiny/values.yaml +++ b/charts/oai-spgwu-tiny/values.yaml @@ -4,12 +4,12 @@ replicaCount: 1 -namespace: "oaicicd-tmp" +namespace: "PROJECT" image: registry: local - repository: image-registry.openshift-image-registry.svc:5000/oaicicd-tmp/oai-spgwu-tiny - version: develop + repository: image-registry.openshift-image-registry.svc:5000/PROJECT/oai-spgwu-tiny + version: TAG # pullPolicy: IfNotPresent or Never or Always pullPolicy: Always diff --git a/ci-scripts/Jenkinsfile-GitLab-Helm b/ci-scripts/Jenkinsfile-GitLab-Helm index 74ad3f2a9756e4c56bd6f6f3c8ccb2fdb911ca9c..a15b500ebfc236561c6738836c922bd0907a1ebe 100644 --- a/ci-scripts/Jenkinsfile-GitLab-Helm +++ b/ci-scripts/Jenkinsfile-GitLab-Helm @@ -35,6 +35,7 @@ def sendSocialMediaMessage(pipeChannel, pipeColor, pipeMessage) { def cn_ci_host = params.Host_CN_CI_Server // for lock +def cn_ci_resource = params.CN_CI_Resource def ds_tester_ci_resource = params.DsTester // Location of the DsTester workspace @@ -81,10 +82,12 @@ pipeline { JOB_TIMESTAMP = JOB_TIMESTAMP.trim() def allParametersPresent = true - if (params.eNB_IPAddress == null) { + if (params.remote_IPAddress == null) { allParametersPresent = false - if (params.eNB_Credentials == null) { + } + if (params.remote_Credentials == null) { allParametersPresent = false + } if (params.OC_Credentials == null) { allParametersPresent = false } @@ -100,7 +103,7 @@ pipeline { if (params.DS_Tester_Server_Flag != null) { dsT_host_flag = params.DS_Tester_Server_Flag if (dsT_host_flag) { - def allParametersPresent = true + allParametersPresent = true if (params.DS_Tester_Server_Name == null) { allParametersPresent = false } else { @@ -134,7 +137,7 @@ pipeline { } } withCredentials([ - [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.eNB_Credentials}", usernameVariable: 'eNB_Username', passwordVariable: 'eNB_Password'], + [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.remote_Credentials}", usernameVariable: 'remote_Username', passwordVariable: 'remote_Password'], [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.OC_Credentials}", usernameVariable: 'OC_Username', passwordVariable: 'OC_Password'] ]) { if (upstreamEvent) { @@ -156,9 +159,9 @@ pipeline { sh "zip -r -qq oai-cn5g-fed.zip .git" sh "mkdir -p archives DS-TEST-RESULTS" // Prepare the workspace in the remote server - copyTo2ndServer('oai-cn5g-fed.zip', true, ${eNB_Username}, ${params.eNB_IPAddress}) - myShCmd('git clean -x -d -f > /dev/null 2>&1', true, ${eNB_Username}, ${params.eNB_IPAddress}) - myShCmd('mkdir -p archives DS-TEST-RESULTS', true, ${eNB_Username}, ${params.eNB_IPAddress}) + copyTo2ndServer('oai-cn5g-fed.zip', true, ${remote_Username}, ${params.remote_IPAddress}) + myShCmd('git clean -x -d -f > /dev/null 2>&1', true, ${remote_Username}, ${params.remote_IPAddress}) + myShCmd('mkdir -p archives DS-TEST-RESULTS', true, ${remote_Username}, ${params.remote_IPAddress}) } if (scmEvent) { sh "git clean -x -d -f > /dev/null 2>&1" @@ -168,23 +171,23 @@ pipeline { sh "zip -r -qq oai-cn5g-fed.zip .git" sh "mkdir -p archives DS-TEST-RESULTS" // Prepare the workspace in remote server - copyTo2ndServer('oai-cn5g-fed.zip', true, ${eNB_Username}, ${params.eNB_IPAddress}) - myShCmd('git clean -x -d -f > /dev/null 2>&1', true, ${eNB_Username}, ${params.eNB_IPAddress}) + copyTo2ndServer('oai-cn5g-fed.zip', true, ${remote_Username}, ${params.remote_IPAddress}) + myShCmd('git clean -x -d -f > /dev/null 2>&1', true, ${remote_Username}, ${params.remote_IPAddress}) if ("MERGE".equals(env.gitlabActionType)) { myShCmd("./ci-scripts/doGitLabMerge.sh --src-branch ${env.gitlabSourceBranch} --src-commit ${env.gitlabMergeRequestLastCommit} --target-branch ${env.gitlabTargetBranch} --target-commit ${GIT_COMMIT}", new_host_flag, new_host_user, new_host) } - myShCmd('mkdir -p archives DS-TEST-RESULTS', true, ${eNB_Username}, ${params.eNB_IPAddress}) + myShCmd('mkdir -p archives DS-TEST-RESULTS', true, ${remote_Username}, ${params.remote_IPAddress}) } if ((!upstreamEvent) && (!scmEvent)) { sh "git clean -x -d -f > /dev/null 2>&1" sh "zip -r -qq oai-cn5g-fed.zip .git" sh "mkdir -p archives DS-TEST-RESULTS" // Prepare the workspace in the remote server - copyTo2ndServer('oai-cn5g-fed.zip', true, ${eNB_Username}, ${params.eNB_IPAddress}) - myShCmd('git clean -x -d -f > /dev/null 2>&1', true, ${eNB_Username}, ${params.eNB_IPAddress}) - myShCmd('mkdir -p archives DS-TEST-RESULTS', true, ${eNB_Username}, ${params.eNB_IPAddress}) + copyTo2ndServer('oai-cn5g-fed.zip', true, remote_Username, params.remote_IPAddress) + myShCmd('git clean -x -d -f > /dev/null 2>&1', true, remote_Username, params.remote_IPAddress) + myShCmd('mkdir -p archives DS-TEST-RESULTS', true, remote_Username, params.remote_IPAddress) } - imageTags = "oai-nrf:${nrfTag},oai-amf:${amfTag},oai-smf:${smfTag},oai-spgwu-tiny:${spgwuTag}" + imageTags = "mysql:5.7.30,oai-nrf:${nrfTag},oai-amf:${amfTag},oai-smf:${smfTag},oai-spgwu-tiny:${spgwuTag}" } } } @@ -194,13 +197,15 @@ pipeline { script { echo '\u2705 \u001B[32mDeploy CN5G in idle mode\u001B[0m' withCredentials([ - [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.eNB_Credentials}", usernameVariable: 'eNB_Username', passwordVariable: 'eNB_Password'], + [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.remote_Credentials}", usernameVariable: 'remote_Username', passwordVariable: 'remote_Password'], [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.OC_Credentials}", usernameVariable: 'OC_Username', passwordVariable: 'OC_Password'] ]) { - try { - sh "python3 helmDeploy.py --mode=Deploy --eNBIPAddress=${params.eNB_IPAddress} --eNBUserName=${eNB_Username} --eNBPassword=${eNB_Password} --OCUserName=${OC_Username} --OCPassword=${OC_Password} --OCProjectName=${OC_ProjectName} --imageTags=${imageTags}" - } catch (Exception e) { - currentBuild.result = 'FAILURE' + dir('ci-scripts') { + try { + sh "python3 helmDeploy.py --mode=Deploy --remoteIPAdd=${params.remote_IPAddress} --remoteUserName=${remote_Username} --remotePassword=${remote_Password} --OCUserName=${OC_Username} --OCPassword=${OC_Password} --OCProjectName=${OC_ProjectName} --imageTags=${imageTags}" + } catch (Exception e) { + currentBuild.result = 'FAILURE' + } } } } @@ -210,13 +215,15 @@ pipeline { steps { script { withCredentials([ - [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.eNB_Credentials}", usernameVariable: 'eNB_Username', passwordVariable: 'eNB_Password'], + [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.remote_Credentials}", usernameVariable: 'remote_Username', passwordVariable: 'remote_Password'], [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.OC_Credentials}", usernameVariable: 'OC_Username', passwordVariable: 'OC_Password'] ]) { - try { - sh "python3 helmDeploy.py --mode=UnDeploy --eNBIPAddress=${params.eNB_IPAddress} --eNBUserName=${eNB_Username} --eNBPassword=${eNB_Password} --OCUserName=${OC_Username} --OCPassword=${OC_Password} --OCProjectName=${OC_ProjectName} --imageTags=${imageTags}" - } catch (Exception e) { - currentBuild.result = 'FAILURE' + dir('ci-scripts') { + try { + sh "python3 helmDeploy.py --mode=UnDeploy --remoteIPAdd=${params.remote_IPAddress} --remoteUserName=${remote_Username} --remotePassword=${remote_Password} --OCUserName=${OC_Username} --OCPassword=${OC_Password} --OCProjectName=${OC_ProjectName} --imageTags=${imageTags}" + } catch (Exception e) { + currentBuild.result = 'FAILURE' + } } } } diff --git a/ci-scripts/helmDeploy.py b/ci-scripts/helmDeploy.py index 431cadca6446b36aa28fb37546cbcbc367a5151c..c8cf2a91ef7889596f1d3a482887ebcee2bdd9a0 100644 --- a/ci-scripts/helmDeploy.py +++ b/ci-scripts/helmDeploy.py @@ -38,16 +38,21 @@ import re import time import sys +logging.basicConfig( + level=logging.DEBUG, + format="[%(asctime)s] %(name)s:%(levelname)s: %(message)s" +) + class ClusterDeploy: def __init__(self): - self.eNBIPAddress = "" - self.eNBUserName = "" - self.eNBPassword = "" + self.remoteIPAdd = "" + self.remoteUserName = "" + self.remotePassword = "" self.OCUserName = "" self.OCPassword = "" self.OCProjectName = "" self.sourceCodePath = "/tmp/CI-CN5G-FED-RHEL8" - self.imageTags = "" + self.imageTags = "" self.mode = "" #-----------------$ @@ -55,14 +60,14 @@ class ClusterDeploy: #-----------------$ def Deploy_5gcn(self): - lIpAddr = self.eNBIPAddress - lUserName = self.eNBUserName - lPassWord = self.eNBPassword + lIpAddr = self.remoteIPAdd + lUserName = self.remoteUserName + lPassWord = self.remotePassword lSourcePath = self.sourceCodePath ocUserName = self.OCUserName ocPassword = self.OCPassword ocProjectName = self.OCProjectName - limageTags = self.imageTags + limageTags = self.imageTags if lIpAddr == '' or lUserName == '' or lPassWord == '' or lSourcePath == '' or ocUserName == '' or ocPassword == '' or ocProjectName == '' or limageTags == '': sys.exit('Insufficient Parameter') logging.debug('Running on server: ' + lIpAddr) @@ -70,44 +75,46 @@ class ClusterDeploy: mySSH.open(lIpAddr, lUserName, lPassWord) mySSH.command('cd ' + lSourcePath, '\$', 5) - images = limageTags.split(',') - for image in images: - eachImage = image.split(':') - imageName = eachImage(0) - imageTag = eachImage(1) - # Check if image is exist on the Red Hat server, before pushing it to OC cluster - mySSH.command("sudo podman image inspect --format='Size = {{.Size}} bytes' " + imageName + ":" + imageTag, '\$', 60) - if mySSH.getBefore().count('no such image') != 0: - logging.error(f'\u001B[1m No such image {imageName}]\u001B[0m') - mySSH.close() - sys.exit(-1) - else: - result = re.search('Size *= *(?P<size>[0-9\-]+) *bytes', mySSH.getBefore()) - if result is not None: - imageSize = float(result.group('size')) - imageSize = imageSize / 1000 - if imageSize < 1000: - logging.debug(f'\u001B[1m {imageName} size is ' + ('%.0f' % imageSize) + ' kbytes\u001B[0m') - else: - imageSize = imageSize / 1000 - if imageSize < 1000: - logging.debug(f'\u001B[1m {imageName} size is ' + ('%.0f' % imageSize) + ' Mbytes\u001B[0m') - else: - imageSize = imageSize / 1000 - logging.debug(f'\u001B[1m {imageName} is ' + ('%.3f' % imageSize) + ' Gbytes\u001B[0m') - else: - logging.debug(f'{imageName} size is unknown') + images = limageTags.split(',') + for image in images: + eachImage = image.split(':') + imageName = eachImage[0] + imageTag = eachImage[1] + if imageName == 'mysql': + continue + # Check if image is exist on the Red Hat server, before pushing it to OC cluster + mySSH.command2("sudo podman image inspect --format='Size = {{.Size}} bytes' " + imageName + ":" + imageTag + f' | tee -a {lSourcePath}/archives/{imageName}_image_info.log', 60, silent=True) + if mySSH.cmd2Results.count('no such image') != 0: + logging.error(f'\u001B[1m No such image {imageName}]\u001B[0m') + mySSH.close() + sys.exit(-1) + else: + result = re.search('Size *= *(?P<size>[0-9\-]+) *bytes', mySSH.cmd2Results) + if result is not None: + imageSize = float(result.group('size')) + imageSize = imageSize / 1000 + if imageSize < 1000: + logging.debug(f'\u001B[1m {imageName} size is ' + ('%.0f' % imageSize) + ' kbytes\u001B[0m') + else: + imageSize = imageSize / 1000 + if imageSize < 1000: + logging.debug(f'\u001B[1m {imageName} size is ' + ('%.0f' % imageSize) + ' Mbytes\u001B[0m') + else: + imageSize = imageSize / 1000 + logging.debug(f'\u001B[1m {imageName} is ' + ('%.3f' % imageSize) + ' Gbytes\u001B[0m') + else: + logging.debug(f'{imageName} size is unknown') # logging to OC Cluster and then switch to corresponding project - mySSH.command(f'oc login -u {ocUserName} -p {ocPassword}', '\$', 6) - if mySSH.getBefore().count('Login successful.') == 0: + mySSH.command2(f'oc login -u {ocUserName} -p {ocPassword}', 6, silent=True) + if mySSH.cmd2Results.count('Login successful.') == 0: logging.error('\u001B[1m OC Cluster Login Failed\u001B[0m') mySSH.close() sys.exit(-1) else: logging.debug('\u001B[1m Login to OC Cluster Successfully\u001B[0m') - mySSH.command(f'oc project {ocProjectName}', '\$', 6) - if mySSH.getBefore().count(f'Already on project "{ocProjectName}"') == 0 and mySSH.getBefore().count(f'Now using project "{self.OCProjectName}"') == 0: + mySSH.command2(f'oc project {ocProjectName}', 6, silent=True) + if mySSH.cmd2Results.count(f'Already on project "{ocProjectName}"') == 0 and mySSH.cmd2Results.count(f'Now using project "{self.OCProjectName}"') == 0: logging.error(f'\u001B[1m Unable to access OC project {ocProjectName}\u001B[0m') mySSH.close() sys.exit(-1) @@ -115,40 +122,46 @@ class ClusterDeploy: logging.debug(f'\u001B[1m Now using project {ocProjectName}\u001B[0m') # Tag the image and push to the OC cluster - mySSH.command('oc whoami -t | sudo podman login -u ' + ocUserName + ' --password-stdin https://default-route-openshift-image-registry.apps.5glab.nsa.eurecom.fr/ --tls-verify=false', '\$', 6) - if mySSH.getBefore().count('Login Succeeded!') == 0: + mySSH.command2('oc whoami -t | sudo podman login -u ' + ocUserName + ' --password-stdin https://default-route-openshift-image-registry.apps.5glab.nsa.eurecom.fr/ --tls-verify=false', 6, silent=True) + if mySSH.cmd2Results.count('Login Succeeded!') == 0: logging.error('\u001B[1m Podman Login to OC Cluster Registry Failed\u001B[0m') mySSH.close() sys.exit(-1) else: logging.debug('\u001B[1m Podman Login to OC Cluster Registry Successfully\u001B[0m') - for image in images: - imageName = image(0) - imageTag = image(1) - mySSH.command(f'oc create -f openshift/{imageName}-image-stream.yml', '\$', 6) - if mySSH.getBefore().count('(AlreadyExists):') == 0 and mySSH.getBefore().count('created') == 0: - logging.error(f'\u001B[1m Image Stream "{imageName}" Creation Failed on OC Cluster {ocProjectName}\u001B[0m') - mySSH.close() - sys.exit(-1) - else: - logging.debug(f'\u001B[1m Image Stream "{imageName}" created on OC project {ocProjectName}\u001B[0m') - mySSH.command(f'sudo podman tag {imageName}:{imageTag} default-route-openshift-image-registry.apps.5glab.nsa.eurecom.fr/{self.OCProjectName}/{imageName}:{imageTag}', '\$', 6) - mySSH.command(f'sudo podman push default-route-openshift-image-registry.apps.5glab.nsa.eurecom.fr/{self.OCProjectName}/{imageName}:{imageTag} --tls-verify=false', '\$', 60) - if mySSH.getBefore().count('Storing signatures') == 0: - logging.error(f'\u001B[1m Image "{imageName}" push to OC Cluster Registry Failed\u001B[0m') - mySSH.close() - sys.exit(-1) - else: - logging.debug(f'\u001B[1m Image "{imageName}" push to OC Cluster Registry Successfully\u001B[0m') + for image in images: + eachImage = image.split(':') + imageName = eachImage[0] + imageTag = eachImage[1] + if imageName == 'mysql': + continue + mySSH.command2(f'cd {lSourcePath} && oc create -f openshift/{imageName}-image-stream.yml 2>&1 | tee -a archives/5gcn_imagestream_summary.txt', 6, silent=True) + if mySSH.cmd2Results.count('(AlreadyExists):') == 0 and mySSH.cmd2Results.count('created') == 0: + logging.error(f'\u001B[1m Image Stream "{imageName}" Creation Failed on OC Cluster {ocProjectName}\u001B[0m') + mySSH.close() + sys.exit(-1) + else: + logging.debug(f'\u001B[1m Image Stream "{imageName}" created on OC project {ocProjectName}\u001B[0m') + mySSH.command2(f'sudo podman tag {imageName}:{imageTag} default-route-openshift-image-registry.apps.5glab.nsa.eurecom.fr/{self.OCProjectName}/{imageName}:{imageTag}', 6, silent=True) + mySSH.command2(f'sudo podman push default-route-openshift-image-registry.apps.5glab.nsa.eurecom.fr/{self.OCProjectName}/{imageName}:{imageTag} --tls-verify=false 2>&1 | tee -a archives/5gcn_imagepush_summary.txt', 60, silent=True) + time.sleep(10) + if mySSH.cmd2Results.count('Storing signatures') == 0: + logging.error(f'\u001B[1m Image "{imageName}" push to OC Cluster Registry Failed\u001B[0m') + mySSH.close() + sys.exit(-1) + else: + logging.debug(f'\u001B[1m Image "{imageName}" push to OC Cluster Registry Successfully\u001B[0m') - passPods = 0 # Using helm charts deployment + passPods = 0 time.sleep(5) - for image in images: - eachImage = image.split(':') - imageName = eachImage(0) - imageTag = eachImage(1) - mySSH.command(f'sed -i -e "s#TAG#{imageTag}#g" ./charts/oai-5gcn/charts/{imageName}/values.yaml', '\$', 6) + for image in images: + eachImage = image.split(':') + imageName = eachImage[0] + imageTag = eachImage[1] + nameSufix = '' + mySSH.command(f'sed -i -e "s#PROJECT#{ocProjectName}#g" ./charts/{imageName}/values.yaml', '\$', 6, silent=True) + mySSH.command(f'sed -i -e "s#TAG#{imageTag}#g" ./charts/{imageName}/values.yaml', '\$', 6, silent=True) if imageName == 'oai-nrf': nameSufix = 'nrf' elif imageName == 'oai-amf': @@ -157,31 +170,43 @@ class ClusterDeploy: nameSufix = 'smf' elif imageName == 'oai-spgwu-tiny': nameSufix = 'spgwu' - mySSH.command(f'helm install {imageName} ./charts/{imageName}/ | tee -a archives/5gcn_helm_summary.txt 2>&1', '\$', 6) - if mySSH.getBefore().count('STATUS: deployed') == 0: + mySSH.command2(f'cd {lSourcePath} && helm install {imageName} ./charts/{imageName}/ | tee -a archives/5gcn_helm_summary.txt 2>&1', 10, silent=True) + if mySSH.cmd2Results.count('STATUS: deployed') == 0: logging.error(f'\u001B[1m Deploying "{imageName}" Failed using helm chart on OC Cluster\u001B[0m') + self.UnDeploy_5gcn() + self.AnalyzeLogFile_5gcn() + sys.exit(-1) else: logging.debug(f'\u001B[1m Deployed "{imageName}" Successfully using helm chart\u001B[0m') - time.sleep(60) - mySSH.command(f'oc get pods -o wide -l app.kubernetes.io/name={imageName} | tee -a archives/5gcn_pods_summary.txt', '\$', 6, resync=True) - podName = re.findall(f'{imageName}[\S\d\w]+', mySSH.getBefore()) + time.sleep(20) + mySSH.command2(f'oc get pods -o wide -l app.kubernetes.io/instance={imageName} | tee -a {lSourcePath}/archives/5gcn_pods_summary.txt', 6, silent=True) + result = re.search(f'{imageName}[\S\d\w]+', mySSH.cmd2Results) + podName = result.group(0) isRunning = False count = 0 - while count < 2 and isRunning == False: - time.sleep(60) - mySSH.command(f'oc exec {podName} -c {nameSufix} -it -- ps aux', '\$', 6, resync=True) - if mySSH.getBefore().count(f'oai_{nameSufix}') != 0: + while count < 3 and isRunning == False: + time.sleep(10) + if imageName == 'mysql': + mySSH.command2(f'oc exec {podName} -i -t -- mysqladmin -u root --password=linux ping', 6, silent=True) + else: + mySSH.command2(f'oc exec {podName} -c {nameSufix} -it -- ps aux', 6, silent=True) + if mySSH.cmd2Results.count(f'oai_{nameSufix}') != 0 or mySSH.cmd2Results.count(f'mysqld is alive') != 0 : logging.debug(f'\u001B[1m POD "{imageName}" Service Running Sucessfully\u001B[0m') isRunning = True passPods += 1 count +=1 if isRunning == False: logging.error(f'\u001B[1m POD "{imageName}" Service Running FAILED \u001B[0m') + self.UnDeploy_5gcn() + self.AnalyzeLogFile_5gcn() + sys.exit(-1) - if passPods == 4: - logging.debug(f'\u001B[1m Deployment: OK \u001B[0m') + if passPods == 5: + logging.debug(f'\u001B[1m 5gcn Deployment: OK \u001B[0m') + mySSH.command2(f'echo "DEPLOYMENT: OK" > {lSourcePath}/archives/deployment_status.log', 5, silent=True) else: - logging.error(f'\u001B[1m Deployment: KO \u001B[0m') + logging.error(f'\u001B[1m 5gcn Deployment: KO \u001B[0m') + mySSH.command2(f'echo "DEPLOYMENT: KO" > {lSourcePath}/archives/deployment_status.log', 5, silent=True) self.UnDeploy_5gcn() self.AnalyzeLogFile_5gcn() sys.exit(-1) @@ -189,47 +214,80 @@ class ClusterDeploy: def UnDeploy_5gcn(self): mySSH = SSH.SSHConnection() - mySSH.open(lIpAddr, lUserName, lPassWord) - mySSH.command('cd ' + lSourcePath, '\$', 5) + mySSH.open(self.remoteIPAdd, self.remoteUserName, self.remotePassword) + mySSH.command('cd ' + self.sourceCodePath, '\$', 5) logging.debug('\u001B[1m UnDeploying the 5gcn\u001B[0m') # logging to OC Cluster and then switch to corresponding project - mySSH.command(f'oc login -u {ocUserName} -p {ocPassword}', '\$', 6) - if mySSH.getBefore().count('Login successful.') == 0: + mySSH.command2(f'oc login -u {self.OCUserName} -p {self.OCPassword}', 6, silent=True) + if mySSH.cmd2Results.count('Login successful.') == 0: logging.error('\u001B[1m OC Cluster Login Failed\u001B[0m') mySSH.close() sys.exit(-1) else: logging.debug('\u001B[1m Login to OC Cluster Successfully\u001B[0m') - mySSH.command(f'oc project {ocProjectName}', '\$', 6) - if mySSH.getBefore().count(f'Already on project "{ocProjectName}"') == 0 and mySSH.getBefore().count(f'Now using project "{self.OCProjectName}"') == 0: - logging.error(f'\u001B[1m Unable to access OC project {ocProjectName}\u001B[0m') + mySSH.command2(f'oc project {self.OCProjectName}', 6, silent=True) + if mySSH.cmd2Results.count(f'Already on project "{self.OCProjectName}"') == 0 and mySSH.cmd2Results.count(f'Now using project "{self.OCProjectName}"') == 0: + logging.error(f'\u001B[1m Unable to access OC project {self.OCProjectName}\u001B[0m') mySSH.close() sys.exit(-1) else: - logging.debug(f'\u001B[1m Now using project {ocProjectName}\u001B[0m') + logging.debug(f'\u001B[1m Now using project {self.OCProjectName}\u001B[0m') # UnDeploy the 5gcn pods images = self.imageTags.split(',') - for image in images: - eachImage = image.split(':') - imageName = eachImage(0) - imageTag = eachImage(1) - mySSH.command(f'helm uninstall {imageName} | tee -a archives/5gcn_helm_summary.txt 2>&1', '\$', 6) - if mySSH.getBefore().count(f'release "{imageName}" uninstalled') == 0 and mySSH.getBefore().count('release: not found') == 0: + for image in images: + eachImage = image.split(':') + imageName = eachImage[0] + imageTag = eachImage[1] + mySSH.command2(f'cd {self.sourceCodePath} && helm uninstall {imageName} | tee -a archives/5gcn_helm_summary.txt 2>&1', 6, silent=True) + if mySSH.cmd2Results.count(f'release "{imageName}" uninstalled') == 0 and mySSH.cmd2Results.count(f'Error: uninstall: Release not loaded: {imageName}: release: not found') == 0: logging.error(f'\u001B[1m UnDeploying "{imageName}" Failed using helm chart on OC Cluster\u001B[0m') else: logging.debug(f'\u001B[1m UnDeployed "{imageName}" Successfully on OC Cluster\u001B[0m') + time.sleep(2) # Delete images and imagestream - mySSH.command(f'sudo podman rmi default-route-openshift-image-registry.apps.5glab.nsa.eurecom.fr/{self.OCProjectName}/{imageName}:{imageTag}', '\$', 6) - mySSH.command(f'oc delete is {imageName}', '\$', 6) + if imageName == 'mysql': + continue + mySSH.command2(f'sudo podman rmi default-route-openshift-image-registry.apps.5glab.nsa.eurecom.fr/{self.OCProjectName}/{imageName}:{imageTag}', 6, silent=True) + mySSH.command2(f'oc delete is {imageName}', 6, silent=True) logging.debug(f'\u001B[1m Deleted the "{imageName}" Image and ImageStream\u001B[0m') - mySSH.command('oc logout', '\$', 6) + mySSH.command2('oc logout', 6, silent=True) mySSH.close() self.AnalyzeLogFile_5gcn() def AnalyzeLogFile_5gcn(self): - pass + lIpAddr = self.remoteIPAdd + lUserName = self.remoteUserName + lPassWord = self.remotePassword + lSourcePath = self.sourceCodePath + mySSH = SSH.SSHConnection() + mySSH.open(lIpAddr, lUserName, lPassWord) + mySSH.command('cd ' + lSourcePath, '\$', 5) + mySSH.copyin(lIpAddr, lUserName, lPassWord, lSourcePath + '/archives/*', '../archives/') + mySSH.command('rm -rf ./archives', '\$', 5) + mySSH.command('oc logout', '\$', 6) + mySSH.close() + +def Usage(): + print('----------------------------------------------------------------------------------------------------------------------') + print('helmDeploy.py') + print(' Deploy and UnDeploy the 5gcn components on the Cluster.') + print('----------------------------------------------------------------------------------------------------------------------') + print('Usage: python3 helmDeploy.py [options]') + print(' --help Show this help.') + print('---------------------------------------------------------------------------------------------- Mandatory Options -----') + print(' --mode=[Deploy/Deploy]') + print(' --remoteIPAdd=[IP Address of remote Server]') + print(' --remoteUserName=[UserName of remote Server]') + print(' --remotePassword=[Password of remote Server]') + print(' --OCUserName=[Cluster UserName]') + print(' --OCPassword=[Cluster Password]') + print(' --OCProjectName=[Cluster Project name]') + print(' --imageTags=[Image tags of all the 5gcn components]') + print('------------------------------------------------------------------------------------------------- Actions Syntax -----') + print('python3 helmDeploy.py --mode=Deploy [Mandatory Options]') + print('python3 helmDeploy.py --mode=UnDeploy [Mandatory Options]') @@ -242,34 +300,36 @@ class ClusterDeploy: CN = ClusterDeploy() argvs = sys.argv -argc = len(argvs) while len(argvs) > 1: - myArgv = argvs.pop(1) - if re.match('^\-\-mode=(.+)$', myArgv, re.IGNORECASE): - matchReg = re.match('^\-\-mode=(.+)$', myArgv, re.IGNORECASE) - CN.mode = matchReg.group(1) - elif re.match('^\-\-eNBIPAddress=(.+)$', myArgv, re.IGNORECASE): - matchReg = re.match('^\-\-eNBIPAddress=(.+)$', myArgv, re.IGNORECASE) - CN.eNBIPAddress = matchReg.group(1) - elif re.match('^\-\-eNBUserName=(.+)$', myArgv, re.IGNORECASE): - matchReg = re.match('^\-\-eNBUserName=(.+)$', myArgv, re.IGNORECASE) - CN.eNBUserName = matchReg.group(1) - elif re.match('^\-\-eNBPassword=(.+)$', myArgv, re.IGNORECASE): - matchReg = re.match('^\-\-eNBPassword=(.+)$', myArgv, re.IGNORECASE) - CN.eNBPassword = matchReg.group(1) - elif re.match('^\-\-OCUserName=(.+)$', myArgv, re.IGNORECASE): - matchReg = re.match('^\-\-OCUserName=(.+)$', myArgv, re.IGNORECASE) - CN.OCUserName = matchReg.group(1) - elif re.match('^\-\-OCPassword=(.+)$', myArgv, re.IGNORECASE): - matchReg = re.match('^\-\-OCPassword=(.+)$', myArgv, re.IGNORECASE) - CN.OCPassword = matchReg.group(1) - elif re.match('^\-\-OCProjectName=(.+)$', myArgv, re.IGNORECASE): - matchReg = re.match('^\-\-OCProjectName=(.+)$', myArgv, re.IGNORECASE) - CN.OCProjectName = matchReg.group(1) - elif re.match('^\-\-imageTags=(.+)$', myArgv, re.IGNORECASE): - matchReg = re.match('^\-\-imageTags=(.+)$', myArgv, re.IGNORECASE) - CN.imageTags = matchReg.group(1) + myArgv = argvs.pop(1) + if re.match('^\-\-help$', myArgv, re.IGNORECASE): + Usage() + sys.exit(0) + elif re.match('^\-\-mode=(.+)$', myArgv, re.IGNORECASE): + matchReg = re.match('^\-\-mode=(.+)$', myArgv, re.IGNORECASE) + CN.mode = matchReg.group(1) + elif re.match('^\-\-remoteIPAdd=(.+)$', myArgv, re.IGNORECASE): + matchReg = re.match('^\-\-remoteIPAdd=(.+)$', myArgv, re.IGNORECASE) + CN.remoteIPAdd = matchReg.group(1) + elif re.match('^\-\-remoteUserName=(.+)$', myArgv, re.IGNORECASE): + matchReg = re.match('^\-\-remoteUserName=(.+)$', myArgv, re.IGNORECASE) + CN.remoteUserName = matchReg.group(1) + elif re.match('^\-\-remotePassword=(.+)$', myArgv, re.IGNORECASE): + matchReg = re.match('^\-\-remotePassword=(.+)$', myArgv, re.IGNORECASE) + CN.remotePassword = matchReg.group(1) + elif re.match('^\-\-OCUserName=(.+)$', myArgv, re.IGNORECASE): + matchReg = re.match('^\-\-OCUserName=(.+)$', myArgv, re.IGNORECASE) + CN.OCUserName = matchReg.group(1) + elif re.match('^\-\-OCPassword=(.+)$', myArgv, re.IGNORECASE): + matchReg = re.match('^\-\-OCPassword=(.+)$', myArgv, re.IGNORECASE) + CN.OCPassword = matchReg.group(1) + elif re.match('^\-\-OCProjectName=(.+)$', myArgv, re.IGNORECASE): + matchReg = re.match('^\-\-OCProjectName=(.+)$', myArgv, re.IGNORECASE) + CN.OCProjectName = matchReg.group(1) + elif re.match('^\-\-imageTags=(.+)$', myArgv, re.IGNORECASE): + matchReg = re.match('^\-\-imageTags=(.+)$', myArgv, re.IGNORECASE) + CN.imageTags = matchReg.group(1) else: sys.exit('Invalid Parameter: ' + myArgv)