mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-30 08:39:32 +01:00
Merge #847: fix(installer): explain how to enable multi-user mode (@bearyjd)
This commit is contained in:
commit
1d889468df
2 files changed
+221
-9
No files matched your search
+72
-8
@@ -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
|
||||
@@ -2339,26 +2340,50 @@ xml_unescape() {
|
||||
printf '%s' "$1" | sed -e 's/</</g' -e 's/>/>/g' -e 's/&/\&/g'
|
||||
}
|
||||
|
||||
# Read one assignment from the unit's Environment= directives. Each line can
|
||||
# hold several assignments, quoted as whole words. Later assignments and empty
|
||||
# directives override earlier values. The unit is read as data.
|
||||
read_systemd_environment() {
|
||||
awk -v key="$2=" '
|
||||
/^[[:space:]]*Environment[[:space:]]*=/ {
|
||||
sub(/^[[:space:]]*Environment[[:space:]]*=[[:space:]]*/, "")
|
||||
if ($0 == "") value = ""
|
||||
while (match($0, /^("([^"\\]|\\.)*"|\047([^\047\\]|\\.)*\047|[^[:space:]"\047][^[:space:]]*)([[:space:]]|$)/)) {
|
||||
word = substr($0, 1, RLENGTH)
|
||||
$0 = substr($0, RLENGTH + 1)
|
||||
sub(/^[[:space:]]+/, "")
|
||||
sub(/[[:space:]]$/, "", word)
|
||||
if (word ~ /^["\047]/) word = substr(word, 2, length(word) - 2)
|
||||
if (index(word, key) == 1) value = substr(word, length(key) + 1)
|
||||
}
|
||||
}
|
||||
END { print value }
|
||||
' "$1"
|
||||
}
|
||||
|
||||
# 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=""
|
||||
|
||||
if [[ -f "$unit" ]]; then
|
||||
EXISTING_FOUND="1"
|
||||
EXISTING_HOST=$(sed -n 's/^Environment=CODEMAN_HOST=//p' "$unit" | head -1)
|
||||
EXISTING_HOST=$(read_systemd_environment "$unit" CODEMAN_HOST)
|
||||
local pwline
|
||||
pwline=$(sed -n 's/^Environment="CODEMAN_PASSWORD=\(.*\)"$/\1/p' "$unit" | head -1)
|
||||
pwline=$(read_systemd_environment "$unit" CODEMAN_PASSWORD)
|
||||
[[ -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)
|
||||
existing_port=$(sed -n 's/^Environment=CODEMAN_PORT=//p' "$unit" | head -1)
|
||||
[[ "$(read_systemd_environment "$unit" CODEMAN_ALLOW_UNAUTHENTICATED_NETWORK)" == "1" ]] && EXISTING_ACK="1"
|
||||
EXISTING_BASE_URL=$(read_systemd_environment "$unit" CODEMAN_BASE_URL)
|
||||
case "$(read_systemd_environment "$unit" CODEMAN_MULTIUSER)" in
|
||||
1|true) EXISTING_MULTIUSER="1" ;;
|
||||
esac
|
||||
existing_port=$(read_systemd_environment "$unit" CODEMAN_PORT)
|
||||
elif [[ -f "$plist" ]]; then
|
||||
EXISTING_FOUND="1"
|
||||
EXISTING_HOST=$(awk '/<key>CODEMAN_HOST<\/key>/{getline; print}' "$plist" | sed -n 's/.*<string>\(.*\)<\/string>.*/\1/p')
|
||||
@@ -2367,6 +2392,14 @@ 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:]]*/, "")
|
||||
while ($0 ~ /^[[:space:]]*$/ && getline > 0) {}
|
||||
enabled = ($0 ~ /^[[:space:]]*<string>(1|true)<\/string>/)
|
||||
}
|
||||
END { exit !enabled }' "$plist"; 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 +3669,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 +3757,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,6 +4458,28 @@ print_done_screen() {
|
||||
echo -e " ${DIM}(the same variables apply to: codeman web -d)${NC}"
|
||||
fi
|
||||
fi
|
||||
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
|
||||
|
||||
@@ -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,153 @@ 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=1 ', true],
|
||||
['systemd', unit, 'Environment="NOTE=two words" CODEMAN_MULTIUSER=1 NODE_ENV=production', true],
|
||||
['systemd', unit, "Environment='CODEMAN_MULTIUSER=1' NODE_ENV=production", true],
|
||||
['systemd', unit, 'Environment=CODEMAN_MULTIUSER=1 CODEMAN_MULTIUSER=0', false],
|
||||
['systemd', unit, 'Environment=CODEMAN_MULTIUSER=1\nEnvironment=CODEMAN_MULTIUSER=false', false],
|
||||
['systemd', unit, 'Environment=CODEMAN_MULTIUSER=1\nEnvironment=', false],
|
||||
['systemd', unit, 'Environment="NOTE=some text CODEMAN_MULTIUSER=1 more text"', false],
|
||||
['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 \n<string>1</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('preserves a combined systemd binding without reintroducing a disabled multi-user assignment', () => {
|
||||
const output = drive(
|
||||
`
|
||||
parse_flags --port 4000 --service
|
||||
choose_network_binding >/dev/null
|
||||
setup_systemd_service >/dev/null
|
||||
cat "$HOME/${unit}"
|
||||
`,
|
||||
{
|
||||
path: unit,
|
||||
content: [
|
||||
'[Service]',
|
||||
'Environment=CODEMAN_HOST=0.0.0.0 CODEMAN_MULTIUSER=1 "CODEMAN_PASSWORD=test password"',
|
||||
'Environment=CODEMAN_MULTIUSER=0',
|
||||
].join('\n'),
|
||||
}
|
||||
);
|
||||
expect(output).toMatch(/^Environment=CODEMAN_HOST=0\.0\.0\.0$/m);
|
||||
expect(output).toContain('Environment="CODEMAN_PASSWORD=test password"');
|
||||
expect(output).not.toContain('CODEMAN_MULTIUSER');
|
||||
});
|
||||
});
|
||||
|
||||
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');
|
||||
|
||||
Reference in new issue
Block a user