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