-
Notifications
You must be signed in to change notification settings - Fork 12
/
machine.go
147 lines (122 loc) · 3.63 KB
/
machine.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
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aws
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/aws/aws-sdk-go/service/ec2"
"golang.org/x/crypto/ssh"
"github.com/coreos/mantle/platform"
"github.com/coreos/mantle/util"
)
type machine struct {
cluster *cluster
mach *ec2.Instance
dir string
journal *platform.Journal
console string
}
func (am *machine) ID() string {
return *am.mach.InstanceId
}
func (am *machine) IP() string {
return *am.mach.PublicIpAddress
}
func (am *machine) PrivateIP() string {
return *am.mach.PrivateIpAddress
}
func (am *machine) RuntimeConf() platform.RuntimeConfig {
return am.cluster.RuntimeConf()
}
func (am *machine) SSHClient() (*ssh.Client, error) {
return am.cluster.SSHClient(am.IP())
}
func (am *machine) PasswordSSHClient(user string, password string) (*ssh.Client, error) {
return am.cluster.PasswordSSHClient(am.IP(), user, password)
}
func (am *machine) SSH(cmd string) ([]byte, []byte, error) {
return am.cluster.SSH(am, cmd)
}
func (am *machine) Reboot() error {
return platform.RebootMachine(am, am.journal)
}
func (am *machine) Destroy() {
origConsole, err := am.cluster.api.GetConsoleOutput(am.ID())
if err != nil {
plog.Warningf("Error retrieving console log for %v: %v", am.ID(), err)
}
if err := am.cluster.api.TerminateInstances([]string{am.ID()}); err != nil {
plog.Errorf("Error terminating instance %v: %v", am.ID(), err)
}
if am.journal != nil {
am.journal.Destroy()
}
if err := am.saveConsole(origConsole); err != nil {
plog.Errorf("Error saving console for instance %v: %v", am.ID(), err)
}
am.cluster.DelMach(am)
}
func (am *machine) ConsoleOutput() string {
return am.console
}
func (am *machine) saveConsole(origConsole string) error {
// If the instance has e.g. been running for several minutes, the
// returned output will be non-empty but won't necessarily include
// the most recent log messages. So we loop until the post-termination
// logs are different from the pre-termination logs.
err := util.WaitUntilReady(5*time.Minute, 5*time.Second, func() (bool, error) {
var err error
am.console, err = am.cluster.api.GetConsoleOutput(am.ID())
if err != nil {
return false, err
}
if am.console == origConsole {
plog.Debugf("waiting for console for %v", am.ID())
return false, nil
}
return true, nil
})
if err != nil {
err = fmt.Errorf("retrieving console output of %v: %v", am.ID(), err)
if origConsole != "" {
plog.Warning(err)
} else {
return err
}
}
// merge the two logs
overlapLen := 100
if len(am.console) < overlapLen {
overlapLen = len(am.console)
}
origIdx := strings.LastIndex(origConsole, am.console[0:overlapLen])
if origIdx != -1 {
// overlap
am.console = origConsole[0:origIdx] + am.console
} else if origConsole != "" {
// two logs with no overlap; add scissors
am.console = origConsole + "\n\n8<------------------------\n\n" + am.console
}
path := filepath.Join(am.dir, "console.txt")
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()
f.WriteString(am.console)
return nil
}