From 535eada8ab8ae4e74262e286f8ede2c5ba1a0c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Fri, 9 Aug 2019 00:12:15 -0400 Subject: [PATCH] Guess endpoint ID if not set when deploying or removing stacks --- cmd/stackDeploy.go | 14 +++++++++- cmd/stackRemove.go | 66 +++++++++++++++++++++++++--------------------- common/utils.go | 25 ++++++++++++++++++ 3 files changed, 74 insertions(+), 31 deletions(-) diff --git a/cmd/stackDeploy.go b/cmd/stackDeploy.go index d04a8ef..d056bfb 100644 --- a/cmd/stackDeploy.go +++ b/cmd/stackDeploy.go @@ -32,6 +32,18 @@ var stackDeployCmd = &cobra.Command{ stackName := args[0] endpointId := viper.GetInt32("stack.deploy.endpoint") + + // Guess EndpointID if not set + if endpointId == 0 { + logrus.Warning("Endpoint ID not set") + endpoint, err := common.GetDefaultEndpoint() + common.CheckError(err) + endpointId = int32(endpoint.Id) + logrus.WithFields(logrus.Fields{ + "endpoint": endpointId, + }).Debug("Using the only available endpoint") + } + endpointSwarmClusterId, selectionErr := common.GetEndpointSwarmClusterId(uint32(endpointId)) switch selectionErr.(type) { case nil: @@ -139,7 +151,7 @@ func init() { stackCmd.AddCommand(stackDeployCmd) stackDeployCmd.Flags().StringP("stack-file", "c", "", "Path to a file with the content of the stack.") - stackDeployCmd.Flags().Uint32("endpoint", 1, "Endpoint ID.") + stackDeployCmd.Flags().Uint32("endpoint", 0, "Endpoint ID.") stackDeployCmd.Flags().StringP("env-file", "e", "", "Path to a file with environment variables used during stack deployment.") stackDeployCmd.Flags().Bool("replace-env", false, "Replace environment variables instead of merging them.") stackDeployCmd.Flags().BoolP("prune", "r", false, "Prune services that are no longer referenced (only available for Swarm stacks).") diff --git a/cmd/stackRemove.go b/cmd/stackRemove.go index afbadca..2ea323b 100644 --- a/cmd/stackRemove.go +++ b/cmd/stackRemove.go @@ -16,39 +16,48 @@ var stackRemoveCmd = &cobra.Command{ Example: "psu stack rm mystack", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { + portainerClient, clientRetrievalErr := common.GetClient() + common.CheckError(clientRetrievalErr) + stackName := args[0] endpointId := viper.GetInt32("stack.remove.endpoint") var endpointSwarmClusterId string var stack client.Stack + + // Guess EndpointID if not set if endpointId == 0 { + logrus.Warning("Endpoint ID not set") + endpoint, err := common.GetDefaultEndpoint() + common.CheckError(err) + endpointId = int32(endpoint.Id) logrus.WithFields(logrus.Fields{ - "flag": "--endpoint", - }).Fatal("Provide required flag") - } else { - var selectionErr, stackRetrievalErr error - endpointSwarmClusterId, selectionErr = common.GetEndpointSwarmClusterId(uint32(endpointId)) - switch selectionErr.(type) { - case nil: - // It's a swarm cluster - logrus.WithFields(logrus.Fields{ - "stack": stackName, - "endpoint": endpointId, - "swarm": endpointSwarmClusterId, - }).Debug("Getting stack") - stack, stackRetrievalErr = common.GetStackByName(stackName, endpointSwarmClusterId, uint32(endpointId)) - common.CheckError(stackRetrievalErr) - case *common.StackClusterNotFoundError: - // It's not a swarm cluster - logrus.WithFields(logrus.Fields{ - "stack": stackName, - "endpoint": endpointId, - }).Debug("Getting stack") - stack, stackRetrievalErr = common.GetStackByName(stackName, "", uint32(endpointId)) - common.CheckError(stackRetrievalErr) - default: - // Something else happened - common.CheckError(selectionErr) - } + "endpoint": endpointId, + }).Debug("Using the only available endpoint") + } + + var selectionErr, stackRetrievalErr error + endpointSwarmClusterId, selectionErr = common.GetEndpointSwarmClusterId(uint32(endpointId)) + switch selectionErr.(type) { + case nil: + // It's a swarm cluster + logrus.WithFields(logrus.Fields{ + "stack": stackName, + "endpoint": endpointId, + "swarm": endpointSwarmClusterId, + }).Debug("Getting stack") + stack, stackRetrievalErr = common.GetStackByName(stackName, endpointSwarmClusterId, uint32(endpointId)) + common.CheckError(stackRetrievalErr) + case *common.StackClusterNotFoundError: + // It's not a swarm cluster + logrus.WithFields(logrus.Fields{ + "stack": stackName, + "endpoint": endpointId, + }).Debug("Getting stack") + stack, stackRetrievalErr = common.GetStackByName(stackName, "", uint32(endpointId)) + common.CheckError(stackRetrievalErr) + default: + // Something else happened + common.CheckError(selectionErr) } logrus.WithFields(logrus.Fields{ "stack": stackName, @@ -60,9 +69,6 @@ var stackRemoveCmd = &cobra.Command{ // The stack exists stackId := stack.Id - portainerClient, err := common.GetClient() - common.CheckError(err) - logrus.WithFields(logrus.Fields{ "stack": stackName, }).Info("Removing stack") diff --git a/common/utils.go b/common/utils.go index f82b6bd..d625d9f 100644 --- a/common/utils.go +++ b/common/utils.go @@ -1,6 +1,7 @@ package common import ( + "errors" "fmt" "github.com/sirupsen/logrus" @@ -8,6 +9,30 @@ import ( "github.com/greenled/portainer-stack-utils/client" ) +func GetDefaultEndpoint() (endpoint client.EndpointSubset, err error) { + portainerClient, err := GetClient() + if err != nil { + return + } + + logrus.Debug("Getting endpoints") + endpoints, err := portainerClient.GetEndpoints() + if err != nil { + return + } + + if len(endpoints) == 0 { + err = errors.New("No endpoints available") + return + } else if len(endpoints) > 1 { + err = errors.New("Several endpoints available") + return + } + endpoint = endpoints[0] + + return +} + func GetStackByName(name string, swarmId string, endpointId uint32) (stack client.Stack, err error) { portainerClient, err := GetClient() if err != nil {