Commit fcc0c4cd authored by Lionel Walter's avatar Lionel Walter
Browse files

Refactor HelmStart and HelmStop

parent eb10d165
......@@ -4,27 +4,24 @@ from flask_restful import Api
from flasgger import Swagger
from kubernetes.config import ConfigException
from import_api_app.resources.HelmStart import HelmStart
from import_api_app.resources.HelmStop import HelmStop
from import_api_app.resources.JobList import JobList
from import_api_app.resources.JobStart import JobStart
from import_api_app.resources.JobStop import JobStop
from import_api_app.resources.JobReport import JobReport
from import_api_app.resources.Job import Job
from import_api_app.resources.KafkaTopics import KafkaTopics
# from import_api_app.resources.ReadJobOptionsFromDrupal import ReadJobOptionsFromDrupal
import import_api_app.configuration
from import_api_app.resources.WriteJobResultsToDrupal import WriteJobResultToDrupal
from import_api_app.resources.ReadJobOptionsFromDrupal import ReadJobOptionsFromDrupal
# from import_api_app.resources.HelmStart import HelmStart
# from import_api_app.resources.HelmStop import HelmStop
import os
import logging
def create_app(test_config=None):
app = Flask(__name__)
app.config.from_object(import_api_app.configuration)
if os.environ['FLASK_ENV'] == 'development':
app.logger.setLevel(logging.DEBUG)
# app.config.from_envvar('YOURAPPLICATION_SETTINGS')
# Based on this example :
......@@ -70,11 +67,11 @@ def create_app(test_config=None):
)
api.add_resource(JobStop, '/v1/job/<process_id>/<job_name>/stop')
# api.add_resource(
# HelmStart,
# '/v1/helm/<institution_id>/<record_set_id>/<process_id>/<job_name>/start'
# )
# api.add_resource(HelmStop, '/v1/helm/<process_id>/<job_name>/stop')
api.add_resource(
HelmStart,
'/v1/helm/<institution_id>/<record_set_id>/<process_id>/<job_name>/start'
)
api.add_resource(HelmStop, '/v1/helm/<process_id>/<job_name>/stop')
api.add_resource(Job, '/v1/job/<process_id>/<job_name>')
api.add_resource(WriteJobResultToDrupal, '/v1/drupal/<job_log_drupal_uuid>/<report>')
......
class ImportApiError(Exception):
def __init__(self, message):
self.message = message
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
from import_api_app.models.Error import ImportApiError
def start(process_id, job_name, job_parameters):
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 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)
from flask_restful import Resource, current_app, reqparse
from flask_restful import Resource, reqparse
from flasgger import swag_from
from import_api_app.utility import generate_helm_name
import import_api_app.helm as helm
import os
from subprocess import CalledProcessError
from import_api_app.models.Error import ImportApiError
from import_api_app.models.Helm import start
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):
......@@ -21,31 +17,7 @@ class HelmStart(Resource):
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 CalledProcessError:
current_app.logger.error("It was not possible to run the helm install command")
return {'error': 'Unexpected Helm error'}, 500
return start(process_id, job_name, job_parameters)
except ImportApiError as e:
return {'error': e.message}, 500
from flask_restful import Resource, current_app
import import_api_app.helm as helm
from flask_restful import Resource
from flasgger import swag_from
from import_api_app.utility import generate_helm_name
from subprocess import CalledProcessError
from import_api_app.models.Error import ImportApiError
from import_api_app.models.Helm import stop
class HelmStop(Resource):
......@@ -10,30 +10,7 @@ class HelmStop(Resource):
# @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 CalledProcessError:
current_app.logger.error("It was not possible to run the helm install command")
return stop(process_id, job_name)
except ImportApiError:
return {'error': 'Unexpected Helm error'}, 500
......@@ -15,12 +15,12 @@ def test_job_list(client):
assert response.status_code == 200
def test_job_start(client):
def test_helm_start(client):
jobs_response = client.get('/v1/jobs')
jobs_json = jobs_response.get_json()
number_of_jobs_before = len(jobs_json)
response = client.post('/v1/job/123/456/789/text-file-validation/start', json={
response = client.post('/v1/helm/123/456/789/text-file-validation/start', json={
"job-parameters": {
"appDirectory": "AFZ/AfZ-Becker-Audiovisuals"
}
......@@ -34,7 +34,7 @@ def test_job_start(client):
assert number_of_jobs_after == number_of_jobs_before + 1
# try to start a second job with same job id
response = client.post('/v1/job/123/456/789/text-file-validation/start', json={
response = client.post('/v1/helm/123/456/789/text-file-validation/start', json={
"job-parameters": {
"appDirectory": "AFZ/AfZ-Becker-Audiovisuals"
}
......@@ -42,7 +42,7 @@ def test_job_start(client):
assert response.status_code == 500
# use invalid chart name
response = client.post('/v1/job/123/456/789/TESTTEST/start', json={
response = client.post('/v1/helm/123/456/789/TESTTEST/start', json={
"job-parameters": {
"appDirectory": "AFZ/AfZ-Becker-Audiovisuals",
}
......@@ -58,14 +58,14 @@ def test_get_job(client):
assert response.status_code == 404
def test_job_stop(client):
response = client.delete('/v1/job/789/text-file-validation/stop')
def test_helm_stop(client):
response = client.delete('/v1/helm/789/text-file-validation/stop')
assert response.status_code == 200
response = client.delete('/v1/job/789/jdskfhjkdshjkewhrjkewrjkewhjkhrjkewhr/stop')
response = client.delete('/v1/helm/789/jdskfhjkdshjkewhrjkewrjkewhjkhrjkewhr/stop')
assert response.status_code == 500
response = client.get('/v1/job/789/text-file-validation')
response = client.get('/v1/helm/789/text-file-validation')
assert response.status_code == 404
......
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