-
Notifications
You must be signed in to change notification settings - Fork 5
/
toggle_cc_job.go
83 lines (72 loc) · 1.95 KB
/
toggle_cc_job.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
package cfbackup
import (
"errors"
"fmt"
"strings"
"time"
"github.com/pivotalservices/gtils/bosh"
. "github.com/pivotalservices/gtils/http"
)
// Not ping server so frequently and exausted the resources
var TaskPingFreq time.Duration = 1000 * time.Millisecond
type CloudControllerJobs []CCJob
type CloudController struct {
deploymentName string
director bosh.Bosh
cloudControllers CloudControllerJobs
manifest string
}
var NewDirector = func(ip, username, password string, port int) bosh.Bosh {
return bosh.NewBoshDirector(ip, username, password, port, NewHttpGateway())
}
func NewCloudController(ip, username, password, deploymentName, manifest string, cloudControllers CloudControllerJobs) *CloudController {
director := NewDirector(ip, username, password, 25555)
return &CloudController{
deploymentName: deploymentName,
director: director,
cloudControllers: cloudControllers,
manifest: manifest,
}
}
func (c *CloudController) Start() error {
return c.toggleController("started")
}
func (c *CloudController) Stop() error {
return c.toggleController("stopped")
}
func (c *CloudController) toggleController(state string) error {
for _, ccjob := range c.cloudControllers {
taskId, err := c.director.ChangeJobState(c.deploymentName, ccjob.Job, state, ccjob.Index, strings.NewReader(c.manifest))
if err != nil {
return err
}
err = c.waitUntilDone(taskId)
if err != nil {
return err
}
}
return nil
}
func (c *CloudController) waitUntilDone(taskId int) (err error) {
time.Sleep(TaskPingFreq)
result, err := c.director.RetrieveTaskStatus(taskId)
if err != nil {
return
}
switch bosh.TASKRESULT[result.State] {
case bosh.ERROR:
err = errors.New(fmt.Sprintf("Task %d process failed", taskId))
return
case bosh.QUEUED:
err = c.waitUntilDone(taskId)
return
case bosh.PROCESSING:
err = c.waitUntilDone(taskId)
return
case bosh.DONE:
return
default:
err = bosh.TaskResultUnknown
return
}
}