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

refactor installation strategies as different functions

parent ab49b1d7
Pipeline #31926 failed with stages
in 55 seconds
......@@ -55,34 +55,39 @@ class AutoDeploy(Resource):
branch = body["object_attributes"]["ref"]
for stage in body["builds"]:
if stage["stage"] == "publish" and stage["status"] == "success":
status = "success"
projectName = body["project"]["path_with_namespace"].split("/")[-1]
repositoryUrl = body["project"]["git_http_url"]
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)
app.logger.debug("cloning repository")
proc = subprocess.run(
"git clone " + repositoryUrl + " autodeploy-workdir/",
shell=True,
capture_output=True,
text=True,
check=True,
)
output.append(
"git clone {}: {} (stderr: {})".format(
repositoryUrl, proc.stdout, proc.stderr
)
)
if os.path.exists(os.getcwd() + "/autodeploy-workdir/helm-charts"):
msgs, status = deployHelmCharts(
projectName, repositoryPath, tag, branch
# if tag = semver: deploy on prod+stage
if re.match(r"^(([0-9]+)\.([0-9]+)\.([0-9]+))$", tag):
app.logger.debug(
"commit on prod-branch with semver-tag detected: installing on prod+stage"
)
pullChartUri = (
os.environ["GITLAB_REGISTRY"]
+ "/"
+ repositoryPath
+ ":"
+ tag
+ "-chart"
)
msgs, status = installFromRepo(pullChartUri, projectName)
output.extend(msgs)
# deploy on test
elif branch == "master":
app.logger.debug(
"commit on master-branch without semver-tag detected: installing on test"
)
msgs, status = installFromDir(repositoryUrl, projectName)
output.extend(msgs)
except subprocess.CalledProcessError as ex:
msg = "command {} failed with return code {} -- STDOUT: {} -- STDERR: {}".format(
ex.cmd, ex.returncode, ex.stdout, ex.stderr
......@@ -141,42 +146,39 @@ def _upgrade_installation(chartsDir, projectName, filenameBase, filename=None):
"success",
)
except subprocess.CalledProcessError as ex:
msg = (
"command {} failed with return code {} -- STDOUT: {} -- STDERR: {}".format(
ex.cmd, ex.returncode, ex.stdout, ex.stderr
)
msg = "upgrading helm chart failed with return code {} -- STDOUT: {} -- STDERR: {}".format(
ex.cmd, ex.returncode, ex.stdout, ex.stderr
)
app.logger.warning(msg)
return msg, "failure"
def deployHelmCharts(projectName, repositoryPath, tag, branch):
app.logger.debug("helm charts detected")
def installFromRepo(pullChartUri, projectName):
output = []
status = ""
if re.match(
r"^(([0-9]+)\.([0-9]+)\.([0-9]+))$", tag
): # if tag = semver: deploy on prod+stage
app.logger.debug(
"commit on prod-branch with semver-tag detected: installing on prod+stage"
)
pullChartUri = (
os.environ["GITLAB_REGISTRY"] + "/" + repositoryPath + ":" + tag + "-chart"
)
app.logger.debug("pulling helm charts")
app.logger.debug("pulling helm charts")
try:
proc = subprocess.run(
"export HELM_EXPERIMENTAL_OCI=1 && helm chart pull " + pullChartUri,
shell=True,
capture_output=True,
text=True,
check=True,
)
output.append(
"pulling charts from {}: {} (stderr: {})".format(
pullChartUri, proc.stdout, proc.stderr
)
)
pulledChartsDir = os.getcwd() + "/autodeploy-workdir/pulled-helm-charts/"
app.logger.debug("exporting helm charts")
except subprocess.CalledProcessError as ex:
msg = "pulling helm chart failed with return code {} -- STDOUT: {} -- STDERR: {}".format(
ex.cmd, ex.returncode, ex.stdout, ex.stderr
)
app.logger.warning(msg)
return output, "failure"
pulledChartsDir = os.getcwd() + "/autodeploy-workdir/pulled-helm-charts/"
app.logger.debug("exporting helm charts")
try:
proc = subprocess.run(
"export HELM_EXPERIMENTAL_OCI=1 && helm chart export "
+ pullChartUri
......@@ -185,38 +187,67 @@ def deployHelmCharts(projectName, repositoryPath, tag, branch):
shell=True,
capture_output=True,
text=True,
check=True,
)
output.append(
"exporting charts to local directory: {} (stderr: {})".format(
proc.stdout, proc.stderr
)
)
pulledChartsDir += projectName
if os.path.exists(pulledChartsDir + "/helm-values"):
app.logger.debug("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:
msg, status = _upgrade_installation(
pullChartUri, projectName, filenameBase, filename
)
output.append(msg)
elif branch == "master": # deploy on test
app.logger.debug(
"commit on master-branch without semver-tag detected: installing on test"
except subprocess.CalledProcessError as ex:
msg = "exporting helm chart failed with return code {} -- STDOUT: {} -- STDERR: {}".format(
ex.cmd, ex.returncode, ex.stdout, ex.stderr
)
chartsDir = os.getcwd() + "/autodeploy-workdir/helm-charts"
if os.path.exists(chartsDir + "/helm-values"):
app.logger.debug("helm value files detected")
for filename in os.listdir(chartsDir + "/helm-values/"):
filenameBase = os.path.splitext(filename)[0]
if "-test." in filename:
msg, status = _upgrade_installation(
chartsDir, 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)
output.append(msg)
app.logger.warning(msg)
return output, "failure"
pulledChartsDir += projectName
if os.path.exists(pulledChartsDir + "/helm-values"):
app.logger.debug("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:
msg, status = _upgrade_installation(
pullChartUri, projectName, filenameBase, filename
)
output.append(msg)
return output, status
def installFromDir(repositoryUrl, projectName):
output = []
status = ""
app.logger.debug("cloning repository")
try:
proc = subprocess.run(
"git clone " + repositoryUrl + " autodeploy-workdir/",
shell=True,
capture_output=True,
text=True,
check=True,
)
output.append(
"git clone {}: {} (stderr: {})".format(
repositoryUrl, proc.stdout, proc.stderr
)
)
except subprocess.CalledProcessError as ex:
msg = "cloning git repo failed with return code {} -- STDOUT: {} -- STDERR: {}".format(
ex.cmd, 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"):
app.logger.debug("helm value files detected")
for filename in os.listdir(chartsDir + "/helm-values/"):
filenameBase = os.path.splitext(filename)[0]
if "-test." in filename:
msg, status = _upgrade_installation(
chartsDir, 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)
output.append(msg)
return output, status
Supports Markdown
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