Skip to content

Commit

Permalink
Progress (will reset-cleanup later)
Browse files Browse the repository at this point in the history
  • Loading branch information
AutomationD committed Apr 21, 2024
1 parent ea078d2 commit e0d7e90
Show file tree
Hide file tree
Showing 23 changed files with 331 additions and 137 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Copyright © 2021 HazelOps OÜ
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
9 changes: 9 additions & 0 deletions internal/aws/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func GetSession(c *SessionConfig) (*session.Session, error) {
upd := false

config := aws.NewConfig().WithRegion(c.Region).WithCredentials(credentials.NewSharedCredentials("", c.Profile)).WithEndpoint(c.EndpointUrl)

if len(c.EndpointUrl) > 0 {
logrus.Debug(fmt.Sprintf("Session established. Endpoint: %s", c.EndpointUrl))
} else {
logrus.Debug(fmt.Sprintf("Session established with a default endpoint"))
}

//
//if len(c.EndpointUrl) > 0 {
// // If EndpointUrl is set to a non-default value specify it
Expand All @@ -55,8 +62,10 @@ func GetSession(c *SessionConfig) (*session.Session, error) {
default:
// Error only if it's not a localhost endpoint
if !(strings.Contains(c.EndpointUrl, "localhost") || strings.Contains(c.EndpointUrl, "127.0.0.1")) {
// If endpoint is not related to LocalStack then it's an error
return nil, err
}

logrus.Debug("[NO MFA] Using Endpoint: ", c.EndpointUrl)
}
}
Expand Down
21 changes: 21 additions & 0 deletions internal/commands/boostrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package commands

import (
"github.com/hazelops/ize/internal/config"
"github.com/spf13/cobra"
)

func NewCmdBoostrap(project *config.Project) *cobra.Command {

cmd := &cobra.Command{
Use: "boostrap",
Short: "Boostrap resources",
TraverseChildren: true,
}

cmd.AddCommand(
NewBoostrapTerraformState(project),
)

return cmd
}
115 changes: 115 additions & 0 deletions internal/commands/boostrap_terraform_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package commands

import (
"fmt"
"github.com/hazelops/ize/internal/config"
"github.com/hazelops/ize/pkg/templates"
"github.com/spf13/cobra"
"text/template"
)

type BootstrapTerraformStateOptions struct {
Config *config.Project
AppName string
Explain bool
}

var boostrapTerraformStateExplainTmpl = `
SERVICE_SECRETS_FILE={{.EnvDir}}/secrets/{{svc}}.json
SERVICE_SECRETS=$(cat $SERVICE_SECRETS_FILE | jq -e -r '. | keys[]')
for item in $(echo $SERVICE_SECRETS); do
aws --profile={{.AwsProfile}} ssm put-parameter --name="/{{.Env}}/{{svc}}/${item}" --value="$(cat $SERVICE_SECRETS_FILE | jq -r .$item )" --type SecureString --overwrite && \
aws --profile={{.AwsProfile}} ssm add-tags-to-resource --resource-type "Parameter" --resource-id "/{{.Env}}/{{svc}}/${item}" \
--tags "Key=Application,Value={{svc}}" "Key=EnvVarName,Value=${item}"
done
`

var boostrapTerraformStateExample = templates.Examples(`
# Boostrap Terraform State:
TBD
`)

func NewBoostrapTerraformStateFlags(project *config.Project) *BootstrapTerraformStateOptions {
return &BootstrapTerraformStateOptions{
Config: project,
}
}

func NewBoostrapTerraformState(project *config.Project) *cobra.Command {
o := NewBoostrapTerraformStateFlags(project)

cmd := &cobra.Command{
Use: "terraform-state",
Example: boostrapTerraformStateExample,
Short: "Boostrap Terraform State",
Long: "This command creates Terraform State bucket and DynamoDB table based on ize name convention",
//Args: cobra.MinimumNArgs(1),
//ValidArgsFunction: config.GetApps,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

err := o.Complete(cmd)
if err != nil {
return err
}

err = o.Validate()
if err != nil {
return err
}

err = o.Run()
if err != nil {
return err
}

return nil
},
}

return cmd
}

func (o *BootstrapTerraformStateOptions) Complete(cmd *cobra.Command) error {
//o.AppName = cmd.Flags().Args()[0]

println("Done")
return nil
}

func (o *BootstrapTerraformStateOptions) Validate() error {
if len(o.Config.Env) == 0 {
return fmt.Errorf("env must be specified\n")
}
println("Valid")

return nil
}

func (o *BootstrapTerraformStateOptions) Run() error {
if o.Explain {
err := o.Config.Generate(boostrapTerraformStateExplainTmpl, template.FuncMap{
"svc": func() string {
return o.AppName
},
})
if err != nil {
return err
}

return nil
}
println("Running")
// TODO: Create bucket via S3 Client with forcePAth ON
//name := "test"
//
//_, err := o.Config.AWSClient.S3Client().CreateBucket(&s3.CreateBucketInput{
// Bucket: &name,
//})
//if err != nil {
// return err
//}

return nil
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions internal/commands/ize.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func newRootCmd(project *config.Project) *cobra.Command {
NewCmdPush(project),
NewCmdUp(project),
NewCmdNvm(project),
NewCmdBoostrap(project),
NewValidateCmd(),
NewVersionCmd())

Expand Down Expand Up @@ -119,8 +120,8 @@ func Execute() {
}

func getConfig(cfg *config.Project) {
if slices.Contains(os.Args, "terraform") ||
slices.Contains(os.Args, "nvm") ||
if slices.Contains(os.Args, "terraform") ||
slices.Contains(os.Args, "nvm") ||
!(slices.Contains(os.Args, "aws-profile") ||
slices.Contains(os.Args, "doc") ||
slices.Contains(os.Args, "completion") ||
Expand Down
1 change: 1 addition & 0 deletions internal/commands/up_infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func (o *UpInfraOptions) Validate() error {

func (o *UpInfraOptions) Run() error {
if o.Explain {
// TODO: Get actual backend.tf from the ize gen tfenv template
tmpl := `# Change to the dir
cd {{.EnvDir}}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Ecs struct {
DependsOn []string `mapstructure:"depends_on,omitempty"`
}

type K8s struct {
type Helm struct {
Name string `mapstructure:",omitempty"`
Path string `mapstructure:",omitempty"`
Image string `mapstructure:",omitempty"`
Expand Down
Loading

0 comments on commit e0d7e90

Please sign in to comment.