Ejemplo n.º 1
0
func ServiceStream(rw http.ResponseWriter, r *http.Request) {
	log := servicesLogger("stream").Start()

	service, err := models.GetServiceFromName(mux.Vars(r)["service"])

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	logs := make(chan []byte)
	done := make(chan bool)

	service.SubscribeLogs(logs, done)

	ws, err := upgrader.Upgrade(rw, r, nil)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	log.Success("step=upgrade service=%q", service.Name)

	defer ws.Close()

	for data := range logs {
		ws.WriteMessage(websocket.TextMessage, data)
	}

	log.Success("step=ended service=%q", service.Name)
}
Ejemplo n.º 2
0
Archivo: apps.go Proyecto: oren/kernel
func AppEnvironment(rw http.ResponseWriter, r *http.Request) {
	log := appsLogger("environment").Start()

	app := mux.Vars(r)["app"]

	env, err := models.GetEnvironment(app)

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

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	params := map[string]interface{}{
		"App":         app,
		"Environment": env,
	}

	switch r.Header.Get("Content-Type") {
	case "application/json":
		RenderJson(rw, params["Environment"])
	default:
		RenderPartial(rw, "app", "environment", params)
	}
}
Ejemplo n.º 3
0
func ServiceDelete(rw http.ResponseWriter, r *http.Request) {
	log := servicesLogger("delete").Start()

	vars := mux.Vars(r)
	name := vars["service"]

	service, err := models.GetServiceFromName(name)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	log.Success("step=services.get service=%q", service.Name)

	err = service.Delete()

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	log.Success("step=service.delete service=%q", service.Name)

	RenderText(rw, "ok")
}
Ejemplo n.º 4
0
Archivo: apps.go Proyecto: oren/kernel
func AppLogs(rw http.ResponseWriter, r *http.Request) {
	// log := appsLogger("logs").Start()

	app := mux.Vars(r)["app"]

	RenderPartial(rw, "app", "logs", app)
}
Ejemplo n.º 5
0
func ReleasePromote(rw http.ResponseWriter, r *http.Request) {
	log := releasesLogger("promote").Start()

	vars := mux.Vars(r)
	app := vars["app"]
	release := vars["release"]

	rel, err := models.GetRelease(app, release)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	err = rel.Promote()

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	log.Success("step=release.promote app=%q", app)

	RenderText(rw, "ok")
}
Ejemplo n.º 6
0
Archivo: apps.go Proyecto: oren/kernel
func AppShow(rw http.ResponseWriter, r *http.Request) {
	log := appsLogger("show").Start()

	app := mux.Vars(r)["app"]

	a, err := models.GetApp(app)

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

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	switch r.Header.Get("Content-Type") {
	case "application/json":
		RenderJson(rw, a)
	default:
		RenderTemplate(rw, "app", a)
	}
}
Ejemplo n.º 7
0
func ProcessRunAttached(ws *websocket.Conn) {
	defer ws.Close()

	log := processesLogger("run.attached").Start()

	vars := mux.Vars(ws.Request())
	app := vars["app"]
	process := vars["process"]
	command := ws.Request().Header.Get("Command")

	ps, err := models.GetProcess(app, process)

	if err != nil {
		helpers.Error(log, err)
		ws.Write([]byte(fmt.Sprintf("error: %s\n", err)))
		return
	}

	log.Success("step=upgrade app=%q", ps.App)

	defer ws.Close()

	err = ps.RunAttached(command, ws)

	if err != nil {
		helpers.Error(log, err)
		ws.Write([]byte(fmt.Sprintf("error: %s\n", err)))
		return
	}

	log.Success("step=ended app=%q", ps.App)
}
Ejemplo n.º 8
0
func EnvironmentDelete(rw http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	app := vars["app"]
	name := vars["name"]

	env, err := models.GetEnvironment(app)

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

	if err != nil {
		helpers.Error(nil, err)
		RenderError(rw, err)
		return
	}

	delete(env, name)

	err = models.PutEnvironment(app, env)

	if err != nil {
		helpers.Error(nil, err)
		RenderError(rw, err)
		return
	}

	RenderText(rw, "ok")
}
Ejemplo n.º 9
0
func EnvironmentCreate(rw http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	app := vars["app"]
	name := vars["name"]
	value := GetForm(r, "value")

	env, err := models.GetEnvironment(app)

	if err != nil {
		helpers.Error(nil, err)
		RenderError(rw, err)
		return
	}

	env[strings.ToUpper(name)] = value

	err = models.PutEnvironment(app, env)

	if err != nil {
		helpers.Error(nil, err)
		RenderError(rw, err)
		return
	}

	RenderText(rw, "ok")
}
Ejemplo n.º 10
0
Archivo: apps.go Proyecto: oren/kernel
func AppDelete(rw http.ResponseWriter, r *http.Request) {
	log := appsLogger("delete").Start()

	vars := mux.Vars(r)
	name := vars["app"]

	app, err := models.GetApp(name)

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

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	log.Success("step=app.get app=%q", app.Name)

	err = app.Delete()

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	log.Success("step=app.delete app=%q", app.Name)

	RenderText(rw, "ok")
}
Ejemplo n.º 11
0
Archivo: apps.go Proyecto: oren/kernel
func AppNameAvailable(rw http.ResponseWriter, r *http.Request) {
	app, _ := models.GetApp(mux.Vars(r)["app"])

	if app != nil {
		RenderText(rw, "false")
	} else {
		RenderText(rw, "true")
	}
}
Ejemplo n.º 12
0
func AppUpdate(rw http.ResponseWriter, r *http.Request) {
	log := appsLogger("update").Start()

	vars := mux.Vars(r)
	name := vars["app"]

	app, err := models.GetApp(name)

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

	params := map[string]string{}

	if process := GetForm(r, "process"); process != "" {
		process = models.UpperName(process)

		if count := GetForm(r, "count"); count != "" {
			params[process+"DesiredCount"] = count
		}

		if cpu := GetForm(r, "cpu"); cpu != "" {
			params[process+"Cpu"] = cpu
		}

		if mem := GetForm(r, "mem"); mem != "" {
			params[process+"Memory"] = mem
		}
	}

	if len(params) > 0 {
		err := app.UpdateParams(params)

		if ae, ok := err.(awserr.Error); ok {
			if ae.Code() == "ValidationError" {
				switch {
				case strings.Index(ae.Error(), "No updates are to be performed") > -1:
					RenderNotFound(rw, fmt.Sprintf("no updates are to be performed: %s", name))
					return
				case strings.Index(ae.Error(), "can not be updated") > -1:
					RenderNotFound(rw, fmt.Sprintf("app is already updating: %s", name))
					return
				}
			}
		}

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

	Redirect(rw, r, fmt.Sprintf("/apps/%s", name))
}
Ejemplo n.º 13
0
Archivo: apps.go Proyecto: oren/kernel
func AppStream(rw http.ResponseWriter, r *http.Request) {
	log := appsLogger("stream").Start()

	app := mux.Vars(r)["app"]

	ws, err := upgrader.Upgrade(rw, r, nil)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	log.Success("step=upgrade app=%q", app)

	defer ws.Close()

	a, err := models.GetApp(mux.Vars(r)["app"])

	if awsError(err) == "ValidationError" {
		ws.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("ERROR: no such app: %s\n", app)))
		return
	}

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	logs := make(chan []byte)
	done := make(chan bool)

	a.SubscribeLogs(logs, done)

	for data := range logs {
		ws.WriteMessage(websocket.TextMessage, data)
	}

	log.Success("step=ended app=%q", app)
}
Ejemplo n.º 14
0
func ServiceLogs(rw http.ResponseWriter, r *http.Request) {
	name := mux.Vars(r)["service"]

	service, err := models.GetServiceFromName(name)

	if err != nil {
		RenderError(rw, err)
		return
	}

	RenderPartial(rw, "service", "logs", service)
}
Ejemplo n.º 15
0
func ClusterTop(rw http.ResponseWriter, r *http.Request) {
	name := aws.String(os.Getenv("RACK"))

	res, err := models.CloudFormation().DescribeStacks(&cloudformation.DescribeStacksInput{StackName: name})

	if err != nil {
		RenderError(rw, err)
		return
	}

	if len(res.Stacks) == 0 {
		RenderError(rw, fmt.Errorf("Stack %s does not exist", os.Getenv("RACK")))
		return
	}

	stack := res.Stacks[0]

	outputs := make(map[string]string)

	for _, output := range stack.Outputs {
		outputs[*output.OutputKey] = *output.OutputValue
	}

	cluster := outputs["Cluster"]

	params := &cloudwatch.GetMetricStatisticsInput{
		MetricName: aws.String(mux.Vars(r)["metric"]),
		StartTime:  aws.Time(time.Now().Add(-2 * time.Minute)),
		EndTime:    aws.Time(time.Now()),
		Period:     aws.Long(60),
		Namespace:  aws.String("AWS/ECS"),
		Statistics: []*string{ // Required
			aws.String("Maximum"),
			aws.String("Average"),
			aws.String("Minimum"),
		},
		Dimensions: []*cloudwatch.Dimension{
			{
				Name:  aws.String("ClusterName"),
				Value: aws.String(cluster),
			},
		},
	}

	resp, err := models.CloudWatch().GetMetricStatistics(params)

	if err != nil {
		RenderError(rw, err)
		return
	}

	RenderJson(rw, resp)
}
Ejemplo n.º 16
0
Archivo: apps.go Proyecto: oren/kernel
func AppStatus(rw http.ResponseWriter, r *http.Request) {
	log := appsLogger("status").Start()

	app, err := models.GetApp(mux.Vars(r)["app"])

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	RenderText(rw, app.Status)
}
Ejemplo n.º 17
0
func ProcessLogs(rw http.ResponseWriter, r *http.Request) {
	// log := processesLogger("logs").Start()

	vars := mux.Vars(r)
	app := vars["app"]
	process := vars["process"]

	params := map[string]string{
		"App":     app,
		"Process": process,
	}

	RenderPartial(rw, "process", "logs", params)
}
Ejemplo n.º 18
0
func ServiceStatus(rw http.ResponseWriter, r *http.Request) {
	log := servicesLogger("show").Start()

	name := mux.Vars(r)["service"]
	service, err := models.GetServiceFromName(name)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	RenderText(rw, service.Status)
}
Ejemplo n.º 19
0
Archivo: apps.go Proyecto: oren/kernel
func AppDebug(rw http.ResponseWriter, r *http.Request) {
	log := appsLogger("environment").Start()

	app := mux.Vars(r)["app"]

	a, err := models.GetApp(app)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	RenderPartial(rw, "app", "debug", a)
}
Ejemplo n.º 20
0
Archivo: apps.go Proyecto: oren/kernel
func AppResources(rw http.ResponseWriter, r *http.Request) {
	log := appsLogger("resources").Start()

	app := mux.Vars(r)["app"]

	resources, err := models.ListResources(app)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	RenderPartial(rw, "app", "resources", resources)
}
Ejemplo n.º 21
0
Archivo: apps.go Proyecto: oren/kernel
func AppDeployments(rw http.ResponseWriter, r *http.Request) {
	log := appsLogger("deployments").Start()

	app := mux.Vars(r)["app"]

	deployments, err := models.ListDeployments(app)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	RenderPartial(rw, "app", "deployments", deployments)
}
Ejemplo n.º 22
0
func BuildLogs(ws *websocket.Conn) {
	defer ws.Close()

	log := buildsLogger("logs").Start()

	vars := mux.Vars(ws.Request())
	id := vars["build"]

	log.Success("step=upgrade build=%q", id)

	defer ws.Close()

	// proxy to docker container logs
	// https://docs.docker.com/reference/api/docker_remote_api_v1.19/#get-container-logs
	client, err := docker.NewClient("unix:///var/run/docker.sock")

	if err != nil {
		helpers.Error(log, err)
		ws.Write([]byte(fmt.Sprintf("error: %s\n", err)))
		return
	}

	r, w := io.Pipe()

	quit := make(chan bool)

	go scanLines(r, ws)
	go keepAlive(ws, quit)

	err = client.Logs(docker.LogsOptions{
		Container:    fmt.Sprintf("build-%s", id),
		Follow:       true,
		Stdout:       true,
		Stderr:       true,
		Tail:         "all",
		RawTerminal:  false,
		OutputStream: w,
		ErrorStream:  w,
	})

	quit <- true

	if err != nil {
		helpers.Error(log, err)
		ws.Write([]byte(fmt.Sprintf("error: %s\n", err)))
		return
	}
}
Ejemplo n.º 23
0
func ReleaseShow(rw http.ResponseWriter, r *http.Request) {
	log := releasesLogger("show").Start()

	vars := mux.Vars(r)
	app := vars["app"]
	release := vars["release"]

	rr, err := models.GetRelease(app, release)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	RenderJson(rw, rr)
}
Ejemplo n.º 24
0
func BuildStatus(rw http.ResponseWriter, r *http.Request) {
	log := buildsLogger("status").Start()

	vars := mux.Vars(r)
	app := vars["app"]
	id := vars["build"]

	build, err := models.GetBuild(app, id)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	RenderText(rw, build.Status)
}
Ejemplo n.º 25
0
func BuildGet(rw http.ResponseWriter, r *http.Request) {
	log := buildsLogger("list").Start()

	vars := mux.Vars(r)
	app := vars["app"]
	build := vars["build"]

	b, err := models.GetBuild(app, build)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	RenderJson(rw, b)
}
Ejemplo n.º 26
0
Archivo: apps.go Proyecto: oren/kernel
func AppEvents(rw http.ResponseWriter, r *http.Request) {
	log := appsLogger("events").Start()

	app := mux.Vars(r)["app"]

	events, err := models.ListECSEvents(app)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	for i, _ := range events {
		match := regexServiceCleaner.FindStringSubmatch(events[i].Message)

		if len(match) == 3 {
			events[i].Message = fmt.Sprintf("[ECS] (%s) %s", match[1], match[2])
		}
	}

	es, err := models.ListEvents(app)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	for _, e := range es {
		events = append(events, models.ServiceEvent{
			Message:   fmt.Sprintf("[CFM] (%s) %s %s", e.Name, e.Status, e.Reason),
			CreatedAt: e.Time,
		})
	}

	sort.Sort(sort.Reverse(events))

	data := ""

	for _, e := range events {
		data += fmt.Sprintf("%s: %s\n", e.CreatedAt.Format(time.RFC3339), e.Message)
	}

	RenderText(rw, data)
}
Ejemplo n.º 27
0
func ServiceUnlink(rw http.ResponseWriter, r *http.Request) {
	log := servicesLogger("unlink").Start()

	vars := mux.Vars(r)

	app := vars["app"]
	name := vars["name"]

	err := models.UnlinkService(app, name)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
	}

	RenderText(rw, "ok")
}
Ejemplo n.º 28
0
func ServiceShow(rw http.ResponseWriter, r *http.Request) {
	log := servicesLogger("show").Start()

	name := mux.Vars(r)["service"]

	service, err := models.GetServiceFromName(name)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	// sort.Sort(services)

	RenderTemplate(rw, "service", service)
}
Ejemplo n.º 29
0
func ServiceLink(rw http.ResponseWriter, r *http.Request) {
	log := servicesLogger("link").Start()

	vars := mux.Vars(r)

	app := vars["app"]
	name := GetForm(r, "name")
	stack := GetForm(r, "stack")

	err := models.LinkService(app, name, stack)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
	}

	Redirect(rw, r, fmt.Sprintf("/apps/%s", app))
}
Ejemplo n.º 30
0
func AppReleases(rw http.ResponseWriter, r *http.Request) {
	log := appsLogger("releases").Start()

	vars := mux.Vars(r)
	app := vars["app"]

	l := map[string]string{
		"id":      r.URL.Query().Get("id"),
		"created": r.URL.Query().Get("created"),
	}

	a, err := models.GetApp(app)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	releases, err := models.ListReleases(app, l)

	if err != nil {
		helpers.Error(log, err)
		RenderError(rw, err)
		return
	}

	params := map[string]interface{}{
		"App":      a,
		"Releases": releases,
	}

	if len(releases) > 0 {
		params["Last"] = releases[len(releases)-1]
	}

	switch r.Header.Get("Content-Type") {
	case "application/json":
		RenderJson(rw, releases)
	default:
		RenderPartial(rw, "app", "releases", params)
	}
}