Skip to content

Commit

Permalink
feat: adds command for scanning for deployment files
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman committed Jan 30, 2024
1 parent 65e4cf6 commit 81eed2f
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
repository: ${{ inputs.deployment_repo }}
token: ${{ secrets.token }}
- name: Install updater CLI
uses: input-output-hk/catalyst-ci/actions/install@allow-multiple-install
uses: input-output-hk/catalyst-ci/actions/install@master
with:
asset: updater
- name: Test
Expand Down
4 changes: 4 additions & 0 deletions services/jorm-metrics-server/deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
overrides:
- app: jormungandr
path: exporter.image.tag
value: GITHUB_SHA
79 changes: 65 additions & 14 deletions tools/updater/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package main
// cspell: words afero alecthomas cuelang cuecontext cuectx existingfile mheers Timoni nolint

import (
"encoding/json"
"fmt"
"os"
"strings"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
Expand All @@ -15,34 +18,82 @@ import (
)

var cli struct {
Scan scanCmd `cmd:"" help:"Scans a directory for deployment files."`
Update updateCmd `cmd:"" help:"Overrides a target path in a CUE file with the given value."`
}

type scanCmd struct {
Path string `arg:"" help:"The path to scan for deployment files." type:"existingdir" required:"true"`
Template []string `short:"t" help:"A key/value pair used to override constant values in deployment configurations."`
}

func (c *scanCmd) Run() error {
files, err := pkg.ScanForDeploymentFiles(c.Path, afero.NewOsFs())
if err != nil {
return err
}

overrides := []pkg.OverrideConfig{}
for _, file := range files {
overrides = append(overrides, file.Overrides...)
}

for _, template := range c.Template {
pair := strings.Split(template, "=")
pkg.ApplyTemplateValue(overrides, pair[0], pair[1])
}

output, err := json.Marshal(overrides)
if err != nil {
return fmt.Errorf("failed to marshal overrides: %v", err)
}

fmt.Print(string(output))

return nil
}

type updateCmd struct {
BundleFile string `type:"existingfile" short:"b" help:"Path to the Timoni bundle file to modify." required:"true"`
Path string `arg:"" help:"A dot separated path to the value to override (must already exist)."`
Value string `arg:"" help:"The value to override the value at the path with."`
}

func main() {
ctx := kong.Parse(&cli,
kong.Name("updater"),
kong.Description("A helper tool for modifying CUE files to override arbitrary values. Useful for updating Timoni bundles."))

func (c *updateCmd) Run() error {
cuectx := cuecontext.New()
v, err := pkg.ReadFile(cuectx, cli.BundleFile, afero.NewOsFs())
ctx.FatalIfErrorf(err)
v, err := pkg.ReadFile(cuectx, c.BundleFile, afero.NewOsFs())
if err != nil {
return err
}

if !v.LookupPath(cue.ParsePath(cli.Path)).Exists() {
ctx.Fatalf("path %q does not exist", cli.Path)
if !v.LookupPath(cue.ParsePath(c.Path)).Exists() {
return fmt.Errorf("path %q does not exist", c.Path)
}

v, err = ch.Replace(v, cli.Path, cli.Value)
ctx.FatalIfErrorf(err)
v, err = ch.Replace(v, c.Path, c.Value)
if err != nil {
return err
}

node := v.Syntax(cue.Final(), cue.Concrete(true), cue.Docs(true))
src, err := format.Node(node)
ctx.FatalIfErrorf(err)
if err != nil {
return err
}

if err := os.WriteFile(cli.BundleFile, src, 0644); err != nil { //nolint:gosec
ctx.Fatalf("failed to write file %q: %v", cli.BundleFile, err)
if err := os.WriteFile(c.BundleFile, src, 0644); err != nil { //nolint:gosec
return fmt.Errorf("failed to write file %q: %v", c.BundleFile, err)
}

return nil
}

func main() {
ctx := kong.Parse(&cli,
kong.Name("updater"),
kong.Description("A helper tool for modifying CUE files to override arbitrary values. Useful for updating Timoni bundles."))

err := ctx.Run()
ctx.FatalIfErrorf(err)
os.Exit(0)
}
6 changes: 4 additions & 2 deletions tools/updater/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/input-output-hk/catalyst-ci/tools/updater

go 1.20
go 1.21

toolchain go1.21.5

require (
cuelang.org/go v0.7.0
Expand All @@ -9,6 +11,7 @@ require (
github.com/onsi/ginkgo/v2 v2.9.2
github.com/onsi/gomega v1.27.5
github.com/spf13/afero v1.11.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -26,5 +29,4 @@ require (
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
19 changes: 19 additions & 0 deletions tools/updater/go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
cuelabs.dev/go/oci/ociregistry v0.0.0-20231103182354-93e78c079a13 h1:zkiIe8AxZ/kDjqQN+mDKc5BxoVJOqioSdqApjc+eB1I=
cuelabs.dev/go/oci/ociregistry v0.0.0-20231103182354-93e78c079a13/go.mod h1:XGKYSMtsJWfqQYPwq51ZygxAPqpEUj/9bdg16iDPTAA=
cuelang.org/go v0.7.0 h1:gMztinxuKfJwMIxtboFsNc6s8AxwJGgsJV+3CuLffHI=
cuelang.org/go v0.7.0/go.mod h1:ix+3dM/bSpdG9xg6qpCgnJnpeLtciZu+O/rDbywoMII=
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0=
github.com/alecthomas/assert/v2 v2.1.0/go.mod h1:b/+1DI2Q6NckYi+3mXyH3wFb8qG37K/DuK80n7WefXA=
github.com/alecthomas/kong v0.8.1 h1:acZdn3m4lLRobeh3Zi2S2EpnXTd1mOL6U7xVml+vfkY=
github.com/alecthomas/kong v0.8.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
github.com/alecthomas/repr v0.1.0/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand All @@ -14,50 +17,64 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/proto v1.10.0 h1:pDGyFRVV5RvV+nkBK9iy3q67FBy9Xa7vwrOTE+g5aGw=
github.com/emicklei/proto v1.10.0/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A=
github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U=
github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubevela/workflow v0.6.0 h1:fYXviOYD5zqHs3J61tNbM4HZ85EcZlPm7Fyz8Q5o9Fk=
github.com/kubevela/workflow v0.6.0/go.mod h1:sjLcYqKHKeCQ+w77gijoNILwIShJKnCU+e3q7ETtZGI=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mheers/cue-helper v0.0.0-20231214081257-a325036f9a0d h1:JZsFf1WWJjBq7swih1HeWbmnh5uKZ2Ol2HSZ8o6kcK8=
github.com/mheers/cue-helper v0.0.0-20231214081257-a325036f9a0d/go.mod h1:TvPPtiz/noXLe+NDP3XJLg8Y0ROt3m6rVlqUYlLuba0=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de h1:D5x39vF5KCwKQaw+OC9ZPiLVHXz3UFw2+psEX+gYcto=
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de/go.mod h1:kJun4WP5gFuHZgRjZUWWuH1DTxCtxbHDOIJsudS8jzY=
github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU=
github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts=
github.com/onsi/gomega v1.27.5 h1:T/X6I0RNFw/kTqgfkZPcQ5KU6vCnWNBGdtrIx2dpGeQ=
github.com/onsi/gomega v1.27.5/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc4 h1:oOxKUJWnFC4YGHCCMNql1x4YaDfYBTS5Y4x/Cgeo1E0=
github.com/opencontainers/image-spec v1.1.0-rc4/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0 h1:sadMIsgmHpEOGbUs6VtHBXRR1OHevnj7hLx9ZcdNGW4=
github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0/go.mod h1:jgxiZysxFPM+iWKwQwPR+y+Jvo54ARd4EisXxKYpB5c=
github.com/rogpeppe/go-internal v1.11.1-0.20231026093722-fa6a31e0812c h1:fPpdjePK1atuOg28PXfNSqgwf9I/qD1Hlo39JFwKBXk=
github.com/rogpeppe/go-internal v1.11.1-0.20231026093722-fa6a31e0812c/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -68,8 +85,10 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
72 changes: 72 additions & 0 deletions tools/updater/pkg/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package pkg

import (
"fmt"
"os"

"github.com/spf13/afero"
"gopkg.in/yaml.v3"
)

// Filename is the static name of a deployment configuration file.
const Filename = "deployment.yml"

// DeploymentFile represents a deployment configuration file.
type DeploymentFile struct {
Overrides []OverrideConfig `json:"overrides" yaml:"overrides"`
}

// OverrideConfig represents configuration for overriding a value in a CUE file.
type OverrideConfig struct {
App string `json:"app" yaml:"app"`
Path string `json:"path" yaml:"path"`
Value string `json:"value" yaml:"value"`
}

// ScanForDeploymentFiles scans a directory for deployment configuration files.
func ScanForDeploymentFiles(dir string, fs afero.Fs) ([]DeploymentFile, error) {

files := []DeploymentFile{}
err := afero.Walk(fs, dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
return nil
}

if info.Name() == Filename {
contents, err := afero.ReadFile(fs, path)
if err != nil {
return fmt.Errorf("failed to read deployment file at %q: %v", path, err)
}

deploymentFile := DeploymentFile{}
if err := yaml.Unmarshal(contents, &deploymentFile); err != nil {
// If we can't unmarshal the file, we assume it's not a deployment file and just log a warning.
fmt.Fprintf(os.Stderr, "warning: failed to parse deployment file %q: %v", path, err)
return nil
}

files = append(files, deploymentFile)
}

return nil
})

if err != nil {
return nil, err
}

return files, nil
}

// ApplyTemplateValue applies a template value to a list of overrides.
func ApplyTemplateValue(overrides []OverrideConfig, key, value string) {
for i, override := range overrides {
if override.Value == key {
overrides[i].Value = value
}
}
}
Loading

0 comments on commit 81eed2f

Please sign in to comment.