Documentation : ajoute des info() .
[lhc/ateliers.git] / vm_remote
1 #!/bin/sh
2 set -e -f ${DRY_RUN:+-n} -u
3 tool=${0%/*}
4 . "$tool"/lib/functions.sh
5 . "$tool"/etc/vm.sh
6
7 rule_help () { # SYNTAX: [--hidden]
8 local hidden; [ ${1:+set} ] || hidden=set
9 cat >&2 <<-EOF
10 DESCRIPTION:
11 ce script regroupe des règles pour administrer la VM ($vm_fqdn)
12 _depuis_ une machine distante ;
13 il sert à la fois d'outil (aisément bidouillable)
14 et de documentation (préçise).
15 Voir \`$tool/vm_host' pour les règles côté machine hôte ($vm_host).
16 Voir \`$tool/vm_hosted' pour les règles côté VM hébergée ($vm_fqdn).
17 SYNTAX: $0 \$RULE \${RULE}_SYNTAX
18 RULES:
19 $(sed -ne "s/^rule_\(${hidden:+[^_]}[^ ]*\) () {\( *#.*\|\)/ \1\2/p" "$tool"/etc/vm.sh "$0")
20 ENVIRONMENT:
21 TRACE # affiche les commandes avant leur exécution
22 $(sed -ne 's/^readonly \([^ ][^ =]*\).*}\( *#.*\|\)$/\t$\1\2/p' "$tool"/etc/vm.sh "$0")
23 EOF
24 }
25
26 rule_git_config () { # DESCRIPTION: configure le .git local pour bien pousser
27 (
28 cd "$tool"
29 git remote rm host || true
30 git remote add host $vm_host:tool/vm
31 git config --replace remote.host.push HEAD:refs/remotes/master
32 git remote rm hosted || true
33 git remote add hosted root@$vm_fqdn:tool/vm
34 git config --replace remote.hosted.push HEAD:refs/remotes/master
35 )
36 }
37 rule_git_push () { # SYNTAX: {host|hosted} $git_push_options
38 local remote=${1#remote=}; shift
39 GIT_SSH=./vm_ssh git push -v "$remote" "$@"
40 info "penser à faire : vm_hosted git_reset"
41 }
42
43 rule_ssh () {
44 "$tool"/vm_ssh $vm_fqdn "$@"
45 }
46 rule_mosh () {
47 mosh --ssh="$tool/vm_ssh $*" $vm_fqdn
48 }
49 rule__ssh_known_hosts_update () {
50 rule ssh \
51 -o StrictHostKeyChecking=no \
52 -o CheckHostIP=no \
53 -o HashKnownHosts=no \
54 whoami
55 }
56 rule_key_disk_send () {
57 gpg --decrypt var/sec/luks/$vm_fqdn.key.gpg |
58 "$tool"/vm_ssh root@$vm_fqdn "$@" \
59 -o CheckHostIP=no \
60 -o HostKeyAlias=init.$vm_fqdn \
61 tee /lib/cryptsetup/passfifo \>/dev/null
62 }
63 rule_key_disk_backup () {
64 for part in root var home
65 do
66 mkdir -p var/sec/luks
67 rule ssh -l root ' \
68 tmp=$(mktemp)
69 cryptsetup luksHeaderBackup \
70 /dev/$vm_lvm_vg/${vm_lvm_lv}_${part} \
71 --header-backup-file "$tmp" \
72 cat "$tmp"
73 shred --remove "$tmp"
74 ' |
75 gpg --encrypt --recipient $USER@ \
76 -o var/sec/luks/${vm_lvm_lv}_${part}.luks.gpg
77 done
78 }
79
80 rule=${1:-help}
81 ${1+shift}
82 case $rule in
83 (help);;
84 (*)
85 assert 'test ! "$(hostname --fqdn)" = "$vm_fqdn"' vm_fqdn
86 assert 'test ! "$(hostname --fqdn)" = "$vm_host"' vm_host
87 ;;
88 esac
89 rule $rule "$@"