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