|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/octoproject/octo-cli/config" |
| 8 | + "github.com/octoproject/octo-cli/faas" |
| 9 | + "github.com/octoproject/octo-cli/knative" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + ErrBadGatewayURL = errors.New("error empty gateway url") |
| 15 | + ErrOpenfaasCredentials = errors.New("error empty Openfaas username or password") |
| 16 | + ErrUnknownPlatform = errors.New("error unknown platform") |
| 17 | +) |
| 18 | + |
| 19 | +type deplpyOptions struct { |
| 20 | + fileName string |
| 21 | + username string |
| 22 | + password string |
| 23 | + gatewayURL string |
| 24 | + namespace string |
| 25 | + image string |
| 26 | + imagePullPolicy string |
| 27 | +} |
| 28 | + |
| 29 | +func NewDeployCommand() *cobra.Command { |
| 30 | + options := deplpyOptions{} |
| 31 | + |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "deploy", |
| 34 | + Short: "Deploy a new service", |
| 35 | + Example: `octo-cli deploy -f example.yml -i functions/nodeinfo-http:latest |
| 36 | +octo-cli deploy -f example.yml -g http://127.0.0.1:8080 -i functions/nodeinfo-http:latest -u admin -p 123456 `, |
| 37 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 38 | + return runDeploy(options) |
| 39 | + }, |
| 40 | + SilenceUsage: true, |
| 41 | + } |
| 42 | + |
| 43 | + flags := cmd.Flags() |
| 44 | + flags.StringVarP(&options.fileName, "file", "f", "", "Name of the service yml file") |
| 45 | + flags.StringVarP(&options.username, "username", "u", "", "Openfaas gateway username") |
| 46 | + flags.StringVarP(&options.password, "password", "p", "", "Openfaas gateway password") |
| 47 | + flags.StringVarP(&options.gatewayURL, "gateway", "g", "http://127.0.0.1:8080", "Openfaas gateway URL") |
| 48 | + flags.StringVarP(&options.namespace, "namespace", "n", "", "Namespace for deployed function") |
| 49 | + flags.StringVarP(&options.image, "image", "i", "", "Docker image name") |
| 50 | + flags.StringVarP(&options.imagePullPolicy, "pullPolicy", "", "", "Docker image pull policy") |
| 51 | + |
| 52 | + return cmd |
| 53 | +} |
| 54 | + |
| 55 | +func runDeploy(o deplpyOptions) error { |
| 56 | + s, err := config.LoadService(o.fileName) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + env := map[string]string{ |
| 62 | + "DB_USER": s.DB.User, |
| 63 | + "DB_PASSWORD": s.DB.Password, |
| 64 | + "DB_NAME": s.DB.Name, |
| 65 | + "DB_HOST": s.DB.Host, |
| 66 | + "DB_DIALECT": s.DB.Type, |
| 67 | + "DB_PORT": s.DB.Port, |
| 68 | + "DB_TIMEOUT": s.DB.RequestTimeout, |
| 69 | + } |
| 70 | + |
| 71 | + switch s.Platform { |
| 72 | + case "openfaas": |
| 73 | + if len(o.username) < 1 || len(o.password) < 1 { |
| 74 | + return ErrOpenfaasCredentials |
| 75 | + } |
| 76 | + |
| 77 | + c := faas.New(o.username, o.password, o.gatewayURL) |
| 78 | + |
| 79 | + f := faas.Function{ |
| 80 | + ServiceName: s.ServiceName, |
| 81 | + Image: o.image, |
| 82 | + Namespace: o.namespace, |
| 83 | + EnvVars: env, |
| 84 | + } |
| 85 | + |
| 86 | + err := c.DeployFunction(&f) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + fmt.Printf("Servive %s deployed successfully.\n", s.ServiceName) |
| 92 | + |
| 93 | + case "knative": |
| 94 | + f := knative.Function{ |
| 95 | + ServiceName: s.ServiceName, |
| 96 | + Image: o.image, |
| 97 | + Namespace: o.namespace, |
| 98 | + EnvVars: env, |
| 99 | + } |
| 100 | + |
| 101 | + err := knative.DeployFunction(&f) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + fmt.Printf("Servive %s deployed successfully.\n", s.ServiceName) |
| 107 | + default: |
| 108 | + return ErrUnknownPlatform |
| 109 | + |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
0 commit comments