github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/providers/cs/machine.go (about)

     1  package cs
     2  
     3  import (
     4  	"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
     5  	"github.com/caos/orbos/internal/operator/orbiter/kinds/providers/ssh"
     6  	"github.com/cloudscale-ch/cloudscale-go-sdk"
     7  )
     8  
     9  var _ infra.Machine = (*machine)(nil)
    10  
    11  type action struct {
    12  	required  bool
    13  	require   func()
    14  	unrequire func()
    15  }
    16  
    17  type machine struct {
    18  	server *cloudscale.Server
    19  	*ssh.Machine
    20  	remove       func() error
    21  	context      *context
    22  	reboot       *action
    23  	replacement  *action
    24  	pool         *Pool
    25  	poolName     string
    26  	X_ID         string `header:"id"`
    27  	X_internalIP string `header:"internal ip"`
    28  	X_externalIP string `header:"external ip"`
    29  }
    30  
    31  func newMachine(server *cloudscale.Server, internalIP, externalIP string, sshMachine *ssh.Machine, remove func() error, context *context, pool *Pool, poolName string) *machine {
    32  	return &machine{
    33  		server:       server,
    34  		X_ID:         server.Name,
    35  		X_internalIP: internalIP,
    36  		X_externalIP: externalIP,
    37  		Machine:      sshMachine,
    38  		remove:       remove,
    39  		context:      context,
    40  		pool:         pool,
    41  		poolName:     poolName,
    42  	}
    43  }
    44  
    45  func (m *machine) ID() string                     { return m.X_ID }
    46  func (m *machine) IP() string                     { return m.X_internalIP }
    47  func (m *machine) Destroy() (func() error, error) { return m.remove, nil }
    48  
    49  func (m *machine) RebootRequired() (required bool, require func(), unrequire func()) {
    50  
    51  	m.reboot = m.initAction(
    52  		m.reboot,
    53  		func() []string { return m.context.desired.RebootRequired },
    54  		func(machines []string) { m.context.desired.RebootRequired = machines })
    55  
    56  	return m.reboot.required, m.reboot.require, m.reboot.unrequire
    57  }
    58  
    59  func (m *machine) ReplacementRequired() (required bool, require func(), unrequire func()) {
    60  
    61  	m.replacement = m.initAction(
    62  		m.replacement,
    63  		func() []string { return m.context.desired.ReplacementRequired },
    64  		func(machines []string) { m.context.desired.ReplacementRequired = machines })
    65  
    66  	return m.replacement.required, m.replacement.require, m.replacement.unrequire
    67  }
    68  
    69  func (m *machine) initAction(a *action, getSlice func() []string, setSlice func([]string)) *action {
    70  	if a != nil {
    71  		return a
    72  	}
    73  
    74  	newAction := &action{
    75  		required:  false,
    76  		unrequire: func() {},
    77  		require: func() {
    78  			s := getSlice()
    79  			s = append(s, m.server.Name)
    80  			setSlice(s)
    81  		},
    82  	}
    83  
    84  	s := getSlice()
    85  	for sIdx := range s {
    86  		req := s[sIdx]
    87  		if req == m.ID() {
    88  			newAction.required = true
    89  			break
    90  		}
    91  	}
    92  
    93  	if newAction.required {
    94  		newAction.unrequire = func() {
    95  			s := getSlice()
    96  			for sIdx := range s {
    97  				req := s[sIdx]
    98  				if req == m.ID() {
    99  					s = append(s[0:sIdx], s[sIdx+1:]...)
   100  				}
   101  			}
   102  			setSlice(s)
   103  		}
   104  	}
   105  
   106  	return newAction
   107  }