Skip to content

Commit

Permalink
Fix missing default admin listen address
Browse files Browse the repository at this point in the history
Improve handling of environment based defaults in packages
Improve documentation
Fixes #307
  • Loading branch information
driskell committed Apr 24, 2016
1 parent 2568f18 commit a07d815
Show file tree
Hide file tree
Showing 19 changed files with 339 additions and 187 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Expand Up @@ -2,8 +2,8 @@
/.vagrant
/*.gem
/bin
/lc-lib/config/platform.go
/lc-lib/core/version.go
/lc-admin/platform.go
/node_modules
/platform.go
/spec/tmp
/vendor/bundle
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ configuration in RPM and DEB packages (#303)
1.00e2 instead of 100 (#304)
* Fix hang that could occur when Logstash failed in loadbalance or failover
network methods (#311)
* Fix `admin` `listen address` not having a default (#307)
* Improve debug logging of backoff calculations

## 2.0.0
Expand Down
7 changes: 6 additions & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion docs/Configuration.md
Expand Up @@ -277,7 +277,9 @@ this.

### `listen address`

*String. Optional. Default: tcp:127.0.0.1:1234*
*String. Required when `enabled` is true.
Default: tcp:127.0.0.1:1234
RPM/DEB Package Default: unix:/var/run/log-courier/admin.socket*

The address the REST interface should listen on in the format
`transport:address`.
Expand Down Expand Up @@ -418,6 +420,7 @@ This setting can not be greater than the `spool max bytes` setting.
### `persist directory`

*String. Required
RPM/DEB Package Default: /var/lib/log-courier
Requires restart*

The directory that Log Courier should store its persistence data in.
Expand Down
7 changes: 5 additions & 2 deletions lc-admin/lc-admin.go
Expand Up @@ -28,6 +28,9 @@ import (
"github.com/driskell/log-courier/lc-lib/core"
)

// Generate platform-specific default configuration values
//go:generate go run ../lc-lib/config/generate/platform.go main config.DefaultConfigurationFile config.DefaultGeneralPersistDir admin.DefaultAdminBind

type commandProcessor interface {
ProcessCommand(string) bool
}
Expand Down Expand Up @@ -86,12 +89,12 @@ func (a *lcAdmin) startUp() {

func (a *lcAdmin) loadConfig() {
if a.configFile == "" && a.adminConnect == "" {
if config.DefaultGeneralAdminBind == "" {
if admin.DefaultAdminBind == "" {
fmt.Printf("Either -connect or -config must be specified\n")
flag.PrintDefaults()
os.Exit(1)
} else {
a.adminConnect = config.DefaultGeneralAdminBind
a.adminConnect = admin.DefaultAdminBind
}
return
}
Expand Down
17 changes: 12 additions & 5 deletions lc-lib/admin/config.go
Expand Up @@ -22,8 +22,12 @@ import (
"github.com/driskell/log-courier/lc-lib/config"
)

const (
defaultGeneralAdminEnabled bool = false
var (
defaultAdminEnabled = false

// DefaultAdminBind is the default bind address to use when admin is enabled
// and can be modified during init()
DefaultAdminBind = "tcp:127.0.0.1:12345"
)

// Config holds the admin configuration
Expand All @@ -36,6 +40,12 @@ type Config struct {
apiRoot APINavigatable
}

// InitDefaults initialises default values
func (c *Config) InitDefaults() {
c.Enabled = defaultAdminEnabled
c.Bind = DefaultAdminBind
}

// Validate validates the config structure
func (c *Config) Validate() (err error) {
if c.Enabled && c.Bind == "" {
Expand All @@ -54,9 +64,6 @@ func (c *Config) SetEntry(path string, entry APINavigatable) {
func init() {
config.RegisterConfigSection("admin", func() config.Section {
c := &Config{}
c.Enabled = defaultGeneralAdminEnabled
// TODO: Implement default bind address which is currently stored in config pkg
c.Bind = "" //DefaultGeneralAdminBind
return c
})
}
11 changes: 10 additions & 1 deletion lc-lib/config/config.go
Expand Up @@ -30,6 +30,15 @@ import (
"gopkg.in/op/go-logging.v1"
)

var (
// DefaultConfigurationFile is a path to the default configuration file to
// load, this can be changed during init()
DefaultConfigurationFile = ""

// DefaultGeneralPersistDir is a path to the default directory to store
DefaultGeneralPersistDir = ""
)

const (
defaultGeneralHost string = "localhost.localdomain"
defaultGeneralLogLevel logging.Level = logging.INFO
Expand Down Expand Up @@ -94,7 +103,7 @@ func (gc *General) InitDefaults() {
gc.LogStdout = defaultGeneralLogStdout
gc.LogSyslog = defaultGeneralLogSyslog
gc.MaxLineBytes = defaultGeneralMaxLineBytes
gc.PersistDir = defaultGeneralPersistDir
gc.PersistDir = DefaultGeneralPersistDir
gc.ProspectInterval = defaultGeneralProspectInterval
gc.SpoolSize = defaultGeneralSpoolSize
gc.SpoolMaxBytes = defaultGeneralSpoolMaxBytes
Expand Down
122 changes: 122 additions & 0 deletions lc-lib/config/generate/platform.go
@@ -0,0 +1,122 @@
/*
* Copyright 2015-2016 Jason Woods.
*
* 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 main

import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"

"github.com/fatih/camelcase"
)

const platformHeader = `// THIS IS A GO GENERATED FILE
/*
* Copyright 2014-2016 Jason Woods.
*
* 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.
*/
`

// Generate platform.go
// It should contain platform specific defaults, such as the default
// configuration location and persist directory.
// Useful for package maintainers
func main() {
if len(os.Args) < 2 {
log.Fatalf("Usage: go run <path-to-lc-lib>/config/generate.go <package-name> <configs>...")
}

platformFile := platformHeader
platformFile += fmt.Sprintf("package %s\n\n", os.Args[1])

config := parseConfigArgs(os.Args[2:])
platformFile += generatePackageImports(config)
platformFile += generateInit(config)

platformFileBytes := []byte(os.ExpandEnv(platformFile))
if err := ioutil.WriteFile("platform.go", platformFileBytes, 0644); err != nil {
log.Fatalf("Failed to write platform.go: %s", err)
}
}

func parseConfigArgs(args []string) map[string][]string {
config := map[string][]string{}

for _, v := range args {
envSplit := strings.SplitN(v, ".", 2)
if len(envSplit) != 2 {
log.Fatalf("Config variable needs package specifier: %s", v)
}

config[envSplit[0]] = append(config[envSplit[0]], envSplit[1])
}

return config
}

func generatePackageImports(config map[string][]string) string {
platformFile := "import (\n"
for pkg := range config {
platformFile += fmt.Sprintf("\t\"github.com/driskell/log-courier/lc-lib/%s\"\n", pkg)
}
platformFile += ")\n\n"
return platformFile
}

func generateInit(config map[string][]string) string {
first := true
platformFile := "func init() {\n"
for pkg, nameArr := range config {
for _, name := range nameArr {
if !first {
platformFile += "\n"
} else {
first = false
}
platformFile += generateInitSegment(pkg, name)
}
}
platformFile += "}\n"
return platformFile
}

func generateInitSegment(pkg, name string) string {
envArr := camelcase.Split(name)
for i := range envArr {
envArr[i] = strings.ToUpper(envArr[i])
}

envName := "LC_" + strings.Join(envArr, "_")
platformFile := fmt.Sprintf("\t// %s\n", envName)
platformFile += fmt.Sprintf("\t%s.%s = \"${%s}\"\n", pkg, name, envName)
return platformFile
}
29 changes: 0 additions & 29 deletions lc-lib/config/platform.go.tmpl

This file was deleted.

43 changes: 0 additions & 43 deletions lc-lib/config/platform_generate.go

This file was deleted.

20 changes: 0 additions & 20 deletions lc-lib/config/platform_include.go

This file was deleted.

6 changes: 3 additions & 3 deletions lc-lib/core/version_include.go → lc-lib/core/version.go
@@ -1,5 +1,5 @@
/*
* Copyright 2015 Jason Woods.
* Copyright 2014-2016 Jason Woods.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,5 +16,5 @@

package core

// Generate the version number
//go:generate go run version_generate.go
// LogCourierVersion is the library version number
const LogCourierVersion string = "2.0.0"
23 changes: 0 additions & 23 deletions lc-lib/core/version.go.tmpl

This file was deleted.

0 comments on commit a07d815

Please sign in to comment.