fix(installer): preserve multi-user service configuration

This commit is contained in:
bearyjd committed 2026-11-22 20:34:11 -05:00
1 parent 7d6e7fd411
commit c22bbd70ac
2 files changed
+162 -7

No files matched your search

+43 -6
View File
@@ -161,6 +161,7 @@ TS_RENAME_RECORD="$HOME/.codeman/tailscale-rename"
# tailscale_choose_mapping (path mode) or read back from the service file.
BIND_BASE_URL=""
EXISTING_BASE_URL=""
EXISTING_MULTIUSER="0"
EXISTING_VERSION=""
# Answer presets from flags. LAUNCH_PRESET: 1 = run now, 2 = service, 3 = do
@@ -2342,10 +2343,10 @@ xml_unescape() {
# Read the binding out of an already-installed service file, if any. A service
# file WITHOUT our CODEMAN_HOST line is a pre-1.8 install, which effectively
# ran loopback (the server default), so it reports 127.0.0.1. Also reads back
# the sub-path (CODEMAN_BASE_URL) and the port the service was written with,
# and the installed version, for the preflight summary and the re-run defaults.
# the service sub-path, port, multi-user mode, and installed version for the
# preflight summary and the re-run defaults.
read_existing_binding() {
EXISTING_FOUND="0"; EXISTING_HOST=""; EXISTING_PASSWORD=""; EXISTING_ACK="0"; EXISTING_BASE_URL=""
EXISTING_FOUND="0"; EXISTING_HOST=""; EXISTING_PASSWORD=""; EXISTING_ACK="0"; EXISTING_BASE_URL=""; EXISTING_MULTIUSER="0"
local unit="$HOME/.config/systemd/user/codeman-web.service"
local plist="$HOME/Library/LaunchAgents/com.codeman.web.plist"
local existing_port=""
@@ -2358,6 +2359,7 @@ read_existing_binding() {
[[ -n "$pwline" ]] && EXISTING_PASSWORD=$(systemd_env_unescape "$pwline")
grep -q '^Environment=CODEMAN_ALLOW_UNAUTHENTICATED_NETWORK=1' "$unit" && EXISTING_ACK="1"
EXISTING_BASE_URL=$(sed -n 's/^Environment=CODEMAN_BASE_URL=//p' "$unit" | head -1)
grep -Eq '^Environment="?CODEMAN_MULTIUSER=(1|true)"?$' "$unit" && EXISTING_MULTIUSER="1"
existing_port=$(sed -n 's/^Environment=CODEMAN_PORT=//p' "$unit" | head -1)
elif [[ -f "$plist" ]]; then
EXISTING_FOUND="1"
@@ -2367,6 +2369,13 @@ read_existing_binding() {
[[ -n "$pwraw" ]] && EXISTING_PASSWORD=$(xml_unescape "$pwraw")
grep -q '<key>CODEMAN_ALLOW_UNAUTHENTICATED_NETWORK</key>' "$plist" && EXISTING_ACK="1"
EXISTING_BASE_URL=$(awk '/<key>CODEMAN_BASE_URL<\/key>/{getline; print}' "$plist" | sed -n 's/.*<string>\(.*\)<\/string>.*/\1/p')
if awk '/<key>CODEMAN_MULTIUSER<\/key>/ {
sub(/.*<key>CODEMAN_MULTIUSER<\/key>[[:space:]]*/, "")
if ($0 == "") getline
print
}' "$plist" | grep -Eq '^[[:space:]]*<string>(1|true)</string>'; then
EXISTING_MULTIUSER="1"
fi
existing_port=$(awk '/<key>CODEMAN_PORT<\/key>/{getline; print}' "$plist" | sed -n 's/.*<string>\(.*\)<\/string>.*/\1/p')
fi
@@ -3636,6 +3645,11 @@ setup_launchd_service() {
<string>$(xml_escape "$CODEMAN_PORT")</string>"
fi
if [[ "$EXISTING_MULTIUSER" == "1" ]]; then
bind_plist+=$'\n'" <key>CODEMAN_MULTIUSER</key>
<string>1</string>"
fi
cat > "$agent_plist" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
@@ -3719,6 +3733,10 @@ setup_systemd_service() {
bind_env="${bind_env:+$bind_env$'\n'}Environment=CODEMAN_PORT=$CODEMAN_PORT"
fi
if [[ "$EXISTING_MULTIUSER" == "1" ]]; then
bind_env="${bind_env:+$bind_env$'\n'}Environment=CODEMAN_MULTIUSER=1"
fi
# Create service file
cat > "$service_file" << EOF
[Unit]
@@ -4416,9 +4434,28 @@ print_done_screen() {
echo -e " ${DIM}(the same variables apply to: codeman web -d)${NC}"
fi
fi
echo -e " ${YELLOW}${BOLD}Multi-user setup${NC} Adding accounts does not enable multi-user mode."
echo -e " Stop the running server before adding the first admin with ${CYAN}codeman users add <name> --admin${NC}."
echo -e " Start with ${CYAN}codeman web --multiuser${NC}, or set ${CYAN}CODEMAN_MULTIUSER=1${NC} in the service configuration and restart."
if [[ "$EXISTING_MULTIUSER" != "1" ]]; then
echo -e " ${BOLD}Users${NC} Create the first admin with ${CYAN}codeman users add <name> --admin${NC} before enabling multi-user mode."
echo -e " To start manually, run ${CYAN}$(start_command_hint) --multiuser${NC}"
if [[ "$launch" == "2" ]]; then
case "$svc" in
systemd)
if [[ -f "$unit" ]]; then
echo -e " In ${CYAN}~/.config/systemd/user/codeman-web.service${NC}, add ${CYAN}Environment=CODEMAN_MULTIUSER=1${NC} under [Service]."
echo -e " Then run ${CYAN}systemctl --user daemon-reload && systemctl --user restart codeman-web${NC}"
fi
;;
launchd)
if [[ -f "$agent_plist" ]]; then
echo -e " In ${CYAN}~/Library/LaunchAgents/com.codeman.web.plist${NC}, add this entry to EnvironmentVariables."
echo -e " ${CYAN}<key>CODEMAN_MULTIUSER</key><string>1</string>${NC}"
echo -e " Then run ${CYAN}launchctl unload ~/Library/LaunchAgents/com.codeman.web.plist${NC}"
echo -e " followed by ${CYAN}launchctl load ~/Library/LaunchAgents/com.codeman.web.plist${NC}"
fi
;;
esac
fi
fi
if [[ -f "$INSTALL_DIR/$SOURCE_MARKER" && ! -d "$INSTALL_DIR/.git" ]]; then
echo -e " ${BOLD}Update${NC} re-run the install line ${DIM}(installed without git, so the in-app updater is off)${NC}"
else
+119 -1
View File
@@ -14,7 +14,7 @@ import { describe, expect, it } from 'vitest';
import { spawnSync } from 'node:child_process';
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { STOCK_CLIS } from '../src/config/cli-registry/stock.js';
import { CLI_INSTALLED_MARKER } from '../src/utils/cli-executable-resolver.js';
@@ -840,6 +840,7 @@ describe('install.sh review fixes for #460', () => {
}
const done = fn('print_done_screen', '\nupdate() {');
expect(done).toContain('$(start_command_hint)');
expect(done).toContain('$(start_command_hint) --multiuser');
expect(done).not.toMatch(/CODEMAN_HOST=0\.0\.0\.0 codeman web/);
});
@@ -926,6 +927,123 @@ describe('install.sh review fixes for #460', () => {
});
});
describe('install.sh multi-user guidance and reconfiguration', () => {
const unit = '.config/systemd/user/codeman-web.service';
const agent = 'Library/LaunchAgents/com.codeman.web.plist';
function drive(script: string, existing?: { path: string; content: string }) {
const home = mkdtempSync(join(tmpdir(), 'codeman-multiuser-service-'));
try {
if (existing) {
const path = join(home, existing.path);
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, existing.content);
}
const env = {
...process.env,
HOME: home,
CODEMAN_INSTALL_DIR: join(home, 'app'),
CODEMAN_NONINTERACTIVE: '1',
CODEMAN_INSTALL_SH_LIB: '1',
};
const driver = `
. "$1"
unset CODEMAN_HOST CODEMAN_PORT CODEMAN_PASSWORD CODEMAN_TAILSCALE
systemctl() { return 0; }
loginctl() { return 0; }
launchctl() { return 0; }
sleep() { :; }
verify_systemd_active() { return 0; }
codeman_answers_locally() { return 1; }
detect_tailscale_serve_url() { return 1; }
detect_lan_ip() { echo 192.0.2.1; }
print_qr() { :; }
check_cloudflared() { return 1; }
detect_all_clis() { CLI_FOUND_COUNT=1; }
${script}
`;
const result = spawnSync('bash', ['-c', driver, 'bash', INSTALL_SH], {
encoding: 'utf-8',
timeout: 30_000,
env,
});
expect(result.status, result.stderr).toBe(0);
return result.stdout.replace(/\x1b\[[0-9;]*m/g, '');
} finally {
rmSync(home, { recursive: true, force: true });
}
}
it('prints a binding-aware manual command after the aligned first-admin step', () => {
const output = drive(`
BIND_HOST=0.0.0.0; BIND_PASSWORD=x; BIND_BASE_URL=/codeman; CODEMAN_PORT=4000
print_done_screen 3 true
`);
expect(output).toMatch(/ Users .*codeman users add <name> --admin/);
expect(output).toContain(
"CODEMAN_HOST=0.0.0.0 CODEMAN_PASSWORD='<your-password>' CODEMAN_BASE_URL=/codeman CODEMAN_PORT=4000 codeman web --multiuser"
);
expect(output.indexOf('codeman users add')).toBeLessThan(output.indexOf('codeman web --multiuser'));
expect(output).not.toMatch(/stop the running server|daemon-reload|launchctl (load|unload)/i);
});
it('prints the exact systemd edit and daemon reload', () => {
const output = drive('read_existing_binding; print_done_screen "" ""', { path: unit, content: '[Service]\n' });
expect(output).toContain('~/.config/systemd/user/codeman-web.service');
expect(output).toContain('Environment=CODEMAN_MULTIUSER=1');
expect(output).toContain('[Service]');
expect(output).toContain('systemctl --user daemon-reload && systemctl --user restart codeman-web');
expect(output).not.toContain('EnvironmentVariables');
});
it('prints the exact LaunchAgent edit and unload/load sequence', () => {
const output = drive('read_existing_binding; print_done_screen "" ""', { path: agent, content: '<plist/>\n' });
const guidance = output.split(' Users ')[1] ?? '';
expect(guidance).toContain('EnvironmentVariables');
expect(guidance).toContain('<key>CODEMAN_MULTIUSER</key><string>1</string>');
const unload = 'launchctl unload ~/Library/LaunchAgents/com.codeman.web.plist';
const load = 'launchctl load ~/Library/LaunchAgents/com.codeman.web.plist';
expect(guidance.indexOf(unload)).toBeGreaterThan(-1);
expect(guidance.indexOf(load)).toBeGreaterThan(guidance.indexOf(unload));
expect(guidance).not.toContain('daemon-reload');
});
it.each([
['systemd', unit, 'Environment=CODEMAN_MULTIUSER=1', true],
['systemd', unit, 'Environment="CODEMAN_MULTIUSER=true"', true],
['systemd', unit, 'Environment=CODEMAN_MULTIUSER=0', false],
['systemd', unit, '', false],
['launchd', agent, '<key>CODEMAN_MULTIUSER</key><string>1</string>', true],
['launchd', agent, '<key>CODEMAN_MULTIUSER</key>\n<string>true</string>', true],
['launchd', agent, '<key>CODEMAN_MULTIUSER</key>\n<string>0</string>', false],
['launchd', agent, '', false],
])('reconfigures %s with existing setting %s %s (enabled %s)', (supervisor, path, setting, enabled) => {
if (supervisor === 'launchd' && existsSync('/Library/LaunchDaemons/com.codeman.web.plist')) return;
const setup = supervisor === 'systemd' ? 'setup_systemd_service' : 'setup_launchd_service';
const output = drive(
`
parse_flags --port 4000 --service
choose_network_binding >/dev/null
${setup} >/dev/null
read_existing_binding
print_done_screen "" ""
echo 'SERVICE FILE'
sed -n '/CODEMAN_MULTIUSER/{p;n;p;}' "$HOME/${path}"
`,
{ path, content: setting + '\n' }
);
expect(output).toContain('http://localhost:4000');
const service = output.split('SERVICE FILE\n')[1];
if (enabled) {
expect(output).not.toContain('codeman users add');
expect(service).toMatch(/Environment=CODEMAN_MULTIUSER=1|<key>CODEMAN_MULTIUSER<\/key>\s*<string>1<\/string>/);
} else {
expect(output).toContain('codeman users add');
expect(service).toBe('');
}
});
});
it('install.sh never contains the unauthenticated tunnel opt-in', () => {
expect(SOURCE).not.toContain('CODEMAN_ALLOW_UNAUTHENTICATED_TUNNEL');
expect(SOURCE).not.toContain('--allow-unauthenticated-tunnel');