Merge pull request #14 from janeczku/cache-revamp

Separate 'archive' parameter in 'load' and 'save'
This commit is contained in:
Brad Rydzewski 2015-11-19 13:22:06 -08:00
commit f510688b48
2 changed files with 37 additions and 26 deletions

12
DOCS.md
View File

@ -9,9 +9,10 @@ The following parameters are used to configure this plugin:
* `tag` - repository tag for the image
* `insecure` - enable insecure communication to this registry
* `storage_driver` - use `aufs`, `devicemapper`, `btrfs` or `overlay` driver
* `archive` - save and restore image layers to/from a tarred archive
* `file` - absolute or relative path to archive file
* `tag` - limit archiving to these tag(s) (optional)
* `save` - save image layers to the specified tar file (see [docker save](https://docs.docker.com/engine/reference/commandline/save/))
* `file` - absolute / relative destination path
* `tags` - cherry-pick tags to save (optional)
* `load` - restore image layers from the specified tar file
The following is a sample Docker configuration in your .drone.yml file:
@ -68,9 +69,10 @@ publish:
tag:
- latest
- "1.0.1"
archive:
load: docker/image.tar
save:
file: docker/image.tar
tag: latest
tags: latest
cache:
mount:

51
main.go
View File

@ -12,9 +12,11 @@ import (
"github.com/drone/drone-plugin-go/plugin"
)
type Archive struct {
File string `json:"file"`
Tag StrSlice `json:"tag"`
type Save struct {
// Absolute or relative path
File string `json:"destination"`
// Only save specified tags (optional)
Tags StrSlice `json:"tag"`
}
type Docker struct {
@ -30,7 +32,8 @@ type Docker struct {
File string `json:"file"`
Context string `json:"context"`
Dns []string `json:"dns"`
Archive Archive `json:"archive"`
Load string `json:"load"`
Save Save `json:"save"`
}
func main() {
@ -66,10 +69,16 @@ func main() {
if vargs.Tag.Len() == 0 {
vargs.Tag = StrSlice{[]string{"latest"}}
}
// Archive file can be both a relative or absolute path
if len(vargs.Archive.File) != 0 {
if ! filepath.IsAbs(vargs.Archive.File) {
vargs.Archive.File = filepath.Join(workspace.Path, vargs.Archive.File)
// Get absolute path for 'save' file
if len(vargs.Save.File) != 0 {
if ! filepath.IsAbs(vargs.Save.File) {
vargs.Save.File = filepath.Join(workspace.Path, vargs.Save.File)
}
}
// Get absolute path for 'load' file
if len(vargs.Load) != 0 {
if ! filepath.IsAbs(vargs.Load) {
vargs.Load = filepath.Join(workspace.Path, vargs.Load)
}
}
@ -138,12 +147,12 @@ func main() {
trace(cmd)
cmd.Run()
// Load archived image if exists
if len(vargs.Archive.File) != 0 {
if _, err := os.Stat(vargs.Archive.File); err != nil {
fmt.Printf("Archive %s does not exist. Building from scratch.\n", vargs.Archive.File)
// Restore from tarred image repository
if len(vargs.Load) != 0 {
if _, err := os.Stat(vargs.Load); err != nil {
fmt.Printf("Archive %s does not exist. Building from scratch.\n", vargs.Load)
} else {
cmd := exec.Command("/usr/bin/docker", "load", "-i", vargs.Archive.File)
cmd := exec.Command("/usr/bin/docker", "load", "-i", vargs.Load)
cmd.Dir = workspace.Path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@ -195,17 +204,17 @@ func main() {
}
}
// Save the image to the archive
if len(vargs.Archive.File) != 0 {
// if the path's directory does not exist, create it
dir := filepath.Dir(vargs.Archive.File)
// Save to tarred image repository
if len(vargs.Save.File) != 0 {
// if the destination directory does not exist, create it
dir := filepath.Dir(vargs.Save.File)
os.MkdirAll(dir, 0755)
cmd = exec.Command("/usr/bin/docker", "save", "-o", vargs.Archive.File)
cmd = exec.Command("/usr/bin/docker", "save", "-o", vargs.Save.File)
// Limit save command to the given tag(s)
if vargs.Archive.Tag.Len() != 0 {
for _, tag := range vargs.Archive.Tag.Slice() {
// Limit saving to the given tags
if vargs.Save.Tags.Len() != 0 {
for _, tag := range vargs.Save.Tags.Slice() {
name_ := fmt.Sprintf("%s:%s", vargs.Repo, tag)
cmd.Args = append(cmd.Args, name_)
}