diff --git a/garbage-collect/handler.go b/garbage-collect/handler.go index 39107e8a6..549ee72f7 100644 --- a/garbage-collect/handler.go +++ b/garbage-collect/handler.go @@ -9,7 +9,6 @@ import ( "net/http" "os" "strings" - "time" "github.com/alexellis/hmac" "github.com/openfaas/openfaas-cloud/sdk" @@ -45,7 +44,12 @@ func Handle(req []byte) string { log.Fatal(err) } - fmt.Printf("Functions owned by %s:\n %s", owner, deployedFunctions) + deployedList := "" + for _, fn := range deployedFunctions { + deployedList += fn.GetOwner() + "/" + fn.GetRepo() + ", " + } + + log.Printf("Functions owned by %s:\n %s", owner, strings.Trim(deployedList, ", ")) deleted := 0 for _, fn := range deployedFunctions { @@ -110,11 +114,8 @@ func included(fn *openFaaSFunction, owner string, functionStack []string) bool { func deleteFunction(target, gatewayURL string) error { var err error - fmt.Println("Delete ", target) + log.Printf("Deleting: %s", target) - c := http.Client{ - Timeout: time.Second * 3, - } delReq := struct { FunctionName string }{ @@ -127,19 +128,21 @@ func deleteFunction(target, gatewayURL string) error { addAuthErr := sdk.AddBasicAuth(httpReq) if addAuthErr != nil { - log.Printf("Basic auth error %s", addAuthErr) + log.Printf("Basic auth error: %s", addAuthErr) + return addAuthErr } - response, err := c.Do(httpReq) + response, err := http.DefaultClient.Do(httpReq) if err == nil { - defer response.Body.Close() if response.Body != nil { + defer response.Body.Close() + bodyBytes, bErr := ioutil.ReadAll(response.Body) if bErr != nil { log.Fatal(bErr) } - log.Println(string(bodyBytes)) + log.Println("Delete response:", string(bodyBytes)) } } @@ -150,18 +153,14 @@ func listFunctions(owner, gatewayURL string) ([]openFaaSFunction, error) { var err error - c := http.Client{ - Timeout: time.Second * 3, - } - request, _ := http.NewRequest(http.MethodGet, gatewayURL+"/function/list-functions?user="+owner, nil) - response, err := c.Do(request) + response, err := http.DefaultClient.Do(request) if err == nil { - defer response.Body.Close() - if response.Body != nil { + defer response.Body.Close() + bodyBytes, bErr := ioutil.ReadAll(response.Body) if bErr != nil { log.Fatal(bErr) diff --git a/git-tar/Dockerfile b/git-tar/Dockerfile index 7197d07bf..35d9fcd9b 100644 --- a/git-tar/Dockerfile +++ b/git-tar/Dockerfile @@ -1,14 +1,12 @@ -FROM golang:1.11-alpine3.10 as build +FROM openfaas/faas-cli:0.11.8 as faas-cli +FROM openfaas/classic-watchdog:0.18.1 as watchdog +FROM golang:1.13-alpine3.11 as build ENV CGO_ENABLED=0 +ENV GO111MODULE=off -RUN apk --no-cache add curl \ - && echo "Pulling binaries from GitHub." \ - && curl -sSLf https://github.com/openfaas/faas/releases/download/0.18.1/fwatchdog > /usr/bin/fwatchdog \ - && curl -sSLf https://github.com/openfaas/faas-cli/releases/download/0.9.3/faas-cli > /usr/local/bin/faas-cli \ - && chmod +x /usr/bin/fwatchdog \ - && chmod +x /usr/local/bin/faas-cli \ - && apk del curl --no-cache +COPY --from=watchdog /fwatchdog /usr/bin/ +COPY --from=faas-cli /usr/bin/faas-cli /usr/bin/ WORKDIR /go/src/handler COPY . . @@ -21,7 +19,7 @@ RUN CGO_ENABLED=0 GOOS=linux \ go build --ldflags "-s -w" -a -installsuffix cgo -o handler . && \ go test $(go list ./... | grep -v /vendor/) -cover -FROM alpine:3.10 +FROM alpine:3.11 as ship RUN apk --no-cache add \ ca-certificates \ @@ -40,7 +38,7 @@ WORKDIR /home/app COPY --from=build /go/src/handler/handler . COPY --from=build /usr/bin/fwatchdog /usr/bin/fwatchdog -COPY --from=build /usr/local/bin/faas-cli /usr/local/bin/faas-cli +COPY --from=build /usr/bin/faas-cli /usr/local/bin/faas-cli RUN chmod 777 /tmp diff --git a/git-tar/function/handler.go b/git-tar/function/handler.go index 5466d573b..11ac9f390 100644 --- a/git-tar/function/handler.go +++ b/git-tar/function/handler.go @@ -93,7 +93,8 @@ func Handle(req []byte) []byte { os.Exit(-1) } - clonePath, err := clone(pushEvent) + fetcher := GitRepoFetcher{} + clonePath, err := clone(fetcher, pushEvent) if err != nil { log.Println("Clone ", err.Error()) status.AddStatus(sdk.StatusFailure, "clone error : "+err.Error(), sdk.StackContext) @@ -218,7 +219,7 @@ func Handle(req []byte) []byte { tarMsg += fmt.Sprintf("%s @ %s, ", tar.functionName, tar.imageName) } - deploymentMessage := fmt.Sprintf("Deployed: %s. Took %s", strings.TrimRight(tarMsg, ", "), completed.String()) + deploymentMessage := fmt.Sprintf("Deployed: %s, time taken: %.2fs", strings.TrimRight(tarMsg, ", "), completed.Seconds()) auditEvent := sdk.AuditEvent{ Message: deploymentMessage, @@ -228,7 +229,7 @@ func Handle(req []byte) []byte { } sdk.PostAudit(auditEvent) - return []byte(deploymentMessage) + return []byte(deploymentMessage + "\n") } func collect(pushEvent sdk.PushEvent, stack *stack.Services) error { diff --git a/git-tar/function/ops.go b/git-tar/function/ops.go index 028d99cfc..9309bc4fe 100644 --- a/git-tar/function/ops.go +++ b/git-tar/function/ops.go @@ -273,10 +273,17 @@ func getRepositoryURL(e sdk.PushEvent, authToken tokener) (string, error) { return cu, nil } -func clone(pushEvent sdk.PushEvent) (string, error) { +func clone(fetcher RepoFetcher, pushEvent sdk.PushEvent) (string, error) { workDir := os.TempDir() destPath := path.Join(workDir, path.Join(pushEvent.Repository.Owner.Login, pushEvent.Repository.Name)) + if len(pushEvent.Repository.Owner.Login) == 0 { + return "", fmt.Errorf("login must be specified") + } + if len(pushEvent.Repository.Name) == 0 { + return "", fmt.Errorf("repo name must be specified") + } + if _, err := os.Stat(destPath); err == nil { truncateErr := os.RemoveAll(destPath) if truncateErr != nil { @@ -314,25 +321,45 @@ func clone(pushEvent sdk.PushEvent) (string, error) { } } - git := exec.Command("git", "clone", cloneURL) - git.Dir = path.Join(workDir, pushEvent.Repository.Owner.Login) - log.Println(git.Dir) - err = git.Start() + fetcher.Clone(cloneURL, path.Join(workDir, pushEvent.Repository.Owner.Login)) + fetcher.Checkout(pushEvent.AfterCommitID, destPath) + + return destPath, err +} + +type RepoFetcher interface { + Clone(url, path string) error + Checkout(commitID, path string) error +} + +type GitRepoFetcher struct { +} + +func (c GitRepoFetcher) Clone(url, path string) error { + git := exec.Command("git", "clone", url) + git.Dir = path + log.Printf("Cloning %s to %s", url, path) + + err := git.Start() if err != nil { - return "", fmt.Errorf("Cannot start git: %t", err) + return fmt.Errorf("Cannot start git: %t", err) } - err = git.Wait() + return git.Wait() +} + +func (c GitRepoFetcher) Checkout(commitID, path string) error { + git := exec.Command("git", "checkout", commitID) + git.Dir = path + log.Printf("Checking out SHA: %s to %s", commitID, path) - git = exec.Command("git", "checkout", pushEvent.AfterCommitID) - git.Dir = destPath - err = git.Start() + err := git.Start() if err != nil { - return "", fmt.Errorf("Cannot start git checkout: %t", err) + return fmt.Errorf("Cannot start git checkout: %t", err) } - err = git.Wait() - return destPath, err + err = git.Wait() + return err } func deploy(tars []tarEntry, pushEvent sdk.PushEvent, stack *stack.Services, status *sdk.Status, payloadSecret string) error { diff --git a/git-tar/function/ops_test.go b/git-tar/function/ops_test.go index 4df51267c..90a4add0b 100644 --- a/git-tar/function/ops_test.go +++ b/git-tar/function/ops_test.go @@ -356,3 +356,93 @@ func mockTempTemplatesDir(files []string, directory string) (string, error) { } return templatesDir, nil } + +func Test_Clone(t *testing.T) { + fetcher := FakeFetcher{} + ev := sdk.PushEvent{ + Repository: sdk.PushEventRepository{ + Owner: sdk.Owner{ + Login: "alexellis", + }, + Name: "test-repo", + }, + } + + path, err := clone(fetcher, ev) + if err != nil { + t.Error(err) + t.Fail() + } + + wantPathSuffix := ev.Repository.Owner.Login + "-" + ev.Repository.Name + if strings.HasSuffix(path, wantPathSuffix) { + t.Errorf("want suffix: %s, path was: %s", wantPathSuffix, path) + } + + if _, err := os.Stat(path); err != nil { + t.Errorf("repo not cloned to: %s", path) + } + + defer os.RemoveAll(path) +} + +func Test_CloneErrorsWithEmptyOwner(t *testing.T) { + fetcher := FakeFetcher{} + ev := sdk.PushEvent{ + Repository: sdk.PushEventRepository{ + Owner: sdk.Owner{ + Login: "", + }, + Name: "test-repo", + }, + } + + _, err := clone(fetcher, ev) + if err == nil { + t.Error("no login should throw an error") + t.Fail() + return + } + + wantErr := "login must be specified" + if err.Error() != wantErr { + t.Errorf("want err: %q, got %q", wantErr, err.Error()) + t.Fail() + } +} + +func Test_CloneErrorsWithEmptyRepoName(t *testing.T) { + fetcher := FakeFetcher{} + ev := sdk.PushEvent{ + Repository: sdk.PushEventRepository{ + Owner: sdk.Owner{ + Login: "alexellis", + }, + Name: "", + }, + } + + _, err := clone(fetcher, ev) + if err == nil { + t.Error("no repo name should throw an error") + t.Fail() + return + } + + wantErr := "repo name must be specified" + if err.Error() != wantErr { + t.Errorf("want err: %q, got %q", wantErr, err.Error()) + t.Fail() + } +} + +type FakeFetcher struct { +} + +func (c FakeFetcher) Clone(url, path string) error { + return os.MkdirAll(path, 0700) +} + +func (c FakeFetcher) Checkout(commitID, path string) error { + return os.MkdirAll(path, 0700) +} diff --git a/github-event/handler.go b/github-event/handler.go index b779cb013..873f497cc 100644 --- a/github-event/handler.go +++ b/github-event/handler.go @@ -116,10 +116,11 @@ func Handle(req []byte) string { "Content-Type": "application/json", } - body, statusCode, err := forward(req, "github-push", headers) + forwardTo := "github-push" + body, statusCode, err := forward(req, forwardTo, headers) if statusCode == http.StatusOK { - return fmt.Sprintf("Forwarded to function: %d, %s", statusCode, body) + return fmt.Sprintf("[%s]: %d, %s", forwardTo, statusCode, body) } if err != nil { @@ -318,9 +319,11 @@ func forward(req []byte, function string, headers map[string]string) (string, in if res.Body != nil { defer res.Body.Close() } + body, _ := ioutil.ReadAll(res.Body) - if res.StatusCode != http.StatusOK { + if res.StatusCode != http.StatusOK && + res.StatusCode != http.StatusAccepted { err = fmt.Errorf(string(body)) } diff --git a/github-push/handler.go b/github-push/handler.go index 9e1936ada..625ac89bc 100644 --- a/github-push/handler.go +++ b/github-push/handler.go @@ -104,7 +104,11 @@ func Handle(req []byte) string { sdk.PostAudit(auditEvent) - return fmt.Sprintf("Push - %v, git-tar status: %d\n", pushEvent, statusCode) + return fmt.Sprintf("Push: %s\n, git-tar: %d\n", formatPushEvent(pushEvent), statusCode) +} + +func formatPushEvent(pushEvent sdk.PushEvent) string { + return pushEvent.Repository.Owner.Login + "/" + pushEvent.Repository.Name + "@" + pushEvent.Ref + "#" + pushEvent.Ref + " [" + pushEvent.Repository.CloneURL + "]" } func postEvent(pushEvent sdk.PushEvent) (int, error) { diff --git a/stack.yml b/stack.yml index eaba9a2b3..d87cf2033 100644 --- a/stack.yml +++ b/stack.yml @@ -6,7 +6,7 @@ functions: system-github-event: lang: go handler: ./github-event - image: functions/github-event:0.9.1 + image: functions/github-event:0.9.2 labels: openfaas-cloud: "1" role: openfaas-system @@ -27,7 +27,7 @@ functions: github-push: lang: go handler: ./github-push - image: functions/github-push:0.7.4 + image: functions/github-push:0.7.5 labels: openfaas-cloud: "1" role: openfaas-system @@ -49,7 +49,7 @@ functions: git-tar: lang: dockerfile handler: ./git-tar - image: functions/of-git-tar:0.17.1 + image: functions/of-git-tar:0.17.2 labels: openfaas-cloud: "1" role: openfaas-system @@ -95,7 +95,7 @@ functions: garbage-collect: lang: go handler: ./garbage-collect - image: functions/garbage-collect:0.4.6 + image: functions/garbage-collect:0.4.7 labels: openfaas-cloud: "1" role: openfaas-system