Exemplo n.º 1
0
def conf_commit_http(url):
    json_command={
        "conf-begin": {
            "sendblock": {
                "cmd": "conf-commit"
            },
            "receive": {
                "type": "block"
            }
        }
    }
    utils.send_http(url, json_command)
Exemplo n.º 2
0
def sync_cname_default(id_zone, id_record):
    tags = {"id_record": id_record}
    cmd.zone_begin_http(url, tags)
    json_command = cmd.zone_insert(tags)
    check_res = send_http(url, json_command)
    if check_res:
        # state change
        state = change_state("id_record", id_record, "1")
        db.update("zn_record", data=state)
    cmd.zone_commit_http(url, tags)
Exemplo n.º 3
0
def sync_ns(id_zone):
    tags = {"id_zone": id_zone}
    cmd.z_begin(url, tags)
    result_ns = cmd.zone_ns_insert(tags)
    for i in result_ns:
        check_res = send_http(url, i['command'])
        if check_res:
            state = change_state("id_record", i['id_record'], "1")
            db.update("zn_record", data=state)
    cmd.z_commit(url, tags)
Exemplo n.º 4
0
def sync_soa(id_zone):
    tags = {"id_zone": id_zone}
    cmd.z_begin(url, tags)
    id_record, respons = cmd.zone_soa_insert_default(tags)
    check_res = send_http(url, respons)
    if check_res:
        # state change
        state = change_state("id_record", id_record, "1")
        db.update("zn_record", data=state)

    cmd.z_commit(url, tags)
Exemplo n.º 5
0
def sync_conf_insert(id_zone):
    tags = {"id_zone": id_zone}
    respons_c_insert = cmd.config_insert(tags)
    cmd.conf_begin_http(url)
    check_res = send_http(url, respons_c_insert)
    if check_res:
        # state change
        state = change_state("id_zone", id_zone, "1")
        check = db.update("zn_zone", data=state)
        print(check)
    cmd.conf_commit_http(url)
Exemplo n.º 6
0
def zone_commit_http(url, tags):
    fields = tags['id_record']
    domain_data = model.get_by_id("v_record", "id_record", fields)
    json_command={
        "zone-commit": {
            "sendblock": {
                "cmd": "zone-commit",
                "zone": domain_data[0]['nm_zone']
            },
            "receive": {
                "type": "block"
            }
        }
    }
    res = utils.send_http(url, json_command)
    return res
Exemplo n.º 7
0
def z_begin(url,tags):
    domain_name = None
    fields = tags['id_zone']
    domain_data = model.get_by_id("zn_zone", "id_zone", fields)
    for i in domain_data:
        domain_name = i['nm_zone']
    json_command={
        "zone-begin": {
            "sendblock": {
                "cmd": "zone-begin",
                "zone": domain_name
            },
            "receive": {
                "type": "block"
            }
        }
    }
    return utils.send_http(url,json_command)
