khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame^] | 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | //"context" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | //dt "github.com/docker/docker/api/types" |
| 8 | //dc "github.com/docker/docker/client" |
| 9 | "os" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // Constants used to differentiate between the KV stores |
| 14 | const ( |
| 15 | ConsulStoreName string = "consul" |
| 16 | EtcdStoreName string = "etcd" |
| 17 | ) |
| 18 | |
| 19 | // CoordinatorFlags represents the set of configurations used by the coordinator |
| 20 | type RWCoreFlags struct { |
| 21 | // Command line parameters |
| 22 | InstanceID string |
| 23 | KVStoreType string |
| 24 | KVStoreTimeout int // in seconds |
| 25 | KVStoreHost string |
| 26 | KVStorePort int |
| 27 | LogLevel string |
| 28 | } |
| 29 | |
| 30 | // NewRWCoreFlags returns a new coordinator config |
| 31 | func NewRWCoreFlags() *RWCoreFlags { |
| 32 | var rwCoreFlag = RWCoreFlags{ // Default values |
| 33 | InstanceID: "rw_coreInstance001", |
| 34 | KVStoreType: ConsulStoreName, |
| 35 | KVStoreTimeout: 5, |
| 36 | KVStoreHost: "10.100.198.240", |
| 37 | //KVStorePort: 2379, |
| 38 | KVStorePort: 8500, |
| 39 | LogLevel: "info", |
| 40 | } |
| 41 | return &rwCoreFlag |
| 42 | } |
| 43 | |
| 44 | // ParseCommandArguments parses the arguments when running coordinator |
| 45 | func (cf *RWCoreFlags) ParseCommandArguments() { |
| 46 | flag.IntVar(&(cf.KVStoreTimeout), |
| 47 | "kv-store-request-timeout", |
| 48 | cf.KVStoreTimeout, |
| 49 | "The default timeout when making a kv store request") |
| 50 | |
| 51 | flag.StringVar(&(cf.KVStoreType), |
| 52 | "kv-store-type", |
| 53 | cf.KVStoreType, |
| 54 | "KV store type") |
| 55 | |
| 56 | flag.StringVar(&(cf.KVStoreHost), |
| 57 | "kv-store-host", |
| 58 | cf.KVStoreHost, |
| 59 | "KV store host") |
| 60 | |
| 61 | flag.IntVar(&(cf.KVStorePort), |
| 62 | "kv-store-port", |
| 63 | cf.KVStorePort, |
| 64 | "KV store port") |
| 65 | |
| 66 | flag.StringVar(&(cf.LogLevel), |
| 67 | "log-level", |
| 68 | cf.LogLevel, |
| 69 | "Log level") |
| 70 | |
| 71 | flag.Parse() |
| 72 | |
| 73 | // Update the necessary keys with the prefixes |
| 74 | start := time.Now() |
| 75 | containerName := getContainerInfo() |
| 76 | fmt.Println("container name:", containerName) |
| 77 | if len(containerName) > 0 { |
| 78 | cf.InstanceID = containerName |
| 79 | } |
| 80 | |
| 81 | fmt.Println("Inside config:", cf) |
| 82 | elapsed := time.Since(start) |
| 83 | fmt.Println("time:", elapsed/time.Second) |
| 84 | } |
| 85 | |
| 86 | func getContainerInfo() string { |
| 87 | return os.Getenv("HOSTNAME") |
| 88 | } |