commands

package
v0.0.0-...-6bf55fe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2021 License: Apache-2.0 Imports: 24 Imported by: 1

Documentation

Index

Constants

View Source
const (
	GZIPb64 = iota
	BROTLI
)

Variables

View Source
var (
	// KeyFrom is the key in the Consul KV data store where we want to pull data.
	// This configuration variable will pull data from:
	//  /PrefixLocation/KeyFrom/data
	//  /PrefixLocation/KeyFrom/checksum
	KeyFrom string

	// KeyTo is the key in the Consul KV data store where we want to send data to.
	// This configuration variable will save data into:
	//  /PrefixLocation/KeyTo/data
	//  /PrefixLocation/KeyTo/checksum
	KeyTo string
)
View Source
var (
	// KeyInLocation is the key in the Consul KV data store where we want to store the data.
	// This configuration variable will save data into:
	//  /PrefixLocation/KeyInLocation/data
	//  /PrefixLocation/KeyInLocation/checksum
	KeyInLocation string

	// FiletoRead is the file to read to get the data.
	FiletoRead string

	// Sorted is an option to sort the file alphabetically. Doesn't work on many types
	// of files. But works great on files with many blank lines where ordering doesn't matter.
	Sorted bool

	// UrltoRead is an HTTP URL to read data from using ReadURL().
	UrltoRead string
)
View Source
var (
	// FiletoLock is the location we want to write the data to.
	FiletoLock string

	// LockReason is the reason why you are locking the file.
	LockReason string
)
View Source
var (
	// KeyOutLocation This Consul key is the location we want to pull data from.
	// This data MUST be in the standard kvexpress structure of:
	//  /PrefixLocation/KeyOutLocation/data
	//  /PrefixLocation/KeyOutLocation/checksum
	KeyOutLocation string

	// FiletoWrite is the location we want to write the data to.
	FiletoWrite string

	// IgnoreStop is a special command to pull data EVEN if there's a stop key present.
	IgnoreStop bool
)
View Source
var (
	// RawKeyOutLocation This Consul key is the location we want to pull data from.
	// This data can be ANY Consul key and doesn't have to be in any particular format or structure.
	// It doesn't have to have a Checksum either - it can be any Consul key at all.
	// Give the complete path - does not use PrefixLocation.
	RawKeyOutLocation string

	// RawFiletoWrite is the location we want to write the data to.
	RawFiletoWrite string
)
View Source
var (
	// Token is used by the Consul API and references an ACL that Consul uses
	// to control access the KV store: https://www.consul.io/docs/internals/acl.html
	Token string

	// PostExec refers to an optional command to run upon
	// successful completion of the command's task. An example:
	// kvexpress out -k hosts -f /etc/hosts -e "sudo pkill -HUP dnsmasq"
	PostExec string

	// ConsulServer if you are not talking to a Consul node on localhost - this is for you.
	ConsulServer string

	// PrefixLocation all Consul KV data related to kvexpress is stored underneath
	// this path. Defaults to `kvexpress` which
	PrefixLocation string

	// MinFileLength is the minimum number of lines a file is expected to have.
	// Keeps blank or truncated files out of the KV store.
	MinFileLength int

	// FilePermissions are the permissions for the files that are written to the filesystem.
	FilePermissions int

	// DogStatsd enables reporting of tagged statsd metrics to the local Datadog Agent.
	// http://docs.datadoghq.com/guides/dogstatsd/
	DogStatsd bool

	// Owner will be the owner of any file that's been written to the filesystem.
	Owner string

	// ConfigFile is the path to a yaml encoded configuration file.
	// Loaded with LoadConfig.
	ConfigFile string

	// DogStatsdAddress if you're not running a local Datadog agent.
	DogStatsdAddress string
	DogStatsdHost    string
	DogStatsdPort    int

	// DatadogAPIKey is for sending events to Datadog through the HTTP api.
	DatadogAPIKey string

	// DatadogAPPKey is for sending events to Datadog through the HTTP api.
	DatadogAPPKey string

	// Compress is for compressing data on the way in and out of Consul.
	Compress bool

	// Direction adds information about which command is running to the logs.
	Direction string

	// Verbose logs all output to stdout.
	Verbose bool
)
View Source
var (
	// KeyStopLocation This Consul key is the one we want to halt all updates and distribution of.
	KeyStopLocation string

	// KeyStopReason This is the reason that we are stopping distribution of the key.
	KeyStopReason string
)
View Source
var (
	// FiletoClean is the file we want to erase.
	FiletoClean string
)
View Source
var (
	// FiletoUnlock is the location we want to write the data to.
	FiletoUnlock string
)
View Source
var RootCmd = &cobra.Command{
	Use:   "kvexpress",
	Short: "Configuration data -> Consul KV -> Filesytem",
	Long:  `Small Go program to put and pull configuration data out of Consul and write to filesystem.`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("`kvexpress -h` for help information.")
		fmt.Println("`kvexpress -v` for version information.")
	},
}

RootCmd is the default Cobra struct that starts it all off. https://github.com/spf13/cobra

Functions

func AutoEnable

func AutoEnable()

AutoEnable helps to automatically enable flags based on cues from the environment.

func BlankLineStrip

func BlankLineStrip(data []string) []string

BlankLineStrip takes a slice of strings, ranges over them and only returns a slice of strings where the lines weren't blank.

func CheckFiletoWrite

func CheckFiletoWrite(filename, checksum string)

CheckFiletoWrite takes a filename and checksum and stops execution if there is a directory OR the file has the same checksum.

func CheckFullFilename

func CheckFullFilename(file string)

CheckFullFilename makes sure that the filename begins with a slash.

func CheckFullPath

func CheckFullPath(file string)

CheckFullPath will check the path and recursively create directories if they don't exist.

func CheckLastFile

func CheckLastFile(file string, perms int, owner string)

CheckLastFile creates a .last file if it doesn't exist.

func ChecksumCompare

func ChecksumCompare(data string, checksum string) bool

ChecksumCompare takes a string, generates a SHA256 checksum and compares against the passed checksum to see if they match.

func ChownFile

func ChownFile(filepath string, owner string) (bool, int, int)

ChownFile does what it sounds like.

func CompareFilename

func CompareFilename(file string) string

CompareFilename returns a .compare filename based on the passed file.

func CompressData

func CompressData(data string) ([]byte, uint64)

CompressData compresses a string to place into Consul's KV store.

func ComputeChecksum

func ComputeChecksum(data string) string

ComputeChecksum takes a string and computes a SHA256 checksum.

func Connect

func Connect(server string, token string) (*consul.Client, error)

Connect sets up a connection to Consul.

func DDAPIConnect

func DDAPIConnect(api, app string) *datadog.Client

DDAPIConnect connects to the Datadog API and returns a client object.

func DDCopyDataEvent

func DDCopyDataEvent(dd *datadog.Client, keyFrom, keyTo string)

DDCopyDataEvent sends a Datadog event to the API when we have used `kvexpress copy` to copy a Consul key.

func DDLengthEvent

func DDLengthEvent(dd *datadog.Client, key, value string)

DDLengthEvent sends a Datadog event to the API when the file/url is too short.

func DDSaveDataEvent

func DDSaveDataEvent(dd *datadog.Client, key, value string)

DDSaveDataEvent sends a Datadog event to the API when we have updated a Consul key.

func DDSaveStopEvent

func DDSaveStopEvent(dd *datadog.Client, key, value string)

DDSaveStopEvent sends a Datadog event when we have added a stop key to Consul.

func DDStopEvent

func DDStopEvent(dd *datadog.Client, key, value string)

DDStopEvent sends a Datadog event to the API when there's a stop key present.

func DecompressData

func DecompressData(data []byte, compressFlags uint64) string

DecompressData decompresses a string taken from Consul's KV store.

func Del

func Del(c *consul.Client, key string) bool

Del removes a key from the Consul KV store.

func FileLockPath

func FileLockPath(file string) string

FileLockPath generates the path for the KV store for a particular file.

func GenerateLockReason

func GenerateLockReason() string

GenerateLockReason creates a reason with filename, username and date.

func Get

func Get(c *consul.Client, key string) string

Get the value from a key in the Consul KV store.

func GetCurrentUsername

func GetCurrentUsername() string

GetCurrentUsername grabs the current user running the kvexpress binary.

func GetGroupID

func GetGroupID(owner string) int

GetGroupID looks up the Group Id for the owner passed.

func GetHostname

func GetHostname() string

GetHostname returns the hostname.

func GetOwnerID

func GetOwnerID(owner string) int

GetOwnerID looks up the User Id for the owner passed.

func GetRaw

func GetRaw(c *consul.Client, key string) ([]byte, uint64)

Get the raw value + flags from a key in the Consul KV store.

func GetStringConfig

func GetStringConfig(c *simpleyaml.Yaml, configValue string) string

GetStringConfig grabs the string from the config object.

func KeyPath

func KeyPath(key string, suffix string) string

KeyPath returns the standard kvexpress paths for data, checksum and stop.

func LastFilename

func LastFilename(file string) string

LastFilename returns a .last filename based on the passed file.

func LengthCheck

func LengthCheck(data string, minLength int) bool

LengthCheck makes sure a string has at least minLength lines.

func LineCount

func LineCount(data string) int

LineCount splits a string by linebreak and returns the number of lines.

func LoadConfig

func LoadConfig(filename string)

LoadConfig opens a file and reads the yaml formatted configuration data. It will set configuration globals and/or ENV variables as required.

func LockFile

func LockFile(key string) bool

