Demo entry 6789084

12

   

Submitted by anonymous on Apr 25, 2019 at 09:03
Language: Python 3. Code size: 5.3 kB.

import json
import request
import logging
import requests
from datetime import datetime
from flask import Flask, request
from pyhelm.repo import RepoUtils
from pyhelm.tiller import Tiller
from pyhelm.chartbuilder import ChartBuilder
from cassandra_db import transfer_path, write_data, obtain_data, delete_data, obtain_port

Dingo_Web = Flask(__name__)
timeID = datetime.now()
tiller_ins = Tiller('127.0.0.1', 27413)


def save_log(data, log_path=transfer_path("log/train.log")):
    data = str(data)
    with open(log_path, "a") as f:
        f.write("timeID: {}\n".format(timeID))
        f.write("{}: {}\n".format(datetime.now(), data))


@Dingo_Web.route('/ai_train_or_predict/', methods=['POST', 'GET'])
def http_ai_train_or_predict():
    save_log("enter ai_train_or_predict function")
    try:
        data = json.loads(request.data.decode())
        docker_name = data["user_id"] + data["model_name"]
        chart_path = RepoUtils.from_repo('http://nexus.daleding.com/repository/helm-hosted/', 'dingo-ai', '0.1.0')
        chart = ChartBuilder({'name': 'demo', 'source': {'type': 'directory', 'location': chart_path}})
        origin_port = 20008
        new_port = 20008
        while origin_port > 0:
            exist_port = obtain_data(origin_port=origin_port)
            if any(exist_port) is False:  # 判断对象是否为空
                new_port = origin_port
                service_name = data["user_id"] + data["model_name"]
                write_data(rows=[data["user_id"], data["model_name"], str(new_port), service_name])
                break
            else:
                origin_port += 1
        print(new_port)
        data1 = str(data)
        escape_data = data1.replace("'", "\"")   # 转义
        values = {
            "service": {"nodePort": new_port},
            "resources": {
                "limits": {
                    "cpu": "1000m"
                }
            },
            "image": {"args": '{data}'.format(data=escape_data)}
        }
        tiller_ins.install_release(chart.get_helm_chart(), dry_run=False, namespace='default', timeout=50,
                                   name=docker_name, values=values)  # dry_run = True模拟参数测试,没有安装服务
        return json.dumps({"result": "yes", "function_result": "success"})
    except KeyError:
        logging.error("error json in ai_train function")
        return json.dumps({"result": "no", "function_result": "no"})


@Dingo_Web.route('/del_ai_model/', methods=['POST', 'GET'])
def http_del_ai_model():
    save_log("enter del_ai_model function")
    try:
        data = json.loads(request.data.decode())
        docker_name = data["user_id"] + data["model_name"]
        headers = {'Content-Type': 'application/json'}
        # 删除 release
        tiller_ins.uninstall_release(docker_name, purge=True)  # purge=True可以彻底删除tiller
        # 查端口
        obtain_port_result = obtain_port(docker_name)
        for x in obtain_port_result:
            docker_request = requests.post("http://127.0.0.1:{}/del_model/".format(x.port), json=data, headers=headers)
            new_docker_request = json.loads(docker_request.text)
            if new_docker_request["result"] == "yes":
                return json.dumps({"result": "yes", "function_result": "success"})
            else:
                logging.error("error json in del_model function")
                return json.dumps({"result": "no", "function_result": "no"})
        # 删端口
        delete_result = delete_data(docker_name)
        if delete_result != "success":
            logging.error("error in delete_data function")

    except KeyError:
        logging.error("error json in del_ai_model function")
        return json.dumps({"result": "no", "function_result": "no"})


@Dingo_Web.route('/stop_ai_training/', methods=['POST', 'GET'])
def http_stop_ai_training():
    save_log("enter stop_ai_training function")
    try:
        data = json.loads(request.data.decode())
        docker_name = data["user_id"] + data["model_name"]
        # service_name = obtain_data(data["user_id"] + data["model_name"])
        # tiller_ins.uninstall_release(docker_name, purge=True)
        # 查端口
        obtain_port_result = obtain_port(docker_name)
        for x in obtain_port_result:
            docker_request = requests.get('http://10.129.4.53:{}/stop_training/'.format(x.port))
            # print(docker_request.text)
            new_docker_request = json.loads(docker_request.text)
            if new_docker_request["result"] == "yes":
                return json.dumps({"result": "yes", "function_result": "success"})
            else:
                logging.error("error json in stop_training function")
                return json.dumps({"result": "no", "function_result": "no"})
        # 删端口
        delete_result = delete_data(docker_name)
        if delete_result != "success":
            logging.error("error in delete_data function")
    except KeyError:
        logging.error("error json in stop_ai_training function")
        return json.dumps({"result": "no", "function_result": "no"})


@Dingo_Web.route('/hello/', methods=['POST', 'GET'])
def api_server_hello():
    return "0"


if __name__ == "__main__":
    Dingo_Web.run(host='0.0.0.0', port=8010)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).