added config flixble system

This commit is contained in:
2023-02-22 02:46:11 +00:00
parent c7a665fdc8
commit 4dac4234ad
22 changed files with 440 additions and 799 deletions

View File

@@ -1,75 +1,47 @@
package config
import (
"github.com/spf13/viper"
"io"
"os"
"github.com/spf13/viper"
"os"
)
var (
defaultPath string
defaults = map[string]interface{}{}
configName = "config"
configType = "json"
configFile = "config.json"
configPaths []string
defaultEnvName = "P2PRC"
//defaultPath string
defaults = map[string]interface{}{}
configName = "config"
configType = "json"
configFile = "config.json"
configPaths []string
defaultEnvName = "P2PRC"
)
type Config struct {
MachineName string
IPTable string
DockerContainers string
DefaultDockerFile string
SpeedTestFile string
IPV6Address string
PluginPath string
TrackContainersPath string
ServerPort string
GroupTrackContainersPath string
FRPServerPort string
BehindNAT string
//NetworkInterface string
//NetworkInterfaceIPV6Index int
}
// Exists reports whether the named file or directory exists.
func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// Copy the src file to dst. Any existing file will be overwritten and will not
// copy file attributes.
// Source: https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file
func Copy(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
MachineName string
IPTable string
DockerContainers string
DefaultDockerFile string
SpeedTestFile string
IPV6Address string
PluginPath string
TrackContainersPath string
ServerPort string
GroupTrackContainersPath string
FRPServerPort string
BehindNAT string
//NetworkInterface string
//NetworkInterfaceIPV6Index int
}
// GetPathP2PRC Getting P2PRC Directory from environment variable
func GetPathP2PRC() (string, error) {
curDir := os.Getenv(defaultEnvName)
return curDir + "/", nil
func GetPathP2PRC(Envname string) (string, error) {
if Envname != "" {
err := SetEnvName(Envname)
if err != nil {
return "", err
}
}
curDir := os.Getenv(defaultEnvName)
return curDir + "/", nil
}
// SetEnvName Sets the environment name
@@ -77,120 +49,55 @@ func GetPathP2PRC() (string, error) {
// your environment variable
// This is useful when extending the use case of P2PRC
func SetEnvName(EnvName string) error {
defaultEnvName = EnvName
// Handling error to be implemented only if needed
return nil
defaultEnvName = EnvName
// Handling error to be implemented only if needed
return nil
}
// GetCurrentPath Getting P2PRC Directory from environment variable
func GetCurrentPath() (string, error) {
curDir := os.Getenv("PWD")
return curDir + "/", nil
}
// SetDefaults This function to be called only during a
// make install
func SetDefaults() error {
//Setting current directory to default path
defaultPath, err := GetPathP2PRC()
if err != nil {
return err
}
//Creates ip_table.json in the json directory
err = Copy("p2p/ip_table.json", "p2p/iptable/ip_table.json")
if err != nil {
return err
}
//Creates a copy of trackcontainers.json in the appropriate directory
err = Copy("client/trackcontainers.json", "client/trackcontainers/trackcontainers.json")
if err != nil {
return err
}
//Creates a copy of trackcontainers.json in the appropriate directory
err = Copy("client/grouptrackcontainers.json", "client/trackcontainers/grouptrackcontainers.json")
if err != nil {
return err
}
//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"
// Random name generator
hostname, err := os.Hostname()
if err != nil {
return err
}
defaults["MachineName"] = hostname
//defaults["NetworkInterface"] = "wlp0s20f3"
//defaults["NetworkInterfaceIPV6Index"] = "2"
//Paths to search for config file
configPaths = append(configPaths, defaultPath)
if fileExists(defaultPath + "config.json") {
err := os.Remove(defaultPath + "config.json")
if err != nil {
return err
}
}
//Calling configuration file
_, err = ConfigInit()
if err != nil {
return err
}
return nil
}
func ConfigInit() (*Config, error) {
//Setting current directory to default path
defaultPath, err := GetPathP2PRC()
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
// ConfigInit Pass environment name as an optional parameter
func ConfigInit(defaultsParameter map[string]interface{}, envNameOptional ...string) (*Config, error) {
if len(envNameOptional) > 0 {
defaultEnvName = envNameOptional[0]
}
if defaultsParameter != nil {
defaults = defaultsParameter
}
//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
}

View File

@@ -7,7 +7,7 @@ import (
)
func TestConfigInit(t *testing.T) {
_,err := ConfigInit()
_, err := ConfigInit(nil)
if err != nil {
t.Error(err)
}
@@ -60,4 +60,4 @@ func TestSetEnvName(t *testing.T) {
t.Error(err)
}
fmt.Println(path)
}
}

114
config/generate/generate.go Normal file
View File

@@ -0,0 +1,114 @@
package generate
import (
"github.com/Akilan1999/p2p-rendering-computation/config"
"os"
)
var (
defaults = map[string]interface{}{}
configPaths []string
defaultEnvName = "P2PRC"
)
// GetPathP2PRC Getting P2PRC Directory from environment variable
func GetPathP2PRC() (string, error) {
curDir := os.Getenv(defaultEnvName)
return curDir + "/", nil
}
// SetEnvName Sets the environment name
// This is to ensure that the Path of your project is detected from
// your environment variable
// This is useful when extending the use case of P2PRC
func SetEnvName(EnvName string) error {
if EnvName != "" {
defaultEnvName = EnvName
}
// Handling error to be implemented only if needed
return nil
}
// GetCurrentPath Getting P2PRC Directory from environment variable
func GetCurrentPath() (string, error) {
curDir := os.Getenv("PWD")
return curDir + "/", nil
}
// SetDefaults This function to be called only during a
// make install
func SetDefaults(envName string) error {
//Setting current directory to default path
defaultPath, err := GetCurrentPath()
if err != nil {
return err
}
// Set Env name
err = SetEnvName(envName)
if err != nil {
return err
}
////Creates ip_table.json in the json directory
//err = Copy("p2p/ip_table.json", "p2p/iptable/ip_table.json")
//if err != nil {
// return err
//}
//
////Creates a copy of trackcontainers.json in the appropriate directory
//err = Copy("client/trackcontainers.json", "client/trackcontainers/trackcontainers.json")
//if err != nil {
// return err
//}
//
////Creates a copy of trackcontainers.json in the appropriate directory
//err = Copy("client/grouptrackcontainers.json", "client/trackcontainers/grouptrackcontainers.json")
//if err != nil {
// return err
//}
//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"
// Random name generator
hostname, err := os.Hostname()
if err != nil {
return err
}
defaults["MachineName"] = hostname
//defaults["NetworkInterface"] = "wlp0s20f3"
//defaults["NetworkInterfaceIPV6Index"] = "2"
//Paths to search for config file
configPaths = append(configPaths, defaultPath)
if fileExists(defaultPath + "config.json") {
err := os.Remove(defaultPath + "config.json")
if err != nil {
return err
}
}
//Calling configuration file
_, err = config.ConfigInit(defaults, envName)
if err != nil {
return err
}
err = GenerateFiles()
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,159 @@
package generate
import (
"github.com/Akilan1999/p2p-rendering-computation/p2p"
"os"
)
// GenerateFiles Generates all the files needed to setup P2PRC
func GenerateFiles(rootNodes ...p2p.IpAddress) (err error) {
err = GenerateIPTableFile(rootNodes)
err = GenerateDockerFiles()
err = GeneratePluginDirectory()
err = GenerateClientTrackContainers()
return
}
// GenerateIPTableFile Generates the IPTable file with the appropirate root node
func GenerateIPTableFile(rootNodes []p2p.IpAddress) (err error) {
var rootnodes p2p.IpAddresses
var rootnode p2p.IpAddress
err = CreateIPTableFolderStructure()
if err != nil {
return err
}
// If root node addresses are not provided as optional parameters
if len(rootNodes) <= 0 {
rootnode.Name = "Node1"
rootnode.ServerPort = "8088"
rootnode.NAT = "False"
rootnode.Ipv4 = "64.227.168.102"
rootnodes.IpAddress = append(rootnodes.IpAddress, rootnode)
} else {
// if root nodes are provided then override them as the optional parameter
for i := range rootNodes {
rootnodes.IpAddress = append(rootnodes.IpAddress, rootNodes[i])
}
}
err = rootnodes.WriteIpTable()
return
}
// CreateIPTableFolderStructure Create folder structure for IPTable
func CreateIPTableFolderStructure() (err error) {
path, err := GetCurrentPath()
if err != nil {
return err
}
if _, err = os.Stat(path + "p2p"); os.IsNotExist(err) {
if err = os.Mkdir(path+"p2p", os.ModePerm); err != nil {
return err
}
if _, err = os.Stat(path + "p2p/iptable"); os.IsNotExist(err) {
if err = os.Mkdir(path+"p2p/iptable", os.ModePerm); err != nil {
return err
}
}
if _, err = os.Stat(path + "p2p/iptable/ip_table.json"); os.IsNotExist(err) {
_, err = os.Create(path + "p2p/iptable/ip_table.json")
if err != nil {
return err
}
}
}
return
}
// GenerateDockerFiles Generate default docker files
func GenerateDockerFiles() (err error) {
path, err := GetCurrentPath()
if err != nil {
return err
}
if _, err = os.Stat(path + "server"); os.IsNotExist(err) {
if err = os.Mkdir(path+"server", os.ModePerm); err != nil {
return err
}
if _, err = os.Stat(path + "server/docker"); os.IsNotExist(err) {
if err = os.Mkdir(path+"server/docker", os.ModePerm); err != nil {
return err
}
}
if _, err = os.Stat(path + "server/docker/containers"); os.IsNotExist(err) {
if err = os.Mkdir(path+"server/docker/containers", os.ModePerm); err != nil {
return err
}
}
if _, err = os.Stat(path + "server/docker/containers"); os.IsNotExist(err) {
if err = os.Mkdir(path+"server/docker/containers", os.ModePerm); err != nil {
return err
}
}
if _, err = os.Stat(path + "server/docker/containers/docker-ubuntu-sshd"); os.IsNotExist(err) {
if err = os.Mkdir(path+"server/docker/containers/docker-ubuntu-sshd", os.ModePerm); err != nil {
return err
}
}
//if _, err = os.Stat(path + "server/docker/ip_table.json"); os.IsNotExist(err) {
// _, err = os.Create(path + "p2p/iptable/ip_table.json")
// if err != nil {
// return err
// }
//}
}
return
}
// GeneratePluginDirectory Generates plugin directory structure
func GeneratePluginDirectory() (err error) {
path, err := GetCurrentPath()
if err != nil {
return err
}
if _, err = os.Stat(path + "plugin"); os.IsNotExist(err) {
if err = os.Mkdir(path+"plugin", os.ModePerm); err != nil {
return err
}
if _, err = os.Stat(path + "plugin/deploy"); os.IsNotExist(err) {
if err = os.Mkdir(path+"plugin/deploy", os.ModePerm); err != nil {
return err
}
}
}
return
}
func GenerateClientTrackContainers() (err error) {
path, err := GetCurrentPath()
if err != nil {
return err
}
if _, err = os.Stat(path + "client"); os.IsNotExist(err) {
if err = os.Mkdir(path+"client", os.ModePerm); err != nil {
return err
}
if _, err = os.Stat(path + "client/trackcontainers.json"); os.IsNotExist(err) {
_, err = os.Create(path + "client/trackcontainers.json")
if err != nil {
return err
}
}
if _, err = os.Stat(path + "client/grouptrackcontainers.json"); os.IsNotExist(err) {
_, err = os.Create(path + "client/grouptrackcontainers.json")
if err != nil {
return err
}
}
}
return
}

View File

@@ -0,0 +1,39 @@
package generate
import (
"io"
"os"
)
// Exists reports whether the named file or directory exists.
func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// Copy the src file to dst. Any existing file will be overwritten and will not
// copy file attributes.
// Source: https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file
func Copy(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}