Skip to content

Commit

Permalink
add track of version for each node
Browse files Browse the repository at this point in the history
part of #36
  • Loading branch information
Ivan Vlasic committed Nov 26, 2021
1 parent 445e84a commit 5490f3e
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 48 deletions.
25 changes: 0 additions & 25 deletions aws/cloudformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ import (
"github.com/mantil-io/mantil/cli/log"
)

type CloudformationNoUpdatesError struct{}

func (e *CloudformationNoUpdatesError) Error() string {
return fmt.Sprintf("node is already up to date")
}

type CloudFormation struct {
aws *AWS
cli *cloudformation.Client
Expand Down Expand Up @@ -78,14 +72,6 @@ func (f *CloudFormation) CreateStack(name, templateBody string, resourceTags map
}

func (f *CloudFormation) UpdateStack(name, templateBody string, resourceTags map[string]string) (*StackWaiter, error) {
upToDate, err := f.isStackUpToDate(name, templateBody)
if err != nil {
return nil, log.Wrap(err)
}
if upToDate {
return nil, log.Wrap(&CloudformationNoUpdatesError{})
}

usi := &cloudformation.UpdateStackInput{
StackName: aws.String(name),
Capabilities: []types.Capability{types.CapabilityCapabilityNamedIam},
Expand Down Expand Up @@ -162,17 +148,6 @@ func (f *CloudFormation) DeleteStack(name string) *StackWaiter {
return sw
}

func (f *CloudFormation) isStackUpToDate(name, template string) (bool, error) {
gti := &cloudformation.GetTemplateInput{
StackName: aws.String(name),
}
gto, err := f.cli.GetTemplate(context.Background(), gti)
if err != nil {
return false, log.Wrap(err)
}
return aws.ToString(gto.TemplateBody) == template, nil
}

// tries to find reason why stack action failed by going through all the events until first one with status failed is encountered
// asumption is made that first failed event is also the reason for the failure of the stack action
// for stacks that do not longer exists (like in the case of create failure) stack id must be provided instead of name
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func newAwsInstallCommand() *cobra.Command {
showAwsDryRunInfo(a)
return nil
}
if err := stp.Create(domain.Deployment().GetPath); err != nil {
if err := stp.Create(domain.Version(), domain.Deployment().GetPath); err != nil {
return log.Wrap(err)
}
showNextSteps(texts.AwsInstall.NextSteps)
Expand Down Expand Up @@ -109,7 +109,7 @@ func newAwsUpgradeCommand() *cobra.Command {
showAwsDryRunInfo(a)
return nil
}
if err := stp.Upgrade(domain.Deployment().GetPath); err != nil {
if err := stp.Upgrade(domain.Version(), domain.Deployment().GetPath); err != nil {
return log.Wrap(err)
}
showNextSteps(texts.AwsUpgrade.NextSteps)
Expand Down
7 changes: 3 additions & 4 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"strings"

"github.com/mantil-io/mantil/aws"
"github.com/mantil-io/mantil/cli/controller"
"github.com/mantil-io/mantil/cli/log"
"github.com/mantil-io/mantil/cli/ui"
Expand Down Expand Up @@ -233,9 +232,9 @@ Please check the following rules when naming projects, stages and functions:
return
}

var cnue *aws.CloudformationNoUpdatesError
if errors.As(err, &cnue) {
ui.Info("Node is already up to date with the current version of CLI.")
var naud *domain.NodeAlreadyUpToDateError
if errors.As(err, &naud) {
ui.Info("Node %s is already up to date with the current version %s of CLI.", naud.Name, naud.Version)
return
}

Expand Down
4 changes: 2 additions & 2 deletions cli/controller/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ func Nodes() error {
}
var data [][]string
for _, n := range fs.Workspace().Nodes {
data = append(data, []string{n.Name, n.AccountID, n.Region, n.ID})
data = append(data, []string{n.Name, n.AccountID, n.Region, n.ID, n.Version})
}
ShowTable([]string{"name", "AWS Account", "AWS Region", "ID"}, data)
ShowTable([]string{"name", "AWS Account", "AWS Region", "ID", "Version"}, data)
return nil
}

Expand Down
14 changes: 9 additions & 5 deletions cli/controller/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ func NewSetup(a *SetupArgs) (*Setup, error) {
}, nil
}

func (c *Setup) Create(getPath func(string) (string, string)) error {
func (c *Setup) Create(version string, getPath func(string) (string, string)) error {
if !c.regionSupported() {
return log.Wrapf(`currently not available in this region
Available regions are:
+ %s`, strings.Join(supportedAWSRegions, "\n\t+ "))
}
ws := c.store.Workspace()
bucket, key := getPath(c.aws.Region())
n, err := ws.NewNode(c.nodeName, c.aws.AccountID(), c.aws.Region(), bucket, key)
n, err := ws.NewNode(c.nodeName, c.aws.AccountID(), c.aws.Region(), bucket, key, version)
if err != nil {
return log.Wrap(err)
}
Expand Down Expand Up @@ -179,14 +179,18 @@ func (c *Setup) createSetupStack(acf domain.NodeFunctions, suffix string) error
return nil
}

func (c *Setup) Upgrade(getPath func(string) (string, string)) error {
func (c *Setup) Upgrade(version string, getPath func(string) (string, string)) error {
ws := c.store.Workspace()
n := ws.Node(c.nodeName)
if n == nil {
return log.Wrap(&domain.NodeNotFoundError{Name: c.nodeName})
}
if n.Version == version {
return log.Wrap(&domain.NodeAlreadyUpToDateError{Name: n.Name, Version: n.Version})
}

bucket, key := getPath(c.aws.Region())
n.UpdateFunctions(bucket, key)
n.UpgradeVersion(version, bucket, key)

c.stackName = n.SetupStackName()
c.lambdaName = n.SetupLambdaName()
Expand Down Expand Up @@ -231,7 +235,7 @@ func (c *Setup) upgrade(n *domain.Node) error {
InfrastructureDuration: infrastructureDuration,
}})

ui.Title("\nMantil node %s upgraded to version %s.\n", c.nodeName, c.store.Workspace().Version)
ui.Title("\nMantil node %s upgraded to version %s.\n", c.nodeName, n.Version)
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions domain/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ func (e *NodeNotFoundError) Error() string {
return fmt.Sprintf("node %s not found", e.Name)
}

type NodeAlreadyUpToDateError struct {
Name string
Version string
}

func (e *NodeAlreadyUpToDateError) Error() string {
return fmt.Sprintf("node %s already at version %s", e.Name, e.Version)
}

type WorkspaceNoNodesError struct{}

func (e *WorkspaceNoNodesError) Error() string {
Expand Down
1 change: 1 addition & 0 deletions domain/testdata/workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ created_at: 0
nodes:
- name: dev
id: fpdtuji
version: ""
accountID: "052548195718"
region: eu-central-1
bucket: mantil-eu-central-1-052548195718
Expand Down
18 changes: 10 additions & 8 deletions domain/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ type WorkspaceProject struct {
}

type Node struct {
Name string `yaml:"name,omitempty"`
ID string `yaml:"id"`

Name string `yaml:"name,omitempty"`
ID string `yaml:"id"`
Version string `yaml:"version"`
// AWS related attributes
AccountID string `yaml:"accountID"` // AWS account id
Region string `yaml:"region"` // AWS region
Expand Down Expand Up @@ -109,7 +109,7 @@ func (w *Workspace) Node(name string) *Node {
return nil
}

func (w *Workspace) NewNode(name, awsAccountID, awsRegion, functionsBucket, functionsPath string) (*Node, error) {
func (w *Workspace) NewNode(name, awsAccountID, awsRegion, functionsBucket, functionsPath, version string) (*Node, error) {
if w.nodeExists(name) {
return nil, errors.WithStack(&NodeExistsError{name})
}
Expand All @@ -121,8 +121,9 @@ func (w *Workspace) NewNode(name, awsAccountID, awsRegion, functionsBucket, func
bucket := fmt.Sprintf("mantil-%s", uid)
a := &Node{
Name: name,
AccountID: awsAccountID,
ID: uid,
Version: version,
AccountID: awsAccountID,
Region: awsRegion,
Bucket: bucket,
Keys: NodeKeys{
Expand Down Expand Up @@ -155,9 +156,10 @@ func (n *Node) ResourceTags() map[string]string {
}
}

func (n *Node) UpdateFunctions(bucket, path string) {
n.Functions.Bucket = bucket
n.Functions.Path = path
func (n *Node) UpgradeVersion(version, functionsBbucket, functionsPath string) {
n.Version = version
n.Functions.Bucket = functionsBbucket
n.Functions.Path = functionsPath
}

func (n *Node) AuthEnv() map[string]string {
Expand Down
4 changes: 2 additions & 2 deletions domain/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestNewWorkspaceNode(t *testing.T) {
require.Len(t, w.Nodes, 0)

t.Run("add new node", func(t *testing.T) {
a, err := w.NewNode("first", "accountID", "region", "bucket", "path")
a, err := w.NewNode("first", "accountID", "region", "bucket", "path", "vTest")
require.NoError(t, err)
require.Equal(t, a.AccountID, "accountID")
require.Equal(t, a.Region, "region")
Expand All @@ -26,7 +26,7 @@ func TestNewWorkspaceNode(t *testing.T) {

t.Run("add existing node", func(t *testing.T) {
require.True(t, w.nodeExists("first"))
a, err := w.NewNode("first", "accountID", "region", "bucket", "path")
a, err := w.NewNode("first", "accountID", "region", "bucket", "path", "vTest")
require.Nil(t, a)
var ea *NodeExistsError
require.ErrorAs(t, err, &ea)
Expand Down

0 comments on commit 5490f3e

Please sign in to comment.