示例#1
0
func (f *Facade) UpdateService(ctx datastore.Context, svc service.Service) error {
	glog.V(2).Infof("Facade.UpdateService: %+v", svc)
	//cannot update service without validating it.
	if svc.DesiredState != int(service.SVCStop) {
		if err := f.validateServicesForStarting(ctx, &svc); err != nil {
			glog.Warningf("Could not validate service %s (%s) for starting: %s", svc.Name, svc.ID, err)
			svc.DesiredState = int(service.SVCStop)
		}

		for _, ep := range svc.GetServiceVHosts() {
			for _, vh := range ep.VHosts {
				//check that vhosts aren't already started elsewhere
				if err := zkAPI(f).CheckRunningVHost(vh, svc.ID); err != nil {
					return err
				}
			}
		}
	}

	return f.updateService(ctx, &svc)
}
示例#2
0
// UpdateServiceVhosts updates vhosts of a service
func UpdateServiceVhosts(conn client.Connection, svc *service.Service) error {
	glog.V(2).Infof("UpdateServiceVhosts for ID:%s Name:%s", svc.ID, svc.Name)

	// generate map of current vhosts
	currentvhosts := map[string]string{}
	if svcvhosts, err := conn.Children(zkServiceVhosts); err == client.ErrNoNode {
		/*
			// do not do this, otherwise, nodes aren't deleted when calling RemoveServiceVhost

			if exists, err := zzk.PathExists(conn, zkServiceVhosts); err != nil {
				return err
			} else if !exists {
				err := conn.CreateDir(zkServiceVhosts)
				if err != client.ErrNodeExists && err != nil {
					return err
				}
			}
		*/
	} else if err != nil {
		glog.Errorf("UpdateServiceVhosts unable to retrieve vhost children at path %s %s", zkServiceVhosts, err)
		return err
	} else {
		for _, svcvhost := range svcvhosts {
			parts := strings.SplitN(svcvhost, "_", 2)
			vhostname := parts[1]
			currentvhosts[svcvhost] = vhostname
		}
	}
	glog.V(2).Infof("  currentvhosts %+v", currentvhosts)

	// generate map of vhosts in the service
	svcvhosts := map[string]string{}
	for _, ep := range svc.GetServiceVHosts() {
		for _, vhostname := range ep.VHosts {
			svcvhosts[fmt.Sprintf("%s_%s", svc.ID, vhostname)] = vhostname
		}
	}
	glog.V(2).Infof("  svcvhosts %+v", svcvhosts)

	// remove vhosts in current not in svc that match serviceid
	for sv, vhostname := range currentvhosts {
		svcID := strings.SplitN(sv, "_", 2)[0]
		if svcID != svc.ID {
			continue
		}

		if _, ok := svcvhosts[sv]; !ok {
			if err := RemoveServiceVhost(conn, svc.ID, vhostname); err != nil {
				return err
			}
		}
	}

	// add vhosts from svc not in current
	for sv, vhostname := range svcvhosts {
		if _, ok := currentvhosts[sv]; !ok {
			if err := UpdateServiceVhost(conn, svc.ID, vhostname); err != nil {
				return err
			}
		}
	}

	return nil
}