summaryrefslogtreecommitdiffstats
path: root/bin/secrets
blob: 244431ca4df8da410f9b95af395dc5b425bac988 (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
67
68
69
70
71
72
73
74
75
#!/usr/bin/env bash
set -eu
set -o pipefail

opened() {
    test -e /dev/mapper/tozt-secrets
}

mounted() {
    grep -q '^/dev/mapper/tozt-secrets /mnt' /proc/mounts
}

cmd_open() {
    if ! opened; then
        pass="$(pass show crypt/luks/tozt-secrets)"
        echo -n "$pass" | sudo cryptsetup --key-file=- open ~/crypt/tozt-secrets.luks tozt-secrets
    fi
    if ! mounted; then
        sudo mount /dev/mapper/tozt-secrets /mnt
    fi
}

cmd_close() {
    if mounted; then
        sudo umount /dev/mapper/tozt-secrets
    fi
    if opened; then
        sudo cryptsetup close tozt-secrets
    fi
}

cmd_sync() {
    host="${2:-tozt}"
    if [ "${host}" = "tozt" ]; then
        hostname=tozt.net
    elif [ "${host}" = "mail" ]; then
        hostname=mail.tozt.net
    elif [ "${host}" = "partofme" ]; then
        hostname=partofme
    else
        echo "unknown host ${host}" >&2
        exit 1
    fi

    if mounted && opened; then
        was_opened=1
    else
        cmd_open
        was_opened=""
    fi

    rsync -avz --delete \
        /mnt/puppet/"$host"/. \
        root@"$hostname":/usr/local/share/puppet-tozt/modules/secret/files

    if [ -z "$was_opened" ]; then
        cmd_close
    fi
}

case "$1" in
    open)
        cmd_open "$@"
        ;;
    close)
        cmd_close "$@"
        ;;
    sync)
        cmd_sync "$@"
        ;;
    *)
        echo "unknown subcommand $1" >&2
        exit 1
        ;;
esac