config abstraction changes added

This commit is contained in:
2023-03-22 09:43:40 +00:00
parent 88dddd0230
commit 69c0aa5cb1
14 changed files with 2015 additions and 1924 deletions

View File

@@ -206,7 +206,7 @@ func (grp *Group) AddGroupToFile() error {
// result to Groups
func ReadGroup() (*Groups, error) {
// Get Path from config
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return nil, err
}
@@ -234,7 +234,7 @@ func (grp *Groups) WriteGroup() error {
return err
}
// Get Path from config
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}

View File

@@ -29,7 +29,7 @@ func AddTrackContainer(d *docker.DockerVM, ipAddress string) error {
return errors.New("d is nil")
}
//Get config information to derive paths for track containers json file
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}
@@ -81,7 +81,7 @@ func AddTrackContainer(d *docker.DockerVM, ipAddress string) error {
// RemoveTrackedContainer This function removos tracked container from the trackcontainer JSON file
func RemoveTrackedContainer(id string) error {
//Get config information to derive paths for track containers json file
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}
@@ -118,7 +118,7 @@ func RemoveTrackedContainer(id string) error {
// ViewTrackedContainers View Containers currently tracked
func ViewTrackedContainers() (error, *TrackContainers) {
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err, nil
}
@@ -194,7 +194,7 @@ func (TC *TrackContainer) ModifyContainerInformation() error {
// WriteContainers Write information back to the config file
func (TC *TrackContainers) WriteContainers() error {
// Initialize config file
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
// write modified information to the tracked json file
data, err := json.MarshalIndent(TC, "", "\t")
if err != nil {

View File

@@ -18,7 +18,7 @@ var mu sync.Mutex
// UpdateIpTable Does the following to update it's IP table
func UpdateIpTable(IpAddress string, serverPort string, wg *sync.WaitGroup) error {
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}
@@ -78,7 +78,7 @@ func UpdateIpTable(IpAddress string, serverPort string, wg *sync.WaitGroup) erro
// on the ip tables
func UpdateIpTableListClient() error {
// Get config information
Config, err := config.ConfigInit(nil)
Config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}

View File

@@ -135,7 +135,7 @@ var CliAction = func(ctx *cli.Context) error {
//Sets default paths to the config file
if SetDefaultConfig {
_, err := generate.SetDefaults("P2PRC", false)
_, err := generate.SetDefaults("P2PRC", false, nil, true)
if err != nil {
fmt.Print(err)
}

View File

@@ -1,7 +1,8 @@
package config
import (
"github.com/spf13/viper"
"encoding/json"
"io/ioutil"
"os"
)
@@ -28,6 +29,7 @@ type Config struct {
GroupTrackContainersPath string
FRPServerPort string
BehindNAT string
CustomConfig interface{}
//NetworkInterface string
//NetworkInterfaceIPV6Index int
}
@@ -41,6 +43,9 @@ func GetPathP2PRC(Envname string) (string, error) {
}
}
curDir := os.Getenv(defaultEnvName)
if curDir == "" {
return curDir, nil
}
return curDir + "/", nil
}
@@ -59,45 +64,93 @@ func GetEnvName() string {
}
// ConfigInit Pass environment name as an optional parameter
func ConfigInit(defaultsParameter map[string]interface{}, envNameOptional ...string) (*Config, error) {
func ConfigInit(defaultsParameter map[string]interface{}, CustomConfig interface{}, envNameOptional ...string) (*Config, error) {
if len(envNameOptional) > 0 {
defaultEnvName = envNameOptional[0]
}
//
////Setting current directory to default path
//defaultPath, err := GetPathP2PRC(defaultEnvName)
//if err != nil {
// return nil, err
//}
////Paths to search for config file
//configPaths = append(configPaths, defaultPath)
//
////Add all possible configurations paths
//for _, v := range configPaths {
// viper.AddConfigPath(v)
//}
//
////Read config file
//if err := viper.ReadInConfig(); err != nil {
// // If the error thrown is config file not found
// //Sets default configuration to viper
// for k, v := range defaults {
// viper.SetDefault(k, v)
// }
// viper.SetConfigName(configName)
// viper.SetConfigFile(configFile)
// viper.SetConfigType(configType)
//
// if err = viper.WriteConfig(); err != nil {
// return nil, err
// }
//}
//
//// Adds configuration to the struct
//var config Config
//if err := viper.Unmarshal(&config); err != nil {
// return nil, err
//}
//
//return &config, nil
//Setting current directory to default path
defaultPath, err := GetPathP2PRC(defaultEnvName)
if err != nil {
return nil, err
}
//Paths to search for config file
configPaths = append(configPaths, defaultPath)
//Add all possible configurations paths
for _, v := range configPaths {
viper.AddConfigPath(v)
}
//Read config file
if err := viper.ReadInConfig(); err != nil {
// If the error thrown is config file not found
//Sets default configuration to viper
for k, v := range defaults {
viper.SetDefault(k, v)
}
viper.SetConfigName(configName)
viper.SetConfigFile(configFile)
viper.SetConfigType(configType)
if err = viper.WriteConfig(); err != nil {
// Open our jsonFile
jsonFile, err := os.Open(defaultPath + configFile)
// if we os.Open returns an error then handle it
if err != nil {
return nil, err
}
}
// Adds configuration to the struct
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var config Config
if err := viper.Unmarshal(&config); err != nil {
json.Unmarshal(byteValue, &config)
if CustomConfig != nil {
// Convert Custom Config to byte
customConfigByte, err := json.Marshal(config.CustomConfig)
if err != nil {
return nil, err
}
// Again map the byte to the CustomConfig interface
json.Unmarshal(customConfigByte, &CustomConfig)
}
return &config, nil
}
func (c *Config) WriteConfig() error {
//Getting Current Directory from environment variable
//curDir := os.Getenv("REMOTEGAMING")
defaultPath, err := GetPathP2PRC(defaultEnvName)
if err != nil {
return err
}
file, _ := json.MarshalIndent(c, "", " ")
_ = ioutil.WriteFile(defaultPath+"config.json", file, 0644)
return nil
}

View File

@@ -37,7 +37,7 @@ func GetCurrentPath() (string, error) {
// SetDefaults This function to be called only during a
// make install
func SetDefaults(envName string, forceDefault bool, ConfigUpdate ...*config.Config) (*config.Config, error) {
func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, NoBoilerPlate bool, ConfigUpdate ...*config.Config) (*config.Config, error) {
//Setting current directory to default path
defaultPath, err := GetCurrentPath()
if err != nil {
@@ -68,38 +68,31 @@ func SetDefaults(envName string, forceDefault bool, ConfigUpdate ...*config.Conf
// return err
//}
var Defaults config.Config
if len(ConfigUpdate) == 0 {
//Setting default paths for the config file
defaults["IPTable"] = defaultPath + "p2p/iptable/ip_table.json"
defaults["DefaultDockerFile"] = defaultPath + "server/docker/containers/docker-ubuntu-sshd/"
defaults["DockerContainers"] = defaultPath + "server/docker/containers/"
defaults["SpeedTestFile"] = defaultPath + "p2p/50.bin"
defaults["IPV6Address"] = ""
defaults["PluginPath"] = defaultPath + "plugin/deploy"
defaults["TrackContainersPath"] = defaultPath + "client/trackcontainers/trackcontainers.json"
defaults["GroupTrackContainersPath"] = defaultPath + "client/trackcontainers/grouptrackcontainers.json"
defaults["ServerPort"] = "8088"
defaults["FRPServerPort"] = "0"
defaults["BehindNAT"] = "True"
Defaults.IPTable = defaultPath + "p2p/iptable/ip_table.json"
Defaults.DefaultDockerFile = defaultPath + "server/docker/containers/docker-ubuntu-sshd/"
Defaults.DockerContainers = defaultPath + "server/docker/containers/"
Defaults.SpeedTestFile = defaultPath + "p2p/50.bin"
Defaults.IPV6Address = ""
Defaults.PluginPath = defaultPath + "plugin/deploy"
Defaults.TrackContainersPath = defaultPath + "client/trackcontainers/trackcontainers.json"
Defaults.GroupTrackContainersPath = defaultPath + "client/trackcontainers/grouptrackcontainers.json"
Defaults.ServerPort = "8088"
Defaults.FRPServerPort = "True"
Defaults.CustomConfig = CustomConfig
Defaults.BehindNAT = "True"
// Random name generator
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
defaults["MachineName"] = hostname
Defaults.MachineName = hostname
} else {
defaults["IPTable"] = ConfigUpdate[0].IPTable
defaults["DefaultDockerFile"] = ConfigUpdate[0].DefaultDockerFile
defaults["DockerContainers"] = ConfigUpdate[0].DockerContainers
defaults["SpeedTestFile"] = ConfigUpdate[0].SpeedTestFile
defaults["IPV6Address"] = ConfigUpdate[0].IPV6Address
defaults["PluginPath"] = ConfigUpdate[0].PluginPath
defaults["TrackContainersPath"] = ConfigUpdate[0].TrackContainersPath
defaults["GroupTrackContainersPath"] = ConfigUpdate[0].GroupTrackContainersPath
defaults["ServerPort"] = ConfigUpdate[0].ServerPort
defaults["FRPServerPort"] = ConfigUpdate[0].FRPServerPort
defaults["BehindNAT"] = ConfigUpdate[0].BehindNAT
Defaults = *ConfigUpdate[0]
}
//defaults["NetworkInterface"] = "wlp0s20f3"
@@ -115,15 +108,24 @@ func SetDefaults(envName string, forceDefault bool, ConfigUpdate ...*config.Conf
}
}
//Calling configuration file
Config, err := config.ConfigInit(defaults, envName)
// write defaults to the config file
err = Defaults.WriteConfig()
if err != nil {
return nil, err
}
//Calling configuration file
Config, err := config.ConfigInit(defaults, nil, envName)
if err != nil {
return nil, err
}
if !NoBoilerPlate {
err = GenerateFiles()
if err != nil {
return nil, err
}
}
return Config, nil
}

View File

@@ -0,0 +1,35 @@
package generate
import (
"fmt"
"github.com/Akilan1999/p2p-rendering-computation/config"
"testing"
)
type CustomConfig struct {
Test string
}
// Test case to generate defaults with custom data-structure
func TestSetDefaults(t *testing.T) {
setDefaults, err := SetDefaults("", true, &CustomConfig{Test: "lol"}, true)
if err != nil {
fmt.Println(err)
t.Fail()
return
}
fmt.Println(setDefaults)
var c CustomConfig
_, err = config.ConfigInit(nil, &c)
if err != nil {
fmt.Println(err)
t.Fail()
return
}
fmt.Println(c)
}

View File

@@ -27,6 +27,7 @@ type IpAddress struct {
ServerPort string `json:"ServerPort"`
NAT string `json:"NAT"`
EscapeImplementation string `json:"EscapeImplementation"`
CustomInformation []byte
}
type IP struct {
@@ -36,7 +37,7 @@ type IP struct {
// ReadIpTable Read data from Ip tables from json file
func ReadIpTable() (*IpAddresses, error) {
// Get Path from config
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return nil, err
}
@@ -101,7 +102,7 @@ func (i *IpAddresses) WriteIpTable() error {
}
// Get Path from config
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}
@@ -189,7 +190,7 @@ func CurrentPublicIP() (string, error) {
// GetCurrentIPV6 gets the current IPV6 address based on the interface
// specified in the config file
func GetCurrentIPV6() (string, error) {
Config, err := config.ConfigInit(nil)
Config, err := config.ConfigInit(nil, nil)
if err != nil {
return "", err
}

View File

@@ -99,7 +99,7 @@ func (s *IpAddress) UploadSpeed() error {
// Get upload file path from config file
// Get Path from config
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}

View File

@@ -24,7 +24,7 @@ func DownloadPlugin(pluginurl string) error {
// Replaces / with _
folder := strings.Replace(path, "/", "_", -1)
// Reads plugin path from the config path
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}
@@ -44,7 +44,7 @@ func DownloadPlugin(pluginurl string) error {
// DeletePlugin The following function deletes a plugin based on
// the plugin name provided.
func DeletePlugin(pluginname string) error {
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}

View File

@@ -67,7 +67,7 @@ type Host struct {
// DetectPlugins Detects all the plugins available
func DetectPlugins() (*Plugins, error) {
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return nil, err
}
@@ -141,7 +141,7 @@ func RunPlugin(pluginName string, IPAddresses []*ExecuteIP) (*Plugin, error) {
plugindetected = plugin
plugindetected.Execute = IPAddresses
// Get Execute plugin path from config file
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return nil, err
}

View File

@@ -74,7 +74,7 @@ func TestExecuteIP_ModifyHost(t *testing.T) {
var testip ExecuteIP
// Get plugin path from config file
Config, err := config.ConfigInit(nil)
Config, err := config.ConfigInit(nil, nil)
if err != nil {
fmt.Println(err)
t.Fail()

View File

@@ -84,7 +84,7 @@ func BuildRunContainer(NumPorts int, GPU string, ContainerName string) (*DockerV
//Default parameters
RespDocker.TagName = "p2p-ubuntu"
// Get Path from config
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return nil, err
}
@@ -330,7 +330,7 @@ func StopAndRemoveContainer(containername string) error {
// ViewAllContainers returns all containers runnable and which can be built
func ViewAllContainers() (*DockerContainers, error) {
// Traverse the deploy path as per given in the config file
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return nil, err
}

View File

@@ -19,7 +19,7 @@ func Server() (*gin.Engine, error) {
r := gin.Default()
//Get Server port based on the config file
config, err := config.ConfigInit(nil)
config, err := config.ConfigInit(nil, nil)
if err != nil {
return nil, err
}