-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
241 lines (197 loc) · 11.3 KB
/
__init__.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
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
from opsdroid.skill import Skill
from opsdroid.matchers import match_regex
import aiohttp
import datetime
class AWXSkill(Skill):
async def _rest_call(self, deployment, api_url, call_method):
auth = aiohttp.BasicAuth(
login=self.config["sites"][deployment]["username"],
password=self.config["sites"][deployment]["password"],
)
timeout = aiohttp.ClientTimeout(total=60)
async with aiohttp.ClientSession(auth=auth, timeout=timeout) as session:
if call_method == "get":
async with session.get(api_url) as resp:
data = await resp.json()
return data
else:
async with session.post(api_url) as resp:
data = await resp.json()
return data
async def _get_inventories(self, deployment):
api_url = f"{self.config['sites'][deployment]['url']}/api/v2/inventories/"
data = await self._rest_call(deployment, api_url, "get")
return_text = f"*{deployment} - Inventories*\n"
for i in data["results"]:
return_text = f"{return_text}```ID: {i['id']} Name: {i['name']}```\n"
return return_text
async def _update_inventory(self, deployment, inventory):
api_url = f"{self.config['sites'][deployment]['url']}/api/v2/inventories/{inventory}/update_inventory_sources/"
data = await self._rest_call(deployment, api_url, "post")
return_text = f"*{deployment} - Inventory Update* \n"
result = data[0]
return_text = f"{return_text}```State: {result['status']}```"
return return_text
async def _update_project(self, deployment, id):
api_url = (
f"{self.config['sites'][deployment]['url']}/api/v2/projects/{id}/update/"
)
return_text = f"*{deployment} - Project Update* \n"
result = await self._rest_call(deployment, api_url, "post")
return_text = f"{return_text}```State: {result['status']}```"
return return_text
async def _get_projects(self, deployment):
api_url = f"{self.config['sites'][deployment]['url']}/api/v2/projects/"
data = await self._rest_call(deployment, api_url, "get")
if data["count"] > 0:
return_text = f"*{deployment} - Projects*\n"
for i in data["results"]:
return_text = f"{return_text}```ID: {i['id']}\nName: {i['name']}\nscm_url: {i['scm_url']}\nscm_branch: {i['scm_branch']}```\n"
else:
return_text = f"*{deployment} - No Projects*"
return return_text
async def _get_running_jobs(self, deployment):
api_url = (
f"{self.config['sites'][deployment]['url']}/api/v2/jobs/?status=running"
)
data = await self._rest_call(deployment, api_url, "get")
if data["count"] > 0:
return_text = f"*{deployment} - Running Jobs*\n"
for i in data["results"]:
return_text = f"{return_text}```Date: {i['started']} ID: {i['id']} Name: {i['name']} Playbook: {i['playbook']}```\n"
else:
return_text = f"*{deployment} - No Running Jobs*"
return return_text
async def _get_failed_jobs(self, deployment, num=5, yesterday=None):
if yesterday:
yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
start_time = yesterday.isoformat()
api_url = f"{self.config['sites'][deployment]['url']}/api/v2/jobs/?status=failed&order_by=-started&started__gt={start_time}&page_size=100"
else:
api_url = f"{self.config['sites'][deployment]['url']}/api/v2/jobs/?status=failed&order_by=-started&page_size={num}"
data = await self._rest_call(deployment, api_url, "get")
if data["count"] > 0:
return_text = f"*{deployment} - Failed Jobs*\n"
for i in data["results"]:
return_text = f"{return_text}```Date: {i['started']} ID: {i['id']} Name: {i['name']} Playbook: {i['playbook']}```\n"
else:
return_text = f"*{deployment} - No Failed Jobs*"
return return_text
async def _get_scheduled_jobs(self, deployment, num=5):
api_url = f"{self.config['sites'][deployment]['url']}/api/v2/schedules/?enabled=true&order_by=next_run&page_size={num}"
data = await self._rest_call(deployment, api_url, "get")
if data["count"] > 0:
return_text = f"*{deployment} - Next {num} Scheduled Jobs*\n"
for i in data["results"]:
return_text = f"{return_text}```Next Run: {i['next_run']} ID: {i['id']} Name: {i['name']}```\n"
else:
return_text = f"*{deployment} - No Scheduled Jobs*"
return return_text
async def _get_scheduled_jobs_past(self, deployment):
today = datetime.datetime.now()
next_run = today.isoformat()
api_url = f"{self.config['sites'][deployment]['url']}/api/v2/schedules/?enabled=true&order_by=next_run&next_run__lt={next_run}&page_size=20"
data = await self._rest_call(deployment, api_url, "get")
if data["count"] > 0:
return_text = f"*{deployment} - Past Scheduled Jobs*\n"
for i in data["results"]:
return_text = f"{return_text}```Next Run: {i['next_run']} ID: {i['id']} Name: {i['name']}```\n"
else:
return_text = f"*{deployment} - No Scheduled Jobs*"
return return_text
async def _get_deployments(self):
sites = self.config["sites"]
return_text = f"*AWX Deployments*\n"
for site in sites:
return_text = f"{return_text}```Deployment: {site} URL: {self.config['sites'][site]['url']}```\n"
return return_text
async def _get_help(self):
return_text = f"*Help*\n"
return_text = f"{return_text}```awx help - returns this help screen```\n"
return_text = f"{return_text}```awx list deployments - Returns Deployment keywords and urls```\n"
return_text = f"{return_text}```awx <deployment> list inventory - Returns name and id for all inventories in specific deployment```\n"
return_text = f"{return_text}```awx <deployment> update inventory <id> - Updates inventory sources for inventory in specific deployment```\n"
return_text = f"{return_text}```awx <deployment> list running jobs - Returns information about running jobs for specific deployment```\n"
return_text = f"{return_text}```awx <deployment> list failed jobs - Returns information about last 5 failed jobs for specific deployment```\n"
return_text = f"{return_text}```awx <deployment> list failed jobs yesterday - Returns information about last 24 hours of failed jobs for specific deployment```\n"
return_text = f"{return_text}```awx <deployment> list failed jobs num:<#> - Returns information about last # failed jobs for specific deployment```\n"
return_text = f"{return_text}```awx <deployment> list scheduled jobs - Returns information about next 5 scheduled jobs for specific deployment```\n"
return_text = f"{return_text}```awx <deployment> list scheduled jobs num:<#> - Returns information about next # scheduled jobs for specific deployment```\n"
return_text = f"{return_text}```awx <deployment> list scheduled jobs past - Returns information about scheduled jobs with next_run in the past```\n"
return_text = f"{return_text}```awx <deployment> list projects - Returns information about project for specific deployment```\n"
return_text = f"{return_text}```awx <deployment> update project <id> - Updated a project by id```\n"
return return_text
# being Matching Functions
@match_regex(r"^awx (?P<deployment>\w+-\w+|\w+) list inventory$")
async def list_inventory(self, message):
deployment = message.regex.group("deployment")
inventories = await self._get_inventories(deployment)
await message.respond(f"{inventories}")
@match_regex(
r"^awx (?P<deployment>\w+-\w+|\w+) update inventory (?P<inventory>\d+)$"
)
async def update_inventory(self, message):
deployment = message.regex.group("deployment")
inventory = message.regex.group("inventory")
update = await self._update_inventory(deployment, inventory)
await message.respond(f"{update}")
@match_regex(r"^awx (?P<deployment>\w+-\w+|\w+) list running jobs$")
async def list_running_jobs(self, message):
deployment = message.regex.group("deployment")
inventories = await self._get_running_jobs(deployment)
await message.respond(f"{inventories}")
@match_regex(r"^awx (?P<deployment>\w+-\w+|\w+) list failed jobs$")
async def list_failed_jobs(self, message):
deployment = message.regex.group("deployment")
inventories = await self._get_failed_jobs(deployment)
await message.respond(f"{inventories}")
@match_regex(r"^awx (?P<deployment>\w+-\w+|\w+) list failed jobs yesterday$")
async def list_failed_jobs_yesterday(self, message):
deployment = message.regex.group("deployment")
inventories = await self._get_failed_jobs(deployment, yesterday=True)
await message.respond(f"{inventories}")
@match_regex(
r"^awx (?P<deployment>\w+-\w+|\w+) list failed jobs num: (?P<num>\d+)$"
)
async def list_failed_jobs_num(self, message):
deployment = message.regex.group("deployment")
num = message.regex.group("num")
inventories = await self._get_failed_jobs(deployment, num)
await message.respond(f"{inventories}")
@match_regex(r"^awx (?P<deployment>\w+-\w+|\w+) list scheduled jobs$")
async def list_scheduled_jobs(self, message):
deployment = message.regex.group("deployment")
inventories = await self._get_scheduled_jobs(deployment)
await message.respond(f"{inventories}")
@match_regex(
r"^awx (?P<deployment>\w+-\w+|\w+) list scheduled jobs num: (?P<num>\d+)$"
)
async def list_scheduled_jobs_num(self, message):
deployment = message.regex.group("deployment")
num = message.regex.group("num")
inventories = await self._get_scheduled_jobs(deployment, num)
await message.respond(f"{inventories}")
@match_regex(r"^awx (?P<deployment>\w+-\w+|\w+) list scheduled jobs past$")
async def list_scheduled_jobs_past(self, message):
deployment = message.regex.group("deployment")
inventories = await self._get_scheduled_jobs_past(deployment)
await message.respond(f"{inventories}")
@match_regex(r"^awx list deployments$")
async def list_deployments(self, message):
inventories = await self._get_deployments()
await message.respond(f"{inventories}")
@match_regex(r"^awx help$")
async def list_help(self, message):
inventories = await self._get_help()
await message.respond(f"{inventories}")
@match_regex(r"^awx (?P<deployment>\w+-\w+|\w+) list projects$")
async def list_projects(self, message):
deployment = message.regex.group("deployment")
inventories = await self._get_projects(deployment)
await message.respond(f"{inventories}")
@match_regex(r"^awx (?P<deployment>\w+-\w+|\w+) update project (?P<id>\d+)$")
async def update_project(self, message):
deployment = message.regex.group("deployment")
id = message.regex.group("id")
update = await self._update_project(deployment, id)
await message.respond(f"{update}")