-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
02b40b3
commit 59fb0bc
Showing
11 changed files
with
93 additions
and
79 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
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
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
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 |
---|---|---|
@@ -1,47 +1,68 @@ | ||
package ddevapp | ||
|
||
import ( | ||
"github.com/ddev/ddev/pkg/fileutil" | ||
"github.com/ddev/ddev/pkg/globalconfig" | ||
"github.com/ddev/ddev/pkg/util" | ||
copy2 "github.com/otiai10/copy" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
dockerImages "github.com/ddev/ddev/pkg/docker" | ||
"github.com/ddev/ddev/pkg/dockerutil" | ||
"github.com/ddev/ddev/pkg/globalconfig" | ||
"github.com/ddev/ddev/pkg/nodeps" | ||
"github.com/ddev/ddev/pkg/util" | ||
) | ||
|
||
// PopulateCustomCommandFiles sets up the custom command files in the project | ||
// PopulateGlobalCustomCommandFiles sets up the custom command files in the project | ||
// directories where they need to go. | ||
func PopulateCustomCommandFiles(app *DdevApp) error { | ||
|
||
func PopulateGlobalCustomCommandFiles() error { | ||
sourceGlobalCommandPath := filepath.Join(globalconfig.GetGlobalDdevDir(), "commands") | ||
err := os.MkdirAll(sourceGlobalCommandPath, 0755) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
projectCommandPath := app.GetConfigPath("commands") | ||
// Make sure our target global command directory is empty | ||
copiedGlobalCommandPath := app.GetConfigPath(".global_commands") | ||
err = os.MkdirAll(copiedGlobalCommandPath, 0755) | ||
// Remove contents of the directory, if the directory exists and has some contents | ||
commandDirInVolume := "/mnt/ddev-global-cache/global-commands/" | ||
_, _, err = performTaskInContainer([]string{"rm", "-rf", commandDirInVolume}) | ||
if err != nil { | ||
util.Error("Unable to create directory %s: %v", copiedGlobalCommandPath, err) | ||
return nil | ||
return fmt.Errorf("unable to rm %s: %v", commandDirInVolume, err) | ||
} | ||
|
||
// Make sure it's empty | ||
err = fileutil.PurgeDirectory(copiedGlobalCommandPath) | ||
// Copy commands into container (this will create the directory if it's not there already) | ||
uid, _, _ := util.GetContainerUIDGid() | ||
err = dockerutil.CopyIntoVolume(sourceGlobalCommandPath, "ddev-global-cache", "global-commands", uid, "host", false) | ||
if err != nil { | ||
util.Error("Unable to remove %s: %v", copiedGlobalCommandPath, err) | ||
return nil | ||
return err | ||
} | ||
|
||
err = copy2.Copy(sourceGlobalCommandPath, copiedGlobalCommandPath) | ||
// Make sure all commands can be executed | ||
_, stderr, err := performTaskInContainer([]string{"sh", "-c", "chmod -R u+rwx " + commandDirInVolume}) | ||
if err != nil { | ||
return err | ||
return fmt.Errorf("unable to chmod %s: %v (stderr=%s)", commandDirInVolume, err, stderr) | ||
} | ||
|
||
if !fileutil.FileExists(projectCommandPath) || !fileutil.IsDirectory(projectCommandPath) { | ||
return nil | ||
} | ||
return nil | ||
} | ||
|
||
// performTaskInContainer runs a command in the web container if it's available, | ||
// but uses an anonymous container otherwise. | ||
func performTaskInContainer(command []string) (string, string, error) { | ||
app, err := GetActiveApp("") | ||
if err == nil { | ||
status, _ := app.SiteStatus() | ||
if status == SiteRunning { | ||
// Prepare docker exec command | ||
opts := &ExecOpts{ | ||
RawCmd: command, | ||
Tty: false, | ||
NoCapture: false, | ||
} | ||
return app.Exec(opts) | ||
} | ||
} | ||
|
||
// If there is no running active site, use an anonymous container instead. | ||
containerName := "performTaskInContainer" + nodeps.RandomString(12) | ||
uid, _, _ := util.GetContainerUIDGid() | ||
return dockerutil.RunSimpleContainer(dockerImages.GetWebImage(), containerName, command, nil, nil, []string{"ddev-global-cache:/mnt/ddev-global-cache"}, uid, true, false, map[string]string{"com.ddev.site-name": ""}, nil) | ||
} |
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
Oops, something went wrong.