-
Notifications
You must be signed in to change notification settings - Fork 0
/
up-vag.sh
executable file
·76 lines (68 loc) · 1.85 KB
/
up-vag.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
#!/bin/bash
# update vagrant boxes
# bug: error when no box installed
# 'There ...'
DEBUG=0
# initial box list
TMPBOXLISTINIT=$(mktemp)
vagrant box list | sort > $TMPBOXLISTINIT
if [ .$DEBUG = .1 ]; then
echo "box list at start"
cat $TMPBOXLISTINIT
fi
# handle multiple providers
if [ .$DEBUG = .1 ]; then
echo "trying to update boxes"
fi
for b in `cat $TMPBOXLISTINIT|grep -v ', 0)'|awk '{print $1i"#"$2}'|sed 's/(//'|sed 's/,//'|sort|uniq`
do
date
#echo "line=" $b
BOX=`echo $b|cut -f 1 -d '#'`
PROV=`echo $b|cut -f 2 -d '#'`
echo $BOX $PROV
vagrant box update --box $BOX --provider $PROV
done
# remove old version of vagrant boxes
TMPFILE=$(mktemp)
vagrant box list | sort > $TMPFILE
if [ .$DEBUG = .1 ]; then
echo "boxes list after update"
cat $TMPFILE
fi
cat $TMPFILE
echo "remove old boxes"
first='1'
while read -r vname vtype vver || [[ -n "$vver" ]]; do
vtype=`echo $vtype | sed -e 's/[\(\),]//g'`
vver=`echo $vver | sed -e 's/[\(\),]//g'`
# echo cur: $vname $vtype $vver prev: $pname $ptype $pver
if [ .$first = '.1' ] ; then
first=0
else
if ([ .$pname = .$vname ] && [ .$ptype = .$vtype ]) ; then
echo -n "will try to remove older $pname $ptype version $pver and keep newer version $vver: "
vagrant box remove $pname --provider $ptype --box-version $pver 2>/dev/null
ret=$?
if [ .$ret != .0 ] ; then
# TODO find who use this box
echo "removal failed, box is in use"
else
echo "success"
fi
fi
fi
pname=$vname
ptype=$vtype
pver=$vver
done < $TMPFILE
# final box list
TMPBOXLISTFIN=$(mktemp)
vagrant box list | sort > $TMPBOXLISTFIN
# list changes
if [ .$DEBUG = .1 ] ; then
echo "boxes list before update vs after old removal"
cat $TMPFILE
sdiff -s $TMPBOXLISTINIT $TMPBOXLISTFIN
fi
rm $TMPFILE $TMPBOXLISTINIT $TMPBOXLISTFIN