|
9 | 9 | * |
10 | 10 | * Both need root. Without it, the command prints what it would change and the |
11 | 11 | * `sudo` line to run. It is safe to run again; it skips what is already done. |
| 12 | + * |
| 13 | + * It cannot fix a server that is itself running as root. Chrome refuses its |
| 14 | + * sandbox for uid 0, so that case warns instead of reporting the host ready. |
| 15 | + * `sudo` from a non-root user is only root for this command and still reports |
| 16 | + * ready when the checks pass. |
12 | 17 | */ |
13 | 18 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
14 | 19 | import * as Console from "effect/Console"; |
@@ -99,90 +104,98 @@ const installedBrowser = Effect.fn("browserSetup.installedBrowser")(function* (b |
99 | 104 | return Option.none<string>(); |
100 | 105 | }); |
101 | 106 |
|
102 | | -const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe( |
103 | | - Command.withDescription( |
104 | | - "Set up this Linux host for T3's browser: allow Chrome's sandbox and install its libraries.", |
105 | | - ), |
106 | | - Command.withHandler(({ baseDir }) => |
107 | | - Effect.gen(function* () { |
108 | | - if ((yield* HostProcess.Platform) !== "linux") { |
109 | | - return yield* Console.log("Nothing to set up: T3's browser runs as is on this system."); |
110 | | - } |
111 | | - const fs = yield* FileSystem.FileSystem; |
112 | | - const isRoot = (yield* HostProcess.UserId) === 0; |
113 | | - const setupCommand = yield* resolveRootCliCommand(PreviewBrowserHost.SETUP_SUBCOMMAND); |
| 107 | +/** Runs `t3 browser setup` for an explicit or default T3 home. */ |
| 108 | +export const runBrowserSetup = Effect.fn("browserSetup")(function* ( |
| 109 | + baseDir: Option.Option<string>, |
| 110 | +) { |
| 111 | + if ((yield* HostProcess.Platform) !== "linux") { |
| 112 | + return yield* Console.log("Nothing to set up: T3's browser runs as is on this system."); |
| 113 | + } |
| 114 | + const fs = yield* FileSystem.FileSystem; |
| 115 | + const env = yield* HostProcess.Environment; |
| 116 | + const uid = yield* HostProcess.UserId; |
| 117 | + const isRoot = uid === 0; |
| 118 | + const warnsAboutRoot = PreviewBrowserHost.setupWarnsAboutRoot({ uid, env }); |
114 | 119 |
|
115 | | - const needsProfile = yield* PreviewBrowserHost.sandboxBlocked; |
116 | | - const browser = yield* installedBrowser(yield* setupBaseDir(baseDir)); |
117 | | - const missing = Option.isSome(browser) |
118 | | - ? yield* PreviewBrowserHost.missingLibraries(browser.value) |
119 | | - : []; |
120 | | - const hasApt = yield* fs.exists("/usr/bin/apt-get").pipe(Effect.orElseSucceed(() => false)); |
| 120 | + const needsProfile = yield* PreviewBrowserHost.sandboxBlocked; |
| 121 | + const browser = yield* installedBrowser(yield* setupBaseDir(baseDir)); |
| 122 | + const missing = Option.isSome(browser) |
| 123 | + ? yield* PreviewBrowserHost.missingLibraries(browser.value) |
| 124 | + : []; |
| 125 | + const hasApt = yield* fs.exists("/usr/bin/apt-get").pipe(Effect.orElseSucceed(() => false)); |
| 126 | + const reportReady = (message: string) => |
| 127 | + Console.log(warnsAboutRoot ? PreviewBrowserHost.ROOT_SANDBOX_MESSAGE : message); |
121 | 128 |
|
122 | | - if (!needsProfile && missing.length === 0) { |
123 | | - return yield* Console.log( |
124 | | - Option.isSome(browser) |
125 | | - ? "This host is ready for T3's browser." |
126 | | - : "Chrome's sandbox is allowed here. T3's browser installs on first use; if it then reports missing libraries, run this again.", |
127 | | - ); |
128 | | - } |
| 129 | + if (!needsProfile && missing.length === 0) { |
| 130 | + return yield* reportReady( |
| 131 | + Option.isSome(browser) |
| 132 | + ? "This host is ready for T3's browser." |
| 133 | + : "Chrome's sandbox is allowed here. T3's browser installs on first use; if it then reports missing libraries, run this again.", |
| 134 | + ); |
| 135 | + } |
129 | 136 |
|
130 | | - if (!isRoot) { |
131 | | - if (needsProfile) { |
132 | | - yield* Console.log( |
133 | | - `This host blocks the sandbox T3's browser runs in. Setup installs an AppArmor profile at ${PreviewBrowserHost.APPARMOR_PROFILE_PATH} that allows it.`, |
134 | | - ); |
135 | | - } |
136 | | - if (missing.length > 0) { |
137 | | - yield* Console.log(`T3's browser is missing ${missing.join(", ")}; setup installs them.`); |
138 | | - } |
139 | | - return yield* Console.log(`\nThis needs root. Run:\n\n ${setupCommand}\n`); |
140 | | - } |
| 137 | + if (!isRoot) { |
| 138 | + const setupCommand = yield* resolveRootCliCommand(PreviewBrowserHost.SETUP_SUBCOMMAND); |
| 139 | + if (needsProfile) { |
| 140 | + yield* Console.log( |
| 141 | + `This host blocks the sandbox T3's browser runs in. Setup installs an AppArmor profile at ${PreviewBrowserHost.APPARMOR_PROFILE_PATH} that allows it.`, |
| 142 | + ); |
| 143 | + } |
| 144 | + if (missing.length > 0) { |
| 145 | + yield* Console.log(`T3's browser is missing ${missing.join(", ")}; setup installs them.`); |
| 146 | + } |
| 147 | + return yield* Console.log(`\nThis needs root. Run:\n\n ${setupCommand}\n`); |
| 148 | + } |
141 | 149 |
|
142 | | - if (needsProfile) { |
143 | | - yield* fs |
144 | | - .writeFileString( |
145 | | - PreviewBrowserHost.APPARMOR_PROFILE_PATH, |
146 | | - PreviewBrowserHost.APPARMOR_PROFILE, |
147 | | - ) |
148 | | - .pipe( |
149 | | - Effect.mapError( |
150 | | - (cause) => |
151 | | - new BrowserSetupStepError({ |
152 | | - step: `write ${PreviewBrowserHost.APPARMOR_PROFILE_PATH}`, |
153 | | - detail: cause.message, |
154 | | - }), |
155 | | - ), |
156 | | - ); |
157 | | - yield* runStep("load the AppArmor profile", "apparmor_parser", [ |
158 | | - "-r", |
159 | | - PreviewBrowserHost.APPARMOR_PROFILE_PATH, |
160 | | - ]); |
161 | | - yield* Console.log("Allowed Chrome's sandbox for T3's browser."); |
162 | | - } |
| 150 | + if (needsProfile) { |
| 151 | + yield* fs |
| 152 | + .writeFileString( |
| 153 | + PreviewBrowserHost.APPARMOR_PROFILE_PATH, |
| 154 | + PreviewBrowserHost.APPARMOR_PROFILE, |
| 155 | + ) |
| 156 | + .pipe( |
| 157 | + Effect.mapError( |
| 158 | + (cause) => |
| 159 | + new BrowserSetupStepError({ |
| 160 | + step: `write ${PreviewBrowserHost.APPARMOR_PROFILE_PATH}`, |
| 161 | + detail: cause.message, |
| 162 | + }), |
| 163 | + ), |
| 164 | + ); |
| 165 | + yield* runStep("load the AppArmor profile", "apparmor_parser", [ |
| 166 | + "-r", |
| 167 | + PreviewBrowserHost.APPARMOR_PROFILE_PATH, |
| 168 | + ]); |
| 169 | + yield* Console.log("Allowed Chrome's sandbox for T3's browser."); |
| 170 | + } |
163 | 171 |
|
164 | | - if (missing.length > 0) { |
165 | | - if (!hasApt) { |
166 | | - return yield* Console.log( |
167 | | - `T3's browser is missing ${missing.join(", ")}. Install them with your package manager, then run this again.`, |
168 | | - ); |
169 | | - } |
170 | | - yield* runStep("refresh the package lists", "apt-get", ["update"]); |
171 | | - const packages = yield* Effect.forEach(PreviewBrowserHost.DEBIAN_PACKAGES, (names) => |
172 | | - Effect.findFirst(names, aptOffers).pipe(Effect.map(Option.getOrElse(() => names[0]!))), |
173 | | - ); |
174 | | - yield* runStep("install the browser's libraries", "apt-get", [ |
175 | | - "install", |
176 | | - "-y", |
177 | | - "--no-install-recommends", |
178 | | - ...packages, |
179 | | - ]); |
180 | | - yield* Console.log("Installed the browser's libraries."); |
181 | | - } |
| 172 | + if (missing.length > 0) { |
| 173 | + if (!hasApt) { |
| 174 | + return yield* Console.log( |
| 175 | + `T3's browser is missing ${missing.join(", ")}. Install them with your package manager, then run this again.`, |
| 176 | + ); |
| 177 | + } |
| 178 | + yield* runStep("refresh the package lists", "apt-get", ["update"]); |
| 179 | + const packages = yield* Effect.forEach(PreviewBrowserHost.DEBIAN_PACKAGES, (names) => |
| 180 | + Effect.findFirst(names, aptOffers).pipe(Effect.map(Option.getOrElse(() => names[0]!))), |
| 181 | + ); |
| 182 | + yield* runStep("install the browser's libraries", "apt-get", [ |
| 183 | + "install", |
| 184 | + "-y", |
| 185 | + "--no-install-recommends", |
| 186 | + ...packages, |
| 187 | + ]); |
| 188 | + yield* Console.log("Installed the browser's libraries."); |
| 189 | + } |
| 190 | + |
| 191 | + yield* reportReady("This host is ready for T3's browser."); |
| 192 | +}); |
182 | 193 |
|
183 | | - yield* Console.log("This host is ready for T3's browser."); |
184 | | - }), |
| 194 | +const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe( |
| 195 | + Command.withDescription( |
| 196 | + "Set up this Linux host for T3's browser: allow Chrome's sandbox and install its libraries.", |
185 | 197 | ), |
| 198 | + Command.withHandler(({ baseDir }) => runBrowserSetup(baseDir)), |
186 | 199 | ); |
187 | 200 |
|
188 | 201 | export const browserCommand = Command.make("browser").pipe( |
|
0 commit comments