Guess endpoint ID if not set when deploying or removing stacks

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-09 00:12:15 -04:00
parent e7a48ccc3e
commit 535eada8ab
3 changed files with 74 additions and 31 deletions

View File

@ -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).")

View File

@ -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")

View File

@ -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 {