Commit 7361c168 authored by Robert Schmidt's avatar Robert Schmidt

Use new SSH class to execute image build

parent de4a9e1d
...@@ -352,7 +352,7 @@ class Cluster: ...@@ -352,7 +352,7 @@ class Cluster:
self.cmd.run(f'for pod in $(oc get pods | tail -n +2 | awk \'{{print $1}}\'); do oc get pod $pod -o json &>> cmake_targets/log/build_pod_summary.log; done', '\$', 60) self.cmd.run(f'for pod in $(oc get pods | tail -n +2 | awk \'{{print $1}}\'); do oc get pod $pod -o json &>> cmake_targets/log/build_pod_summary.log; done', '\$', 60)
build_log_name = f'build_log_{self.testCase_id}' build_log_name = f'build_log_{self.testCase_id}'
cls_containerize.CopyLogsToExecutor(self.cmd, lSourcePath, build_log_name, lIpAddr, 'oaicicd', CONST.CI_NO_PASSWORD) cls_containerize.CopyLogsToExecutor(self.cmd, lSourcePath, build_log_name)
self.cmd.run('for pod in $(oc get pods | tail -n +2 | awk \'{print $1}\'); do oc delete pod ${pod}; done') self.cmd.run('for pod in $(oc get pods | tail -n +2 | awk \'{print $1}\'); do oc delete pod ${pod}; done')
......
...@@ -44,6 +44,7 @@ from zipfile import ZipFile ...@@ -44,6 +44,7 @@ from zipfile import ZipFile
#----------------------------------------------------------- #-----------------------------------------------------------
# OAI Testing modules # OAI Testing modules
#----------------------------------------------------------- #-----------------------------------------------------------
import cls_cmd
import sshconnection as SSH import sshconnection as SSH
import helpreadme as HELP import helpreadme as HELP
import constants as CONST import constants as CONST
...@@ -83,20 +84,20 @@ def CreateWorkspace(sshSession, sourcePath, ranRepository, ranCommitID, ranTarge ...@@ -83,20 +84,20 @@ def CreateWorkspace(sshSession, sourcePath, ranRepository, ranCommitID, ranTarge
logging.debug(f'Merging with the target branch: {ranTargetBranch}') logging.debug(f'Merging with the target branch: {ranTargetBranch}')
sshSession.command(f'git merge --ff origin/{ranTargetBranch} -m "Temporary merge for CI"', '\$', 30) sshSession.command(f'git merge --ff origin/{ranTargetBranch} -m "Temporary merge for CI"', '\$', 30)
def CopyLogsToExecutor(sshSession, sourcePath, log_name, scpIp, scpUser, scpPw): def CopyLogsToExecutor(cmd, sourcePath, log_name):
sshSession.command(f'cd {sourcePath}/cmake_targets', '\$', 5) cmd.cd(f'{sourcePath}/cmake_targets')
sshSession.command(f'rm -f {log_name}.zip', '\$', 5) cmd.run(f'rm -f {log_name}.zip')
sshSession.command(f'mkdir -p {log_name}', '\$', 5) cmd.run(f'mkdir -p {log_name}')
sshSession.command(f'mv log/* {log_name}', '\$', 5) cmd.run(f'mv log/* {log_name}')
sshSession.command(f'zip -r -qq {log_name}.zip {log_name}', '\$', 5) cmd.run(f'zip -r -qq {log_name}.zip {log_name}')
# copy zip to executor for analysis # copy zip to executor for analysis
if (os.path.isfile(f'./{log_name}.zip')): if (os.path.isfile(f'./{log_name}.zip')):
os.remove(f'./{log_name}.zip') os.remove(f'./{log_name}.zip')
if (os.path.isdir(f'./{log_name}')): if (os.path.isdir(f'./{log_name}')):
shutil.rmtree(f'./{log_name}') shutil.rmtree(f'./{log_name}')
sshSession.copyin(scpIp, scpUser, scpPw, f'{sourcePath}/cmake_targets/{log_name}.zip', '.') cmd.copyin(f'{sourcePath}/cmake_targets/{log_name}.zip', f'./{log_name}.zip')
sshSession.command(f'rm -f {log_name}.zip','\$', 5) cmd.run(f'rm -f {log_name}.zip')
ZipFile(f'{log_name}.zip').extractall('.') ZipFile(f'{log_name}.zip').extractall('.')
def AnalyzeBuildLogs(buildRoot, images, globalStatus): def AnalyzeBuildLogs(buildRoot, images, globalStatus):
...@@ -329,12 +330,11 @@ class Containerize(): ...@@ -329,12 +330,11 @@ class Containerize():
HELP.GenericHelp(CONST.Version) HELP.GenericHelp(CONST.Version)
sys.exit('Insufficient Parameter') sys.exit('Insufficient Parameter')
logging.debug('Building on server: ' + lIpAddr) logging.debug('Building on server: ' + lIpAddr)
mySSH = SSH.SSHConnection() cmd = cls_cmd.RemoteCmd(lIpAddr)
mySSH.open(lIpAddr, lUserName, lPassWord)
# Checking the hostname to get adapted on cli and dockerfileprefixes # Checking the hostname to get adapted on cli and dockerfileprefixes
mySSH.command('hostnamectl', '\$', 5) cmd.run('hostnamectl')
result = re.search('Ubuntu|Red Hat', mySSH.getBefore()) result = re.search('Ubuntu|Red Hat', cmd.getBefore())
self.host = result.group(0) self.host = result.group(0)
if self.host == 'Ubuntu': if self.host == 'Ubuntu':
self.cli = 'docker' self.cli = 'docker'
...@@ -369,18 +369,18 @@ class Containerize(): ...@@ -369,18 +369,18 @@ class Containerize():
# Workaround for some servers, we need to erase completely the workspace # Workaround for some servers, we need to erase completely the workspace
if self.forcedWorkspaceCleanup: if self.forcedWorkspaceCleanup:
mySSH.command('echo ' + lPassWord + ' | sudo -S rm -Rf ' + lSourcePath, '\$', 15) cmd.run(f'sudo -S rm -Rf {lSourcePath}')
self.testCase_id = HTML.testCase_id self.testCase_id = HTML.testCase_id
CreateWorkspace(mySSH, lSourcePath, self.ranRepository, self.ranCommitID, self.ranTargetBranch, self.ranAllowMerge) CreateWorkspace(cmd, lSourcePath, self.ranRepository, self.ranCommitID, self.ranTargetBranch, self.ranAllowMerge)
# if asterix, copy the entitlement and subscription manager configurations # if asterix, copy the entitlement and subscription manager configurations
if self.host == 'Red Hat': if self.host == 'Red Hat':
mySSH.command('mkdir -p ./etc-pki-entitlement ./rhsm-conf ./rhsm-ca', '\$', 5) cmd.run('mkdir -p ./etc-pki-entitlement ./rhsm-conf ./rhsm-ca')
mySSH.command('sudo cp /etc/rhsm/rhsm.conf ./rhsm-conf/', '\$', 5) cmd.run('cp /etc/rhsm/rhsm.conf ./rhsm-conf/')
mySSH.command('sudo cp /etc/rhsm/ca/redhat-uep.pem ./rhsm-ca/', '\$', 5) cmd.run('cp /etc/rhsm/ca/redhat-uep.pem ./rhsm-ca/')
mySSH.command('sudo cp /etc/pki/entitlement/*.pem ./etc-pki-entitlement/', '\$', 5) cmd.run('cp /etc/pki/entitlement/*.pem ./etc-pki-entitlement/')
baseImage = 'ran-base' baseImage = 'ran-base'
baseTag = 'develop' baseTag = 'develop'
...@@ -389,8 +389,8 @@ class Containerize(): ...@@ -389,8 +389,8 @@ class Containerize():
if (self.ranAllowMerge): if (self.ranAllowMerge):
imageTag = 'ci-temp' imageTag = 'ci-temp'
if self.ranTargetBranch == 'develop': if self.ranTargetBranch == 'develop':
mySSH.command('git diff HEAD..origin/develop -- cmake_targets/build_oai cmake_targets/tools/build_helper docker/Dockerfile.base' + self.dockerfileprefix + ' | grep --colour=never -i INDEX', '\$', 5) cmd.run(f'git diff HEAD..origin/develop -- cmake_targets/build_oai cmake_targets/tools/build_helper docker/Dockerfile.base{self.dockerfileprefix} | grep --colour=never -i INDEX')
result = re.search('index', mySSH.getBefore()) result = re.search('index', cmd.getBefore())
if result is not None: if result is not None:
forceBaseImageBuild = True forceBaseImageBuild = True
baseTag = 'ci-temp' baseTag = 'ci-temp'
...@@ -398,31 +398,31 @@ class Containerize(): ...@@ -398,31 +398,31 @@ class Containerize():
forceBaseImageBuild = True forceBaseImageBuild = True
# Let's remove any previous run artifacts if still there # Let's remove any previous run artifacts if still there
mySSH.command(self.cli + ' image prune --force', '\$', 30) cmd.run(f"{self.cli} image prune --force")
if forceBaseImageBuild: if forceBaseImageBuild:
mySSH.command(self.cli + ' image rm ' + baseImage + ':' + baseTag + ' || true', '\$', 30) cmd.run(f"{self.cli} image rm {baseImage}:{baseTag}")
for image,pattern in imageNames: for image,pattern in imageNames:
mySSH.command(self.cli + ' image rm ' + image + ':' + imageTag + ' || true', '\$', 30) cmd.run(f"{self.cli} image rm {image}:{imageTag}")
# Build the base image only on Push Events (not on Merge Requests) # Build the base image only on Push Events (not on Merge Requests)
# On when the base image docker file is being modified. # On when the base image docker file is being modified.
if forceBaseImageBuild: if forceBaseImageBuild:
mySSH.command(self.cli + ' build ' + self.cliBuildOptions + ' --target ' + baseImage + ' --tag ' + baseImage + ':' + baseTag + ' --file docker/Dockerfile.base' + self.dockerfileprefix + ' . > cmake_targets/log/ran-base.log 2>&1', '\$', 1600) cmd.run(f"{self.cli} build {self.cliBuildOptions} --target {baseImage} --tag {baseImage}:{baseTag} --file docker/Dockerfile.base{self.dockerfileprefix} . &> cmake_targets/log/ran-base.log", timeout=1600)
# First verify if the base image was properly created. # First verify if the base image was properly created.
mySSH.command(self.cli + ' image inspect --format=\'Size = {{.Size}} bytes\' ' + baseImage + ':' + baseTag, '\$', 5) ret = cmd.run(f"{self.cli} image inspect --format=\'Size = {{{{.Size}}}} bytes\' {baseImage}:{baseTag}")
allImagesSize = {} allImagesSize = {}
if mySSH.getBefore().count('o such image') != 0: if ret.returncode != 0:
logging.error('\u001B[1m Could not build properly ran-base\u001B[0m') logging.error('\u001B[1m Could not build properly ran-base\u001B[0m')
# Recover the name of the failed container? # Recover the name of the failed container?
mySSH.command(self.cli + ' ps --quiet --filter "status=exited" -n1 | xargs ' + self.cli + ' rm -f', '\$', 5) cmd.run(f"{self.cli} ps --quiet --filter \"status=exited\" -n1 | xargs {self.cli} rm -f")
mySSH.command(self.cli + ' image prune --force', '\$', 30) cmd.run(f"{self.cli} image prune --force")
mySSH.close() cmd.close()
logging.error('\u001B[1m Building OAI Images Failed\u001B[0m') logging.error('\u001B[1m Building OAI Images Failed\u001B[0m')
HTML.CreateHtmlTestRow(self.imageKind, 'KO', CONST.ALL_PROCESSES_OK) HTML.CreateHtmlTestRow(self.imageKind, 'KO', CONST.ALL_PROCESSES_OK)
HTML.CreateHtmlTabFooter(False) HTML.CreateHtmlTabFooter(False)
sys.exit(1) sys.exit(1)
else: else:
result = re.search('Size *= *(?P<size>[0-9\-]+) *bytes', mySSH.getBefore()) result = re.search('Size *= *(?P<size>[0-9\-]+) *bytes', cmd.getBefore())
if result is not None: if result is not None:
size = float(result.group("size")) / 1000000 size = float(result.group("size")) / 1000000
imageSizeStr = f'{size:.1f}' imageSizeStr = f'{size:.1f}'
...@@ -432,10 +432,10 @@ class Containerize(): ...@@ -432,10 +432,10 @@ class Containerize():
logging.debug('ran-base size is unknown') logging.debug('ran-base size is unknown')
# Recover build logs, for the moment only possible when build is successful # Recover build logs, for the moment only possible when build is successful
mySSH.command(self.cli + ' create --name test ' + baseImage + ':' + baseTag, '\$', 5) cmd.run(f"{self.cli} create --name test {baseImage}:{baseTag}")
mySSH.command('mkdir -p cmake_targets/log/ran-base', '\$', 5) cmd.run("mkdir -p cmake_targets/log/ran-base")
mySSH.command(self.cli + ' cp test:/oai-ran/cmake_targets/log/. cmake_targets/log/ran-base', '\$', 5) cmd.run(f"{self.cli} cp test:/oai-ran/cmake_targets/log/. cmake_targets/log/ran-base")
mySSH.command(self.cli + ' rm -f test', '\$', 5) cmd.run(f"{self.cli} rm -f test")
# Build the target image(s) # Build the target image(s)
status = True status = True
...@@ -444,24 +444,24 @@ class Containerize(): ...@@ -444,24 +444,24 @@ class Containerize():
attemptedImages += [image] attemptedImages += [image]
# the archived Dockerfiles have "ran-base:latest" as base image # the archived Dockerfiles have "ran-base:latest" as base image
# we need to update them with proper tag # we need to update them with proper tag
mySSH.command(f'sed -i -e "s#{baseImage}:latest#{baseImage}:{baseTag}#" docker/Dockerfile.{pattern}{self.dockerfileprefix}', '\$', 5) cmd.run(f'sed -i -e "s#{baseImage}:latest#{baseImage}:{baseTag}#" docker/Dockerfile.{pattern}{self.dockerfileprefix}')
if image != 'ran-build': if image != 'ran-build':
mySSH.command(f'sed -i -e "s#ran-build:latest#ran-build:{imageTag}#" docker/Dockerfile.{pattern}{self.dockerfileprefix}', '\$', 5) cmd.run(f'sed -i -e "s#ran-build:latest#ran-build:{imageTag}#" docker/Dockerfile.{pattern}{self.dockerfileprefix}')
mySSH.command(f'{self.cli} build {self.cliBuildOptions} --target {image} --tag {image}:{imageTag} --file docker/Dockerfile.{pattern}{self.dockerfileprefix} . > cmake_targets/log/{image}.log 2>&1', '\$', 1200) cmd.run(f'{self.cli} build {self.cliBuildOptions} --target {image} --tag {image}:{imageTag} --file docker/Dockerfile.{pattern}{self.dockerfileprefix} . > cmake_targets/log/{image}.log 2>&1', timeout=1200)
# split the log # split the log
mySSH.command('mkdir -p cmake_targets/log/' + image, '\$', 5) cmd.run(f"mkdir -p cmake_targets/log/{image}")
mySSH.command('python3 ci-scripts/docker_log_split.py --logfilename=cmake_targets/log/' + image + '.log', '\$', 5) cmd.run(f"python3 ci-scripts/docker_log_split.py --logfilename=cmake_targets/log/{image}.log")
# checking the status of the build # check the status of the build
mySSH.command(self.cli + ' image inspect --format=\'Size = {{.Size}} bytes\' ' + image + ':' + imageTag, '\$', 5) ret = cmd.run(f"{self.cli} image inspect --format=\'Size = {{{{.Size}}}} bytes\' {image}:{imageTag}")
if mySSH.getBefore().count('o such image') != 0: if ret.returncode != 0:
logging.error('\u001B[1m Could not build properly ' + image + '\u001B[0m') logging.error('\u001B[1m Could not build properly ' + image + '\u001B[0m')
status = False status = False
# Here we should check if the last container corresponds to a failed command and destroy it # Here we should check if the last container corresponds to a failed command and destroy it
mySSH.command(self.cli + ' ps --quiet --filter "status=exited" -n1 | xargs ' + self.cli + ' rm -f', '\$', 5) cmd.run(f"{self.cli} ps --quiet --filter \"status=exited\" -n1 | xargs {self.cli} rm -f")
allImagesSize[image] = 'N/A -- Build Failed' allImagesSize[image] = 'N/A -- Build Failed'
break break
else: else:
result = re.search('Size *= *(?P<size>[0-9\-]+) *bytes', mySSH.getBefore()) result = re.search('Size *= *(?P<size>[0-9\-]+) *bytes', cmd.getBefore())
if result is not None: if result is not None:
size = float(result.group("size")) / 1000000 size = float(result.group("size")) / 1000000
imageSizeStr = f'{size:.1f}' imageSizeStr = f'{size:.1f}'
...@@ -471,18 +471,18 @@ class Containerize(): ...@@ -471,18 +471,18 @@ class Containerize():
logging.debug(f'{image} size is unknown') logging.debug(f'{image} size is unknown')
allImagesSize[image] = 'unknown' allImagesSize[image] = 'unknown'
# Now pruning dangling images in between target builds # Now pruning dangling images in between target builds
mySSH.command(self.cli + ' image prune --force', '\$', 30) cmd.run(f"{self.cli} image prune --force")
# Remove all intermediate build images and clean up # Remove all intermediate build images and clean up
if self.ranAllowMerge and forceBaseImageBuild: if self.ranAllowMerge and forceBaseImageBuild:
mySSH.command(f'{self.cli} image rm {baseImage}:{baseTag} || true', '\$', 30) cmd.run(f"{self.cli} image rm {baseImage}:{baseTag}")
mySSH.command(f'{self.cli} image rm ran-build:{imageTag}','\$', 30) cmd.run(f"{self.cli} image rm ran-build:{imageTag}")
mySSH.command(f'{self.cli} volume prune --force','\$', 15) cmd.run(f"{self.cli} volume prune --force")
# create a zip with all logs # create a zip with all logs
build_log_name = f'build_log_{self.testCase_id}' build_log_name = f'build_log_{self.testCase_id}'
CopyLogsToExecutor(mySSH, lSourcePath, build_log_name, lIpAddr, lUserName, lPassWord) CopyLogsToExecutor(cmd, lSourcePath, build_log_name)
mySSH.close() cmd.close()
# Analyze the logs # Analyze the logs
collectInfo = AnalyzeBuildLogs(build_log_name, attemptedImages, status) collectInfo = AnalyzeBuildLogs(build_log_name, attemptedImages, status)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment