forked from ContainX/depcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
117 lines (102 loc) · 3.06 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package marathon
import (
"fmt"
"github.com/gondor/depcon/pkg/encoding"
"github.com/gondor/depcon/pkg/envsubst"
"github.com/gondor/depcon/pkg/httpclient"
"os"
"time"
)
func (c *MarathonClient) CreateGroupFromFile(filename string, opts *CreateOptions) (*Group, error) {
log.Info("Creating Group from file: %s", filename)
options := initCreateOptions(opts)
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("Error opening filename %s, %s", filename, err.Error())
}
var encoder encoding.Encoder
encoder, err = encoding.NewEncoderFromFileExt(filename)
if err != nil {
return nil, err
}
parsed, missing := envsubst.SubstFileTokens(file, filename, options.EnvParams)
if options.ErrorOnMissingParams && missing {
return nil, ErrorAppParamsMissing
}
group := new(Group)
err = encoder.UnMarshalStr(parsed, &group)
if err != nil {
return nil, err
}
return c.CreateGroup(group, options.Wait, options.Force)
}
func (c *MarathonClient) CreateGroup(group *Group, wait, force bool) (*Group, error) {
log.Info("Creating Group '%s', wait: %v, force: %v", group.GroupID, wait, force)
result := new(DeploymentID)
resp := c.http.HttpPost(c.marathonUrl(API_GROUPS), group, result)
if resp.Error != nil {
if resp.Error == httpclient.ErrorMessage {
if resp.Status == 409 {
if force {
return c.UpdateGroup(group, wait)
}
return nil, ErrorGroupExists
}
if resp.Status == 422 {
return nil, ErrorInvalidGroupId
}
return nil, fmt.Errorf("Error occurred (Status %v) Body -> %s", resp.Status, resp.Content)
}
return nil, resp.Error
}
if wait {
if err := c.WaitForDeployment(result.DeploymentID, time.Duration(500)*time.Second); err != nil {
return nil, err
}
}
return group, nil
}
func (c *MarathonClient) UpdateGroup(group *Group, wait bool) (*Group, error) {
log.Info("Update Group '%s', wait = %v", group.GroupID, wait)
result := new(DeploymentID)
resp := c.http.HttpPut(c.marathonUrl(API_GROUPS), group, result)
if resp.Error != nil {
if resp.Error == httpclient.ErrorMessage {
if resp.Status == 422 {
return nil, ErrorGropAppExists
}
}
return nil, resp.Error
}
if wait {
if err := c.WaitForDeployment(result.DeploymentID, c.determineTimeout(nil)); err != nil {
return nil, err
}
}
// Get the latest version of the application to return
return c.GetGroup(group.GroupID)
}
func (c *MarathonClient) ListGroups() (*Groups, error) {
groups := new(Groups)
resp := c.http.HttpGet(c.marathonUrl(API_GROUPS), groups)
if resp.Error != nil {
return nil, resp.Error
}
return groups, nil
}
func (c *MarathonClient) GetGroup(id string) (*Group, error) {
group := new(Group)
resp := c.http.HttpGet(c.marathonUrl(API_GROUPS, id), group)
if resp.Error != nil {
return nil, resp.Error
}
return group, nil
}
func (c *MarathonClient) DestroyGroup(id string) (*DeploymentID, error) {
deploymentId := new(DeploymentID)
resp := c.http.HttpDelete(fmt.Sprintf("%s?force=true", c.marathonUrl(API_GROUPS, id)), nil, deploymentId)
if resp.Error != nil {
return nil, resp.Error
}
return deploymentId, nil
}