Exemplo n.º 8
0
    def post(self):
        url_env = os.getenv("SOCKET_AGENT_HOST")
        port = os.getenv("SOCKET_AGENT_PORT")
        url_fix = url_env + ":" + port
        url = url_fix + "/api/command_rest"
        json_req = request.get_json(force=True)
        command = utils.get_command(request.path)
        init_data = parse.parser(json_req, command)
        respons = dict()

        if init_data['action'] == 'conf-read':
            respons = cmd.conf_read()
            http_respons = utils.send_http(url, data=respons)
            return response(200, data=http_respons)

        if init_data['action'] == 'conf-insert':
            tags = dict()
            for i in init_data['data']:
                tags = i['tags']
            respons = cmd.config_insert(tags)
            cmd.conf_begin_http(url)
            http_respons = utils.send_http(url, respons)
            if http_respons:
                # state change
                state = utils.change_state("id_zone", tags['id_zone'], "1")
                db.update("zn_zone", data=state)

            cmd.conf_commit_http(url)
            return response(200, data=http_respons)

        if init_data['action'] == 'zone-read':
            tags = dict()
            for i in init_data['data']:
                tags = i['tags']
            respons = cmd.zone_read(tags)
            http_respons = utils.send_http(url, respons)
            return response(200, data=http_respons)

        if init_data['action'] == 'zone-soa-insert':
            result = list()
            for i in init_data['data']:
                tags = i['tags']
            begin_json = cmd.zone_begin(tags)
            begin_respon = utils.send_http(url, begin_json)
            result.append(begin_respon)

            try:
                id_record, respons = cmd.zone_soa_insert_default(tags)
            except Exception as e:
                respons = {"Status": False, "Error": str(e)}
                return response(400, message=respons)
            else:
                http_respons = utils.send_http(url, respons)
                # state change
                if http_respons:
                    state = utils.change_state("id_record", id_record, "1")
                    db.update("zn_record", data=state)

                result.append(http_respons)
                commit_json = cmd.zone_commit(tags)
                commit_response = utils.send_http(url, commit_json)
                result.append(commit_response)
                return response(200, data=result)

        if init_data['action'] == 'zone-begin':
            for i in init_data['data']:
                tags = i['tags']
            respons = cmd.zone_begin(tags)
            http_response = utils.send_http(url, respons)
            return response(200, data=http_response)

        if init_data['action'] == 'zone-commit':
            for i in init_data['data']:
                tags = i['tags']
            respons = cmd.zone_commit(tags)
            http_response = utils.send_http(url, respons)
            return response(200, data=http_response)

        if init_data['action'] == 'zone-insert':
            respons = list()
            for i in init_data['data']:
                tags = i['tags']
            json_begin = cmd.zone_begin_http(url, tags)
            respons.append(json_begin)
            json_command = cmd.zone_insert(tags)
            http_response = utils.send_http(url, json_command)
            # change state
            if http_response:
                state = utils.change_state("id_record", tags['id_record'], "1")
                try:
                    db.update("zn_record", data=state)
                except Exception as e:
                    print(e)
            respons.append(http_response)
            res_commit = cmd.zone_commit_http(url, tags)
            respons.append(res_commit)
            return response(200, data=respons)

        if init_data['action'] == 'zone-ns-insert':
            respons = list()
            for i in init_data['data']:
                tags = i['tags']
            res_begin = cmd.z_begin(url, tags)
            respons.append(res_begin)
            try:
                result = cmd.zone_ns_insert(tags)
            except Exception as e:
                respons = {"Status": False, "Error": str(e)}
                return response(400, message=respons)
            else:
                for i in result:
                    state = None
                    http_response = utils.send_http(url, i['command'])
                    # state change
                    if http_response:
                        state = utils.change_state("id_record", i['id_record'],
                                                   "1")
                        try:
                            db.update("zn_record", data=state)
                        except Exception as e:
                            print(e)
                    respons.append(http_response)

                res_commit = cmd.z_commit(url, tags)
                respons.append(res_commit)
                return response(200, data=respons)

        if init_data['action'] == 'zone-srv-insert':
            result = list()
            for i in init_data['data']:
                tags = i['tags']
            begin_json = cmd.zone_begin_http(url, tags)
            # begin_respon = utils.send_http(url,begin_json)
            result.append(begin_json)

            try:
                respons = cmd.zone_insert_srv(tags)
            except Exception as e:
                respons = {"status": False, "error": str(e)}
                return response(400, data=result, message=respons)
            else:
                http_response = utils.send_http(url, respons)
                if http_response:
                    state = utils.change_state("id_record", tags['id_record'],
                                               "1")
                    try:
                        db.update("zn_record", data=state)
                    except Exception as e:
                        print(e)

                result.append(http_response)
                commit_json = cmd.zone_commit_http(url, tags)
                # commit_response = utils.send_http(url,commit_json)
                result.append(commit_json)
                return response(200, data=result)

        if init_data['action'] == 'zone-mx-insert':
            result = list()
            for i in init_data['data']:
                tags = i['tags']

            begin_json = cmd.zone_begin_http(url, tags)
            # begin_respon = utils.send_http(url,begin_json)
            result.append(begin_json)

            try:
                respons = cmd.zone_insert_mx(tags)

            except Exception as e:
                respons = {"status": False, "error": str(e)}
                return response(400, data=result, message=respons)
            else:
                http_response = utils.send_http(url, respons)
                # change state
                if http_respons:
                    state = utils.change_state("id_record", tags['id_record'],
                                               "1")
                    try:
                        db.update("zn_record", data=state)
                    except Exception as e:
                        print(e)

                result.append(http_response)
                commit_json = cmd.zone_commit_http(url, tags)
                result.append(commit_json)
                return response(200, data=result)

        # delete all zone
        if init_data['action'] == 'conf-unset':
            result = list()
            for i in init_data['data']:
                tags = i['tags']
            data = cmd.conf_unset(tags)
            cmd.conf_begin_http(url)
            http_respons = utils.send_http(url, data)
            cmd.conf_commit_http(url)
            return response(200, data=http_respons)

        if init_data['action'] == 'zone-unset':
            result = list()
            for i in init_data['data']:
                print(i)
                tags = i['tags']
            respons = list()
            res_begin = cmd.zone_begin_http(url, tags)
            respons.append(res_begin)
            json_command = cmd.zone_unset(tags)
            http_response = utils.send_http(url, json_command)
            respons.append(http_response)
            res_commit = cmd.zone_commit_http(url, tags)
            respons.append(res_commit)
            return response(200, data=respons)
Exemplo n.º 9
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('app_name', type=str, required=True)
        parser.add_argument('app_port', type=str, required=True)
        parser.add_argument('username', type=str, required=True)
        parser.add_argument('bless_file',
                            type=werkzeug.datastructures.FileStorage,
                            location='files')
        args = parser.parse_args()

        bless_file = args['bless_file']
        username = args['username']
        app_port = args['app_port']
        app_name = args['app_name']
        c_user = get_jwt_identity()
        project_id = None

        id_userdata = db.get_by_id("tb_user", "username", c_user)
        id_userdata = id_userdata[0]['id_userdata']
        obj_insert = {
            "id_userdata": str(id_userdata),
            "nm_project_app": app_name,
            "nm_project_port": app_port
        }
        try:
            project_id = db.insert(table="tb_project_app", data=obj_insert)
        except Exception as e:
            return response(401, message=str(e))
        if project_id:
            upload_folder = APP_ROOT + BLESS_FOLDER + "/" + project_id + "/"
            if not os.path.exists(upload_folder):
                os.makedirs(upload_folder)
                bless_file.save(upload_folder + "bless.yml")
            else:
                bless_file.save(upload_folder + "bless.yml")

            url_ops = os.getenv("OPENSTACK_URL") + ":" + os.getenv(
                "OPENSTACK_PORT")
            url_create = url_ops + "/api/create"

            headers = {"Access-Token": os.getenv("TOKEN_OPENSTACK")}
            send_to_openstack = {
                "instances": {
                    app_name: {
                        "parameters": {
                            "project_id": project_id,
                            "app_name": app_name,
                            "app_port": app_port,
                            "private_network": "vm-net",
                            "key_name": "vm-key",
                            "username": username
                        },
                        "template": "bless"
                    }
                }
            }
            res_fix = dict()
            data_create = list()
            data_respon = list()
            data_pemkey = ""
            try:
                data_create = utils.send_http(url_create,
                                              data=send_to_openstack,
                                              headers=headers)
                url_vm = url_ops + "/api/list/vm"
                url_pemkey = url_ops + "/api/list/pemkey/" + app_name
                c_limit = True
                while c_limit:
                    data_vm = utils.get_http(url_vm, headers=headers)
                    for i in data_vm['data']:
                        if i['name'] == app_name:
                            res_fix = i
                            c_limit = False
                    data_pemkey = utils.get_http(url_pemkey, headers=headers)
            except Exception as e:
                return response(401, message=str(e))
            else:
                data_respon.append({
                    "create": data_create['data'],
                    "vm": res_fix,
                    "pemkey": data_pemkey
                })
                return response(200, data=data_respon)