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
5 changed files
with
173 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/osbuild/osbuild | ||
|
||
go 1.18 |
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,124 @@ | ||
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.`, | ||
} | ||
|
||
var apiArgumentsPath = "/run/osbuild/api/arguments" | ||
|
||
func parseInputs(inputs map[string]interface{}) (string, error) { | ||
image := inputs["file"].(map[string]interface{}) | ||
files := image["data"].(map[string]interface{})["files"].(map[string]interface{}) | ||
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(image["path"].(string), file) | ||
return path, nil | ||
} | ||
|
||
func apiArguments() (map[string]interface{}, error) { | ||
f, err := os.Open(apiArgumentsPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
var data = make(map[string]interface{}) | ||
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 | ||
} | ||
// XXX: use proper structs | ||
inputs := args["inputs"].(map[string]interface{}) | ||
output := args["tree"].(string) | ||
options := args["options"].(map[string]interface{}) | ||
|
||
filename := options["filename"].(string) | ||
source, err := parseInputs(inputs) | ||
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