Merge pull request #124 from foomo/stackitcloud-stackit

feat: terragrunt & stackit provider
This commit is contained in:
Kevin Franklin Kim 2024-07-16 22:55:39 +02:00 committed by GitHub
commit deaadcb185
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 823 additions and 1 deletions

View File

@ -67,7 +67,7 @@ func New(l log.Logger, cache cache.Cache, opts ...Option) (*Doctl, error) {
}
if err := files.MkdirAll(path.Dir(inst.cfg.ConfigPath)); err != nil {
return nil, errors.Wrapf(err, "failed to create config path: %s", inst.cfg.ConfigPath)
return nil, errors.Wrapf(err, "failed to create config path: %s", path.Dir(inst.cfg.ConfigPath))
}
if err := os.Setenv("DIGITALOCEAN_CONFIG", env.Path(inst.cfg.ConfigPath)); err != nil {

1
go.mod
View File

@ -18,6 +18,7 @@ require (
github.com/samber/lo v1.45.0
github.com/slack-go/slack v0.13.0
github.com/spf13/viper v1.19.0
go.uber.org/zap v1.21.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/oauth2 v0.21.0
golang.org/x/sync v0.7.0

26
go.sum
View File

@ -24,6 +24,8 @@ github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat6
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/charlievieth/fastwalk v1.0.6 h1:C7nXgxQIjEkpKWT1fbXGFzQiblwqq2ZsxrR0ohh5IRs=
github.com/charlievieth/fastwalk v1.0.6/go.mod h1:rV19+IF9Y2TYQNy4MqEk5M/spNHjKsA0i71yrsv2p4E=
github.com/cloudrecipes/packagejson v1.0.0 h1:f6InIxXWQ9/u1XNk7pX2BeUIw7IOYZS5B26o685XbZk=
@ -117,6 +119,7 @@ github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/term v1.1.0 h1:xIAAdCMh3QIAy+5FrE8Ad8XoDhEU4ufwbaSozViP9kk=
@ -183,33 +186,48 @@ github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@ -219,6 +237,7 @@ golang.org/x/sys v0.0.0-20200918174421-af09f7315aff/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@ -246,12 +265,17 @@ golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/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=
@ -259,6 +283,8 @@ gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@ -0,0 +1,21 @@
# POSH terragrunt provider
## Usage
### Plugin
```go
func New(l log.Logger) (plugin.Plugin, error) {
// ...
inst.commands.MustAdd(terragrunt.NewCommand(l, inst.op, inst.cache))
// ...
}
```
### Config
```yaml
terragrunt:
path: devops/terragrunt
cachPath: devops/cache/terragrunt
```

View File

@ -0,0 +1,294 @@
package terragrunt
import (
"context"
"errors"
"os"
"os/exec"
"path"
"strings"
"github.com/foomo/posh-providers/onepassword"
"github.com/foomo/posh/pkg/cache"
"github.com/foomo/posh/pkg/command/tree"
"github.com/foomo/posh/pkg/env"
"github.com/foomo/posh/pkg/log"
"github.com/foomo/posh/pkg/prompt/goprompt"
"github.com/foomo/posh/pkg/readline"
"github.com/foomo/posh/pkg/shell"
"github.com/foomo/posh/pkg/util/files"
"github.com/foomo/posh/pkg/util/suggests"
"github.com/spf13/viper"
"go.uber.org/zap"
)
type (
Command struct {
l log.Logger
op *onepassword.OnePassword
cfg Config
name string
cache cache.Namespace
configKey string
commandTree tree.Root
}
StackNameProvider func(path string) string
CommandOption func(*Command)
)
// ------------------------------------------------------------------------------------------------
// ~ Options
// ------------------------------------------------------------------------------------------------
func CommandWithName(v string) CommandOption {
return func(o *Command) {
o.name = v
}
}
func WithConfigKey(v string) CommandOption {
return func(o *Command) {
o.configKey = v
}
}
// ------------------------------------------------------------------------------------------------
// ~ Constructor
// ------------------------------------------------------------------------------------------------
func NewCommand(l log.Logger, op *onepassword.OnePassword, cache cache.Cache, opts ...CommandOption) (*Command, error) {
inst := &Command{
l: l.Named("terragrunt"),
op: op,
name: "terragrunt",
cache: cache.Get("terragrunt"),
configKey: "terragrunt",
}
for _, opt := range opts {
if opt != nil {
opt(inst)
}
}
if err := viper.UnmarshalKey(inst.configKey, &inst.cfg); err != nil {
return nil, err
}
if err := os.Setenv("TERRAGRUNT_DOWNLOAD", env.Path(inst.cfg.CachePath)); err != nil {
return nil, err
}
stackArgs := tree.Args{
{
Name: "stacks",
Description: "Stacks to run",
Repeat: true,
Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest {
return inst.getStacks(ctx, r)
},
},
}
inst.commandTree = tree.New(&tree.Node{
Name: inst.name,
Description: "Run terragrunt commands",
Nodes: tree.Nodes{
{
Name: "env",
Values: inst.getEnvs,
Description: "Environment to provision",
Nodes: tree.Nodes{
{
Name: "site",
Values: inst.getSites,
Description: "Site to provision",
Nodes: tree.Nodes{
{
Name: "secrets",
Description: "Render secret templates",
Execute: inst.secrets,
},
// terraform: main commands
{
Name: "init",
Description: "Prepare your working directory for other commands",
Args: stackArgs,
Execute: inst.execute,
},
{
Name: "validate",
Description: "Check whether the configuration is valid",
Args: stackArgs,
Execute: inst.execute,
},
{
Name: "plan",
Description: "Show changes required by the current configuration",
Args: stackArgs,
Execute: inst.execute,
},
{
Name: "apply",
Description: "Create or update infrastructure",
Args: stackArgs,
Execute: inst.execute,
},
{
Name: "destroy",
Description: "Destroy previously-created infrastructure",
Args: stackArgs,
Execute: inst.execute,
},
// {
// Name: "command",
// Values: func(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
// return []prompt.Suggest{
// // terraform: main commands
// {Text: "init", Description: "Prepare your working directory for other commands"},
// {Text: "validate", Description: "Check whether the configuration is valid"},
// {Text: "plan", Description: "Show changes required by the current configuration"},
// {Text: "apply", Description: "Create or update infrastructure"},
// {Text: "destroy", Description: "Destroy previously-created infrastructure"},
// // terraform: other commands
// {Text: "hclfmt", Description: "Reformat your configuration in the standard style"},
// // {Text: "console", Description: "Try Terragrunt expressions at an interactive command prompt"},
// {Text: "force-unlock", Description: "Unlock a stuck lock on the current workspace"},
// {Text: "get", Description: "Install or upgrade remote Terragrunt modules"},
// {Text: "graph", Description: "Generate a Graphviz graph of the steps in an operation"},
// {Text: "import", Description: "Associate existing infrastructure with a Terragrunt resource"},
// // {Text: "login", Description: "Obtain and save credentials for a remote host"},
// // {Text: "logout", Description: "Remove locally-stored credentials for a remote host"},
// {Text: "output", Description: "Show output values from your root module"},
// {Text: "providers:lock", Description: "Write out dependency locks for the configured providers"},
// {Text: "refresh", Description: "Update the state to match remote systems"},
// {Text: "show", Description: "Show the current state or a saved plan"},
// {Text: "state", Description: "Advanced state management"},
// // {"taint", "Mark a resource instance as not fully functional"},
// // {"untaint", "Remove the 'tainted' state from a resource instance"},
// // {"version", "Show the current Terragrunt version"},
// // {"workspace", "Workspace management"},
// }
// },
// Description: "Terragrunt command to execute",
// Args: tree.Args{
// {
// Name: "stacks",
// Description: "Stacks to run",
// Repeat: true,
// Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest {
// return inst.getStacks(ctx, r)
// },
// },
// },
// Execute: inst.execute,
// },
},
Execute: inst.execute,
},
},
},
},
})
return inst, nil
}
// ------------------------------------------------------------------------------------------------
// ~ Public methods
// ------------------------------------------------------------------------------------------------
func (c *Command) Name() string {
return c.commandTree.Node().Name
}
func (c *Command) Description() string {
return c.commandTree.Node().Description
}
func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
return c.commandTree.Complete(ctx, r)
}
func (c *Command) Execute(ctx context.Context, r *readline.Readline) error {
return c.commandTree.Execute(ctx, r)
}
func (c *Command) Validate(ctx context.Context, r *readline.Readline) error {
if _, err := exec.LookPath("terragrunt"); err != nil {
c.l.Print()
return errors.New("missing terragrunt executable")
}
return nil
}
func (c *Command) Help(ctx context.Context, r *readline.Readline) string {
return c.commandTree.Help(ctx, r)
}
// ------------------------------------------------------------------------------------------------
// ~ Private methods
// ------------------------------------------------------------------------------------------------
func (c *Command) secrets(ctx context.Context, r *readline.Readline) error {
env := r.Args().At(0)
site := r.Args().At(1)
// validate stack & change dir
values, err := files.Find(ctx, c.cfg.StacksPath(env, site), "secrets.tpl.yaml", files.FindWithIsFile(true))
if err != nil {
return err
}
c.l.Info("Rendering secret templates...")
for _, value := range values {
c.l.Info("└ " + value)
if err := c.op.RenderFileTo(ctx, value, strings.Replace(value, ".tpl.yaml", ".yaml", 1)); err != nil {
return err
}
}
return nil
}
func (c *Command) execute(ctx context.Context, r *readline.Readline) error {
environment := r.Args().At(0)
site := r.Args().At(1)
command := r.Args().At(2)
stacks := r.Args().From(3)
c.l.Info("Running terragrunt...")
for _, stack := range stacks {
c.l.Info("└ " + stack)
if err := shell.New(ctx, c.l, "terragrunt", command).
Args(r.AdditionalFlags()...).
Dir(path.Join(c.cfg.StacksPath(environment, site), stack)).
Run(); err != nil {
return err
}
}
return nil
}
func (c *Command) getEnvs(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
return c.cache.GetSuggests("envs", func() any {
return suggests.List(c.cfg.EnvNames())
})
}
func (c *Command) getSites(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
env := r.Args().At(0)
return c.cache.GetSuggests("sites-"+env, func() any {
return suggests.List(c.cfg.SiteNames(env))
})
}
func (c *Command) getStacks(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
env := r.Args().At(0)
site := r.Args().At(1)
return c.cache.GetSuggests("stacks-"+env+"-"+site, func() any {
stacks, err := c.cfg.StackNames(ctx, env, site)
if err != nil {
c.l.Debug("failed to retrieve stacks", zap.Error(err))
}
return suggests.List(stacks)
})
}

View File

@ -0,0 +1,74 @@
package terragrunt
import (
"context"
"os"
"path"
"strings"
"github.com/foomo/posh/pkg/util/files"
"golang.org/x/exp/slices"
)
type Config struct {
Path string `json:"path" yaml:"path"`
CachePath string `json:"cachePath" yaml:"cachePath"`
}
// ------------------------------------------------------------------------------------------------
// ~ Public methods
// ------------------------------------------------------------------------------------------------
func (c Config) EnvNames() []string {
return c.dirNames(c.EnvsPath())
}
func (c Config) EnvsPath() string {
return path.Join(c.Path, "envs")
}
func (c Config) SiteNames(env string) []string {
return c.dirNames(c.SitesPath(env))
}
func (c Config) SitesPath(env string) string {
return path.Join(c.EnvsPath(), env)
}
func (c Config) StackNames(ctx context.Context, env, site string) ([]string, error) {
var ret []string
root := c.StacksPath(env, site)
out, err := files.Find(ctx, root, "terragrunt.hcl", files.FindWithIsFile(true))
if err != nil {
return nil, err
}
for _, v := range out {
v = strings.TrimPrefix(v, root)
v = strings.TrimSuffix(v, "/terragrunt.hcl")
if v = strings.TrimPrefix(v, "/"); strings.Contains(v, "/") {
ret = append(ret, v)
}
}
slices.Sort(ret)
return ret, err
}
func (c Config) StacksPath(env, site string) string {
return path.Join(c.SitesPath(env), site)
}
// ------------------------------------------------------------------------------------------------
// ~ Private methods
// ------------------------------------------------------------------------------------------------
func (c Config) dirNames(path string) []string {
var ret []string
if files, err := os.ReadDir(path); err == nil {
for _, value := range files {
if value.IsDir() && !strings.HasPrefix(value.Name(), ".") && !strings.HasPrefix(value.Name(), "_") {
ret = append(ret, value.Name())
}
}
}
return ret
}

View File

@ -0,0 +1,80 @@
# POSH stackit provider
## Usage
### Plugin
```go
package main
import (
"github.com/foomo/posh-providers/kubernets/kubectl"
"github.com/foomo/posh-providers/stackitcloud/stackit"
"github.com/foomo/posh/pkg/cache"
"github.com/foomo/posh/pkg/command"
)
type Plugin struct {
l log.Logger
cache cache.Cache
stackit *stackit.Stackit
kubectl *kubectl.Kubectl
commands command.Commands
}
func New(l log.Logger) (plugin.Plugin, error) {
var err error
inst := &Plugin{
l: l,
cache: &cache.MemoryCache{},
commands: command.Commands{},
}
// ...
inst.kubectl, err = kubectl.New(l, inst.cache)
if err != nil {
return nil, errors.Wrap(err, "failed to create kubectl")
}
inst.stackit, err = stackit.New(l, inst.cache)
if err != nil {
return nil, errors.Wrap(err, "failed to create stackit")
}
// ...
inst.commands.Add(stackit.NewCommand(l, inst.cache, inst.stackit, inst.kubectl))
// ...
return inst, nil
}
```
### Config
```yaml
## stackit
stackit:
projects:
my-project:
id: 123456-123456-123456
clusters:
dev:
name: my-project-dev-cluster
```
### Ownbrew
To install binary locally, add:
```yaml
ownbrew:
packages:
## https://github.com/stackitcloud/stackit-cli/releases
- name: stackit
tap: foomo/tap/stackitcloud/stackit-cli
version: 0.9.0
```

View File

@ -0,0 +1,5 @@
package stackit
type Cluster struct {
Name string `json:"name"`
}

View File

@ -0,0 +1,183 @@
package stackit
import (
"context"
"github.com/foomo/posh-providers/kubernets/kubectl"
"github.com/foomo/posh/pkg/cache"
"github.com/foomo/posh/pkg/command/tree"
"github.com/foomo/posh/pkg/log"
"github.com/foomo/posh/pkg/prompt/goprompt"
"github.com/foomo/posh/pkg/readline"
"github.com/foomo/posh/pkg/shell"
"github.com/foomo/posh/pkg/util/suggests"
"github.com/pkg/errors"
)
type (
Command struct {
l log.Logger
name string
cache cache.Namespace
stackit *Stackit
kubectl *kubectl.Kubectl
commandTree tree.Root
clusterNameFn ClusterNameFn
}
ClusterNameFn func(name string, cluster Cluster) string
CommandOption func(*Command)
)
// ------------------------------------------------------------------------------------------------
// ~ Options
// ------------------------------------------------------------------------------------------------
func CommandWithName(v string) CommandOption {
return func(o *Command) {
o.name = v
}
}
func CommandWithClusterNameFn(v ClusterNameFn) CommandOption {
return func(o *Command) {
o.clusterNameFn = v
}
}
// ------------------------------------------------------------------------------------------------
// ~ Constructor
// ------------------------------------------------------------------------------------------------
func NewCommand(l log.Logger, cache cache.Cache, stackit *Stackit, kubectl *kubectl.Kubectl, opts ...CommandOption) *Command {
inst := &Command{
l: l.Named("stackit"),
name: "stackit",
cache: cache.Get("stackit"),
stackit: stackit,
kubectl: kubectl,
clusterNameFn: func(name string, cluster Cluster) string {
return name
},
}
for _, opt := range opts {
if opt != nil {
opt(inst)
}
}
inst.commandTree = tree.New(&tree.Node{
Name: inst.name,
Description: "Manage stackit cloud resources",
Nodes: tree.Nodes{
{
Name: "auth",
Description: "Login to the stackit cloud provider",
Execute: inst.auth,
},
{
Name: "project",
Description: "Run a command against the given project",
Nodes: tree.Nodes{
{
Name: "project",
Values: func(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
return suggests.List(inst.stackit.Config().ProjectNames())
},
Description: "Project to run against",
Nodes: tree.Nodes{
{
Name: "kubeconfig",
Description: "Retrieve credentials to access remote cluster.",
Args: tree.Args{
{
Name: "cluster",
Description: "Name of the cluster.",
Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest {
project, err := inst.stackit.Config().Project(r.Args().At(0))
if err != nil {
return []goprompt.Suggest{}
}
return suggests.List(project.ClusterNames())
},
},
},
Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error {
fs.Internal().String("profile", "", "Store credentials in given profile.")
return fs.Internal().SetValues("profile", "stackit")
},
Execute: inst.kubeconfig,
},
},
},
},
},
},
})
return inst
}
// ------------------------------------------------------------------------------------------------
// ~ Public methods
// ------------------------------------------------------------------------------------------------
func (c *Command) Name() string {
return c.commandTree.Node().Name
}
func (c *Command) Description() string {
return c.commandTree.Node().Description
}
func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
return c.commandTree.Complete(ctx, r)
}
func (c *Command) Execute(ctx context.Context, r *readline.Readline) error {
return c.commandTree.Execute(ctx, r)
}
func (c *Command) Help(ctx context.Context, r *readline.Readline) string {
return c.commandTree.Help(ctx, r)
}
// ------------------------------------------------------------------------------------------------
// ~ Private methods
// ------------------------------------------------------------------------------------------------
func (c *Command) kubeconfig(ctx context.Context, r *readline.Readline) error {
ifs := r.FlagSets().Internal()
project, err := c.stackit.Config().Project(r.Args().At(0))
if err != nil {
return err
}
clusterName := r.Args().At(2)
cluster, err := project.Cluster(clusterName)
if err != nil {
return err
}
kubectlCluster := c.kubectl.Cluster(c.clusterNameFn(clusterName, cluster))
if kubectlCluster == nil {
return errors.Errorf("failed to retrieve kubectl cluster for: %q", cluster.Name)
}
profile, err := ifs.GetString("profile")
if err != nil {
return err
}
return shell.New(ctx, c.l, "stackit", "ske", "cluster", "kubeconfig", "create", cluster.Name).
Args("--filepath", kubectlCluster.Config(profile)).
Args("--project-id", project.ID).
Args(r.AdditionalArgs()...).
Run()
}
func (c *Command) auth(ctx context.Context, r *readline.Readline) error {
return shell.New(ctx, c.l, "stackit", "auth", "login").
Args(r.Flags()...).
Args(r.AdditionalArgs()...).
Args(r.AdditionalFlags()...).
Run()
}

