Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds 'faas-cli template pull' command #201

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 27 additions & 25 deletions builder/build.go
Expand Up @@ -9,49 +9,51 @@ import (
"log"
"os"
"strings"

"github.com/openfaas/faas-cli/stack"
)

// BuildImage construct Docker image from function parameters
func BuildImage(image string, handler string, functionName string, language string, nocache bool, squash bool, shrinkwrap bool) {

switch language {
case "node", "python", "ruby", "csharp", "python3", "go":
tempPath := createBuildTemplate(functionName, handler, language)
if stack.IsValidTemplate(language) {

fmt.Printf("Building: %s with %s template. Please wait..\n", image, language)
var tempPath string
if strings.ToLower(language) == "dockerfile" {

flagStr := buildFlagString(nocache, squash, os.Getenv("http_proxy"), os.Getenv("https_proxy"))
if shrinkwrap {
fmt.Printf("Nothing to do for: %s.\n", functionName)

if shrinkwrap {
fmt.Printf("%s shrink-wrapped to %s\n", functionName, tempPath)
} else {
builder := strings.Split(fmt.Sprintf("docker build %s-t %s .", flagStr, image), " ")
return
}

ExecCommand(tempPath, builder)
fmt.Printf("Image: %s built.\n", image)
}
break
case "Dockerfile", "dockerfile":
if shrinkwrap {
fmt.Printf("Nothing to do for: %s.\n", functionName)
} else {
tempPath := handler
tempPath = handler
if _, err := os.Stat(handler); err != nil {
fmt.Printf("Unable to build %s, %s is an invalid path\n", image, handler)
fmt.Printf("Image: %s not built.\n", image)

break
return
}
fmt.Printf("Building: %s with Dockerfile. Please wait..\n", image)

flagStr := buildFlagString(nocache, squash, os.Getenv("http_proxy"), os.Getenv("https_proxy"))
} else {

builder := strings.Split(fmt.Sprintf("docker build %s-t %s .", flagStr, image), " ")
fmt.Println(strings.Join(builder, " "))
ExecCommand(tempPath, builder)
fmt.Printf("Image: %s built.\n", image)
tempPath = createBuildTemplate(functionName, handler, language)
fmt.Printf("Building: %s with %s template. Please wait..\n", image, language)

if shrinkwrap {
fmt.Printf("%s shrink-wrapped to %s\n", functionName, tempPath)

return
}
}
default:

flagStr := buildFlagString(nocache, squash, os.Getenv("http_proxy"), os.Getenv("https_proxy"))
builder := strings.Split(fmt.Sprintf("docker build %s-t %s .", flagStr, image), " ")
ExecCommand(tempPath, builder)
fmt.Printf("Image: %s built.\n", image)

} else {
log.Fatalf("Language template: %s not supported. Build a custom Dockerfile instead.", language)
}
}
Expand Down
2 changes: 1 addition & 1 deletion commands/build.go
Expand Up @@ -149,7 +149,7 @@ func PullTemplates(templateUrl string) error {
if err != nil || exists == nil {
log.Println("No templates found in current directory.")

err = fetchTemplates(templateUrl)
err = fetchTemplates(templateUrl, false)
if err != nil {
log.Println("Unable to download templates from Github.")
return err
Expand Down
28 changes: 27 additions & 1 deletion commands/deploy.go
Expand Up @@ -66,7 +66,7 @@ var deployCmd = &cobra.Command{
[--handler HANDLER_DIR]
[--fprocess PROCESS]
[--env ENVVAR=VALUE ...]
[--label LABEL=VALUE ...]
[--label LABEL=VALUE ...]
[--replace=false]
[--update=false]
[--constraint PLACEMENT_CONSTRAINT ...]
Expand Down Expand Up @@ -160,6 +160,28 @@ func runDeploy(cmd *cobra.Command, args []string) {
log.Fatalln(envErr)
}

// Get FProcess to use from the ./template/template.yml, if a template is being used
if languageExistsNotDockerfile(function.Language) {
pathToTemplateYAML := "./template/" + function.Language + "/template.yml"
if _, err := os.Stat(pathToTemplateYAML); os.IsNotExist(err) {
log.Fatalln(err.Error())
return
}

var langTemplate stack.LanguageTemplate
parsedLangTemplate, err := stack.ParseYAMLForLanguageTemplate(pathToTemplateYAML)

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

if parsedLangTemplate != nil {
langTemplate = *parsedLangTemplate
function.FProcess = langTemplate.FProcess
}
}

proxy.DeployFunction(function.FProcess, services.Provider.GatewayURL, function.Name, function.Image, function.Language, replace, allEnvironment, services.Provider.Network, constraints, update, secrets, allLabels)
}
} else {
Expand Down Expand Up @@ -277,3 +299,7 @@ func compileEnvironment(envvarOpts []string, yamlEnvironment map[string]string,
functionAndStack := mergeMap(yamlEnvironment, fileEnvironment)
return mergeMap(functionAndStack, envvarArguments), nil
}

func languageExistsNotDockerfile(language string) bool {
return len(language) > 0 && strings.ToLower(language) != "dockerfile"
}