-
Notifications
You must be signed in to change notification settings - Fork 13
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
If two artifacts provide the same file, re-link or re-copy that file from the artifact left after the other was undeployed/uninstalled. #3455
Changes from 4 commits
1b30be2
4eda2f2
fc84551
845e392
3aa4976
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,9 +28,10 @@ type depotConfig struct { | |
} | ||
|
||
type deployment struct { | ||
Type deploymentType `json:"type"` | ||
Path string `json:"path"` | ||
Files []string `json:"files"` | ||
Type deploymentType `json:"type"` | ||
Path string `json:"path"` | ||
Files []string `json:"files"` | ||
RelativeSrc string `json:"relativeSrc"` | ||
} | ||
|
||
type deploymentType string | ||
|
@@ -187,9 +188,10 @@ func (d *depot) DeployViaLink(id strfmt.UUID, relativeSrc, absoluteDest string) | |
d.config.Deployments[id] = []deployment{} | ||
} | ||
d.config.Deployments[id] = append(d.config.Deployments[id], deployment{ | ||
Type: deploymentTypeLink, | ||
Path: absoluteDest, | ||
Files: files.RelativePaths(), | ||
Type: deploymentTypeLink, | ||
Path: absoluteDest, | ||
Files: files.RelativePaths(), | ||
RelativeSrc: relativeSrc, | ||
}) | ||
|
||
return nil | ||
|
@@ -243,9 +245,10 @@ func (d *depot) DeployViaCopy(id strfmt.UUID, relativeSrc, absoluteDest string) | |
d.config.Deployments[id] = []deployment{} | ||
} | ||
d.config.Deployments[id] = append(d.config.Deployments[id], deployment{ | ||
Type: deploymentTypeCopy, | ||
Path: absoluteDest, | ||
Files: files.RelativePaths(), | ||
Type: deploymentTypeCopy, | ||
Path: absoluteDest, | ||
Files: files.RelativePaths(), | ||
RelativeSrc: relativeSrc, | ||
}) | ||
|
||
return nil | ||
|
@@ -270,16 +273,35 @@ func (d *depot) Undeploy(id strfmt.UUID, relativeSrc, path string) error { | |
if !ok { | ||
return errs.New("deployment for %s not found in depot", id) | ||
} | ||
deploy := sliceutils.Filter(deployments, func(d deployment) bool { return d.Path == path }) | ||
if len(deploy) != 1 { | ||
deployments = sliceutils.Filter(deployments, func(d deployment) bool { return d.Path == path }) | ||
if len(deployments) != 1 { | ||
return errs.New("no deployment found for %s in depot", path) | ||
} | ||
deploy := deployments[0] | ||
|
||
// Perform uninstall based on deployment type | ||
if err := smartlink.UnlinkContents(filepath.Join(d.Path(id), relativeSrc), path); err != nil { | ||
return errs.Wrap(err, "failed to unlink artifact") | ||
} | ||
|
||
// Re-link or re-copy any files provided by other artifacts. | ||
redeploys, err := d.getSharedFilesToRedeploy(id, deploy, path) | ||
if err != nil { | ||
return errs.Wrap(err, "failed to get shared files") | ||
} | ||
for sharedFile, relinkSrc := range redeploys { | ||
switch deploy.Type { | ||
case deploymentTypeLink: | ||
if err := smartlink.Link(relinkSrc, sharedFile); err != nil { | ||
return errs.Wrap(err, "failed to relink file") | ||
} | ||
case deploymentTypeCopy: | ||
if err := fileutils.CopyFile(relinkSrc, sharedFile); err != nil { | ||
return errs.Wrap(err, "failed to re-copy file") | ||
} | ||
} | ||
} | ||
|
||
// Write changes to config | ||
d.config.Deployments[id] = sliceutils.Filter(d.config.Deployments[id], func(d deployment) bool { return d.Path != path }) | ||
|
||
|
@@ -300,6 +322,40 @@ func (d *depot) validateVolume(absoluteDest string) error { | |
return nil | ||
} | ||
|
||
// getSharedFilesToRedeploy returns a map of deployed files to re-link to (or re-copy from) another | ||
// artifact that provides those files. The key is the deployed file path and the value is the | ||
// source path from another artifact. | ||
func (d *depot) getSharedFilesToRedeploy(id strfmt.UUID, deploy deployment, path string) (map[string]string, error) { | ||
// Map of deployed paths to other sources that provides those paths. | ||
redeploy := make(map[string]string, 0) | ||
|
||
// For each file deployed by this artifact, find another artifact (if any) that deploys its own copy. | ||
for _, relativeDeployedFile := range deploy.Files { | ||
deployedFile := filepath.Join(path, relativeDeployedFile) | ||
for artifactId, artifactDeployments := range d.config.Deployments { | ||
if artifactId == id { | ||
continue | ||
} | ||
|
||
for _, deployment := range artifactDeployments { | ||
for _, fileToDeploy := range deployment.Files { | ||
if relativeDeployedFile == fileToDeploy { | ||
// We'll want to redeploy this from other artifact's copy after undeploying the currently deployed version. | ||
newSrc := filepath.Join(d.Path(artifactId), deployment.RelativeSrc, relativeDeployedFile) | ||
logging.Debug("More than one artifact provides '%s'", relativeDeployedFile) | ||
logging.Debug("Will redeploy '%s' to '%s'", newSrc, deployedFile) | ||
redeploy[deployedFile] = newSrc | ||
goto nextDeployedFile | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Would you mind using an anonymous function or a var to break out of the outer loop? This might be my own personal pet peeve, but I find the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Three consecutive
looked hideous, so I tried an anonymous function. Hopefully that passes your filter :) |
||
} | ||
} | ||
} | ||
} | ||
nextDeployedFile: | ||
} | ||
|
||
return redeploy, nil | ||
} | ||
|
||
// Save will write config changes to disk (ie. links between depot artifacts and runtimes that use it). | ||
// It will also delete any stale artifacts which are not used by any runtime. | ||
func (d *depot) Save() error { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: You could simplify this with
fileutils.MkdirUnlessExists()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. I also found a case where we can use
fileutils.Mkdir()
instead ofos.Mkdir()
with a magic number.