Helm.py 2.31 KB
Newer Older
1
2
3
4
5
6
from flask_restful import current_app

from import_api_app.utility import generate_helm_name
import import_api_app.helm as helm
import os
from subprocess import CalledProcessError
Lionel Walter's avatar
Lionel Walter committed
7
from import_api_app.helpers.Error import ImportApiError
8
9
10
11


def start(process_id, job_name, job_parameters):
    helm_client = helm.Helm()
Jonas Waeber's avatar
Jonas Waeber committed
12
    helm_name = generate_helm_name(process_id, job_parameters['recordSetId'], job_name)
13
14
15
16
17
18
19
20
21
22
23
24
25
26

    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',
Lionel Walter's avatar
Lionel Walter committed
27
                       'message': 'Job successfully started',
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
                       'job_id': helm_name
                   }, 201
        else:
            return {
                       'status': 'FAILURE',
                       'message': message,
                       'job_id': helm_name
                   }, 500
    except CalledProcessError:
        message = "It was not possible to run the helm install command"
        current_app.logger.error(message)
        raise ImportApiError(message)


def stop(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 CalledProcessError:
        message = "It was not possible to run the helm uninstall command"
        current_app.logger.error(message)
        raise ImportApiError(message)