github.com/coreos/mantle@v0.13.0/platform/machine/gcloud/machine.go (about) 1 // Copyright 2017 CoreOS, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package gcloud 16 17 import ( 18 "os" 19 "path/filepath" 20 21 "golang.org/x/crypto/ssh" 22 23 "github.com/coreos/mantle/platform" 24 ) 25 26 type machine struct { 27 gc *cluster 28 name string 29 intIP string 30 extIP string 31 dir string 32 journal *platform.Journal 33 console string 34 } 35 36 func (gm *machine) ID() string { 37 return gm.name 38 } 39 40 func (gm *machine) IP() string { 41 return gm.extIP 42 } 43 44 func (gm *machine) PrivateIP() string { 45 return gm.intIP 46 } 47 48 func (gm *machine) RuntimeConf() platform.RuntimeConfig { 49 return gm.gc.RuntimeConf() 50 } 51 52 func (gm *machine) SSHClient() (*ssh.Client, error) { 53 return gm.gc.SSHClient(gm.IP()) 54 } 55 56 func (gm *machine) PasswordSSHClient(user string, password string) (*ssh.Client, error) { 57 return gm.gc.PasswordSSHClient(gm.IP(), user, password) 58 } 59 60 func (gm *machine) SSH(cmd string) ([]byte, []byte, error) { 61 return gm.gc.SSH(gm, cmd) 62 } 63 64 func (gm *machine) Reboot() error { 65 return platform.RebootMachine(gm, gm.journal) 66 } 67 68 func (gm *machine) Destroy() { 69 if err := gm.saveConsole(); err != nil { 70 plog.Errorf("Error saving console for instance %v: %v", gm.ID(), err) 71 } 72 73 if err := gm.gc.flight.api.TerminateInstance(gm.name); err != nil { 74 plog.Errorf("Error terminating instance %v: %v", gm.ID(), err) 75 } 76 77 if gm.journal != nil { 78 gm.journal.Destroy() 79 } 80 81 gm.gc.DelMach(gm) 82 } 83 84 func (gm *machine) ConsoleOutput() string { 85 return gm.console 86 } 87 88 func (gm *machine) saveConsole() error { 89 var err error 90 gm.console, err = gm.gc.flight.api.GetConsoleOutput(gm.name) 91 if err != nil { 92 return err 93 } 94 95 path := filepath.Join(gm.dir, "console.txt") 96 f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644) 97 if err != nil { 98 return err 99 } 100 defer f.Close() 101 f.WriteString(gm.console) 102 103 return nil 104 } 105 106 func (gm *machine) JournalOutput() string { 107 if gm.journal == nil { 108 return "" 109 } 110 111 data, err := gm.journal.Read() 112 if err != nil { 113 plog.Errorf("Reading journal for instance %v: %v", gm.ID(), err) 114 } 115 return string(data) 116 }