aboutsummaryrefslogtreecommitdiffstats
path: root/moon_engine/moon_engine/plugins/pyorchestrator.py
blob: bf2d70f9b471213401dcbdd677501f64aede7318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# Software Name: MOON

# Version: 5.4

# SPDX-FileCopyrightText: Copyright (c) 2018-2020 Orange and its contributors
# SPDX-License-Identifier: Apache-2.0

# This software is distributed under the 'Apache License 2.0',
# the text of which is available at 'http://www.apache.org/licenses/LICENSE-2.0.txt'
# or see the "LICENSE" file for more details.


import logging
import os
import time
import requests
import subprocess  # nosec
from uuid import uuid4
import yaml
from moon_engine.orchestration_driver import PipelineDriver
from moon_engine.api import configuration
from moon_engine.api.configuration import get_configuration
from moon_engine import get_api_key
from moon_utilities.auth_functions import xor_decode
from moon_utilities import exceptions
from datetime import datetime

LOGGER = logging.getLogger("moon.engine.orchestrator.driver.pyorchestrator")

PLUGIN_TYPE = "orchestration"
pipelines = {}
ports = []


def init():
    """Initialize the plugin by initializing wrappers

    :return: nothing
    """

    # FIXME: get pipelines from Manager
    pass


def create_gunicorn_config(host, port, server_type, uuid):
    """Create a Gunicorn config file in a temporary directory

    :return: filename
    """
    config_dir = get_configuration("orchestration").get("config_dir", "/tmp")  # nosec
    # (/tmp is a fallback solution)
    _log_config = get_configuration("logging")
    _log_config["handlers"]["file"]["filename"] = os.path.join(config_dir,
                                                               "moon_{}.log".format(uuid))
    __manager_url = get_configuration("management")["url"]
    filename = os.path.join(config_dir, "gunicorn_{}.cfg".format(uuid4().hex))
    fd = open(filename, "w")
    fd.write("""bind = "{host}:{port}"
workers = {workers}
moon = "{moon_filename}"
    """.format(
        host=host,
        port=port,
        workers=1,
        moon_filename=os.path.join(config_dir, "moon_{}.yaml".format(uuid)),
    ))
    fd.close()
    return filename


def create_moon_config(uuid, manager_cnx=True, policy_file=None):
    """Create a Moon config file in a temporary directory

    :return: filename
    """
    LOGGER.info(f"create_moon_config({uuid})")
    config_dir = get_configuration("orchestration").get("config_dir", "/tmp")  # nosec
    _log_config = get_configuration("logging")
    _log_config["handlers"]["file"]["filename"] = os.path.join(config_dir,
                                                               "moon_{}.log".format(uuid))
    if manager_cnx:
        __manager_url = get_configuration("management")["url"]
        api_token = get_api_key(get_configuration("management")["url"],
                                get_configuration("management")["user"],
                                get_configuration("management")["password"])
    else:
        __manager_url = ""
        api_token = ""
    config_dir = get_configuration("orchestration").get("config_dir", "/tmp")  # nosec
    # (/tmp is a fallback solution)
    filename = os.path.join(config_dir, "moon_{}.yaml".format(uuid))
    config_dict = {
        "type": "pipeline",
        "uuid": uuid,
        "management": {
            "url": __manager_url,
            "token_file": os.path.join(config_dir, "db_{}.json".format(uuid))
        },
        "incremental_updates": True,
        "api_token": api_token,
        "data": "",
        "logging": _log_config,
        "authorization": get_configuration("authorization",
                                           {"driver": "moon_engine.plugins.authz"}),
        "plugins": get_configuration("plugins"),
        "debug": get_configuration(key='debug', default=False)
    }
    if policy_file:
        config_dict['data'] = policy_file
    if not manager_cnx:
        config_dict['uuid'] = ""
        config_dict['incremental_updates'] = False
    LOGGER.info("Writing config file to {}".format(filename))
    yaml.dump(config_dict, open(filename, "w"), default_flow_style=False)
    return filename


