|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 dingodb.com, Inc. All Rights Reserved |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * Project: DingoFS |
| 19 | + * Created Date: 2024-10-28 |
| 20 | + * Author: Wei Dong (jackblack369) |
| 21 | + */ |
| 22 | + |
| 23 | +package gateway |
| 24 | + |
| 25 | +import ( |
| 26 | + "github.com/dingodb/curveadm/cli/cli" |
| 27 | + comm "github.com/dingodb/curveadm/internal/common" |
| 28 | + "github.com/dingodb/curveadm/internal/configure" |
| 29 | + "github.com/dingodb/curveadm/internal/errno" |
| 30 | + "github.com/dingodb/curveadm/internal/playbook" |
| 31 | + cliutil "github.com/dingodb/curveadm/internal/utils" |
| 32 | + "github.com/spf13/cobra" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + START_GATEWAY_EXAMPLE = `Examples: |
| 37 | + $ curveadm gateway start --host dingfs1 --listen-address=:9000 --console-address=:9001 --mountpoint=/home/dingofs/client` |
| 38 | +) |
| 39 | + |
| 40 | +type startOptions struct { |
| 41 | + name string |
| 42 | + host string |
| 43 | + fileName string |
| 44 | + mountPoint string |
| 45 | +} |
| 46 | + |
| 47 | +var ( |
| 48 | + START_GATEWAY_PLAYBOOK_STEPS = []int{ |
| 49 | + playbook.START_GATEWAY, |
| 50 | + } |
| 51 | +) |
| 52 | + |
| 53 | +func NewStartGatewayCommand(curveadm *cli.CurveAdm) *cobra.Command { |
| 54 | + var options startOptions |
| 55 | + |
| 56 | + cmd := &cobra.Command{ |
| 57 | + // Use: "start --host={hostName} --listen-address={listenAddr} --console-address={consoleAddr} --mountpoint={path} ", |
| 58 | + Use: "start {name} {mountpoint} --host dingo7232 -c gateway.yaml ", |
| 59 | + Short: "start s3 gateway", |
| 60 | + Args: cliutil.ExactArgs(2), |
| 61 | + Example: START_GATEWAY_EXAMPLE, |
| 62 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 63 | + options.name = args[0] |
| 64 | + options.mountPoint = args[1] |
| 65 | + return runStart(curveadm, options) |
| 66 | + }, |
| 67 | + DisableFlagsInUseLine: true, |
| 68 | + } |
| 69 | + |
| 70 | + flags := cmd.Flags() |
| 71 | + flags.StringVar(&options.host, "host", "localhost", "Specify target host") |
| 72 | + flags.StringVarP(&options.fileName, "conf", "c", "gateway.yaml", "Specify gateway configuration file") |
| 73 | + |
| 74 | + return cmd |
| 75 | +} |
| 76 | + |
| 77 | +func runStart(curveadm *cli.CurveAdm, options startOptions) error { |
| 78 | + |
| 79 | + // 1) generate mount playbook |
| 80 | + pb, err := genStartPlaybook(curveadm, options) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + // 2) run playground |
| 86 | + err = pb.Run() |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + // 3) print success prompt |
| 92 | + // curveadm.WriteOutln(color.GreenString("start gateway success !\n gateway listen address: %s%s \n gateway console address: %s%s \n"), |
| 93 | + // options.host, options.listenAddress, options.host, options.consoleAddress) |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func genStartPlaybook(curveadm *cli.CurveAdm, options startOptions) (*playbook.Playbook, error) { |
| 99 | + steps := START_GATEWAY_PLAYBOOK_STEPS |
| 100 | + pb := playbook.NewPlaybook(curveadm) |
| 101 | + |
| 102 | + // parse client configure |
| 103 | + gc, err := configure.ParseGatewayConfig(options.fileName) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + listenPort := gc.GetListenPort() |
| 108 | + if listenPort == "" { |
| 109 | + listenPort = "19000" |
| 110 | + } |
| 111 | + gatewayListenAddr := ":" + listenPort |
| 112 | + |
| 113 | + consolePort := gc.GetConsolePort() |
| 114 | + if consolePort == "" { |
| 115 | + consolePort = "19001" |
| 116 | + } |
| 117 | + gatewayConsoleAddr := ":" + consolePort |
| 118 | + |
| 119 | + mdsAddr := gc.GetDingofsMDSAddr() |
| 120 | + if mdsAddr == "" { |
| 121 | + return nil, errno.ERR_GATEWAY_MDSADDR_EMPTY |
| 122 | + } |
| 123 | + |
| 124 | + for _, step := range steps { |
| 125 | + |
| 126 | + pb.AddStep(&playbook.PlaybookStep{ |
| 127 | + Type: step, |
| 128 | + Configs: gc, |
| 129 | + Options: map[string]interface{}{ |
| 130 | + comm.GATEWAY_NAME: options.name, |
| 131 | + comm.GATEWAY_HOST: options.host, |
| 132 | + comm.GATEWAY_LISTEN_ADDR: gatewayListenAddr, |
| 133 | + comm.GATEWAY_CONSOLE_ADDR: gatewayConsoleAddr, |
| 134 | + comm.GATEWAY_MOUNTPOINT: options.mountPoint, |
| 135 | + comm.MDSADDR: mdsAddr, |
| 136 | + }, |
| 137 | + }) |
| 138 | + } |
| 139 | + return pb, nil |
| 140 | +} |
0 commit comments