diff --git a/bootstrap.config.go b/bootstrap.config.go new file mode 100644 index 0000000..585e327 --- /dev/null +++ b/bootstrap.config.go @@ -0,0 +1,7 @@ +// Copyright 2018 Mikhail Klementev. All rights reserved. +// Use of this source code is governed by a AGPLv3 license +// (or later) that can be found in the LICENSE file. + +package main + +const imagesURL = "https://github.com/jollheef/out-of-tree/releases/download/v0.2/images.tar.gz" diff --git a/bootstrap.go b/bootstrap.go new file mode 100644 index 0000000..92443a9 --- /dev/null +++ b/bootstrap.go @@ -0,0 +1,76 @@ +// Copyright 2018 Mikhail Klementev. All rights reserved. +// Use of this source code is governed by a AGPLv3 license +// (or later) that can be found in the LICENSE file. + +package main + +import ( + "io" + "io/ioutil" + "log" + "net/http" + "os" + "os/exec" + "os/user" +) + +// inspired by Edd Turtle code +func downloadFile(filepath string, url string) (err error) { + out, err := os.Create(filepath) + if err != nil { + return + } + defer out.Close() + + resp, err := http.Get(url) + if err != nil { + return + } + defer resp.Body.Close() + + _, err = io.Copy(out, resp.Body) + return +} + +func unpackTar(archive, destination string) (err error) { + cmd := exec.Command("tar", "xf", archive) + cmd.Dir = destination + "/" + + rawOutput, err := cmd.CombinedOutput() + if err != nil { + // I don't like when some errors printed inside + // So if you know way to do it better - FIXME please + log.Println("Unpack images error:", string(rawOutput), err) + return + } + + return +} + +func bootstrapHandler() (err error) { + usr, err := user.Current() + if err != nil { + return + } + + imagesPath := usr.HomeDir + "/.out-of-tree/images/" + os.MkdirAll(imagesPath, os.ModePerm) + + tmp, err := ioutil.TempDir("/tmp/", "out-of-tree_") + if err != nil { + log.Println("Temporary directory creation error:", err) + return + } + defer os.RemoveAll(tmp) + + imagesArchive := tmp + "/images.tar.gz" + + err = downloadFile(imagesArchive, imagesURL) + if err != nil { + log.Println("Download file error:", err) + return + } + + err = unpackTar(imagesArchive, imagesPath) + return +} diff --git a/main.go b/main.go index 468e052..bf148db 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,9 @@ func main() { debugFlagGDB := debugCommand.Flag("gdb", "Set gdb listen address") debugGDB := debugFlagGDB.Default("tcp::1234").String() + bootstrapCommand := app.Command("bootstrap", + "Create directories && download images") + // Check for required commands for _, cmd := range []string{"timeout", "docker", "qemu"} { _, err := exec.Command("which", cmd).CombinedOutput() @@ -123,6 +126,8 @@ func main() { case debugCommand.FullCommand(): err = debugHandler(kcfg, *path, *debugKernel, *debugGDB, *dockerTimeout) + case bootstrapCommand.FullCommand(): + err = bootstrapHandler() } if err != nil {