github.com/coreos/mantle@v0.13.0/platform/machine/packet/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 packet
    16  
    17  import (
    18  	"strings"
    19  
    20  	"golang.org/x/crypto/ssh"
    21  
    22  	"github.com/coreos/mantle/platform"
    23  	"github.com/packethost/packngo"
    24  )
    25  
    26  type machine struct {
    27  	cluster   *cluster
    28  	device    *packngo.Device
    29  	journal   *platform.Journal
    30  	console   *console
    31  	publicIP  string
    32  	privateIP string
    33  }
    34  
    35  func (pm *machine) ID() string {
    36  	return pm.device.ID
    37  }
    38  
    39  func (pm *machine) IP() string {
    40  	return pm.publicIP
    41  }
    42  
    43  func (pm *machine) PrivateIP() string {
    44  	return pm.privateIP
    45  }
    46  
    47  func (pm *machine) RuntimeConf() platform.RuntimeConfig {
    48  	return pm.cluster.RuntimeConf()
    49  }
    50  
    51  func (pm *machine) SSHClient() (*ssh.Client, error) {
    52  	return pm.cluster.SSHClient(pm.IP())
    53  }
    54  
    55  func (pm *machine) PasswordSSHClient(user string, password string) (*ssh.Client, error) {
    56  	return pm.cluster.PasswordSSHClient(pm.IP(), user, password)
    57  }
    58  
    59  func (pm *machine) SSH(cmd string) ([]byte, []byte, error) {
    60  	return pm.cluster.SSH(pm, cmd)
    61  }
    62  
    63  func (pm *machine) Reboot() error {
    64  	return platform.RebootMachine(pm, pm.journal)
    65  }
    66  
    67  func (pm *machine) Destroy() {
    68  	if err := pm.cluster.flight.api.DeleteDevice(pm.ID()); err != nil {
    69  		plog.Errorf("Error terminating device %v: %v", pm.ID(), err)
    70  	}
    71  
    72  	if pm.journal != nil {
    73  		pm.journal.Destroy()
    74  	}
    75  
    76  	pm.cluster.DelMach(pm)
    77  }
    78  
    79  func (pm *machine) ConsoleOutput() string {
    80  	if pm.console == nil {
    81  		return ""
    82  	}
    83  	output := pm.console.Output()
    84  	// The provisioning OS boots through iPXE and the real OS boots
    85  	// through GRUB.  Try to ignore console logs from provisioning, but
    86  	// it's better to return everything than nothing.
    87  	grub := strings.Index(output, "GNU GRUB")
    88  	if grub == -1 {
    89  		plog.Warningf("Couldn't find GRUB banner in console output of %s", pm.ID())
    90  		return output
    91  	}
    92  	linux := strings.Index(output[grub:], "Linux version")
    93  	if linux == -1 {
    94  		plog.Warningf("Couldn't find Linux banner in console output of %s", pm.ID())
    95  		return output
    96  	}
    97  	return output[grub+linux:]
    98  }
    99  
   100  func (pm *machine) JournalOutput() string {
   101  	if pm.journal == nil {
   102  		return ""
   103  	}
   104  
   105  	data, err := pm.journal.Read()
   106  	if err != nil {
   107  		plog.Errorf("Reading journal for device %v: %v", pm.ID(), err)
   108  	}
   109  	return string(data)
   110  }