-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathremote
executable file
·223 lines (190 loc) · 7.1 KB
/
remote
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
set -eo pipefail
HERE=$(cd $(dirname $0); pwd)
: ${REMOTE_HOST_FILE:=$HERE/remote-host}
# where to put source code on remote machine
: ${REMOTE_ROOT:=./source}
# Remote branch to use
: ${REMOTE_BRANCH:=main}
: ${REMOTE_DIGEST_TOOL:='sha256sum | cut -d " " -f 1'}
ensure_remote_host() {
if [ -z "$REMOTE_HOST" ]; then
if [ ! -f $REMOTE_HOST_FILE ] || [ -z "$(cat $REMOTE_HOST_FILE)" ]; then
cat >&2 <<EOF
fatal: $0: no REMOTE_HOST set
You can find substrate devices on your LAN with '$0 discover'
then set one as the default with '$0 set-host REMOTE_HOST'
Be sure your ~/.ssh/config has a stanza that looks like this (with an appropriate IdentityFile):
Host substrate-*.local
User root
IdentityFile ~/.ssh/id_substrate
EOF
exit 1
fi
REMOTE_HOST=$(cat $REMOTE_HOST_FILE)
fi
}
ensure_remote_root() {
ensure_remote_host
# make them absolute paths
REMOTE_ROOT="$(ssh $REMOTE_HOST "mkdir -p $REMOTE_ROOT; cd $REMOTE_ROOT; pwd")"
: ${REMOTE_DIGEST_BASEPATH:=$REMOTE_ROOT/.git}
REMOTE_DIGEST_LAST_HEAD=$REMOTE_DIGEST_BASEPATH/substrate-last-head
REMOTE_DIGEST_LAST_STAGED=$REMOTE_DIGEST_BASEPATH/substrate-last-staged-digest
REMOTE_DIGEST_LAST_UNSTAGED=$REMOTE_DIGEST_BASEPATH/substrate-last-unstaged-digest
}
fatal() {
echo "fatal: $0: $@"
exit 1
}
# use files on the remote machine to detect unexpected changes. abort if we detect any.
remote_sanity_check() {
if ! ssh $REMOTE_HOST "! [ -e $REMOTE_DIGEST_LAST_HEAD ] || test \"\$(git -C $REMOTE_ROOT rev-parse HEAD)\" == \"\$(cat $REMOTE_DIGEST_LAST_HEAD)\""; then
fatal "remote checkout in unexpected state: unexpected HEAD; to force, run: $0 force-push"
fi
if ! ssh $REMOTE_HOST "! [ -e $REMOTE_DIGEST_LAST_STAGED ] || test \"\$(git -C $REMOTE_ROOT diff --cached -s | $REMOTE_DIGEST_TOOL)\" == \"\$(cat $REMOTE_DIGEST_LAST_STAGED)\""; then
fatal "remote checkout in unexpected state; unexpected staged changes; to force, run: $0 force-push"
fi
if ! ssh $REMOTE_HOST "! [ -e $REMOTE_DIGEST_LAST_UNSTAGED ] || test \"\$(git -C $REMOTE_ROOT diff | $REMOTE_DIGEST_TOOL)\" == \"\$(cat $REMOTE_DIGEST_LAST_UNSTAGED)\""; then
fatal "remote checkout in unexpected state; unexpected unstaged changes; to force, run: $0 force-push"
fi
}
remote_reset_check() {
ensure_remote_root
ssh $REMOTE_HOST "rm -f $REMOTE_DIGEST_LAST_HEAD $REMOTE_DIGEST_LAST_STAGED $REMOTE_DIGEST_LAST_UNSTAGED"
}
push() {
ensure_remote_root
# only move forward if remote files are unchanged.
remote_sanity_check
ssh $REMOTE_HOST "mkdir -p $REMOTE_ROOT && git -C $REMOTE_ROOT init . && git -C $REMOTE_ROOT config receive.denyCurrentBranch ignore"
# remove the ready file before we make any other changes to it. this prevents file watching logic from seeing a partial sync. expect something else to put it back.
ssh $REMOTE_HOST "rm -f $REMOTE_ROOT/defs/ready"
git push --force ssh://${REMOTE_HOST}${REMOTE_ROOT}/.git +HEAD:$REMOTE_BRANCH
ssh $REMOTE_HOST "cd $REMOTE_ROOT && git reset --hard $REMOTE_BRANCH && git clean -fd"
ssh $REMOTE_HOST "git -C $REMOTE_ROOT rev-parse HEAD > $REMOTE_DIGEST_LAST_HEAD"
if ! git diff --cached -s --exit-code; then
git diff --cached | ssh $REMOTE_HOST "cd $REMOTE_ROOT && git apply --index"
ssh $REMOTE_HOST "git -C $REMOTE_ROOT diff --cached -s | $REMOTE_DIGEST_TOOL > $REMOTE_DIGEST_LAST_STAGED"
else
ssh $REMOTE_HOST "rm -f $REMOTE_DIGEST_LAST_STAGED"
fi
if ! git diff -s --exit-code; then
git diff | ssh $REMOTE_HOST "cd $REMOTE_ROOT && git apply"
ssh $REMOTE_HOST "git -C $REMOTE_ROOT diff | $REMOTE_DIGEST_TOOL > $REMOTE_DIGEST_LAST_UNSTAGED"
else
ssh $REMOTE_HOST "rm -f $REMOTE_DIGEST_LAST_UNSTAGED"
fi
# ensure we wrote reasonable files
remote_sanity_check
# NB disable cache syncing for now because it seems to destroy xattrs
# [ ! -e os/cache ] || rsync -aP --no-perms --no-times --no-owner --no-group os/cache/. ${REMOTE_HOST}:${REMOTE_ROOT}/os/cache/
# [ ! -e os/gen/oob ] || rsync -aP --no-perms --no-times --no-owner --no-group os/gen/oob/. ${REMOTE_HOST}:${REMOTE_ROOT}/os/gen/oob/
# [ ! -e tools/cue ] || rsync -aP --no-perms --no-times --no-owner --no-group tools/cue/. ${REMOTE_HOST}:${REMOTE_ROOT}/tools/cue/
}
pull() {
ensure_remote_host
# NB disable cache syncing for now because it seems to destroy xattrs
# rsync -aP --no-perms --no-times --no-owner --no-group ${REMOTE_HOST}:${REMOTE_ROOT}/os/cache/. os/cache/
# rsync -aP --no-perms --no-times --no-owner --no-group ${REMOTE_HOST}:${REMOTE_ROOT}/os/gen/oob/. os/gen/oob/
rsync -aP --no-perms --no-times --no-owner --no-group ${REMOTE_HOST}:${REMOTE_ROOT}/os/builds/. os/builds/
rsync -aP --no-perms --no-times --no-owner --no-group ${REMOTE_HOST}:${REMOTE_ROOT}/tools/cue/. tools/cue/
}
pull_latest() {
ensure_remote_host
LATEST_BUILD=$(ssh $REMOTE_HOST "readlink $REMOTE_ROOT/os/builds/latest")
rsync -aP --no-perms --no-times --no-owner --no-group ${REMOTE_HOST}:${REMOTE_ROOT}/os/builds/$LATEST_BUILD os/builds/
rsync -aP --no-perms --no-times --no-owner --no-group ${REMOTE_HOST}:${REMOTE_ROOT}/os/builds/latest os/builds/
}
discover() {
dns-sd -t 1 -B _substrate._tcp local. | tail -n +5 | awk '{print $7 ".local"}' | sort -u
}
announce() {
# Advertise a proxy for a device that isn't fully/properly up. Works without needing to modify /etc/hosts
host=$1
ip=$2
exec dns-sd -P $host _substrate._tcp local. 80 $host.local $ip
}
case "$1" in
ssh)
shift
ensure_remote_host
exec ssh $REMOTE_HOST "$@"
;;
force-push)
shift
remote_reset_check
push
;;
push)
shift
push
;;
pull)
shift
pull
;;
pull-latest)
shift
pull_latest
;;
host)
shift
ensure_remote_host
echo $REMOTE_HOST
;;
discover)
shift
discover
;;
announce)
shift
announce "$@"
;;
set-host)
shift
host=$1
echo $host > $REMOTE_HOST_FILE
cat >&2 <<EOF
If this is the first time you are using this device, you need to add your authorized authorized_key first by visiting:
http://substrate:substrate@$host:8181/
EOF
identityfile=$(ssh -G $host | grep identityfile | awk '{print $2}')
identityfile="${identityfile/#\~/$HOME}"
if [ -n "$identityfile" ]; then
pubkeyfile="${identityfile}.pub"
if [ -e $pubkeyfile ]; then
pubkey=$(cat $pubkeyfile)
fi
fi
if [ -n "$pubkey" ]; then
cat >&2 <<EOF
Run these commands on the shell linked above to add your configured IdentityFile as an authorized key
mkdir -p ~/.ssh/
echo "$pubkey" >> ~/.ssh/authorized_keys
EOF
else
cat >&2 <<EOF
Be sure your ~/.ssh/config has a stanza that looks like this (with an appropriate IdentityFile):
Host substrate-*.local
User root
IdentityFile ~/.ssh/id_substrate
EOF
fi
;;
-h|--help)
echo "usage: $0 ssh"
echo " $0 force-push"
echo " $0 push"
echo " $0 pull"
echo " $0 pull-latest"
echo " $0 host"
echo " $0 discover"
echo " $0 use REMOTE_HOST"
echo " $0 ./dev.sh reload"
;;
*)
push
exec ssh -t $REMOTE_HOST "cd $REMOTE_ROOT && exec $(printf '%q ' "$@")"
;;
esac