Esempio n. 1
0
// Init initializes the driver.
func (d *driver) Init(ctx types.Context, config gofig.Config) error {
	d.config = config

	fields := map[string]interface{}{
		"provider":        vbox.Name,
		"moduleName":      vbox.Name,
		"endpoint":        d.endpoint(),
		"userName":        d.username(),
		"tls":             d.tls(),
		"volumePath":      d.volumePath(),
		"controllerName":  d.controllerName(),
		"machineNameOrId": d.machineNameID(""),
	}

	ctx.Info("initializing driver: ", fields)
	d.vbox = vboxc.New(d.username(), d.password(),
		d.endpoint(), d.tls(), d.controllerName())

	if err := d.vbox.Logon(); err != nil {
		return goof.WithFieldsE(fields,
			"error logging in", err)
	}

	ctx.WithFields(fields).Info("storage driver initialized")
	return nil
}
Esempio n. 2
0
func (d *idm) initPathCache(ctx types.Context) {
	if !d.pathCacheEnabled() {
		ctx.Info("path cache initializion disabled")
		return
	}

	if name, ok := context.ServiceName(ctx); !ok || name == "" {
		ctx.Info("path cache initializion disabled; no service name in ctx")
		return
	}

	f := func(async bool) {
		ctx.WithField("async", async).Info("initializing the path cache")
		_, err := d.List(ctx, apiutils.NewStoreWithData(initPathCacheMap))
		if err != nil {
			ctx.WithField("async", async).WithError(err).Error(
				"error initializing the path cache")
		} else {
			ctx.WithField("async", async).Debug("initialized the path cache")
		}
	}

	if d.pathCacheAsync() {
		go f(true)
	} else {
		f(false)
	}
}
Esempio n. 3
0
// Start starts the daemon.
func Start(
	ctx apitypes.Context,
	config gofig.Config,
	host string,
	stop <-chan os.Signal) (<-chan error, error) {

	var (
		err           error
		errs          = make(chan error)
		daemonErrChan <-chan error
	)

	if daemonErrChan, err = start(ctx, config, host, stop); err != nil {
		ctx.WithError(err).Error("daemon failed to initialize")
		return nil, err
	}

	ctx.Info("service successfully initialized, waiting on stop signal")

	go func() {
		sig := <-stop
		ctx.WithField("signal", sig).Info("service received stop signal")
		util.WaitUntilLibStorageStopped(ctx, daemonErrChan)
		close(errs)
	}()

	return errs, nil
}
Esempio n. 4
0
func (s *server) createMux(ctx types.Context) *mux.Router {
	m := mux.NewRouter()
	for _, apiRouter := range s.routers {
		for _, r := range apiRouter.Routes() {

			ctx := ctx.WithValue(context.RouteKey, r)

			f := s.makeHTTPHandler(ctx, r)
			mr := m.Path(r.GetPath())
			mr = mr.Name(r.GetName())
			mr = mr.Methods(r.GetMethod())
			mr = mr.Queries(r.GetQueries()...)
			mr.Handler(f)

			if l, ok := context.GetLogLevel(ctx); ok && l >= log.DebugLevel {
				ctx.WithFields(log.Fields{
					"path":         r.GetPath(),
					"method":       r.GetMethod(),
					"queries":      r.GetQueries(),
					"len(queries)": len(r.GetQueries()),
				}).Debug("registered route")
			} else {
				ctx.Info("registered route")
			}
		}
	}
	return m
}
Esempio n. 5
0
func (s *server) makeHTTPHandler(
	ctx types.Context,
	route types.Route) http.HandlerFunc {

	return func(w http.ResponseWriter, req *http.Request) {

		w.Header().Set(types.ServerNameHeader, s.name)

		ctx := context.WithRequestRoute(ctx, req, route)

		if req.TLS != nil {
			if len(req.TLS.PeerCertificates) > 0 {
				userName := req.TLS.PeerCertificates[0].Subject.CommonName
				ctx = ctx.WithValue(context.UserKey, userName)
			}
		}

		ctx.Info("http request")

		vars := mux.Vars(req)
		if vars == nil {
			vars = map[string]string{}
		}
		store := utils.NewStoreWithVars(vars)

		handlerFunc := s.handleWithMiddleware(ctx, route)
		if err := handlerFunc(ctx, w, req, store); err != nil {
			ctx.Error(err)
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
	}
}
Esempio n. 6
0
// VolumeDetach detaches a volume.
func (d *driver) VolumeDetach(
	ctx types.Context,
	volumeID string,
	opts *types.VolumeDetachOpts) (*types.Volume, error) {
	// review volume with attachments to any host
	ec2vols, err := d.getVolume(ctx, volumeID, "")
	if err != nil {
		return nil, goof.WithError("error getting volume", err)
	}
	volumes, convErr := d.toTypesVolume(
		ctx, ec2vols, types.VolAttReqTrue)
	if convErr != nil {
		return nil, goof.WithError("error converting to types.Volume", convErr)
	}

	// no volumes to detach
	if len(volumes) == 0 {
		return nil, errNoVolReturned
	}

	// volume has no attachments
	if len(volumes[0].Attachments) == 0 {
		return nil, errVolAlreadyDetached
	}

	dvInput := &awsec2.DetachVolumeInput{
		VolumeId: &volumeID,
		Force:    &opts.Force,
	}

	// Detach volume using EC2 API call
	if _, err = mustSession(ctx).DetachVolume(dvInput); err != nil {
		return nil, goof.WithFieldsE(
			log.Fields{
				"provider": d.Name(),
				"volumeID": volumeID}, "error detaching volume", err)
	}

	if err = d.waitVolumeComplete(ctx, volumeID, waitVolumeDetach); err != nil {
		return nil, goof.WithError("error waiting for volume detach", err)
	}

	ctx.Info("detached volume", volumeID)

	// check if successful detach
	detachedVol, err := d.VolumeInspect(
		ctx, volumeID, &types.VolumeInspectOpts{
			Attachments: types.VolAttReqTrue,
			Opts:        opts.Opts,
		})
	if err != nil {
		return nil, goof.WithError("error getting volume", err)
	}

	return detachedVol, nil
}
Esempio n. 7
0
// VolumeDetach detaches a volume.
func (d *driver) VolumeDetach(
	ctx types.Context,
	volumeID string,
	opts *types.VolumeDetachOpts) (*types.Volume, error) {

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

	if volumeID == "" {
		return nil, goof.New("missing volume id")
	}

	volumes, err := d.getVolume(ctx, volumeID, "", types.VolAttFalse)
	if err != nil {
		return nil, err
	}

	if len(volumes) == 0 {
		return nil, goof.New("no volume returned")
	}

	// TODO: Check if volumes[[0].Attachments > 0?

	if err := d.detachVolume(ctx, volumeID, ""); err != nil {
		return nil, goof.WithFieldsE(
			log.Fields{
				"provier":  vbox.Name,
				"volumeID": volumeID}, "error detaching volume", err)
	}

	ctx.Info("detached volume", volumeID)
	return d.VolumeInspect(
		ctx, volumeID, &types.VolumeInspectOpts{
			Attachments: types.VolAttReqTrue})
}
Esempio n. 8
0
func (c *client) dial(ctx types.Context) error {

	ctx.WithField("path", lsxMutex).Info("lsx lock file path")

	svcInfos, err := c.Services(ctx)
	if err != nil {
		return err
	}

	// controller clients do not have any additional dialer logic
	if c.isController() {
		return nil
	}

	store := utils.NewStore()
	c.ctx = c.ctx.WithValue(context.ServerKey, c.ServerName())

	if !c.config.GetBool(types.ConfigExecutorNoDownload) {

		ctx.Info("initializing executors cache")
		if _, err := c.Executors(ctx); err != nil {
			return err
		}

		if err := c.updateExecutor(ctx); err != nil {
			return err
		}
	}

	for service, _ := range svcInfos {
		ctx := c.ctx.WithValue(context.ServiceKey, service)
		ctx.Info("initializing supported cache")
		supported, err := c.Supported(ctx, store)
		if err != nil {
			return goof.WithError("error initializing supported cache", err)
		}

		if !supported {
			ctx.Warn("executor not supported")
			continue
		}

		ctx.Info("initializing instance ID cache")
		if _, err := c.InstanceID(ctx, store); err != nil {
			if err == types.ErrNotImplemented {
				ctx.WithError(err).Warn("cannot get instance ID")
				continue
			}
			return goof.WithError("error initializing instance ID cache", err)
		}
	}

	return nil
}
Esempio n. 9
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
}