Esempio n. 1
0
func (s *scheduler) AddUpdateResourcePool(pool *pool.ResourcePool) error {
	if p, err := s.facade.GetResourcePool(datastore.Get(), pool.ID); err != nil {
		return err
	} else if p == nil {
		return s.facade.AddResourcePool(datastore.Get(), pool)
	}

	return s.facade.UpdateResourcePool(datastore.Get(), pool)
}
Esempio n. 2
0
func (s *scheduler) AddUpdateHost(host *host.Host) error {
	if h, err := s.facade.GetHost(datastore.Get(), host.ID); err != nil {
		return err
	} else if h == nil {
		return s.facade.AddHost(datastore.Get(), host)
	}

	return s.facade.UpdateHost(datastore.Get(), host)
}
Esempio n. 3
0
func (s *scheduler) AddUpdateService(svc *service.Service) error {
	if sv, err := s.facade.GetService(datastore.Get(), svc.ID); err != nil {
		return err
	} else if sv == nil {
		return s.facade.AddService(datastore.Get(), *svc)
	}

	return s.facade.UpdateService(datastore.Get(), *svc)
}
Esempio n. 4
0
func (this *ControlPlaneDao) UpdateService(svc service.Service, unused *int) error {
	if err := this.facade.UpdateService(datastore.Get(), svc); err != nil {
		return err
	}

	// Create the tenant volume
	if tenantID, err := this.facade.GetTenantID(datastore.Get(), svc.ID); err != nil {
		glog.Warningf("Could not get tenant for service %s: %s", svc.ID, err)
	} else if _, err := this.dfs.GetVolume(tenantID); err != nil {
		glog.Warningf("Could not create volume for tenant %s: %s", tenantID, err)
	}
	return nil
}
Esempio n. 5
0
func (this *ControlPlaneDao) GetRunningService(request dao.ServiceStateRequest, running *dao.RunningService) error {
	glog.V(3).Infof("ControlPlaneDao.GetRunningService: request=%v", request)
	*running = dao.RunningService{}

	serviceID := request.ServiceID
	poolID, err := this.facade.GetPoolForService(datastore.Get(), serviceID)
	if err != nil {
		glog.Errorf("Unable to get service %v: %v", serviceID, err)
		return err
	}

	poolBasedConn, err := zzk.GetLocalConnection(zzk.GeneratePoolPath(poolID))
	if err != nil {
		glog.Errorf("Error in getting a connection based on pool %v: %v", poolID, err)
		return err
	}

	if thisRunning, err := zkservice.LoadRunningService(poolBasedConn, request.ServiceID, request.ServiceStateID); err != nil {
		glog.Errorf("zkservice.LoadRunningService failed (conn: %+v serviceID: %v): %v", poolBasedConn, request.ServiceID, err)
		return err
	} else {
		if thisRunning != nil {
			*running = *thisRunning
		}
	}

	return nil
}
Esempio n. 6
0
// GetServiceAddressAssignments fills in all AddressAssignments for the specified serviced id.
func (this *ControlPlaneDao) GetServiceAddressAssignments(serviceID string, assignments *[]addressassignment.AddressAssignment) error {
	err := this.facade.GetServiceAddressAssignments(datastore.Get(), serviceID, assignments)
	if assignments == nil {
		*assignments = make([]addressassignment.AddressAssignment, 0)
	}
	return err
}
Esempio n. 7
0
func (this *ControlPlaneDao) GetRunningServices(request dao.EntityRequest, allRunningServices *[]dao.RunningService) error {
	// we initialize the data container to something here in case it has not been initialized yet
	*allRunningServices = make([]dao.RunningService, 0)
	allPools, err := this.facade.GetResourcePools(datastore.Get())
	if err != nil {
		glog.Error("runningservice.go failed to get resource pool")
		return err
	} else if allPools == nil || len(allPools) == 0 {
		return fmt.Errorf("no resource pools found")
	}

	for _, aPool := range allPools {
		poolBasedConn, err := zzk.GetLocalConnection(zzk.GeneratePoolPath(aPool.ID))
		if err != nil {
			glog.Error("runningservice.go Failed to get connection based on pool: %v", aPool.ID)
			return err
		}

		singlePoolRunningServices := []dao.RunningService{}
		singlePoolRunningServices, err = zkservice.LoadRunningServices(poolBasedConn)
		if err != nil {
			glog.Errorf("Failed GetAllRunningServices: %v", err)
			return err
		}

		for _, rs := range singlePoolRunningServices {
			*allRunningServices = append(*allRunningServices, rs)
		}
	}

	return nil
}
Esempio n. 8
0
func (this *ControlPlaneDao) GetRunningServicesForHost(hostID string, services *[]dao.RunningService) error {
	// we initialize the data container to something here in case it has not been initialized yet
	*services = make([]dao.RunningService, 0)
	myHost, err := this.facade.GetHost(datastore.Get(), hostID)
	if err != nil {
		glog.Errorf("Unable to get host %v: %v", hostID, err)
		return err
	} else if myHost == nil {
		return nil
	}

	poolBasedConn, err := zzk.GetLocalConnection(zzk.GeneratePoolPath(myHost.PoolID))
	if err != nil {
		glog.Errorf("Error in getting a connection based on pool %v: %v", myHost.PoolID, err)
		return err
	}

	*services, err = zkservice.LoadRunningServicesByHost(poolBasedConn, hostID)
	if err != nil {
		glog.Errorf("zkservice.LoadRunningServicesByHost (conn: %+v host: %v) failed: %v", poolBasedConn, hostID, err)
		return err
	}

	return nil
}
Esempio n. 9
0
// GetService gets a service.
func (this *ControlPlaneDao) GetService(id string, myService *service.Service) error {
	svc, err := this.facade.GetService(datastore.Get(), id)
	if svc != nil {
		*myService = *svc
	}
	return err
}
Esempio n. 10
0
func (this *ControlPlaneDao) GetRunningServicesForService(serviceID string, services *[]dao.RunningService) error {
	// we initialize the data container to something here in case it has not been initialized yet
	*services = make([]dao.RunningService, 0)

	poolID, err := this.facade.GetPoolForService(datastore.Get(), serviceID)
	if err != nil {
		glog.Errorf("Unable to get service %v: %v", serviceID, err)
		return err
	}

	poolBasedConn, err := zzk.GetLocalConnection(zzk.GeneratePoolPath(poolID))
	if err != nil {
		glog.Errorf("Error in getting a connection based on pool %v: %v", poolID, err)
		return err
	}

	svcs, err := zkservice.LoadRunningServicesByService(poolBasedConn, serviceID)
	if err != nil {
		glog.Errorf("LoadRunningServicesByService failed (conn: %+v serviceID: %v): %v", poolBasedConn, serviceID, err)
		return err
	}

	for _, svc := range svcs {
		*services = append(*services, svc)
	}

	return nil
}
Esempio n. 11
0
func (d *daemon) initContext() (datastore.Context, error) {
	datastore.Register(d.dsDriver)
	ctx := datastore.Get()
	if ctx == nil {
		return nil, errors.New("context not available")
	}
	return ctx, nil
}
Esempio n. 12
0
// Get a service endpoint.
func (this *ControlPlaneDao) GetServiceEndpoints(serviceID string, response *map[string][]dao.ApplicationEndpoint) (err error) {
	if result, err := this.facade.GetServiceEndpoints(datastore.Get(), serviceID); err == nil {
		*response = result
		return nil
	} else {
		return err
	}
}
Esempio n. 13
0
// The tenant id is the root service uuid. Walk the service tree to root to find the tenant id.
func (this *ControlPlaneDao) GetTenantId(serviceID string, tenantId *string) error {
	if tid, err := this.facade.GetTenantID(datastore.Get(), serviceID); err == nil {
		*tenantId = tid
		return nil
	} else {
		return err
	}
}
Esempio n. 14
0
// Get tagged services (can also filter by name and/or tenantID)
func (this *ControlPlaneDao) GetTaggedServices(request dao.ServiceRequest, services *[]service.Service) error {
	if svcs, err := this.facade.GetTaggedServices(datastore.Get(), request); err == nil {
		*services = svcs
		return nil
	} else {
		return err
	}
}
Esempio n. 15
0
//getService is an internal method that returns a Service without filling in all related service data like address assignments
//and modified config files
func (f *Facade) getService(ctx datastore.Context, id string) (service.Service, error) {
	glog.V(3).Infof("Facade.getService: id=%s", id)
	store := f.serviceStore
	svc, err := store.Get(datastore.Get(), id)
	if err != nil || svc == nil {
		return service.Service{}, err
	}
	return *svc, err
}
Esempio n. 16
0
func (this *ControlPlaneDao) GetUser(userName string, user *userdomain.User) error {
	glog.V(2).Infof("ControlPlaneDao.GetUser: userName=%s", userName)
	store := userdomain.NewStore()
	err := store.Get(datastore.Get(), userdomain.Key(userName), user)
	glog.V(2).Infof("ControlPlaneDao.GetUser: userName=%s, user=%+v, err=%s", userName, user, err)
	if user == nil {
		*user = userdomain.User{}
	}
	return err
}
Esempio n. 17
0
func (this *ControlPlaneDao) FindChildService(request dao.FindChildRequest, service *service.Service) error {
	svc, err := this.facade.FindChildService(datastore.Get(), request.ServiceID, request.ChildName)
	if err != nil {
		return err
	}

	if svc != nil {
		*service = *svc
	} else {
		glog.Warningf("unable to find child of service: %+v", service)
	}
	return nil
}
Esempio n. 18
0
//addUser places a new user record into elastic searchp
func (this *ControlPlaneDao) AddUser(newUser userdomain.User, userName *string) error {
	glog.V(2).Infof("ControlPlane.NewUser: %+v", newUser)
	name := strings.TrimSpace(*userName)
	newUser.Password = hashPassword(newUser.Password)

	// save the user
	var existing userdomain.User
	if err := this.GetUser(name, &existing); err != nil && !datastore.IsErrNoSuchEntity(err) {
		return err
	}
	store := userdomain.NewStore()
	return store.Put(datastore.Get(), userdomain.Key(name), &newUser)
}
Esempio n. 19
0
func (dfs *DistributedFilesystem) desynchronize(image *docker.Image) error {
	// inspect the image
	dImg, err := image.Inspect()
	if err != nil {
		glog.Errorf("Could not inspect image %s (%s): %s", image.ID, image.UUID, err)
		return err
	}

	// look up services for that tenant
	svcs, err := dfs.facade.GetServices(datastore.Get(), dao.ServiceRequest{TenantID: image.ID.User})
	if err != nil {
		glog.Errorf("Could not get services for tenant %s from %s (%s): %s", image.ID.User, image.ID, image.UUID, err)
		return err
	}

	for _, svc := range svcs {
		// figure out which services are using the provided image
		svcImageID, err := commons.ParseImageID(svc.ImageID)
		if err != nil {
			glog.Warningf("Could not parse image %s for %s (%s): %s", svc.ImageID, svc.Name, svc.ID)
			continue
		} else if !svcImageID.Equals(image.ID) {
			continue
		}

		// TODO: we need to switch to using dao.ControlPlane
		conn, err := zzk.GetLocalConnection(zzk.GeneratePoolPath(svc.PoolID))
		if err != nil {
			glog.Warningf("Could not acquire connection to the coordinator (%s): %s", svc.PoolID, err)
			continue
		}

		states, err := zkservice.GetServiceStates(conn, svc.ID)
		if err != nil {
			glog.Warningf("Could not get running services for %s (%s): %s", svc.Name, svc.ID)
			continue
		}

		for _, state := range states {
			// check if the instance has been running since before the commit
			if state.IsRunning() && state.Started.Before(dImg.Created) {
				state.InSync = false
				if err := zkservice.UpdateServiceState(conn, &state); err != nil {
					glog.Warningf("Could not update service state %s for %s (%s) as out of sync: %s", state.ID, svc.Name, svc.ID, err)
					continue
				}
			}
		}
	}
	return nil
}
Esempio n. 20
0
// ResetRegistry will update the host:port of the docker registry
func (dfs *DistributedFilesystem) ResetRegistry() error {
	// get all the services in the system
	svcs, err := dfs.facade.GetServices(datastore.Get(), dao.ServiceRequest{})
	if err != nil {
		glog.Errorf("Could not get services for updating the registry")
		return err
	}

	imagemap := make(map[string]struct{})
	for _, svc := range svcs {
		imageID, err := commons.ParseImageID(svc.ImageID)
		if err != nil {
			glog.Errorf("Could not parse image ID (%s) for service %s (%s): %s", svc.ImageID, svc.Name, svc.ID, err)
			return err
		}

		if imageID.Host == dfs.dockerHost && imageID.Port == dfs.dockerPort {
			continue
		}

		if _, ok := imagemap[imageID.BaseName()]; !ok {
			if err := dfs.registerImages(imageID.BaseName()); err != nil {
				glog.Errorf("Could not reregister image %s: %s", imageID.BaseName(), err)
				return err
			}
			imagemap[imageID.BaseName()] = struct{}{}
		}

		imageID.Host, imageID.Port = dfs.dockerHost, dfs.dockerPort
		svc.ImageID = imageID.String()
		if err := dfs.facade.UpdateService(datastore.Get(), svc); err != nil {
			glog.Errorf("Could not update service %s (%s) with image %s", svc.Name, svc.ID, svc.ImageID)
			return err
		}
	}

	return nil
}
Esempio n. 21
0
func (this *ControlPlaneDao) getPoolBasedConnection(serviceID string) (client.Connection, error) {
	poolID, err := this.facade.GetPoolForService(datastore.Get(), serviceID)
	if err != nil {
		glog.V(2).Infof("ControlPlaneDao.GetPoolForService service=%+v err=%s", serviceID, err)
		return nil, err
	}

	poolBasedConn, err := zzk.GetLocalConnection(zzk.GeneratePoolPath(poolID))
	if err != nil {
		glog.Errorf("Error in getting a connection based on pool %v: %v", poolID, err)
		return nil, err
	}
	return poolBasedConn, nil
}
Esempio n. 22
0
//UpdateUser updates the user entry in elastic search. NOTE: It is assumed the
//pasword is NOT hashed when updating the user record
func (this *ControlPlaneDao) UpdateUser(user userdomain.User, unused *int) error {
	glog.V(2).Infof("ControlPlaneDao.UpdateUser: %+v", user)

	id := strings.TrimSpace(user.Name)
	if id == "" {
		return errors.New("empty User.Name not allowed")
	}

	user.Name = id
	user.Password = hashPassword(user.Password)

	store := userdomain.NewStore()
	return store.Put(datastore.Get(), userdomain.Key(user.Name), &user)
}
Esempio n. 23
0
func (this *ControlPlaneDao) RemoveService(id string, unused *int) error {
	var tenantID string
	if err := this.GetTenantId(id, &tenantID); err != nil {
		glog.Errorf("Could not find tenant for service %s: %s", id, err)
		return err
	}
	if err := this.facade.RemoveService(datastore.Get(), id); err != nil {
		return err
	}
	if tenantID == id {
		// We're deleting the top-level service, so there may be snapshots
		if err := this.DeleteSnapshots(id, unused); err != nil {
			return err
		}
	}
	return nil
}
Esempio n. 24
0
// CloneService clones a service.  Return error if given serviceID is not found
func (this *ControlPlaneDao) CloneService(request dao.ServiceCloneRequest, clonedServiceId *string) error {
	svc, err := this.facade.GetService(datastore.Get(), request.ServiceID)
	if err != nil {
		glog.Errorf("ControlPlaneDao.CloneService: unable to find service id %+v: %s", request.ServiceID, err)
		return err
	}

	cloned, err := service.CloneService(svc, request.Suffix)
	if err != nil {
		glog.Errorf("ControlPlaneDao.CloneService: unable to rename service %+v %v: %s", svc.ID, svc.Name, err)
		return err
	}

	if err := this.AddService(*cloned, clonedServiceId); err != nil {
		return err
	}

	return nil
}
Esempio n. 25
0
func (ft *FacadeTest) TestDaoValidServiceForStart(t *C) {
	testService := service.Service{
		ID: "TestDaoValidServiceForStart_ServiceID",
		Endpoints: []service.ServiceEndpoint{
			service.ServiceEndpoint{
				EndpointDefinition: servicedefinition.EndpointDefinition{
					Name:        "TestDaoValidServiceForStart_EndpointName",
					Protocol:    "tcp",
					PortNumber:  8081,
					Application: "websvc",
					Purpose:     "import",
				},
			},
		},
	}
	err := ft.Facade.validateServicesForStarting(datastore.Get(), &testService)
	if err != nil {
		t.Error("Services failed validation for starting: ", err)
	}
}
Esempio n. 26
0
// RegisterHealthCheck updates the healthStatus and healthTime structures with a health check result.
func RegisterHealthCheck(serviceID string, instanceID string, name string, passed string, f *facade.Facade) {
	lock.Lock()
	defer lock.Unlock()
	serviceStatus, ok := healthStatuses[serviceID]
	if !ok {
		serviceStatus = make(map[string]map[string]*domain.HealthCheckStatus)
		healthStatuses[serviceID] = serviceStatus
	}
	instanceStatus, ok := serviceStatus[instanceID]
	if !ok {
		instanceStatus = make(map[string]*domain.HealthCheckStatus)
		serviceStatus[instanceID] = instanceStatus
	}
	if name == "__instance_shutdown" {
		delete(serviceStatus, instanceID)
		return
	}
	thisStatus, ok := instanceStatus[name]
	if !ok {
		healthChecks, err := f.GetHealthChecksForService(datastore.Get(), serviceID)
		if err != nil {
			glog.Errorf("Unable to acquire health checks: %+v", err)
			return
		}
		for iname, icheck := range healthChecks {
			_, ok = instanceStatus[iname]
			if !ok {
				instanceStatus[iname] = &domain.HealthCheckStatus{name, "unknown", 0, icheck.Interval.Seconds(), time.Now().Unix(), ""}
			}
		}
	}
	thisStatus, ok = instanceStatus[name]
	if !ok {
		glog.Warningf("ignoring %s health status %s, not found in service %s", passed, name, serviceID)
		return
	}
	thisStatus.Status = passed
	thisStatus.Timestamp = time.Now().UTC().Unix()
}
Esempio n. 27
0
func (this *ControlPlaneDao) StopRunningInstance(request dao.HostServiceRequest, unused *int) error {
	myHost, err := this.facade.GetHost(datastore.Get(), request.HostID)
	if err != nil {
		glog.Errorf("Unable to get host %v: %v", request.HostID, err)
		return err
	}
	if myHost == nil {
		return fmt.Errorf("Host %s does not exist", request.HostID)
	}
	poolBasedConn, err := zzk.GetLocalConnection(zzk.GeneratePoolPath(myHost.PoolID))
	if err != nil {
		glog.Errorf("Error in getting a connection based on pool %v: %v", myHost.PoolID, err)
		return err
	}

	if err := zkservice.StopServiceInstance(poolBasedConn, request.HostID, request.ServiceStateID); err != nil {
		glog.Errorf("zkservice.StopServiceInstance failed (conn: %+v hostID: %v serviceStateID: %v): %v", poolBasedConn, request.HostID, request.ServiceStateID, err)
		return err
	}

	return nil
}
Esempio n. 28
0
func (ft *FacadeTest) TestDaoInvalidServiceForStart(t *C) {
	testService := service.Service{
		ID: "TestDaoInvalidServiceForStart_ServiceID",
		Endpoints: []service.ServiceEndpoint{
			service.ServiceEndpoint{
				EndpointDefinition: servicedefinition.EndpointDefinition{
					Name:        "TestDaoInvalidServiceForStart_EndpointName",
					Protocol:    "tcp",
					PortNumber:  8081,
					Application: "websvc",
					Purpose:     "import",
					AddressConfig: servicedefinition.AddressResourceConfig{
						Port:     8081,
						Protocol: commons.TCP,
					},
				},
			},
		},
	}
	err := ft.Facade.validateServicesForStarting(datastore.Get(), &testService)
	if err == nil {
		t.Error("Services should have failed validation for starting...")
	}
}
Esempio n. 29
0
func (this *ControlPlaneDao) Action(request dao.AttachRequest, unused *int) error {
	ctx := datastore.Get()
	svc, err := this.facade.GetService(ctx, request.Running.ServiceID)
	if err != nil {
		return err
	}

	var command []string
	if request.Command == "" {
		return fmt.Errorf("missing command")
	}

	if err := svc.EvaluateActionsTemplate(serviceGetter(ctx, this.facade), childFinder(ctx, this.facade), request.Running.InstanceID); err != nil {
		return err
	}

	action, ok := svc.Actions[request.Command]
	if !ok {
		return fmt.Errorf("action not found for service %s: %s", svc.ID, request.Command)
	}

	command = append([]string{action}, request.Args...)
	req := zkdocker.Action{
		HostID:   request.Running.HostID,
		DockerID: request.Running.DockerID,
		Command:  command,
	}

	conn, err := zzk.GetLocalConnection(zzk.GeneratePoolPath(svc.PoolID))
	if err != nil {
		return err
	}

	_, err = zkdocker.SendAction(conn, &req)
	return err
}
Esempio n. 30
0
func (s *S) SetUpTest(c *C) {
	s.ElasticTest.SetUpTest(c)
	datastore.Register(s.Driver())
	s.ctx = datastore.Get()
	s.hs = NewStore()
}