Skip to content

Commit

Permalink
cmd/add: support adding multiple instances from same service
Browse files Browse the repository at this point in the history
Change-Id: I94650b4dcb5a28a9d96c10115ddc9ade02162032
  • Loading branch information
elek authored and Storj Robot committed Aug 29, 2023
1 parent 4783cf8 commit 652da59
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 26 deletions.
14 changes: 11 additions & 3 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@ package cmd
import (
"github.com/spf13/cobra"

"storj.io/storj-up/pkg/recipe"
"storj.io/storj-up/pkg/runtime/runtime"
)

func init() {
RootCmd.AddCommand(&cobra.Command{
var instance int
cmd := &cobra.Command{
Use: "add <selector>",
Short: "add more services to existing stack. " + SelectorHelp,
Args: cobra.MinimumNArgs(1),
RunE: ExecuteStorjUP(runtime.ApplyRecipes),
})
RunE: ExecuteStorjUP(func(stack recipe.Stack, rt runtime.Runtime, args []string) error {
return runtime.ApplyRecipes(stack, rt, args, instance)
}),
}

cmd.PersistentFlags().IntVarP(&instance, "instance", "i", 0, "Number of requested instance (default/0 = use the one defined in the recipe")
RootCmd.AddCommand(cmd)

}
2 changes: 1 addition & 1 deletion cmd/history/undo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_Undo(t *testing.T) {
st, err := recipe.GetStack()
require.NoError(t, err)

err = runtime.ApplyRecipes(st, rt, []string{"satellite-api"})
err = runtime.ApplyRecipes(st, rt, []string{"satellite-api"}, 0)
require.NoError(t, err)

// first modification
Expand Down
6 changes: 3 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func initCmd() *cobra.Command {
if err != nil {
return err
}
err = runtime.ApplyRecipes(st, n, normalizedArgs(args))
err = runtime.ApplyRecipes(st, n, normalizedArgs(args), 0)
if err != nil {
return err
}
Expand All @@ -75,7 +75,7 @@ func initCmd() *cobra.Command {
if err != nil {
return err
}
err = runtime.ApplyRecipes(st, n, normalizedArgs(args))
err = runtime.ApplyRecipes(st, n, normalizedArgs(args), 0)
if err != nil {
return err
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func initCmd() *cobra.Command {
if err != nil {
return err
}
err = runtime.ApplyRecipes(st, n, normalizedArgs(args))
err = runtime.ApplyRecipes(st, n, normalizedArgs(args), 0)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/modify/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestAddFlag(t *testing.T) {
})

rt := compose.NewEmptyCompose(dir)
err := runtime.ApplyRecipes(st, rt, []string{"base"})
err := runtime.ApplyRecipes(st, rt, []string{"base"}, 0)
require.NoError(t, err)

err = addFlag(st, rt, []string{"base", "nf=2"})
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestRemoveFlag(t *testing.T) {
})

rt := compose.NewEmptyCompose(dir)
err := runtime.ApplyRecipes(st, rt, []string{"base"})
err := runtime.ApplyRecipes(st, rt, []string{"base"}, 0)
require.NoError(t, err)

err = removeFlag(st, rt, []string{"base", "flag"})
Expand Down
4 changes: 2 additions & 2 deletions cmd/modify/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAddPort(t *testing.T) {
})

rt := compose.NewEmptyCompose(dir)
err := runtime.ApplyRecipes(st, rt, []string{"base"})
err := runtime.ApplyRecipes(st, rt, []string{"base"}, 0)
require.NoError(t, err)

err = addPort(st, rt, []string{"base", "90"})
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestRemovePort(t *testing.T) {
})

rt := compose.NewEmptyCompose(dir)
err := runtime.ApplyRecipes(st, rt, []string{"base"})
err := runtime.ApplyRecipes(st, rt, []string{"base"}, 0)
require.NoError(t, err)

err = removePort(st, rt, []string{"base", "80"})
Expand Down
2 changes: 1 addition & 1 deletion cmd/testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ func InitCompose(dir string) (st recipe.Stack, rt runtime.Runtime, err error) {
if err != nil {
return
}
err = runtime.ApplyRecipes(st, rt, []string{"db", "minimal"})
err = runtime.ApplyRecipes(st, rt, []string{"db", "minimal"}, 0)
return
}
2 changes: 1 addition & 1 deletion pkg/runtime/compose/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestCompose(t *testing.T) {
st, err := recipe.GetEmbeddedStack()
require.NoError(t, err)

err = runtime.ApplyRecipes(st, rt, st.AllRecipeNames())
err = runtime.ApplyRecipes(st, rt, st.AllRecipeNames(), 0)
require.NoError(t, err)

}
2 changes: 1 addition & 1 deletion pkg/runtime/nomad/nomad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ func TestNomad(t *testing.T) {
st, err := recipe.GetEmbeddedStack()
require.NoError(t, err)

err = runtime.ApplyRecipes(st, rt, st.AllRecipeNames())
err = runtime.ApplyRecipes(st, rt, st.AllRecipeNames(), 0)
require.NoError(t, err)
}
16 changes: 7 additions & 9 deletions pkg/runtime/runtime/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func Match(service Service, matcher recipe.Matcher) bool {
}

// ApplyRecipes can apply full recipes and other services (partial recipes) based on the selectors.
func ApplyRecipes(st recipe.Stack, rt Runtime, selector []string) error {
func ApplyRecipes(st recipe.Stack, rt Runtime, selector []string, instanceOverride int) error {
for _, name := range selector {
rcp, err := st.Get(name)
if err == nil {
Expand All @@ -116,17 +116,15 @@ func ApplyRecipes(st recipe.Stack, rt Runtime, selector []string) error {
for _, r := range st {
for _, s := range r.Add {
if s.Name == name {
instance := s.Instance
if instance == 0 {
instance = 1
if instanceOverride != 0 {
s.Instance = instanceOverride
}

for i := 1; i <= instance; i++ {
err = AddServiceToRuntime(rt, *s)
if err != nil {
return errs.Wrap(err)
}
err = AddServiceToRuntime(rt, *s)
if err != nil {
return errs.Wrap(err)
}

added++
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/runtime/runtime/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestApplyRecipeCreateByRecipe(t *testing.T) {
},
},
}
err := ApplyRecipes(st, rt, []string{"minimal"})
err := ApplyRecipes(st, rt, []string{"minimal"}, 0)
require.NoError(t, err)

require.Len(t, rt.Services, 1)
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestApplyRecipeModifyExisting(t *testing.T) {
},
},
}
err := ApplyRecipes(st, rt, []string{"minimal"})
err := ApplyRecipes(st, rt, []string{"minimal"}, 0)
require.NoError(t, err)

require.Len(t, rt.Services, 2)
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/standalone/standalone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ func TestStandalone(t *testing.T) {
st, err := recipe.GetEmbeddedStack()
require.NoError(t, err)

err = runtime.ApplyRecipes(st, rt, st.AllRecipeNames())
err = runtime.ApplyRecipes(st, rt, st.AllRecipeNames(), 0)
require.NoError(t, err)
}

0 comments on commit 652da59

Please sign in to comment.