Compare commits

...

5 commits

Author SHA1 Message Date
07d276b2a3 agregar repro-build y repro-run.json 2023-01-22 10:15:19 -03:00
17560621e0 separar runner de cli 2023-01-22 10:14:48 -03:00
7fe4b8c08e readme: arreglar identación 2023-01-22 10:07:59 -03:00
bfdd518233 borrar comentarios 2023-01-22 10:06:36 -03:00
7f3bc81bd2 reestructurar 2023-01-22 10:06:23 -03:00
12 changed files with 99 additions and 60 deletions

26
cli.go Normal file
View file

@ -0,0 +1,26 @@
package main
import (
"encoding/json"
"log"
"os"
"gitea.nulo.in/Nulo/repro-run/runner"
)
func main() {
config, err := readConfig()
must(err)
must(runner.Run(config, "rootfs/", "cache/"))
}
func must(err error, where ...string) {
if err != nil {
log.Fatal(err, where)
}
}
func readConfig() (config runner.Config, err error) {
err = json.NewDecoder(os.Stdin).Decode(&config)
return
}

View file

@ -1,16 +0,0 @@
package main
import (
"encoding/json"
"os"
)
type Config struct {
Command string
Cache []string
}
func readConfig() (config Config, err error) {
err = json.NewDecoder(os.Stdin).Decode(&config)
return
}

View file

@ -17,8 +17,7 @@ file repro-run
```
```sh
echo '{"Command": "/src/test", "Cache": ["/home/repro/.cache/go-build"]}
' | go run .
echo '{"Command": "/src/test", "Cache": ["/home/repro/.cache/go-build"]}' | go run .
```
genera

5
repro-build Executable file
View file

@ -0,0 +1,5 @@
#!/bin/sh -xe
apk add --quiet git go
git clone --quiet file:///src repo
cd repo
go build .

1
repro-run.json Normal file
View file

@ -0,0 +1 @@
{ "Command": "/src/repro-build", "Cache": ["/home/repro/.cache/go-build"] }

6
runner/config.go Normal file
View file

@ -0,0 +1,6 @@
package runner
type Config struct {
Command string
Cache []string
}

View file

@ -1,7 +1,8 @@
package main
package runner
import (
"embed"
"fmt"
"io"
"log"
"os"
@ -13,53 +14,71 @@ import (
//go:embed alpine/keys
var alpineKeys embed.FS
func main() {
config, err := readConfig()
must(err)
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)
func Run(config Config, rootfs string, cache string) (err error) {
if err = os.RemoveAll("rootfs/"); err != nil {
return
}
must(os.MkdirAll("cache/_var_cache_apk", 0700))
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")
must(os.Symlink("/var/cache/apk", "rootfs/etc/apk/cache"))
// must(os.MkdirAll("rootfs/var/cache", 0700))
// must(os.RemoveAll("rootfs/var/cache/apk"))
// must(os.Symlink("../../../cache/apk", "rootfs/var/cache/apk"))
if err = os.Symlink("/var/cache/apk", "rootfs/etc/apk/cache"); err != nil {
return
}
cmd := exec.Command("apk", "add",
"--root", "rootfs",
"--initdb",
"--cache-dir", "../cache/_var_cache_apk",
"apk-tools")
must(cmd.Run(), "apk add apk-tools")
// must(os.Remove("rootfs/var/cache/apk"))
if err = cmd.Run(); err != nil {
return fmt.Errorf("apk add apk-tools: %w", err)
}
cached := append(config.Cache, "/var/cache/apk")
var cachedParams []string
for _, c := range cached {
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)
}
tmp, err := os.MkdirTemp("", "repro-run-")
must(err)
if err != nil {
return
}
defer os.RemoveAll(tmp)
log.Println("/work = " + tmp)
@ -78,24 +97,23 @@ https://dl-cdn.alpinelinux.org/alpine/v3.17/community`), 0600))
cmd = exec.Command("bwrap", append(params,
"apk", "add", "busybox", "busybox-suid", "libc-utils", "alpine-baselayout", "alpine-conf", "alpine-release")...)
// cmd.Stdout = os.Stdout
// cmd.Stderr = os.Stderr
must(cmd.Run(), "bwrap apk add")
if err = cmd.Run(); err != nil {
return fmt.Errorf("bwrap apk add: %w", err)
}
cmd = exec.Command("bwrap", append(params,
"--uid", "0",
"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.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
must(cmd.Run())
if err = cmd.Run(); err != nil {
return fmt.Errorf("running Command: %w", err)
}
func must(err error, where ...string) {
if err != nil {
log.Fatal(err, where)
}
return
}