f0254b9a4438430530bf179ac7d60ffca3e5da25
[invirt/packages/invirt-remote.git] / files / usr / sbin / remctl-update.sh
1 #!/bin/sh
2 DIR=/etc/remctl/sipb-xen-auto
3 TEMPLATE=$DIR/conf.template
4 MACHINEDIR=$DIR/machine.d
5 MOIRADIR=$DIR/moira-acl
6 MOIRATMP=$DIR/moira-tmp
7 MACHINETMP=$DIR/machine-list-tmp
8 AUTOMACHINELIST=$DIR/auto-machine-list
9 AUTOMOIRALIST=$DIR/auto-moira-list
10 BINDIR=/usr/sbin
11 ACLDIR=$DIR/acl
12
13 update_machine()
14 {
15     machine="$1"
16     sed "s/#MACHINENAME#/$machine/g" "$TEMPLATE" | \
17         sed "s,#BINDIR#,$BINDIR,g" >| "$MACHINETMP"
18     if ! cmp -s "$MACHINEDIR/$machine" "$MACHINETMP"; then
19         mv "$MACHINETMP" "$MACHINEDIR/$machine"
20     else
21         rm -f "$MACHINETMP"
22     fi
23 }
24
25 update_moiragroup()
26 {
27     group="$1"
28     # Should perhaps replace with LDAP, but fine for now.
29
30     # We should do more careful error checking so we don't take away
31     # all bits and delete the moira-acl files whenever there's an AFS
32     # outage.
33     pts membership system:"$group" -noauth | tail -n+2 | \
34         sed 's/\./\//' | \
35         sed 's/^  //' | \
36         sed 's/$/@ATHENA.MIT.EDU/g' >| "$MOIRATMP"
37     if test -s "$MOIRATMP"; then
38         if ! cmp -s "$MOIRADIR/$group" "$MOIRATMP"; then
39             mv "$MOIRATMP" "$MOIRADIR/$group"
40         fi
41     else
42         if test -e "$MOIRADIR/$group"; then
43             rm "$MOIRADIR/$group"
44         fi
45     fi
46     rm -f "$MOIRATMP"
47 }
48
49 check_machine_name()
50 {
51     machinename="$1"
52     if ! perl -0e 'exit($ARGV[0] !~ /^[A-Za-z0-9][A-Za-z0-9._-]*$/)' -- "$machinename"; then
53         echo "Bad machine name"
54         exit 1
55     fi
56 }
57
58 case "$1" in
59     moiragroup)
60         update_moiragroup "$2"
61         ;;
62
63     all_machines)
64         # update the remctl.conf definitions
65         for machine in `cat "$AUTOMACHINELIST"`; do
66             update_machine "$machine"
67         done
68         ;;
69     all_moira)
70         # update our moira ACL lists
71         for group in `cat "$AUTOMOIRALIST"`; do
72             update_moiragroup "$group"
73         done
74         ;;
75     auto_machine_list)
76         # update the list of maintained machines
77         /bin/ls "$ACLDIR" >| "$AUTOMACHINELIST"
78         ;;
79     auto_moira_list)
80         # update the moira list-of-lists
81         # /bin/ls "$MOIRADIR" >| "$AUTOMOIRALIST" # BAD IDEA in case of outage
82
83         # This extracts the list of all moira lists we care about, and updates those.
84         grep -R moira "$ACLDIR/" /etc/remctl/acl/ | perl -pe 's/.*moira-acl\/(.*)/$1/g' >| "$AUTOMOIRALIST"
85         ;;
86     unregister)
87         machine="$2"
88         check_machine_name "$machine"
89         rm -f "$ACLDIR"/"$machine"
90         rm -f "$MACHINEDIR"/"$machine"
91         "$0" web
92         ;;
93     moveregister)
94         oldmachine="$2"
95         newmachine="$3"
96         check_machine_name "$oldmachine"
97         check_machine_name "$newmachine"
98         mv "$ACLDIR"/"$oldmachine" "$ACLDIR"/"$newmachine"
99         rm -f "$MACHINEDIR"/"$oldmachine"
100         "$0" web
101         ;;
102     register)
103         machine="$2"
104         check_machine_name "$machine"
105         if [ -e "$ACLDIR"/"$machine" ]; then
106             echo "Machine already registered"
107             exit 1
108         fi
109         echo "include /etc/remctl/acl/web" > "$ACLDIR/$machine"
110         "$0" web
111         ;;
112     web)
113         "$0" auto_machine_list
114         "$0" all_machines
115         ;;      
116     remctl-moira-update|all)
117         "$0" auto_machine_list
118         "$0" all_machines
119         "$0" auto_moira_list
120         "$0" all_moira
121         ;;
122 esac
123
124 exit 0