Example #1
0
func SystemUpdate(rw http.ResponseWriter, r *http.Request) *httperr.Error {
	rack, err := models.GetSystem()

	if err != nil {
		return httperr.Server(err)
	}

	notifyData := map[string]string{}

	if count := GetForm(r, "count"); count != "" {
		count, err := strconv.Atoi(count)

		if err != nil {
			return httperr.Server(err)
		}

		rack.Count = count

		notifyData["count"] = strconv.Itoa(count)
	}

	if t := GetForm(r, "type"); t != "" {
		rack.Type = t

		notifyData["type"] = t
	}

	if version := GetForm(r, "version"); version != "" {
		rack.Version = version

		notifyData["version"] = version
	}

	err = rack.Save()

	if awsError(err) == "ValidationError" {
		switch {
		case strings.Index(err.Error(), "No updates are to be performed") > -1:
			return httperr.Errorf(403, "no system updates are to be performed")
		case strings.Index(err.Error(), "can not be updated") > -1:
			return httperr.Errorf(403, "system is already updating")
		}
	}

	if err != nil {
		return httperr.Server(err)
	}

	rack, err = models.GetSystem()

	if err != nil {
		return httperr.Server(err)
	}

	models.NotifySuccess("system:update", notifyData)

	return RenderJson(rw, rack)
}
Example #2
0
func SystemUpdate(rw http.ResponseWriter, r *http.Request) error {
	rack, err := models.GetSystem()

	if err != nil {
		return err
	}

	if count := GetForm(r, "count"); count != "" {
		count, err := strconv.Atoi(count)

		if err != nil {
			return err
		}

		rack.Count = count
	}

	if t := GetForm(r, "type"); t != "" {
		rack.Type = t
	}

	if version := GetForm(r, "version"); version != "" {
		rack.Version = version
	}

	err = rack.Save()

	if awsError(err) == "ValidationError" {
		switch {
		case strings.Index(err.Error(), "No updates are to be performed") > -1:
			return RenderForbidden(rw, fmt.Sprintf("no system updates are to be performed."))
		case strings.Index(err.Error(), "can not be updated") > -1:
			return RenderForbidden(rw, fmt.Sprintf("system is already updating."))
		}
	}

	if err != nil {
		return err
	}

	rack, err = models.GetSystem()

	if err != nil {
		return err
	}

	return RenderJson(rw, rack)
}
Example #3
0
func SystemShow(rw http.ResponseWriter, r *http.Request) error {
	rack, err := models.GetSystem()

	if awsError(err) == "ValidationError" {
		return RenderNotFound(rw, fmt.Sprintf("no such stack: %s", rack))
	}

	if err != nil {
		return err
	}

	return RenderJson(rw, rack)
}
Example #4
0
func SystemShow(rw http.ResponseWriter, r *http.Request) *httperr.Error {
	rack, err := models.GetSystem()

	if awsError(err) == "ValidationError" {
		return httperr.Errorf(404, "no such stack: %s", rack)
	}

	if err != nil {
		return httperr.Server(err)
	}

	return RenderJson(rw, rack)
}
Example #5
0
func heartbeat() {
	system, err := models.GetSystem()

	if err != nil {
		log.Error(err)
		return
	}

	apps, err := models.ListApps()

	if err != nil {
		log.Error(err)
		return
	}

	helpers.TrackEvent("kernel-heartbeat", map[string]interface{}{
		"app_count":      len(apps),
		"instance_count": system.Count,
		"instance_type":  system.Type,
		"region":         os.Getenv("AWS_REGION"),
		"version":        system.Version,
	})
}
Example #6
0
func InstanceTerminate(rw http.ResponseWriter, r *http.Request) *httperr.Error {
	rack, err := models.GetSystem()

	if awsError(err) == "ValidationError" {
		return httperr.Errorf(404, "no such stack: %s", rack)
	}

	if err != nil {
		return httperr.Server(err)
	}

	instanceId := mux.Vars(r)["id"]

	_, err = models.EC2().TerminateInstances(&ec2.TerminateInstancesInput{
		InstanceIds: []*string{&instanceId},
	})

	if err != nil {
		return httperr.Server(err)
	}

	return RenderSuccess(rw)
}
Example #7
0
func StartCluster() {
	var log = logger.New("ns=cluster_monitor")

	defer recoverWith(func(err error) {
		helpers.Error(log, err)
	})

	// Report cluster size one time on start
	system, err := models.GetSystem()

	if err != nil {
		log.Error(err)
	} else {
		helpers.TrackEvent("kernel-cluster-monitor", fmt.Sprintf("count=%d type=%s", system.Count, system.Type))
	}

	for _ = range time.Tick(5 * time.Minute) {
		log.Log("tick")

		instances := Instances{}

		err := instances.describeASG()

		if err != nil {
			log.Error(err)
			continue
		}

		err = instances.describeECS()

		if err != nil {
			log.Error(err)
			continue
		}

		// TODO: Add an instances.testDocker() call to the mission critical path

		// Test if ASG Instance is registered and connected in ECS cluster
		for _, i := range instances {
			if !i.ASG {
				// TODO: Rogue instance?! Terminate?
				continue
			}

			if !i.ECS {
				// Not registered or not connected => set Unhealthy
				_, err := models.AutoScaling().SetInstanceHealth(
					&autoscaling.SetInstanceHealthInput{
						HealthStatus:             aws.String("Unhealthy"),
						InstanceId:               aws.String(i.Id),
						ShouldRespectGracePeriod: aws.Bool(true),
					},
				)

				i.Unhealthy = true

				if err != nil {
					log.Error(err)
					continue
				}
			}
		}

		log.Log(instances.log())
	}
}