forked from incidentbot/incidentbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
224 lines (180 loc) · 6.18 KB
/
Copy pathmain.py
File metadata and controls
224 lines (180 loc) · 6.18 KB
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
import sys
from incidentbot.configuration.settings import settings, __version__
from incidentbot.models.database import (
create_default_admin_user,
db_verify,
engine,
ApplicationData,
)
from incidentbot.scheduler.core import process as TaskScheduler
from incidentbot.logging import logger
from sqlmodel import Session, select
from uvicorn import run
"""
Scheduler
"""
TaskScheduler.start()
"""
Startup
"""
def db_check():
logger.info("Testing the database connection...")
if not db_verify():
logger.fatal(
"Cannot connect to the database - check settings and try again."
)
sys.exit(1)
def startup_message(provider: str, workspace: str, wrap: bool = False) -> str:
"""
Returns diagnostic info for startup or troubleshooting
"""
msg = f"""
--------------------------------------------------------------------------------
incident bot {__version__}
--------------------------------------------------------------------------------
Database host: {settings.POSTGRES_HOST}
Incidents digest channel: {settings.digest_channel}
Logging level: {settings.LOG_LEVEL}
Provider: {provider}
Workspace: {workspace}
Timezone: {settings.options.timezone}
--------------------------------------------------------------------------------
"""
if wrap:
return f"""
```
{msg}
```
"""
else:
return msg
def startup_tasks():
# Database Models
# --------------------
# create_models()
create_default_admin_user()
"""
Tasks that should be run at each startup
"""
logger.info("Running startup tasks...")
match settings.platform:
case "slack":
from incidentbot.scheduler.core import (
update_slack_channel_list,
update_slack_user_list,
)
# Store Slack Channels
# --------------------
update_slack_channel_list()
# Store Slack Users
# --------------------
update_slack_user_list()
# Integration Tests
# --------------------
if (
settings.integrations
and settings.integrations.atlassian
and settings.integrations.atlassian.confluence
and settings.integrations.atlassian.confluence.enabled
):
from incidentbot.confluence.api import ConfluenceApi
api_test = ConfluenceApi()
passes = api_test.test()
if not passes:
logger.fatal(
"Could not verify Confluence parent page exists.\nYou provided: {}/{}".format(
settings.integrations.atlassian.confluence.space,
settings.integrations.atlassian.confluence.parent,
)
)
sys.exit(1)
if (
settings.integrations
and settings.integrations.atlassian
and settings.integrations.atlassian.jira
and settings.integrations.atlassian.jira.enabled
):
from incidentbot.jira.api import JiraApi
api_test = JiraApi()
passes = api_test.test()
if not passes:
logger.fatal(
"Could not verify Jira project exists.\nYou provided: {}".format(
settings.integrations.atlassian.jira.project,
)
)
sys.exit(1)
if (
settings.integrations
and settings.integrations.atlassian
and settings.integrations.atlassian.opsgenie
and settings.integrations.atlassian.opsgenie.enabled
):
from incidentbot.scheduler.core import update_opsgenie_oc_data
update_opsgenie_oc_data()
if (
settings.integrations
and settings.integrations.pagerduty
and settings.integrations.pagerduty.enabled
):
from incidentbot.pagerduty.api import PagerDutyInterface
pagerduty_interface = PagerDutyInterface()
if len(pagerduty_interface.test()) == 0:
logger.fatal(
"PagerDuty test failed: unable to retrieve oncall iterable - either no schedules exist or none were returned",
)
sys.exit(1)
from incidentbot.scheduler.core import update_pagerduty_oc_data
update_pagerduty_oc_data()
try:
with Session(engine) as session:
if not session.exec(
select(ApplicationData).filter(
ApplicationData.name == "auto_page_teams"
)
).first():
auto_page_teams = ApplicationData(
name="auto_page_teams",
json_data={"teams": []},
)
session.add(auto_page_teams)
session.commit()
except Exception as error:
logger.error(f"Error storing auto_page_teams: {error}")
if __name__ == "__main__":
# Database Check
# --------------------
db_check()
# Startup Tests
# --------------------
startup_tasks()
# Startup Message
# --------------------
match settings.platform:
case "slack":
from incidentbot.slack.client import (
slack_workspace_id,
)
print(
startup_message(provider="Slack", workspace=slack_workspace_id)
)
# Always check to make sure the bot user is in the digest channel
# --------------------
match settings.platform:
case "slack":
from incidentbot.slack.client import (
check_bot_user_in_digest_channel,
)
check_bot_user_in_digest_channel()
# Platform Integrations
# --------------------
match settings.platform:
case "slack":
from incidentbot.slack.handler import app as slack_app
from slack_bolt.adapter.socket_mode import SocketModeHandler
handler = SocketModeHandler(slack_app, settings.SLACK_APP_TOKEN)
# API and handler Integration
# --------------------
from incidentbot.api.main import app
handler.connect()
run(app, host="0.0.0.0", port=3000)