summaryrefslogtreecommitdiff
path: root/actions/download_action.go
blob: a4099e7cb70ac627f1f2156609d52d1f32820e8a (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Download Action

Download a single file from Internet and unpack it in place if needed.

Yaml syntax:
 - action: download
   url: http://example.domain/path/filename.ext
   name: firmware
   filename: output_name
   unpack: bool
   compression: gz

Mandatory properties:

- url -- URL to an object for download

- name -- string which allow to use downloaded object in other actions
via 'origin' property. If 'unpack' property is set to 'true' name will
refer to temporary directory with extracted content.

Optional properties:

- filename -- use this property as the name for saved file. Useful if URL does not
contain file name in path, for example it is possible to download files from URLs without path part.

- unpack -- hint for action to extract all files from downloaded archive.
See the 'Unpack' action for more information.

- compression -- optional hint for unpack allowing to use proper compression method.
See the 'Unpack' action for more information.
*/
package actions

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

type DownloadAction struct {
	debos.BaseAction `yaml:",inline"`
	Url              string // URL for downloading
	Filename         string // File name, overrides the name from URL.
	Unpack           bool   // Unpack downloaded file to directory dedicated for download
	Compression      string // compression type
	Name             string // exporting path to file or directory(in case of unpack)
}

// validateUrl checks if supported URL is passed from recipe
// Return:
// - parsed URL
// - nil in case of success
func (d *DownloadAction) validateUrl() (*url.URL, error) {

	url, err := url.Parse(d.Url)
	if err != nil {
		return url, err
	}

	switch url.Scheme {
	case "http", "https":
		// Supported scheme
	default:
		return url, fmt.Errorf("Unsupported URL is provided: '%s'", url.String())
	}

	return url, nil
}

func (d *DownloadAction) validateFilename(context *debos.DebosContext, url *url.URL) (filename string, err error) {
	if len(d.Filename) == 0 {
		// Trying to guess the name from URL Path
		filename = path.Base(url.Path)
	} else {
		filename = path.Base(d.Filename)
	}
	if len(filename) == 0 {
		return "", fmt.Errorf("Incorrect filename is provided for '%s'", d.Url)
	}
	filename = path.Join(context.Scratchdir, filename)
	return filename, nil
}

func (d *DownloadAction) archive(filename string) (debos.Archive, error) {
	archive, err := debos.NewArchive(filename)
	if err != nil {
		return archive, err
	}
	switch archive.Type() {
	case debos.Tar:
		if len(d.Compression) > 0 {
			if err := archive.AddOption("tarcompression", d.Compression); err != nil {
				return archive, err
			}
		}
	default:
	}
	return archive, nil
}

func (d *DownloadAction) Verify(context *debos.DebosContext) error {
	var filename string

	if len(d.Name) == 0 {
		return fmt.Errorf("Property 'name' is mandatory for download action\n")
	}

	url, err := d.validateUrl()
	if err != nil {
		return err
	}
	filename, err = d.validateFilename(context, url)
	if err != nil {
		return err
	}
	if d.Unpack == true {
		if _, err := d.archive(filename); err != nil {
			return err
		}
	}
	return nil
}

func (d *DownloadAction) Run(context *debos.DebosContext) error {
	var filename string
	d.LogStart()

	url, err := d.validateUrl()
	if err != nil {
		return err
	}

	filename, err = d.validateFilename(context, url)
	if err != nil {
		return err
	}
	originPath := filename

	switch url.Scheme {
	case "http", "https":
		err := debos.DownloadHttpUrl(url.String(), filename)
		if err != nil {
			return err
		}
	default:
		return fmt.Errorf("Unsupported URL is provided: '%s'", url.String())
	}

	if d.Unpack == true {
		archive, err := d.archive(filename)
		if err != nil {
			return err
		}

		targetdir := filename + ".d"
		err = archive.RelaxedUnpack(targetdir)
		if err != nil {
			return err
		}
		originPath = targetdir
	}

	context.Origins[d.Name] = originPath

	return nil
}