github.com/coreos/mantle@v0.13.0/platform/machine/do/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 do 16 17 import ( 18 "context" 19 "strconv" 20 21 "github.com/digitalocean/godo" 22 "golang.org/x/crypto/ssh" 23 24 "github.com/coreos/mantle/platform" 25 ) 26 27 type machine struct { 28 cluster *cluster 29 droplet *godo.Droplet 30 journal *platform.Journal 31 publicIP string 32 privateIP string 33 } 34 35 func (dm *machine) ID() string { 36 return strconv.Itoa(dm.droplet.ID) 37 } 38 39 func (dm *machine) IP() string { 40 return dm.publicIP 41 } 42 43 func (dm *machine) PrivateIP() string { 44 return dm.privateIP 45 } 46 47 func (dm *machine) RuntimeConf() platform.RuntimeConfig { 48 return dm.cluster.RuntimeConf() 49 } 50 51 func (dm *machine) SSHClient() (*ssh.Client, error) { 52 return dm.cluster.SSHClient(dm.IP()) 53 } 54 55 func (dm *machine) PasswordSSHClient(user string, password string) (*ssh.Client, error) { 56 return dm.cluster.PasswordSSHClient(dm.IP(), user, password) 57 } 58 59 func (dm *machine) SSH(cmd string) ([]byte, []byte, error) { 60 return dm.cluster.SSH(dm, cmd) 61 } 62 63 func (dm *machine) Reboot() error { 64 return platform.RebootMachine(dm, dm.journal) 65 } 66 67 func (dm *machine) Destroy() { 68 if err := dm.cluster.flight.api.DeleteDroplet(context.TODO(), dm.droplet.ID); err != nil { 69 plog.Errorf("Error deleting droplet %v: %v", dm.droplet.ID, err) 70 } 71 72 if dm.journal != nil { 73 dm.journal.Destroy() 74 } 75 76 dm.cluster.DelMach(dm) 77 } 78 79 func (dm *machine) ConsoleOutput() string { 80 // DigitalOcean provides no API for retrieving ConsoleOutput 81 return "" 82 } 83 84 func (dm *machine) JournalOutput() string { 85 if dm.journal == nil { 86 return "" 87 } 88 89 data, err := dm.journal.Read() 90 if err != nil { 91 plog.Errorf("Reading journal for droplet %v: %v", dm.droplet.ID, err) 92 } 93 return string(data) 94 }