Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #597 from pzmrzy/create_image_by_url
Browse files Browse the repository at this point in the history
Add create image by url
  • Loading branch information
neosab authored Sep 7, 2018
2 parents 4fcdf02 + 9542134 commit 1db701e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
10 changes: 10 additions & 0 deletions e2e/tests/images.bats
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ load variables
batch_create_images
}

@test "Create with URL" {
run dispatch create --file=https://raw.githubusercontent.com/vmware/dispatch/master/examples/seed.yaml --work-dir=test_create_by_url
assert_success

run_with_retry "dispatch get function hello-js -o json | jq -r .status" "READY" 10 5
run_with_retry "dispatch get function hello-py -o json | jq -r .status" "READY" 10 5
run_with_retry "dispatch get function http-py -o json | jq -r .status" "READY" 10 5
run_with_retry "dispatch get function hello-ps1 -o json | jq -r .status" "READY" 10 5
}

@test "Update images" {
run dispatch update --work-dir ${BATS_TEST_DIRNAME} -f images_update.yaml
assert_success
Expand Down
64 changes: 59 additions & 5 deletions pkg/dispatchcli/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
Expand All @@ -31,6 +33,8 @@ var (
createExample = i18n.T(``)
file = i18n.T(``)
workDir = i18n.T(``)
baseURL = i18n.T(``)
isURL = false
)

// ModelAction is the function type for CLI actions
Expand Down Expand Up @@ -59,10 +63,43 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
if err != nil {
return errors.Wrapf(err, "Error reading file %s", fullPath)
}

return importBytes(out, b, actionMap, actionName)
}

func importFileWithURL(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []string, actionMap map[string]ModelAction, actionName string) error {
resp, err := http.Get(file)
if err != nil {
return err
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return importBytes(out, contents, actionMap, actionName)
}

func downloadFile(filepath string, url string) error {
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()

resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}

return nil
}

func importBytes(out io.Writer, b []byte, actionMap map[string]ModelAction, actionName string) error {

var err error
Expand Down Expand Up @@ -146,11 +183,21 @@ func importBytes(out io.Writer, b []byte, actionMap map[string]ModelAction, acti
if err := yaml.Unmarshal(doc, m); err != nil {
return errors.Wrapf(err, "Error decoding function document %s", doc)
}

if m.SourcePath != "" {
sourcePath := filepath.Join(workDir, m.SourcePath)
isDir, err := utils.IsDir(sourcePath)
if err != nil {
return err
if isURL {
url := baseURL + m.SourcePath
err = os.MkdirAll(sourcePath[:strings.LastIndex(sourcePath, "/")], 0755)
downloadFile(sourcePath, url)
if err != nil {
return err
}
} else {
return err
}
}
if isDir && m.Handler == "" {
return fmt.Errorf("error creating function %s: handler is required, source path %s is a directory", *m.Name, sourcePath)
Expand Down Expand Up @@ -319,14 +366,21 @@ func NewCmdCreate(out io.Writer, errOut io.Writer) *cobra.Command {
}

initCreateMap()
if strings.HasPrefix(file, "http://") || strings.HasPrefix(file, "https://") {
isURL = true
baseURL = file[:strings.LastIndex(file, "/")+1]
err := importFileWithURL(out, errOut, cmd, args, createMap, "Created")
CheckErr(err)
} else {
err := importFile(out, errOut, cmd, args, createMap, "Created")
CheckErr(err)
}

err := importFile(out, errOut, cmd, args, createMap, "Created")
CheckErr(err)
},
}

cmd.Flags().StringVarP(&cmdFlagApplication, "application", "a", "", "associate with an application")
cmd.Flags().StringVarP(&file, "file", "f", "", "Path to YAML file")
cmd.Flags().StringVarP(&file, "file", "f", "", "Path to YAML file or an URL")
cmd.Flags().StringVarP(&workDir, "work-dir", "w", "", "Working directory relative paths are based on")

cmd.AddCommand(NewCmdCreateBaseImage(out, errOut))
Expand Down

0 comments on commit 1db701e

Please sign in to comment.