-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_context.go
More file actions
168 lines (129 loc) · 3.5 KB
/
build_context.go
File metadata and controls
168 lines (129 loc) · 3.5 KB
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
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"strconv"
"strings"
"github.com/coreos/go-systemd/unit"
"github.com/LEW21/siren/imagectl"
)
type BuildContext struct {
Task *Task
Image imagectl.Image
Directory string
}
func (b BuildContext) RealPath(path string) string {
if path[0] == '/' {
panic("BuildContext.RealPath: The argument has to be a relative path.")
}
return b.Directory + "/" + path
}
func (b *BuildContext) Run(name string, arg ...string) error {
return b.Task.RunCmd(b.Image.Command(name, arg...))
}
func (b *BuildContext) Copy(arg ...string) error {
dst := arg[len(arg)-1]
src := arg[:len(arg)-1]
// TODO? add support for wildcards
args := []string{"-R"}
for _, srcdir := range src {
args = append(args, b.RealPath(srcdir))
}
args = append(args, b.Image.RealPath(dst))
return b.Task.RunCommand("cp", args...)
}
func (b *BuildContext) download(uri *url.URL) (res *url.URL, err error) {
res = &url.URL{}
res.Fragment = uri.Fragment
uri.Fragment = ""
res.Path = "/var/lib/siren/" + unit.UnitNameEscape(uri.String())
{
_, err := os.Stat(res.Path)
if err == nil {
return res, nil
}
}
err = b.Task.RunCommand("wget", uri.String(), "-O", res.Path, "--progress=dot:mega")
return res, err
}
func (b *BuildContext) Untar(arg ...string) error {
dst := arg[len(arg)-1]
src := arg[:len(arg)-1]
for _, tarfile := range src {
u, err := url.Parse(tarfile)
if err != nil {
return err
}
if u.Scheme == "http" || u.Scheme == "https" {
var err error
u, err = b.download(u)
if err != nil {
return err
}
} else {
u.Path = b.RealPath(u.Path)
}
args := []string{"-xf", u.Path, "-C", b.Image.RealPath(dst)}
if u.Fragment != "" {
strip_components := 1 + strings.Count(u.Fragment, "/")
args = append(args, u.Fragment, "--strip-components", strconv.Itoa(strip_components))
}
if err := b.Task.RunCommand("tar", args...); err != nil {
return err
}
}
return nil
}
func (b *BuildContext) Set(name, value string) error {
return ioutil.WriteFile(b.Image.RealPath(name), []byte(value), 0644)
}
func unitName(name string) string {
if strings.HasSuffix(name, ".service") || strings.HasSuffix(name, ".socket") {
return name
}
return name + ".service"
}
func (b *BuildContext) addUnit(name string) error {
return b.Copy(name, "/usr/lib/systemd/system/")
}
func (b *BuildContext) AddUnit(name string) error {
return b.addUnit(unitName(name))
}
func (b *BuildContext) Enable(name string) error {
name = unitName(name)
if err := b.addUnit(name); err != nil {
fmt.Fprintln(b.Task, "Warning: " + name + " file not found.")
}
return b.Run("systemctl", "enable", name)
}
var ErrNotEnoughArguments = errors.New("not enough arguments")
// Don't error out when we get too many arguments - we can use them to extend the commands in the future.
func (b *BuildContext) Exec(cmd []string) error {
command := cmd[0]
arg := cmd[1:]
switch (command) {
case "RUN":
return b.Run(arg[0], arg[1:]...)
case "COPY":
return b.Copy(arg...)
case "UNTAR":
return b.Untar(arg...)
case "SET":
return b.Set(arg[0], arg[1])
case "ADD_UNIT":
return b.AddUnit(arg[0])
case "ENABLE":
return b.Enable(arg[0])
default:
return errors.New("Unknown command: " + command)
}
}
func (b *BuildContext) SubtaskExec(cmd []string) {
subtask := NewTask(b.Task, cmd[0] + " (" + strings.Join(cmd[1:], ") (") + ")"); defer subtask.Finish()
maintask := b.Task
b.Task = subtask; defer func(){b.Task = maintask}()
b.Task.Require(b.Exec(cmd))
}