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

     1  // Copyright 2018 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 openstack
    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/openstack"
    26  )
    27  
    28  type machine struct {
    29  	cluster *cluster
    30  	mach    *openstack.Server
    31  	dir     string
    32  	journal *platform.Journal
    33  	console string
    34  }
    35  
    36  func (om *machine) ID() string {
    37  	return om.mach.Server.ID
    38  }
    39  
    40  func (om *machine) IP() string {
    41  	if om.mach.FloatingIP != nil {
    42  		return om.mach.FloatingIP.IP
    43  	} else {
    44  		return om.mach.Server.AccessIPv4
    45  	}
    46  }
    47  
    48  func (om *machine) PrivateIP() string {
    49  	for _, addrs := range om.mach.Server.Addresses {
    50  		addrs, ok := addrs.([]interface{})
    51  		if !ok {
    52  			continue
    53  		}
    54  		for _, addr := range addrs {
    55  			a, ok := addr.(map[string]interface{})
    56  			if !ok {
    57  				continue
    58  			}
    59  			iptype, ok := a["OS-EXT-IPS:type"].(string)
    60  			ip, ok2 := a["addr"].(string)
    61  			if ok && ok2 && iptype == "fixed" {
    62  				return ip
    63  			}
    64  		}
    65  	}
    66  	return om.IP()
    67  }
    68  
    69  func (om *machine) RuntimeConf() platform.RuntimeConfig {
    70  	return om.cluster.RuntimeConf()
    71  }
    72  
    73  func (om *machine) SSHClient() (*ssh.Client, error) {
    74  	return om.cluster.SSHClient(om.IP())
    75  }
    76  
    77  func (om *machine) PasswordSSHClient(user string, password string) (*ssh.Client, error) {
    78  	return om.cluster.PasswordSSHClient(om.IP(), user, password)
    79  }
    80  
    81  func (om *machine) SSH(cmd string) ([]byte, []byte, error) {
    82  	return om.cluster.SSH(om, cmd)
    83  }
    84  
    85  func (om *machine) Reboot() error {
    86  	return platform.RebootMachine(om, om.journal)
    87  }
    88  
    89  func (om *machine) Destroy() {
    90  	if err := om.saveConsole(); err != nil {
    91  		plog.Errorf("Error saving console for instance %v: %v", om.ID(), err)
    92  	}
    93  
    94  	if err := om.cluster.flight.api.DeleteServer(om.ID()); err != nil {
    95  		plog.Errorf("deleting server %v: %v", om.ID(), err)
    96  	}
    97  
    98  	if om.journal != nil {
    99  		om.journal.Destroy()
   100  	}
   101  
   102  	om.cluster.DelMach(om)
   103  }
   104  
   105  func (om *machine) ConsoleOutput() string {
   106  	return om.console
   107  }
   108  
   109  func (om *machine) saveConsole() error {
   110  	var err error
   111  	om.console, err = om.cluster.flight.api.GetConsoleOutput(om.ID())
   112  	if err != nil {
   113  		return fmt.Errorf("Error retrieving console log for %v: %v", om.ID(), err)
   114  	}
   115  
   116  	path := filepath.Join(om.dir, "console.txt")
   117  	f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644)
   118  	if err != nil {
   119  		return err
   120  	}
   121  	defer f.Close()
   122  	f.WriteString(om.console)
   123  
   124  	return nil
   125  }
   126  
   127  func (om *machine) JournalOutput() string {
   128  	if om.journal == nil {
   129  		return ""
   130  	}
   131  
   132  	data, err := om.journal.Read()
   133  	if err != nil {
   134  		plog.Errorf("Reading journal for instance %v: %v", om.ID(), err)
   135  	}
   136  	return string(data)
   137  }