From 9c19d518dba924c6c1d4d625e471ce0b00d6d4d0 Mon Sep 17 00:00:00 2001 From: rick Date: Mon, 17 Nov 2025 17:39:27 +0800 Subject: [PATCH] chore: improve run service in docker --- cmd/service.go | 43 +++++++++++++++++++++++++++++++++++-------- go.mod | 2 +- go.sum | 20 ++++++++++++++++++++ 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/cmd/service.go b/cmd/service.go index 75b6da6d..ff5d8975 100644 --- a/cmd/service.go +++ b/cmd/service.go @@ -32,6 +32,8 @@ import ( "github.com/spf13/cobra" ) +const modeDockerInSystemService = "docker-in-system" + func createServiceCommand(execer fakeruntime.Execer) (c *cobra.Command) { opt := &serviceOption{ Execer: execer, @@ -58,18 +60,19 @@ DaoCloud: docker.m.daocloud.io/linuxsuren/api-testing`, flags := c.Flags() flags.StringVarP(&opt.scriptPath, "script-path", "", "", "The service script file path") flags.StringVarP(&opt.mode, "mode", "m", "", - fmt.Sprintf("Availeble values: %v", service.ServiceModeOS.All())) + fmt.Sprintf("Availeble values: %v", append(service.ServiceModeOS.All(), modeDockerInSystemService))) flags.StringVarP(&opt.image, "image", "", defaultImage, "The image of the service which as a container") flags.StringVarP(&opt.pull, "pull", "", "always", `Pull image before creating ("always"|"missing"|"never")`) flags.StringVarP(&opt.version, "version", "", version.GetVersion(), "The version of the service image") flags.StringVarP(&opt.LocalStorage, "local-storage", "", "/var/data/atest", "The local storage path which will be mounted into the container") + flags.IntVarP(&opt.port, "port", "", 8080, "The port of the service") flags.StringVarP(&opt.SecretServer, "secret-server", "", "", "The secret server URL") flags.StringVarP(&opt.SkyWalking, "skywalking", "", "", "Push the browser tracing data to the Apache SkyWalking URL") return } -const defaultImage = "linuxsuren.docker.scarf.sh/linuxsuren/api-testing" +const defaultImage = "ghcr.io/linuxsuren/api-testing" type serviceOption struct { action string @@ -80,6 +83,7 @@ type serviceOption struct { fakeruntime.Execer mode string pull string + port int SecretServer string SkyWalking string @@ -91,18 +95,41 @@ func (o *serviceOption) preRunE(c *cobra.Command, args []string) (err error) { o.stdOut = c.OutOrStdout() o.action = args[0] + serviceCommand := "atest" + serviceArgs := []string{"server", "--extension-registry=ghcr.io"} + if o.mode == modeDockerInSystemService { + if o.Execer.OS() == "windows" { + serviceCommand = "run" + serviceArgs = []string{"atest", "server", "--extension-registry=ghcr.io"} + o.mode = "docker" + } else { + serviceCommand = "docker" + serviceArgs = []string{"run", "-v=atest:/root/.config/atest", "-v=atest-ssh:/root/.ssh", + fmt.Sprintf("-p=%d:8080", o.port), "--pull", o.pull, fmt.Sprintf("%s:%s", o.image, o.version), + "atest", "server", "--extension-registry=ghcr.io"} + o.mode = string(service.ServiceModeOS) + } + } + if strings.Contains(o.version, "unknown") { + o.version = "" + } + if o.service, err = service.GetAvailableService(service.ServiceMode(o.mode), service.ContainerOption{ - Image: o.action, - Pull: o.pull, - Tag: o.version, - Writer: c.OutOrStdout(), + Image: o.image, + Pull: o.pull, + Tag: o.version, + Writer: c.OutOrStdout(), + Volumes: map[string]string{"atest": "/root/.config/atest"}, + Ports: map[int]int{o.port: 8080}, + Restart: "always", + Name: "atest", }, service.CommonService{ ID: "atest", Name: "atest", Description: "API Testing Server", - Command: "atest", - Args: []string{"server"}, + Command: serviceCommand, + Args: serviceArgs, Execer: o.Execer, }); err != nil { return diff --git a/go.mod b/go.mod index 55fdf380..79566609 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/invopop/jsonschema v0.7.0 github.com/jhump/protoreflect v1.15.3 github.com/linuxsuren/go-fake-runtime v0.0.5 - github.com/linuxsuren/go-service v0.0.1 + github.com/linuxsuren/go-service v0.0.2-0.20251117091849-c58edc8748d9 github.com/linuxsuren/unstructured v0.0.1 github.com/prometheus/client_golang v1.22.0 github.com/prometheus/common v0.67.2 diff --git a/go.sum b/go.sum index 95d6e8ef..5676602f 100644 --- a/go.sum +++ b/go.sum @@ -133,6 +133,26 @@ github.com/linuxsuren/go-fake-runtime v0.0.5 h1:x1qvuGMfly3L4BTwx6Hq5oUcuf/1u0kS github.com/linuxsuren/go-fake-runtime v0.0.5/go.mod h1:hlE6bZp76N3YPDsKi5YKOf1XmcJy4rvf8EtkTLYRYLw= github.com/linuxsuren/go-service v0.0.1 h1:GoeK2HLDlRh+QQvFlxOferGtDUwzO3YduumMJ0XYPJ8= github.com/linuxsuren/go-service v0.0.1/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw= +github.com/linuxsuren/go-service v0.0.2-0.20251117075600-96234f59e3f2 h1:vy8Y8NA7vjZmHE+28v2Un4mtc4DG1XtXo66hbeQ4pXE= +github.com/linuxsuren/go-service v0.0.2-0.20251117075600-96234f59e3f2/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw= +github.com/linuxsuren/go-service v0.0.2-0.20251117081324-130f4c6054f7 h1:/xN+kJ5Vo+FCCjasmapVw5lRMwb6sgdJZqTu02kfNag= +github.com/linuxsuren/go-service v0.0.2-0.20251117081324-130f4c6054f7/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw= +github.com/linuxsuren/go-service v0.0.2-0.20251117081529-39b6694d1e30 h1:C9aOFP9tVFcDKlQAd3X4rU43s+VmS/R/BNzHKWD6uhU= +github.com/linuxsuren/go-service v0.0.2-0.20251117081529-39b6694d1e30/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw= +github.com/linuxsuren/go-service v0.0.2-0.20251117082747-764ea1ba3825 h1:6CwFWPq2DjFrW1gbH2FtKPlNPCm1j0vgYBl5/LWeT6Y= +github.com/linuxsuren/go-service v0.0.2-0.20251117082747-764ea1ba3825/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw= +github.com/linuxsuren/go-service v0.0.2-0.20251117083110-fbec39b2f85d h1:+kMcMI8rB9VbpcfWH8h3lOH7sYjT0+uq0EWsDf1byf4= +github.com/linuxsuren/go-service v0.0.2-0.20251117083110-fbec39b2f85d/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw= +github.com/linuxsuren/go-service v0.0.2-0.20251117083330-afb28a74b935 h1:bAFGUhlWuRla9Di1Fq7alY9Q9lXeLDgHMQdyLPVbVOg= +github.com/linuxsuren/go-service v0.0.2-0.20251117083330-afb28a74b935/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw= +github.com/linuxsuren/go-service v0.0.2-0.20251117083554-81682ffea100 h1:7uMuI3tJB53C4+dlBrfF4FaQ9YxY3KvatUSZL9Kgo8A= +github.com/linuxsuren/go-service v0.0.2-0.20251117083554-81682ffea100/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw= +github.com/linuxsuren/go-service v0.0.2-0.20251117084203-71bf4d3ebe0f h1:XYw35A2a2XafK57MSSoyDICrTw+TOcUWWT0cqHE/Khw= +github.com/linuxsuren/go-service v0.0.2-0.20251117084203-71bf4d3ebe0f/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw= +github.com/linuxsuren/go-service v0.0.2-0.20251117091757-3ae9fc74c5b9 h1:5jCZ8Xh4Bxpq9pwrs+T4CBVCfG4gVTpx45t3s/0gnxI= +github.com/linuxsuren/go-service v0.0.2-0.20251117091757-3ae9fc74c5b9/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw= +github.com/linuxsuren/go-service v0.0.2-0.20251117091849-c58edc8748d9 h1:w5prP6ROOxInPV38KL+0laf7ZDIHlidBerhrphHqWHU= +github.com/linuxsuren/go-service v0.0.2-0.20251117091849-c58edc8748d9/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw= github.com/linuxsuren/http-downloader v0.0.99 h1:fEu+HkHdYeLM932c7IfmuaDJqWxVU5sIEnS/Aln8h9o= github.com/linuxsuren/http-downloader v0.0.99/go.mod h1:OngIAkbOJTMbd+IMRbt3TiWSizVJZvPfjdbTpl6uHLo= github.com/linuxsuren/oauth-hub v0.0.1 h1:5LAdX9ZlWhaM7P10rdxiXPk26eceYHRyfkFXsym6AxY=