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-03-20 18:16:26 +08:00
|
|
|
stdout, err := cmd.Execf("docker-compose -f %s up -d", filePath)
|
2023-02-27 17:22:14 +08:00
|
|
|
return 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)
|
2023-02-27 17:22:14 +08:00
|
|
|
return stdout, err
|
2022-12-06 15:59:25 +08:00
|
|
|
}
|
|
|
|
|
2023-03-01 11:51:49 +08:00
|
|
|
func Start(filePath string) (string, error) {
|
|
|
|
stdout, err := cmd.Execf("docker-compose -f %s start", filePath)
|
|
|
|
return stdout, err
|
|
|
|
}
|
|
|
|
|
2022-12-06 15:59:25 +08:00
|
|
|
func Stop(filePath string) (string, error) {
|
2023-02-14 14:19:26 +08:00
|
|
|
stdout, err := cmd.Execf("docker-compose -f %s stop", filePath)
|
2023-02-27 17:22:14 +08:00
|
|
|
return 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)
|
2023-02-27 17:22:14 +08:00
|
|
|
return 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)
|
2023-02-27 17:22:14 +08:00
|
|
|
return 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
|
|
|
|
}
|