feat: refactor to ymsctl & yms-daemon

This commit is contained in:
2026-08-17 02:10:10 +08:00
parent 7755da72e7
commit 79f18fcdea
23 changed files with 394 additions and 47 deletions
+7 -1
View File
@@ -33,6 +33,7 @@ type persistedContainerRequest struct {
PreviousPort int `json:"previousPort"`
PreviousContainer string `json:"previousContainer"`
TargetHealthEndpoint string `json:"targetHealthEndpoint"`
StartLog bool `json:"startLog"`
GatewayBeforePath string `json:"gatewayBeforePath"`
GatewayAfterPath string `json:"gatewayAfterPath"`
GatewayReceiptPath string `json:"gatewayReceiptPath"`
@@ -40,7 +41,7 @@ type persistedContainerRequest struct {
// UpdateContainerImage pulls one development image, freezes its repository
// digest, and updates the inactive Docker backend slot.
func (u *Updater) UpdateContainerImage(ctx context.Context, imageReference string, report ProgressReporter) (transaction.Transaction, error) {
func (u *Updater) UpdateContainerImage(ctx context.Context, imageReference string, startLog bool, report ProgressReporter) (transaction.Transaction, error) {
if u.containerExecutor == nil || u.engine == nil {
return transaction.Transaction{}, errors.New("container backend updater is not configured")
}
@@ -138,6 +139,7 @@ func (u *Updater) UpdateContainerImage(ctx context.Context, imageReference strin
TargetPort: targetPort, TargetContainer: targetSlot.ContainerName,
PreviousPort: before.ActivePort, PreviousContainer: previousContainer,
TargetHealthEndpoint: targetSlot.HealthEndpoint,
StartLog: startLog,
GatewayBeforePath: beforePath, GatewayAfterPath: afterPath,
GatewayReceiptPath: filepath.Join(transactionRoot, "gateway.applied"),
}
@@ -294,6 +296,10 @@ func (u *Updater) runContainerUpdate(ctx context.Context, record transaction.Tra
ConfigLocation: deploymentconfig.ContainerConfigLocation,
RestartPolicy: containerengine.RestartPolicy{Name: "no"},
HealthEndpoint: request.TargetHealthEndpoint,
StartLog: request.StartLog,
LogReporter: func(line string) {
reportProgress(report, Progress{TransactionID: record.ID, State: transaction.StateStarting, Message: "CONTAINER LOG " + line})
},
}
switch record.State {
case transaction.StateCreated, transaction.StateValidating, transaction.StatePrepared, transaction.StateStarting:
+11 -7
View File
@@ -31,7 +31,7 @@ func TestContainerUpdaterPullsSwitchesAndStopsPreviousSlot(t *testing.T) {
},
}, 0)
record, err := updater.UpdateContainerImage(ctx, containerTestImage, nil)
record, err := updater.UpdateContainerImage(ctx, containerTestImage, true, nil)
if err != nil {
t.Fatalf("update container backend: %v", err)
}
@@ -61,7 +61,7 @@ func TestContainerUpdaterFirstInstallCreatesInactiveSlotBeforeSwitch(t *testing.
return nil
}
record, err := updater.UpdateContainerImage(ctx, containerTestImage, func(item Progress) {
record, err := updater.UpdateContainerImage(ctx, containerTestImage, true, func(item Progress) {
progress = append(progress, item)
})
if err != nil {
@@ -99,7 +99,7 @@ func TestContainerUpdaterRejectsMissingActiveWithPresentInactive(t *testing.T) {
},
}, 0)
_, err := updater.UpdateContainerImage(context.Background(), containerTestImage, nil)
_, err := updater.UpdateContainerImage(context.Background(), containerTestImage, true, nil)
if err == nil || !strings.Contains(err.Error(), "active backend container backend-8080 is missing but inactive container backend-8081 is running") {
t.Fatalf("unexpected missing-active result: %v", err)
}
@@ -116,7 +116,7 @@ func TestContainerUpdaterRecoversFailedRollbackThenRetriesSameImage(t *testing.T
return nil
}
rollingBack, err := updater.UpdateContainerImage(ctx, containerTestImage, nil)
rollingBack, err := updater.UpdateContainerImage(ctx, containerTestImage, true, nil)
if err == nil || rollingBack.State != transaction.StateRollingBack {
t.Fatalf("unexpected failed rollback result: record=%+v err=%v", rollingBack, err)
}
@@ -126,12 +126,12 @@ func TestContainerUpdaterRecoversFailedRollbackThenRetriesSameImage(t *testing.T
}
failGateway = false
rolledBack, err := updater.UpdateContainerImage(ctx, containerTestImage, nil)
rolledBack, err := updater.UpdateContainerImage(ctx, containerTestImage, true, nil)
if err == nil || rolledBack.State != transaction.StateRolledBack {
t.Fatalf("unexpected resumed rollback result: record=%+v err=%v", rolledBack, err)
}
committed, err := updater.UpdateContainerImage(ctx, containerTestImage, nil)
committed, err := updater.UpdateContainerImage(ctx, containerTestImage, true, nil)
if err != nil || committed.State != transaction.StateCommitted {
t.Fatalf("retry same image after rollback: record=%+v err=%v", committed, err)
}
@@ -169,7 +169,7 @@ func TestContainerUpdaterRejectsMissingCommittedContainer(t *testing.T) {
t.Fatalf("commit previous backend deployment: %v", err)
}
_, err = updater.UpdateContainerImage(ctx, containerTestImage, nil)
_, err = updater.UpdateContainerImage(ctx, containerTestImage, true, nil)
if err == nil || !strings.Contains(err.Error(), "committed active backend container backend-8080 is missing") {
t.Fatalf("unexpected committed-container drift result: %v", err)
}
@@ -336,6 +336,10 @@ func (e *containerUpdateEngine) StartContainer(_ context.Context, name string) e
e.containers[name] = record
return nil
}
func (e *containerUpdateEngine) ContainerLogs(context.Context, string) (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader("")), nil
}
func (e *containerUpdateEngine) StopContainer(_ context.Context, name string) error {
e.stopped = append(e.stopped, name)
record, found := e.containers[name]