mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
cmd: allow to set arrays using environment variables
This commit is contained in:
15
cmd/serve.go
15
cmd/serve.go
@@ -7,6 +7,7 @@ import (
|
|||||||
netHTTP "net/http"
|
netHTTP "net/http"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
@@ -117,12 +118,20 @@ and exports them to Kafka.`,
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// From AKVORADO_SQUID_PURPLE_QUIRK=47, we
|
// From AKVORADO_SQUID_PURPLE_QUIRK=47, we
|
||||||
// build a map "squid -> purple -> quirk -> 47"
|
// build a map "squid -> purple -> quirk ->
|
||||||
|
// 47". From AKVORADO_SQUID_3_PURPLE=47, we
|
||||||
|
// build "squid[3] -> purple -> 47"
|
||||||
var rawConfig interface{}
|
var rawConfig interface{}
|
||||||
rawConfig = kv[1]
|
rawConfig = kv[1]
|
||||||
for i := len(kk) - 1; i > 0; i-- {
|
for i := len(kk) - 1; i > 0; i-- {
|
||||||
rawConfig = map[string]interface{}{
|
if index, err := strconv.Atoi(kk[i]); err == nil {
|
||||||
kk[i]: rawConfig,
|
newRawConfig := make([]interface{}, index+1)
|
||||||
|
newRawConfig[index] = rawConfig
|
||||||
|
rawConfig = newRawConfig
|
||||||
|
} else {
|
||||||
|
rawConfig = map[string]interface{}{
|
||||||
|
kk[i]: rawConfig,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := decoder.Decode(rawConfig); err != nil {
|
if err := decoder.Decode(rawConfig); err != nil {
|
||||||
|
|||||||
@@ -106,10 +106,15 @@ core:
|
|||||||
ioutil.WriteFile(configFile, []byte(config), 0644)
|
ioutil.WriteFile(configFile, []byte(config), 0644)
|
||||||
|
|
||||||
// Environment
|
// Environment
|
||||||
os.Setenv("AKVORADO_FLOW_WORKERS", "3")
|
|
||||||
os.Setenv("AKVORADO_SNMP_CACHEDURATION", "22m")
|
os.Setenv("AKVORADO_SNMP_CACHEDURATION", "22m")
|
||||||
os.Setenv("AKVORADO_SNMP_DEFAULTCOMMUNITY", "privateer")
|
os.Setenv("AKVORADO_SNMP_DEFAULTCOMMUNITY", "privateer")
|
||||||
os.Setenv("AKVORADO_KAFKA_BROKERS", "127.0.0.1:9092,127.0.0.2:9092")
|
os.Setenv("AKVORADO_KAFKA_BROKERS", "127.0.0.1:9092,127.0.0.2:9092")
|
||||||
|
os.Setenv("AKVORADO_FLOW_WORKERS", "3")
|
||||||
|
os.Setenv("AKVORADO_FLOW_INPUTS_0_LISTEN", "0.0.0.0:2056")
|
||||||
|
// We may be lucky or the environment is keeping order
|
||||||
|
os.Setenv("AKVORADO_FLOW_INPUTS_1_TYPE", "file")
|
||||||
|
os.Setenv("AKVORADO_FLOW_INPUTS_1_DECODER", "netflow")
|
||||||
|
os.Setenv("AKVORADO_FLOW_INPUTS_1_PATHS", "f1,f2")
|
||||||
|
|
||||||
// Start serves with it
|
// Start serves with it
|
||||||
root := cmd.RootCmd
|
root := cmd.RootCmd
|
||||||
@@ -127,8 +132,23 @@ core:
|
|||||||
if err := yaml.Unmarshal(buf.Bytes(), &got); err != nil {
|
if err := yaml.Unmarshal(buf.Bytes(), &got); err != nil {
|
||||||
t.Fatalf("Unmarshal() error:\n%+v", err)
|
t.Fatalf("Unmarshal() error:\n%+v", err)
|
||||||
}
|
}
|
||||||
want(t, got["flow"]["workers"], 3)
|
|
||||||
want(t, got["snmp"]["cacheduration"], "22m0s")
|
want(t, got["snmp"]["cacheduration"], "22m0s")
|
||||||
want(t, got["snmp"]["defaultcommunity"], "privateer")
|
want(t, got["snmp"]["defaultcommunity"], "privateer")
|
||||||
want(t, got["kafka"]["brokers"], []string{"127.0.0.1:9092", "127.0.0.2:9092"})
|
want(t, got["kafka"]["brokers"], []string{"127.0.0.1:9092", "127.0.0.2:9092"})
|
||||||
|
want(t, got["flow"], map[string]interface{}{
|
||||||
|
"inputs": []map[string]interface{}{
|
||||||
|
{
|
||||||
|
"type": "udp",
|
||||||
|
"decoder": "netflow",
|
||||||
|
"listen": "0.0.0.0:2056",
|
||||||
|
"queuesize": 100000,
|
||||||
|
"workers": 5,
|
||||||
|
}, {
|
||||||
|
"type": "file",
|
||||||
|
"decoder": "netflow",
|
||||||
|
"paths": []string{"f1", "f2"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"workers": 3,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,6 +112,10 @@ func ConfigurationUnmarshalerHook() mapstructure.DecodeHookFunc {
|
|||||||
|
|
||||||
// Alter config with a copy of the concrete type
|
// Alter config with a copy of the concrete type
|
||||||
original := reflect.Indirect(reflect.ValueOf(input))
|
original := reflect.Indirect(reflect.ValueOf(input))
|
||||||
|
if !configField.IsNil() && configField.Elem().Type().Elem() == reflect.TypeOf(input).Elem() {
|
||||||
|
// Use the value we already have instead of default.
|
||||||
|
original = reflect.Indirect(configField.Elem())
|
||||||
|
}
|
||||||
copy := reflect.New(original.Type())
|
copy := reflect.New(original.Type())
|
||||||
copy.Elem().Set(reflect.ValueOf(original.Interface()))
|
copy.Elem().Set(reflect.ValueOf(original.Interface()))
|
||||||
configField.Set(copy)
|
configField.Set(copy)
|
||||||
|
|||||||
@@ -107,7 +107,11 @@ func TestDecodeConfiguration(t *testing.T) {
|
|||||||
Workers: 10,
|
Workers: 10,
|
||||||
Inputs: []InputConfiguration{{
|
Inputs: []InputConfiguration{{
|
||||||
Decoder: "netflow",
|
Decoder: "netflow",
|
||||||
Config: &udp.DefaultConfiguration,
|
Config: &udp.Configuration{
|
||||||
|
Workers: 2,
|
||||||
|
QueueSize: 100,
|
||||||
|
Listen: "127.0.0.1:2055",
|
||||||
|
},
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
Source: map[string]interface{}{
|
Source: map[string]interface{}{
|
||||||
@@ -122,8 +126,8 @@ func TestDecodeConfiguration(t *testing.T) {
|
|||||||
Inputs: []InputConfiguration{{
|
Inputs: []InputConfiguration{{
|
||||||
Decoder: "netflow",
|
Decoder: "netflow",
|
||||||
Config: &udp.Configuration{
|
Config: &udp.Configuration{
|
||||||
Workers: 1,
|
Workers: 2,
|
||||||
QueueSize: 100000,
|
QueueSize: 100,
|
||||||
Listen: "192.0.2.1:2055",
|
Listen: "192.0.2.1:2055",
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
@@ -160,7 +164,7 @@ func TestDecodeConfiguration(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check we didn't alter the default value
|
// Check we didn't alter the default value for UDP
|
||||||
if diff := helpers.Diff(udp.DefaultConfiguration, udp.Configuration{
|
if diff := helpers.Diff(udp.DefaultConfiguration, udp.Configuration{
|
||||||
Workers: 1,
|
Workers: 1,
|
||||||
QueueSize: 100000,
|
QueueSize: 100000,
|
||||||
|
|||||||
Reference in New Issue
Block a user