-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoupdatepatchserver_template.sh
147 lines (117 loc) · 4.73 KB
/
autoupdatepatchserver_template.sh
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
#!/bin/bash
# Shell script to update a patchserver based on what new packages are
# added to Jamf.
# Copyright (C) 2019 Dan Engh
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# variables
######################
# current user
OldIFS="$IFS"
IFS=$'\n'
currUser=$( ls -l /dev/console | awk '{print $3}' )
# slack server
slackServer="https://hooks.slack.com/services/THXXXXXXX/BLXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX"
# time variables
timestamp=$(date "+%Y-%m-%dT%H:%M:%SZ")
tdate=$(date "+%Y-%m-%d")
ydate=$(date -v -1d "+%Y-%m-%d")
# file variables
computerName=$(scutil --get ComputerName)
basePath="/Users/"$currUser"/Library/AutoPkg/Logs"
mkdir -p "$basePath"
currPackageList=""$basePath"/$tdate-Package-List.txt"
arcPackageList=""$basePath"/$ydate-Package-List.txt"
diffPackageList=""$basePath"/New-Package-List.txt"
packageLog=""$basePath"/newPackage.log"
# notifications
SlackNotification() {
curl ${slackServer} --silent --data-urlencode "${payload}"
}
# create files if they don't exist
if [[ ! -e "$currPackageList" ]]; then
touch "$currPackageList"
fi
if [[ ! -e "$arcPackageList" ]]; then
touch "$arcPackageList"
fi
if [[ ! -e "$diffPackageList" ]]; then
touch "$diffPackageList"
fi
if [[ ! -e "$packageLog" ]]; then
touch "$packageLog"
fi
# pull any new json files from github repo
pushd /path/to/git/repo/
git pull
popd
# query the jss for package list and start logging
jss_helper package > "$currPackageList"
echo $(date) >> "$packageLog"
echo "********************************************" >> "$packageLog"
payload="payload={\"text\": \"***************"$tdate" "$computerName"***************\"}"
SlackNotification
# find differences in packages from the previous day to find new packages
diff -yB --suppress-common-lines "$currPackageList" "$arcPackageList" > "$diffPackageList"
# read differences, determine title and version and add them into an array
newPackages=($(awk -F ': ' '{print $3}' "$diffPackageList"))
# split name and version into new arrays for title and version
iter=0
softTitle=()
softVer=()
for item in "${newPackages[@]}"; do
softTitle[$iter]=$( cut -d'-' -f1 <<< "$item" )
echo "${softTitle[$iter]}"
softVer[$iter]=$( cut -d'-' -f2 <<< "$item" )
echo "${softVer[$iter]}"
iter=$(( iter + 1 ))
done
# bring iterator back down to last added index
iter=$(( iter - 1 ))
# iterate through stored values in both arrays to mark up json files with new date and version
while [[ $iter -ge 0 ]]; do
# put software title into a useable state for substitution
updateJson="${softTitle[$iter]}"
verJson="${softVer[$iter]}"
echo "$updateJson"" ""$verJson" >> $packageLog
# recursively substitute %20 in place of spaces to find correct json file
# %20 is needed in place of spaces on the patch server for ID. Also removes the space if the
# %20 named file doesn't exist
while [[ "$revJson" =~ " " ]]; do
revJson=${revJson/" "/%20}
done
if [[ -z $(find /Users/"$currUser"/Documents/GitHub/JAMF_Patch_Server/ -iname "$revJson"*update*) ]];then
revJson=${revJson/"%20"/""}
fi
# find json file and old date, version and replace them with new date and version
softJson=$(find /path/to/git/repo/ -iname "$updateJson"*update*) > /dev/null
echo "$softJson"
if [[ "$softJson" != "" ]]; then
payload="payload={\"text\": \""$tdate" : *"${softTitle[$iter]}"* *"$verJson"* has been updated on the patch server\"}"
oldVer=$( cat "$softJson" | grep -m 1 version | awk '{print $2}')
oldDate=$( cat "$softJson" | grep -m 1 releaseDate | awk '{print $2}')
sed -i '' -e "s|$oldVer|\"${softVer[$iter]}\",|g" "$softJson"
sed -i '' -e "s|$oldDate|\"$timestamp\",|g" "$softJson"
curl --http1.1 -H "Content-Type: application/json" http://your.patchserver.com:5000/api/v1/title/"$revJson"/version -T "$softJson" -X POST
SlackNotification
echo "${softTitle[$iter]}"" has been updated" >> $packageLog
else
payload="payload={\"text\": \""$tdate" : *"${softTitle[$iter]}"* *"$verJson"* has updated. Please update patch definitions manually\"}"
SlackNotification
echo "${softTitle[$iter]}"" does not have a patch definition" >> $packageLog
fi
iter=$(( iter - 1 ))
done
# commit changes to update json files back to github
pushd /path/to/git/repo/
git commit -a -m "daily update"
git push
popd