Publish AI workspace source through the isolated runtime
This commit is contained in:
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");
|
||||
|
||||
Reference in new issue
Block a user