-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodule.py
159 lines (139 loc) · 5.94 KB
/
module.py
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
# Copyright 2021 getcarrier.io
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Module """
from pylon.core.tools import log # pylint: disable=E0611,E0401
from pylon.core.tools import module # pylint: disable=E0611,E0401
import json
from .models.tasks import Task
from .tools.TaskManager import TaskManager, PostProcessingManager
from tools import theme, VaultClient, api_tools
class Module(module.ModuleModel):
""" Task module """
def __init__(self, context, descriptor):
self.context = context
self.descriptor = descriptor
def init(self):
""" Init module """
log.info("Initializing module Tasks")
from .init_db import init_db
init_db()
self.descriptor.init_api()
self.descriptor.init_blueprint()
self.descriptor.init_rpcs()
self.descriptor.init_events()
theme.register_subsection(
"configuration", "tasks",
"Tasks",
title="Tasks",
kind="slot",
permissions={
"permissions": ["configuration.tasks"],
"recommended_roles": {
"administration": {"admin": True, "viewer": True, "editor": True},
"default": {"admin": True, "viewer": True, "editor": True},
"developer": {"admin": True, "viewer": True, "editor": True},
}},
prefix="tasks_",
weight=5,
)
theme.register_mode_subsection(
"administration", "configuration",
"tasks", "Tasks",
title="Tasks",
kind="slot",
permissions={
"permissions": ["configuration.tasks"],
"recommended_roles": {
"administration": {"admin": True, "viewer": True, "editor": True},
"default": {"admin": True, "viewer": True, "editor": True},
"developer": {"admin": True, "viewer": True, "editor": True},
}},
prefix="administration_tasks_",
# icon_class="fas fa-server fa-fw",
# weight=2,
)
self.descriptor.init_slots()
self.descriptor.register_tool('TaskManager', TaskManager)
self.descriptor.register_tool('PostProcessingManager', PostProcessingManager)
vault_client = VaultClient()
secrets = vault_client.get_all_secrets()
if 'control_tower_id' not in secrets:
cc = self.create_control_tower_task()
secrets['control_tower_id'] = cc.task_id
if 'rabbit_queue_checker_id' not in secrets:
rqc = self.create_rabbit_queue_checker_task()
secrets['rabbit_queue_checker_id'] = rqc.task_id
vault_client.set_secrets(secrets)
def create_control_tower_task(self) -> Task:
cc_args = {
"funcname": "control_tower",
"invoke_func": "lambda.handler",
"runtime": "Python 3.8",
"region": "default",
"env_vars": json.dumps({
"token": "{{secret.auth_token}}",
"galloper_url": "{{secret.galloper_url}}",
"GALLOPER_WEB_HOOK": '{{secret.post_processor}}',
"project_id": '{{secret.project_id}}',
"loki_host": '{{secret.loki_host}}'
}),
's3_settings': {'integration_id': 1, 'is_local': False}
}
task_manager = TaskManager(mode='administration')
return task_manager.create_task(
self.descriptor.config['control_tower_task_path'],
cc_args
)
def create_rabbit_queue_checker_task(self) -> Task:
rabbit_queue_checker_args = {
"funcname": "rabbit_queue_checker",
"invoke_func": "lambda.handler",
"runtime": "Python 3.8",
"region": "default",
"env_vars": json.dumps({
"token": '{{secret.auth_token}}',
# "callback_url": ''.join([
# '{{secret.galloper_url}}',
# api_tools.build_api_url('projects', 'rabbitmq', mode=mode)
# ]),
"rabbit_host": '{{secret.rabbit_host}}',
"rabbit_user": '{{secret.rabbit_user}}',
"rabbit_password": '{{secret.rabbit_password}}',
"AWS_LAMBDA_FUNCTION_TIMEOUT": 120,
'vhost_template': 'project_{project_id}_vhost',
'core_vhost': 'carrier',
'put_url': ''.join([
'{{secret.galloper_url}}',
api_tools.build_api_url('projects', 'rabbitmq',
mode='administration', trailing_slash=True),
'None'
]),
'project_ids_get_url': ''.join([
'{{secret.galloper_url}}',
api_tools.build_api_url('projects', 'rabbitmq',
mode='administration', trailing_slash=True),
'None'
]),
}),
's3_settings': {'integration_id': 1, 'is_local': False}
}
task_manager = TaskManager(mode='administration')
return task_manager.create_task(
self.descriptor.config['rabbit_queue_checker_task_path'],
rabbit_queue_checker_args,
'rabbit_queue_checker.zip'
)
def deinit(self): # pylint: disable=R0201
""" De-init module """
log.info("De-initializing module Tasks")