summaryrefslogtreecommitdiff
path: root/actions/unpack_action.go
blob: d4993b1452097922f73f9a490cfa711c1da30531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Unpack Action

Unpack files from archive to the filesystem.
Useful for creating target rootfs from saved tarball with prepared file structure.

Only (compressed) tar archives are supported currently.

Yaml syntax:
 - action: unpack
   origin: name
   file: file.ext
   compression: gz

Mandatory properties:

- file -- archive's file name. It is possible to skip this property if 'origin'
referenced to downloaded file.

One of the mandatory properties may be omitted with limitations mentioned above.
It is expected to find archive with name pointed in `file` property inside of `origin` in case if both properties are used.

Optional properties:

- origin -- reference to a named file or directory.
The default value is 'artifacts' directory in case if this property is omitted.

- compression -- optional hint for unpack allowing to use proper compression method.

Currently only 'gz', bzip2' and 'xz' compression types are supported.
If not provided an attempt to autodetect the compression type will be done.
*/
package actions

import (
	"fmt"
	"github.com/go-debos/debos"
)

type UnpackAction struct {
	debos.BaseAction `yaml:",inline"`
	Compression      string
	Origin           string
	File             string
}

func (pf *UnpackAction) Verify(context *debos.DebosContext) error {

	if len(pf.Origin) == 0 && len(pf.File) == 0 {
		return fmt.Errorf("Filename can't be empty. Please add 'file' and/or 'origin' property.")
	}

	archive, err := debos.NewArchive(pf.File)
	if err != nil {
		return err
	}
	if len(pf.Compression) > 0 {
		if archive.Type() != debos.Tar {
			return fmt.Errorf("Option 'compression' is supported for Tar archives only.")
		}
		if err := archive.AddOption("tarcompression", pf.Compression); err != nil {
			return fmt.Errorf("'%s': %s", pf.File, err)
		}
	}

	return nil
}

func (pf *UnpackAction) Run(context *debos.DebosContext) error {
	pf.LogStart()
	var origin string

	if len(pf.Origin) > 0 {
		var found bool
		//Trying to get a filename from origins first
		origin, found = context.Origins[pf.Origin]
		if !found {
			return fmt.Errorf("Origin not found '%s'", pf.Origin)
		}
	} else {
		origin = context.Artifactdir
	}

	infile, err := debos.RestrictedPath(origin, pf.File)
	if err != nil {
		return err
	}

	archive, err := debos.NewArchive(infile)
	if err != nil {
		return err
	}
	if len(pf.Compression) > 0 {
		if err := archive.AddOption("tarcompression", pf.Compression); err != nil {
			return err
		}
	}

	return archive.Unpack(context.Rootdir)
}