From f419d5be2d823656215b7351ff80bcf78dcb28a6 Mon Sep 17 00:00:00 2001 From: Vignesh Goutham Ganesh Date: Mon, 9 May 2022 12:46:46 -0700 Subject: [PATCH] Add Bootconfig support to WriteFile action Signed-off-by: Vignesh Goutham Ganesh --- actions/writefile/v1/go.sum | 6 ------ actions/writefile/v1/main.go | 30 +++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/actions/writefile/v1/go.sum b/actions/writefile/v1/go.sum index 221a802..67a36b2 100644 --- a/actions/writefile/v1/go.sum +++ b/actions/writefile/v1/go.sum @@ -5,7 +5,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= @@ -13,21 +12,16 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/actions/writefile/v1/main.go b/actions/writefile/v1/main.go index e5ed3cf..b38ef1c 100644 --- a/actions/writefile/v1/main.go +++ b/actions/writefile/v1/main.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "os" + "os/exec" "path/filepath" "strconv" "strings" @@ -13,7 +14,10 @@ import ( log "github.com/sirupsen/logrus" ) -const mountAction = "/mountAction" +const ( + mountAction = "/mountAction" + bootConfigAction = "/usr/bin/bootconfig" +) func main() { fmt.Printf("WriteFile - Write file to disk\n------------------------\n") @@ -23,6 +27,7 @@ func main() { filePath := os.Getenv("DEST_PATH") contents := os.Getenv("CONTENTS") + bootconfig := os.Getenv("BOOTCONFIG_CONTENTS") uid := os.Getenv("UID") gid := os.Getenv("GID") mode := os.Getenv("MODE") @@ -42,6 +47,10 @@ func main() { log.Fatalf("Could not parse mode: %v", err) } + if bootconfig != "" && contents != "" { + log.Fatal("Both BOOTCONFIG_CONTENTS and CONTENTS cannot be set.") + } + fileMode := os.FileMode(modePrime) dirModePrime, err := strconv.ParseUint(dirMode, 8, 32) @@ -82,12 +91,31 @@ func main() { log.Fatalf("Failed to ensure directory exists: %v", err) } + // If bootconfig is set, contents will be empty and will serve as output initrd file provided + // to bootconfig tool fqFilePath := filepath.Join(mountAction, filePath) // Write the file to disk if err := ioutil.WriteFile(fqFilePath, []byte(contents), fileMode); err != nil { log.Fatalf("Could not write file %s: %v", filePath, err) } + if bootconfig != "" { + // Write the input bootconfig to file to serve as input to the tool + inputFilePath := "/userInputBootConfig" + err := ioutil.WriteFile(inputFilePath, []byte(bootconfig), fileMode) + if err != nil { + log.Fatalf("Could not write file %s: %v", filePath, err) + } + defer os.Remove(inputFilePath) + + // Parse through bootconfig if enabled + cmd := exec.Command(bootConfigAction, "-a", inputFilePath, fqFilePath) + output, err := cmd.Output() + if err != nil { + log.Fatalf("Error running Bootconfig tool. Err: %v, Output: %s", err, string(output)) + } + } + if err := os.Chown(fqFilePath, fileUID, fileGID); err != nil { log.Fatalf("Could not modify ownership of file %s: %v", filePath, err) } -- 2.24.3 (Apple Git-128)