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

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