LockFile sets a key in Consul so that a particular file won't be updated. See commands/lock.go

func LockFilePath

func LockFilePath(file string) string

LockFilePath generates a filename for the `$filename.locked` files used by `kvexpress lock` and `kvexpress unlock`

func LockFileRemove

func LockFileRemove(file string)

LockFileRemove removes a `$filename.locked` when running `kvexpress unlock`.

func LockFileWrite

func LockFileWrite(file string)

LockFileWrite writes a `$filename.locked` file with instructions for how to unlock.

func Log

func Log(message, priority string)

Log adds the global Direction to a message and sends to syslog. Syslog is setup in main.go

func LogFatal

func LogFatal(message string, id string, location string)

LogFatal prints to screen, sends to syslog, creates a fatal error and stops

func ParseConfig

func ParseConfig(data []byte) *simpleyaml.Yaml

ParseConfig takes the data from a file and parses the config.

func ParseStatsdAddress

func ParseStatsdAddress(address string) (string, int)

func RandomTmpFile

func RandomTmpFile() string

RandomTmpFile is used to create a .compare or .last file for UrltoRead()

func ReadFile

func ReadFile(filepath string) string

ReadFile reads a file in the filesystem and returns a string.

func ReadURL

func ReadURL(url string) string

ReadURL grabs a URL and returns the string from the body.

func RemoveFile

func RemoveFile(filename string)

RemoveFile takes a filename and stops if it's a directory. It will log success or failure of removal.

func Retry

func Retry(callback func() error, tries int)

Retry loops through the callback func and tries several times to do the thing.

func ReturnCurrentUTC

func ReturnCurrentUTC() string

ReturnCurrentUTC returns the current UTC time in RFC3339 format.

func RunCommand

func RunCommand(command string) bool

RunCommand runs a cli command with arguments.

func RunTime

func RunTime(start time.Time, key string, location string)

RunTime sends time coded logs and dogstatsd metrics when called. Location is set when the RunTime function is called.

func Set

func Set(c *consul.Client, key string, value string) bool

Set the value for a key in the Consul KV store.

func SetDirection

func SetDirection() string

SetDirection returns the direction.

func SetRaw

func SetRaw(c *consul.Client, key string, value []byte, flags uint64) bool

Set the value for a key in the Consul KV store.

func SortFile

func SortFile(file string) string

SortFile takes a string, splits it into lines, removes all blank lines using BlankLineStrip() and then sorts the remaining lines.

func StatsdChecksum

func StatsdChecksum(key string)

StatsdChecksum sends metrics to Dogstatsd on a `kvexpress out` operation where the checksum doesn't match.

func StatsdConsul

func StatsdConsul(key, location string)

StatsdConsul sends metrics to DogStatsd when Consul has a KV write or delete error.

func StatsdIn

func StatsdIn(key string, dataLength int, data string)

StatsdIn sends metrics to Dogstatsd on a `kvexpress in` operation.

func StatsdLength

func StatsdLength(key string)

StatsdLength sends metrics to Dogstatsd on a `kvexpress out` operation where the file isn't long enough.

func StatsdLock

func StatsdLock(key string)

StatsdLock sends metrics to Dogstatsd on a `kvexpress lock` operation.

func StatsdLocked

func StatsdLocked(file string)

StatsdLocked sends metrics to Dogstatsd on a `kvexpress out` operation that is blocked by a locked file.

func StatsdOut

func StatsdOut(key string)

StatsdOut sends metrics to Dogstatsd on a `kvexpress out` operation.

func StatsdPanic

func StatsdPanic(key, location string)

StatsdPanic sends metrics to Dogstatsd when something really bad happens. It also stops the execution of kvexpress.

func StatsdRaw

func StatsdRaw(key string)

StatsdRaw sends metrics to Dogstatsd on a `kvexpress raw` operation.

func StatsdReconnect

func StatsdReconnect(times int)

StatsdReconnect sends metrics when we have Consul connection retries.

func StatsdRunTime

func StatsdRunTime(key string, location string, msec int64)

StatsdRunTime sends metrics to Dogstatsd on various operations.

func StatsdSetup

func StatsdSetup() *godspeed.Godspeed

StatsdSetup sets up the connection to dogstatsd.

func StatsdUnlock

func StatsdUnlock(key string)

StatsdUnlock sends metrics to Dogstatsd on a `kvexpress unlock` operation.

func UnixDiff

func UnixDiff(old, new string) string

UnixDiff runs diff to generate text for the Datadog events.

func UnlockFile

func UnlockFile(key string) bool

UnlockFile removes a key in Consul so that a particular file can be updated. See commands/unlock.go

func WriteFile

func WriteFile(data string, filepath string, perms int, owner string)

WriteFile writes a string to a filepath. It also chowns the file to the owner and group of the user running the program if it's not set as a different user.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL