Compare commits
2
Commits
4f45187ca0
...
253ccb02f6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
253ccb02f6 | ||
|
|
c3ed3c026b |
No files matched your search
+43
-1
@@ -8,6 +8,7 @@
|
|||||||
# agent/deploy.sh --rotate web01 issue a new token, then reinstall
|
# agent/deploy.sh --rotate web01 issue a new token, then reinstall
|
||||||
# agent/deploy.sh --dry-run show what would happen
|
# agent/deploy.sh --dry-run show what would happen
|
||||||
# agent/deploy.sh -- --with-needrestart pass the rest to install.sh
|
# agent/deploy.sh -- --with-needrestart pass the rest to install.sh
|
||||||
|
# agent/deploy.sh --allow-outdated deploy even if this checkout is behind
|
||||||
#
|
#
|
||||||
# Settings: agent/deploy.conf (see deploy.conf.example). Hosts: agent/hosts,
|
# Settings: agent/deploy.conf (see deploy.conf.example). Hosts: agent/hosts,
|
||||||
# one per line: `<ssh target> [kernelwatch name]`; the name defaults to the
|
# one per line: `<ssh target> [kernelwatch name]`; the name defaults to the
|
||||||
@@ -149,10 +150,47 @@ deploy_one() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
usage() { sed -n '2,16s/^# \{0,1\}//p' "$0"; }
|
usage() { sed -n '2,17s/^# \{0,1\}//p' "$0"; }
|
||||||
|
|
||||||
|
# Under sudo, ssh would read root's ~/.ssh/config and keys instead of yours (a
|
||||||
|
# custom Port or IdentityFile silently disappears, so it tries port 22). Nothing
|
||||||
|
# here needs root locally: install.sh runs under sudo on each host.
|
||||||
|
refuse_sudo() {
|
||||||
|
[[ -n ${SUDO_USER:-} && $(id -u) == 0 ]] || return 0
|
||||||
|
die "don't run this with sudo: it uses your own SSH config and keys, and runs sudo on each host itself. Run it as $SUDO_USER."
|
||||||
|
}
|
||||||
|
|
||||||
|
# Deploying from a checkout that's behind its upstream ships an old agent: the
|
||||||
|
# dashboard keeps showing "agent is older" after a deploy that looked fine.
|
||||||
|
# Fetches with a time limit; a failed fetch only warns. Behind: asks on a
|
||||||
|
# terminal, otherwise needs --allow-outdated. Skipped outside a git checkout or
|
||||||
|
# without an upstream branch.
|
||||||
|
check_checkout() {
|
||||||
|
local repo upstream behind
|
||||||
|
repo=$(git -C "$here" rev-parse --show-toplevel 2>/dev/null) || return 0
|
||||||
|
upstream=$(git -C "$repo" rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null) || return 0
|
||||||
|
local -a fetch=(git -C "$repo" fetch --quiet)
|
||||||
|
command -v timeout >/dev/null && fetch=(timeout 20 "${fetch[@]}")
|
||||||
|
if ! GIT_TERMINAL_PROMPT=0 "${fetch[@]}" >/dev/null 2>&1; then
|
||||||
|
echo "deploy: warning: couldn't fetch $upstream to check this checkout is up to date" >&2
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
behind=$(git -C "$repo" rev-list --count "HEAD..$upstream" 2>/dev/null) || return 0
|
||||||
|
((behind > 0)) || return 0
|
||||||
|
local msg="this checkout is $behind commit(s) behind $upstream, so it would deploy an older agent (git pull first)"
|
||||||
|
if ((allow_outdated || dry_run)); then
|
||||||
|
echo "deploy: warning: $msg" >&2
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
[[ -t 0 && -t 2 ]] || die "$msg; pass --allow-outdated to deploy it anyway"
|
||||||
|
local answer
|
||||||
|
read -r -p "deploy: $msg. Deploy anyway? [y/N] " answer
|
||||||
|
[[ $answer == [yY]* ]] || die "stopped; nothing was deployed"
|
||||||
|
}
|
||||||
|
|
||||||
mode=upgrade
|
mode=upgrade
|
||||||
dry_run=0
|
dry_run=0
|
||||||
|
allow_outdated=0
|
||||||
targets=()
|
targets=()
|
||||||
install_args=()
|
install_args=()
|
||||||
while (($#)); do
|
while (($#)); do
|
||||||
@@ -165,6 +203,7 @@ while (($#)); do
|
|||||||
--register) mode=register ;;
|
--register) mode=register ;;
|
||||||
--rotate) mode=rotate ;;
|
--rotate) mode=rotate ;;
|
||||||
--dry-run) dry_run=1 ;;
|
--dry-run) dry_run=1 ;;
|
||||||
|
--allow-outdated) allow_outdated=1 ;;
|
||||||
-h | --help) usage; exit 0 ;;
|
-h | --help) usage; exit 0 ;;
|
||||||
-*) usage >&2; exit 2 ;;
|
-*) usage >&2; exit 2 ;;
|
||||||
*) targets+=("$1") ;;
|
*) targets+=("$1") ;;
|
||||||
@@ -172,6 +211,7 @@ while (($#)); do
|
|||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
|
refuse_sudo
|
||||||
load_config
|
load_config
|
||||||
[[ -n $SERVER_URL ]] || die "SERVER_URL is not set in $CONF"
|
[[ -n $SERVER_URL ]] || die "SERVER_URL is not set in $CONF"
|
||||||
[[ $mode == upgrade || -n $DASHBOARD ]] || die "DASHBOARD is not set in $CONF"
|
[[ $mode == upgrade || -n $DASHBOARD ]] || die "DASHBOARD is not set in $CONF"
|
||||||
@@ -182,6 +222,8 @@ if ((${#targets[@]} == 0)); then
|
|||||||
((${#targets[@]})) || die "no hosts given and none in $HOSTS_FILE"
|
((${#targets[@]})) || die "no hosts given and none in $HOSTS_FILE"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
check_checkout
|
||||||
|
|
||||||
failed=()
|
failed=()
|
||||||
for t in "${targets[@]}"; do
|
for t in "${targets[@]}"; do
|
||||||
say "==> $t"
|
say "==> $t"
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ agent/deploy.sh --rotate web01 # new token, reinstall
|
|||||||
agent/deploy.sh --dry-run # show the plan
|
agent/deploy.sh --dry-run # show the plan
|
||||||
agent/deploy.sh -- --with-needrestart # pass options through to install.sh
|
agent/deploy.sh -- --with-needrestart # pass options through to install.sh
|
||||||
agent/deploy.sh -- --with-apt-update # refresh package lists before each report
|
agent/deploy.sh -- --with-apt-update # refresh package lists before each report
|
||||||
|
agent/deploy.sh --allow-outdated # deploy even if this checkout is behind upstream
|
||||||
```
|
```
|
||||||
|
|
||||||
For each host it copies the agent into a private temp directory and runs
|
For each host it copies the agent into a private temp directory and runs
|
||||||
@@ -78,6 +79,19 @@ in your terminal. A plain run is an upgrade: config and token stay as they
|
|||||||
are. You'll get sudo prompts on each host, and on the dashboard when
|
are. You'll get sudo prompts on each host, and on the dashboard when
|
||||||
registering, unless you have passwordless sudo.
|
registering, unless you have passwordless sudo.
|
||||||
|
|
||||||
|
Run it as yourself, not with `sudo`: it connects with your own SSH config and
|
||||||
|
keys and runs `sudo` on each host itself. Under `sudo`, ssh would read root's
|
||||||
|
config instead, so a custom `Port` or `IdentityFile` disappears and it tries
|
||||||
|
port 22. It refuses to run that way.
|
||||||
|
|
||||||
|
Before deploying, it fetches your checkout's upstream branch. If the checkout
|
||||||
|
is behind (you pulled on the dashboard host but not here, say), it would ship an
|
||||||
|
older agent than the server expects, and the dashboard would keep saying
|
||||||
|
"agent is older" after a deploy that looked fine. So it asks before going on,
|
||||||
|
or refuses when it isn't run from a terminal; `git pull` first, or pass
|
||||||
|
`--allow-outdated` to deploy it anyway. `--dry-run` only warns. If the fetch
|
||||||
|
fails (offline, say), it warns and deploys.
|
||||||
|
|
||||||
Both files are git-ignored. Host names default to each host's `hostname -s`;
|
Both files are git-ignored. Host names default to each host's `hostname -s`;
|
||||||
put `<ssh target> <name>` in `agent/hosts` to override. For a host your
|
put `<ssh target> <name>` in `agent/hosts` to override. For a host your
|
||||||
workstation can't reach directly (e.g. IPv6-only while you're on IPv4), give it
|
workstation can't reach directly (e.g. IPv6-only while you're on IPv4), give it
|
||||||
|
|||||||
@@ -189,6 +189,111 @@ class DeployTest(unittest.TestCase):
|
|||||||
self.assertNotEqual(proc.returncode, 0)
|
self.assertNotEqual(proc.returncode, 0)
|
||||||
self.assertIn("deploy.conf.example", proc.stderr)
|
self.assertIn("deploy.conf.example", proc.stderr)
|
||||||
|
|
||||||
|
def fake_uid(self, uid):
|
||||||
|
write_exec(os.path.join(self.tmp, "bin", "id"),
|
||||||
|
f'#!/bin/sh\n[ "$1" = -u ] && {{ echo {uid}; exit 0; }}\nexec /usr/bin/id "$@"\n')
|
||||||
|
|
||||||
|
def test_refuses_to_run_under_sudo(self):
|
||||||
|
self.fake_uid(0)
|
||||||
|
self.env["SUDO_USER"] = "patrick"
|
||||||
|
proc = self.deploy("web01.example.test")
|
||||||
|
self.assertNotEqual(proc.returncode, 0)
|
||||||
|
self.assertIn("don't run this with sudo", proc.stderr)
|
||||||
|
self.assertIn("Run it as patrick", proc.stderr)
|
||||||
|
self.assertFalse(os.path.exists(os.path.join(self.log, "ssh.log")), "nothing was contacted")
|
||||||
|
|
||||||
|
def test_root_login_without_sudo_is_allowed(self):
|
||||||
|
# Root's own SSH config is the one it means to use.
|
||||||
|
self.fake_uid(0)
|
||||||
|
self.env.pop("SUDO_USER", None)
|
||||||
|
self.assertEqual(self.deploy("web01.example.test").returncode, 0)
|
||||||
|
|
||||||
|
def test_sudo_user_set_but_not_root_is_allowed(self):
|
||||||
|
# e.g. a shell started from `sudo -u patrick -s`: not root, so ssh reads the right config.
|
||||||
|
self.fake_uid(1000)
|
||||||
|
self.env["SUDO_USER"] = "someone"
|
||||||
|
self.assertEqual(self.deploy("web01.example.test").returncode, 0)
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipUnless(shutil.which("bash") and shutil.which("tar") and shutil.which("git"),
|
||||||
|
"needs bash, tar and git")
|
||||||
|
class DeployCheckoutTest(DeployTest):
|
||||||
|
"""deploy.sh compares its checkout with the upstream branch before deploying."""
|
||||||
|
|
||||||
|
def git(self, *args, cwd=None):
|
||||||
|
env = {**os.environ, "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1",
|
||||||
|
"GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@example.test",
|
||||||
|
"GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "t@example.test"}
|
||||||
|
subprocess.run(["git", *args], cwd=cwd or self.tmp, env=env, check=True,
|
||||||
|
capture_output=True, timeout=30)
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
# self.tmp becomes a checkout of a local "origin" with agent/ committed.
|
||||||
|
self.origin = os.path.join(self.tmp, "origin.git")
|
||||||
|
self.git("init", "-q", "--bare", "-b", "main", self.origin)
|
||||||
|
with open(os.path.join(self.tmp, ".gitignore"), "w") as f:
|
||||||
|
f.write("/log/\n/homes/\n/remote-tmp/\n/bin/\n/origin.git/\n/other/\n*.db\n*.toml\n")
|
||||||
|
self.git("init", "-q", "-b", "main")
|
||||||
|
self.git("add", "-A")
|
||||||
|
self.git("commit", "-q", "-m", "agent")
|
||||||
|
self.git("remote", "add", "origin", self.origin)
|
||||||
|
self.git("push", "-q", "-u", "origin", "main")
|
||||||
|
for key in ("GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE"):
|
||||||
|
self.env.pop(key, None)
|
||||||
|
self.env.update(GIT_CONFIG_GLOBAL=os.devnull, GIT_CONFIG_NOSYSTEM="1")
|
||||||
|
|
||||||
|
def push_newer_commit(self):
|
||||||
|
other = os.path.join(self.tmp, "other")
|
||||||
|
self.git("clone", "-q", self.origin, other)
|
||||||
|
self.git("commit", "-q", "--allow-empty", "-m", "newer agent", cwd=other)
|
||||||
|
self.git("push", "-q", "origin", "main", cwd=other)
|
||||||
|
|
||||||
|
def test_up_to_date_checkout_deploys_quietly(self):
|
||||||
|
proc = self.deploy("web01.example.test")
|
||||||
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||||
|
self.assertNotIn("behind", proc.stderr)
|
||||||
|
|
||||||
|
def test_outdated_checkout_is_refused_without_a_terminal(self):
|
||||||
|
self.push_newer_commit()
|
||||||
|
proc = self.deploy("web01.example.test")
|
||||||
|
self.assertNotEqual(proc.returncode, 0)
|
||||||
|
self.assertIn("this checkout is 1 commit(s) behind origin/main", proc.stderr)
|
||||||
|
self.assertIn("--allow-outdated", proc.stderr)
|
||||||
|
self.assertFalse(os.path.exists(os.path.join(self.log, "ssh.log")), "nothing was deployed")
|
||||||
|
|
||||||
|
def test_allow_outdated_deploys_with_a_warning(self):
|
||||||
|
self.push_newer_commit()
|
||||||
|
proc = self.deploy("--allow-outdated", "web01.example.test")
|
||||||
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||||
|
self.assertIn("warning: this checkout is 1 commit(s) behind", proc.stderr)
|
||||||
|
self.installed("web01.example.test")
|
||||||
|
|
||||||
|
def test_dry_run_only_warns(self):
|
||||||
|
self.push_newer_commit()
|
||||||
|
proc = self.deploy("--dry-run", "web01.example.test")
|
||||||
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||||
|
self.assertIn("warning: this checkout is 1 commit(s) behind", proc.stderr)
|
||||||
|
|
||||||
|
def test_unreachable_upstream_only_warns(self):
|
||||||
|
shutil.rmtree(self.origin)
|
||||||
|
proc = self.deploy("web01.example.test")
|
||||||
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||||
|
self.assertIn("couldn't fetch origin/main", proc.stderr)
|
||||||
|
|
||||||
|
def test_ahead_of_upstream_is_fine(self):
|
||||||
|
self.git("commit", "-q", "--allow-empty", "-m", "local work")
|
||||||
|
proc = self.deploy("web01.example.test")
|
||||||
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||||
|
self.assertNotIn("behind", proc.stderr)
|
||||||
|
|
||||||
|
def test_no_upstream_skips_the_check(self):
|
||||||
|
self.git("branch", "-q", "--unset-upstream")
|
||||||
|
self.push_newer_commit()
|
||||||
|
proc = self.deploy("web01.example.test")
|
||||||
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||||
|
self.assertNotIn("behind", proc.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class PackagingTest(unittest.TestCase):
|
class PackagingTest(unittest.TestCase):
|
||||||
|
|||||||
Reference in new issue
Block a user