Stop deploy.sh running under sudo or from an outdated checkout
Under sudo, ssh reads root's config and keys, so a custom Port silently disappears and the deploy fails with "connect to host ... port 22: Connection refused". deploy.sh needs no root locally (install.sh runs under sudo on each host), so it now refuses when run as root via sudo, naming the user to run it as. A real root login is still allowed. Deploying from a checkout behind its upstream ships an older agent than the server expects, and the dashboard keeps saying "agent is older" after a deploy that looked fine. deploy.sh now fetches the upstream (20s limit) and, if the checkout is behind, asks on a terminal or refuses otherwise; --allow-outdated deploys anyway and --dry-run only warns. A failed fetch warns and carries on; no checkout or no upstream skips the check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4f45187ca0
commit
c3ed3c026b
2 files changed
+148
-1
No files matched your search
@@ -189,6 +189,111 @@ class DeployTest(unittest.TestCase):
|
||||
self.assertNotEqual(proc.returncode, 0)
|
||||
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):
|
||||
|
||||
Reference in new issue
Block a user