2016-02-29 17:28:59 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2016-04-08 19:29:13 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-02-29 17:28:59 +00:00
|
|
|
"strings"
|
2016-04-08 19:29:13 +00:00
|
|
|
|
|
|
|
"github.com/abcum/surreal/log"
|
2016-07-04 10:33:07 +00:00
|
|
|
"github.com/abcum/surreal/util/cert"
|
2016-04-08 19:29:13 +00:00
|
|
|
"github.com/abcum/surreal/util/uuid"
|
2016-02-29 17:28:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setup() {
|
|
|
|
|
2016-04-08 19:29:13 +00:00
|
|
|
if opts.Auth.Auth != "" {
|
|
|
|
|
|
|
|
if opts.Auth.User != "" {
|
|
|
|
log.Fatal("Specify only --auth or --auth-user")
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Auth.Pass != "" {
|
|
|
|
log.Fatal("Specify only --auth or --auth-pass")
|
|
|
|
}
|
2016-02-29 17:28:59 +00:00
|
|
|
|
2016-04-08 19:29:13 +00:00
|
|
|
both := strings.SplitN(opts.Auth.Auth, ":", 2)
|
2016-02-29 17:28:59 +00:00
|
|
|
|
|
|
|
if len(both) == 2 {
|
2016-04-08 19:29:13 +00:00
|
|
|
opts.Auth.User = both[0]
|
|
|
|
opts.Auth.Pass = both[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that security
|
|
|
|
// is enabled by default
|
|
|
|
|
|
|
|
if opts.Auth.User == "" {
|
|
|
|
opts.Auth.User = "root"
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Auth.Pass == "" {
|
|
|
|
opts.Auth.Pass = "root"
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:33:07 +00:00
|
|
|
// Ensure that security
|
|
|
|
// is enabled by default
|
|
|
|
|
|
|
|
if opts.Cert.Pem != "" {
|
|
|
|
|
|
|
|
if opts.Cert.Crt != "" {
|
|
|
|
log.Fatal("Specify only --cert-pem or --cert-crt")
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Cert.Key != "" {
|
|
|
|
log.Fatal("Specify only --cert-pem or --cert-key")
|
|
|
|
}
|
|
|
|
|
2016-07-04 19:56:10 +00:00
|
|
|
err := cert.Extract(opts.Cert.Pem, "cert.key", "cert.crt")
|
2016-07-04 10:33:07 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Cert.Crt = "cert.crt"
|
|
|
|
|
|
|
|
opts.Cert.Key = "cert.key"
|
2016-04-08 19:29:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the default
|
2016-07-04 10:33:07 +00:00
|
|
|
// database options are set
|
2016-04-08 19:29:13 +00:00
|
|
|
|
2016-07-04 10:33:07 +00:00
|
|
|
if opts.DB.Base == "" {
|
|
|
|
opts.DB.Base = "surreal"
|
2016-04-08 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:33:07 +00:00
|
|
|
if opts.DB.Path == "" {
|
|
|
|
opts.DB.Path = "surreal.db"
|
2016-04-08 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the default
|
|
|
|
// node details are defined
|
|
|
|
|
|
|
|
if opts.Node.Host == "" {
|
|
|
|
opts.Node.Host, _ = os.Hostname()
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Node.Name == "" {
|
|
|
|
opts.Node.Name = opts.Node.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Node.UUID == "" {
|
|
|
|
opts.Node.UUID = opts.Node.Name + "-" + uuid.NewV4().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the defined ports
|
|
|
|
// are within range
|
|
|
|
|
2016-07-04 10:33:07 +00:00
|
|
|
if opts.Port.Web == 0 {
|
|
|
|
opts.Port.Web = 8000
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Port.Tcp == 0 {
|
|
|
|
opts.Port.Tcp = 33693
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Port.Web == opts.Port.Tcp {
|
2016-04-08 19:29:13 +00:00
|
|
|
log.Fatal("Defined ports must be different")
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:33:07 +00:00
|
|
|
if opts.Port.Web > 65535 {
|
2016-04-08 19:29:13 +00:00
|
|
|
log.Fatal("Please specify a valid port number for --port-http")
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:33:07 +00:00
|
|
|
if opts.Port.Tcp > 65535 {
|
2016-04-08 19:29:13 +00:00
|
|
|
log.Fatal("Please specify a valid port number for --port-raft")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define the listen string
|
|
|
|
// with host:port format
|
|
|
|
|
2016-07-04 10:33:07 +00:00
|
|
|
opts.Conn.Web = fmt.Sprintf(":%d", opts.Port.Web)
|
|
|
|
opts.Conn.Tcp = fmt.Sprintf(":%d", opts.Port.Tcp)
|
2016-04-08 19:29:13 +00:00
|
|
|
|
|
|
|
// Ensure that string args
|
|
|
|
// are converted to slices
|
|
|
|
|
2016-07-04 10:33:07 +00:00
|
|
|
for _, v := range strings.Split(opts.Node.Attr, ",") {
|
|
|
|
if c := strings.Trim(v, " "); c != "" {
|
|
|
|
opts.Node.Tags = append(opts.Node.Tags, c)
|
|
|
|
}
|
2016-04-08 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:33:07 +00:00
|
|
|
for _, v := range strings.Split(opts.Cluster.Join, ",") {
|
|
|
|
if c := strings.Trim(v, " "); c != "" {
|
|
|
|
opts.Cluster.Peer = append(opts.Cluster.Peer, c)
|
|
|
|
}
|
2016-04-08 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the specified
|
|
|
|
// logging level is allowed
|
|
|
|
|
|
|
|
if opts.Logging.Level != "" {
|
|
|
|
|
|
|
|
chk := map[string]bool{
|
|
|
|
"debug": true,
|
|
|
|
"info": true,
|
|
|
|
"warning": true,
|
|
|
|
"error": true,
|
|
|
|
"fatal": true,
|
|
|
|
"panic": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := chk[opts.Logging.Level]; !ok {
|
|
|
|
log.Fatal("Incorrect log level specified")
|
2016-02-29 17:28:59 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 19:29:13 +00:00
|
|
|
log.SetLevel(opts.Logging.Level)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the specified
|
|
|
|
// logging output is allowed
|
|
|
|
|
|
|
|
if opts.Logging.Output != "" {
|
|
|
|
|
|
|
|
chk := map[string]bool{
|
|
|
|
"stdout": true,
|
|
|
|
"stderr": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := chk[opts.Logging.Output]; !ok {
|
|
|
|
log.Fatal("Incorrect log output specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetOutput(opts.Logging.Output)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the specified
|
|
|
|
// logging format is allowed
|
|
|
|
|
|
|
|
if opts.Logging.Format != "" {
|
|
|
|
|
|
|
|
chk := map[string]bool{
|
|
|
|
"text": true,
|
|
|
|
"json": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := chk[opts.Logging.Format]; !ok {
|
|
|
|
log.Fatal("Incorrect log format specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetFormat(opts.Logging.Format)
|
|
|
|
|
2016-02-29 17:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|