Verified Commit bc51526d authored by Sebastian Schüpbach's avatar Sebastian Schüpbach
Browse files

use tempdirs for helm installations

parent ae34fdbc
Pipeline #31988 passed with stages
in 2 minutes and 21 seconds
......@@ -2,9 +2,10 @@ from flask_restful import Resource
from flask import request as flaskRequest
import json
import subprocess
import re
import os
import shutil
from re import match
from os import environ, listdir, path
from shutil import rmtree
from tempfile import mkdtemp
from autodeploy_service_app.app import app
......@@ -60,16 +61,13 @@ class AutoDeploy(Resource):
repositoryPath = body['project']['path_with_namespace']
app.logger.info('deploy-request received from ' + repositoryUrl)
app.logger.debug('removing workdir')
shutil.rmtree('autodeploy-workdir/', ignore_errors=True)
# if tag = semver: deploy on prod+stage
if re.match(r'^(([0-9]+)\.([0-9]+)\.([0-9]+))$', tag):
if match(r'^(([0-9]+)\.([0-9]+)\.([0-9]+))$', tag):
app.logger.debug(
'commit with semver-tag detected: installing on prod+stage'
)
pullChartUri = (
os.environ['GITLAB_REGISTRY']
environ['GITLAB_REGISTRY']
+ '/'
+ repositoryPath
+ ':'
......@@ -174,7 +172,7 @@ def installFromRepo(pullChartUri, projectName):
)
app.logger.warning(msg)
return output, 'failure'
pulledChartsDir = os.getcwd() + '/autodeploy-workdir/pulled-helm-charts/'
pulledChartsDir = mkdtemp()
app.logger.debug('exporting helm charts')
try:
proc = subprocess.run(
......@@ -198,21 +196,22 @@ def installFromRepo(pullChartUri, projectName):
)
app.logger.warning(msg)
return output, 'failure'
pulledChartsDir += projectName
helmValuesDir = path.join(pulledChartsDir, projectName, "helm-values")
# TODO: Remove later
print(pulledChartsDir)
if os.path.exists(pulledChartsDir + '/helm-values'):
app.logger.info(helmValuesDir)
if path.exists(helmValuesDir):
# TODO: Change to debug later
app.logger.info('helm value files detected')
for filename in os.listdir(pulledChartsDir + '/helm-values/'):
filenameBase = os.path.splitext(filename)[0]
if '-prod.' in filename or '-stage.' in filename:
for filename in listdir(helmValuesDir):
filenameBase = path.splitext(filename)[0]
if ('-prod.' in filename or '-stage.' in filename) and status != 'failure':
# TODO: Remove later
app.logger.info('upgrade installation for ' + filename)
msg, status = _upgrade_installation(
pullChartUri, projectName, filenameBase, filename
)
output.append(msg)
rmtree(pulledChartsDir)
return output, status
......@@ -220,35 +219,37 @@ def installFromDir(repositoryUrl, projectName):
output = []
status = ''
app.logger.debug('cloning repository')
repoDir = mkdtemp()
try:
subprocess.run(
'git clone ' + repositoryUrl + ' autodeploy-workdir/',
'git clone {} {}'.format(repositoryUrl, repoDir),
shell=True,
capture_output=True,
text=True,
check=True,
)
output.append(
'git clone {}'.format(repositoryUrl)
)
output.append('git clone {}'.format(repositoryUrl))
except subprocess.CalledProcessError as ex:
msg = 'cloning git repo failed with return code {} -- STDOUT: {} -- STDERR: {}'.format(
ex.returncode, ex.stdout, ex.stderr
)
app.logger.warning(msg)
return output, 'failure'
chartsDir = os.getcwd() + '/autodeploy-workdir/helm-charts'
if os.path.exists(chartsDir + '/helm-values'):
helmChartDir = path.join(repoDir, 'helm-charts')
helmValueDir = path.join(helmChartDir, 'helm-values')
if path.exists(helmValueDir):
app.logger.debug('helm value files detected')
for filename in os.listdir(chartsDir + '/helm-values/'):
filenameBase = os.path.splitext(filename)[0]
for filename in listdir(helmValueDir):
filenameBase = path.splitext(filename)[0]
if '-test.' in filename:
msg, status = _upgrade_installation(
chartsDir, projectName, filenameBase, filename
helmChartDir, projectName, filenameBase, filename
)
output.append(msg)
else:
# this section should be obsolete since we shouldn't have helm-charts w/o valuefiles
msg, status = _upgrade_installation(chartsDir, projectName, projectName)
msg, status = _upgrade_installation(helmChartDir, projectName, projectName)
output.append(msg)
rmtree(repoDir)
return output, 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