Exemplo n.º 1
0
func actionCreate(c *cli.Context) {
	codePath := c.Args().First()
	if codePath == "" {
		codePath = cwd
	}
	if codePath == "" {
		// TODO Warning?
	}
	codePath = codePath

	// Check required params
	requiredAttrs := map[string]string{
		"name":    c.String("name"),
		"runtime": c.String("runtime"),
	}
	for a, v := range requiredAttrs {
		if v == "" {
			logger.Fatalf("%q is required", a)
		}
	}

	envVars := c.StringSlice("env")
	for _, e := range envVars {
		if _, _, err := envLib.ParseLine(e); err != nil {
			logger.Fatalf("Invalid environment variable %q. Pattern must be VARIABLE=VALUE", e)
		}
	}

	timeout := checkTimeoutOrFatal(c)
	function := types.Function{
		Name:        c.String("name"),
		Runtime:     c.String("runtime"),
		Handler:     c.String("handler"),
		Description: c.String("description"),
		Timeout:     timeout.Nanoseconds() / 1000 / 1000,
		Memory:      c.Int("memory"),
		Instances:   c.Int("instances"),
		Env:         envVars,
	}

	created, err := api.FunctionCreate(function)
	if err != nil {
		logger.Fatalf("Error while creating function: %s", err)
	} else {
		logger.Printf("Function %q (%q) created.\n", created.Name, created.ID[:shortIDLen])
	}
}
Exemplo n.º 2
0
func actionEnvSet(c *cli.Context) {
	fid := checkFidOrFatal(c)

	if !c.Args().Present() {
		// Inform user to type vars
		logger.Println("You must inform environment variables in format VAR=VALUE")
		return
	}

	vars := make([]string, len(c.Args()))
	for _, a := range c.Args() {
		if _, _, err := envLib.ParseLine(a); err != nil {
			logger.Fatalf("Invalid environment variable %q. Pattern must be VARIABLE=VALUE", err)
		}
		vars = append(vars, a)
	}

	if err := api.FunctionEnvSet(fid, vars); err != nil {
		logger.Fatalf("Error while settings environment variables: %s", err)
	} else {
		logger.Println("Environment variables was set.")
	}
}
Exemplo n.º 3
0
func (s *Server) functionCreate(w http.ResponseWriter, r *http.Request) {
	var f types.Function
	s.parseBody(w, r, &f)

	// Validate function
	if f.Name == "" {
		exc := fmt.Errorf("[functionCreate] Attribute \"name\" is required")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}
	if f.Runtime == "" {
		exc := fmt.Errorf("[functionCreate] Attribute \"runtime\" is required")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	} else {
		// Check if runtime exists
		if rt, err := s.s.Runtimes().FindByIDOrName(f.Runtime); err == store.ErrNotFound {
			exc := fmt.Errorf("[functionCreate] Runtime %q does not exists", f.Runtime)
			s.failure(w, r, http.StatusBadRequest, exc)
			return
		} else {
			f.Runtime = rt.ID
		}
	}
	if f.Timeout <= 0 {
		exc := fmt.Errorf("[functionCreate] Attribute \"timeout\" must be greater than 0")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}
	if f.Memory < 32 {
		exc := fmt.Errorf("[functionCreate] Attribute \"memory\" must be greater than 32 MB")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}
	if f.Instances <= 0 {
		exc := fmt.Errorf("[functionCreate] Attribute \"instances\" must be greater than 0")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}
	for _, e := range f.Env {
		if _, _, err := env.ParseLine(e); err != nil {
			exc := fmt.Errorf("Invalid environment variable %q. Pattern must be VARIABLE=VALUE", e)
			s.failure(w, r, http.StatusBadRequest, exc)
			return
		}
	}

	now := time.Now()
	f.ID = types.NewID()
	f.Created = now
	f.Updated = now

	created, err := s.s.Functions().Create(f)
	if err != nil {
		exc := fmt.Errorf("[functionCreate] Store error caused by: %s", err)
		s.failure(w, r, http.StatusInternalServerError, exc)
		return
	}
	logger.Printf("Function %q created with runtime %q", created.Name, created.Runtime)

	s.success(w, r, http.StatusCreated, created)
}