View File

@ -0,0 +1,30 @@
package stackit
import (
"sort"
"github.com/pkg/errors"
"github.com/samber/lo"
)
type Config struct {
Projects map[string]Project `json:"projects" yaml:"projects"`
}
// ------------------------------------------------------------------------------------------------
// ~ Public methods
// ------------------------------------------------------------------------------------------------
func (c Config) ProjectNames() []string {
ret := lo.Keys(c.Projects)
sort.Strings(ret)
return ret
}
func (c Config) Project(name string) (Project, error) {
value, ok := c.Projects[name]
if !ok {
return Project{}, errors.Errorf("given project not found: %s", name)
}
return value, nil
}

View File

@ -0,0 +1,31 @@
package stackit
import (
"sort"
"github.com/pkg/errors"
"github.com/samber/lo"
)
type Project struct {
ID string `json:"id" yaml:"id"`
Clusters map[string]Cluster `json:"clusters" yaml:"clusters"`
}
// ------------------------------------------------------------------------------------------------
// ~ Public methods
// ------------------------------------------------------------------------------------------------
func (c Project) Cluster(name string) (Cluster, error) {
value, ok := c.Clusters[name]
if !ok {
return Cluster{}, errors.Errorf("given cluster not found: %s", name)
}
return value, nil
}
func (c Project) ClusterNames() []string {
ret := lo.Keys(c.Clusters)
sort.Strings(ret)
return ret
}

