Skip to content

Commit

Permalink
fix: the service command not working in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Dec 13, 2023
1 parent 0d9016e commit 1263afc
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 338 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,19 @@ jobs:
sudo atest service status
atest run -p '.github/testing/*.yaml' --request-ignore-error --report github --report-file bin/report.json --report-github-repo linuxsuren/api-testing --report-github-pr ${{ github.event.number }}
sudo atest service status
sudo atest service stop
sudo atest service uninstall
atest convert -p .github/testing/core.yaml --converter jmeter -t sample.jmx
- name: Run JMeter Tests
uses: rbhadti94/[email protected]
with:
testFilePath: sample.jmx
- name: Service Test
run: |
docker version
sudo atest service start -m docker
curl http://localhost:8080/healthz
Build:
runs-on: ubuntu-20.04
Expand Down
284 changes: 20 additions & 264 deletions cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,22 @@ AliYun: registry.aliyuncs.com/linuxsuren/api-testing
DaoCloud: docker.m.daocloud.io/linuxsuren/api-testing`,
PreRunE: opt.preRunE,
RunE: opt.runE,
Args: cobra.MinimumNArgs(1),
}
flags := c.Flags()
flags.StringVarP(&opt.action, "action", "a", "",
fmt.Sprintf("The action of the service, available values: %v", Action("").All()))
flags.StringVarP(&opt.scriptPath, "script-path", "", "", "The service script file path")
flags.StringVarP(&opt.mode, "mode", "m", string(ServiceModeOS),
fmt.Sprintf("Availeble values: %v", ServiceModeOS.All()))
flags.StringVarP(&opt.image, "image", "", defaultImage, "The image of the service which as a container")
flags.StringVarP(&opt.mode, "mode", "m", "",
fmt.Sprintf("Availeble values: %v", service.ServiceModeOS.All()))
flags.StringVarP(&opt.image, "image", "", service.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",
flags.StringVarP(&opt.LocalStorage, "local-storage", "", "/var/data/atest",
"The local storage path which will be mounted into the container")
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")
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
}

type serverFeatureOption struct {
secretServer string
skyWalking string
localStorage string
}

type serviceOption struct {
action string
scriptPath string
Expand All @@ -69,46 +62,29 @@ type serviceOption struct {
mode string
pull string

serverFeatureOption
service.ServerFeatureOption
stdOut io.Writer
}

type serviceMode string

const (
ServiceModeOS serviceMode = "os"
ServiceModeContainer serviceMode = "container"
ServiceModePodman serviceMode = "podman"
ServiceModeDocker serviceMode = "docker"
)

func (s serviceMode) All() []serviceMode {
return []serviceMode{ServiceModeOS, ServiceModeContainer,
ServiceModePodman, ServiceModeDocker}
}

func (s serviceMode) String() string {
return string(s)
}

func (o *serviceOption) preRunE(c *cobra.Command, args []string) (err error) {
o.stdOut = c.OutOrStdout()
if o.action == "" && len(args) > 0 {
o.action = args[0]
}
o.action = args[0]

switch serviceMode(o.mode) {
case ServiceModeOS:
o.service = service.NewService(o.Execer, o.scriptPath)
default:
o.service, err = o.getContainerService()
}
o.service = service.GetAvailableService(service.ServiceMode(o.mode), o.Execer,
service.ContainerOption{
Image: o.action,
Pull: o.pull,
Tag: o.version,
Writer: c.OutOrStdout(),
}, o.ServerFeatureOption, o.scriptPath)

if o.service == nil {
err = fmt.Errorf("not supported service")
return
} else if err == nil {
err = o.Execer.MkdirAll(o.localStorage, os.ModePerm)
local := os.ExpandEnv("$HOME/.config/atest")
if err = o.Execer.MkdirAll(local, os.ModePerm); err == nil {
err = o.Execer.MkdirAll(o.LocalStorage, os.ModePerm)
}
}
return
}
Expand Down Expand Up @@ -149,223 +125,3 @@ const (
ActionRestart Action = "restart"
ActionStatus Action = "status"
)

func (a Action) All() []Action {
return []Action{ActionInstall, ActionUninstall,
ActionStart, ActionStop,
ActionRestart, ActionStatus}
}

func emptyThenDefault(value, defaultValue string) string {
if value == "" {
value = defaultValue
}
return value
}

func (o *serviceOption) getOSService() (svc service.Service, err error) {
//if o.Execer.OS() != fakeruntime.OSLinux && o.Execer.OS() != fakeruntime.OSDarwin {
// err = fmt.Errorf("only support on Linux/Darwin instead of %s", o.Execer.OS())
//} else {
// svc = service.NewService(o.Execer, o.scriptPath)
//}
svc = service.NewService(o.Execer, o.scriptPath)
return
}

func (o *serviceOption) getContainerService() (service service.Service, err error) {
var client string
switch serviceMode(o.mode) {
case ServiceModeDocker:
client = ServiceModeDocker.String()
case ServiceModePodman, ServiceModeContainer:
client = ServiceModePodman.String()
default:
err = fmt.Errorf("not support mode: '%s'", o.mode)
return
}

var clientPath string
if clientPath, err = o.LookPath(client); err == nil {
if clientPath == "" {
clientPath = client
}
service = newContainerService(o.Execer, clientPath,
o.image, o.version, o.pull, o.serverFeatureOption, o.stdOut)
}
return
}

type commonService struct {
fakeruntime.Execer
scriptPath string
script string
}

type containerService struct {
Execer fakeruntime.Execer
name string
client string
image string
tag string
pull string
localStorage string
secretServer string
skyWalking string
stdOut io.Writer
errOut io.Writer
}

const defaultImage = "linuxsuren.docker.scarf.sh/linuxsuren/api-testing"

func newContainerService(execer fakeruntime.Execer, client, image, tag, pull string,
featureOption serverFeatureOption, writer io.Writer) (svc service.Service) {
if tag == "" {
tag = "latest"
}
if image == "" {
image = defaultImage
}

containerServer := &containerService{
Execer: execer,
client: client,
name: service.ServiceName,
image: image,
tag: tag,
pull: pull,
localStorage: featureOption.localStorage,
secretServer: featureOption.secretServer,
skyWalking: featureOption.skyWalking,
stdOut: writer,
errOut: writer,
}

if strings.HasSuffix(client, ServiceModePodman.String()) {
svc = &podmanService{
containerService: containerServer,
}
} else {
svc = containerServer
}
return
}

func (s *containerService) Start() (output string, err error) {
if s.exist() {
output, err = s.Execer.RunCommandAndReturn(s.client, "", "start", s.name)
} else {
err = s.Execer.SystemCall(s.client, append([]string{s.client}, s.getStartArgs()...), os.Environ())
}
return
}

func (s *containerService) Stop() (output string, err error) {
output, err = s.Execer.RunCommandAndReturn(s.client, "", "stop", s.name)
return
}

func (s *containerService) Restart() (output string, err error) {
output, err = s.Execer.RunCommandAndReturn(s.client, "", "restart", s.name)
return
}

func (s *containerService) Status() (_ string, err error) {
err = s.Execer.SystemCall(s.client, []string{s.client, "stats", s.name}, os.Environ())
return
}

func (s *containerService) Install() (output string, err error) {
output, err = s.Start()
return
}

func (s *containerService) Uninstall() (output string, err error) {
output, err = s.Stop()
if err == nil {
output, err = s.Execer.RunCommandAndReturn(s.client, "", "rm", s.name)
}
return
}

func (s *containerService) exist() bool {
output, err := s.Execer.RunCommandAndReturn(s.client, "", "ps", "--all", "--filter", fmt.Sprintf("name=%s", s.name))
return err == nil && strings.Contains(output, s.name)
}

func (s *containerService) getStartArgs() []string {
args := []string{"run", "--name=" + s.name,
"--restart=always",
"-d",
fmt.Sprintf("--pull=%s", s.pull),
"--network=host",
"-v", s.localStorage + ":/var/www/data",
"-v", os.ExpandEnv("$HOME/.config/atest:/root/.config/atest"),
s.image + ":" + s.tag,
"atest", "server"}
if s.secretServer != "" {
args = append(args, "--secret-server="+s.secretServer)
}
if s.skyWalking != "" {
args = append(args, "--skywalking="+s.skyWalking)
}
args = append(args, "--console-path=/var/www/html")
return args
}

type podmanService struct {
*containerService
}

func (s *podmanService) Install() (output string, err error) {
output, err = s.Start()
return
}

func (s *podmanService) Start() (output string, err error) {
if s.exist() {
err = s.Execer.RunCommandWithIO(s.client, "", s.stdOut, s.errOut, nil, "start", s.name)
} else {
err = s.Execer.RunCommandWithIO(s.client, "", s.stdOut, s.errOut, nil, s.getStartArgs()...)
if err == nil {
output, err = s.installService()
}
}
return
}

func (s *podmanService) Stop() (output string, err error) {
output, err = s.Execer.RunCommandAndReturn(service.SystemCtl, "", "stop", service.PodmanServiceName)
return
}

func (s *podmanService) installService() (output string, err error) {
output, err = s.Execer.RunCommandAndReturn(s.client, "", "generate", "systemd", "--new", "--files", "--name", s.name)
if err == nil {
var result string
result, err = s.Execer.RunCommandAndReturn("mv", "", service.PodmanServiceName, "/etc/systemd/system")
if err == nil {
output = fmt.Sprintf("%s\n%s", output, result)
if result, err = s.Execer.RunCommandAndReturn(service.SystemCtl, "", "enable", service.PodmanServiceName); err == nil {
output = fmt.Sprintf("%s\n%s", output, result)
}
}
}
return
}

func (s *podmanService) Uninstall() (output string, err error) {
output, err = s.containerService.Uninstall()
if err == nil {
var result string
if result, err = s.uninstallService(); err == nil {
output = fmt.Sprintf("%s\n%s", output, result)
}
}
return
}

func (s *podmanService) uninstallService() (output string, err error) {
output, err = s.Execer.RunCommandAndReturn(service.SystemCtl, "", "disable", service.PodmanServiceName)
return
}
Loading

0 comments on commit 1263afc

Please sign in to comment.