Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(*): add upgrade action support #135

Merged
merged 1 commit into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/exec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func buildRootCommand(in io.Reader) *cobra.Command {
cmd.AddCommand(buildVersionCommand(m))
cmd.AddCommand(buildBuildCommand(m))
cmd.AddCommand(buildInstallCommand(m))
cmd.AddCommand(buildUpgradeCommand(m))
cmd.AddCommand(buildUninstallCommand(m))

return cmd
Expand Down
22 changes: 22 additions & 0 deletions cmd/exec/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"github.com/deislabs/porter/pkg/mixin/exec"
"github.com/spf13/cobra"
)

func buildUpgradeCommand(m *exec.Mixin) *cobra.Command {
var opts struct {
file string
}
cmd := &cobra.Command{
Use: "upgrade",
Short: "Execute the upgrade functionality of this mixin",
RunE: func(cmd *cobra.Command, args []string) error {
return m.Upgrade(opts.file)
},
}
flags := cmd.Flags()
flags.StringVarP(&opts.file, "file", "f", "", "Path to the script to execute")
return cmd
}
25 changes: 21 additions & 4 deletions pkg/config/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Manifest struct {
Mixins []string `yaml:"mixins,omitempty"`
Install Steps `yaml:"install"`
Uninstall Steps `yaml:"uninstall"`
Upgrade Steps `yaml:"upgrade"`
Parameters []ParameterDefinition `yaml:"parameters,omitempty"`
Credentials []CredentialDefinition `yaml:"credentials,omitempty"`
Dependencies []*Dependency `yaml:"dependencies,omitempty"`
Expand Down Expand Up @@ -244,6 +245,8 @@ func (m *Manifest) GetSteps(action Action) (Steps, error) {
steps = m.Install
case ActionUninstall:
steps = m.Uninstall
case ActionUpgrade:
steps = m.Upgrade
}

if len(steps) == 0 {
Expand Down Expand Up @@ -315,16 +318,22 @@ func (m *Manifest) MergeDependency(dep *Dependency) error {
// append uninstall steps so that we unroll it in dependency order (i.e. uninstall wordpress before we delete the database)
m.MergeUninstall(dep)

// prepend dependency's upgrade steps
m.MergeUpgrade(dep)

return nil
}

func (m *Manifest) MergeInstall(dep *Dependency) {
dep.m.Install.setDependency(dep)

result := make(Steps, len(m.Install)+len(dep.m.Install))
copy(result[:len(m.Install)], dep.m.Install)
copy(result[len(m.Install):], m.Install)
m.Install = result
m.Install = prependSteps(dep.m.Install, m.Install)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Yay! Thank you for tidying this up. It sparks much joy

}

func (m *Manifest) MergeUpgrade(dep *Dependency) {
dep.m.Upgrade.setDependency(dep)

m.Upgrade = prependSteps(dep.m.Upgrade, m.Upgrade)
}

func (m *Manifest) MergeUninstall(dep *Dependency) {
Expand All @@ -333,6 +342,14 @@ func (m *Manifest) MergeUninstall(dep *Dependency) {
m.Uninstall = append(m.Uninstall, dep.m.Uninstall...)
}

func prependSteps(s1, s2 Steps) Steps {
result := make(Steps, len(s2)+len(s1))
copy(result[:len(s2)], s1)
copy(result[len(s2):], s2)

return result
}

func prependMixins(m1, m2 []string) []string {
mixins := make([]string, len(m1), len(m1)+len(m2))
copy(mixins, m1)
Expand Down
10 changes: 10 additions & 0 deletions pkg/config/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ func TestManifest_MergeDependency(t *testing.T) {
Install: Steps{
&Step{Description: "install wordpress"},
},
Upgrade: Steps{
&Step{Description: "upgrade wordpress"},
},
Uninstall: Steps{
&Step{Description: "uninstall wordpress"},
},
Expand All @@ -239,6 +242,9 @@ func TestManifest_MergeDependency(t *testing.T) {
Install: Steps{
&Step{Description: "install mysql"},
},
Upgrade: Steps{
&Step{Description: "upgrade mysql"},
},
Uninstall: Steps{
&Step{Description: "uninstall mysql"},
},
Expand All @@ -257,6 +263,10 @@ func TestManifest_MergeDependency(t *testing.T) {
assert.Equal(t, "install mysql", m.Install[0].Description)
assert.Equal(t, "install wordpress", m.Install[1].Description)

assert.Len(t, m.Upgrade, 2)
assert.Equal(t, "upgrade mysql", m.Upgrade[0].Description)
assert.Equal(t, "upgrade wordpress", m.Upgrade[1].Description)

assert.Len(t, m.Uninstall, 2)
assert.Equal(t, "uninstall wordpress", m.Uninstall[0].Description)
assert.Equal(t, "uninstall mysql", m.Uninstall[1].Description)
Expand Down
7 changes: 2 additions & 5 deletions pkg/mixin/exec/uninstall.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package exec

func (m *Mixin) Uninstall(commandFile string) error {
err := m.LoadInstruction(commandFile)
if err != nil {
return err
}
return m.Execute()
// re-use Install's logic
return m.Install(commandFile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a follow-on PR, it's up for grabs to anyone (doesn't have to be you!) who would like to extract the logic from m.Install into a function that is named a bit more generically so that all of these functions can call into it, maybe m.ExecuteScript so that we don't need this comment.

It's a bit confusing to see uninstall calling to install here. I know it's because exec is an odd-ball mixin. 😀

}
6 changes: 6 additions & 0 deletions pkg/mixin/exec/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package exec

func (m *Mixin) Upgrade(commandFile string) error {
// re-use Install's logic
return m.Install(commandFile)
}