diff options
author | Shashank Kumar Shankar <shashank.kumar.shankar@intel.com> | 2018-03-07 15:19:20 -0800 |
---|---|---|
committer | Shashank Kumar Shankar <shashank.kumar.shankar@intel.com> | 2018-03-08 16:58:57 -0800 |
commit | 086faa54cc995f9850c0946f42a83b58fed02046 (patch) | |
tree | 52fd5bf5ccab077d7120f15b9aae57a4cdcdf451 | |
parent | 5031a4e20a281b7a94f8c3743b1572314c574ec4 (diff) |
Update some fixes.
Adding some fixes to the docker builds and code.
Change-Id: Ia7446519ad64339eefd79975918b469052a27934
Issue-ID: MUSIC-41
Signed-off-by: Shashank Kumar Shankar <shashank.kumar.shankar@intel.com>
-rwxr-xr-x[-rw-r--r--] | deployment/docker-build.sh | 0 | ||||
-rwxr-xr-x[-rw-r--r--] | deployment/docker-entrypoint.sh | 8 | ||||
-rwxr-xr-x[-rw-r--r--] | deployment/run.sh | 2 | ||||
-rwxr-xr-x[-rw-r--r--] | deployment/setup-dependency.sh | 0 | ||||
-rw-r--r-- | src/dkv/api/backendPropertiesConnection.go | 62 | ||||
-rw-r--r-- | src/dkv/api/backendfakes.go | 14 | ||||
-rw-r--r-- | src/dkv/api/configHandlers.go | 10 | ||||
-rw-r--r-- | src/dkv/api/initialise.go | 2 | ||||
-rw-r--r-- | src/dkv/api/registrationHandlers.go | 2 |
9 files changed, 49 insertions, 51 deletions
diff --git a/deployment/docker-build.sh b/deployment/docker-build.sh index 767554f..767554f 100644..100755 --- a/deployment/docker-build.sh +++ b/deployment/docker-build.sh diff --git a/deployment/docker-entrypoint.sh b/deployment/docker-entrypoint.sh index 9b29e3e..0ec8d0e 100644..100755 --- a/deployment/docker-entrypoint.sh +++ b/deployment/docker-entrypoint.sh @@ -8,8 +8,6 @@ function start_consul_server { # Running consul in server mode since we are doing a single node. If we need to add more, # We need to run multiple consul agents in client mode without providing the -server arguements. - # CHANGE THIS TO SERVER MODE! - # consul agent -dev > /dev/null 2>&1 & consul agent -bootstrap -server -bind=127.0.0.1 -data-dir=/dkv/consul & } @@ -28,6 +26,8 @@ function set_paths { } set_paths -start_consul_server -sleep 5 +if [ "$CONSUL_IP" = "localhost" ]; then + start_consul_server + sleep 5 +fi start_api_server diff --git a/deployment/run.sh b/deployment/run.sh index 1aae1f6..2451ef9 100644..100755 --- a/deployment/run.sh +++ b/deployment/run.sh @@ -1,6 +1,6 @@ #!/bin/bash CONSUL_IP="localhost" -MOUNTPATH="/configs" +MOUNTPATH="/configs/" docker run -e CONSUL_IP=$CONSUL_IP -e MOUNTPATH=$MOUNTPATH -it --name dkv -p 8200:8200 -p 8080:8080 dkv diff --git a/deployment/setup-dependency.sh b/deployment/setup-dependency.sh index fcb2d51..fcb2d51 100644..100755 --- a/deployment/setup-dependency.sh +++ b/deployment/setup-dependency.sh diff --git a/src/dkv/api/backendPropertiesConnection.go b/src/dkv/api/backendPropertiesConnection.go index 24140cd..d485b40 100644 --- a/src/dkv/api/backendPropertiesConnection.go +++ b/src/dkv/api/backendPropertiesConnection.go @@ -22,32 +22,28 @@ import ( "io/ioutil" "log" "os" - "sync" ) type KeyValuesInterface interface { - WriteKVsToConsul(string, string) error - ConfigReader(string, string, string) error - ReadMultiplePropertiesRecursive(string) error - ReadMultipleProperties(string) error - ReadProperty(string) error + WriteKVsToConsul(string, string, map[string]string) error + ConfigReader(string, string, string) (map[string]string, error) + ReadMultiplePropertiesRecursive(string, *map[string]string) error + ReadMultipleProperties(string, *map[string]string) error + ReadProperty(string, *map[string]string) error } -type KeyValuesStruct struct { - sync.RWMutex - kvs map[string]string -} +type KeyValuesStruct struct{} var KeyValues KeyValuesInterface -func (kvStruct *KeyValuesStruct) WriteKVsToConsul(token string, subdomain string) error { +func (kvStruct *KeyValuesStruct) WriteKVsToConsul(token string, subdomain string, kvs map[string]string) error { var prefix = "" if subdomain != "" { prefix += token + "/" + subdomain } else { prefix += token + "/" } - for key, value := range kvStruct.kvs { + for key, value := range kvs { key = prefix + key err := Consul.RequestPUT(key, value) if err != nil { @@ -59,50 +55,48 @@ func (kvStruct *KeyValuesStruct) WriteKVsToConsul(token string, subdomain string return nil } -func (kvStruct *KeyValuesStruct) ConfigReader(token string, subdomain string, filename string) error { - defer kvStruct.Unlock() - - kvStruct.Lock() +func (kvStruct *KeyValuesStruct) ConfigReader(token string, subdomain string, filename string) (map[string]string, error) { var filepath = MOUNTPATH + kvs := make(map[string]string) if filename != "" && subdomain != "" { // Specific file in specific domain. filepath += token + "/" + subdomain + "/" + filename - err := kvStruct.ReadProperty(filepath) + err := kvStruct.ReadProperty(filepath, &kvs) if err != nil { - return err + return kvs, err } - return nil + return kvs, nil } if filename != "" && subdomain == "" { // Specific file in Token filepath += token + "/" + filename - err := kvStruct.ReadProperty(filepath) + err := kvStruct.ReadProperty(filepath, &kvs) if err != nil { - return err + return kvs, err } - return nil + return kvs, nil } if filename == "" && subdomain != "" { // All files in specific domain filepath += token + "/" + subdomain - err := kvStruct.ReadMultipleProperties(filepath) + err := kvStruct.ReadMultipleProperties(filepath, &kvs) if err != nil { - return err + return kvs, err } } filepath += token - err := kvStruct.ReadMultiplePropertiesRecursive(filepath) + err := kvStruct.ReadMultiplePropertiesRecursive(filepath, &kvs) if err != nil { - return err + return kvs, err } - return nil + return kvs, nil } -func (kvStruct *KeyValuesStruct) ReadMultiplePropertiesRecursive(path string) error { +func (kvStruct *KeyValuesStruct) ReadMultiplePropertiesRecursive(path string, kvs *map[string]string) error { // Go inside each sub directory and run ReadMultipleProperties inside. files, err := ioutil.ReadDir(path) if err != nil { @@ -112,35 +106,35 @@ func (kvStruct *KeyValuesStruct) ReadMultiplePropertiesRecursive(path string) er for _, f := range files { fi, _ := os.Stat(path + "/" + f.Name()) if fi.Mode().IsDir() { - kvStruct.ReadMultipleProperties(path + "/" + f.Name()) + kvStruct.ReadMultipleProperties(path+"/"+f.Name(), kvs) } else { - kvStruct.ReadProperty(path + "/" + f.Name()) + kvStruct.ReadProperty(path+"/"+f.Name(), kvs) } } return nil } -func (kvStruct *KeyValuesStruct) ReadMultipleProperties(path string) error { +func (kvStruct *KeyValuesStruct) ReadMultipleProperties(path string, kvs *map[string]string) error { files, err := ioutil.ReadDir(path) if err != nil { return err } for _, f := range files { - kvStruct.ReadProperty(path + f.Name()) + kvStruct.ReadProperty(path+f.Name(), kvs) } return nil } -func (kvStruct *KeyValuesStruct) ReadProperty(path string) error { +func (kvStruct *KeyValuesStruct) ReadProperty(path string, kvs *map[string]string) error { _, err := os.Stat(path) if err != nil { return errors.New("File does not exists.") } p := properties.MustLoadFile(path, properties.UTF8) for _, key := range p.Keys() { - kvStruct.kvs[key] = p.MustGet(key) + (*kvs)[key] = p.MustGet(key) } return nil } diff --git a/src/dkv/api/backendfakes.go b/src/dkv/api/backendfakes.go index 5415608..c5ca39a 100644 --- a/src/dkv/api/backendfakes.go +++ b/src/dkv/api/backendfakes.go @@ -73,11 +73,12 @@ type FakeKeyValues struct { KeyValuesStruct } -func (f *FakeKeyValues) ConfigReader(token string, subdomain string, filename string) error { - return nil +func (f *FakeKeyValues) ConfigReader(token string, subdomain string, filename string) (map[string]string, error) { + kvs := make(map[string]string) + return kvs, nil } -func (f *FakeKeyValues) WriteKVsToConsul(token string, subdomain string) error { +func (f *FakeKeyValues) WriteKVsToConsul(token string, subdomain string, kvs map[string]string) error { return nil } @@ -86,11 +87,12 @@ type FakeKeyValuesErr struct { KeyValuesStruct } -func (f *FakeKeyValuesErr) ConfigReader(token string, subdomain string, filename string) error { - return errors.New("Internal Server Error") +func (f *FakeKeyValuesErr) ConfigReader(token string, subdomain string, filename string) (map[string]string, error) { + kvs := make(map[string]string) + return kvs, errors.New("Internal Server Error") } -func (f *FakeKeyValuesErr) WriteKVsToConsul(token string, subdomain string) error { +func (f *FakeKeyValuesErr) WriteKVsToConsul(token string, subdomain string, kvs map[string]string) error { return errors.New("Internal Server Error") } diff --git a/src/dkv/api/configHandlers.go b/src/dkv/api/configHandlers.go index f5bac34..7178433 100644 --- a/src/dkv/api/configHandlers.go +++ b/src/dkv/api/configHandlers.go @@ -81,6 +81,8 @@ func HandleConfigUpload(w http.ResponseWriter, r *http.Request) { } defer f.Close() io.Copy(f, file) + + GenerateResponse(w, r, http.StatusOK, "Configuration uploaded to Token: "+token) } func HandleConfigLoad(w http.ResponseWriter, r *http.Request) { @@ -102,14 +104,14 @@ func HandleConfigLoad(w http.ResponseWriter, r *http.Request) { return } - err = KeyValues.ConfigReader(body.Token, body.Subdomain, body.Filename) + kvs_map, err := KeyValues.ConfigReader(body.Token, body.Subdomain, body.Filename) if err != nil { GenerateResponse(w, r, http.StatusInternalServerError, string(err.Error())) return } - err = KeyValues.WriteKVsToConsul(body.Token, body.Subdomain) + err = KeyValues.WriteKVsToConsul(body.Token, body.Subdomain, kvs_map) if err != nil { GenerateResponse(w, r, http.StatusInternalServerError, string(err.Error())) @@ -119,12 +121,12 @@ func HandleConfigLoad(w http.ResponseWriter, r *http.Request) { } func HandleDefaultConfigLoad(w http.ResponseWriter, r *http.Request) { - err := KeyValues.ConfigReader("default", "", "") + kvs_map, err := KeyValues.ConfigReader("default", "", "") if err != nil { GenerateResponse(w, r, http.StatusInternalServerError, string(err.Error())) return } - err = KeyValues.WriteKVsToConsul("default", "") + err = KeyValues.WriteKVsToConsul("default", "", kvs_map) if err != nil { GenerateResponse(w, r, http.StatusInternalServerError, string(err.Error())) } else { diff --git a/src/dkv/api/initialise.go b/src/dkv/api/initialise.go index dfbcbde..f4edc6c 100644 --- a/src/dkv/api/initialise.go +++ b/src/dkv/api/initialise.go @@ -20,7 +20,7 @@ import "os" func Initialise() error { Consul = &ConsulStruct{} - KeyValues = &KeyValuesStruct{kvs: make(map[string]string)} + KeyValues = &KeyValuesStruct{} Directory = &DirectoryStruct{directory: ""} err := Consul.InitializeConsulClient() diff --git a/src/dkv/api/registrationHandlers.go b/src/dkv/api/registrationHandlers.go index 1c50b33..0f697db 100644 --- a/src/dkv/api/registrationHandlers.go +++ b/src/dkv/api/registrationHandlers.go @@ -91,7 +91,7 @@ func HandleServiceGet(w http.ResponseWriter, r *http.Request) { if found == true { GenerateResponse(w, r, http.StatusOK, service) } else { - GenerateResponse(w, r, http.StatusNotFound, "Service for Token:"+token+"not found.") + GenerateResponse(w, r, http.StatusNotFound, "Service for Token: "+token+" not found.") } } |