surrealpatch/cli/setup.go

309 lines
6.9 KiB
Go
Raw Normal View History

// 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-07-16 13:43:53 +00:00
"regexp"
"strings"
2016-04-08 19:29:13 +00:00
"github.com/abcum/surreal/cnf"
2016-04-08 19:29:13 +00:00
"github.com/abcum/surreal/log"
"github.com/abcum/surreal/util/uuid"
)
func setup() {
2016-07-06 18:02:29 +00:00
// --------------------------------------------------
// DB
// --------------------------------------------------
// Ensure that the default
// database options are set
if opts.DB.Base == "" {
opts.DB.Base = "surreal"
}
if opts.DB.Path == "" {
2016-07-16 13:43:53 +00:00
opts.DB.Path = "boltdb://surreal.db"
}
2016-07-19 11:05:32 +00:00
if opts.DB.Code != "" {
opts.DB.Key = []byte(opts.DB.Code)
}
if ok, _ := regexp.MatchString(`^(boltdb|mysql|pgsql)://(.+)$`, opts.DB.Path); !ok {
2016-07-16 13:43:53 +00:00
log.Fatal("Specify a valid data store configuration path")
2016-07-06 18:02:29 +00:00
}
2016-07-19 11:05:32 +00:00
switch len(opts.DB.Key) {
case 0, 16, 24, 32:
default:
log.Fatal("Specify a valid encryption key length. Valid key sizes are 16bit, 24bit, or 32bit.")
}
if strings.HasPrefix(opts.DB.Cert.CA, "-----") {
var err error
var doc *os.File
if doc, err = os.Create("db.ca"); err != nil {
log.Fatal("Can not decode PEM encoded CA into db.ca")
}
doc.Write([]byte(opts.DB.Cert.CA))
doc.Close()
opts.Cert.Crt = "db.ca"
}
if strings.HasPrefix(opts.DB.Cert.Crt, "-----") {
var err error
var doc *os.File
if doc, err = os.Create("db.key"); err != nil {
log.Fatal("Can not decode PEM encoded certificate into db.crt")
}
doc.Write([]byte(opts.DB.Cert.Crt))
doc.Close()
opts.Cert.Crt = "db.crt"
}
if strings.HasPrefix(opts.DB.Cert.Key, "-----") {
var err error
var doc *os.File
if doc, err = os.Create("db.crt"); err != nil {
log.Fatal("Can not decode PEM encoded private key into db.key")
}
doc.Write([]byte(opts.DB.Cert.Key))
doc.Close()
opts.Cert.Crt = "db.key"
}
if opts.DB.Cert.CA != "" || opts.DB.Cert.Crt != "" || opts.DB.Cert.Key != "" {
opts.DB.Cert.SSL = true
}
if opts.DB.Cert.CA == "" && opts.DB.Cert.SSL {
log.Fatal("Specify a valid PEM encoded CA file.")
}
if opts.DB.Cert.Crt == "" && opts.DB.Cert.SSL {
log.Fatal("Specify a valid PEM encoded certificate file.")
}
if opts.DB.Cert.Key == "" && opts.DB.Cert.SSL {
log.Fatal("Specify a valid PEM encoded private key file.")
}
// --------------------------------------------------
// Lang
// --------------------------------------------------
// Ensure that the default
// language options are set
if opts.DB.Lang == "" {
opts.DB.Lang = "lua"
}
if opts.DB.Lang != "js" && opts.DB.Lang != "lua" {
log.Fatal("Specify a valid runtime language. Valid languages are js, or lua.")
}
2016-07-06 18:02:29 +00:00
// --------------------------------------------------
// Auth
// --------------------------------------------------
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-04-08 19:29:13 +00:00
both := strings.SplitN(opts.Auth.Auth, ":", 2)
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-06 18:02:29 +00:00
// --------------------------------------------------
// Nodes
// --------------------------------------------------
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()
}
2016-07-06 18:02:29 +00:00
// Convert string args to slices
for _, v := range strings.Split(opts.Node.Attr, ",") {
if c := strings.Trim(v, " "); c != "" {
opts.Node.Tags = append(opts.Node.Tags, c)
}
2016-07-04 10:33:07 +00:00
}
2016-07-06 18:02:29 +00:00
// Convert string args to slices
for _, v := range strings.Split(opts.Cluster.Join, ",") {
if c := strings.Trim(v, " "); c != "" {
opts.Cluster.Peer = append(opts.Cluster.Peer, c)
}
2016-07-04 10:33:07 +00:00
}
2016-07-06 18:02:29 +00:00
// --------------------------------------------------
// Ports
// --------------------------------------------------
2016-04-08 19:29:13 +00:00
2016-07-06 18:12:00 +00:00
// Specify default port
if opts.Port.Web == 0 {
opts.Port.Web = 8000
}
// Specify default port
if opts.Port.Tcp == 0 {
opts.Port.Tcp = 33693
}
2016-07-06 18:02:29 +00:00
// Ensure port number is valid
if opts.Port.Web < 0 || opts.Port.Web > 65535 {
log.Fatal("Please specify a valid port number for --port-web")
2016-04-08 19:29:13 +00:00
}
2016-07-06 18:02:29 +00:00
// Ensure port number is valid
if opts.Port.Tcp < 0 || opts.Port.Tcp > 65535 {
log.Fatal("Please specify a valid port number for --port-tcp")
2016-04-08 19:29:13 +00:00
}
2016-07-06 18:02:29 +00:00
// Store the ports in host:port string 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
2016-07-06 18:02:29 +00:00
// --------------------------------------------------
// Certs
// --------------------------------------------------
2016-04-08 19:29:13 +00:00
if strings.HasPrefix(opts.Cert.Crt, "-----") {
var err error
var doc *os.File
if doc, err = os.Create("cert.crt"); err != nil {
log.Fatal("Can not decode PEM encoded certificate into cert.crt")
2016-07-04 10:33:07 +00:00
}
doc.Write([]byte(opts.Cert.Crt))
doc.Close()
opts.Cert.Crt = "cert.crt"
}
2016-04-08 19:29:13 +00:00
if strings.HasPrefix(opts.Cert.Key, "-----") {
var err error
var doc *os.File
if doc, err = os.Create("cert.key"); err != nil {
log.Fatal("Can not decode PEM encoded private key into cert.key")
2016-07-04 10:33:07 +00:00
}
doc.Write([]byte(opts.Cert.Key))
doc.Close()
2016-07-06 18:02:29 +00:00
opts.Cert.Key = "cert.key"
2016-04-08 19:29:13 +00:00
}
2016-07-06 18:02:29 +00:00
// --------------------------------------------------
// Logging
// --------------------------------------------------
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-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)
}
cnf.Settings = opts
}