Esempio n. 1
0
func healthHandler(responseWriter http.ResponseWriter, request *http.Request) {

	log.Printf("got health request %s", request.URL.RawQuery)

	// do some tutum call to check health
	serviceList, err := tutum.ListServices()
	if err != nil {
		log.Printf("tutum problem: %s", err)
		http.Error(responseWriter, "tutum problem", http.StatusInternalServerError)
		return
	}

	fmt.Fprintf(responseWriter, "ok. len(serviceList)=%d", len(serviceList.Objects))
}
Esempio n. 2
0
func redeployService(serviceNameToRedeploy string, haproxyServiceName string) error {
	// tutum User, ApiKey should be set in environment TUTUM_USER, TUTUM_APIKEY

	serviceList, err := tutum.ListServices()
	if err != nil {
		return err
	}

	redeployedServiceUuid, err := getServiceUuid(serviceNameToRedeploy, serviceList.Objects)
	if err != nil {
		return err
	}
	haproxyServiceUuid, err := getServiceUuid(haproxyServiceName, serviceList.Objects)
	if err != nil {
		return err
	}
	haproxyContainerUuid, err := getServicesFirstContainerUuid(haproxyServiceUuid)
	if err != nil {
		return err
	}

	redeployedService, err := tutum.GetService(redeployedServiceUuid)
	if err != nil {
		return err
	}

	log.Printf("redeploying service %s (%s) via haproxy %s (%s)",
		redeployedService.Name, redeployedServiceUuid, haproxyServiceName, haproxyServiceUuid)

	for i := range redeployedService.Containers {
		err = doRedeployContainer(redeployedService.Containers[i], redeployedService, haproxyContainerUuid)
		if err != nil {
			return err
		}
		time.Sleep(25 * time.Second)
	}

	// future improvement: do final check via haproxy

	log.Printf("successfully redeployed service %s (%s) via haproxy %s (%s)",
		redeployedService.Name, redeployedServiceUuid, haproxyServiceName, haproxyServiceUuid)

	return nil
}
Esempio n. 3
0
func main() {
	fmt.Printf("Drone Tutum Plugin built from %s\n", buildCommit)

	repo := drone.Repo{}
	vargs := Params{}

	plugin.Param("repo", &repo)
	plugin.Param("vargs", &vargs)
	plugin.MustParse()

	if vargs.Service == "" {
		vargs.Service = repo.Name
	}

	if vargs.Key == "" {
		fmt.Println("Error: Please provide an API key")
		os.Exit(1)
	}

	if vargs.Username == "" {
		fmt.Println("Error: Please provide a username")
		os.Exit(1)
	}

	tutum.User = vargs.Username
	tutum.ApiKey = vargs.Key

	svcs, err := tutum.ListServices()

	if err != nil {
		fmt.Printf("Error: Failed to get a list of services. %s\n", err)
		os.Exit(1)
	}

	service := vargs.Service
	stack := ""
	stackURI := ""

	if strings.Contains(service, ".") {
		parts := strings.Split(service, ".")

		if len(parts) != 2 {
			fmt.Printf("Error: Failed to parse service name %q.\n", service)
			os.Exit(1)
		}

		service = parts[0]
		stack = parts[1]

		stacks, err := tutum.ListStacks()

		if err != nil {
			fmt.Printf("Error: Failed to get a list of the stacks. %s\n", err)
			os.Exit(1)
		}

		foundStack := false
		var stk tutum.Stack

		for _, stk = range stacks.Objects {
			if stk.Name == stack {
				foundStack = true
				break
			}
		}

		if !foundStack {
			fmt.Printf("Error: Failed to find stack %q.\n", stack)
			os.Exit(1)
		}

		stackURI = stk.Resource_uri
	}

	foundService := false
	var svc tutum.Service

	for _, svc = range svcs.Objects {
		if svc.Name == service {
			if stackURI == "" {
				foundService = true
				break
			}

			if svc.Stack == stackURI {
				foundService = true
				break
			}
		}
	}

	if !foundService {
		fmt.Printf("Error: Failed to find server %s.\n", vargs.Service)
		os.Exit(1)
	}

	if vargs.Image != "" && vargs.Image != svc.Image_name {
		err = svc.Update(tutum.ServiceCreateRequest{
			Image: vargs.Image,
		})

		if err != nil {
			fmt.Printf("Error: Failed to update the %s image for %s. %s\n", vargs.Image, vargs.Service, err)
			os.Exit(1)
		}
	}

	if vargs.Redeploy {
		fmt.Printf("Redeploying service \"%s\"\n", svc.Name)

		err = svc.Redeploy(tutum.ReuseVolumesOption{
			Reuse: vargs.ReuseVolumes,
		})

		if err != nil {
			fmt.Printf("Failed to redeploy the %s service. %s\n", svc.Name, err)
			os.Exit(1)
		}
	}
}