2022-09-22 16:16:04 +08:00
|
|
|
package compose
|
|
|
|
|
2022-10-11 16:27:58 +08:00
|
|
|
import (
|
2023-02-14 14:19:26 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2022-10-11 16:27:58 +08:00
|
|
|
"github.com/compose-spec/compose-go/loader"
|
|
|
|
"github.com/compose-spec/compose-go/types"
|
|
|
|
)
|
2022-09-22 16:16:04 +08:00
|
|
|
|
|
|
|
func Up(filePath string) (string, error) {
|
2023-02-14 14:19:26 +08:00
|
|
|
stdout, err := cmd.Execf("docker-compose -f %s up -d", filePath)
|
2022-09-27 16:57:23 +08:00
|
|
|
return string(stdout), err
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Down(filePath string) (string, error) {
|
2023-02-14 14:19:26 +08:00
|
|
|
stdout, err := cmd.Execf("docker-compose -f %s down", filePath)
|
2022-12-06 15:59:25 +08:00
|
|
|
return string(stdout), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func Stop(filePath string) (string, error) {
|
2023-02-14 14:19:26 +08:00
|
|
|
stdout, err := cmd.Execf("docker-compose -f %s stop", filePath)
|
2022-09-27 16:57:23 +08:00
|
|
|
return string(stdout), err
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Restart(filePath string) (string, error) {
|
2023-02-14 14:19:26 +08:00
|
|
|
stdout, err := cmd.Execf("docker-compose -f %s restart", filePath)
|
2022-09-27 16:57:23 +08:00
|
|
|
return string(stdout), err
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2022-09-26 22:54:38 +08:00
|
|
|
|
2022-12-06 15:05:13 +08:00
|
|
|
func Operate(filePath, operation string) (string, error) {
|
2023-02-14 14:19:26 +08:00
|
|
|
stdout, err := cmd.Execf("docker-compose -f %s %s", filePath, operation)
|
2022-12-06 15:05:13 +08:00
|
|
|
return string(stdout), err
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:54:38 +08:00
|
|
|
func Rmf(filePath string) (string, error) {
|
2023-02-14 14:19:26 +08:00
|
|
|
stdout, err := cmd.Execf("docker-compose -f %s rm -f", filePath)
|
2022-09-27 16:57:23 +08:00
|
|
|
return string(stdout), err
|
2022-09-26 22:54:38 +08:00
|
|
|
}
|
2022-10-11 16:27:58 +08:00
|
|
|
|
|
|
|
func GetComposeProject(yml []byte, env map[string]string) (*types.Project, error) {
|
|
|
|
var configFiles []types.ConfigFile
|
|
|
|
configFiles = append(configFiles, types.ConfigFile{
|
|
|
|
Filename: "docker-compose.yml",
|
|
|
|
Content: yml},
|
|
|
|
)
|
|
|
|
details := types.ConfigDetails{
|
|
|
|
WorkingDir: "",
|
|
|
|
ConfigFiles: configFiles,
|
|
|
|
Environment: env,
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := loader.Load(details, func(options *loader.Options) {
|
|
|
|
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return project, nil
|
|
|
|
}
|