def kill_server(uuid):
    """Kill the server given its UUID

    :param uuid: UUID of the server
    :return: nothing
    """
    LOGGER.info("pipelines={}".format(pipelines))
    if uuid in pipelines:
        LOGGER.info("pipeline={}".format(pipelines[uuid]))
        # Fixme: if the server has been restarted, the process attribute is empty
        LOGGER.info("Killing server {} after {} of uptime".format(
            uuid,
            str(datetime.now() - datetime.fromtimestamp(pipelines[uuid]["starttime"]))
        ))
        with open(pipelines[uuid]["process"], 'r') as pid_file:
            try:
                pid = int(pid_file.read())
            except ValueError:
                LOGGER.error("The pid found in {} is not valid".format(pipelines[uuid]["process"]))
                return

        os.kill(pid, 15)
        del_server_port(pipelines[uuid]["port"])
        pipelines.pop(uuid)
    else:
        LOGGER.warning("Cannot find UUID {} in wrappers or interfaces".format(uuid))


def get_ports_range():
    ports_range = get_configuration("orchestration")["port"]
    return int(ports_range.split(".")[0]), int(ports_range.split(".")[-1])


def get_next_port(server_host="127.0.0.1"):
    port_min, port_max = get_ports_range()
    _port = port_min
    _ports = []
    for _pipeline in pipelines:
        _ports.append(pipelines[_pipeline]["port"])
    _ports.sort()
    if not _ports:
        _port = port_min
    elif _ports[-1]+1 > port_max:
        raise Exception(
            "Cannot add a new slave because "
            "the port range is bounded to {}".format(port_max))
    while True:
        if _port in _ports:
            _port += 1
            continue
        try:
            requests.get("http://{}:{}/status".format(server_host, _port), timeout=1)
        except requests.exceptions.ConnectionError:
            break
        if _port > port_max:
            raise Exception(
                "Cannot add a new pipeline because "
                "the port range is bounded to {}".format(port_max))
        _port += 1
    return _port


def add_server_port(port):
    ports.append(port)


def del_server_port(port):
    try:
        ports.remove(port)
    except ValueError:
        LOGGER.warning("port {} is not in the known port".format(port))


def get_server_url(uuid=None):
    if not uuid:
        return
    url = ""
    try:
        if uuid in pipelines:
            url = "http://{}:{}".format(pipelines[uuid]["server_ip"],
                                        pipelines[uuid]["port"])
        if url:
            response = requests.get(url + "/status")
            if response.status_code == 200:
                return url
    except TimeoutError:
        LOGGER.warning("A timeout occurred when connecting to {}".format(url))
    # if port has not be found in local data, try to get information from remote servers
    port_min, port_max = get_ports_range()
    host = "127.0.0.1"
    for _port in range(port_min, port_max):
        try:
            req = requests.get("http://{}:{}/status".format(host, _port), timeout=1)
            data = req.json()
            if "status" in data and data["status"]["uuid"] == uuid:
                return "http://{}:{}".format(host, _port)
        except Exception as e:
            LOGGER.warning("Error getting information from {} ({})".format(host, str(e)))
            return


