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

remove obsolete files

parent d5a62368
# Copyright (C) 2019 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# https://gerrit.googlesource.com/k8s-gerrit/+/refs/heads/master/tests/helpers/helm.py
import json
import subprocess
class Helm:
def _exec_command(self, cmd, fail_on_err=True):
base_cmd = [
"helm",
]
# for debug print (' '.join(base_cmd+cmd))
return subprocess.run(
base_cmd + cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=fail_on_err,
text=True,
)
def install(
self,
chart,
name,
values_file=None,
set_values=None,
namespace=None,
fail_on_err=True,
wait=True,
):
"""Installs a chart on the cluster
Arguments:
chart {str} -- Release name or path of a helm chart
name {str} -- Name with which the chart will be installed on the cluster
Keyword Arguments:
values_file {str} -- Path to a custom values.yaml file (default: {None})
set_values {dict} -- Dictionary containing key-value-pairs that are used
to overwrite values in the values.yaml-file.
(default: {None})
namespace {str} -- Namespace to install the release into (default: {default})
fail_on_err {bool} -- Whether to fail with an exception if the installation
fails (default: {True})
wait {bool} -- Whether to wait for all pods to be ready (default: {True})
Returns:
CompletedProcess -- CompletedProcess-object returned by subprocess
containing details about the result and output of the
executed command.
"""
helm_cmd = ["install", name, chart]
if values_file:
helm_cmd.extend(("-f", values_file))
if set_values:
opt_list = ["%s=%s" % (k, v) for k, v in set_values.items()]
helm_cmd.extend(("--set", ",".join(opt_list)))
if namespace:
helm_cmd.extend(("--namespace", namespace))
if wait:
helm_cmd.append("--wait")
return self._exec_command(helm_cmd, fail_on_err)
def list(self):
"""Lists helm charts installed on the cluster.
Returns:
list -- List of helm chart realeases installed on the cluster.
"""
helm_cmd = ["list", "--all", "--output", "json"]
output = self._exec_command(helm_cmd).stdout
output = json.loads(output)
return output["Releases"]
def upgrade(
self,
chart,
name,
values_file=None,
set_values=None,
reuse_values=True,
recreate_pods=False,
fail_on_err=True,
):
"""Updates a chart on the cluster
Arguments:
chart {str} -- Release name or path of a helm chart
name {str} -- Name with which the chart will be installed on the cluster
Keyword Arguments:
values_file {str} -- Path to a custom values.yaml file (default: {None})
set_values {dict} -- Dictionary containing key-value-pairs that are used
to overwrite values in the values.yaml-file.
(default: {None})
reuse_values {bool} -- Whether to reuse existing not overwritten values
(default: {True})
recreate_pods {bool} -- Whether to restart changed pods (default: {False})
fail_on_err {bool} -- Whether to fail with an exception if the installation
fails (default: {True})
Returns:
CompletedProcess -- CompletedProcess-object returned by subprocess
containing details about the result and output of the
executed command.
"""
helm_cmd = ["upgrade", name, chart, "--wait"]
if values_file:
helm_cmd.extend(("-f", values_file))
if reuse_values:
helm_cmd.append("--reuse-values")
if recreate_pods:
helm_cmd.append("--recreate-pods")
if set_values:
opt_list = ["%s=%s" % (k, v) for k, v in set_values.items()]
helm_cmd.extend(("--set", ",".join(opt_list)))
return self._exec_command(helm_cmd, fail_on_err)
def uninstall(self, name, namespace=None, fail_on_err=True):
"""Uninstall a chart from the cluster
Arguments:
name {str} -- Name of the chart to delete
Keyword Arguments:
namespace {str} -- Namespace to uninstallnstall the release into (default: {default})
fail_on_err {bool} -- Whether to fail with an exception if the installation
fails (default: {True})
Returns:
CompletedProcess -- CompletedProcess-object returned by subprocess
containing details about the result and output of the
executed command.
"""
helm_cmd = ["uninstall", name]
if namespace:
helm_cmd.extend(("--namespace", namespace))
return self._exec_command(helm_cmd, fail_on_err)
def uninstall_all(self, exceptions=None):
"""Deletes all charts on the cluster
Keyword Arguments:
exceptions {list} -- List of chart names not to delete (default: {None})
"""
charts = self.list()
for chart in charts:
if chart["Name"] in exceptions:
continue
self.uninstall(chart["Name"])
from flask_restful import Resource, current_app, reqparse
from flasgger import swag_from
from autodeploy_service_app.utility import generate_helm_name
import autodeploy_service_app.helm as helm
import os
class HelmStart(Resource):
# Todo validate requests
# @swag.validate('job-parameters')
@swag_from("HelmStart.yml")
def post(self, institution_id, record_set_id, process_id, job_name):
parser = reqparse.RequestParser()
parser.add_argument("job-parameters", type=dict)
args = parser.parse_args()
job_parameters = args["job-parameters"]
job_parameters["institutionId"] = institution_id
job_parameters["recordSetId"] = record_set_id
job_parameters["processId"] = process_id
helm_client = helm.Helm()
helm_name = generate_helm_name(process_id, job_name)
try:
output = helm_client.install(
chart=os.path.join(current_app.root_path, "charts", job_name),
name=helm_name,
namespace=current_app.config["NAMESPACE"],
set_values=job_parameters,
fail_on_err=False,
)
message = output.stdout + output.stderr
message = message.replace("\n", ". ")
if output.returncode == 0:
return {
"status": "SUCCESS",
"message": message,
"job_id": helm_name,
}, 201
else:
return {
"status": "FAILURE",
"message": message,
"job_id": helm_name,
}, 500
except BaseException:
current_app.logger.error(
"It was not possible to run the helm install command"
)
return {"error": "Unexpected Helm error"}, 500
Start a job in the kubernetes cluster (using helm charts)
---
tags:
- Helm
parameters:
- in: body
name: body
schema:
id: job-parameters
properties:
job-parameters:
type: object
properties:
appDirectory:
type: string
example: BECKER
description: The directory in the SFTP server
- in: path
name: institution_id
required: true
description: The Institution Identifier. We start with Drupal Node Id and will use the official identifier once it has been defined.
default: 123
type: string
- in: path
name: record_set_id
required: true
description: The ID of the Record Set (Bestand). We start with Drupal Node Id and will use the official identifier once it has been defined.
default: 456
type: string
- in: path
name: process_id
required: true
description: The identifier of the import process. Must be lowercase and
contain only letters, numbers and -.
default: 789
- in: path
type: string
name: job_name
example: text-file-validation
enum: ['text-file-validation', 'table-data-transform', 'mapper-service']
required: true
responses:
201:
description: Success, job is started
schema:
id: helm-result
properties:
status:
type: string
example: SUCCESS/FAILURE
enum: ['SUCCESS', 'FAILURE']
message:
type: string
example: 'NAME: j0001.
LAST DEPLOYED: Thu May 14 16:15:54 2020.
NAMESPACE: memobase. STATUS: deployed. REVISION: 1.
TEST SUITE: None.'
job_id:
type: string
example: j0001
500:
description: It was impossible to start the job
schema:
$ref: '#/definitions/helm-result'
from flask_restful import Resource, current_app
import import_api_app.helm as helm
from flasgger import swag_from
from import_api_app.utility import generate_helm_name
class HelmStop(Resource):
# Todo validate requests
# @swag.validate('job-parameters')
@swag_from("HelmStop.yml")
def delete(self, process_id, job_name):
helm_client = helm.Helm()
helm_name = generate_helm_name(process_id, job_name)
try:
output = helm_client.uninstall(
name=helm_name,
namespace=current_app.config["NAMESPACE"],
fail_on_err=False,
)
message = output.stdout + output.stderr
message = message.replace("\n", ". ")
if output.returncode == 0:
return {
"status": "SUCCESS",
"message": message,
"job_id": helm_name,
}, 200
else:
return {
"status": "FAILURE",
"message": message,
"job_id": helm_name,
}, 500
except BaseException:
current_app.logger.error(
"It was not possible to run the helm install command"
)
return {"error": "Unexpected Helm error"}, 500
Stop a job in the kubernetes cluster (using helm charts)
---
tags:
- Helm
parameters:
- in: path
name: process_id
required: true
description: The identifier of the import process. Must be lowercase and
contain only letters, numbers and -.
default: 789
- in: path
type: string
name: job_name
example: text-file-validation
enum: ['text-file-validation', 'table-data-transform', 'mapper-service']
required: true
responses:
200:
description: Success, job is deleted
schema:
id: helm-result
properties:
status:
type: string
example: SUCCESS/FAILURE
enum: ['SUCCESS', 'FAILURE']
message:
type: string
example: 'NAME: j0001. LAST DEPLOYED: Thu May 14 16:15 2020.
NAMESPACE: memobase. STATUS: deployed.
REVISION: 1. TEST SUITE: None.'
job_id:
type: string
example: j0001
500:
description: It was impossible to stop the job
schema:
$ref: '#/definitions/helm-result'
\ No newline at end of file
from datetime import timezone
def generate_helm_name(process_id, job_name):
return process_id + "-" + job_name
def get_job_info(job):
""" "
job is a Kubernetes V1Job
"""
utc_start_time = job.status.start_time
local_start_time = utc_start_time.replace(tzinfo=timezone.utc).astimezone(tz=None)
status = "Unknown"
if job.status.failed:
status = "Failed"
elif job.status.active:
status = "Running"
return {
"status": status,
"started": local_start_time.strftime("%m/%d/%Y, %H:%M:%S"),
}
......@@ -6,7 +6,7 @@ from kubernetes import config
import os
import logging
from autodeploy_service_app.app import app
from autodeploy_service_app.resources.AutoDeploy import AutoDeploy
from autodeploy_service_app.AutoDeploy import AutoDeploy
# TODO : maybe take that to a configuration (development vs pod running in
......
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