Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Added support for custom-container (#59)
Browse files Browse the repository at this point in the history
Co-authored-by: shuai.chang <shuai.chang@alibaba-inc.com>
  • Loading branch information
shuaichang and shuai.chang authored Aug 22, 2020
1 parent f2d9014 commit ac3dd81
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 49 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ $ go install github.com/Masterminds/glide
__4. 安装依赖__
在项目根目录下,执行 `glide i -v` 进行依赖安装

__5. 构建 fcli 二进制__
```
$ go get github.com/karalabe/xgo
$ make binary
```

### 提交 pull request

__1. 将修改 push 到个人账号里的本地仓库__
Expand Down
66 changes: 50 additions & 16 deletions cmd/create_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package cmd

import (
"fmt"
"github.com/aliyun/fc-go-sdk"
"io/ioutil"
"strings"

"github.com/spf13/cobra"

"github.com/aliyun/fc-go-sdk"
"github.com/aliyun/fcli/util"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

type createFuncInputType struct {
Expand All @@ -22,6 +22,10 @@ type createFuncInputType struct {
codeFile string
codeOSSBucket string
codeOSSObject string
customContainerImage string
customContainerCommand string
customContainerArgs string
caPort int32
memory int32
timeout int32
initializationTimeout int32
Expand All @@ -44,6 +48,11 @@ func init() {
createFuncCmd.Flags().Int32VarP(&createFuncInput.initializationTimeout, "initializationTimeout", "e", 30, "timeout in seconds")
createFuncCmd.Flags().StringVarP(&createFuncInput.codeOSSBucket, "code-bucket", "b", "", "oss bucket of the code")
createFuncCmd.Flags().StringVarP(&createFuncInput.codeOSSObject, "code-object", "o", "", "oss object of the code")
createFuncCmd.Flags().StringVarP(&createFuncInput.customContainerImage, "custom-container-image", "g", "", "custom container config image")
createFuncCmd.Flags().StringVarP(&createFuncInput.customContainerCommand, "custom-container-command", "n", "", "custom container config command, e.g. [\"node\"]")
createFuncCmd.Flags().StringVarP(&createFuncInput.customContainerArgs, "custom-container-args", "r", "", "custom container config args, e.g. [\"server.js\"]")
createFuncCmd.Flags().Int32Var(&createFuncInput.caPort, "ca-port", 0, "args of custom container config")

createFuncCmd.Flags().StringVarP(
&createFuncInput.codeDir, "code-dir", "d", "",
"function code directory. If both code-file and code-dir are provided, code-file will be used.")
Expand Down Expand Up @@ -98,20 +107,29 @@ var createFuncCmd = &cobra.Command{
input.WithEnvironmentVariables(envMap)
}

if createFuncInput.codeFile != "" {
var data []byte
data, err := ioutil.ReadFile(createFuncInput.codeFile)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
input.WithCode(fc.NewCode().WithZipFile(data))
} else if createFuncInput.codeDir != "" {
input.WithCode(fc.NewCode().WithDir(createFuncInput.codeDir))
if createFuncInput.runtime == util.RuntimeCustomContainer {
input = createFunctionInputWithCustomContainerConfig(cmd.Flags(), input, createFuncInput.customContainerImage,
createFuncInput.customContainerCommand, createFuncInput.customContainerArgs)
} else {
input.WithCode(fc.NewCode().
WithOSSBucketName(createFuncInput.codeOSSBucket).
WithOSSObjectName(createFuncInput.codeOSSObject))
if createFuncInput.codeFile != "" {
var data []byte
data, err := ioutil.ReadFile(createFuncInput.codeFile)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
input.WithCode(fc.NewCode().WithZipFile(data))
} else if createFuncInput.codeDir != "" {
input.WithCode(fc.NewCode().WithDir(createFuncInput.codeDir))
} else {
input.WithCode(fc.NewCode().
WithOSSBucketName(createFuncInput.codeOSSBucket).
WithOSSObjectName(createFuncInput.codeOSSObject))
}
}

if cmd.Flags().Changed("ca-port") {
input.WithCAPort(createFuncInput.caPort)
}

client, err := util.NewFClient(gConfig)
Expand All @@ -125,3 +143,19 @@ var createFuncCmd = &cobra.Command{
}
},
}

func createFunctionInputWithCustomContainerConfig(flags *pflag.FlagSet, input *fc.CreateFunctionInput,
image, command, args string) *fc.CreateFunctionInput {
if flags.Changed("custom-container-image") {
input.WithCustomContainerConfig(fc.NewCustomContainerConfig().
WithImage(image))
if flags.Changed("custom-container-command") {
input.CustomContainerConfig.WithCommand(command)
}
if flags.Changed("custom-container-args") {
input.CustomContainerConfig.WithArgs(args)
}
}

return input
}
64 changes: 42 additions & 22 deletions cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@ import (
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"time"

"gopkg.in/yaml.v2"

"github.com/abiosoft/ishell"
"github.com/aliyun/aliyun-log-go-sdk"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"strconv"

sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/aliyun/fc-go-sdk"
"github.com/aliyun/fcli/util"
"github.com/aliyun/fcli/version"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v2"
)

type shellState struct {
Expand Down Expand Up @@ -343,6 +340,10 @@ var shellCmd = &cobra.Command{
etag := flags.String("etag", "", "function etag for update")
environmentVariables := flags.StringArray("env", []string{}, "set environment variables. e.g. --env VAR1=val1 --env VAR2=val2")
environmentConfigFiles := flags.StringArray("env-file", []string{}, "read in a file of environment variables. e.g. --env-file FILE1 --env-file FILE2")
customContainerImage := flags.StringP("custom-container-image", "g", "", "custom container config image")
customContainerCommand := flags.StringP("custom-container-command", "n", "", "custom container config command, e.g. [\"node\"]")
customContainerArgs := flags.StringP("custom-container-args", "r", "", "custom container config args, e.g. [\"server.js\"]")
caPort := flags.Int32("ca-port", 9000, "args of custom container config")

err := flags.Parse(args)
if err != nil {
Expand Down Expand Up @@ -397,23 +398,34 @@ var shellCmd = &cobra.Command{
WithInitializer(*initializer).
WithRuntime(*runtime).
WithEnvironmentVariables(envMap)
if *codeFile != "" {
var data []byte
data, err = ioutil.ReadFile(*codeFile)
if err != nil {
return err
}
input.WithCode(fc.NewCode().WithZipFile(data))
} else if *codeDir != "" {
if err == nil {
input.WithCode(fc.NewCode().WithDir(*codeDir))

}
if *runtime == util.RuntimeCustomContainer {
input = createFunctionInputWithCustomContainerConfig(flags, input, *customContainerImage,
*customContainerCommand, *customContainerArgs)
} else {
input.WithCode(fc.NewCode().
WithOSSBucketName(*ossBucket).
WithOSSObjectName(*ossObject))
if *codeFile != "" {
var data []byte
data, err = ioutil.ReadFile(*codeFile)
if err != nil {
return err
}
input.WithCode(fc.NewCode().WithZipFile(data))
} else if *codeDir != "" {
if err == nil {
input.WithCode(fc.NewCode().WithDir(*codeDir))

}
} else {
input.WithCode(fc.NewCode().
WithOSSBucketName(*ossBucket).
WithOSSObjectName(*ossObject))
}
}

if flags.Changed("ca-port") {
input.WithCAPort(*caPort)
}

_, err = client.CreateFunction(input)
} else {
input := fc.NewUpdateFunctionInput(serviceName, functionName)
Expand Down Expand Up @@ -462,6 +474,14 @@ var shellCmd = &cobra.Command{
} else {
return fmt.Errorf("both code bucket and object should be provided")
}

if flags.Changed("ca-port") {
input.WithCAPort(*caPort)
}

input = updateFunctionInputWithCustomContainerConfig(flags, input,
*customContainerImage, *customContainerCommand, *customContainerArgs)

_, err = client.UpdateFunction(input)
}
return err
Expand Down
39 changes: 35 additions & 4 deletions cmd/update_function.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package cmd

import (
"github.com/aliyun/fc-go-sdk"
"fmt"
"io/ioutil"
"strings"

"fmt"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/aliyun/fc-go-sdk"
"github.com/aliyun/fcli/util"
"github.com/spf13/cobra"
)

type updateFuncInputType struct {
Expand All @@ -29,6 +29,10 @@ type updateFuncInputType struct {
etag *string
environmentVariables *[]string
environmentConfigFiles *[]string
customContainerImage *string
customContainerCommand *string
customContainerArgs *string
caPort *int32
}

var updateFuncInput updateFuncInputType
Expand All @@ -48,6 +52,11 @@ func init() {
updateFuncInput.memory = updateFuncCmd.Flags().Int32P("memory", "m", 0, "memory size in MB")
updateFuncInput.codeOSSBucket = updateFuncCmd.Flags().StringP("bucket", "b", "", "oss code bucket")
updateFuncInput.codeOSSObject = updateFuncCmd.Flags().StringP("object", "o", "", "oss code object")
updateFuncInput.customContainerImage = updateFuncCmd.Flags().StringP("custom-container-image", "g", "", "custom container config image")
updateFuncInput.customContainerCommand = updateFuncCmd.Flags().StringP("custom-container-command", "n", "", "custom container config command, e.g. [\"node\"]")
updateFuncInput.customContainerArgs = updateFuncCmd.Flags().StringP("custom-container-args", "r", "", "custom container config args, e.g. [\"server.js\"]")
updateFuncInput.caPort = updateFuncCmd.Flags().Int32("ca-port", 9000, "args of custom container config")

updateFuncInput.codeDir = updateFuncCmd.Flags().String(
"code-dir", "", "function code directory. If both code-file and code-dir are provided, "+
"code-file will be used.")
Expand Down Expand Up @@ -131,6 +140,13 @@ var updateFuncCmd = &cobra.Command{
WithOSSObjectName(*updateFuncInput.codeOSSObject))
}

if cmd.Flags().Changed("ca-port") {
input.WithCAPort(*updateFuncInput.caPort)
}

input = updateFunctionInputWithCustomContainerConfig(cmd.Flags(), input, *updateFuncInput.customContainerImage,
*updateFuncInput.customContainerCommand, *updateFuncInput.customContainerArgs)

client, err := util.NewFClient(gConfig)
if err != nil {
fmt.Printf("Error: can not create fc client: %s\n", err)
Expand All @@ -142,3 +158,18 @@ var updateFuncCmd = &cobra.Command{
}
},
}

func updateFunctionInputWithCustomContainerConfig(flags *pflag.FlagSet, input *fc.UpdateFunctionInput, image, command, args string) *fc.UpdateFunctionInput {
if flags.Changed("custom-container-image") {
input.WithCustomContainerConfig(fc.NewCustomContainerConfig().
WithImage(image))
if flags.Changed("custom-container-command") {
input.CustomContainerConfig.WithCommand(command)
}
if flags.Changed("custom-container-args") {
input.CustomContainerConfig.WithArgs(args)
}
}

return input
}
10 changes: 5 additions & 5 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package: github.com/aliyun/fcli
import:
- package: github.com/aliyun/fc-go-sdk
version: 0882be48e49f6c9682da173b4121d72db92924c5
- package: github.com/alyu/configparser
- package: github.com/spf13/cobra
- package: gopkg.in/matryer/try.v1
Expand All @@ -9,7 +10,7 @@ import:
- package: github.com/abiosoft/ishell
- package: gopkg.in/yaml.v2
- package: github.com/aliyun/aliyun-log-go-sdk
version: ^0.1.0
version: 0402edd803e987675a1f88f1cb80bfa006e7e28e
- package: gopkg.in/AlecAivazis/survey.v1
version: ^1.6.3
- package: github.com/denisbrodbeck/machineid
Expand Down
5 changes: 4 additions & 1 deletion util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

"github.com/aliyun/fcli/version"

"github.com/aliyun/aliyun-log-go-sdk"
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/denisbrodbeck/machineid"
"github.com/spf13/viper"

Expand Down Expand Up @@ -52,6 +52,9 @@ const (

// KeyValueDelimiter defines key value delimiter of map params
KeyValueDelimiter = "="

// RuntimeCustomContainer const for custom-container runtime
RuntimeCustomContainer = "custom-container"
)

// GlobalConfig define the global configurations.
Expand Down

0 comments on commit ac3dd81

Please sign in to comment.