Exemplo n.º 1
0
// Validator for application creation
func (f ApplicationCreateForm) Validate() *errors.Errors {
	if errs := validator.Validate(&f); errs != nil {
		return errs
	}

	app, err := ApplicationMapper.FetchOne(f.Name)
	if err != nil {
		panic(err)
	}

	if app != nil {
		return errors.New(errors.Error{
			Label: "duplicate_name",
			Field: "name",
			Text:  "Duplicate application name: " + f.Name,
		})
	}

	for _, d := range f.Deps {
		depApp, err := ApplicationMapper.FetchOne(d)
		if err != nil {
			panic(err)
		}

		if depApp == nil {
			return errors.New(errors.Error{
				Label: "invalid_dep",
				Field: "deps",
				Text:  "Invalid dependence: " + d,
			})
		}
	}

	return nil
}
Exemplo n.º 2
0
func (d *fsDriver) store(dir string, name string, data []byte) (string, error) {

	targetDir := path.Join(d.BaseDir, dir)

	// Check for target directory
	if err := os.MkdirAll(targetDir, 0755); err != nil {
		log.Println("ressources/file/fs: failed to create directory:", err)
		return "", errors.New(errors.Error{
			Label: "internal_error",
			Field: "file",
			Text:  err.Error(),
		})
	}

	targetFile := path.Join(targetDir, name)

	if err := ioutil.WriteFile(targetFile, data, 0644); err != nil {
		return "", errors.New(errors.Error{
			Label: "internal_error",
			Field: "file",
			Text:  err.Error(),
		})
	}

	return targetFile, nil
}
Exemplo n.º 3
0
func (b *Build) AttachFile(f multipart.File) error {

	data, err := ioutil.ReadAll(f)
	if err != nil {
		return errors.New(errors.Error{
			Label: "internal_error",
			Field: "file",
			Text:  err.Error(),
		})
	}

	if contentType := http.DetectContentType(data); contentType != "application/zip" {
		return errors.New(errors.Error{
			Label: "internal_error",
			Field: "file",
			Text:  "Bad content-type, want application/zip have " + contentType,
		})
	}

	if b.FilePath, err = file.Store("builds", b.Id.Hex()+".zip", data); err != nil {
		return err
	}

	// Check ident
	if err := b.readRuntimeConfig(data); err != nil {
		return err
	}

	return nil
}
Exemplo n.º 4
0
// Validator for application creation
func (f AlertPolicyForm) Validate() *errors.Errors {
	if errs := validator.Validate(&f); errs != nil {
		return errs
	}

	if len(f.AlertGroups) == 0 {
		return errors.New(errors.Error{
			Label: "unknown_alert_groups",
			Field: "alert_groups",
			Text:  "Missing alert groups",
		})
	}

	for _, agName := range f.AlertGroups {

		ag, err := AlertGroupMapper.FetchOne(agName)
		if err != nil {
			panic(err)
		}

		if ag == nil {
			return errors.New(errors.Error{
				Label: "unknown_alert_groups",
				Field: "alert_groups",
				Text:  "Unknown alert group: " + agName,
			})
		}
	}

	return nil
}
Exemplo n.º 5
0
func (pc *BuildController) postBuild(c *gin.Context) {

	app, err := pc.getApp(c)
	if err != nil {
		c.JSON(http.StatusNotFound, err)
		return
	}

	commitHash := c.DefaultPostForm("commit_hash", "")

	build := models.BuildMapper.Create(app, commitHash)

	file, _, err := c.Request.FormFile("file")
	if err != nil {
		c.JSON(http.StatusBadRequest, errors.New(errors.Error{
			Label: "invalid_file",
			Field: "file",
			Text:  "Missing zip file",
		}))
		return
	}
	defer file.Close()

	if err := build.AttachFile(file); err != nil {
		c.JSON(http.StatusBadRequest, errors.New(errors.Error{
			Label: "invalid_file",
			Field: "file",
			Text:  err.Error(),
		}))
		return
	}

	if err := models.BuildMapper.Save(build); err != nil {
		panic(err)
	}

	if c.DefaultQuery("deploy", "0") == "1" {

		resp, err := http.Post(fmt.Sprintf("http://localhost:8080/api/apps/%s/builds/%s/deploy", app.Id.Hex(), build.Id.Hex()), "json/application", nil)
		if err != nil {
			c.JSON(http.StatusBadRequest, errors.New(errors.Error{
				Label: "deploy_issue",
				Field: "deploy",
				Text:  err.Error(),
			}))
			return
		}
		resp.Body.Close()
	}

	c.JSON(http.StatusCreated, build)
}
Exemplo n.º 6
0
func FormatErrors(errs error) *errors.Errors {

	e := errors.New()

	m := govalidator.ErrorsByField(errs)
	for key, value := range m {

		key = govalidator.CamelCaseToUnderscore(key)
		e.Add(errors.Error{
			Label: fmt.Sprintf("invalid_%s", key),
			Field: key,
			Text:  value,
		})
	}

	return e
}
Exemplo n.º 7
0
func (ctl *ApplicationController) postAddApplicationDockerAction(c *gin.Context) {

	var form models.ApplicationCreateForm
	if err := c.Bind(&form); err != nil {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	if err := form.Validate(); err != nil {
		fmt.Println(err.Errors)
		c.HTML(http.StatusOK, "application_add_docker.html", map[string]interface{}{
			"errors": err.Errors,
			"form":   form,
		})
		return
	}

	application := models.ApplicationMapper.Create(&form)

	if application.Type != models.APPLICATION_TYPE_DOCKER {
		c.HTML(http.StatusOK, "application_add_docker.html", map[string]interface{}{
			"errors": errors.New(errors.Error{
				Label: "invalid_type",
				Field: "type",
				Text:  "Invalid application type",
			}).Errors,
			"form": form,
		})
		return
	}

	build := models.BuildMapper.CreateDocker(application, &form.ApplicationDockerForm)

	if err := models.BuildMapper.Save(build); err != nil {
		panic(err)
	}

	if err := models.ApplicationMapper.Save(application); err != nil {
		panic(err)
	}

	c.Redirect(http.StatusFound, "/")
}
Exemplo n.º 8
0
func (pc *BuildController) getApp(c *gin.Context) (*models.Application, error) {

	id := c.Param("id")

	app, err := models.ApplicationMapper.FetchOne(id)
	if err != nil {
		panic(err)
	}

	if app == nil {
		return nil, errors.New(errors.Error{
			Label: "invalid_application",
			Field: "id",
			Text:  "Invalid application ID in URL",
		})
	}

	return app, nil
}
Exemplo n.º 9
0
// Validator for application creation
func (f AccessCreateForm) Validate() *errors.Errors {
	if errs := validator.Validate(&f); errs != nil {
		return errs
	}

	a, err := AccessMapper.FetchOne(f.Type)
	if err != nil {
		panic(err)
	}

	if a != nil {
		return errors.New(errors.Error{
			Label: "duplicate_type",
			Field: "type",
			Text:  "Duplicate access type: " + f.Type,
		})
	}

	return nil
}
Exemplo n.º 10
0
func (pc *BuildController) postBuildDeploy(c *gin.Context) {

	buildId := c.Param("build_id")

	app, err := pc.getApp(c)
	if err != nil {
		c.JSON(http.StatusNotFound, err)
		return
	}

	build, err := models.BuildMapper.FetchOne(app, buildId)
	if err != nil {
		c.JSON(http.StatusNotFound, err)
		return
	}

	if build == nil {
		c.JSON(http.StatusNotFound, errors.New(errors.Error{
			Label: "invalid_build",
			Field: "build_id",
			Text:  "Invalid build ID in URL",
		}))
		return
	}

	depl := models.DeploymentMapper.Create(app, build)

	if err := models.DeploymentMapper.Save(depl); err != nil {
		panic(err)
	}

	app.CurrentDeploymentId = depl.Id.Hex()
	if err := models.ApplicationMapper.Update(app); err != nil {
		panic(err)
	}

	go depl.Run()

	c.JSON(http.StatusOK, depl)
}
Exemplo n.º 11
0
func (bm *buildMapper) FetchOne(app *Application, buildId string) (*Build, error) {

	if !bson.IsObjectIdHex(buildId) {

		return nil, errors.New(errors.Error{
			Label: "invalid_build_id",
			Field: "build_id",
			Text:  "Invalid build id hex",
		})
	}

	col := C(buildCollection)
	defer col.Database.Session.Close()

	build := new(Build)
	if err := col.Find(bson.M{"application_id": app.Id, "_id": bson.ObjectIdHex(buildId)}).One(build); err != nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}

	return build, nil
}
Exemplo n.º 12
0
func (dm *deploymentMapper) FetchOne(app *Application, deployId string) (*Deployment, error) {

	if !bson.IsObjectIdHex(deployId) {

		return nil, errors.New(errors.Error{
			Label: "invalid_deploy_id",
			Field: "deploy_id",
			Text:  "Invalid deploy id hex",
		})
	}

	col := C(deploymentCollection)
	defer col.Database.Session.Close()

	deploy := new(Deployment)
	if err := col.Find(bson.M{"application_id": app.Id, "_id": bson.ObjectIdHex(deployId)}).One(deploy); err != nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}

	return deploy, nil
}
Exemplo n.º 13
0
func (lfm *logfileMapper) FetchOne(app *Application, logfileId string) (*Logfile, error) {

	if !bson.IsObjectIdHex(logfileId) {

		return nil, errors.New(errors.Error{
			Label: "invalid_logfile_id",
			Field: "logfile_id",
			Text:  "Invalid logfile id hex",
		})
	}

	col := C(logfileCollection)
	defer col.Database.Session.Close()

	logfile := new(Logfile)
	if err := col.Find(bson.M{"application_id": app.Id, "_id": bson.ObjectIdHex(logfileId)}).One(logfile); err != nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}

	return logfile, nil
}
Exemplo n.º 14
0
func (ctl *ApplicationController) postEditApplicationAction(c *gin.Context) {

	id := c.Param("id")

	// Get the application
	application := ctl.getApplication(c, id)
	if application == nil {
		return
	}

	// Hydrate the form with the request values
	var form models.ApplicationUpdateForm
	if err := c.Bind(&form); err != nil {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	// Check the form values
	if err := form.Validate(application); err != nil {
		fmt.Println(err.Errors)
		c.HTML(http.StatusOK, "application_edit.html", map[string]interface{}{
			"errors":      err.Errors,
			"form":        form,
			"application": application,
		})
		return
	}

	// Update the application
	application.Update(&form)

	if application.Type == models.APPLICATION_TYPE_SERVICE {

		if len(form.Packages) == 0 {
			c.HTML(http.StatusOK, "application_edit.html", map[string]interface{}{
				"errors": errors.New(errors.Error{
					Label: "invalid_packages",
					Field: "packages",
					Text:  "Invalid packages count, min 1",
				}).Errors,
				"form":        form,
				"application": application,
			})
			return
		}

		build, err := models.BuildMapper.FetchLast(application)
		if err != nil {
			panic(err)
		}

		if build == nil {
			panic("No last build for service: " + application.Name)
		}

		build.RuntimeCfg.Dependencies = build.RuntimeCfg.Dependencies.FromString(form.Packages)

		if err := models.BuildMapper.Update(build); err != nil {
			panic(err)
		}
	} else if application.Type == models.APPLICATION_TYPE_DOCKER {
		if len(form.Image) == 0 {
			c.HTML(http.StatusOK, "application_edit.html", map[string]interface{}{
				"errors": errors.New(errors.Error{
					Label: "invalid_image",
					Field: "image",
					Text:  "Invalid image",
				}).Errors,
				"form":        form,
				"application": application,
			})
			return
		}

		build, err := models.BuildMapper.FetchLast(application)
		if err != nil {
			panic(err)
		}

		if build == nil {
			panic("No last build for service: " + application.Name)
		}

		tmpBuild := models.BuildMapper.CreateDocker(application, &form.ApplicationDockerForm)
		build.RuntimeCfg.Docker = tmpBuild.RuntimeCfg.Docker

		if err := models.BuildMapper.Update(build); err != nil {
			panic(err)
		}

	}

	// Save the application
	if err := models.ApplicationMapper.Update(application); err != nil {
		panic(err)
	}

	c.Redirect(http.StatusFound, "/application/show/"+application.Id.Hex())
}