|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2017 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "fmt" |
| 28 | + "net/url" |
| 29 | + "os" |
| 30 | + "os/exec" |
| 31 | + "time" |
| 32 | + |
| 33 | + "github.com/arangodb-helper/arangodb/client" |
| 34 | + "github.com/spf13/cobra" |
| 35 | + "github.com/spf13/pflag" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + cmdStart = &cobra.Command{ |
| 40 | + Use: "start", |
| 41 | + Short: "Start the ArangoDB starter in the background", |
| 42 | + Run: cmdStartRun, |
| 43 | + } |
| 44 | + waitForServers bool |
| 45 | +) |
| 46 | + |
| 47 | +func init() { |
| 48 | + f := cmdStart.Flags() |
| 49 | + f.BoolVar(&waitForServers, "starter.wait", false, "If set, the (parent) starter waits until all database servers are ready before exiting.") |
| 50 | + |
| 51 | + cmdMain.AddCommand(cmdStart) |
| 52 | +} |
| 53 | + |
| 54 | +func cmdStartRun(cmd *cobra.Command, args []string) { |
| 55 | + log.Infof("Starting %s version %s, build %s in the background", projectName, projectVersion, projectBuild) |
| 56 | + |
| 57 | + // Setup logging |
| 58 | + configureLogging() |
| 59 | + |
| 60 | + // Build service |
| 61 | + service := mustPrepareService(true) |
| 62 | + |
| 63 | + // Find executable |
| 64 | + exePath, err := os.Executable() |
| 65 | + if err != nil { |
| 66 | + log.Fatalf("Cannot find executable path: %#v", err) |
| 67 | + } |
| 68 | + |
| 69 | + // Build command line |
| 70 | + childArgs := make([]string, 0, len(os.Args)) |
| 71 | + cmd.InheritedFlags().VisitAll(func(f *pflag.Flag) { |
| 72 | + if f.Changed { |
| 73 | + switch f.Name { |
| 74 | + case "ssl.auto-key", "ssl.auto-server-name", "ssl.auto-organization", "ssl.keyfile": |
| 75 | + // Do not pass these along |
| 76 | + default: |
| 77 | + a := "--" + f.Name |
| 78 | + value := f.Value.String() |
| 79 | + if value != "" { |
| 80 | + a = a + "=" + value |
| 81 | + } |
| 82 | + childArgs = append(childArgs, a) |
| 83 | + } |
| 84 | + } |
| 85 | + }) |
| 86 | + if service.SslKeyFile != "" { |
| 87 | + childArgs = append(childArgs, "--ssl.keyfile="+service.SslKeyFile) |
| 88 | + } |
| 89 | + |
| 90 | + log.Debugf("Found child args: %#v", childArgs) |
| 91 | + |
| 92 | + // Start detached child |
| 93 | + c := exec.Command(exePath, childArgs...) |
| 94 | + if err := c.Start(); err != nil { |
| 95 | + log.Fatalf("Failed to start detached child: %#v", err) |
| 96 | + } |
| 97 | + c.Process.Release() |
| 98 | + |
| 99 | + // Create starter client |
| 100 | + scheme := "http" |
| 101 | + if sslAutoKeyFile || sslKeyFile != "" { |
| 102 | + scheme = "https" |
| 103 | + } |
| 104 | + starterURL, err := url.Parse(fmt.Sprintf("%s://127.0.0.1:%d", scheme, masterPort)) |
| 105 | + if err != nil { |
| 106 | + log.Fatalf("Failed to create starter URL: %#v", err) |
| 107 | + } |
| 108 | + client, err := client.NewArangoStarterClient(*starterURL) |
| 109 | + if err != nil { |
| 110 | + log.Fatalf("Failed to create starter client: %#v", err) |
| 111 | + } |
| 112 | + |
| 113 | + // Wait for detached starter to be alive |
| 114 | + log.Info("Waiting for starter API to be available...") |
| 115 | + rootCtx := context.Background() |
| 116 | + for { |
| 117 | + ctx, cancel := context.WithTimeout(rootCtx, time.Second) |
| 118 | + _, err := client.Version(ctx) |
| 119 | + cancel() |
| 120 | + if err == nil { |
| 121 | + break |
| 122 | + } |
| 123 | + time.Sleep(time.Millisecond * 100) |
| 124 | + } |
| 125 | + |
| 126 | + // Wait until all servers ready (if needed) |
| 127 | + if waitForServers { |
| 128 | + log.Info("Waiting for database instances to be available...") |
| 129 | + for { |
| 130 | + var err error |
| 131 | + ctx, cancel := context.WithTimeout(rootCtx, time.Second) |
| 132 | + list, err := client.Processes(ctx) |
| 133 | + cancel() |
| 134 | + if err == nil && list.ServersStarted { |
| 135 | + // Start says it has started the servers, now wait for servers to be up. |
| 136 | + allUp := true |
| 137 | + for _, server := range list.Servers { |
| 138 | + ctx, cancel := context.WithTimeout(rootCtx, time.Second) |
| 139 | + up, _, _ := service.TestInstance(ctx, server.IP, server.Port) |
| 140 | + cancel() |
| 141 | + if !up { |
| 142 | + allUp = false |
| 143 | + break |
| 144 | + } |
| 145 | + } |
| 146 | + if allUp { |
| 147 | + break |
| 148 | + } |
| 149 | + } |
| 150 | + time.Sleep(time.Millisecond * 100) |
| 151 | + } |
| 152 | + log.Info("Database instances are available.") |
| 153 | + } |
| 154 | +} |
0 commit comments