def start_new_server(uuid):
    """Start a new server in a new process

    :param uuid: UUID of the server
    :return: nothing
    """
    _url = get_server_url(uuid)
    config_dir = get_configuration("orchestration").get("config_dir", "/tmp")  # nosec
    server_ip = "127.0.0.1"
    config_filename = os.path.join(config_dir, "moon_{}.yaml".format(uuid))
    LOGGER.info("Starting server {} {}".format(_url, uuid))
    if _url:
        _port = int(_url.split(":")[-1])
        add_server_port(_port)
        config = yaml.safe_load(open(config_filename))
        log_file = config["logging"]["handlers"]["file"]["filename"]
        _out = {
            "pipeline_id": uuid,
            "starttime": time.time(),
            "port": _port,
            "host": server_ip,
            "server_ip": server_ip,
            "log_file": log_file
        }
    else:
        _port = get_next_port()
        create_moon_config(uuid=uuid)
        pid_file = os.path.join(config_dir, uuid + ".pid")
        # NOTE: we have actually no solution to get the actual IP address
        # so we need to put 0.0.0.0 in the host address
        gunicorn_config = create_gunicorn_config(
            host="0.0.0.0",  # nosec
            port=_port,
            server_type="pipeline",
            uuid=uuid)
        command = ["gunicorn", "moon_engine.server:__hug_wsgi__", "--threads", "10",
                   "-p", pid_file, "-D", "-c", gunicorn_config]
        LOGGER.info("Executing {}".format(" ".join(command)))
        subprocess.Popen(command, stdout=subprocess.PIPE, close_fds=True)  # nosec
        # (command attribute is safe)
        _out = {
            "pipeline_id": uuid,
            "starttime": time.time(),
            "port": _port,
            "host": server_ip,
            "server_ip": server_ip,
            "process": pid_file,
        }
        time.sleep(1)
        config = yaml.safe_load(open(config_filename))
        log_file = config["logging"]["handlers"]["file"]["filename"]
        _out["log"] = log_file
        for cpt in range(10):
            try:
                f_sock = open(log_file)
            except FileNotFoundError:
                time.sleep(1)
            else:
                break
        else:
            LOGGER.error("Cannot find log file ({})".format(log_file))
            return
        p_sock = 0
        LOGGER.info("Process running")
        while True:
            f_sock.seek(p_sock)
            latest_data = f_sock.read()
            p_sock = f_sock.tell()
            if latest_data and "APIKEY" in latest_data:
                _index_start = latest_data.index("APIKEY=") + len("APIKEY=")
                _index_stop = latest_data.index("\n", _index_start)
                key = latest_data[_index_start:_index_stop].strip()
                # api_key = get_api_key_for_user("admin")
                api_key = configuration.get_configuration('api_token')
                try:
                    engine_api_key = xor_decode(key, api_key)
                except exceptions.DecryptError:
                    engine_api_key = False
                _out["api_key"] = engine_api_key
                break
            time.sleep(1)

    return _out


class PipelineConnector(PipelineDriver):

    def __init__(self, driver_name, engine_name):
        self.driver_name = driver_name
        self.engine_name = engine_name

    def update_pipeline(self, pipeline_id, data):
        _url = get_server_url(pipeline_id)
        if not _url:
            self.add_pipeline(pipeline_id, data)
        if "security_pipeline" in data:
            req = requests.post("{}/update".format(_url), json={"attributes": "pdp"})
            if req.status_code == 206:
                LOGGER.warning("No pipeline available...")
            elif req.status_code != 202:
                LOGGER.warning("Error sending upgrade command to pipeline ({})".format(req))
        if "vim_project_id" in data and data['vim_project_id']:
            LOGGER.warning("Cannot update vim_project_id for the moment")
            # FIXME: manage vim_project_id

    def delete_pipeline(self, pipeline_id):
        LOGGER.info("Deleting pipeline {}".format(pipeline_id))
        kill_server(pipeline_id)

    def add_pipeline(self, pipeline_id=None, data=None):
        LOGGER.debug("Adding POD in Engine {} {}".format(pipeline_id, data))
        if not pipeline_id:
            pipeline_id = uuid4().hex
        if not data:
            content = dict()
        else:
            content = dict(data)
        content.update(start_new_server(pipeline_id))
        pipelines[pipeline_id] = content
        return pipelines[pipeline_id]

    def get_pipelines(self, pipeline_id=None):
        results = {}
        for interface in pipelines:
            results[interface] = {
                "starttime": pipelines[interface]["starttime"],
                "port": pipelines[interface]["port"],
                "server_ip": pipelines[interface]["server_ip"],
                "status": "down",
                "log": pipelines[interface]["log"]
            }
            try:
                req = requests.get("http://{}:{}/status".format(
                    pipelines[interface]["server_ip"],
                    pipelines[interface]["port"]
                ))
                if req.status_code == 200:
                    results[interface]["status"] = "up"
            except TimeoutError:
                LOGGER.warning("Timeout connecting {} on port {}".format(
                    pipelines[interface]["server_ip"],
                    pipelines[interface]["port"]
                ))
        return results

    def get_pipeline_api_key(self, pipeline_id):
        return pipelines.get(pipeline_id, {}).get('api_key', "")


class Connector(PipelineConnector):
    pass


init()