-
Notifications
You must be signed in to change notification settings - Fork 0
/
msync
executable file
·135 lines (122 loc) · 3.18 KB
/
msync
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env bash
if [ -z "$LSYNCD_TEMPLATE" ]; then
LSYNCD_TEMPLATE="$(dirname "$0")/template.lua"
fi
if [ -z "$XDG_RUNTIME_DIR" ]; then
echo "Don't know where to store program state. Set XDG_RUNTIME_DIR."
exit 1
fi
LSYNCD_DIR="$XDG_RUNTIME_DIR/machine-sync"
COUNT_FILE="$LSYNCD_DIR/count"
function create_dir {
if [ ! -f "$COUNT_FILE" ]; then
id=1
else
id=$(( $(cat "$COUNT_FILE") + 1 ))
fi
echo "$id" > "$COUNT_FILE"
mkdir -p "$LSYNCD_DIR/$id"
echo "$LSYNCD_DIR/$id"
}
function status {
mkdir -p "$LSYNCD_DIR"
# so that empty is reported correctly
find "$LSYNCD_DIR" -maxdepth 1 -mindepth 1 -type d -printf '%P\n' | while read -r id; do
pid=$(< "$LSYNCD_DIR/$id/pid")
if ps -p "$pid" > /dev/null; then
dir=$(basename "$(< "$LSYNCD_DIR/$id/dir")")
machines=$(< "$LSYNCD_DIR/$id/machines")
pid=$(< "$LSYNCD_DIR/$id/pid")
echo "$id ($pid): $dir -> $machines"
else
rm -rf "${LSYNCD_DIR:--}/$id"
fi
done
}
function get_instance_from_dir {
dir=$1
mkdir -p "$LSYNCD_DIR"
find "$LSYNCD_DIR" -maxdepth 1 -mindepth 1 -type d -printf '%P\n' | while read -r id; do
if [ "$(< "$LSYNCD_DIR/$id/dir")" = "$dir" ]; then
echo "$id"
return 0
fi
done
}
function start {
local -n _args=$1
local -n _machines=$2
# stop existing session of the same dir
existing=$(get_instance_from_dir "$PWD")
if [ -n "$existing" ]; then
echo "Replacing with existing session $existing syncing to $(< "$LSYNCD_DIR/$existing/machines")"
stop "$existing"
fi
dir=$(create_dir)
prev_ifs="$IFS"
IFS=:
export MACHINES="${_machines[*]}"
IFS="$prev_ifs"
if [ -z "$debug" ]; then
lsyncd -nodaemon "$LSYNCD_TEMPLATE" "${_args[@]}" &>"$dir/logs" & disown
else
lsyncd -nodaemon "$LSYNCD_TEMPLATE" "${_args[@]}"
fi
echo "$!" > "$dir/pid"
echo "${_args[@]}" > "$dir/args"
echo "$PWD" > "$dir/dir"
echo "${_machines[@]}" > "$dir/machines"
}
function stop {
for id in "$@"; do
kill "$(< "$LSYNCD_DIR/$id/pid")" 2>/dev/null
rm -rf "${LSYNCD_DIR:--}/$id"
done
}
case "$1" in
"")
cmd="status"
shift
;;
start|status|stop)
cmd="$1"
shift
;;
*)
echo "first arg needs to be cmd (status|stop|start)"
exit 1
;;
esac
case "$cmd" in
status)
status
;;
start)
machines=()
args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--debug)
debug=1
shift
;;
-*)
args+=("$1")
shift
;;
*)
if [[ $1 == *:* ]]; then
echo "Do not pass targets with : as machines in there"
exit 1
fi
machines+=("$1")
shift
;;
esac
done
start args machines
;;
stop)
stop "$@"
;;
esac