github.com/coreos/mantle@v0.13.0/platform/machine/qemu/machine.go (about)

     1  // Copyright 2016 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 qemu
    16  
    17  import (
    18  	"io/ioutil"
    19  
    20  	"golang.org/x/crypto/ssh"
    21  
    22  	"github.com/coreos/mantle/platform"
    23  	"github.com/coreos/mantle/platform/local"
    24  	"github.com/coreos/mantle/system/exec"
    25  )
    26  
    27  type machine struct {
    28  	qc          *Cluster
    29  	id          string
    30  	qemu        exec.Cmd
    31  	netif       *local.Interface
    32  	journal     *platform.Journal
    33  	consolePath string
    34  	console     string
    35  }
    36  
    37  func (m *machine) ID() string {
    38  	return m.id
    39  }
    40  
    41  func (m *machine) IP() string {
    42  	return m.netif.DHCPv4[0].IP.String()
    43  }
    44  
    45  func (m *machine) PrivateIP() string {
    46  	return m.netif.DHCPv4[0].IP.String()
    47  }
    48  
    49  func (m *machine) RuntimeConf() platform.RuntimeConfig {
    50  	return m.qc.RuntimeConf()
    51  }
    52  
    53  func (m *machine) SSHClient() (*ssh.Client, error) {
    54  	return m.qc.SSHClient(m.IP())
    55  }
    56  
    57  func (m *machine) PasswordSSHClient(user string, password string) (*ssh.Client, error) {
    58  	return m.qc.PasswordSSHClient(m.IP(), user, password)
    59  }
    60  
    61  func (m *machine) SSH(cmd string) ([]byte, []byte, error) {
    62  	return m.qc.SSH(m, cmd)
    63  }
    64  
    65  func (m *machine) Reboot() error {
    66  	return platform.RebootMachine(m, m.journal)
    67  }
    68  
    69  func (m *machine) Destroy() {
    70  	if err := m.qemu.Kill(); err != nil {
    71  		plog.Errorf("Error killing instance %v: %v", m.ID(), err)
    72  	}
    73  
    74  	m.journal.Destroy()
    75  
    76  	if buf, err := ioutil.ReadFile(m.consolePath); err == nil {
    77  		m.console = string(buf)
    78  	} else {
    79  		plog.Errorf("Error reading console for instance %v: %v", m.ID(), err)
    80  	}
    81  
    82  	m.qc.DelMach(m)
    83  }
    84  
    85  func (m *machine) ConsoleOutput() string {
    86  	return m.console
    87  }
    88  
    89  func (m *machine) JournalOutput() string {
    90  	if m.journal == nil {
    91  		return ""
    92  	}
    93  
    94  	data, err := m.journal.Read()
    95  	if err != nil {
    96  		plog.Errorf("Reading journal for instance %v: %v", m.ID(), err)
    97  	}
    98  	return string(data)
    99  }