forked from osbuild/osbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
osbuild: start stage separation support
This commit prepares for stage separation support. The old import mechanism is still supported but if it fails the new way of just running the binary and outputing the data via stdout is used.
- Loading branch information
Showing
6 changed files
with
278 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func MockApiArguments(t *testing.T, apiArgs []byte) (restore func()) { | ||
saved := apiArgumentsPath | ||
|
||
tmpdir := t.TempDir() | ||
apiArgumentsPath = filepath.Join(tmpdir, "apiArguments") | ||
if err := ioutil.WriteFile(apiArgumentsPath, apiArgs, 0644); err != nil { | ||
t.Fatalf("%v", err) | ||
} | ||
|
||
return func() { | ||
apiArgumentsPath = saved | ||
} | ||
} | ||
|
||
var ( | ||
ApiArguments = apiArguments | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module org.osbuild.stages/staticgzip | ||
|
||
go 1.19 | ||
|
||
require "org.osbuild.stages/staticgzip" v0.0.0 | ||
replace "org.osbuild.stages/staticgzip" v0.0.0 => "./" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package main | ||
|
||
import ( | ||
"compress/gzip" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
var info = map[string]string{ | ||
"schema_2": ` | ||
{ | ||
"inputs": { | ||
"type": "object", | ||
"additionalProperties": false, | ||
"required": ["file"], | ||
"properties": { | ||
"file": { | ||
"type": "object", | ||
"additionalProperties": true | ||
} | ||
} | ||
}, | ||
"options": { | ||
"additionalProperties": false, | ||
"required": ["filename"], | ||
"properties": { | ||
"filename": { | ||
"description": "Filename to use for the compressed file", | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}`, | ||
"doc": ` | ||
Compress a file using gzip | ||
No external dependencies.`, | ||
} | ||
|
||
type apiArgs struct { | ||
Tree string `json:"tree"` | ||
Inputs struct { | ||
File struct { | ||
Path string `json:"path"` | ||
Data struct { | ||
Files map[string]interface{} `json:"files"` | ||
} `json:"data"` | ||
} `json:"file"` | ||
} `json:"inputs"` | ||
Options struct { | ||
Filename string `json:"filename"` | ||
} `json:"options"` | ||
} | ||
|
||
var apiArgumentsPath = "/run/osbuild/api/arguments" | ||
|
||
func parseInputs(args *apiArgs) (string, error) { | ||
files := args.Inputs.File.Data.Files | ||
if len(files) != 1 { | ||
return "", fmt.Errorf("unexpected amount of destination files %q", files) | ||
} | ||
// XXX: fugly | ||
var file string | ||
for k, _ := range files { | ||
file = k | ||
} | ||
|
||
path := filepath.Join(args.Inputs.File.Path, file) | ||
return path, nil | ||
} | ||
|
||
func apiArguments() (*apiArgs, error) { | ||
f, err := os.Open(apiArgumentsPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
// alternatively we could unmarshal to map[string]interface{} and | ||
// poke around via type assertions but that is also not fun | ||
var data apiArgs | ||
dec := json.NewDecoder(f) | ||
if err := dec.Decode(&data); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &data, nil | ||
} | ||
|
||
func run() error { | ||
args, err := apiArguments() | ||
if err != nil { | ||
return err | ||
} | ||
output := args.Tree | ||
filename := args.Options.Filename | ||
source, err := parseInputs(args) | ||
if err != nil { | ||
return err | ||
} | ||
target := filepath.Join(output, filename) | ||
|
||
inf, err := os.Open(source) | ||
if err != nil { | ||
return err | ||
} | ||
defer inf.Close() | ||
outf, err := os.Create(target) | ||
if err != nil { | ||
return err | ||
} | ||
w := gzip.NewWriter(outf) | ||
if _, err := io.Copy(w, inf); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
if len(os.Args) == 2 && os.Args[1] == "--all" { | ||
enc := json.NewEncoder(os.Stdout) | ||
if err := enc.Encode(&info); err != nil { | ||
fmt.Fprintf(os.Stderr, "error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
os.Exit(0) | ||
} | ||
|
||
if err := run(); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
main "org.osbuild.stages/staticgzip" | ||
) | ||
|
||
var fakeInput = []byte(` | ||
{ | ||
"tree": "/run/osbuild/tree", | ||
"paths": { | ||
"devices": "/dev", | ||
"inputs": "/run/osbuild/inputs", | ||
"mounts": "/run/osbuild/mounts" | ||
}, | ||
"devices": {}, | ||
"inputs": { | ||
"file": { | ||
"path": "/run/osbuild/inputs/file", | ||
"data": { | ||
"files": { | ||
"sha256:f950375066d74787f31cbd8f9f91c71819357cad243fb9d4a0d9ef4fa76709e0": {} | ||
} | ||
} | ||
} | ||
}, | ||
"mounts": {}, | ||
"options": { | ||
"filename": "compressed.gz" | ||
}, | ||
"meta": { | ||
"id": "6dc907e7cf7b6436938d55eabad9209d4d4b7c4f338f3eef20f1d212aca48c79" | ||
} | ||
} | ||
`) | ||
|
||
func TestApiArguments(t *testing.T) { | ||
restore := main.MockApiArguments(t, fakeInput) | ||
defer restore() | ||
|
||
args, err := main.ApiArguments() | ||
if err != nil { | ||
t.Fatalf("%v", err) | ||
} | ||
if args.Inputs.File.Path != "/run/osbuild/inputs/file" { | ||
t.Fatalf("unexpected args %v", args) | ||
} | ||
expected := map[string]interface{}{ | ||
"sha256:f950375066d74787f31cbd8f9f91c71819357cad243fb9d4a0d9ef4fa76709e0": map[string]interface{}{}, | ||
} | ||
input := args.Inputs.File.Data.Files | ||
if !reflect.DeepEqual(input, expected) { | ||
t.Fatalf("unexpected args %v", args) | ||
} | ||
if args.Options.Filename != "compressed.gz" { | ||
t.Fatalf("unexpected args %v", args) | ||
} | ||
} |