-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTeams.py
executable file
·187 lines (167 loc) · 5.75 KB
/
Teams.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
#!/usr/bin/env python3
# Teams.py v1.0b
# Tony Williams 25/07/2019
#
"""See docstring for Teams class"""
import json
import plistlib
import os.path as path
import datetime
import logging
import logging.handlers
import sys
import requests
__all__ = ["Teams"]
# logging requirements
LOGFILE = "/usr/local/var/log/Teams.log"
LOGLEVEL = logging.INFO
class Teams:
"""When given the location of an output plist from Autopkg parses it
and sends the details on packages uploaded to Jamf Pro to Teams
"""
description = __doc__
def __init__(self):
# extremely dumb command line processing
try:
self.plist = sys.argv[1]
except IndexError:
self.plist = "autopkg.plist"
# URL of Teams webhook
self.url = "https://outlook.office.com/webhook/"
# token
self.url += "-e03688a2ab2d/IncomingWebhook/0ac15911fcfa42deb"
self.url += "1d07f0672950542/63a48cfb-c3ef-4ee9-be63-fafbe4177f30"
# URL for a button to open package test policy in Jamf Pro
self.pol_base = "https://suncorp.jamfcloud.com/policies.html?id="
# set up logging
now = datetime.datetime.now().strftime("%d/%m/%Y %H:%M")
frmt = "%(levelname)s {} %(message)s".format(now)
# set up logging
logging.basicConfig(filename=LOGFILE, level=LOGLEVEL, format=frmt)
self.logger = logging.getLogger("")
# set logging formatting
# ch = logging.StreamHandler()
ch = logging.handlers.TimedRotatingFileHandler(
LOGFILE, when="D", interval=1, backupCount=7
)
ch.setFormatter(logging.Formatter(frmt))
self.logger.addHandler(ch)
self.logger.setLevel(LOGLEVEL)
# JSON for the message to Teams
# "sections" will be replaced by our work
self.template = """
{
"@context": "https://schema.org/extensions",
"@type": "MessageCard",
"themeColor": "0072C6",
"title": "Autopkg",
"text": "Packages uploaded",
"sections": [
]
}
"""
# JSON for a section of a message
# we will have a section for each package uploaded
# in this Autopkg run
self.section = """
{
"startGoup": "true", "title": "**AppName**", "text": "version",
"potentialAction": [
{
"@type": "OpenUri",
"name": "Policy",
"targets": [
{
"os": "default",
"uri": "https://docs.microsoft.com/outlook/actionable-messages"
}
]
}
]
}
"""
# JSON template for the error message card.
self.err_template = """
{
"@context": "https://schema.org/extensions",
"@type": "MessageCard",
"themeColor": "0072C6",
"title": "Autopkg",
"text": "Package errors",
"sections": [
]
}
"""
# JSON template for a single error on error card.
self.err_section = """
{
"text": "A long message",
"startGoup": "true",
"title": "**Firefox.pkg**"
}
"""
# JSON template for the empty run message card.
self.none_template = """
{
"@context": "https://schema.org/extensions",
"@type": "MessageCard",
"themeColor": "0072C6",
"title": "Autopkg",
"text": "**Empty Run**"
}
"""
def Teams(self):
"""Do the packages uploaded!"""
self.logger.info("Starting Run")
sections = []
empty = False
jsr = "jpc_importer_summary_result"
try:
fp = open(self.plist, "rb")
pl = plistlib.load(fp)
except IOError:
self.logger.error("Failed to load %s", self.plist)
sys.exit()
item = 0
if jsr not in pl["summary_results"]:
self.logger.debug("No JPCImporter results")
empty = True
else:
for p in pl["summary_results"][jsr]["data_rows"]:
sections.append(json.loads(self.section))
# get the package name without the '.pkg' at the end
pkg_name = path.basename(p["pkg_path"])[:-4]
pol_id = p["policy_id"]
self.logger.debug("Policy: %s Name: %s", pol_id, pkg_name)
(app, version) = pkg_name.split("-")
pol_uri = self.pol_base + pol_id
sections[item]["title"] = "**%s**" % app
sections[item]["text"] = version
sections[item]["potentialAction"][0]["targets"][0][
"uri"
] = pol_uri
item = item + 1
j = json.loads(self.template)
j["sections"] = sections
d = json.dumps(j)
requests.post(self.url, data=d)
# do the error messages
fails = pl["failures"]
if len(fails) == 0: # no failures
if empty: # no failures and no summary so send empty run message
requests.post(self.url, self.none_template)
sys.exit()
sections = []
item = 0
for f in fails:
sections.append(json.loads(self.err_section))
sections[item]["title"] = "**%s**" % f["recipe"]
sections[item]["text"] = f["message"].replace("\n", " ")
item = item + 1
j = json.loads(self.err_template)
j["sections"] = sections
d = json.dumps(j)
requests.post(self.url, d)
if __name__ == "__main__":
Teams = Teams()
Teams.Teams()