Skip to content

Commit 811d75c

Browse files
committed
fix: repair DB/Redis setup commands when running as root
Joining the SUDO preamble with && caused the entire command chain to short-circuit when EUID=0 (root), since [ -ne 0 ] exits 1. Fixed by separating the preamble with ; so the actual install commands always run. Also replaced $SUDO -u postgres with a PG_RUN variable that uses runuser for root and sudo -u for non-root.
1 parent a260472 commit 811d75c

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

src/cli/commands/setup.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,42 +112,43 @@ function sh(s: string): string {
112112
}
113113

114114
function buildDbSetupCommand(db: DatabaseConfig): string {
115-
const sudo = 'SUDO=""; [ "$EUID" -ne 0 ] && SUDO="sudo"';
115+
// preamble uses ';' so the conditional SUDO/PG_RUN assignments don't break
116+
// the '&&' chain when running as root (EUID=0 makes [ -ne 0 ] exit 1)
117+
const preamble = 'SUDO=""; [ "$EUID" -ne 0 ] && SUDO="sudo"; PG_RUN="runuser -u postgres --"; [ "$EUID" -ne 0 ] && PG_RUN="sudo -u postgres"';
116118

117119
if (db.type === 'postgres') {
118120
const createUser = db.password
119-
? `$SUDO -u postgres psql -c "CREATE USER \\"${sh(db.user)}\\" WITH PASSWORD '${sh(db.password)}';" 2>/dev/null || true`
120-
: `$SUDO -u postgres psql -c "CREATE USER \\"${sh(db.user)}\\";" 2>/dev/null || true`;
121-
return [
122-
sudo,
121+
? `$PG_RUN psql -c "CREATE USER \\"${sh(db.user)}\\" WITH PASSWORD '${sh(db.password)}';" 2>/dev/null || true`
122+
: `$PG_RUN psql -c "CREATE USER \\"${sh(db.user)}\\";" 2>/dev/null || true`;
123+
const commands = [
123124
'$SUDO apt-get install -y postgresql postgresql-contrib',
124125
'$SUDO systemctl enable postgresql',
125126
'$SUDO systemctl start postgresql',
126127
createUser,
127-
`$SUDO -u postgres psql -tc "SELECT 1 FROM pg_database WHERE datname='${sh(db.name)}'" | grep -q 1 || $SUDO -u postgres createdb -O "${sh(db.user)}" "${sh(db.name)}"`,
128-
].join(' && ');
128+
`$PG_RUN psql -tc "SELECT 1 FROM pg_database WHERE datname='${sh(db.name)}'" | grep -q 1 || $PG_RUN createdb -O "${sh(db.user)}" "${sh(db.name)}"`,
129+
];
130+
return `${preamble}; ${commands.join(' && ')}`;
129131
}
130132

131133
if (db.type === 'mysql') {
132134
const pwClause = db.password ? `IDENTIFIED BY '${sh(db.password)}'` : '';
133-
return [
134-
sudo,
135+
const commands = [
135136
'$SUDO apt-get install -y mysql-server',
136137
'$SUDO systemctl enable mysql',
137138
'$SUDO systemctl start mysql',
138139
`$SUDO mysql -e "CREATE USER IF NOT EXISTS '${sh(db.user)}'@'localhost' ${pwClause};"`,
139140
`$SUDO mysql -e "CREATE DATABASE IF NOT EXISTS \\\`${sh(db.name)}\\\`;"`,
140141
`$SUDO mysql -e "GRANT ALL PRIVILEGES ON \\\`${sh(db.name)}\\\`.* TO '${sh(db.user)}'@'localhost';"`,
141142
`$SUDO mysql -e "FLUSH PRIVILEGES;"`,
142-
].join(' && ');
143+
];
144+
return `${preamble}; ${commands.join(' && ')}`;
143145
}
144146

145147
if (db.type === 'mongodb') {
146148
const createUser = db.password
147149
? `mongosh "${sh(db.name)}" --eval "db.createUser({user:'${sh(db.user)}',pwd:'${sh(db.password)}',roles:[{role:'readWrite',db:'${sh(db.name)}'}]})" 2>/dev/null || true`
148150
: `mongosh "${sh(db.name)}" --eval "db.createUser({user:'${sh(db.user)}',roles:[{role:'readWrite',db:'${sh(db.name)}'}]})" 2>/dev/null || true`;
149-
return [
150-
sudo,
151+
const commands = [
151152
'if ! command -v mongod &>/dev/null; then ' +
152153
'curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | $SUDO gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg; ' +
153154
'echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | $SUDO tee /etc/apt/sources.list.d/mongodb-org-7.0.list; ' +
@@ -156,16 +157,16 @@ function buildDbSetupCommand(db: DatabaseConfig): string {
156157
'$SUDO systemctl enable mongod',
157158
'$SUDO systemctl start mongod',
158159
createUser,
159-
].join(' && ');
160+
];
161+
return `${preamble}; ${commands.join(' && ')}`;
160162
}
161163

162164
return 'true';
163165
}
164166

165167
function buildRedisSetupCommand(redis: RedisConfig): string {
166-
const sudo = 'SUDO=""; [ "$EUID" -ne 0 ] && SUDO="sudo"';
168+
const preamble = 'SUDO=""; [ "$EUID" -ne 0 ] && SUDO="sudo"';
167169
const parts = [
168-
sudo,
169170
'$SUDO apt-get install -y redis-server',
170171
'$SUDO systemctl enable redis-server',
171172
'$SUDO systemctl start redis-server',
@@ -179,5 +180,5 @@ function buildRedisSetupCommand(redis: RedisConfig): string {
179180
);
180181
}
181182

182-
return parts.join(' && ');
183+
return `${preamble}; ${parts.join(' && ')}`;
183184
}

0 commit comments

Comments
 (0)