aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/%HOME%/.local/bin/vagrant-update-all-boxes.sh
blob: 6d7e5a8886144004594330625f2354872a483f50 (plain) (blame)
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
#!/usr/bin/env bash

# Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "linux-home" project.
# For details, see https://github.com/egor-tensin/linux-home.
# Distributed under the MIT License.

# This script updates all Vagrant boxes.

set -o errexit -o nounset -o pipefail
shopt -s inherit_errexit

dump() {
    local msg
    for msg; do
        local prefix="${FUNCNAME[0]}"
        [ "${#FUNCNAME[@]}" -gt 1 ] && prefix="${FUNCNAME[1]}"
        echo "$prefix: $msg"
    done
}

update_box_from_line() {
    local orig_line
    for orig_line; do
        local line="$orig_line"
        line="${line//'*'/}"
        line="${line//"'"/}"

        local box
        box="$( echo "$line" | cut -d ' ' -f 2 )"
        local provider
        provider="$( echo "$line" | cut -d ' ' -f 4 )"

        local sanity_check
        sanity_check="$( echo "$line" | cut -d ' ' -f 3 )"

        if [ "$sanity_check" != 'for' ]; then
            dump "this line is malformed:" "$orig_line" >&2
            return 1
        fi

        dump "updating box '$box' for provider '$provider'..."
        vagrant box update --box "$box" --provider "$provider"
    done
}

update_all_boxes() {
    local output
    output="$( vagrant box outdated --global | grep -F outdated )"

    local line
    while IFS= read -r line; do
        update_box_from_line "$line"
    done <<< "$output"
}

clean_old_boxes() {
    vagrant box prune --force --keep-active-boxes
}

main() {
    update_all_boxes
    clean_old_boxes
}

main