-
Notifications
You must be signed in to change notification settings - Fork 4
/
ProdTeams.py
executable file
·167 lines (147 loc) · 4.84 KB
/
ProdTeams.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
#!/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/ProdTeams.log"
LOGLEVEL = logging.DEBUG
class Teams:
"""When given the location of an output plist from Autopkg parses it
and sends the details on packages productionized 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"
# 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=7, backupCount=4
)
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 Productionized",
"sections": [
]
}
"""
# JSON for a section of a message
# we will have a section for each package uploaded
# in this Autopkg run
# (It looks ugly to keep pylint happy)
self.section = """
{"startGoup": "true", "title": "**AppName**", "text": "version"
}
"""
# JSON template for the error message card.
self.err_template = """
{
"@context": "https://schema.org/extensions",
"@type": "MessageCard",
"themeColor": "0072C6",
"title": "Autopkg",
"text": "Production 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 error message card.
self.none_template = """
{
"@context": "https://schema.org/extensions",
"@type": "MessageCard",
"themeColor": "0072C6",
"title": "Production",
"text": "**Empty Run**"
}
"""
def Teams(self):
"""Do the packages uploaded!"""
self.logger.info("Starting Run")
sections = []
empty = False
jsr = "production_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 Production results")
empty = True
else:
pkgs = pl["summary_results"][jsr]["data_rows"]
for p in pkgs:
sections.append(json.loads(self.section))
name = p["package"]
version = p["version"]
self.logger.debug("Version: %s Name: %s", version, name)
sections[item]["title"] = "**%s**" % name
sections[item]["text"] = version
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:
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()