github.com/didip/deis@v1.4.1/deisctl/backend/fleet/registry.go (about)

     1  package fleet
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"net/http"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/coreos/fleet/client"
    11  	"github.com/coreos/fleet/etcd"
    12  	"github.com/coreos/fleet/machine"
    13  	"github.com/coreos/fleet/pkg"
    14  	"github.com/coreos/fleet/registry"
    15  	"github.com/coreos/fleet/ssh"
    16  )
    17  
    18  // Flags used for Fleet API connectivity
    19  var Flags = struct {
    20  	Debug                 bool
    21  	Version               bool
    22  	Endpoint              string
    23  	EtcdKeyPrefix         string
    24  	EtcdKeyFile           string
    25  	EtcdCertFile          string
    26  	EtcdCAFile            string
    27  	UseAPI                bool
    28  	KnownHostsFile        string
    29  	StrictHostKeyChecking bool
    30  	Tunnel                string
    31  	RequestTimeout        float64
    32  	SSHTimeout            float64
    33  }{}
    34  
    35  const (
    36  	oldVersionWarning = `####################################################################
    37  WARNING: fleetctl (%s) is older than the latest registered
    38  version of fleet found in the cluster (%s). You are strongly
    39  recommended to upgrade fleetctl to prevent incompatibility issues.
    40  ####################################################################
    41  `
    42  )
    43  
    44  // global API client used by commands
    45  var cAPI client.API
    46  
    47  // used to cache MachineStates
    48  var machineStates map[string]*machine.MachineState
    49  var requestTimeout = time.Duration(10) * time.Second
    50  
    51  func getTunnelFlag() string {
    52  	tun := Flags.Tunnel
    53  	if tun != "" && !strings.Contains(tun, ":") {
    54  		tun += ":22"
    55  	}
    56  	return tun
    57  }
    58  
    59  func getChecker() *ssh.HostKeyChecker {
    60  	if !Flags.StrictHostKeyChecking {
    61  		return nil
    62  	}
    63  	keyFile := ssh.NewHostKeyFile(Flags.KnownHostsFile)
    64  	return ssh.NewHostKeyChecker(keyFile)
    65  }
    66  
    67  func getFakeClient() (*registry.FakeRegistry, error) {
    68  	return registry.NewFakeRegistry(), nil
    69  }
    70  
    71  func getRegistryClient() (client.API, error) {
    72  	var dial func(string, string) (net.Conn, error)
    73  	sshTimeout := time.Duration(Flags.SSHTimeout*1000) * time.Millisecond
    74  	tun := getTunnelFlag()
    75  	if tun != "" {
    76  		sshClient, err := ssh.NewSSHClient("core", tun, getChecker(), false, sshTimeout)
    77  		if err != nil {
    78  			return nil, fmt.Errorf("failed initializing SSH client: %v", err)
    79  		}
    80  
    81  		dial = func(network, addr string) (net.Conn, error) {
    82  			tcpaddr, err := net.ResolveTCPAddr(network, addr)
    83  			if err != nil {
    84  				return nil, err
    85  			}
    86  			return sshClient.DialTCP(network, nil, tcpaddr)
    87  		}
    88  	}
    89  
    90  	tlsConfig, err := pkg.ReadTLSConfigFiles(Flags.EtcdCAFile, Flags.EtcdCertFile, Flags.EtcdKeyFile)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	trans := &http.Transport{
    96  		Dial:            dial,
    97  		TLSClientConfig: tlsConfig,
    98  	}
    99  
   100  	timeout := time.Duration(Flags.RequestTimeout*1000) * time.Millisecond
   101  	machines := []string{Flags.Endpoint}
   102  	eClient, err := etcd.NewClient(machines, trans, timeout)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	reg := registry.NewEtcdRegistry(eClient, Flags.EtcdKeyPrefix)
   108  
   109  	// if msg, ok := checkVersion(reg); !ok {
   110  	// 	stderr(msg)
   111  	// }
   112  
   113  	return &client.RegistryClient{Registry: reg}, nil
   114  }
   115  
   116  // checkVersion makes a best-effort attempt to verify that fleetctl is at least as new as the
   117  // latest fleet version found registered in the cluster. If any errors are encountered or fleetctl
   118  // is >= the latest version found, it returns true. If it is < the latest found version, it returns
   119  // false and a scary warning to the user.
   120  // func checkVersion(reg registry.Registry) (string, bool) {
   121  // 	fv := version.SemVersion
   122  // 	lv, err := reg.LatestVersion()
   123  // 	if err != nil {
   124  // 		fmt.Printf("error attempting to check latest fleet version in Registry: %v", err)
   125  // 	} else if lv != nil && fv.LessThan(*lv) {
   126  // 		return fmt.Sprintf(oldVersionWarning, fv.String(), lv.String()), false
   127  // 	}
   128  // 	return "", true
   129  // }