-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c83d08b
commit 0062cee
Showing
4 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
Copyright 2024 LinuxSuRen. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/linuxsuren/api-testing/pkg/version" | ||
fakeruntime "github.com/linuxsuren/go-fake-runtime" | ||
service "github.com/linuxsuren/go-service" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func createServiceCommand(execer fakeruntime.Execer) (c *cobra.Command) { | ||
opt := &serviceOption{ | ||
Execer: execer, | ||
} | ||
c = &cobra.Command{ | ||
Use: "service", | ||
Aliases: []string{"s"}, | ||
Example: `atest service install | ||
atest service start`, | ||
Short: "Install atest as service", | ||
Long: `It could be a native or container service. | ||
Try use sudo if you met any permission issues. | ||
You could choose the alternative images: | ||
Docker Hub: docker.io/linuxsuren/api-testing | ||
GitHub Container Registry: ghcr.io/linuxsuren/api-testing | ||
Scarf: linuxsuren.docker.scarf.sh/linuxsuren/api-testing | ||
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.scriptPath, "script-path", "", "", "The service script file path") | ||
flags.StringVarP(&opt.mode, "mode", "m", "", | ||
fmt.Sprintf("Availeble values: %v", service.ServiceModeOS.All())) | ||
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.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" | ||
|
||
type serviceOption struct { | ||
action string | ||
scriptPath string | ||
service service.Service | ||
image string | ||
version string | ||
fakeruntime.Execer | ||
mode string | ||
pull string | ||
|
||
SecretServer string | ||
SkyWalking string | ||
LocalStorage string | ||
stdOut io.Writer | ||
} | ||
|
||
func (o *serviceOption) preRunE(c *cobra.Command, args []string) (err error) { | ||
o.stdOut = c.OutOrStdout() | ||
o.action = args[0] | ||
|
||
if o.service, err = service.GetAvailableService(service.ServiceMode(o.mode), | ||
service.ContainerOption{ | ||
Image: o.action, | ||
Pull: o.pull, | ||
Tag: o.version, | ||
Writer: c.OutOrStdout(), | ||
}, service.CommonService{ | ||
ID: "atest-collector", | ||
Name: "atest-collector", | ||
Description: "atest-collector", | ||
Command: "atest-collector", | ||
Args: []string{"controller", "/Users/rick/.config/atest/sample_controller.yaml"}, | ||
Execer: o.Execer, | ||
}); err != nil { | ||
return | ||
} | ||
|
||
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 | ||
} | ||
|
||
func (o *serviceOption) runE(c *cobra.Command, args []string) (err error) { | ||
var output string | ||
switch Action(o.action) { | ||
case ActionInstall: | ||
output, err = o.service.Install() | ||
case ActionUninstall: | ||
output, err = o.service.Uninstall() | ||
case ActionStart: | ||
output, err = o.service.Start() | ||
case ActionStop: | ||
output, err = o.service.Stop() | ||
case ActionRestart: | ||
output, err = o.service.Restart() | ||
case ActionStatus: | ||
output, err = o.service.Status() | ||
default: | ||
err = fmt.Errorf("not support action: %q", o.action) | ||
} | ||
|
||
output = strings.TrimSpace(output) | ||
if output != "" { | ||
c.Println(output) | ||
} | ||
return | ||
} | ||
|
||
type Action string | ||
|
||
const ( | ||
ActionInstall Action = "install" | ||
ActionUninstall Action = "uninstall" | ||
ActionStart Action = "start" | ||
ActionStop Action = "stop" | ||
ActionRestart Action = "restart" | ||
ActionStatus Action = "status" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters