From 4db2e8abb336f99ba51c8742538a8d19e3b3f807 Mon Sep 17 00:00:00 2001 From: anianz Date: Tue, 10 Nov 2020 11:35:37 +0100 Subject: [PATCH] initial PoC for out binary --- go.mod | 3 ++ go.sum | 0 out/main.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 out/main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b4fd484 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/cioplenu/concourse-nomad-resource + +go 1.14 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/out/main.go b/out/main.go new file mode 100644 index 0000000..0e6d3ae --- /dev/null +++ b/out/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "strings" + "text/template" +) + +type SourceConfig struct { + URL string `json:"url"` + Name string `json:"name"` + Token string `json:"token"` +} + +type ParamsConfig struct { + JobPath string `json:"job_path"` + Vars map[string]string `json:"vars"` + VarFiles map[string]string `json:"var_files"` +} + +type OutConfig struct { + Source SourceConfig `json:"source"` + Params ParamsConfig `json:"params"` +} + +func check(err error, msg string) { + if err != nil { + fmt.Fprintf(os.Stderr, msg+": %s\n", err) + os.Exit(1) + } +} + +func main() { + if len(os.Args) < 2 { + fmt.Fprintf(os.Stderr, "usage: %s \n", os.Args[0]) + os.Exit(1) + } + sourceDir := os.Args[1] + + var config OutConfig + err := json.NewDecoder(os.Stdin).Decode(&config) + check(err, "Error parsing configuration") + + templPath := filepath.Join(sourceDir, config.Params.JobPath) + templFile, err := ioutil.ReadFile(templPath) + check(err, "Could not read input file "+templPath) + tmpl, err := template.New("job").Parse(string(templFile)) + check(err, "Error parsing template") + + for name, path := range config.Params.VarFiles { + varPath := filepath.Join(sourceDir, path) + varFile, err := ioutil.ReadFile(varPath) + check(err, "Error reading var file") + config.Params.Vars[name] = strings.TrimSpace(string(varFile)) + } + + buf := new(bytes.Buffer) + + err = tmpl.Execute(buf, config.Params.Vars) + check(err, "Error executing template") + + outFile, err := os.Create(templPath) + check(err, "Error creating output file") + defer outFile.Close() + _, err = outFile.Write(buf.Bytes()) + check(err, "Error writing output file") + + cmd := exec.Command( + "nomad", + "job", + "run", + "-address="+config.Source.URL, + "-token="+config.Source.Token, + templPath, + ) + var out bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = &out + err = cmd.Run() + if err != nil { + fmt.Fprintf(os.Stderr, "Error executing nomad: %s\n", err) + fmt.Fprint(os.Stderr, out.String()) + os.Exit(1) + } + + fmt.Println(out.String()) +}