This repository was archived by the owner on Oct 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvagrant-box-updates.sh
More file actions
executable file
·54 lines (47 loc) · 1.68 KB
/
Copy pathvagrant-box-updates.sh
File metadata and controls
executable file
·54 lines (47 loc) · 1.68 KB
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
#!/usr/bin/env bash
# Author: github.com/danielhoherd
# License: MIT
# Purpose: Update all downloaded vagrant boxes to the latest version and delete old box versions
check_for_required_commands() {
for command in "$@" ; do
command -v "${command}" >/dev/null 2>&1 || missing_commands+=( "${command}" )
done
if [[ "${#missing_commands[@]}" -gt 0 ]] ; then
date "+%FT%T%z ${0##*/} ABORT: missing commands: ${missing_commands[*]}"
exit 1
fi
}
check_for_required_commands xargs vagrant awk date head
if [[ "${OSTYPE}" =~ ^darwin ]] ; then
for cmd in awk date head sed sort ; do
command -v "g${cmd}" > /dev/null || { echo "Command missing: 'g${cmd}'" ; (( error += 1 )) ; }
# Evil eval because you can't create a function with a variable for a name
eval "function ${cmd}() { "g${cmd}" \"\$@\" ; }"
# shellcheck disable=SC2163
export -f "${cmd}"
done
if [[ "${error:-0}" -gt 0 ]] ; then echo "Please install the missing commands." ; exit 1 ; fi ;
elif [[ "${OSTYPE}" =~ ^linux ]] ; then
: # nothing to be done
else
echo "ERROR: unsure of how to handle ${OSTYPE}"
exit 1
fi
date "+%FT%T%z Pulling new versions of all existing Vagrant boxes"
vagrant box outdated --machine-readable --global |
awk -F, '$4 == "warn" {print $5}' |
awk -F"'" '{print $2}' |
sort -u |
xargs -r -n1 vagrant box update --box
date "+%FT%T%z Deleting old versions of each existing Vagrant box, keeping only the latest"
vagrant box list |
while read -r box _ ; do
vagrant box list |
grep "$box" |
head -n -1 |
awk '{print $3}' |
sed 's/)$//' |
sort --version-sort |
xargs -r -t -n1 vagrant box remove "$box" --force --box-version
done
find ~/.vagrant.d/boxes/ -empty -delete