@blogic-cz/agent-tools 0.14.5 → 0.14.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -126,83 +126,105 @@ export const runWithProfilePrerequisites = <A, E, CommandError>(
|
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
return Effect.gen(function* () {
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
const shouldTryDirect = options?.tryWithoutPrerequisites === true;
|
|
130
|
+
|
|
131
|
+
const tryDirect = () => effect.pipe(Effect.result);
|
|
132
|
+
|
|
133
|
+
if (shouldTryDirect) {
|
|
134
|
+
const directResult = yield* tryDirect();
|
|
131
135
|
if (Result.isSuccess(directResult)) {
|
|
132
136
|
return directResult.success;
|
|
133
137
|
}
|
|
134
138
|
}
|
|
135
139
|
|
|
136
|
-
const
|
|
140
|
+
const prerequisiteResult = yield* Effect.gen(function* () {
|
|
141
|
+
const startedDrivers: Array<{ driver: ResolvedVpnDriver; cooldownMs: number }> = [];
|
|
137
142
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
143
|
+
for (const prerequisite of vpnPrerequisites) {
|
|
144
|
+
const vpnConfig = config.vpns?.[prerequisite.key];
|
|
145
|
+
if (!vpnConfig) {
|
|
146
|
+
return yield* new PrerequisiteRunError({
|
|
147
|
+
message: `VPN prerequisite "${prerequisite.key}" is not defined.`,
|
|
148
|
+
hint: `Add vpns.${prerequisite.key} to agent-tools.json5 or remove the prerequisite.`,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
146
151
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
152
|
+
const driverResolution = resolveVpnDriverConfig(vpnConfig);
|
|
153
|
+
if (!driverResolution.success) {
|
|
154
|
+
return yield* new PrerequisiteRunError({
|
|
155
|
+
message: driverResolution.error,
|
|
156
|
+
hint: driverResolution.hint,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
154
159
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
+
const driver = driverResolution.driver;
|
|
161
|
+
const wasConnected = yield* isVpnConnected(driver, runCommand);
|
|
162
|
+
if (wasConnected) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
160
165
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
166
|
+
const startCommand = makeVpnCommand(driver, "start");
|
|
167
|
+
const startResult = yield* runCommand(startCommand.command, startCommand.label).pipe(
|
|
168
|
+
Effect.mapError(
|
|
169
|
+
() =>
|
|
170
|
+
new PrerequisiteRunError({
|
|
171
|
+
message: `Failed to start VPN prerequisite "${prerequisite.key}".`,
|
|
172
|
+
hint: missingVpnToolHint(driver),
|
|
173
|
+
}),
|
|
174
|
+
),
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
if (startResult.exitCode !== 0) {
|
|
178
|
+
const stderr = startResult.stderr.trim();
|
|
179
|
+
return yield* new PrerequisiteRunError({
|
|
180
|
+
message:
|
|
181
|
+
stderr !== "" ? stderr : `Failed to start VPN prerequisite "${prerequisite.key}".`,
|
|
182
|
+
hint: missingVpnToolHint(driver),
|
|
183
|
+
});
|
|
184
|
+
}
|
|
180
185
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
186
|
+
const ready = yield* waitForVpn(driver, vpnConfig.connectTimeoutMs ?? 30000, runCommand);
|
|
187
|
+
if (!ready) {
|
|
188
|
+
return yield* new PrerequisiteRunError({
|
|
189
|
+
message: `VPN prerequisite "${prerequisite.key}" did not connect within timeout.`,
|
|
190
|
+
hint: missingVpnToolHint(driver),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
188
193
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
194
|
+
const cleanup = prerequisite.cleanup ?? vpnConfig.defaultCleanup ?? "stop-if-started";
|
|
195
|
+
if (cleanup === "stop-if-started") {
|
|
196
|
+
startedDrivers.push({ driver, cooldownMs: vpnConfig.cooldownMs ?? 0 });
|
|
197
|
+
}
|
|
192
198
|
}
|
|
193
|
-
}
|
|
194
199
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
200
|
+
const cleanup = Effect.gen(function* () {
|
|
201
|
+
for (const started of startedDrivers.toReversed()) {
|
|
202
|
+
if (started.cooldownMs > 0) {
|
|
203
|
+
yield* Effect.sleep(Duration.millis(started.cooldownMs));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const stopCommand = makeVpnCommand(started.driver, "stop");
|
|
207
|
+
yield* runCommand(stopCommand.command, stopCommand.label).pipe(Effect.ignore);
|
|
199
208
|
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
return yield* effect.pipe(Effect.ensuring(cleanup));
|
|
212
|
+
}).pipe(Effect.result);
|
|
200
213
|
|
|
201
|
-
|
|
202
|
-
|
|
214
|
+
if (Result.isSuccess(prerequisiteResult)) {
|
|
215
|
+
return prerequisiteResult.success;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (shouldTryDirect && prerequisiteResult.failure instanceof PrerequisiteRunError) {
|
|
219
|
+
const directRetryResult = yield* tryDirect();
|
|
220
|
+
if (Result.isSuccess(directRetryResult)) {
|
|
221
|
+
return directRetryResult.success;
|
|
203
222
|
}
|
|
204
|
-
|
|
223
|
+
if (!(directRetryResult.failure instanceof PrerequisiteRunError)) {
|
|
224
|
+
return yield* Effect.fail(directRetryResult.failure);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
205
227
|
|
|
206
|
-
return yield*
|
|
228
|
+
return yield* Effect.fail(prerequisiteResult.failure);
|
|
207
229
|
});
|
|
208
230
|
};
|