added docker templating and introducted possbility to change the baseimage name

This commit is contained in:
2023-08-23 22:45:46 +01:00
parent dfa8713aea
commit 6b287f2cf1
9 changed files with 1061 additions and 990 deletions

View File

@@ -1,52 +1,53 @@
package config
import (
"encoding/json"
"io/ioutil"
"os"
"encoding/json"
"io/ioutil"
"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
CustomConfig interface{}
//NetworkInterface string
//NetworkInterfaceIPV6Index int
MachineName string
IPTable string
DockerContainers string
DefaultDockerFile string
DockerRunLogs string
SpeedTestFile string
IPV6Address string
PluginPath string
TrackContainersPath string
ServerPort string
GroupTrackContainersPath string
FRPServerPort string
BehindNAT string
CustomConfig interface{}
//NetworkInterface string
//NetworkInterfaceIPV6Index int
}
// GetPathP2PRC Getting P2PRC Directory from environment variable
func GetPathP2PRC(Envname string) (string, error) {
if Envname != "" {
err := SetEnvName(Envname)
if err != nil {
return "", err
}
}
curDir := os.Getenv(defaultEnvName)
if curDir == "" {
return curDir, nil
}
return curDir + "/", nil
if Envname != "" {
err := SetEnvName(Envname)
if err != nil {
return "", err
}
}
curDir := os.Getenv(defaultEnvName)
if curDir == "" {
return curDir, nil
}
return curDir + "/", nil
}
// SetEnvName Sets the environment name
@@ -54,103 +55,103 @@ func GetPathP2PRC(Envname string) (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
}
func GetEnvName() string {
return defaultEnvName
return defaultEnvName
}
// ConfigInit Pass environment name as an optional parameter
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
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
defaultPath, err := GetPathP2PRC(defaultEnvName)
if err != nil {
return nil, err
}
defaultPath, err := GetPathP2PRC(defaultEnvName)
if err != nil {
return nil, err
}
// 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
}
// 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
}
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
byteValue, _ := ioutil.ReadAll(jsonFile)
var config Config
json.Unmarshal(byteValue, &config)
var config Config
json.Unmarshal(byteValue, &config)
if CustomConfig != nil {
// Convert Custom Config to byte
customConfigByte, err := json.Marshal(config.CustomConfig)
if err != nil {
return nil, err
}
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)
}
// Again map the byte to the CustomConfig interface
json.Unmarshal(customConfigByte, &CustomConfig)
}
return &config, nil
return &config, nil
}
func (c *Config) WriteConfig() error {
//Getting Current Directory from environment variable
//curDir := os.Getenv("REMOTEGAMING")
//Getting Current Directory from environment variable
//curDir := os.Getenv("REMOTEGAMING")
defaultPath, err := GetPathP2PRC(defaultEnvName)
if err != nil {
return err
}
defaultPath, err := GetPathP2PRC(defaultEnvName)
if err != nil {
return err
}
file, _ := json.MarshalIndent(c, "", " ")
file, _ := json.MarshalIndent(c, "", " ")
_ = ioutil.WriteFile(defaultPath+"config.json", file, 0644)
return nil
_ = ioutil.WriteFile(defaultPath+"config.json", file, 0644)
return nil
}

View File

@@ -1,20 +1,20 @@
package generate
import (
"github.com/Akilan1999/p2p-rendering-computation/config"
"os"
"github.com/Akilan1999/p2p-rendering-computation/config"
"os"
)
var (
defaults = map[string]interface{}{}
configPaths []string
defaultEnvName = "P2PRC"
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
curDir := os.Getenv(defaultEnvName)
return curDir + "/", nil
}
// SetEnvName Sets the environment name
@@ -22,110 +22,111 @@ func GetPathP2PRC() (string, error) {
// 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
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
curDir := os.Getenv("PWD")
return curDir + "/", nil
}
// SetDefaults This function to be called only during a
// make install
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 {
return nil, err
}
//Setting current directory to default path
defaultPath, err := GetCurrentPath()
if err != nil {
return nil, err
}
// Set Env name
err = config.SetEnvName(envName)
if err != nil {
return nil, err
}
// Set Env name
err = config.SetEnvName(envName)
if err != nil {
return nil, 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
//}
////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
//}
var Defaults config.Config
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 = "True"
Defaults.CustomConfig = CustomConfig
Defaults.BehindNAT = "True"
// Random name generator
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
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 = "True"
Defaults.CustomConfig = CustomConfig
Defaults.BehindNAT = "True"
Defaults.DockerRunLogs = "/tmp/"
// Random name generator
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
Defaults.MachineName = hostname
} else {
Defaults = *ConfigUpdate[0]
}
Defaults.MachineName = hostname
} else {
Defaults = *ConfigUpdate[0]
}
//defaults["NetworkInterface"] = "wlp0s20f3"
//defaults["NetworkInterfaceIPV6Index"] = "2"
//defaults["NetworkInterface"] = "wlp0s20f3"
//defaults["NetworkInterfaceIPV6Index"] = "2"
//Paths to search for config file
configPaths = append(configPaths, defaultPath)
//Paths to search for config file
configPaths = append(configPaths, defaultPath)
if fileExists(defaultPath+"config.json") && forceDefault {
err := os.Remove(defaultPath + "config.json")
if err != nil {
return nil, err
}
}
if fileExists(defaultPath+"config.json") && forceDefault {
err := os.Remove(defaultPath + "config.json")
if err != nil {
return nil, err
}
}
// write defaults to the config file
err = Defaults.WriteConfig()
if err != nil {
return nil, err
}
// 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
}
//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
}
}
if !NoBoilerPlate {
err = GenerateFiles()
if err != nil {
return nil, err
}
}
return Config, nil
return Config, nil
}