2019-07-21 02:00:04 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2019-08-09 04:12:15 +00:00
|
|
|
"errors"
|
2019-07-21 02:00:04 +00:00
|
|
|
"fmt"
|
2019-08-03 00:25:22 +00:00
|
|
|
|
2019-08-07 03:08:59 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2019-08-03 00:25:22 +00:00
|
|
|
"github.com/greenled/portainer-stack-utils/client"
|
2019-07-21 02:00:04 +00:00
|
|
|
)
|
|
|
|
|
2019-08-09 04:12:15 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-07 03:08:59 +00:00
|
|
|
func GetStackByName(name string, swarmId string, endpointId uint32) (stack client.Stack, err error) {
|
|
|
|
portainerClient, err := GetClient()
|
2019-07-21 02:00:04 +00:00
|
|
|
if err != nil {
|
2019-08-02 16:03:43 +00:00
|
|
|
return
|
2019-07-21 02:00:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 03:08:59 +00:00
|
|
|
stacks, err := portainerClient.GetStacks(swarmId, endpointId)
|
2019-07-21 02:00:04 +00:00
|
|
|
if err != nil {
|
2019-08-02 17:10:59 +00:00
|
|
|
return
|
2019-07-21 02:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, stack := range stacks {
|
|
|
|
if stack.Name == name {
|
|
|
|
return stack, nil
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 17:10:59 +00:00
|
|
|
err = &StackNotFoundError{
|
2019-07-21 02:00:04 +00:00
|
|
|
StackName: name,
|
|
|
|
}
|
2019-08-02 17:10:59 +00:00
|
|
|
return
|
2019-07-21 02:00:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 03:08:59 +00:00
|
|
|
func GetEndpointSwarmClusterId(endpointId uint32) (endpointSwarmClusterId string, err error) {
|
|
|
|
// Get docker information for endpoint
|
|
|
|
portainerClient, err := GetClient()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"endpoint": endpointId,
|
|
|
|
}).Debug("Getting endpoint's Docker info")
|
|
|
|
result, err := portainerClient.GetEndpointDockerInfo(endpointId)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get swarm (if any) information for endpoint
|
|
|
|
id, selectionErr := selectValue(result, []string{"Swarm", "Cluster", "ID"})
|
|
|
|
switch selectionErr.(type) {
|
|
|
|
case nil:
|
|
|
|
endpointSwarmClusterId = id.(string)
|
|
|
|
case *valueNotFoundError:
|
|
|
|
err = &StackClusterNotFoundError{}
|
|
|
|
default:
|
|
|
|
err = selectionErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectValue(jsonMap map[string]interface{}, jsonPath []string) (interface{}, error) {
|
|
|
|
value := jsonMap[jsonPath[0]]
|
|
|
|
if value == nil {
|
|
|
|
return nil, &valueNotFoundError{}
|
|
|
|
} else if len(jsonPath) > 1 {
|
|
|
|
return selectValue(value.(map[string]interface{}), jsonPath[1:])
|
|
|
|
} else {
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 02:00:04 +00:00
|
|
|
// Custom customerrors
|
|
|
|
type StackNotFoundError struct {
|
|
|
|
StackName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StackNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("Stack %s not found", e.StackName)
|
|
|
|
}
|
2019-08-07 03:08:59 +00:00
|
|
|
|
|
|
|
type valueNotFoundError struct{}
|
|
|
|
|
|
|
|
func (e *valueNotFoundError) Error() string {
|
|
|
|
return "Value not found"
|
|
|
|
}
|
|
|
|
|
|
|
|
type StackClusterNotFoundError struct{}
|
|
|
|
|
|
|
|
func (e *StackClusterNotFoundError) Error() string {
|
|
|
|
return "Stack cluster not found"
|
|
|
|
}
|