Publish AI workspace source through the isolated runtime
Verify control plane / verify (push) Canceled after 0s
Verify control plane / verify (pull_request) Canceled after 0s

This commit is contained in:
WebUp Cloud committed 2026-11-08 17:29:54 +08:00
1 parent 03169bd0f6
commit d1d14f0f77
21 files changed
+487 -68

No files matched your search

@@ -57,7 +57,7 @@ public class DeploymentCoordinator {
+ " status='APPLYING',lease_token=?,lease_expires_at=?,attempt=attempt+1,updated_at=CURRENT_TIMESTAMP"
+ " WHERE id=? AND status='QUEUED'",
lease,
Timestamp.from(Instant.now().plusSeconds(180)),
Timestamp.from(Instant.now().plusSeconds(300)),
id)
== 1) {
hosting.event(id, "APPLYING", "已领取部署任务");
@@ -82,6 +82,8 @@ public class DeploymentCoordinator {
result.put("projectId", deployment.get("project_id").toString());
result.put("deploymentId", deployment.get("id").toString());
result.set("services", list);
if (deployment.get("source_json") != null)
result.set("source", json.readTree(deployment.get("source_json").toString()));
ObjectNode branding = result.putObject("branding");
db.query(
"SELECT setting_key,setting_value FROM system_settings WHERE setting_key IN"
@@ -16,10 +16,15 @@ import org.springframework.web.bind.annotation.*;
public class HostingController {
private final JdbcTemplate db;
private final HostingService hosting;
private final SandboxClient sandbox;
private final RuntimeGateway runtime;
public HostingController(JdbcTemplate db, HostingService hosting) {
public HostingController(
JdbcTemplate db, HostingService hosting, SandboxClient sandbox, RuntimeGateway runtime) {
this.db = db;
this.hosting = hosting;
this.sandbox = sandbox;
this.runtime = runtime;
}
public record Plan(
@@ -38,6 +43,8 @@ public class HostingController {
public record Deploy(@NotBlank @Size(max = 200) String idempotencyKey, UUID rollbackId) {}
public record SourceDeploy(@NotBlank @Size(max = 200) String idempotencyKey) {}
public record ServiceSpec(
@NotBlank @Pattern(regexp = "[a-z][a-z0-9-]{0,62}") String name,
@NotBlank @Pattern(regexp = "[a-zA-Z0-9][a-zA-Z0-9._/:@-]{0,499}") String image,
@@ -185,6 +192,19 @@ public class HostingController {
return hosting.deployments(who.id(), project);
}
@PostMapping("/projects/{project}/deployments/source")
public Object deploySource(
@AuthenticationPrincipal SecurityConfig.Identity who,
@PathVariable UUID project,
@Valid @RequestBody SourceDeploy request) {
var existing = hosting.beforeSourceSnapshot(who.id(), project, request.idempotencyKey());
if (existing != null) return existing;
if (!runtime.request("/runtime/source-ready", null).path("ready").asBoolean(false))
throw new ApiError.Failure(503, "源码发布运行环境尚未启用,请联系管理员");
return hosting.deploySource(
who.id(), project, request.idempotencyKey(), sandbox.releaseSnapshot(project));
}
@GetMapping("/projects/{project}/deployments/{id}/events")
public Object events(
@AuthenticationPrincipal SecurityConfig.Identity who,
@@ -11,6 +11,7 @@ import org.springframework.transaction.annotation.Transactional;
@Service
public class HostingService {
private static final String SOURCE_SERVICE = "webup-source";
private final JdbcTemplate db;
private final WalletService wallets;
private final SecretVault vault;
@@ -143,7 +144,7 @@ public class HostingService {
own(user, project);
return db.queryForList(
"SELECT"
+ " id,project_id,name,image,command_json,port,cpu_millis,memory_mb,replicas,health_path,enabled,created_at,updated_at"
+ " id,project_id,name,image,command_json,port,cpu_millis,memory_mb,replicas,health_path,enabled,source_managed,created_at,updated_at"
+ " FROM project_services WHERE project_id=? ORDER BY name",
project);
}
@@ -153,6 +154,15 @@ public class HostingService {
UUID user, UUID project, UUID id, HostingController.ServiceSpec s) {
own(user, project);
db.queryForObject("SELECT id FROM projects WHERE id=? FOR UPDATE", UUID.class, project);
if (SOURCE_SERVICE.equals(s.name())
|| (id != null
&& db.queryForObject(
"SELECT COUNT(*) FROM project_services WHERE id=? AND project_id=? AND"
+ " source_managed=TRUE",
Integer.class,
id,
project)
!= 0)) throw new ApiError.Failure(409, "源码服务由平台管理,请使用「从源码发布」");
if (s.environment() != null
&& (s.environment().size() > 100
|| s.environment().entrySet().stream()
@@ -228,12 +238,113 @@ public class HostingService {
own(user, project);
db.queryForObject("SELECT id FROM projects WHERE id=? FOR UPDATE", UUID.class, project);
operations.checkAllowed(project);
var replay = deploymentReplay(project, key);
if (replay != null) return replay;
String plan = deploymentPlan(project);
String spec, source = null;
if (rollback != null) {
var old =
db.queryForList(
"SELECT spec_json,source_json FROM project_deployments WHERE id=? AND project_id=?"
+ " AND status IN ('ACTIVE','ROLLED_BACK')",
rollback,
project);
if (old.isEmpty()) throw new ApiError.Failure(400, "回滚版本不存在");
spec = old.getFirst().get("spec_json").toString();
source = (String) old.getFirst().get("source_json");
} else {
var list = enabledServices(project);
if (list.isEmpty()) throw new ApiError.Failure(400, "请先配置至少一个服务");
if (list.stream().anyMatch(service -> Boolean.TRUE.equals(service.get("source_managed"))))
throw new ApiError.Failure(409, "此项目含有源码服务,请使用「从源码发布」");
spec = encode(list);
}
return enqueueDeployment(
user,
project,
key,
spec,
source,
plan,
rollback == null ? "等待发布 Worker" : "请求部署历史版本 " + rollback);
}
Map<String, Object> beforeSourceSnapshot(UUID user, UUID project, String key) {
if ("ARCHIVED".equals(own(user, project).get("status")))
throw new ApiError.Failure(409, "归档项目不能发布");
operations.checkAllowed(project);
var replay = deploymentReplay(project, key);
if (replay != null) return replay;
deploymentPlan(project);
return null;
}
@Transactional
public Map<String, Object> deploySource(UUID user, UUID project, String key, JsonNode snapshot) {
db.queryForList("SELECT id FROM projects WHERE id=? AND owner_id=? FOR UPDATE", project, user);
var replay = beforeSourceSnapshot(user, project, key);
if (replay != null) return replay;
String plan = deploymentPlan(project);
JsonNode limits;
try {
limits = json.readTree(plan);
} catch (Exception error) {
throw new ApiError.Failure(500, "套餐快照无效");
}
int cpu = Math.min(500, limits.path("cpu_millis").asInt());
int memory = Math.min(512, limits.path("memory_mb").asInt());
var existing =
db.queryForList(
"SELECT id,source_managed FROM project_services WHERE project_id=? AND name=?",
project,
SOURCE_SERVICE);
if (existing.isEmpty()) {
db.update(
"INSERT INTO"
+ " project_services(id,project_id,name,image,command_json,encrypted_environment,"
+ "port,cpu_millis,memory_mb,replicas,health_path,enabled,source_managed) VALUES"
+ " (?,?,?,'webup-managed-source','[]',?,3000,?,?,1,'/',TRUE,TRUE)",
UUID.randomUUID(),
project,
SOURCE_SERVICE,
vault.encrypt("{}"),
cpu,
memory);
} else {
if (!Boolean.TRUE.equals(existing.getFirst().get("source_managed")))
throw new ApiError.Failure(409, "请先为同名的镜像服务改名,再发布源码");
db.update(
"UPDATE project_services SET cpu_millis=?,memory_mb=?,enabled=TRUE WHERE id=?",
cpu,
memory,
existing.getFirst().get("id"));
}
return enqueueDeployment(
user,
project,
key,
encode(enabledServices(project)),
encode(snapshot),
plan,
"已冻结源码快照,等待自动构建与发布");
}
private List<Map<String, Object>> enabledServices(UUID project) {
return db.queryForList(
"SELECT * FROM project_services WHERE project_id=? AND enabled=TRUE ORDER BY name",
project);
}
private Map<String, Object> deploymentReplay(UUID project, String key) {
var replay =
db.queryForList(
"SELECT * FROM project_deployments WHERE project_id=? AND idempotency_key=?",
project,
key);
if (!replay.isEmpty()) return publicDeployment(replay.getFirst());
return replay.isEmpty() ? null : publicDeployment(replay.getFirst());
}
private String deploymentPlan(UUID project) {
if (db.queryForObject(
"SELECT COUNT(*) FROM project_deployments WHERE project_id=? AND status IN"
+ " ('QUEUED','APPLYING','HEALTH_CHECK')",
@@ -242,40 +353,16 @@ public class HostingService {
> 0) throw new ApiError.Failure(409, "项目已有部署任务");
var subs =
db.queryForList(
"SELECT * FROM project_subscriptions WHERE project_id=? AND status='ACTIVE' AND"
+ " paid_until>CURRENT_TIMESTAMP",
"SELECT plan_snapshot FROM project_subscriptions WHERE project_id=? AND status='ACTIVE'"
+ " AND paid_until>CURRENT_TIMESTAMP",
project);
if (subs.isEmpty()) throw new ApiError.Failure(402, "请先购买有效托管套餐");
String spec;
if (rollback != null) {
var old =
db.queryForList(
"SELECT spec_json FROM project_deployments WHERE id=? AND project_id=? AND status IN"
+ " ('ACTIVE','ROLLED_BACK')",
rollback,
project);
if (old.isEmpty()) throw new ApiError.Failure(400, "回滚版本不存在");
spec = old.getFirst().get("spec_json").toString();
} else {
try {
Map<String, Object> plan =
json.readValue(
subs.getFirst().get("plan_snapshot").toString(),
new com.fasterxml.jackson.core.type.TypeReference<Map<String, Object>>() {});
checkQuota(project, plan);
} catch (ApiError.Failure f) {
throw f;
} catch (Exception e) {
throw new IllegalStateException(e);
}
var list =
db.queryForList(
"SELECT * FROM project_services WHERE project_id=? AND enabled=TRUE ORDER BY name",
project);
if (list.isEmpty()) throw new ApiError.Failure(400, "请先配置至少一个服务");
spec = encode(list);
}
checkSnapshotQuota(project, spec, subs.getFirst().get("plan_snapshot").toString());
return subs.getFirst().get("plan_snapshot").toString();
}
private Map<String, Object> enqueueDeployment(
UUID user, UUID project, String key, String spec, String source, String plan, String detail) {
checkSnapshotQuota(project, spec, plan);
UUID id = UUID.randomUUID();
var previous =
db.queryForList(
@@ -284,20 +371,22 @@ public class HostingService {
project);
db.update(
"INSERT INTO"
+ " project_deployments(id,project_id,user_id,spec_json,status,previous_id,idempotency_key)"
+ " VALUES (?,?,?,?,'QUEUED',?,?)",
+ " project_deployments(id,project_id,user_id,spec_json,source_json,status,previous_id,idempotency_key)"
+ " VALUES (?,?,?,?,?,'QUEUED',?,?)",
id,
project,
user,
spec,
source,
previous.isEmpty() ? null : previous.getFirst().get("id"),
key);
event(id, "QUEUED", rollback == null ? "等待发布 Worker" : "请求部署历史版本 " + rollback);
event(id, "QUEUED", detail);
return publicDeployment(db.queryForMap("SELECT * FROM project_deployments WHERE id=?", id));
}
Map<String, Object> publicDeployment(Map<String, Object> row) {
row.remove("spec_json");
row.put("release_type", row.remove("source_json") == null ? "IMAGE" : "SOURCE");
row.remove("lease_token");
return row;
}
@@ -330,7 +419,8 @@ public class HostingService {
public List<Map<String, Object>> deployments(UUID user, UUID project) {
own(user, project);
return db.queryForList(
"SELECT id,project_id,status,previous_id,error,created_at,updated_at FROM"
"SELECT id,project_id,status,previous_id,error,created_at,updated_at,"
+ " CASE WHEN source_json IS NULL THEN 'IMAGE' ELSE 'SOURCE' END AS release_type FROM"
+ " project_deployments WHERE project_id=? ORDER BY created_at DESC LIMIT 100",
project);
}
@@ -14,6 +14,7 @@ public class RuntimeGateway {
private final ObjectMapper json;
private final HttpClient client =
HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(Duration.ofSeconds(5))
.followRedirects(HttpClient.Redirect.NEVER)
.build();
@@ -36,7 +37,7 @@ public class RuntimeGateway {
throw new IllegalArgumentException();
var builder =
HttpRequest.newBuilder(uri)
.timeout(Duration.ofSeconds(120))
.timeout(Duration.ofSeconds(path.startsWith("/runtime/deployments/") ? 240 : 120))
.header("Authorization", "Bearer " + token)
.header("Content-Type", "application/json");
if (payload != null)
@@ -1,5 +1,6 @@
package run.webup.platform;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import java.net.*;
import java.net.http.*;
@@ -129,6 +130,26 @@ public class SandboxClient {
16000000));
}
JsonNode releaseSnapshot(UUID project) {
try {
var result =
json.readTree(
executeBounded(
project,
new ModelGateway.Call("release", "release_snapshot", json.createObjectNode()),
6000000));
if (result.hasNonNull("error"))
throw new ApiError.Failure(422, "源码无法发布:" + result.path("error").asText());
if (!result.path("archive").isTextual()
|| !result.path("sha256").asText().matches("[0-9a-f]{64}")
|| !Set.of("NODE", "STATIC", "STATIC_BUILD").contains(result.path("runtime").asText()))
throw new ApiError.Failure(502, "源码快照格式无效");
return result;
} catch (JsonProcessingException error) {
throw new ApiError.Failure(502, "源码快照格式无效");
}
}
public JsonNode importReview(UUID workspace, JsonNode snapshot) {
var arguments = json.createObjectNode();
for (String key : List.of("archive", "archiveSha256", "sourceHash"))
@@ -203,7 +224,8 @@ public class SandboxClient {
"version_list",
"archive_source",
"workspace_inspect",
"review_export");
"review_export",
"release_snapshot");
if (!allowed.contains(call.name())) throw new ApiError.Failure(400, "不允许的工具");
try {
var body = json.createObjectNode().put("name", call.name());
@@ -0,0 +1,2 @@
ALTER TABLE project_services ADD COLUMN source_managed BOOLEAN NOT NULL DEFAULT FALSE;
ALTER TABLE project_deployments ADD COLUMN source_json TEXT;
@@ -63,6 +63,36 @@ class HostingIntegrationTest {
true);
}
@Test
void sourcePublishCreatesManagedServiceAndRollbackUsesFrozenArchive() {
assertThrows(
ApiError.Failure.class, () -> hosting.beforeSourceSnapshot(user, project, "source"));
hosting.subscribe(user, project, plan, "source-plan");
assertNull(hosting.beforeSourceSnapshot(user, project, "source"));
var snapshot =
json.createObjectNode()
.put("archive", "fixture-archive")
.put("sha256", "a".repeat(64))
.put("runtime", "NODE")
.put("install", false)
.put("build", false);
var first = hosting.deploySource(user, project, "source", snapshot);
assertEquals("SOURCE", first.get("release_type"));
assertFalse(first.containsKey("source_json"));
assertEquals(first.get("id"), hosting.beforeSourceSnapshot(user, project, "source").get("id"));
assertEquals(true, hosting.services(user, project).getFirst().get("source_managed"));
var claimed = deployments.claim();
var payload = deployments.payload(claimed);
assertEquals(snapshot, payload.path("source"));
assertTrue(payload.path("services").get(0).path("source_managed").asBoolean());
deployments.finish(
(UUID) first.get("id"),
claimed.get("lease_token").toString(),
json.createObjectNode().put("ready", true));
hosting.deploy(user, project, "source-rollback", (UUID) first.get("id"));
assertEquals(snapshot, deployments.payload(deployments.claim()).path("source"));
}
@Test
void purchaseIsIdempotentAndSnapshotsPlan() {
hosting.subscribe(user, project, plan, "first");
+4
View File
@@ -69,6 +69,10 @@ kubectl -n platform-sandboxes delete resourcequota workspace-quota --ignore-not-
## 文件、版本与源码
在云托管选择「从源码发布」,平台会检查当前工作区并为本次发布保存独立快照。支持根目录 `index.html`、含 `scripts.start` 的 Node.js 项目,以及含 `scripts.build` 且输出 `dist/index.html` 或 `build/index.html` 的前端项目。依赖项目需要提交 npm 锁文件。源码服务使用端口 3000,并提供 `HOST`、`PORT` 和 `DATA_DIR` 环境变量。Node.js 项目的 `DATA_DIR` 指向持久卷,应用应把需要保留的数据写到这里。开发工作区中的环境文件、本地数据库和依赖缓存不随源码发布。
运维在 `platform-sandboxes` 的 `webup-runtime` ConfigMap 设置 `SOURCE_IMAGE`,指向具备 Node.js、npm、Python 3 的受信运行镜像,并启用运行发布 Worker。K3s 集群需有持久卷供应器。每次发布保存源码快照,构建和启动在隔离 Pod 内执行,健康检查通过后切换流量。回滚会使用所选版本的源码和锁文件重新构建。发布仍需项目审核通过和有效托管套餐。
用户可通过真实隔离网关列目录、读写 UTF-8 文件、保存 Git 版本、列出最近 30 个版本和下载 ZIP。路径必须位于工作区,读取上限 64KB,单次写入上限 256KB。终端输出截断在 64KB,源码 ZIP 限制为 10MB / 5000 文件并排除 `.git`、node_modules、.venv 与 __pycache__。归档属于项目所有者;其中可能包含项目自身的配置文件,下载后应妥善保管。
所有这些功能均先检查项目所有权,不能用修改 URL 访问其他项目。Kubernetes exec 和 PVC 的真实运行、Git 版本操作以及完整 Agent 开发仍需集群联调验证;本地文件工具测试不能替代隔离验收。
+1
View File
@@ -141,6 +141,7 @@
| GET / POST | `/api/projects/{id}/subscription` | 本人订阅 / 使用钱包购买月度套餐,planId、idempotencyKey |
| GET / POST / PUT | `/api/projects/{id}/services[/{service}]` | 本人 OCI 服务配置;环境变量加密,留空保留 |
| GET / POST | `/api/projects/{id}/deployments` | 本人部署记录 / 创建发布,idempotencyKey、可选 rollbackId |
| POST | `/api/projects/{id}/deployments/source` | 使用当前 AI 工作区源码创建发布,提交 idempotencyKey |
| GET | `/api/projects/{id}/deployments/{deployment}/events` | 发布阶段与错误记录 |
| GET | `/api/projects/{id}/runtime/services/{service}` | 实际运行状态,需要运行网关 |
| GET / POST | `/api/projects/{id}/runtime/services/{service}/logs`、`.../restart` | 实际日志 / 重启,禁止封禁项目重启 |
+1 -1
View File
@@ -9,7 +9,7 @@
- 集群节点 `webup-local`;K3s / gVisor 版本与官方制品哈希固定在 `infra/local/versions.json`。
- 平台默认同时开发 2 个项目,预览 Worker 本实例容量 4。管理员 `/admin/tasks` 可改总并发和队列顺序。
- 预览 AI Worker 已启用,实际调用管理员配置的模型。预览余额是测试余额,外部模型供应商仍可能收取真实 API 费用。
- 运行发布 / 域名 Worker 保持关闭;本阶段验证了开发工作区,没有声称公网托管、镜像构建发布或 HTTPS 域名已验收。
- 本地发布 Worker 默认关闭。部署新版网关并通过隔离验收后,可用 `enable-preview.ps1 -EnableHosting` 启用本地托管和源码发布。公网域名仍需要实际 DNS、Ingress 与证书配置。
## 重启和复验
+25 -16
View File
@@ -44,6 +44,7 @@ const subscriptionActive = computed(() =>
subscription.value.status === "ACTIVE" && new Date(subscription.value.paid_until).getTime() > Date.now(),
);
const hasService = computed(() => services.value.some(service => service.enabled));
const hasManagedSource = computed(() => services.value.some(service => service.enabled && service.source_managed));
const deploying = computed(() =>
deployments.value.some(deployment => ["QUEUED", "APPLYING", "HEALTH_CHECK"].includes(deployment.status)),
);
@@ -159,21 +160,26 @@ async function buy(p: any) {
busy.value = false;
}
}
async function deploy(rollbackId?: string) {
if (!canPublish.value || (!rollbackId && !hasService.value) || busy.value) return;
if (!confirm(rollbackId ? "确认重新发布历史版本?" : "确认发布当前配置?"))
const sourceKeys = new Map<string, string>();
async function deploy(rollbackId?: string, fromSource = false) {
if (!canPublish.value || (!rollbackId && !fromSource && (!hasService.value || hasManagedSource.value)) || busy.value) return;
const id = project.value;
if (!confirm(rollbackId ? "确认重新发布历史版本?" : fromSource ? "确认从当前 AI 工作区源码构建并发布?" : "确认发布当前服务配置?"))
return;
busy.value = true;
error.value = "";
try {
await api("/projects/" + project.value + "/deployments", {
if (fromSource && !sourceKeys.has(id)) sourceKeys.set(id, crypto.randomUUID());
await api("/projects/" + id + "/deployments" + (fromSource ? "/source" : ""), {
method: "POST",
...(fromSource ? { signal: AbortSignal.timeout(170000) } : {}),
body: body({
idempotencyKey: crypto.randomUUID(),
rollbackId: rollbackId || null,
idempotencyKey: fromSource ? sourceKeys.get(id) : crypto.randomUUID(),
...(fromSource ? {} : { rollbackId: rollbackId || null }),
}),
});
success.value = "部署已入队";
if (fromSource) sourceKeys.delete(id);
success.value = fromSource ? "已保存源码快照,等待自动构建与发布" : "部署已入队";
await load();
} catch (e) {
error.value = (e as Error).message;
@@ -236,11 +242,12 @@ onUnmounted(() => clearInterval(timer));
<div>
<span class="eyebrow">CLOUD RUNTIME</span>
<h1>云托管</h1>
<p>OCI 多服务、健康发布与版本回滚。</p>
<p>从项目源码自动发布,也可以使用已有镜像。</p>
</div>
<div class="table-actions">
<button class="primary" :disabled="!canPublish || busy" @click="deploy(undefined, true)">从源码发布</button>
<button class="secondary" :disabled="!canPublish || !hasService || hasManagedSource || busy" @click="deploy()">发布已有镜像</button>
</div>
<button class="primary" :disabled="!canPublish || !hasService || busy" @click="deploy()">
发布当前配置
</button>
</div>
<div v-if="error" class="alert error">{{ error }}</div>
<div v-if="success" class="alert success">{{ success }}</div>
@@ -262,7 +269,7 @@ onUnmounted(() => clearInterval(timer));
<p v-else-if="publication.reviewStatus === 'PENDING'">请联系管理员在「网站管理与人工审核」中处理,通过后即可发布。</p>
<p v-else-if="publication.reviewStatus !== 'APPROVED'">请联系管理员处理审核状态。</p>
<p v-if="!subscriptionActive">发布需要有效的托管套餐,可在下方购买或续期。</p>
<p v-if="!hasService">请添加并启用至少一个服务。</p>
<p v-if="!hasService">可直接从 AI 工作区发布源码。使用已有镜像时,需要先添加服务。</p>
<p v-if="deploying">已有发布任务正在处理。</p>
</div>
<section class="project-grid">
@@ -271,13 +278,13 @@ onUnmounted(() => clearInterval(timer));
<h3>{{ s.name }}</h3>
<span class="status">{{ s.enabled ? "启用" : "停用" }}</span>
</div>
<p>{{ s.image }}</p>
<p>{{ s.source_managed ? "平台自动构建的源码服务" : s.image }}</p>
<small
>{{ s.cpu_millis }}m CPU / {{ s.memory_mb }}MiB / {{ s.replicas }} 副本
/ 端口 {{ s.port }}</small
>
<div class="project-actions">
<button class="link" @click="edit(s)">配置</button
<button v-if="!s.source_managed" class="link" @click="edit(s)">配置</button
><button class="link" :disabled="busy" @click="runtime(s, 'status')">
状态</button
><button class="link" :disabled="busy" @click="runtime(s, 'logs')">
@@ -289,7 +296,7 @@ onUnmounted(() => clearInterval(timer));
</article>
</section>
<div v-if="!services.length" class="panel empty">
尚无服务,添加已有 OCI 镜像开始。
尚无运行服务。可从 AI 工作区源码发布,平台会自动创建服务。
</div>
<section v-if="output" class="panel event-log">
<pre style="white-space: pre-wrap; overflow-wrap: anywhere">{{
@@ -337,6 +344,7 @@ onUnmounted(() => clearInterval(timer));
<th>版本</th>
<th>时间</th>
<th>状态</th>
<th>来源</th>
<th>错误</th>
<th>操作</th>
</tr>
@@ -346,6 +354,7 @@ onUnmounted(() => clearInterval(timer));
<td class="truncate" :title="d.id">{{ d.id }}</td>
<td>{{ new Date(d.created_at).toLocaleString() }}</td>
<td>{{ d.status }}</td>
<td>{{ d.release_type === "SOURCE" ? "源码" : "镜像" }}</td>
<td>{{ d.error }}</td>
<td>
<div class="table-actions">
@@ -362,7 +371,7 @@ onUnmounted(() => clearInterval(timer));
</td>
</tr>
<tr v-if="!deployments.length">
<td colspan="5" class="empty">暂无部署</td>
<td colspan="6" class="empty">暂无部署</td>
</tr>
</tbody>
</table>
+3
View File
@@ -19,6 +19,9 @@ rules:
- apiGroups: ['apps']
resources: ['deployments']
verbs: ['get', 'list', 'create', 'patch']
- apiGroups: ['']
resources: ['persistentvolumeclaims']
verbs: ['get', 'create']
- apiGroups: ['networking.k8s.io']
resources: ['networkpolicies']
verbs: ['get', 'create']
+3
View File
@@ -45,6 +45,9 @@ spec:
- name: RUNTIME_INGRESS_CLASS
valueFrom:
configMapKeyRef: {name: webup-runtime, key: INGRESS_CLASS, optional: true}
- name: RUNTIME_SOURCE_IMAGE
valueFrom:
configMapKeyRef: {name: webup-runtime, key: SOURCE_IMAGE, optional: true}
- name: SANDBOX_IMAGE
value: registry.example.invalid/platform/development-sandbox:0.2.0
- name: SANDBOX_RUNTIME_CLASS
+1 -1
View File
@@ -3,7 +3,7 @@ FROM ${BASE_IMAGE}
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py runtime.py domain_runtime.py review_tools.py workspace_changes.py ./
COPY app.py runtime.py domain_runtime.py review_tools.py workspace_changes.py source_runtime.py ./
USER 10001:10001
EXPOSE 8090
CMD ["uvicorn","app:app","--host","0.0.0.0","--port","8090","--workers","1","--no-access-log"]
+4 -2
View File
@@ -233,6 +233,8 @@ try:
result={'exitCode':p.returncode,'timedOut':timed_out,'output':output[:64000].decode('utf-8','replace'),'truncated':len(output)>64000}
elif name=='review_export':
result=review_export(root)
elif name=='release_snapshot':
result=release_snapshot(root)
elif name=='review_import':
result=review_import(root,values)
elif name=='review_validate':
@@ -380,7 +382,7 @@ def dispose_review(project: str, authorization: str | None = Header(default=None
def execute_tool(project,request,authorization,review):
authorize(authorization)
name = workspace_name(project)
allowed = {'review_import','review_validate','workspace_inspect'} if review else {"list_files", "read_file", "write_file", "delete_file", "run_command", "version_save", "version_list", "archive_source", "workspace_inspect",'review_export'}
allowed = {'review_import','review_validate','workspace_inspect'} if review else {"list_files", "read_file", "write_file", "delete_file", "run_command", "version_save", "version_list", "archive_source", "workspace_inspect",'review_export','release_snapshot'}
if request.name not in allowed:
raise HTTPException(400, "Unknown tool")
payload = json.dumps(request.model_dump(), ensure_ascii=False).encode()
@@ -409,7 +411,7 @@ def execute_tool(project,request,authorization,review):
except Exception:
connection.close()
raise
output_limit = 16_000_000 if request.name in {'archive_source','review_export'} else MAX_OUTPUT
output_limit = 16_000_000 if request.name in {'archive_source','review_export','release_snapshot'} else MAX_OUTPUT
if request.trackChanges:
output_limit = 512_000
output = read_exec_output(connection, output_limit)
+45 -4
View File
@@ -32,16 +32,22 @@ def review_fingerprint(files):
return hashlib.sha256(json.dumps(files, sort_keys=True, separators=(',', ':'), ensure_ascii=True).encode()).hexdigest()
def review_export(root):
def review_export(root, release=False):
files, samples, total, sample_chars = [], [], 0, 0
excluded = {'.npm', '.cache', '.pytest_cache', '.turbo', '.output', '.nuxt'}
def excluded_path(path):
return review_secret(path) or (release and (
any(part in excluded for part in path.parts) or path.parts[0] == 'data'
or path.suffix.lower() in {'.db', '.sqlite', '.sqlite3'}
))
archive = io.BytesIO()
with zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED) as output:
for directory, dirs, names in os.walk(root, followlinks=False):
dirs[:] = sorted(d for d in dirs if not review_secret(pathlib.Path(d)) and not (pathlib.Path(directory) / d).is_symlink())
dirs[:] = sorted(d for d in dirs if not excluded_path(pathlib.Path(d)) and not (pathlib.Path(directory) / d).is_symlink())
for name in sorted(names):
file = pathlib.Path(directory) / name
rel = file.relative_to(root)
if review_secret(rel) or file.is_symlink() or not file.is_file():
if excluded_path(rel) or file.is_symlink() or not file.is_file():
continue
size = file.stat().st_size
if size > 10_000_000 or total + size > 10_000_000 or len(files) >= 2000:
@@ -53,7 +59,7 @@ def review_export(root):
raise ValueError('Review snapshot exceeds size limit')
files.append({'path': rel.as_posix(), 'bytes': len(data), 'sha256': hashlib.sha256(data).hexdigest()})
output.writestr(rel.as_posix(), data)
if file.suffix.lower() in REVIEW_TEXT and file.name not in {'package-lock.json', 'yarn.lock'} and sample_chars < 14000:
if not release and file.suffix.lower() in REVIEW_TEXT and file.name not in {'package-lock.json', 'yarn.lock'} and sample_chars < 14000:
text = data[:64000].decode('utf-8', 'replace')
shown = review_redact(text[:min(7000, 14000 - sample_chars)])
samples.append({'path': rel.as_posix(), 'content': shown, 'truncated': len(shown) < len(text)})
@@ -65,6 +71,41 @@ def review_export(root):
'complete': True, 'totalBytes': total}
def release_snapshot(root):
snapshot = review_export(root, release=True)
data = base64.b64decode(snapshot['archive'])
if len(data) > 4_000_000:
raise ValueError('Source ZIP exceeds 4 MB')
with zipfile.ZipFile(io.BytesIO(data)) as archive:
names = set(archive.namelist())
package = {}
if 'package.json' in names:
package = json.loads(archive.read('package.json'))
if not isinstance(package, dict) or not isinstance(package.get('scripts', {}), dict):
raise ValueError('package.json scripts must be an object')
scripts = package.get('scripts', {})
start = str(scripts.get('start', '')).strip()
build = isinstance(scripts.get('build'), str) and bool(scripts['build'].strip())
install = bool(package.get('dependencies') or package.get('devDependencies')
or package.get('optionalDependencies'))
if start:
runtime = 'NODE'
simple = re.fullmatch(r'node\s+(?:\./)?([\w./-]+\.(?:mjs|cjs|js))', start)
if simple and not build and simple.group(1) not in names:
raise ValueError('Start file missing: ' + simple.group(1))
elif build:
runtime = 'STATIC_BUILD'
elif 'index.html' in names:
runtime = 'STATIC'
install = False
else:
raise ValueError('Source needs package.json scripts.start, scripts.build, or index.html')
if install and not ({'package-lock.json', 'npm-shrinkwrap.json'} & names):
raise ValueError('Dependencies require package-lock.json or npm-shrinkwrap.json')
return {'archive': snapshot['archive'], 'sha256': snapshot['archiveSha256'],
'runtime': runtime, 'install': install, 'build': build}
def review_import(root, values):
if any(root.iterdir()):
raise ValueError('Review workspace must be empty; snapshots cannot overwrite existing data')
+29 -1
View File
@@ -1,5 +1,6 @@
"""Restricted OCI runtime controller. This module never builds or runs code on the gateway host."""
import hmac
import base64
import json
import os
import threading
@@ -12,6 +13,7 @@ from fastapi import APIRouter, Header, HTTPException
from kubernetes import client
from kubernetes.client.rest import ApiException
from pydantic import BaseModel, Field
from source_runtime import Source, attach_source, source_chunks, source_image
router = APIRouter()
NAMESPACE = os.environ.get("RUNTIME_NAMESPACE", "platform-runtimes")
@@ -44,6 +46,7 @@ class Service(BaseModel):
memory_mb: int = Field(ge=64, le=262144)
replicas: int = Field(ge=1, le=20)
health_path: str = Field(pattern=r"^/[^\s]{0,499}$|^$")
source_managed: bool = False
class Deployment(BaseModel):
@@ -51,6 +54,7 @@ class Deployment(BaseModel):
deploymentId: uuid.UUID
services: list[Service] = Field(min_length=1, max_length=100)
branding: dict[str, str] = Field(default_factory=dict)
source: Source | None = None
class ProjectState(BaseModel):
@@ -150,12 +154,24 @@ def deployment_manifest(project, deployment, service, branding=None):
return manifest
@router.get("/runtime/source-ready")
def source_ready(authorization: str | None = Header(default=None)):
auth(authorization)
return {"ready": bool(source_image())}
@router.post("/runtime/deployments/{deployment}")
def deploy(deployment: str, request: Deployment, authorization: str | None = Header(default=None)):
auth(authorization)
did, project = identifier(deployment), identifier(request.projectId)
if did != identifier(request.deploymentId):
raise HTTPException(400, "Deployment ID mismatch")
managed = [service for service in request.services if service.source_managed]
if len(managed) != int(request.source is not None):
raise HTTPException(400, "Source snapshot must belong to exactly one managed service")
if request.source and not source_image():
raise HTTPException(503, "Source runtime image is not configured")
chunks = source_chunks(request.source) if request.source else []
with guard:
lock = locks.setdefault(project, threading.Lock())
if not lock.acquire(timeout=1):
@@ -190,6 +206,18 @@ def deploy(deployment: str, request: Deployment, authorization: str | None = Hea
for service in request.services:
manifest = deployment_manifest(project, did, service, request.branding)
name = manifest["metadata"]["name"]
if service.source_managed:
attach_source(manifest, request.source, len(chunks))
for index, chunk in enumerate(chunks):
ensure(core.create_namespaced_config_map, namespace=NAMESPACE, body={
"metadata": {"name": f"{name}-source-{index}", "labels": manifest["metadata"]["labels"]},
"immutable": True, "binaryData": {f"part-{index:03}": base64.b64encode(chunk).decode()},
})
if request.source.runtime == "NODE":
ensure(core.create_namespaced_persistent_volume_claim, namespace=NAMESPACE, body={
"metadata": {"name": "data-" + identifier(service.id), "labels": {"platform/project": project}},
"spec": {"accessModes": ["ReadWriteOnce"], "resources": {"requests": {"storage": "1Gi"}}},
})
ensure(core.create_namespaced_secret, namespace=NAMESPACE, body={"metadata": {"name": name}, "type": "Opaque", "stringData": service.environment})
proxy_config = branding_config(service.port,request.branding)
if proxy_config is not None:
@@ -199,7 +227,7 @@ def deploy(deployment: str, request: Deployment, authorization: str | None = Hea
target_port = 8081 if proxy_config is not None else service.port
ensure(core.create_namespaced_service, namespace=NAMESPACE, body={"metadata": {"name": stable, "labels": {"platform/project": project}}, "spec": {"selector": manifest["metadata"]["labels"], "ports": [{"port": service.port, "targetPort": target_port}]}})
names.append((name, stable, manifest["metadata"]["labels"], service.port, service.replicas))
deadline = time.monotonic() + 90
deadline = time.monotonic() + (200 if request.source else 90)
while time.monotonic() < deadline:
if all((apps.read_namespaced_deployment(n, NAMESPACE).status.ready_replicas or 0) >= replicas for n, _, _, _, replicas in names):
break
+134
View File
@@ -0,0 +1,134 @@
"""Package immutable source data for the isolated runtime, without a container build daemon."""
import base64
import binascii
import hashlib
import os
from fastapi import HTTPException
from pydantic import BaseModel, Field
class Source(BaseModel):
archive: str = Field(min_length=1, max_length=5_333_336)
sha256: str = Field(pattern=r"^[0-9a-f]{64}$")
runtime: str = Field(pattern=r"^(NODE|STATIC|STATIC_BUILD)$")
install: bool
build: bool
def source_image():
return os.environ.get("RUNTIME_SOURCE_IMAGE", "").strip()
def source_chunks(source):
try:
data = base64.b64decode(source.archive, validate=True)
except (ValueError, binascii.Error):
raise HTTPException(400, "Invalid source archive encoding") from None
if len(data) > 4_000_000 or hashlib.sha256(data).hexdigest() != source.sha256:
raise HTTPException(400, "Source archive checksum or size is invalid")
return [data[i:i + 480_000] for i in range(0, len(data), 480_000)]
PREPARE = r"""
import hashlib, io, os, pathlib, shutil, stat, subprocess, sys, zipfile
release = pathlib.Path('/release').resolve()
root = release
parts = pathlib.Path('/source-bundle')
archive = b''.join(path.read_bytes() for path in sorted(parts.glob('part-*')))
if hashlib.sha256(archive).hexdigest() != sys.argv[1]:
raise SystemExit('Source archive checksum mismatch')
with zipfile.ZipFile(io.BytesIO(archive)) as zip_file:
entries = zip_file.infolist()
if len(entries) > 2000 or sum(entry.file_size for entry in entries) > 10_000_000:
raise SystemExit('Source archive exceeds extraction limits')
for entry in entries:
path = pathlib.PurePosixPath(entry.filename)
if (not path.parts or path.is_absolute() or '..' in path.parts or '\\' in entry.filename
or stat.S_ISLNK(entry.external_attr >> 16) or entry.is_dir()):
raise SystemExit('Unsafe source archive member')
target = root / path
target.parent.mkdir(parents=True, exist_ok=True)
with zip_file.open(entry) as content, target.open('xb') as output:
shutil.copyfileobj(content, output)
environment = dict(os.environ, HOME='/tmp', npm_config_cache='/tmp/npm')
if sys.argv[2] == '1':
subprocess.run(['npm', 'ci', '--no-audit', '--no-fund'], cwd=root, env=environment, check=True, timeout=90)
if sys.argv[3] == '1':
subprocess.run(['npm', 'run', 'build'], cwd=root, env=environment, check=True, timeout=75)
if sys.argv[4] == 'STATIC_BUILD' and not any(
(root / folder / 'index.html').is_file() for folder in ('dist', 'build')
):
raise SystemExit('Static build must create dist/index.html or build/index.html')
"""
SERVE = r"""
import functools, http.server, os, pathlib, sys, urllib.parse
release = pathlib.Path('/release').resolve()
root = release
if sys.argv[1] == 'STATIC_BUILD':
root = next(path for path in (root / 'dist', root / 'build') if (path / 'index.html').is_file())
root = root.resolve()
if not root.is_relative_to(release):
raise SystemExit('Static output must stay inside the release directory')
class StaticHandler(http.server.SimpleHTTPRequestHandler):
def send_head(self):
path = pathlib.Path(self.translate_path(self.path))
if not path.resolve().is_relative_to(root) or any(
part.startswith('.') for part in urllib.parse.unquote(self.path.split('?', 1)[0]).split('/')
):
self.send_error(404)
return None
if path.is_dir() and any(
(path / name).is_file() and not (path / name).resolve().is_relative_to(root)
for name in ('index.html', 'index.htm')
):
self.send_error(404)
return None
return super().send_head()
def list_directory(self, path):
self.send_error(404)
return None
handler = functools.partial(StaticHandler, directory=root)
http.server.ThreadingHTTPServer(('0.0.0.0', int(os.environ['PORT'])), handler).serve_forever()
"""
def attach_source(manifest, source, chunk_count):
pod = manifest["spec"]["template"]["spec"]
container = pod["containers"][0]
name = manifest["metadata"]["name"]
container.update(
image=source_image(), workingDir="/release",
command=["npm", "start"] if source.runtime == "NODE"
else ["python3", "-I", "-c", SERVE, source.runtime],
env=[{"name": "HOST", "value": "0.0.0.0"}, {"name": "PORT", "value": str(container["ports"][0]["containerPort"])},
{"name": "HOME", "value": "/tmp"}, {"name": "DATA_DIR", "value": "/data"}],
)
container["volumeMounts"].append({"name": "release", "mountPath": "/release", "readOnly": True})
pod["securityContext"]["fsGroup"] = 10001
pod["volumes"].extend([
{"name": "release", "emptyDir": {"sizeLimit": "512Mi"}},
{"name": "source-bundle", "projected": {"sources": [
{"configMap": {"name": f"{name}-source-{index}"}} for index in range(chunk_count)
]}},
])
pod["initContainers"] = [{
"name": "prepare-source", "image": source_image(),
"command": ["python3", "-I", "-c", PREPARE, source.sha256,
str(int(source.install)), str(int(source.build)), source.runtime],
"securityContext": container["securityContext"], "resources": container["resources"],
"volumeMounts": [{"name": "release", "mountPath": "/release"},
{"name": "source-bundle", "mountPath": "/source-bundle", "readOnly": True},
{"name": "tmp", "mountPath": "/tmp"}],
}]
if source.runtime == "NODE":
service = manifest["metadata"]["labels"]["platform/service"]
pod["volumes"].append({"name": "app-data", "persistentVolumeClaim": {"claimName": "data-" + service}})
container["volumeMounts"].append({"name": "app-data", "mountPath": "/data"})
+15
View File
@@ -41,6 +41,21 @@ class GatewayTests(unittest.TestCase):
(root / "large.txt").write_bytes(b"x" * 64001)
self.assertIn("error", self.execute_file_tool(root, "read_file", {"path": "large.txt"}))
def test_source_snapshot_checks_start_and_excludes_credentials_and_local_data(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / "package.json").write_text('{"scripts":{"start":"node server.js"}}')
result = self.execute_file_tool(root, "release_snapshot", {})
self.assertIn("server.js", result["error"])
(root / "server.js").write_text("console.log('ready')")
(root / ".env").write_text("secret")
(root / "data").mkdir()
(root / "data/students.db").write_text("private rows")
result = self.execute_file_tool(root, "release_snapshot", {})
self.assertEqual("NODE", result["runtime"])
with zipfile.ZipFile(io.BytesIO(base64.b64decode(result["archive"]))) as archive:
self.assertEqual({"package.json", "server.js"}, set(archive.namelist()))
def test_agent_file_change_counts_and_manual_edits_are_separate(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
+7
View File
@@ -3,6 +3,12 @@ set -euo pipefail
[[ ${WSL_DISTRO_NAME:-} == WebUp-Local && $EUID == 0 && -e /etc/webup-local/environment ]] || { echo 'Dedicated local environment required.' >&2; exit 1; }
project_root=$(realpath "$1")
k3s kubectl apply -f "$project_root/infra/k8s/review-rbac.yaml"
k3s kubectl apply -f "$project_root/infra/k8s/runtime-rbac.yaml"
if k3s kubectl -n platform-sandboxes get configmap webup-runtime >/dev/null 2>&1; then
k3s kubectl -n platform-sandboxes patch configmap webup-runtime --type merge -p '{"data":{"SOURCE_IMAGE":"docker.io/library/webup-development-sandbox:local-v1"}}'
else
k3s kubectl -n platform-sandboxes create configmap webup-runtime --from-literal=SOURCE_IMAGE=docker.io/library/webup-development-sandbox:local-v1
fi
install -d -m 0700 /etc/webup-local
if [[ ! -e /etc/webup-local/gateway.env ]]; then
python3 - <<'PY'
@@ -40,6 +46,7 @@ WantedBy=multi-user.target
UNIT
systemctl daemon-reload
systemctl enable --now webup-gateway-forward
systemctl restart webup-gateway-forward
install -d -m 0700 "$project_root/.runtime/local-cluster"
cp /etc/webup-local/gateway.env "$project_root/.runtime/local-cluster/gateway.env"
python3 - <<'PY'
+6 -1
View File
@@ -1,3 +1,4 @@
param([switch]$EnableHosting)
$ErrorActionPreference = 'Stop'
$projectRoot = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '../..'))
$localDirectory = Join-Path $projectRoot '.runtime/local-cluster'
@@ -27,11 +28,15 @@ foreach ($key in @('SANDBOX_TOKEN', 'RUNTIME_TOKEN')) {
}
$health = Invoke-RestMethod -Uri http://127.0.0.1:8090/health/ready -Headers @{Authorization = ('Bearer ' + $gatewayValues['SANDBOX_TOKEN'])} -TimeoutSec 10
if (-not $health.ready) { throw 'Gateway Kubernetes readiness check failed.' }
if ($EnableHosting) {
$runtime = Invoke-RestMethod -Uri http://127.0.0.1:8090/runtime/source-ready -Headers @{Authorization = ('Bearer ' + $gatewayValues['RUNTIME_TOKEN'])} -TimeoutSec 10
if (-not $runtime.ready) { throw 'Local source runtime is not configured. Deploy the updated gateway first.' }
}
$infra = @{app = @{
'worker-enabled' = $true; 'worker-capacity' = 4
'sandbox-url' = 'http://127.0.0.1:8090'; 'sandbox-token' = $gatewayValues['SANDBOX_TOKEN']
'runtime-url' = 'http://127.0.0.1:8090'; 'runtime-token' = $gatewayValues['RUNTIME_TOKEN']
'runtime-worker-enabled' = $false
'runtime-worker-enabled' = [bool]$EnableHosting
}}
$infra | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath (Join-Path $localDirectory 'preview-infrastructure.yml') -Encoding utf8
Set-Content -LiteralPath $triggerFile -Value (Get-Date -Format o)