reestructurar

This commit is contained in:
Cat /dev/Nulo 2023-01-22 10:06:23 -03:00
parent b4310c09a8
commit 7f3bc81bd2

91
main.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"embed" "embed"
"fmt"
"io" "io"
"log" "log"
"os" "os"
@ -16,50 +17,79 @@ var alpineKeys embed.FS
func main() { func main() {
config, err := readConfig() config, err := readConfig()
must(err) must(err)
must(run(config, "rootfs/", "cache/"))
must(os.RemoveAll("rootfs/"))
must(os.MkdirAll("rootfs/etc/apk/keys", 0700))
must(os.WriteFile("rootfs/etc/apk/repositories", []byte(
`https://dl-cdn.alpinelinux.org/alpine/v3.17/main
https://dl-cdn.alpinelinux.org/alpine/v3.17/community`), 0600))
must(os.WriteFile("rootfs/etc/resolv.conf", []byte(
`nameserver 8.8.8.8`), 0644))
keys, err := alpineKeys.ReadDir("alpine/keys")
must(err)
for _, entry := range keys {
o, err := os.Create(path.Join("rootfs/etc/apk/keys/", entry.Name()))
must(err)
s, err := alpineKeys.Open(path.Join("alpine/keys", entry.Name()))
must(err)
_, err = io.Copy(o, s)
must(err)
} }
must(os.MkdirAll("cache/_var_cache_apk", 0700)) func run(config Config, rootfs string, cache string) (err error) {
if err = os.RemoveAll("rootfs/"); err != nil {
return
}
if err = os.MkdirAll("rootfs/etc/apk/keys", 0700); err != nil {
return
}
if err = os.WriteFile("rootfs/etc/apk/repositories", []byte(
`https://dl-cdn.alpinelinux.org/alpine/v3.17/main
https://dl-cdn.alpinelinux.org/alpine/v3.17/community`), 0600); err != nil {
return
}
if err = os.WriteFile("rootfs/etc/resolv.conf", []byte(`nameserver 8.8.8.8`), 0644); err != nil {
return
}
keys, err := alpineKeys.ReadDir("alpine/keys")
if err != nil {
return
}
for _, entry := range keys {
o, err := os.Create(path.Join("rootfs/etc/apk/keys/", entry.Name()))
if err != nil {
return err
}
s, err := alpineKeys.Open(path.Join("alpine/keys", entry.Name()))
if err != nil {
return err
}
if _, err = io.Copy(o, s); err != nil {
return err
}
}
if err = os.MkdirAll("cache/_var_cache_apk", 0700); err != nil {
return
}
os.Remove("rootfs/etc/apk/cache") os.Remove("rootfs/etc/apk/cache")
must(os.Symlink("/var/cache/apk", "rootfs/etc/apk/cache")) if err = os.Symlink("/var/cache/apk", "rootfs/etc/apk/cache"); err != nil {
return
}
// must(os.MkdirAll("rootfs/var/cache", 0700)) // must(os.MkdirAll("rootfs/var/cache", 0700))
// must(os.RemoveAll("rootfs/var/cache/apk")) // must(os.RemoveAll("rootfs/var/cache/apk"))
// must(os.Symlink("../../../cache/apk", "rootfs/var/cache/apk")) // must(os.Symlink("../../../cache/apk", "rootfs/var/cache/apk"))
// must(os.Remove("rootfs/var/cache/apk"))
cmd := exec.Command("apk", "add", cmd := exec.Command("apk", "add",
"--root", "rootfs", "--root", "rootfs",
"--initdb", "--initdb",
"--cache-dir", "../cache/_var_cache_apk", "--cache-dir", "../cache/_var_cache_apk",
"apk-tools") "apk-tools")
must(cmd.Run(), "apk add apk-tools") if err = cmd.Run(); err != nil {
// must(os.Remove("rootfs/var/cache/apk")) return fmt.Errorf("apk add apk-tools: %w", err)
}
cached := append(config.Cache, "/var/cache/apk") cached := append(config.Cache, "/var/cache/apk")
var cachedParams []string var cachedParams []string
for _, c := range cached { for _, c := range cached {
cacheDir := path.Join("./cache", strings.ReplaceAll(c, "/", "_")) cacheDir := path.Join("./cache", strings.ReplaceAll(c, "/", "_"))
must(os.MkdirAll(cacheDir, 0700)) if err = os.MkdirAll(cacheDir, 0700); err != nil {
return
}
cachedParams = append(cachedParams, "--bind", cacheDir, c) cachedParams = append(cachedParams, "--bind", cacheDir, c)
} }
tmp, err := os.MkdirTemp("", "repro-run-") tmp, err := os.MkdirTemp("", "repro-run-")
must(err) if err != nil {
return
}
defer os.RemoveAll(tmp) defer os.RemoveAll(tmp)
log.Println("/work = " + tmp) log.Println("/work = " + tmp)
@ -78,20 +108,25 @@ https://dl-cdn.alpinelinux.org/alpine/v3.17/community`), 0600))
cmd = exec.Command("bwrap", append(params, cmd = exec.Command("bwrap", append(params,
"apk", "add", "busybox", "busybox-suid", "libc-utils", "alpine-baselayout", "alpine-conf", "alpine-release")...) "apk", "add", "busybox", "busybox-suid", "libc-utils", "alpine-baselayout", "alpine-conf", "alpine-release")...)
// cmd.Stdout = os.Stdout if err = cmd.Run(); err != nil {
// cmd.Stderr = os.Stderr return fmt.Errorf("bwrap apk add: %w", err)
must(cmd.Run(), "bwrap apk add") }
cmd = exec.Command("bwrap", append(params, cmd = exec.Command("bwrap", append(params,
"--uid", "0", "--uid", "0",
"adduser", "-u", "1000", "-D", "repro")...) "adduser", "-u", "1000", "-D", "repro")...)
must(cmd.Run(), "bwrap adduser repro") if err = cmd.Run(); err != nil {
return fmt.Errorf("bwrap adduser repro: %w", err)
}
cmd = exec.Command("bwrap", append(params, strings.Split(config.Command, " ")...)...) cmd = exec.Command("bwrap", append(params, strings.Split(config.Command, " ")...)...)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
must(cmd.Run()) if err = cmd.Run(); err != nil {
return fmt.Errorf("running Command: %w", err)
}
return
} }
func must(err error, where ...string) { func must(err error, where ...string) {