/
main.go
285 lines (254 loc) · 6.39 KB
/
main.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"embed"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"os/user"
"path"
"path/filepath"
"runtime/debug"
"syscall"
"github.com/anuvu/stacker/container"
stackerlog "github.com/anuvu/stacker/log"
"github.com/anuvu/stacker/types"
"github.com/apex/log"
"github.com/pkg/errors"
"github.com/urfave/cli"
"golang.org/x/term"
"gopkg.in/yaml.v2"
)
var (
config types.StackerConfig
version = ""
lxc_version = ""
)
//go:embed lxc-wrapper/lxc-wrapper
var embeddedFS embed.FS
func shouldShowProgress(ctx *cli.Context) bool {
/* if the user provided explicit recommendations, follow those */
if ctx.GlobalBool("no-progress") {
return false
}
if ctx.GlobalBool("progress") {
return true
}
/* otherise, show it when we're attached to a terminal */
return term.IsTerminal(int(os.Stdout.Fd()))
}
func stackerResult(err error) {
if err != nil {
format := "error: %v\n"
if config.Debug {
format = "error: %+v\n"
}
fmt.Fprintf(os.Stderr, format, err)
// propagate the wrapped execution's error code if we're in the
// userns wrapper
exitErr, ok := errors.Cause(err).(*exec.ExitError)
if ok {
os.Exit(exitErr.ExitCode())
}
os.Exit(1)
} else {
os.Exit(0)
}
}
func main() {
sigquits := make(chan os.Signal, 1)
go func() {
for range sigquits {
debug.PrintStack()
}
}()
signal.Notify(sigquits, syscall.SIGQUIT)
app := cli.NewApp()
app.Name = "stacker"
app.Usage = "stacker builds OCI images"
app.Version = fmt.Sprintf("stacker %s liblxc %s", version, lxc_version)
configDir := os.Getenv("XDG_CONFIG_HOME")
if configDir == "" {
homeDir := "/home/user"
user, err := user.Current()
if err == nil {
homeDir = user.HomeDir
}
configDir = path.Join(homeDir, ".config", app.Name)
}
app.Commands = []cli.Command{
buildCmd,
recursiveBuildCmd,
publishCmd,
chrootCmd,
cleanCmd,
inspectCmd,
grabCmd,
internalGoCmd,
unprivSetupCmd,
gcCmd,
checkCmd,
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "stacker-dir",
Usage: "set the directory for stacker's cache",
Value: ".stacker",
},
cli.StringFlag{
Name: "oci-dir",
Usage: "set the directory for OCI output",
Value: "oci",
},
cli.StringFlag{
Name: "roots-dir",
Usage: "set the directory for the rootfs output",
Value: "roots",
},
cli.StringFlag{
Name: "config",
Usage: "stacker config file with defaults",
Value: path.Join(configDir, "conf.yaml"),
},
cli.BoolFlag{
Name: "debug",
Usage: "enable stacker debug mode",
},
cli.BoolFlag{
Name: "q, quiet",
Usage: "silence all logs",
},
cli.StringFlag{
Name: "log-file",
Usage: "log to a file instead of stderr",
},
cli.BoolFlag{
Name: "log-timestamp",
Usage: "whether to log a timestamp prefix",
},
cli.StringFlag{
Name: "storage-type",
Usage: "storage type (one of \"overlay\" or \"btrfs\", defaults to overlay)",
Value: "overlay",
},
cli.BoolFlag{
Name: "internal-userns",
Usage: "used to reexec stacker in a user namespace",
Hidden: true,
},
}
/*
* Here's a barrel of suck: urfave/cli v1 doesn't allow for default
* values of boolean flags. So what we want is to append either
* --progress if this is not a tty, or --no-progress if it is a tty, so
* that we can allow for the right disabling of the thing in the right
* case.
*
* We don't want to convert to v2, because among other things it
* restricts *even more* the order of arguments and flags.
*
* see shouldShowProgress() for how we resolve whether or not to
* actually show it.
*/
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
if isTerminal {
app.Flags = append(app.Flags, cli.BoolFlag{
Name: "no-progress",
Usage: "disable progress when downloading container images or files",
})
} else {
app.Flags = append(app.Flags, cli.BoolFlag{
Name: "progress",
Usage: "enable progress when downloading container images or files",
})
}
var logFile *os.File
// close the log file if we happen to open it
defer func() {
if logFile != nil {
logFile.Close()
}
}()
app.Before = func(ctx *cli.Context) error {
logLevel := log.InfoLevel
if ctx.Bool("debug") {
config.Debug = true
logLevel = log.DebugLevel
if ctx.Bool("quiet") {
return errors.Errorf("debug and quiet don't make sense together")
}
} else if ctx.Bool("quiet") {
logLevel = log.FatalLevel
}
var err error
content, err := ioutil.ReadFile(ctx.String("config"))
if err == nil {
err = yaml.Unmarshal(content, &config)
if err != nil {
return err
}
}
config.EmbeddedFS = embeddedFS
if config.StackerDir == "" || ctx.IsSet("stacker-dir") {
config.StackerDir = ctx.String("stacker-dir")
}
if config.OCIDir == "" || ctx.IsSet("oci-dir") {
config.OCIDir = ctx.String("oci-dir")
}
if config.RootFSDir == "" || ctx.IsSet("roots-dir") {
config.RootFSDir = ctx.String("roots-dir")
}
config.StackerDir, err = filepath.Abs(config.StackerDir)
if err != nil {
return err
}
config.OCIDir, err = filepath.Abs(config.OCIDir)
if err != nil {
return err
}
config.RootFSDir, err = filepath.Abs(config.RootFSDir)
if err != nil {
return err
}
config.StorageType = ctx.String("storage-type")
fi, err := os.Stat(config.CacheFile())
if err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
stat, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return errors.Errorf("unknown sys stat type %T", fi.Sys())
}
if uint64(os.Geteuid()) != uint64(stat.Uid) {
return errors.Errorf("previous run of stacker found with uid %d", stat.Uid)
}
}
var handler log.Handler
handler = stackerlog.NewTextHandler(os.Stderr, ctx.Bool("log-timestamp"))
if ctx.String("log-file") != "" {
logFile, err = os.Create(ctx.String("log-file"))
if err != nil {
return errors.Wrapf(err, "failed to access %v", logFile)
}
handler = stackerlog.NewTextHandler(logFile, ctx.Bool("log-timestamp"))
}
stackerlog.FilterNonStackerLogs(handler, logLevel)
stackerlog.Debugf("stacker version %s", version)
if !ctx.Bool("internal-userns") && len(ctx.Args()) >= 1 && ctx.Args()[0] != "unpriv-setup" {
binary, err := os.Readlink("/proc/self/exe")
if err != nil {
return err
}
cmd := os.Args
cmd[0] = binary
cmd = append(cmd[:2], cmd[1:]...)
cmd[1] = "--internal-userns"
stackerResult(container.MaybeRunInUserns(config, cmd))
}
return nil
}
stackerResult(app.Run(os.Args))
}