Skip to content

Commit ad58082

Browse files
authored
chore: improve run service in docker (#862)
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
1 parent e1db079 commit ad58082

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

cmd/service.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
"github.com/spf13/cobra"
3333
)
3434

35+
const modeDockerInSystemService = "docker-in-system"
36+
3537
func createServiceCommand(execer fakeruntime.Execer) (c *cobra.Command) {
3638
opt := &serviceOption{
3739
Execer: execer,
@@ -58,18 +60,19 @@ DaoCloud: docker.m.daocloud.io/linuxsuren/api-testing`,
5860
flags := c.Flags()
5961
flags.StringVarP(&opt.scriptPath, "script-path", "", "", "The service script file path")
6062
flags.StringVarP(&opt.mode, "mode", "m", "",
61-
fmt.Sprintf("Availeble values: %v", service.ServiceModeOS.All()))
63+
fmt.Sprintf("Availeble values: %v", append(service.ServiceModeOS.All(), modeDockerInSystemService)))
6264
flags.StringVarP(&opt.image, "image", "", defaultImage, "The image of the service which as a container")
6365
flags.StringVarP(&opt.pull, "pull", "", "always", `Pull image before creating ("always"|"missing"|"never")`)
6466
flags.StringVarP(&opt.version, "version", "", version.GetVersion(), "The version of the service image")
6567
flags.StringVarP(&opt.LocalStorage, "local-storage", "", "/var/data/atest",
6668
"The local storage path which will be mounted into the container")
69+
flags.IntVarP(&opt.port, "port", "", 8080, "The port of the service")
6770
flags.StringVarP(&opt.SecretServer, "secret-server", "", "", "The secret server URL")
6871
flags.StringVarP(&opt.SkyWalking, "skywalking", "", "", "Push the browser tracing data to the Apache SkyWalking URL")
6972
return
7073
}
7174

72-
const defaultImage = "linuxsuren.docker.scarf.sh/linuxsuren/api-testing"
75+
const defaultImage = "ghcr.io/linuxsuren/api-testing"
7376

7477
type serviceOption struct {
7578
action string
@@ -80,6 +83,7 @@ type serviceOption struct {
8083
fakeruntime.Execer
8184
mode string
8285
pull string
86+
port int
8387

8488
SecretServer string
8589
SkyWalking string
@@ -91,18 +95,41 @@ func (o *serviceOption) preRunE(c *cobra.Command, args []string) (err error) {
9195
o.stdOut = c.OutOrStdout()
9296
o.action = args[0]
9397

98+
serviceCommand := "atest"
99+
serviceArgs := []string{"server", "--extension-registry=ghcr.io"}
100+
if o.mode == modeDockerInSystemService {
101+
if o.Execer.OS() == "windows" {
102+
serviceCommand = "run"
103+
serviceArgs = []string{"atest", "server", "--extension-registry=ghcr.io"}
104+
o.mode = "docker"
105+
} else {
106+
serviceCommand = "docker"
107+
serviceArgs = []string{"run", "-v=atest:/root/.config/atest", "-v=atest-ssh:/root/.ssh",
108+
fmt.Sprintf("-p=%d:8080", o.port), "--pull", o.pull, fmt.Sprintf("%s:%s", o.image, o.version),
109+
"atest", "server", "--extension-registry=ghcr.io"}
110+
o.mode = string(service.ServiceModeOS)
111+
}
112+
}
113+
if strings.Contains(o.version, "unknown") {
114+
o.version = ""
115+
}
116+
94117
if o.service, err = service.GetAvailableService(service.ServiceMode(o.mode),
95118
service.ContainerOption{
96-
Image: o.action,
97-
Pull: o.pull,
98-
Tag: o.version,
99-
Writer: c.OutOrStdout(),
119+
Image: o.image,
120+
Pull: o.pull,
121+
Tag: o.version,
122+
Writer: c.OutOrStdout(),
123+
Volumes: map[string]string{"atest": "/root/.config/atest"},
124+
Ports: map[int]int{o.port: 8080},
125+
Restart: "always",
126+
Name: "atest",
100127
}, service.CommonService{
101128
ID: "atest",
102129
Name: "atest",
103130
Description: "API Testing Server",
104-
Command: "atest",
105-
Args: []string{"server"},
131+
Command: serviceCommand,
132+
Args: serviceArgs,
106133
Execer: o.Execer,
107134
}); err != nil {
108135
return

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/invopop/jsonschema v0.7.0
2323
github.com/jhump/protoreflect v1.15.3
2424
github.com/linuxsuren/go-fake-runtime v0.0.5
25-
github.com/linuxsuren/go-service v0.0.1
25+
github.com/linuxsuren/go-service v0.0.2-0.20251117091849-c58edc8748d9
2626
github.com/linuxsuren/unstructured v0.0.1
2727
github.com/prometheus/client_golang v1.22.0
2828
github.com/prometheus/common v0.67.2

go.sum

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,26 @@ github.com/linuxsuren/go-fake-runtime v0.0.5 h1:x1qvuGMfly3L4BTwx6Hq5oUcuf/1u0kS
133133
github.com/linuxsuren/go-fake-runtime v0.0.5/go.mod h1:hlE6bZp76N3YPDsKi5YKOf1XmcJy4rvf8EtkTLYRYLw=
134134
github.com/linuxsuren/go-service v0.0.1 h1:GoeK2HLDlRh+QQvFlxOferGtDUwzO3YduumMJ0XYPJ8=
135135
github.com/linuxsuren/go-service v0.0.1/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw=
136+
github.com/linuxsuren/go-service v0.0.2-0.20251117075600-96234f59e3f2 h1:vy8Y8NA7vjZmHE+28v2Un4mtc4DG1XtXo66hbeQ4pXE=
137+
github.com/linuxsuren/go-service v0.0.2-0.20251117075600-96234f59e3f2/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw=
138+
github.com/linuxsuren/go-service v0.0.2-0.20251117081324-130f4c6054f7 h1:/xN+kJ5Vo+FCCjasmapVw5lRMwb6sgdJZqTu02kfNag=
139+
github.com/linuxsuren/go-service v0.0.2-0.20251117081324-130f4c6054f7/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw=
140+
github.com/linuxsuren/go-service v0.0.2-0.20251117081529-39b6694d1e30 h1:C9aOFP9tVFcDKlQAd3X4rU43s+VmS/R/BNzHKWD6uhU=
141+
github.com/linuxsuren/go-service v0.0.2-0.20251117081529-39b6694d1e30/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw=
142+
github.com/linuxsuren/go-service v0.0.2-0.20251117082747-764ea1ba3825 h1:6CwFWPq2DjFrW1gbH2FtKPlNPCm1j0vgYBl5/LWeT6Y=
143+
github.com/linuxsuren/go-service v0.0.2-0.20251117082747-764ea1ba3825/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw=
144+
github.com/linuxsuren/go-service v0.0.2-0.20251117083110-fbec39b2f85d h1:+kMcMI8rB9VbpcfWH8h3lOH7sYjT0+uq0EWsDf1byf4=
145+
github.com/linuxsuren/go-service v0.0.2-0.20251117083110-fbec39b2f85d/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw=
146+
github.com/linuxsuren/go-service v0.0.2-0.20251117083330-afb28a74b935 h1:bAFGUhlWuRla9Di1Fq7alY9Q9lXeLDgHMQdyLPVbVOg=
147+
github.com/linuxsuren/go-service v0.0.2-0.20251117083330-afb28a74b935/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw=
148+
github.com/linuxsuren/go-service v0.0.2-0.20251117083554-81682ffea100 h1:7uMuI3tJB53C4+dlBrfF4FaQ9YxY3KvatUSZL9Kgo8A=
149+
github.com/linuxsuren/go-service v0.0.2-0.20251117083554-81682ffea100/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw=
150+
github.com/linuxsuren/go-service v0.0.2-0.20251117084203-71bf4d3ebe0f h1:XYw35A2a2XafK57MSSoyDICrTw+TOcUWWT0cqHE/Khw=
151+
github.com/linuxsuren/go-service v0.0.2-0.20251117084203-71bf4d3ebe0f/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw=
152+
github.com/linuxsuren/go-service v0.0.2-0.20251117091757-3ae9fc74c5b9 h1:5jCZ8Xh4Bxpq9pwrs+T4CBVCfG4gVTpx45t3s/0gnxI=
153+
github.com/linuxsuren/go-service v0.0.2-0.20251117091757-3ae9fc74c5b9/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw=
154+
github.com/linuxsuren/go-service v0.0.2-0.20251117091849-c58edc8748d9 h1:w5prP6ROOxInPV38KL+0laf7ZDIHlidBerhrphHqWHU=
155+
github.com/linuxsuren/go-service v0.0.2-0.20251117091849-c58edc8748d9/go.mod h1:QX22v61PxpOfJa4Xug8qzGTbPjclDZFx2j1PlGLknJw=
136156
github.com/linuxsuren/http-downloader v0.0.99 h1:fEu+HkHdYeLM932c7IfmuaDJqWxVU5sIEnS/Aln8h9o=
137157
github.com/linuxsuren/http-downloader v0.0.99/go.mod h1:OngIAkbOJTMbd+IMRbt3TiWSizVJZvPfjdbTpl6uHLo=
138158
github.com/linuxsuren/oauth-hub v0.0.1 h1:5LAdX9ZlWhaM7P10rdxiXPk26eceYHRyfkFXsym6AxY=

0 commit comments

Comments
 (0)