Beispiel #1
0
func (s *server) initEndpoints(ctx types.Context) error {

	endpointsObj := s.config.Get(types.ConfigEndpoints)
	if endpointsObj == nil {
		if err := s.initDefaultEndpoint(); err != nil {
			return goof.WithError("no endpoints defined", err)
		}
		endpointsObj = s.config.Get(types.ConfigEndpoints)
	}

	endpoints, ok := endpointsObj.(map[string]interface{})
	if !ok {
		return goof.New("endpoints invalid type")
	}

	if len(endpoints) == 0 {
		if err := s.initDefaultEndpoint(); err != nil {
			return err
		}
	}

	for endpointName := range endpoints {

		endpoint := fmt.Sprintf("%s.%s", types.ConfigEndpoints, endpointName)
		address := fmt.Sprintf("%s.address", endpoint)
		laddr := s.config.GetString(address)
		if laddr == "" {
			return goof.WithField("endpoint", endpoint, "missing address")
		}

		laddrET := types.ParseEndpointType(laddr)
		switch laddrET {

		case types.TCPEndpoint:

			var tcpPort int
			func() {
				tcpPortLock.Lock()
				defer tcpPortLock.Unlock()
				tcpPort = gotil.RandomTCPPort()
			}()

			laddr = fmt.Sprintf("tcp://127.0.0.1:%d", tcpPort)
			s.ctx.WithField("endpoint", endpoint).Info(
				"initializing auto tcp endpoint")

		case types.UnixEndpoint:

			laddr = fmt.Sprintf("unix://%s", utils.GetTempSockFile())
			s.ctx.WithField("endpoint", endpoint).Info(
				"initializing auto unix endpoint")

		}

		s.ctx.WithFields(log.Fields{
			"endpoint": endpoint, "address": laddr}).Debug("endpoint info")

		s.addrs = append(s.addrs, laddr)

		proto, addr, err := gotil.ParseAddress(laddr)
		if err != nil {
			return err
		}

		logFields := map[string]interface{}{
			"endpoint": endpointName,
			"address":  laddr,
		}

		tlsConfig, err :=
			utils.ParseTLSConfig(s.config.Scope(endpoint), logFields, endpoint)
		if err != nil {
			return err
		}

		ctx.WithFields(logFields).Info("configured endpoint")

		srv, err := s.newHTTPServer(proto, addr, tlsConfig)
		if err != nil {
			return err
		}

		ctx.Info("server created")
		s.servers = append(s.servers, srv)
	}

	return nil
}
func (d *driver) Init(ctx types.Context, config gofig.Config) error {
	logFields := log.Fields{}

	addr := config.GetString(types.ConfigHost)
	d.ctx = ctx.WithValue(context.HostKey, addr)
	d.ctx.Debug("got configured host address")

	proto, lAddr, err := gotil.ParseAddress(addr)
	if err != nil {
		return err
	}

	tlsConfig, err := utils.ParseTLSConfig(
		config, logFields, "libstorage.client")
	if err != nil {
		return err
	}

	host := getHost(proto, lAddr, tlsConfig)
	lsxPath := config.GetString(types.ConfigExecutorPath)
	cliType := types.ParseClientType(config.GetString(types.ConfigClientType))
	disableKeepAlive := config.GetBool(types.ConfigHTTPDisableKeepAlive)

	logFields["host"] = host
	logFields["lsxPath"] = lsxPath
	logFields["clientType"] = cliType
	logFields["disableKeepAlive"] = disableKeepAlive

	httpTransport := &http.Transport{
		Dial: func(string, string) (net.Conn, error) {
			if tlsConfig == nil {
				return net.Dial(proto, lAddr)
			}
			return tls.Dial(proto, lAddr, tlsConfig)
		},
		DisableKeepAlives: disableKeepAlive,
	}

	apiClient := apiclient.New(host, httpTransport)
	logReq := config.GetBool(types.ConfigLogHTTPRequests)
	logRes := config.GetBool(types.ConfigLogHTTPResponses)
	apiClient.LogRequests(logReq)
	apiClient.LogResponses(logRes)

	logFields["enableInstanceIDHeaders"] = EnableInstanceIDHeaders
	logFields["enableLocalDevicesHeaders"] = EnableLocalDevicesHeaders
	logFields["logRequests"] = logReq
	logFields["logResponses"] = logRes

	d.client = client{
		APIClient:    apiClient,
		ctx:          ctx,
		config:       config,
		clientType:   cliType,
		serviceCache: &lss{Store: utils.NewStore()},
	}

	if d.clientType == types.IntegrationClient {

		newIIDCache := utils.NewStore
		dur, err := time.ParseDuration(
			config.GetString(types.ConfigClientCacheInstanceID))
		if err != nil {
			logFields["iidCacheDuration"] = dur.String()
			newIIDCache = func() types.Store {
				return utils.NewTTLStore(dur, true)
			}
		}

		d.lsxCache = &lss{Store: utils.NewStore()}
		d.supportedCache = utils.NewStore()
		d.instanceIDCache = &lss{Store: newIIDCache()}
	}

	d.ctx.WithFields(logFields).Info("created libStorage client")

	if err := d.dial(ctx); err != nil {
		return err
	}

	d.ctx.Info("successefully dialed libStorage server")
	return nil
}