Skip to content

Commit

Permalink
Background fn (#7)
Browse files Browse the repository at this point in the history
* Update with pubsub / bucket

* Make logging work

* Fix logging

* Update with working CLI

* Fix build command
  • Loading branch information
MartinSahlen committed Mar 28, 2017
1 parent 33b3c2e commit 92cf21e
Show file tree
Hide file tree
Showing 1,085 changed files with 2,165,552 additions and 4,671 deletions.
4 changes: 0 additions & 4 deletions .gitignore
@@ -1,6 +1,2 @@
target
.DS_Store
tool/
client/
swagger/
app/
79 changes: 0 additions & 79 deletions cmd.go

This file was deleted.

114 changes: 114 additions & 0 deletions cmd/deploy.go
@@ -0,0 +1,114 @@
package cmd

import (
"io/ioutil"
"log"
"os"
"os/exec"
"strings"

t "github.com/MartinSahlen/go-cloud-fn/template"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
)

// deployCmd represents the deploy command
var deployCmd = &cobra.Command{
Use: "deploy <function-name>",
Short: "Deploy your cloud function",
Long: `This command lets you deploy your function with a given
set of parameters.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
log.Println("You need to provide the function name as first argument.")
return
}
if !emulator && stageBucket == "" {
log.Println("Production environment needs stage-bucket")
return
}
if !triggerHTTP && triggerBucket == "" && triggerTopic == "" {
log.Println("You need to set either trigger-http, trigger-bucket or trigger-topic")
return
}
functionName := args[0]
targetDir := "target/" + uuid.NewV4().String() + "/"
index, err := t.GenerateIndex(t.IndexTemplateData{
FunctionName: functionName,
TargetDir: targetDir,
TriggerHTTP: triggerHTTP,
})
if err != nil {
log.Println(err)
return
}
err = os.MkdirAll(targetDir, os.ModePerm)
if err != nil {
log.Println(err)
return
}
err = ioutil.WriteFile(targetDir+"index.js", []byte(index), os.ModePerm)
if err != nil {
log.Println(err)
return
}

//Set the correct trigger (only one)
trigger := []string{}
if triggerHTTP {
trigger = append(trigger, "--trigger-http")
} else if triggerBucket != "" {
trigger = append(trigger, "--trigger-bucket")
trigger = append(trigger, triggerBucket)
} else if triggerTopic != "" {
trigger = append(trigger, "--trigger-topic")
trigger = append(trigger, triggerTopic)
}

deployArguments := []string{
"gcloud beta functions deploy",
functionName,
"--local-path", targetDir,
"--timeout", timeout,
}
//Use functions if it's emulator we're deploying to.
var buildCmd string
if emulator {
deployArguments[0] = "functions deploy"
buildCmd = "go build -o "
} else {
buildCmd = "GOOS=linux go build -o "
deployArguments = append(
deployArguments,
"--memory", memory,
"--stage-bucket", stageBucket)
}

compile, err := exec.Command("sh", "-c", buildCmd+targetDir+functionName).CombinedOutput()
if err != nil {
log.Println(compile)
return
}
deploy, _ := exec.Command("sh", "-c", strings.Join(append(deployArguments, trigger...), " ")).CombinedOutput()
log.Println(string(deploy))
},
}

var emulator bool
var stageBucket string
var triggerHTTP bool
var triggerBucket string
var triggerTopic string
var memory string
var timeout string

func init() {
RootCmd.AddCommand(deployCmd)
deployCmd.Flags().BoolVarP(&emulator, "emulator", "e", false, "Deploy to emulator")
deployCmd.Flags().StringVarP(&stageBucket, "stage-bucket", "s", "", "Set GCS bucket to upload zip bundle")
deployCmd.Flags().BoolVarP(&triggerHTTP, "trigger-http", "j", false, "Set function to trigger on HTTP event")
deployCmd.Flags().StringVarP(&triggerBucket, "trigger-bucket", "b", "", "Set function to trigger on this GCS bucket event")
deployCmd.Flags().StringVarP(&triggerTopic, "trigger-topic", "t", "", "Set function to trigger on this Pubsub topic")
deployCmd.Flags().StringVarP(&memory, "memory", "m", "1024MB", "Set function memory [MAX 2048MB]")
deployCmd.Flags().StringVarP(&timeout, "timeout", "o", "540s", "Set function timeout [MAX 540s]")
}
24 changes: 24 additions & 0 deletions cmd/root.go
@@ -0,0 +1,24 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "go-cloud-fn",
Short: "Write google cloud functions in go",
Long: `go-cloud-fn lets you write and deploy google cloud functions in go.`,
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
21 changes: 17 additions & 4 deletions lock.json
@@ -1,12 +1,25 @@
{
"memo": "",
"memo": "b8d71edfbc4289396a962935d96aa3e544bdef0ce9eb55e27e154807306baf62",
"projects": [
{
"name": "github.com/docopt/docopt-go",
"name": "golang.org/x/net",
"branch": "master",
"revision": "784ddc588536785e7299f7272f39101f7faccc3f",
"revision": "6c23252515492caf9b228a9d5cabcdbde29f7f82",
"packages": [
"."
"context",
"context/ctxhttp"
]
},
{
"name": "google.golang.org/api",
"branch": "master",
"revision": "48e49d1645e228d1c50c3d54fb476b2224477303",
"packages": [
"gensupport",
"googleapi",
"googleapi/internal/uritemplates",
"pubsub/v1beta2",
"storage/v1beta2"
]
}
]
Expand Down
84 changes: 16 additions & 68 deletions main.go
@@ -1,73 +1,21 @@
package main
// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import (
"log"
"os/exec"
package main

docopt "github.com/docopt/docopt-go"
)
import "github.com/MartinSahlen/go-cloud-fn/cmd"

func main() {
usage := `go-cloud-fn.
Usage:
go-cloud-fn build <function-name>
go-cloud-fn build <function-name> --production
go-cloud-fn build <function-name> --development
go-cloud-fn deploy <function-name> <stage-bucket>
Options:
-h --help Show this screen.
-p --production Deploy for production
-d --development Deploy for local development
--version Show version.`

arguments, err := docopt.Parse(usage, nil, true, "go-cloud-fn 0.0 Pre-Alpha", false)

if err != nil {
panic(err)
}

functionName := arguments["<function-name>"].(string)

//build := arguments["build"].(bool)
//build := arguments["deploy"].(bool)

conf := config{
FunctionName: functionName,
TargetDir: "target",
TriggerHTTP: true,
StageBucket: "your-bucket",
ExecutableName: functionName,
Production: true,
}

buildOut, err := parseTemplate(buildSh, conf)
if err != nil {
panic(err)
} /*
httpOut, err := parseTemplate(httpJs, conf)
if err != nil {
panic(err)
}
deployOut, err := parseTemplate(deploySh, conf)
if err != nil {
panic(err)
}*/
log.Println(buildOut)
out, err := exec.Command(buildOut).Output()
if err != nil {
panic(err)
}
log.Println(out)
/*
err = ioutil.WriteFile("build.sh", []byte(buildOut), os.ModePerm)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(conf.TargetDir+"/index.js", []byte(httpOut), os.ModePerm)
if err != nil {
panic(err)
}*/
cmd.Execute()
}
25 changes: 24 additions & 1 deletion shim/bucket.go
@@ -1,3 +1,26 @@
package shim

func HandleBucketEvent() {}
import (
"encoding/json"
"io/ioutil"
"log"
"os"

storage "google.golang.org/api/storage/v1beta2"
)

type BucketHandlerFunc func(object storage.Object)

func HandleBucketEvent(h BucketHandlerFunc) {
stdin, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}

var object storage.Object
err = json.Unmarshal(stdin, &object)
if err != nil {
log.Fatal(err)
}
h(object)
}

0 comments on commit 92cf21e

Please sign in to comment.