Exemplo n.º 1
0
func RestGetService(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var allServices []*dao.Service

	if err := client.GetServices(&empty, &allServices); err != nil {
		glog.Errorf("Could not get services: %v", err)
		RestServerError(w)
		return
	}

	sid, err := url.QueryUnescape(r.PathParam("serviceId"))
	if err != nil {
		RestBadRequest(w)
		return
	}

	for _, service := range allServices {
		if service.Id == sid {
			w.WriteJson(&service)
			return
		}
	}

	glog.Errorf("No such service [%v]", sid)
	RestServerError(w)
}
Exemplo n.º 2
0
func getNamedServices(client *serviced.ControlClient, nmregex string) ([]*dao.Service, error) {
	services := []*dao.Service{}
	if err := client.GetServices(&empty, &services); err != nil {
		glog.Errorf("Could not get named services: %v", err)
		return nil, err
	}

	return filterByNameRegex(nmregex, services)
}
Exemplo n.º 3
0
func RestGetPools(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var poolsMap map[string]*dao.ResourcePool
	err := client.GetResourcePools(&empty, &poolsMap)
	if err != nil {
		glog.Error("Could not get resource pools: ", err)
		RestServerError(w)
		return
	}
	w.WriteJson(&poolsMap)
}
Exemplo n.º 4
0
func getServices(client *serviced.ControlClient) ([]*dao.Service, error) {
	services := []*dao.Service{}
	if err := client.GetServices(&empty, &services); err != nil {
		glog.Errorf("Could not get services: %v", err)
		return nil, err
	}

	glog.V(2).Infof("Returning %d services", len(services))
	return services, nil
}
Exemplo n.º 5
0
func RestGetHosts(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var hosts map[string]*dao.Host
	err := client.GetHosts(&empty, &hosts)
	if err != nil {
		glog.Errorf("Could not get hosts: %v", err)
		RestServerError(w)
		return
	}
	glog.V(2).Infof("Returning %d hosts", len(hosts))
	w.WriteJson(&hosts)
}
Exemplo n.º 6
0
func RestGetServiceLogs(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	serviceId, err := url.QueryUnescape(r.PathParam("serviceId"))
	if err != nil {
		RestBadRequest(w)
		return
	}
	var logs string
	err = client.GetServiceLogs(serviceId, &logs)
	if err != nil {
		glog.Errorf("Unexpected error getting logs: %v", err)
		RestServerError(w)
	}
	w.WriteJson(&SimpleResponse{logs, serviceLinks(serviceId)})
}
Exemplo n.º 7
0
func getTaggedServices(client *serviced.ControlClient, tags, nmregex string) ([]*dao.Service, error) {
	services := []*dao.Service{}
	var ts interface{}
	ts = strings.Split(tags, ",")
	if err := client.GetTaggedServices(&ts, &services); err != nil {
		glog.Errorf("Could not get tagged services: %v", err)
		return nil, err
	}

	if nmregex != "" {
		return filterByNameRegex(nmregex, services)
	}
	glog.V(2).Infof("Returning %d tagged services", len(services))
	return services, nil
}
Exemplo n.º 8
0
func RestGetAllRunning(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var services []*dao.RunningService
	err := client.GetRunningServices(&empty, &services)
	if err != nil {
		glog.Errorf("Could not get services: %v", err)
		RestServerError(w)
		return
	}
	if services == nil {
		glog.V(3).Info("Services was nil, returning empty list instead")
		services = []*dao.RunningService{}
	}
	glog.V(2).Infof("Return %d running services", len(services))
	w.WriteJson(&services)
}
Exemplo n.º 9
0
func RestRemovePool(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	poolId, err := url.QueryUnescape(r.PathParam("poolId"))
	if err != nil {
		RestBadRequest(w)
		return
	}
	var unused int
	err = client.RemoveResourcePool(poolId, &unused)
	if err != nil {
		glog.Error("Could not remove resource pool: ", err)
		RestServerError(w)
		return
	}
	glog.V(0).Info("Removed pool ", poolId)
	w.WriteJson(&SimpleResponse{"Removed resource pool", poolsLinks()})
}
Exemplo n.º 10
0
func RestRemoveHost(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var unused int
	hostId, err := url.QueryUnescape(r.PathParam("hostId"))
	if err != nil {
		RestBadRequest(w)
		return
	}
	err = client.RemoveHost(hostId, &unused)
	if err != nil {
		glog.Errorf("Could not remove host: %v", err)
		RestServerError(w)
		return
	}
	glog.V(0).Info("Removed host ", hostId)
	w.WriteJson(&SimpleResponse{"Removed host", hostsLinks()})
}
Exemplo n.º 11
0
func RestAddPool(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var payload dao.ResourcePool
	var poolId string
	err := r.DecodeJsonPayload(&payload)
	if err != nil {
		glog.V(1).Info("Could not decode pool payload: ", err)
		RestBadRequest(w)
		return
	}
	err = client.AddResourcePool(payload, &poolId)
	if err != nil {
		glog.Error("Unable to add pool: ", err)
		RestServerError(w)
		return
	}
	glog.V(0).Info("Added pool ", poolId)
	w.WriteJson(&SimpleResponse{"Added resource pool", poolLinks(poolId)})
}
Exemplo n.º 12
0
func RestDeployAppTemplate(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var payload dao.ServiceTemplateDeploymentRequest
	err := r.DecodeJsonPayload(&payload)
	if err != nil {
		glog.V(1).Info("Could not decode deployment payload: ", err)
		RestBadRequest(w)
		return
	}
	var unused int
	err = client.DeployTemplate(payload, &unused)
	if err != nil {
		glog.Error("Could not deploy template: ", err)
		RestServerError(w)
		return
	}
	glog.V(0).Info("Deployed template ", payload)
	w.WriteJson(&SimpleResponse{"Deployed app template", servicesLinks()})
}
Exemplo n.º 13
0
func RestGetTopServices(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var allServices []*dao.Service
	topServices := []*dao.Service{}

	err := client.GetServices(&empty, &allServices)
	if err != nil {
		glog.Errorf("Could not get services: %v", err)
		RestServerError(w)
		return
	}
	for _, service := range allServices {
		if len(service.ParentServiceId) == 0 {
			topServices = append(topServices, service)
		}
	}
	glog.V(2).Infof("Returning %d services as top services", len(topServices))
	w.WriteJson(&topServices)
}
Exemplo n.º 14
0
func RestGetRunningForService(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	serviceId, err := url.QueryUnescape(r.PathParam("serviceId"))
	if err != nil {
		RestBadRequest(w)
		return
	}
	var services []*dao.RunningService
	err = client.GetRunningServicesForService(serviceId, &services)
	if err != nil {
		glog.Errorf("Could not get services: %v", err)
		RestServerError(w)
		return
	}
	if services == nil {
		glog.V(3).Info("Running services was nil, returning empty list instead")
		services = []*dao.RunningService{}
	}
	glog.V(2).Infof("Returning %d running services for service %s", len(services), serviceId)
	w.WriteJson(&services)
}
Exemplo n.º 15
0
func RestGetHostsForResourcePool(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var poolHosts []*dao.PoolHost
	poolId, err := url.QueryUnescape(r.PathParam("poolId"))
	if err != nil {
		glog.V(1).Infof("Unable to acquire pool ID: %v", err)
		RestBadRequest(w)
		return
	}
	err = client.GetHostsForResourcePool(poolId, &poolHosts)
	if err != nil {
		glog.Errorf("Could not get hosts: %v", err)
		RestServerError(w)
		return
	}
	if poolHosts == nil {
		glog.V(3).Info("Pool hosts was nil, returning empty list instead")
		poolHosts = []*dao.PoolHost{}
	}
	glog.V(2).Infof("Returning %d hosts for pool %s", len(poolHosts), poolId)
	w.WriteJson(&poolHosts)
}
Exemplo n.º 16
0
func RestGetRunningService(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	serviceStateId, err := url.QueryUnescape(r.PathParam("serviceStateId"))
	if err != nil {
		RestBadRequest(w)
		return
	}
	serviceId, err := url.QueryUnescape(r.PathParam("serviceId"))
	if err != nil {
		RestBadRequest(w)
		return
	}
	request := dao.ServiceStateRequest{serviceId, serviceStateId}

	var running dao.RunningService
	err = client.GetRunningService(request, &running)
	if err != nil {
		glog.Errorf("Unexpected error getting logs: %v", err)
		RestServerError(w)
	}
	w.WriteJson(running)

}
Exemplo n.º 17
0
func RestAddHost(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var payload dao.Host
	var hostId string
	err := r.DecodeJsonPayload(&payload)
	if err != nil {
		glog.V(1).Infof("Could not decode host payload: %v", err)
		RestBadRequest(w)
		return
	}
	// Save the pool ID and IP address for later. GetInfo wipes these
	pool := payload.PoolId
	ipAddr := payload.IpAddr
	remoteClient, err := serviced.NewAgentClient(payload.IpAddr)
	if err != nil {
		glog.Errorf("Could not create connection to host %s: %v", payload.IpAddr, err)
		RestServerError(w)
		return
	}

	err = remoteClient.GetInfo(0, &payload)
	if err != nil {
		glog.Errorf("Unable to get remote host info: %v", err)
		RestBadRequest(w)
		return
	}
	// Reset the pool ID and IP address
	payload.PoolId = pool
	parts := strings.Split(ipAddr, ":")
	payload.IpAddr = parts[0]

	err = client.AddHost(payload, &hostId)
	if err != nil {
		glog.Errorf("Unable to add host: %v", err)
		RestServerError(w)
		return
	}
	glog.V(0).Info("Added host ", hostId)
	w.WriteJson(&SimpleResponse{"Added host", hostLinks(hostId)})
}
Exemplo n.º 18
0
func RestUpdatePool(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	poolId, err := url.QueryUnescape(r.PathParam("poolId"))
	if err != nil {
		RestBadRequest(w)
		return
	}
	var payload dao.ResourcePool
	var unused int
	err = r.DecodeJsonPayload(&payload)
	if err != nil {
		glog.V(1).Info("Could not decode pool payload: ", err)
		RestBadRequest(w)
		return
	}
	err = client.UpdateResourcePool(payload, &unused)
	if err != nil {
		glog.Error("Unable to update pool: ", err)
		RestServerError(w)
		return
	}
	glog.V(1).Info("Updated pool ", poolId)
	w.WriteJson(&SimpleResponse{"Updated resource pool", poolLinks(poolId)})
}
Exemplo n.º 19
0
func RestUpdateHost(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	hostId, err := url.QueryUnescape(r.PathParam("hostId"))
	if err != nil {
		RestBadRequest(w)
		return
	}
	glog.V(3).Infof("Received update request for %s", hostId)
	var payload dao.Host
	var unused int
	err = r.DecodeJsonPayload(&payload)
	if err != nil {
		glog.V(1).Infof("Could not decode host payload: %v", err)
		RestBadRequest(w)
		return
	}
	err = client.UpdateHost(payload, &unused)
	if err != nil {
		glog.Errorf("Unable to update host: %v", err)
		RestServerError(w)
		return
	}
	glog.V(1).Info("Updated host ", hostId)
	w.WriteJson(&SimpleResponse{"Updated host", hostLinks(hostId)})
}
Exemplo n.º 20
0
func RestUpdateService(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	serviceId, err := url.QueryUnescape(r.PathParam("serviceId"))
	glog.V(3).Infof("Received update request for %s", serviceId)
	if err != nil {
		RestBadRequest(w)
		return
	}
	var payload dao.Service
	var unused int
	err = r.DecodeJsonPayload(&payload)
	if err != nil {
		glog.V(1).Info("Could not decode service payload: ", err)
		RestBadRequest(w)
		return
	}
	err = client.UpdateService(payload, &unused)
	if err != nil {
		glog.Errorf("Unable to update service %s: %v", serviceId, err)
		RestServerError(w)
		return
	}
	glog.V(1).Info("Updated service ", serviceId)
	w.WriteJson(&SimpleResponse{"Updated service", serviceLinks(serviceId)})
}
Exemplo n.º 21
0
func RestKillRunning(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	serviceStateId, err := url.QueryUnescape(r.PathParam("serviceStateId"))
	if err != nil {
		RestBadRequest(w)
		return
	}
	hostId, err := url.QueryUnescape(r.PathParam("hostId"))
	if err != nil {
		RestBadRequest(w)
		return
	}
	request := dao.HostServiceRequest{hostId, serviceStateId}
	glog.V(1).Info("Received request to kill ", request)

	var unused int
	err = client.StopRunningInstance(request, &unused)
	if err != nil {
		glog.Errorf("Unable to stop service: %v", err)
		RestServerError(w)
		return
	}

	w.WriteJson(&SimpleResponse{"Marked for death", servicesLinks()})
}
Exemplo n.º 22
0
func RestAddService(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var payload dao.Service
	var serviceId string
	err := r.DecodeJsonPayload(&payload)
	if err != nil {
		glog.V(1).Info("Could not decode service payload: ", err)
		RestBadRequest(w)
		return
	}
	service, err := dao.NewService()
	if err != nil {
		glog.Errorf("Could not create service: %v", err)
		RestServerError(w)
		return
	}
	now := time.Now()
	service.Name = payload.Name
	service.Description = payload.Description
	service.Context = payload.Context
	service.Tags = payload.Tags
	service.PoolId = payload.PoolId
	service.ImageId = payload.ImageId
	service.Startup = payload.Startup
	service.Instances = payload.Instances
	service.ParentServiceId = payload.ParentServiceId
	service.DesiredState = payload.DesiredState
	service.Launch = payload.Launch
	service.Endpoints = payload.Endpoints
	service.ConfigFiles = payload.ConfigFiles
	service.Volumes = payload.Volumes
	service.CreatedAt = now
	service.UpdatedAt = now

	//for each endpoint, evaluate it's Application
	if err = service.EvaluateEndpointTemplates(client); err != nil {
		glog.Errorf("Unable to evaluate service endpoints: %v", err)
		RestServerError(w)
		return
	}

	//add the service to the data store
	err = client.AddService(*service, &serviceId)
	if err != nil {
		glog.Errorf("Unable to add service: %v", err)
		RestServerError(w)
		return
	}

	//deploy the service, in other words start it
	var unused int
	sduuid, _ := dao.NewUuid()
	deployment := dao.ServiceDeployment{sduuid, "", service.Id, now}
	err = client.AddServiceDeployment(deployment, &unused)
	if err != nil {
		glog.Errorf("Unable to add service deployment: %v", err)
		RestServerError(w)
		return
	}

	glog.V(0).Info("Added service ", serviceId)
	w.WriteJson(&SimpleResponse{"Added service", serviceLinks(serviceId)})
}
Exemplo n.º 23
0
func RestGetAppTemplates(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var unused int
	var templatesMap map[string]*dao.ServiceTemplate
	client.GetServiceTemplates(unused, &templatesMap)
	w.WriteJson(&templatesMap)
}