Commit 3ad40104 authored by Lionel Walter's avatar Lionel Walter
Browse files

Create HelmStart and HelmStop

parent 45c36513
from flask_restful import Resource, current_app, reqparse
from flasgger import swag_from
from import_api_app.utility import generate_helm_name
import import_api_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
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