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

     1  // Copyright 2015 CoreOS, Inc.
     2  // Copyright 2018 Red Hat
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package platform
    17  
    18  import (
    19  	"fmt"
    20  	"sync"
    21  
    22  	"github.com/pborman/uuid"
    23  	"golang.org/x/crypto/ssh/agent"
    24  
    25  	"github.com/coreos/mantle/network"
    26  )
    27  
    28  type BaseFlight struct {
    29  	clusterlock sync.Mutex
    30  	clustermap  map[string]Cluster
    31  
    32  	name       string
    33  	platform   Name
    34  	ctPlatform string
    35  	baseopts   *Options
    36  
    37  	agent *network.SSHAgent
    38  }
    39  
    40  func NewBaseFlight(opts *Options, platform Name, ctPlatform string) (*BaseFlight, error) {
    41  	return NewBaseFlightWithDialer(opts, platform, ctPlatform, network.NewRetryDialer())
    42  }
    43  
    44  func NewBaseFlightWithDialer(opts *Options, platform Name, ctPlatform string, dialer network.Dialer) (*BaseFlight, error) {
    45  	agent, err := network.NewSSHAgent(dialer)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	bf := &BaseFlight{
    51  		clustermap: make(map[string]Cluster),
    52  		name:       fmt.Sprintf("%s-%s", opts.BaseName, uuid.New()),
    53  		platform:   platform,
    54  		ctPlatform: ctPlatform,
    55  		baseopts:   opts,
    56  		agent:      agent,
    57  	}
    58  
    59  	return bf, nil
    60  }
    61  
    62  func (bf *BaseFlight) Name() string {
    63  	return bf.name
    64  }
    65  
    66  func (bf *BaseFlight) Platform() Name {
    67  	return bf.platform
    68  }
    69  
    70  func (bf *BaseFlight) Clusters() []Cluster {
    71  	bf.clusterlock.Lock()
    72  	defer bf.clusterlock.Unlock()
    73  	clusters := make([]Cluster, 0, len(bf.clustermap))
    74  	for _, m := range bf.clustermap {
    75  		clusters = append(clusters, m)
    76  	}
    77  	return clusters
    78  }
    79  
    80  func (bf *BaseFlight) AddCluster(c Cluster) {
    81  	bf.clusterlock.Lock()
    82  	defer bf.clusterlock.Unlock()
    83  	bf.clustermap[c.Name()] = c
    84  }
    85  
    86  func (bf *BaseFlight) DelCluster(c Cluster) {
    87  	bf.clusterlock.Lock()
    88  	defer bf.clusterlock.Unlock()
    89  	delete(bf.clustermap, c.Name())
    90  }
    91  
    92  func (bf *BaseFlight) Keys() ([]*agent.Key, error) {
    93  	return bf.agent.List()
    94  }
    95  
    96  // Destroy destroys each Cluster in the Flight and closes the SSH agent.
    97  func (bf *BaseFlight) Destroy() {
    98  	for _, c := range bf.Clusters() {
    99  		c.Destroy()
   100  	}
   101  
   102  	if err := bf.agent.Close(); err != nil {
   103  		plog.Errorf("Error closing agent: %v", err)
   104  	}
   105  }