View File

@ -0,0 +1,77 @@
package stackit
import (
"context"
"github.com/foomo/posh/pkg/cache"
"github.com/foomo/posh/pkg/log"
"github.com/spf13/viper"
)
type (
Stackit struct {
l log.Logger
cfg Config
cache cache.Namespace
configKey string
}
Option func(*Stackit) error
AuthTokenProvider func(ctx context.Context, kubeContext string) (token string, err error)
)
// ------------------------------------------------------------------------------------------------
// ~ Options
// ------------------------------------------------------------------------------------------------
func CommandWithConfigKey(v string) Option {
return func(o *Stackit) error {
o.configKey = v
return nil
}
}
// ------------------------------------------------------------------------------------------------
// ~ Constructor
// ------------------------------------------------------------------------------------------------
func New(l log.Logger, cache cache.Cache, opts ...Option) (*Stackit, error) {
inst := &Stackit{
l: l,
cache: cache.Get("stackit"),
configKey: "stackit",
}
for _, opt := range opts {
if opt != nil {
if err := opt(inst); err != nil {
return nil, err
}
}
}
if err := viper.UnmarshalKey(inst.configKey, &inst.cfg); err != nil {
return nil, err
}
return inst, nil
}
// ------------------------------------------------------------------------------------------------
// ~ Getter
// ------------------------------------------------------------------------------------------------
func (s *Stackit) Config() Config {
return s.cfg
}
// ------------------------------------------------------------------------------------------------
// ~ Public methods
// ------------------------------------------------------------------------------------------------
func (s *Stackit) ClusterNames(projectName string) ([]string, error) {
project, err := s.cfg.Project(projectName)
if err != nil {
return nil, err
}
return project.ClusterNames(), nil
}