Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a custom docker-file.override.yml #158

Merged
merged 1 commit into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cmd/sourced/compose/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,33 @@ func InitDefault() (string, error) {
return activeFilePath, nil
}

// InitDefaultOverride returns the path of the default docker-compose.override.yml file,
// if it does not exists, it is created.
func InitDefaultOverride() (string, error) {
composeDirPath, err := dir()
if err != nil {
return "", err
}

globalOverridePath := filepath.Join(composeDirPath, activeDir, "docker-compose.override.yml")

_, err = os.Stat(globalOverridePath)
if err == nil {
return globalOverridePath, nil
}

if !os.IsNotExist(err) {
return "", errors.Wrap(err, "could not read the global docker-compose.override.yml file")
}

content := []byte(`version: '3.2'`)
if err := ioutil.WriteFile(globalOverridePath, content, 0644); err != nil {
return "", errors.Wrap(err, "could not create the global docker-compose.override.yml file")
}

return globalOverridePath, nil
}

// Download downloads the docker-compose.yml file from the given revision
// or URL. The file is set as the active compose file.
func Download(revOrURL RevOrURL) error {
Expand Down
54 changes: 39 additions & 15 deletions cmd/sourced/compose/workdir/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ var (
// RequiredFiles list of required files in a directory to treat it as a working directory
RequiredFiles = []string{".env", "docker-compose.yml"}

// OptionalFiles list of optional files that could be deleted when pruning
OptionalFiles = []string{"docker-compose.override.yml"}

// ErrMalformed is the returned error when the workdir is wrong
ErrMalformed = goerrors.NewKind("workdir %s is not valid: %s")
)
Expand Down Expand Up @@ -191,16 +194,21 @@ func ListPaths() ([]string, error) {
return res, nil
}

// RemovePath removes working directory by removing required files
// RemovePath removes working directory by removing required and optional files,
// and recursively removes directories up to the workdirs root as long as they are empty
func RemovePath(path string) error {
workdirsRoot, err := workdirsPath()
if err != nil {
return err
}

for _, f := range RequiredFiles {
if err := os.Remove(filepath.Join(path, f)); err != nil {
for _, f := range append(RequiredFiles, OptionalFiles...) {
dpordomingo marked this conversation as resolved.
Show resolved Hide resolved
file := filepath.Join(path, f)
if _, err := os.Stat(file); os.IsNotExist(err) {
continue
}

if err := os.Remove(file); err != nil {
return errors.Wrap(err, "could not remove from workdir directory")
}
}
Expand Down Expand Up @@ -304,27 +312,29 @@ func isEmptyFile(path string) (bool, error) {

// common initialization for both local and remote data
func initWorkdir(workdirPath string, envFile envFile) error {
defaultFilePath, err := composefile.InitDefault()
err := os.MkdirAll(workdirPath, 0755)
if err != nil {
return err
return errors.Wrap(err, "could not create working directory")
}

err = os.MkdirAll(workdirPath, 0755)
defaultFilePath, err := composefile.InitDefault()
if err != nil {
return errors.Wrap(err, "could not create working directory")
return err
}

composePath := filepath.Join(workdirPath, "docker-compose.yml")
_, err = os.Stat(composePath)
if err := link(defaultFilePath, composePath); err != nil {
return err
}

defaultOverridePath, err := composefile.InitDefaultOverride()
if err != nil {
if !os.IsNotExist(err) {
return errors.Wrap(err, "could not read the existing docker-compose.yml file")
}
return err
}

err = os.Symlink(defaultFilePath, composePath)
if err != nil {
return errors.Wrap(err, "could not create symlink to docker-compose.yml file")
}
workdirOverridePath := filepath.Join(workdirPath, "docker-compose.override.yml")
if err := link(defaultOverridePath, workdirOverridePath); err != nil {
return err
}

envPath := filepath.Join(workdirPath, ".env")
Expand All @@ -344,6 +354,20 @@ func initWorkdir(workdirPath string, envFile envFile) error {
return nil
}

func link(linkTargetPath, linkPath string) error {
_, err := os.Stat(linkPath)
if err == nil {
return nil
}

if !os.IsNotExist(err) {
return errors.Wrap(err, "could not read the existing FILE_NAME file")
}

err = os.Symlink(linkTargetPath, linkPath)
return errors.Wrap(err, fmt.Sprintf("could not create symlink to %s", linkTargetPath))
}

func workdirsPath() (string, error) {
path, err := datadir.Path()
if err != nil {
Expand Down