forked from purpleidea/mgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patterns.go
135 lines (118 loc) · 4.5 KB
/
patterns.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Mgmt
// Copyright (C) 2013-2018+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package integration
import (
"context"
"time"
errwrap "github.com/pkg/errors"
)
// SimpleDeployLang is a helper method that takes a struct and runs a sequence
// of methods on it. This particular helper starts up an instance, deploys some
// code, and then shuts down. Both after initially starting up, and after
// deploy, it waits for the instance to converge before running the next step.
func (obj *Instance) SimpleDeployLang(code string) error {
if err := obj.Init(); err != nil {
return errwrap.Wrapf(err, "could not init instance")
}
defer obj.Close() // clean up working directories
// run the program
if err := obj.Run(nil); err != nil {
return errwrap.Wrapf(err, "mgmt could not start")
}
defer obj.Kill() // do a kill -9
// wait for an internal converge signal as a baseline
{
ctx, cancel := context.WithTimeout(context.Background(), longTimeout*time.Second)
defer cancel()
if err := obj.Wait(ctx); err != nil { // wait to get a converged signal
return errwrap.Wrapf(err, "mgmt wait failed") // timeout expired
}
}
// push a deploy
if err := obj.DeployLang(code); err != nil {
return errwrap.Wrapf(err, "mgmt could not deploy")
}
// wait for an internal converge signal
{
ctx, cancel := context.WithTimeout(context.Background(), longTimeout*time.Second)
defer cancel()
if err := obj.Wait(ctx); err != nil { // wait to get a converged signal
return errwrap.Wrapf(err, "mgmt wait failed") // timeout expired
}
}
// press ^C
{
ctx, cancel := context.WithTimeout(context.Background(), longTimeout*time.Second)
defer cancel()
if err := obj.Quit(ctx); err != nil {
if err == context.DeadlineExceeded {
return errwrap.Wrapf(err, "mgmt blocked on exit")
}
return errwrap.Wrapf(err, "mgmt exited with error")
}
}
return nil
}
// SimpleDeployLang is a helper method that takes a struct representing a
// cluster and runs a sequence of methods on it. This particular helper starts
// up a series of instances linearly, deploys some code, and then shuts down.
// Both after initially starting up, after peering each instance, and after
// deploy, it waits for the instance to converge before running the next step.
func (obj *Cluster) SimpleDeployLang(code string) error {
if err := obj.Init(); err != nil {
return errwrap.Wrapf(err, "could not init instance")
}
defer obj.Close() // clean up working directories
// start the cluster
if err := obj.RunLinear(); err != nil {
return errwrap.Wrapf(err, "mgmt could not start")
}
defer obj.Kill() // do a kill -9
// wait for an internal converge signal as a baseline
// FIXME: add this wait if we remove it from RunLinear
//{
// ctx, cancel := context.WithTimeout(context.Background(), time.Duration(longTimeout*len(obj.Hostnames))*time.Second)
// defer cancel()
// if err := obj.Wait(ctx); err != nil { // wait to get a converged signal
// return errwrap.Wrapf(err, "mgmt initial wait failed") // timeout expired
// }
//}
// push a deploy
if err := obj.DeployLang(code); err != nil {
return errwrap.Wrapf(err, "mgmt could not deploy")
}
// wait for an internal converge signal
{
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(longTimeout*len(obj.Hostnames))*time.Second)
defer cancel()
if err := obj.Wait(ctx); err != nil { // wait to get a converged signal
return errwrap.Wrapf(err, "mgmt post-deploy wait failed") // timeout expired
}
}
// press ^C
{
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(longTimeout*len(obj.Hostnames))*time.Second)
defer cancel()
if err := obj.Quit(ctx); err != nil {
if err == context.DeadlineExceeded {
return errwrap.Wrapf(err, "mgmt blocked on exit")
}
return errwrap.Wrapf(err, "mgmt exited with error")
}
}
return nil
}