-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagents
111 lines (93 loc) · 2.95 KB
/
agents
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# quick hack, forked from do_ssh_agent() to generalize,
# since the same paradigm was needed for a dbus agent
# --smm20130823123744
#
##############################################################################
init_agents ()
{
do_agent ssh \
"ssh-agent" \
'sed -i "/^echo/d"' \
"ssh-add" \
SSH_{AGENT_PID,AUTH_SOCK}
# XXX TODO trying to see if we just do dbus-launch once, are
# we ok without anything special in our shell?
return
do_agent dbus \
"dbus-daemon --fork --session --print-pid --print-address" \
'sed -i -n \
-e "1{s,.*,export DBUS_SESSION_BUS_ADDRESS=&,p}" \
-e "2{s,.*,export DBUS_SESSION_BUS_PID=&,p}" \
' \
"" \
DBUS_SESSION_BUS_{PID,ADDRESS}
}
do_agent ()
{
local name=$1
eval "local cmdline=($2)" # run this, record output to tmpfile
eval "local transform=($3)" # then run this, tmpfile as last arg
eval "local runafter=($4)" # then run this, after sourcing tmpfile
shift 4
local vars=($@)
local exe=$cmdline
local pidvar=$vars # first one in array always holds pid
local agentdir=~/var/agents
local cmdfile=$agentdir/${HOSTNAME:?}/$name
# berkeley unix does not have a default template, so
# we have to supply one
#
local tmpfile=`mktemp /tmp/tmp.XXXXXXXXXXXX`
# we don't want these to end up in the root shellfunc
# namespace so make them nested functions (really we
# should be using library routines anyways TODO)
#
eprint () { echo "$0: $@" >&2; }
err () { eprint $1; false; }
# is an agent already running?
#
local agentpid=($(pgrep -u ${SUDO_UID:-${UID:?}} $exe))
((${#agentpid[@]} > 1)) &&
{ err "more than one $name agent running"; return; }
# would we be able to run it if we wanted to?
#
type -P $exe &>/dev/null ||
{ err "$exe not found in path"; return; }
# presence of cmdfile indicates that we should run on this host
#
test -f $cmdfile || return
# do not run if the one running already is known to us
#
source $cmdfile
((agentpid && agentpid == ${!pidvar:-0})) && return
# otherwise we have a stale one laying around, or haven't
# started one yet, so do it, writing output to new tmpfile
#
unset ${vars[@]}
if ((${#cmdline[@]}))
then eval '"${cmdline[@]}" >| $tmpfile' ||
{ err "bad start: $name"; return; }; fi
# transform the tmpfile with the commands if non-empty, with
# tmpfile given as last argument (useful for e.g. sed -i)
#
if ((${#transform[@]}))
then eval '"${transform[@]}" $tmpfile' ||
{ err "bad transform for $name"; return; }; fi
# put into its proper place for next time (to source)
#
mv $tmpfile $cmdfile ||
{ err "cound not rename $tmpfile"; return; }
# and now that we have created the cmdfile, source it ourselves
#
source $cmdfile ||
{ err "bad pidf: $cmdfile"; return; }
# finally, run the given after-command if specified
#
if ((${#runafter[@]}))
then eval '"${runafter[@]}"' ||
{ err "bad runafter ($?) for $name"; return; }; fi
echo "started new $name agent for host $HOSTNAME"
}
init_agents
unset -f init_agents do_agent