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] stackName := args[0]
endpointId := viper.GetInt32("stack.deploy.endpoint") 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)) endpointSwarmClusterId, selectionErr := common.GetEndpointSwarmClusterId(uint32(endpointId))
switch selectionErr.(type) { switch selectionErr.(type) {
case nil: case nil:
@ -139,7 +151,7 @@ func init() {
stackCmd.AddCommand(stackDeployCmd) stackCmd.AddCommand(stackDeployCmd)
stackDeployCmd.Flags().StringP("stack-file", "c", "", "Path to a file with the content of the stack.") 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().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().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).") stackDeployCmd.Flags().BoolP("prune", "r", false, "Prune services that are no longer referenced (only available for Swarm stacks).")

View File

@ -16,15 +16,25 @@ var stackRemoveCmd = &cobra.Command{
Example: "psu stack rm mystack", Example: "psu stack rm mystack",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
portainerClient, clientRetrievalErr := common.GetClient()
common.CheckError(clientRetrievalErr)
stackName := args[0] stackName := args[0]
endpointId := viper.GetInt32("stack.remove.endpoint") endpointId := viper.GetInt32("stack.remove.endpoint")
var endpointSwarmClusterId string var endpointSwarmClusterId string
var stack client.Stack var stack client.Stack
// Guess EndpointID if not set
if endpointId == 0 { if endpointId == 0 {
logrus.Warning("Endpoint ID not set")
endpoint, err := common.GetDefaultEndpoint()
common.CheckError(err)
endpointId = int32(endpoint.Id)
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"flag": "--endpoint", "endpoint": endpointId,
}).Fatal("Provide required flag") }).Debug("Using the only available endpoint")
} else { }
var selectionErr, stackRetrievalErr error var selectionErr, stackRetrievalErr error
endpointSwarmClusterId, selectionErr = common.GetEndpointSwarmClusterId(uint32(endpointId)) endpointSwarmClusterId, selectionErr = common.GetEndpointSwarmClusterId(uint32(endpointId))
switch selectionErr.(type) { switch selectionErr.(type) {
@ -49,7 +59,6 @@ var stackRemoveCmd = &cobra.Command{
// Something else happened // Something else happened
common.CheckError(selectionErr) common.CheckError(selectionErr)
} }
}
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"stack": stackName, "stack": stackName,
}).Debug("Getting stack") }).Debug("Getting stack")
@ -60,9 +69,6 @@ var stackRemoveCmd = &cobra.Command{
// The stack exists // The stack exists
stackId := stack.Id stackId := stack.Id
portainerClient, err := common.GetClient()
common.CheckError(err)
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"stack": stackName, "stack": stackName,
}).Info("Removing stack") }).Info("Removing stack")

View File

@ -1,6 +1,7 @@
package common package common
import ( import (
"errors"
"fmt" "fmt"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -8,6 +9,30 @@ import (
"github.com/greenled/portainer-stack-utils/client" "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) { func GetStackByName(name string, swarmId string, endpointId uint32) (stack client.Stack, err error) {
portainerClient, err := GetClient() portainerClient, err := GetClient()
if err != nil { if err != nil {