github.com/coreos/mantle@v0.13.0/platform/machine/esx/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 esx
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  
    22  	"golang.org/x/crypto/ssh"
    23  
    24  	"github.com/coreos/mantle/platform"
    25  	"github.com/coreos/mantle/platform/api/esx"
    26  )
    27  
    28  type machine struct {
    29  	cluster *cluster
    30  	mach    *esx.ESXMachine
    31  	dir     string
    32  	journal *platform.Journal
    33  	console string
    34  }
    35  
    36  func (em *machine) ID() string {
    37  	return em.mach.Name
    38  }
    39  
    40  func (em *machine) IP() string {
    41  	return em.mach.IPAddress
    42  }
    43  
    44  func (em *machine) PrivateIP() string {
    45  	return em.mach.IPAddress
    46  }
    47  
    48  func (em *machine) RuntimeConf() platform.RuntimeConfig {
    49  	return em.cluster.RuntimeConf()
    50  }
    51  
    52  func (em *machine) SSHClient() (*ssh.Client, error) {
    53  	return em.cluster.SSHClient(em.IP())
    54  }
    55  
    56  func (em *machine) PasswordSSHClient(user string, password string) (*ssh.Client, error) {
    57  	return em.cluster.PasswordSSHClient(em.IP(), user, password)
    58  }
    59  
    60  func (em *machine) SSH(cmd string) ([]byte, []byte, error) {
    61  	return em.cluster.SSH(em, cmd)
    62  }
    63  
    64  func (em *machine) Reboot() error {
    65  	return platform.RebootMachine(em, em.journal)
    66  }
    67  
    68  func (em *machine) Destroy() {
    69  	if err := em.cluster.flight.api.TerminateDevice(em.ID()); err != nil {
    70  		plog.Errorf("Error terminating device %v: %v", em.ID(), err)
    71  	}
    72  
    73  	if em.journal != nil {
    74  		em.journal.Destroy()
    75  	}
    76  
    77  	if err := em.saveConsole(); err != nil {
    78  		plog.Errorf("Error saving console for device %v: %v", em.ID(), err)
    79  	}
    80  
    81  	if err := em.cluster.flight.api.CleanupDevice(em.ID()); err != nil {
    82  		plog.Errorf("Error cleaning up device for device %v: %v", em.ID(), err)
    83  	}
    84  
    85  	em.cluster.DelMach(em)
    86  }
    87  
    88  func (em *machine) ConsoleOutput() string {
    89  	return em.console
    90  }
    91  
    92  func (em *machine) saveConsole() error {
    93  	var err error
    94  	em.console, err = em.cluster.flight.api.GetConsoleOutput(em.ID())
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	path := filepath.Join(em.dir, "console.txt")
   100  	f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
   101  	if err != nil {
   102  		return err
   103  	}
   104  	defer f.Close()
   105  	_, err = f.WriteString(em.console)
   106  	if err != nil {
   107  		return fmt.Errorf("failed writing console to file: %v", err)
   108  	}
   109  
   110  	return nil
   111  }
   112  
   113  func (em *machine) JournalOutput() string {
   114  	if em.journal == nil {
   115  		return ""
   116  	}
   117  
   118  	data, err := em.journal.Read()
   119  	if err != nil {
   120  		plog.Errorf("Reading journal for device %v: %v", em.ID(), err)
   121  	}
   122  	return string(data)
   123  }