Skip to content

Commit

Permalink
feat(tools/debpacker): variables interpolation, extracts files (#4772)
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <francois.samin@corp.ovh.com>
  • Loading branch information
fsamin authored and sguiheux committed Nov 21, 2019
1 parent bed2385 commit 91019a8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
39 changes: 33 additions & 6 deletions tools/debpacker/debpacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ func (p Packer) Config() Config { return p.config }

// Config struct.
type Config struct {
PackageName string `yaml:"package-name"`
Architecture string `yaml:"architecture"`
BinaryFile string `yaml:"binary-file"`
Command string `yaml:"command,omitempty"`
ConfigurationFiles []string `yaml:"configuration-files,omitempty"`
CopyFiles []string `yaml:"copy-files,omitempty"`
PackageName string `yaml:"package-name"`
Architecture string `yaml:"architecture"`
BinaryFile string `yaml:"binary-file"`
Command string `yaml:"command,omitempty"`
ConfigurationFiles []string `yaml:"configuration-files,omitempty"`
CopyFiles []string `yaml:"copy-files,omitempty"`
ExtractFiles []struct {
Archive string `yaml:"archive,omitempty"`
Destination string `yaml:"destination,omitempty"`
} `yaml:"extract-files,omitempty"`
Mkdirs []string `yaml:"mkdirs,omitempty"`
Version string `yaml:"version"`
Description string `yaml:"description"`
Expand Down Expand Up @@ -123,6 +127,10 @@ func (p Packer) Prepare() error {
return err
}

if err := p.extractOtherFiles(); err != nil {
return err
}

if p.config.NeedsService {
if err := p.writeSystemdServiceFile(); err != nil {
return err
Expand Down Expand Up @@ -226,6 +234,25 @@ func (p Packer) copyOtherFiles() error {
return p.writer.CopyFiles(p.outputDirectory, path, os.FileMode(0644), p.config.CopyFiles...)
}

func (p Packer) extractOtherFiles() error {
if len(p.config.ExtractFiles) == 0 {
return nil
}

path := filepath.Join("var", "lib", p.config.PackageName)
if err := p.writer.CreateDirectory(filepath.Join(p.outputDirectory, path), os.FileMode(0755)); err != nil {
return err
}

for _, f := range p.config.ExtractFiles {
path = filepath.Join("var", "lib", p.config.PackageName, f.Destination)
if err := p.writer.ExtractArchive(p.outputDirectory, path, f.Archive); err != nil {
return err
}
}
return nil
}

func (p Packer) writeSystemdServiceFile() error {
path := filepath.Join(p.outputDirectory, "lib", "systemd", "system")
if err := p.writer.CreateDirectory(path, os.FileMode(0755)); err != nil {
Expand Down
27 changes: 25 additions & 2 deletions tools/debpacker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"io"
"io/ioutil"
"os"
"strings"

"github.com/urfave/cli"
yaml "gopkg.in/yaml.v2"

"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/interpolate"
)

func main() {
Expand Down Expand Up @@ -45,11 +47,24 @@ func main() {
return err
}

rawVars := c.StringSlice("var")
mapVars := make(map[string]string, len(rawVars))
for _, v := range rawVars {
tuple := strings.SplitN(v, "=", 2)
mapVars[tuple[0]] = tuple[1]
}

s, err := interpolate.Do(string(b), mapVars)
if err != nil {
return err
}

var config Config
if err := yaml.Unmarshal(b, &config); err != nil {
if err := yaml.Unmarshal([]byte(s), &config); err != nil {
return err
}

workdir := c.String("workdir")
target := c.String("target")
if ok, _ := isDirEmpty(target); !ok {
if !c.Bool("force") {
Expand All @@ -64,7 +79,7 @@ func main() {
}
}

p := New(&fileSystemWriter{}, config, target)
p := New(&fileSystemWriter{workdir: workdir}, config, target)

if err := p.Prepare(); err != nil {
return err
Expand All @@ -87,6 +102,14 @@ func main() {
Name: "force",
Usage: "Force override of existing target folder",
},
cli.StringSliceFlag{
Name: "var",
Usage: "variable=value",
},
cli.StringFlag{
Name: "workdir",
Usage: "debpacker working directory",
},
},
},
}
Expand Down
20 changes: 18 additions & 2 deletions tools/debpacker/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import (
"strings"

zglob "github.com/mattn/go-zglob"
"github.com/mholt/archiver"
"github.com/pkg/errors"
)

type Writer interface {
CreateDirectory(path string, perm os.FileMode) error
CreateFile(path string, content []byte, perm os.FileMode) error
CopyFiles(targetPath string, path string, perm os.FileMode, sources ...string) error
ExtractArchive(targetPath, path, archive string) error
}

type fileSystemWriter struct{}
type fileSystemWriter struct {
workdir string
}

func (f fileSystemWriter) CreateDirectory(path string, perm os.FileMode) error {
return errors.Wrapf(os.MkdirAll(path, os.FileMode(0755)), "Cannot mkdir all at %s", path)
Expand All @@ -33,6 +37,18 @@ func (f fileSystemWriter) CreateFile(path string, content []byte, perm os.FileMo
return errors.Wrapf(err, "Cannot write file at %s", path)
}

func (f fileSystemWriter) ExtractArchive(targetPath, path, archive string) error {
path = filepath.Join(targetPath, path)
if err := f.CreateDirectory(path, os.FileMode(0755)); err != nil {
return err
}

if err := archiver.Unarchive(filepath.Join(f.workdir, archive), path); err != nil {
return err
}
return nil
}

func (f fileSystemWriter) CopyFiles(targetPath string, path string, perm os.FileMode, sources ...string) error {
for _, source := range sources {
dest := targetPath
Expand All @@ -52,7 +68,7 @@ func (f fileSystemWriter) CopyFiles(targetPath string, path string, perm os.File
dest = filepath.Join(targetPath, path)
}

matches, err := zglob.Glob(source)
matches, err := zglob.Glob(filepath.Join(f.workdir, source))
if err != nil && err.Error() != "file does not exist" {
return errors.Wrapf(err, "Error glob for path %s", source)
}
Expand Down
12 changes: 12 additions & 0 deletions tools/debpacker/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type mockWriter struct {
directories []directory
files []file
copies []copy
extracts []extract
}

type directory struct {
Expand All @@ -26,6 +27,12 @@ type copy struct {
sources []string
}

type extract struct {
targetPath string
path string
archive string
}

func (m *mockWriter) CreateDirectory(path string, perm os.FileMode) error {
m.directories = append(m.directories, directory{path, perm})
return nil
Expand All @@ -40,3 +47,8 @@ func (m *mockWriter) CopyFiles(targetPath string, path string, perm os.FileMode,
m.copies = append(m.copies, copy{targetPath: targetPath, path: path, perm: perm, sources: sources})
return nil
}

func (m *mockWriter) ExtractArchive(targetPath, path, archive string) error {
m.extracts = append(m.extracts, extract{targetPath: targetPath, path: path, archive: archive})
return nil
}

0 comments on commit 91019a8

Please sign in to comment.