mirror of https://github.com/portainer/portainer
fix(libstack): fix compose run BE-11381 (#127)
parent
707fc91a32
commit
e112ddfbeb
|
@ -142,7 +142,7 @@ func withComposeService(
|
||||||
// Deploy creates and starts containers
|
// Deploy creates and starts containers
|
||||||
func (c *ComposeDeployer) Deploy(ctx context.Context, filePaths []string, options libstack.DeployOptions) error {
|
func (c *ComposeDeployer) Deploy(ctx context.Context, filePaths []string, options libstack.DeployOptions) error {
|
||||||
return withComposeService(ctx, filePaths, options.Options, func(composeService api.Service, project *types.Project) error {
|
return withComposeService(ctx, filePaths, options.Options, func(composeService api.Service, project *types.Project) error {
|
||||||
addServiceLabels(project)
|
addServiceLabels(project, false)
|
||||||
|
|
||||||
var opts api.UpOptions
|
var opts api.UpOptions
|
||||||
if options.ForceRecreate {
|
if options.ForceRecreate {
|
||||||
|
@ -164,12 +164,17 @@ func (c *ComposeDeployer) Deploy(ctx context.Context, filePaths []string, option
|
||||||
|
|
||||||
func (c *ComposeDeployer) Run(ctx context.Context, filePaths []string, serviceName string, options libstack.RunOptions) error {
|
func (c *ComposeDeployer) Run(ctx context.Context, filePaths []string, serviceName string, options libstack.RunOptions) error {
|
||||||
return withComposeService(ctx, filePaths, options.Options, func(composeService api.Service, project *types.Project) error {
|
return withComposeService(ctx, filePaths, options.Options, func(composeService api.Service, project *types.Project) error {
|
||||||
addServiceLabels(project)
|
addServiceLabels(project, true)
|
||||||
|
|
||||||
|
if err := composeService.Create(ctx, project, api.CreateOptions{}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
opts := api.RunOptions{
|
opts := api.RunOptions{
|
||||||
AutoRemove: options.Remove,
|
AutoRemove: options.Remove,
|
||||||
Command: options.Args,
|
Command: options.Args,
|
||||||
Detach: options.Detached,
|
Detach: options.Detached,
|
||||||
|
Service: serviceName,
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := composeService.RunOneOffContainer(ctx, project, opts); err != nil {
|
if _, err := composeService.RunOneOffContainer(ctx, project, opts); err != nil {
|
||||||
|
@ -235,7 +240,12 @@ func (c *ComposeDeployer) Config(ctx context.Context, filePaths []string, option
|
||||||
return payload, nil
|
return payload, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func addServiceLabels(project *types.Project) {
|
func addServiceLabels(project *types.Project, oneOff bool) {
|
||||||
|
oneOffLabel := "False"
|
||||||
|
if oneOff {
|
||||||
|
oneOffLabel = "True"
|
||||||
|
}
|
||||||
|
|
||||||
for i, s := range project.Services {
|
for i, s := range project.Services {
|
||||||
s.CustomLabels = map[string]string{
|
s.CustomLabels = map[string]string{
|
||||||
api.ProjectLabel: project.Name,
|
api.ProjectLabel: project.Name,
|
||||||
|
@ -243,7 +253,7 @@ func addServiceLabels(project *types.Project) {
|
||||||
api.VersionLabel: api.ComposeVersion,
|
api.VersionLabel: api.ComposeVersion,
|
||||||
api.WorkingDirLabel: "/",
|
api.WorkingDirLabel: "/",
|
||||||
api.ConfigFilesLabel: strings.Join(project.ComposeFiles, ","),
|
api.ConfigFilesLabel: strings.Join(project.ComposeFiles, ","),
|
||||||
api.OneoffLabel: "False",
|
api.OneoffLabel: oneOffLabel,
|
||||||
}
|
}
|
||||||
project.Services[i] = s
|
project.Services[i] = s
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,17 +35,14 @@ services:
|
||||||
|
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|
||||||
filePathOriginal, err := createFile(dir, "docker-compose.yml", composeFileContent)
|
filePathOriginal := createFile(t, dir, "docker-compose.yml", composeFileContent)
|
||||||
require.NoError(t, err)
|
filePathOverride := createFile(t, dir, "docker-compose-override.yml", overrideComposeFileContent)
|
||||||
|
|
||||||
filePathOverride, err := createFile(dir, "docker-compose-override.yml", overrideComposeFileContent)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
filePaths := []string{filePathOriginal, filePathOverride}
|
filePaths := []string{filePathOriginal, filePathOverride}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
err = w.Validate(ctx, filePaths, libstack.Options{ProjectName: projectName})
|
err := w.Validate(ctx, filePaths, libstack.Options{ProjectName: projectName})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
err = w.Pull(ctx, filePaths, libstack.Options{ProjectName: projectName})
|
err = w.Pull(ctx, filePaths, libstack.Options{ProjectName: projectName})
|
||||||
|
@ -73,14 +70,33 @@ services:
|
||||||
require.False(t, containerExists(composeContainerName))
|
require.False(t, containerExists(composeContainerName))
|
||||||
}
|
}
|
||||||
|
|
||||||
func createFile(dir, fileName, content string) (string, error) {
|
func TestRun(t *testing.T) {
|
||||||
|
w := NewComposeDeployer()
|
||||||
|
|
||||||
|
filePath := createFile(t, t.TempDir(), "docker-compose.yml", `
|
||||||
|
services:
|
||||||
|
updater:
|
||||||
|
image: alpine
|
||||||
|
`)
|
||||||
|
|
||||||
|
filePaths := []string{filePath}
|
||||||
|
serviceName := "updater"
|
||||||
|
|
||||||
|
err := w.Run(context.Background(), filePaths, serviceName, libstack.RunOptions{
|
||||||
|
Options: libstack.Options{
|
||||||
|
ProjectName: "project_name",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createFile(t *testing.T, dir, fileName, content string) string {
|
||||||
filePath := filepath.Join(dir, fileName)
|
filePath := filepath.Join(dir, fileName)
|
||||||
|
|
||||||
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
|
err := os.WriteFile(filePath, []byte(content), 0o644)
|
||||||
return "", err
|
require.NoError(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
return filePath, nil
|
return filePath
|
||||||
}
|
}
|
||||||
|
|
||||||
func containerExists(containerName string) bool {
|
func containerExists(containerName string) bool {
|
||||||
|
@ -101,8 +117,7 @@ func Test_Validate(t *testing.T) {
|
||||||
|
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|
||||||
filePathOriginal, err := createFile(dir, "docker-compose.yml", invalidComposeFileContent)
|
filePathOriginal := createFile(t, dir, "docker-compose.yml", invalidComposeFileContent)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
filePaths := []string{filePathOriginal}
|
filePaths := []string{filePathOriginal}
|
||||||
|
|
||||||
|
@ -110,7 +125,7 @@ func Test_Validate(t *testing.T) {
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
err = w.Validate(ctx, filePaths, libstack.Options{ProjectName: projectName})
|
err := w.Validate(ctx, filePaths, libstack.Options{ProjectName: projectName})
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -308,13 +323,11 @@ networks:
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
composeFilePath, err := createFile(dir, "docker-compose.yml", tc.composeFileContent)
|
composeFilePath := createFile(t, dir, "docker-compose.yml", tc.composeFileContent)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
envFilePath := ""
|
envFilePath := ""
|
||||||
if tc.envFileContent != "" {
|
if tc.envFileContent != "" {
|
||||||
envFilePath, err = createFile(dir, "stack.env", tc.envFileContent)
|
envFilePath = createFile(t, dir, "stack.env", tc.envFileContent)
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
w := NewComposeDeployer()
|
w := NewComposeDeployer()
|
||||||
|
|
Loading…
Reference in New Issue