-
Notifications
You must be signed in to change notification settings - Fork 8
/
miac.sh
622 lines (463 loc) · 19.2 KB
/
miac.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#!/bin/bash
## This script is designed to setup a Munki repository on a local machine for sync to the cloud.
## Version: 1.0
## This script carries NO warranty or guarantee. If it breaks, you own both pieces.
## Goals:
## This script should do the following:
# 1. Install git if needed.
# 2. Install the Python pip installer tool if needed.
# 3. Install the PyOpen SSL module if needed.
# 4. Install AutoPkg if needed.
# 5. Install Munki if needed.
# 6. Install the awscli tool if needed.
# 7. Set up a Munki repo and set the logged-in user as the owner.
# 8. Add specified AutoPkg repos.
# 9. Run specified AutoPkg recipes to populate the Munki repo.
# 10. Install AutoPkgr.
# 11. Install Munki Admin.
# 12. Configure AutoPkgr's recipe list.
# 13. Set up default manifest using the packages added to the Munki repo.
# 14. Set up new bucket in AWS's S3 service.
# 15. Synchronize Munki repo with S3 bucket.
## Declare some useful variables:
# Variables you'll need to configure the locally-stored Munki repo.
# It should be OK to use the default values below if
# you're setting up Munki for the first time on macOS.
REPOLOC="/Users/Shared"
REPONAME="munki_repo"
TEXTEDITOR="BBEdit.app"
# Variables you'll need to edit to create the S3 bucket.
# What these variables correspond to for your AWS programmatic user
# AWSSECRETKEY = Access Key
# AWSSECRETPASSWORD = Secret Access Key
AWSSECRETKEY="YOUGOTTAFIXTHIS"
AWSSECRETPASSWORD="YOUGOTTAFIXTHIS"
# AWS region according to https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html
# all lower case
# example:
# AWSREGIONID="us-east-1"
AWSREGIONID="YOUGOTTAFIXTHIS"
AWSOUTPUT="json" # Default output format can be either json, text, or table. json is a safe default to use.
# If desired, set a name for the S3 bucket. Bucket names must be unique and can contain lowercase letters, numbers, and hyphens.
# If you don't set a name here, the script will assign the bucket a name similar to the one shown below:
#
# 68528a32-1c3f-49f9-aec7-3bd57efe6579-miac
YOURBUCKETNAME=""
# Variables you'll need to set for AutoPkg.
## AutoPkg repos:
#
# Enter the list of AutoPkg repos which need to be set up.
#
# All listed recipe repos should go between the two ENDMSG lines.
# The list should look similar to the one shown below:
#
# read -r -d '' AUTOPKG_REPOS <<ENDMSG
# recipes
# rtrouton-recipes
# jleggat-recipes
# timsutton-recipes
# nmcspadden-recipes
# jessepeterson-recipes
# ENDMSG
#
# It should be OK to use the default values below if
# you're setting up Munki for the first time on macOS.
read -r -d '' AUTOPKG_REPOS <<ENDMSG
recipes
rtrouton-recipes
jleggat-recipes
timsutton-recipes
nmcspadden-recipes
jessepeterson-recipes
ENDMSG
# Set the list of AutoPkg recipes you want to run.
# It should be OK to use the default values below if
# you're setting up Munki for the first time on macOS.
AUTOPKGRUN="AdobeFlashPlayer.munki Dropbox.munki Firefox.munki GoogleChrome.munki BBEdit.munki munkitools3.munki MakeCatalogs.munki"
# The variables below this line don't need to be changed.
# They define paths used by this script.
AUTOPKG_LOCATION="/usr/local/bin/autopkg"
PIP_LOCATION="/usr/local/bin/pip"
USERHOME="$HOME"
REPODIR="${REPOLOC}/${REPONAME}"
LOGGER="/usr/bin/logger -t Munki-in-a-Cloud"
MUNKILOC="/usr/local/munki"
MUNKIMAKECATALOGS="/usr/local/munki/makecatalogs"
MANU="/usr/local/munki/manifestutil"
AUTOPKGARRAY=($AUTOPKGRUN)
DEFAULTS="/usr/bin/defaults"
AUTOPKG="/usr/local/bin/autopkg"
ADMINUSERNAME=$(id -nu)
AWS="/usr/local/bin/aws"
GENERICUUID=$(uuidgen | tr '[A-Z]' '[a-z]') # UUID converted to use lower-case letters in place of upper-case.
if [[ -n "$YOURBUCKETNAME" ]]; then
BUCKET="$YOURBUCKETNAME"
else
BUCKET="$GENERICUUID-miac"
fi
## Functions used in the script
rootCheck() {
# Check that the script is NOT running as root
if [[ $EUID -eq 0 ]]; then
echo "### AutoPkg's user-level processes should not be run as root,"
echo "### so this script is NOT MEANT to run with root privileges."
echo ""
echo "### When needed, it will prompt for an admin account's password."
echo "### This will allow sudo to run specific functions using root privileges."
echo ""
echo "### Script will now exit. Please try running it again without root privileges."
echo ""
exit 4 # Running as root.
fi
}
adminCheck() {
# Check that the script is being run by an account with admin rights
if [[ -z $(id -nG | grep -ow admin) ]]; then
echo "### This script may need to use sudo to run specific functions"
echo "### using root privileges. The $(id -nu) account does not have"
echo "### administrator rights associated with it, so it will not be"
echo "### able to use sudo."
echo ""
echo "### Script will now exit."
echo "### Please try running this script again using an admin account."
echo ""
exit 5 # Running as non-admin.
fi
}
installCommandLineTools() {
# Installing the Xcode command line tools on 10.10 and later
echo "### Installing git via installing the Xcode command line tools..."
echo
os_vers=$(/usr/bin/sw_vers -productVersion | awk -F "." '{print $2}')
cmd_line_tools_temp_file="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
# Installing the latest Xcode command line tools on 10.10.x or later.
if [[ "$os_vers" -ge 10 ]]; then
# Create the placeholder file which is checked by the softwareupdate tool
# before allowing the installation of the Xcode command line tools.
touch "$cmd_line_tools_temp_file"
# Identify the correct update in the Software Update feed with "Command Line Tools" in the name for the OS version in question.
if [[ "$os_vers" -ge 15 ]]; then
cmd_line_tools=$(softwareupdate -l | awk '/\*\ Label: Command Line Tools/ { $1=$1;print }' | sed 's/^[[ \t]]*//;s/[[ \t]]*$//;s/*//' | cut -c 9-)
elif [[ "$os_vers" -ge 10 ]] && [[ "$os_vers" -lt 14 ]]; then
cmd_line_tools=$(softwareupdate -l | awk '/\*\ Command Line Tools/ { $1=$1;print }' | grep "$os_vers" | sed 's/^[[ \t]]*//;s/[[ \t]]*$//;s/*//' | cut -c 2-)
fi
# Check to see if the softwareupdate tool has returned more than one Xcode
# command line tool installation option. If it has, use the last one listed
# as that should be the latest Xcode command line tool installer.
if (( $(grep -c . <<<"$cmd_line_tools") > 1 )); then
cmd_line_tools_output="$cmd_line_tools"
cmd_line_tools=$(printf "$cmd_line_tools_output" | tail -1)
fi
# Install the command line tools
sudo softwareupdate -i "$cmd_line_tools" --verbose
# Remove the temp file
if [[ -f "$cmd_line_tools_temp_file" ]]; then
rm "$cmd_line_tools_temp_file"
fi
else
echo "Sorry, this script is only for use on OS X/macOS >= 10.10"
fi
}
installAutoPkg() {
# Install the latest release of AutoPkg
AUTOPKG_LOCATION_LATEST=$(curl https://api.github.com/repos/autopkg/autopkg/releases | python -c 'import json,sys;obj=json.load(sys.stdin);print obj[0]["assets"][0]["browser_download_url"]')
/usr/bin/curl -L -s "${AUTOPKG_LOCATION_LATEST}" -o "$USERHOME/autopkg-latest.pkg"
${LOGGER} "Installing AutoPkg"
sudo installer -verboseR -pkg "$USERHOME/autopkg-latest.pkg" -target /
# Clean up
rm "$USERHOME/autopkg-latest.pkg"
${LOGGER} "AutoPkg Installed"
echo ""
echo "### AutoPkg Installed"
echo ""
}
installMunkiTools() {
${LOGGER} "Grabbing and Installing the Munki Tools Because They Aren't Present"
MUNKI_LATEST=$(curl https://api.github.com/repos/munki/munki/releases/latest | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["assets"][0]["browser_download_url"]')
curl -L "${MUNKI_LATEST}" -o "$USERHOME/munki-latest1.pkg"
## Install The Munki Tools!
# Write a Choices XML file for the Munki package. We are installing the tools, but not the launchd nor the Managed Software Center App. Thanks Rich and Greg for the language!
/bin/cat > "/tmp/com.github.munki-in-a-box.munkiinstall.xml" << 'MUNKICHOICESDONE'
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>attributeSetting</key>
<integer>1</integer>
<key>choiceAttribute</key>
<string>selected</string>
<key>choiceIdentifier</key>
<string>core</string>
</dict>
<dict>
<key>attributeSetting</key>
<integer>1</integer>
<key>choiceAttribute</key>
<string>selected</string>
<key>choiceIdentifier</key>
<string>admin</string>
</dict>
<dict>
<key>attributeSetting</key>
<integer>0</integer>
<key>choiceAttribute</key>
<string>selected</string>
<key>choiceIdentifier</key>
<string>app</string>
</dict>
<dict>
<key>attributeSetting</key>
<integer>0</integer>
<key>choiceAttribute</key>
<string>selected</string>
<key>choiceIdentifier</key>
<string>launchd</string>
</dict>
</array>
</plist>
MUNKICHOICESDONE
sudo /usr/sbin/installer -dumplog -verbose -applyChoiceChangesXML "/tmp/com.github.munki-in-a-box.munkiinstall.xml" -pkg "$USERHOME/munki-latest1.pkg" -target "/"
${LOGGER} "Installed Munki Admin and Munki Core packages"
echo "Installed Munki packages"
# Clean up
rm "$USERHOME/munki-latest1.pkg"
}
installPythonPip() {
# Get Python Pip install tool
${LOGGER} "Installing Python Pip install tool"
sudo easy_install pip
${LOGGER} "Pip Installed"
echo
echo "### Pip Installed"
echo
}
installPythonCryptographyModule() {
# Install pyopenssl to add the cryptography module
# needed by AutoPkg on macOS Sierra and later.
${LOGGER} "Installing Python PyOpenSSL module to add the cryptography module."
pip install -I --user pyopenssl
${LOGGER} "PyOpenSSL Installed"
echo
echo "### PyOpenSSL Installed"
echo
}
installAWSCLI() {
# Install awscli, needed to work with S3
${LOGGER} "Installing awscli tool."
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "$USERHOME/awscli-bundle.zip"
unzip "$USERHOME/awscli-bundle.zip" -d "$USERHOME"
sudo "$USERHOME/awscli-bundle/install" -i /usr/local/aws -b /usr/local/bin/aws
# Clean up
rm -rf "$USERHOME/awscli-bundle"
rm "$USERHOME/awscli-bundle.zip"
${LOGGER} "AWSCLI Installed"
echo
echo "### AWSCLI Installed"
echo
}
## Couple Checks first:
echo "First up: Are you an admin user? Checking on that..."
# Make sure that the script is not being run as root.
rootCheck
# Make sure that the script is being run by an admin account.
adminCheck
echo "Great! The $ADMINUSERNAME account is an admin account."
echo "Any follow-up password requests will be for sudo rights."
${LOGGER} "Starting up Munki in a Cloud..."
${LOGGER} "Starting checks..."
# Make sure the whole script stops if Control-C is pressed.
fn_terminate() {
fn_log_error "Munki-in-a-Box has been terminated."
exit 1
}
trap 'fn_terminate' SIGINT
echo "Do we have git installed?"
# Find git's installed location. There will be an executable stub
# binary available at /usr/bin/git, but that doesn't necessarily mean
# git is actually installed. Instead, without git installed, the stub
# binary will trigger a GUI window which requests the installation of
# install the Xcode command line tools.
# If Xcode.app is installed in /Applications, set /usr/bin/git as
# git's location.
if [[ -x "/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/git" ]]; then
GIT_LOCATION="/usr/bin/git"
# If the Xcode command line tools are installed, set /usr/bin/git as
# git's location.
elif [[ -x "/Library/Developer/CommandLineTools/usr/libexec/git-core/git" ]]; then
GIT_LOCATION="/usr/bin/git"
# If the standalone git is installed, set /usr/local/bin/git as
# git's location.
elif [[ -x "/usr/local/git/bin/git" ]]; then
GIT_LOCATION="/usr/local/bin/git"
# Otherwise, explicitly set GIT_LOCATION to be a null value.
# That will trigger the script to install the Xcode command line tools.
else
GIT_LOCATION=""
fi
# Check for Xcode command line tools and install if needed.
if [[ ! -x "$GIT_LOCATION" ]]; then
installCommandLineTools
else
${LOGGER} "Git installed"
echo "### Git Installed"
fi
# Check for Python pip installer tool and install if needed.
if [[ ! -x "$PIP_LOCATION" ]]; then
installPythonPip
else
${LOGGER} "Pip installed"
echo "### Pip Installed"
fi
# Check for Python cryptography module and install if needed.
if [[ $(pip list | awk '/cryptography/ {print $1}') = "" ]]; then
installPythonCryptographyModule
else
${LOGGER} "Python cryptography module installed"
echo "### PyOpenSSL Installed"
fi
# Get AutoPkg if not already installed
if [[ ! -x ${AUTOPKG_LOCATION} ]]; then
installAutoPkg "${userhome}"
# Clean up if necessary.
if [[ -e "$USERHOME/autopkg-latest.pkg" ]]; then
rm "$USERHOME/autopkg-latest.pkg"
fi
else
${LOGGER} "AutoPkg installed"
echo "### AutoPkg Installed"
fi
## Check for Munki Tools and install if needed.
if [[ ! -x "$MUNKILOC/munkiimport" ]]; then
installMunkiTools
else
${LOGGER} "Munki installed."
echo "/usr/local/munki/munkiimport existed, so I am not reinstalling. Hope you really had the Munki tools installed..."
fi
## Build a local repository
/bin/mkdir -p "$REPODIR/catalogs"
/bin/mkdir -p "$REPODIR/manifests"
/bin/mkdir -p "$REPODIR/pkgs"
/bin/mkdir -p "$REPODIR/pkgsinfo"
/bin/mkdir -p "$REPODIR/icons"
# When later syncing to S3, empty folders will not be synced because
# S3 doesn't have a filesystem concept of directories. To avoid the
# problem and force the complete directory structure to sync, a hidden
# file named .miac will be placed in each directory.
touch "$REPODIR/catalogs/.miac"
touch "$REPODIR/manifests/.miac"
touch "$REPODIR/pkgs/.miac"
touch "$REPODIR/pkgsinfo/.miac"
touch "$REPODIR/icons/.miac"
# Make sure the logged-in user owns the Munki repo directory.
chmod -R "$ADMINUSERNAME" a+rX,g+w "$REPODIR"
## Install autopkg
installAutoPkg
####
# Configure AutoPkg for use with Munki
####
${DEFAULTS} write com.github.autopkg MUNKI_REPO "$REPODIR"
# Add AutoPkg repos (checks if already added)
${AUTOPKG} repo-add ${AUTOPKG_REPOS}
# Update AutoPkg repos (if the repos were already there no update would otherwise happen)
${AUTOPKG} repo-update ${AUTOPKG_REPOS}
${DEFAULTS} write com.googlecode.munki.munkiimport editor "${TEXTEDITOR}"
${DEFAULTS} write com.googlecode.munki.munkiimport repo_path "${REPODIR}"
${DEFAULTS} write com.googlecode.munki.munkiimport pkginfo_extension .plist
${DEFAULTS} write com.googlecode.munki.munkiimport default_catalog testing
${LOGGER} "AutoPkg Configured"
echo "AutoPkg Configured"
# This makes AutoPkg useful on future runs for the admin user defined at the top. It copies & creates preferences for autopkg and munki into their home dir's Library folder, as well as transfers ownership for the ~/Library/AutoPkg folders to them.
plutil -convert xml1 ~/Library/Preferences/com.googlecode.munki.munkiimport.plist
## Download Things and put them into the Repo
####
# Get some Packages and Stuff them in Munki
####
aLen=${#AUTOPKGARRAY[@]}
echo "$aLen" "overrides to create"
for (( j=0; j<aLen; j++));
do
${LOGGER} "Adding ${AUTOPKGARRAY[$j]} override"
${AUTOPKG} make-override "${AUTOPKGARRAY[$j]}"
${AUTOPKG} update-trust-info "${AUTOPKGARRAY[$j]}"
${LOGGER} "Added ${AUTOPKGARRAY[$j]} override"
${LOGGER} "Running ${AUTOPKGARRAY[$j]} recipe"
${AUTOPKG} run "${AUTOPKGARRAY[$j]}"
done
${LOGGER} "AutoPkg Run"
echo "AutoPkg has run"
####
# Install AutoPkgr from the awesome Linde Group!
####
${AUTOPKG} make-override AutoPkgr.install
${AUTOPKG} run local.install.AutoPkgr
${LOGGER} "AutoPkgr Installed"
echo "AutoPkgr Installed"
# Create AutoPkgr recipe list
/bin/mkdir "$USERHOME/Library/Application Support/AutoPkgr"
# Add all recipes to AutoPkgr's list of recipes
/bin/ls -A "$USERHOME/Library/AutoPkg/Cache" | grep -v plist | grep -v MakeCatalogs | grep -v AutoPkgr > "$USERHOME/Library/Application Support/AutoPkgr/recipe_list.txt"
/bin/ls -A "$USERHOME/Library/AutoPkg/Cache" | grep MakeCatalogs >> "$USERHOME/Library/Application Support/AutoPkgr/recipe_list.txt"
####
# Make Munki catalogs
####
"$MUNKIMAKECATALOGS" "$REPODIR"
####
# Create new site_default manifest and add imported packages to it
####
${MANU} new-manifest site_default
echo "Site_Default created"
${MANU} add-catalog testing --manifest site_default
echo "Testing Catalog added to Site_Default"
listofpkgs=($(${MANU} list-catalog-items testing))
echo "List of Packages for adding to repo:" ${listofpkgs[*]}
# Thanks Rich! Code for Array Processing borrowed from First Boot Packager
# Original at https://github.com/rtrouton/rtrouton_scripts/tree/master/rtrouton_scripts/first_boot_package_install/scripts
tLen=${#listofpkgs[@]}
echo "$tLen" " packages to add to manifest"
for (( i=0; i<tLen; i++));
do
${LOGGER} "Adding ${listofpkgs[$i]} to site_default"
${MANU} add-pkg ${listofpkgs[$i]} --manifest site_default
${LOGGER} "Added ${listofpkgs[$i]} to site_default"
done
####
# Install Munki Admin App by the amazing Hannes Juutilainen
####
${AUTOPKG} make-override MunkiAdmin.install
${AUTOPKG} run local.install.MunkiAdmin
# Check for the awscli tool and install if needed.
if [[ ! -x ${AWS} ]]; then
installAWSCLI
else
${LOGGER} "awscli installed"
echo "### awscli Installed"
fi
## Configure the awscli settings
## First we have to add credentials to a specific file.
/bin/mkdir -p ~/.aws
echo "[munki-in-a-cloud]" > ~/.aws/credentials
echo "aws_access_key_id = $AWSSECRETKEY" >> ~/.aws/credentials
echo "aws_secret_access_key = $AWSSECRETPASSWORD" >> ~/.aws/credentials
echo "[munki-in-a-cloud]" > ~/.aws/config
echo "region = $AWSREGIONID" >> ~/.aws/config
echo "[preview]" >> ~/.aws/config
echo "cloudfront = true" >> ~/.aws/config
## Create the S3 bucket
${LOGGER} "Creating S3 bucket"
echo "Creating S3 bucket named $BUCKET in $AWSREGIONID"
# evaluate the region first and add options accordingly
if [[ "$AWSREGIONID" = "us-east-1" ]]; then
"$AWS" s3api create-bucket --acl private --bucket "$BUCKET" --region "$AWSREGIONID" --profile munki-in-a-cloud
else
"$AWS" s3api create-bucket --acl private --bucket "$BUCKET" --region "$AWSREGIONID" --create-bucket-configuration LocationConstraint="$AWSREGIONID" --profile munki-in-a-cloud
fi
## Sync to the S3 bucket
${LOGGER} "Synchronizing S3 bucket"
echo "Synching $REPODIR with S3 bucket named $BUCKET in $AWSREGIONID"
"$AWS" s3 sync "$REPODIR" s3://"$BUCKET" --exclude '*.git/*' --exclude '.DS_Store' --delete --profile munki-in-a-cloud
${LOGGER} "Munki in a Cloud run completed."
echo ""
echo "Munki in a Cloud has finished running."
echo "May your Munki repo be full, the S3 bucket provisioned, and your heart be glad."
echo ""
## Get a Diet Coke and go to the pub.