diff --git a/abstractions/init.go b/abstractions/init.go new file mode 100644 index 0000000..cad9841 --- /dev/null +++ b/abstractions/init.go @@ -0,0 +1,11 @@ +package abstractions + +import ( + "github.com/Akilan1999/p2p-rendering-computation/config/generate" +) + +func Init(name string) { + // set the config file with default paths + generate.SetDefaults(name) + +} diff --git a/client/GroupTrackContainer.go b/client/GroupTrackContainer.go index 77d0b25..3fb7eae 100644 --- a/client/GroupTrackContainer.go +++ b/client/GroupTrackContainer.go @@ -206,7 +206,7 @@ func (grp *Group) AddGroupToFile() error { // result to Groups func ReadGroup() (*Groups, error) { // Get Path from config - config, err := config.ConfigInit() + config, err := config.ConfigInit(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() + config, err := config.ConfigInit(nil) if err != nil { return err } diff --git a/client/TrackContainers.go b/client/TrackContainers.go index c72cb80..f313595 100644 --- a/client/TrackContainers.go +++ b/client/TrackContainers.go @@ -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() + config, err := config.ConfigInit(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() + config, err := config.ConfigInit(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() + config, err := config.ConfigInit(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() + config, err := config.ConfigInit(nil) // write modified information to the tracked json file data, err := json.MarshalIndent(TC, "", "\t") if err != nil { diff --git a/client/clientIPTable/Iptable.go b/client/clientIPTable/Iptable.go index 1cffa05..beb0d04 100644 --- a/client/clientIPTable/Iptable.go +++ b/client/clientIPTable/Iptable.go @@ -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() + config, err := config.ConfigInit(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() + Config, err := config.ConfigInit(nil) if err != nil { return err } diff --git a/cmd/action.go b/cmd/action.go index 8f4b66c..596ed79 100644 --- a/cmd/action.go +++ b/cmd/action.go @@ -4,8 +4,7 @@ import ( "fmt" "github.com/Akilan1999/p2p-rendering-computation/client" "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" - "github.com/Akilan1999/p2p-rendering-computation/config" - "github.com/Akilan1999/p2p-rendering-computation/generate" + "github.com/Akilan1999/p2p-rendering-computation/config/generate" "github.com/Akilan1999/p2p-rendering-computation/p2p" "github.com/Akilan1999/p2p-rendering-computation/plugin" "github.com/Akilan1999/p2p-rendering-computation/server" @@ -14,10 +13,14 @@ import ( var CliAction = func(ctx *cli.Context) error { if Server { - err := server.Server() + _, err := server.Server() if err != nil { fmt.Print(err) } + + for { + + } //server.Rpc() } @@ -132,7 +135,7 @@ var CliAction = func(ctx *cli.Context) error { //Sets default paths to the config file if SetDefaultConfig { - err := config.SetDefaults() + err := generate.SetDefaults("P2PRC") if err != nil { fmt.Print(err) } @@ -247,39 +250,6 @@ var CliAction = func(ctx *cli.Context) error { } } - // Starts server as a reverse proxy so that - // nodes can connect to each other behind NAT - //if FRPProxy { - // err := frp.StartFRPProxyFromRandom() - // if err != nil { - // fmt.Println(err) - // } - //} - - // -- REMOVE ON REGULAR RELEASE -- - // when flag --gen is called an extension - // of the project is created to repurpose - // the project for custom purpose - if Generate != "" { - var err error - // If the module name is provided - if Modulename != "" { - err = generate.GenerateNewProject(Generate, Modulename) - } else { - err = generate.GenerateNewProject(Generate, Generate) - } - if err != nil { - fmt.Println(err) - } else { - fmt.Println("Created new folder: " + Generate) - fmt.Println("1. Enter inside " + Generate + " directory") - fmt.Println("2. git remote add " + Generate + " ") - fmt.Println("3. git push " + Generate + " ") - fmt.Println("4. go mod tidy") - fmt.Println("5. sh install.sh " + Generate) - fmt.Println("6. ./" + Generate + " -h (This is to test if the binary is working)") - } - } //-------------------------------- if PullPlugin != "" { diff --git a/config/config.go b/config/config.go index 9482154..27c7eca 100644 --- a/config/config.go +++ b/config/config.go @@ -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 } diff --git a/config/config_test.go b/config/config_test.go index 2f9857f..af5f850 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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) -} \ No newline at end of file +} diff --git a/config/generate/generate.go b/config/generate/generate.go new file mode 100644 index 0000000..f4b7415 --- /dev/null +++ b/config/generate/generate.go @@ -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 +} diff --git a/config/generate/generateFiles.go b/config/generate/generateFiles.go new file mode 100644 index 0000000..41cb816 --- /dev/null +++ b/config/generate/generateFiles.go @@ -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 +} diff --git a/config/generate/helperFunctions.go b/config/generate/helperFunctions.go new file mode 100644 index 0000000..048afb8 --- /dev/null +++ b/config/generate/helperFunctions.go @@ -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() +} diff --git a/generate/Test/.gitkeep b/generate/Test/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/generate/generate.go b/generate/generate.go deleted file mode 100644 index d68b11b..0000000 --- a/generate/generate.go +++ /dev/null @@ -1,386 +0,0 @@ -// Package generate The purpose of this package is to ensure that we can extend the use-case of P2PRC. -// We will create a project directory with the template to extend the use-case of P2PRC -package generate - -import ( - "fmt" - "github.com/Akilan1999/p2p-rendering-computation/config" - "github.com/otiai10/copy" - "go/ast" - "go/token" - modfile "golang.org/x/mod/modfile" - "io/ioutil" - "os" - "os/exec" - "strings" -) - -// NewProject Struct information required when creating a new project -type NewProject struct { - Name string - Module string - NewDir string - P2PRCPath string - CurrentModule string - Option *copy.Options - Token *token.FileSet - AST *ast.File - FileNameAST string -} - -// GenerateNewProject creates a new copy of the P2PRC -// project for custom modification -func GenerateNewProject(name string, module string) error { - // Create new variable of type NewProject - var newProject NewProject - //Setting module name to the new project - newProject.Module = module - // Get path of the current directory - curDir, err := config.GetCurrentPath() - if err != nil { - return err - } - // Folder name of the new generated project - newProject.NewDir = curDir + name + "/" - // Create a new folder based on name entered - err = CreateFolder(name, curDir) - if err != nil { - return err - } - // get path of P2PRC - P2PRCPATH, err := config.GetPathP2PRC() - if err != nil { - return err - } - // Assign P2PRC path to the newly generated project - newProject.P2PRCPath = P2PRCPATH - // Steps: - // - copy all files from P2PRC - // - remove go.mod and go.sum and create new ones - - // Files we require to skip - - var Options copy.Options - - // IF YOU ARE RELEASING AN EXTENSION OF P2PRC - // MODIFY HERE (AN EXTENSION FROM YOUR EXTENSION :P) - // Skip or have the appropriate files and directories not needed - //---------------------------------------------------------------- - // Action performed: - // - Ensuring main.go file exists - // - Ensuring generate.go file exists - // - Ensuring modifyGenerate.go file exists - // - Ensuring generate_test.go file exists - // - Ensuring server/server.go file exists - // - Ensuring server/gopsutil.go file exists - // - Ensuring server/gpu.go file exists - // - Ensuring cmd/action.go file exists - // - Ensuring cmd/flags.go file exists - // - Skipping all .go files apart from the ones listed above - // - Skipping go.mod file - // - Skipping go.sum file - // - Skipping .idea/ directory - // - Skipping Makefile file - // - Skipping / directory - //---------------------------------------------------------------- - Options.Skip = func(src string) (bool, error) { - switch { - case strings.HasSuffix(src, "main.go"): - return false, nil - case strings.HasSuffix(src, "generate.go"): - return false, nil - case strings.HasSuffix(src, "modifyGenerate.go"): - return false, nil - case strings.HasSuffix(src, "generate_test.go"): - return false, nil - case strings.HasSuffix(src, "server/server.go"): - return false, nil - case strings.HasSuffix(src, "server/gopsutil.go"): - return false, nil - case strings.HasSuffix(src, "server/gpu.go"): - return false, nil - case strings.HasSuffix(src, "cmd/action.go"): - return false, nil - case strings.HasSuffix(src, "cmd/flags.go"): - return false, nil - case strings.HasSuffix(src, "config/config.go"): - return false, nil - case strings.HasSuffix(src, "config/config_test.go"): - return false, nil - case strings.HasSuffix(src, ".go"): - return true, nil - case strings.HasSuffix(src, "go.mod"): - return true, nil - case strings.HasSuffix(src, "go.sum"): - return true, nil - case strings.HasSuffix(src, ".idea"): - return true, nil - case strings.HasSuffix(src, "Makefile"): - return true, nil - case strings.HasSuffix(src, name): - return true, nil - default: - return false, nil - } - } - - // Storing type option in the struct new project - newProject.Option = &Options - - // Copies all files from P2PRC to the new project created - err = copy.Copy(newProject.P2PRCPath, newProject.NewDir, *newProject.Option) - if err != nil { - return err - } - - // Creating a new go.mod file in the appropriate directory - err = newProject.CreateGoMod() - if err != nil { - return err - } - - // Get current project mod name - err = newProject.GetCurrentGoModule() - if err != nil { - return err - } - - // Change the appropriate imports - err = newProject.ChangeImportFiles() - if err != nil { - return err - } - - // Add changes inside the new project - err = newProject.GitAdd() - if err != nil { - return err - } - - // commit changes inside the new project - err = newProject.GitCommit() - if err != nil { - return err - } - // Creates go.sum file - //err = newProject.CreateGoModTidy() - //if err != nil { - // return err - //} - - return nil -} - -// CreateFolder Creates a new folder based on the name and path provided -func CreateFolder(name string, path string) error { - //Create a folder/directory at a full qualified path - err := os.Mkdir(path+name, 0755) - if err != nil { - return err - } - return nil -} - -// CreateGoMod Creates a new go module for the new project created -func (a *NewProject) CreateGoMod() error { - // Create new go.mod in the appropriate directory - cmd := exec.Command("go", "mod", "init", a.Module) - cmd.Dir = a.NewDir - if err := cmd.Run(); err != nil { - return err - } - return nil -} - -func (a *NewProject) CreateGoModTidy() error { - // Installs all the appropriate dependencies and creates go.sum - cmd := exec.Command("go", "mod", "tidy") - cmd.Dir = a.NewDir - if err := cmd.Run(); err != nil { - return err - } - return nil -} - -// ChangeImportFiles Changes Appropriate imports in the appropriate file -func (a *NewProject) ChangeImportFiles() error { - // IF YOU ARE RELEASING AN EXTENSION OF P2PRC - // MODIFY HERE (AN EXTENSION FROM YOUR EXTENSION :P) - //---------------------------------------------------------------- - // Action performed: - // Files we would need to modify the imports in - // - generate/generate.go -> config module - // - generate/generate_test.go -> config module - // - cmd/action.go -> config module, server module, generate module - // - cmd/flags.go -> config module, server module, generate module - // - server/server.go -> config module - // - main.go -> cmd module - //----------------------------------------------------------------- - - //----------------------------------------------------------------- - // 1.0 - generate/generate.go -> config module - a.FileNameAST = a.NewDir + "generate/generate.go" - // Get AST information of the file - err := a.GetASTGoFile() - if err != nil { - return err - } - // Change the appropriate Go file - err = a.ChangeImports(a.CurrentModule+"/config", a.Module+"/config") - if err != nil { - return err - } - // Writes the change to the appropriate file - err = a.WriteGoAst() - if err != nil { - return err - } - //----------------------------------------------------------------- - // 1.1 - generate/generate_test.go -> config module - a.FileNameAST = a.NewDir + "generate/generate_test.go" - // Get AST information of the file - err = a.GetASTGoFile() - if err != nil { - return err - } - // Change the appropriate Go file - err = a.ChangeImports(a.CurrentModule+"/config", a.Module+"/config") - if err != nil { - return err - } - // Writes the change to the appropriate file - err = a.WriteGoAst() - if err != nil { - return err - } - - //----------------------------------------------------------------- - //----------------------------------------------------------------- - // 2.0 - cmd/action.go -> config module, server module, generate module - a.FileNameAST = a.NewDir + "cmd/action.go" - // Get AST information of the file - err = a.GetASTGoFile() - if err != nil { - return err - } - // Change the appropriate Go file - err = a.ChangeImports(a.CurrentModule+"/config", a.Module+"/config") - if err != nil { - return err - } - err = a.ChangeImports(a.CurrentModule+"/server", a.Module+"/server") - if err != nil { - return err - } - err = a.ChangeImports(a.CurrentModule+"/generate", a.Module+"/generate") - if err != nil { - return err - } - // Writes the change to the appropriate file - err = a.WriteGoAst() - if err != nil { - return err - } - //----------------------------------------------------------------- - // 2.1 - cmd/flags.go -> config module, server module, generate module - a.FileNameAST = a.NewDir + "cmd/flags.go" - // Get AST information of the file - err = a.GetASTGoFile() - if err != nil { - return err - } - // Change the appropriate Go file - err = a.ChangeImports(a.CurrentModule+"/config", a.Module+"/config") - if err != nil { - return err - } - err = a.ChangeImports(a.CurrentModule+"/server", a.Module+"/server") - if err != nil { - return err - } - err = a.ChangeImports(a.CurrentModule+"/generate", a.Module+"/generate") - if err != nil { - return err - } - // Writes the change to the appropriate file - err = a.WriteGoAst() - if err != nil { - return err - } - //----------------------------------------------------------------- - //----------------------------------------------------------------- - // 3.0 - server/server.go -> config module - a.FileNameAST = a.NewDir + "server/server.go" - // Get AST information of the file - err = a.GetASTGoFile() - if err != nil { - return err - } - // Change the appropriate Go file - err = a.ChangeImports(a.CurrentModule+"/config", a.Module+"/config") - if err != nil { - return err - } - // Writes the change to the appropriate file - err = a.WriteGoAst() - if err != nil { - return err - } - //----------------------------------------------------------------- - //----------------------------------------------------------------- - // 4.0 - server/server.go -> config module - a.FileNameAST = a.NewDir + "main.go" - // Get AST information of the file - err = a.GetASTGoFile() - if err != nil { - return err - } - // Change the appropriate Go file - err = a.ChangeImports(a.CurrentModule+"/cmd", a.Module+"/cmd") - if err != nil { - return err - } - // Writes the change to the appropriate file - err = a.WriteGoAst() - if err != nil { - return err - } - //----------------------------------------------------------------- - - return nil -} - -// GetCurrentGoModule Gets the current go module name -func (a *NewProject) GetCurrentGoModule() error { - goModBytes, err := ioutil.ReadFile(a.P2PRCPath + "go.mod") - if err != nil { - return err - } - modName := modfile.ModulePath(goModBytes) - fmt.Fprintf(os.Stdout, "modName=%+v\n", modName) - - // Set current module to struct of file NewProject - a.CurrentModule = modName - - return nil -} - -func (a *NewProject) GitAdd() error { - // Installs all the appropriate dependencies and creates go.sum - cmd := exec.Command("git", "add", ".") - cmd.Dir = a.NewDir - if err := cmd.Run(); err != nil { - return err - } - return nil -} - -func (a *NewProject) GitCommit() error { - // Installs all the appropriate dependencies and creates go.sum - cmd := exec.Command("git", "commit", "-m=removed appropriate go files") - cmd.Dir = a.NewDir - if err := cmd.Run(); err != nil { - return err - } - return nil -} diff --git a/generate/generate_test.go b/generate/generate_test.go deleted file mode 100644 index 38a2e3d..0000000 --- a/generate/generate_test.go +++ /dev/null @@ -1,112 +0,0 @@ -package generate - -import ( - "fmt" - "github.com/Akilan1999/p2p-rendering-computation/config" - "testing" -) - -// Tests the create folder function creates a folder -// This test will create a folder in the temporary -// directory -func TestCreateFolder(t *testing.T) { - err := CreateFolder("test", "/tmp/") - if err != nil { - t.Error(err) - } -} - -// Testing if a new project is created successfully -func TestGenerateNewProject(t *testing.T) { - // Checking if a new project is created successfully - err := GenerateNewProject("p2prctest", "p2prctest") - if err != nil { - fmt.Println(err) - t.Error(err) - } -} - -// Testing AST function to ensure imports are -// working as intended -func TestChangingImportAST(t *testing.T) { - // Create a new variable of type NewProject - var np NewProject - // Get current directory - path, err := config.GetCurrentPath() - if err != nil { - fmt.Println(err) - t.Error(err) - } - // Create testcase scenario - err = config.Copy(path+"testcaseAST.go", path+"/Test/testcaseAST.go") - if err != nil { - fmt.Println(err) - t.Error(err) - } - // Sets new directory to the folder test - np.NewDir = path + "Test/" - // Sets file name to be opened and modified in the AST - np.FileNameAST = path + "Test/" + "testcaseAST.go" - // Call the Read AST function - err = np.GetASTGoFile() - if err != nil { - fmt.Println(err) - t.Error(err) - } - // Change an import - err = np.ChangeImports("fmt", "lolol") - if err != nil { - fmt.Println(err) - t.Error(err) - } - // Write those saved changes - err = np.WriteGoAst() - if err != nil { - fmt.Println(err) - t.Error(err) - } - -} - -// Testing the if Go Mod is created -func TestNewProject_CreateGoMod(t *testing.T) { - // Create a new variable of type NewProject - var np NewProject - path, err := config.GetCurrentPath() - if err != nil { - fmt.Println(err) - t.Error(err) - } - // Set new project name as Test - np.Name = "Test" - // Set new project module as github.com/Test - np.Module = "github.com/Test" - // Set Path of the new project - np.NewDir = path + "Test/" - // Creating a go.mod file - err = np.CreateGoMod() - if err != nil { - fmt.Println(err) - t.Error(err) - } -} - -// Testing if the current go module is returned -func TestNewProject_GetCurrentGoModule(t *testing.T) { - // Create a new variable of type NewProject - var np NewProject - path, err := config.GetPathP2PRC() - if err != nil { - fmt.Println(err) - t.Error(err) - } - // Set Current project path - np.P2PRCPath = path - // Get module name - err = np.GetCurrentGoModule() - if err != nil { - fmt.Println(err) - t.Error(err) - } - fmt.Println(np.CurrentModule) -} diff --git a/generate/modifyGenerate.go b/generate/modifyGenerate.go deleted file mode 100644 index cc66061..0000000 --- a/generate/modifyGenerate.go +++ /dev/null @@ -1,49 +0,0 @@ -package generate - -import ( - "go/parser" - "go/printer" - "go/token" - "os" -) - - -// GetASTGoFile Gets AST of the Go file provided -func (np *NewProject)GetASTGoFile() error{ - fset := token.NewFileSet() - node, err := parser.ParseFile(fset, np.FileNameAST, nil, parser.ParseComments) - if err != nil { - return err - } - //Write Token information to the struct - np.Token = fset - // Write AST information the struct - np.AST = node - return nil -} - -// ChangeImports Changes import of the AST -func (np *NewProject)ChangeImports(CurrentImport string,ChangedImport string) error { - // Iterating through the loop and changing the appropriate import - for i, spec := range np.AST.Imports { - // If the current import is found then change it - if spec.Path.Value == "\"" + CurrentImport + "\"" { - np.AST.Imports[i].Path.Value = "\"" + ChangedImport + "\"" - } - } - return nil -} - -// WriteGoAst Write changed imports back to the AST -func (np *NewProject)WriteGoAst() error { - // write new AST to file - f, err := os.Create(np.FileNameAST) - if err != nil { - return nil - } - defer f.Close() - if err := printer.Fprint(f, np.Token, np.AST); err != nil { - return err - } - return nil -} \ No newline at end of file diff --git a/generate/testcaseAST.go b/generate/testcaseAST.go deleted file mode 100644 index 0d1dfbb..0000000 --- a/generate/testcaseAST.go +++ /dev/null @@ -1,9 +0,0 @@ -package generate - -import ( - "fmt" -) - -func TestCaseAST() { - fmt.Println("lol") -} \ No newline at end of file diff --git a/p2p/iptable.go b/p2p/iptable.go index 319fc4d..1facb68 100644 --- a/p2p/iptable.go +++ b/p2p/iptable.go @@ -36,7 +36,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() + config, err := config.ConfigInit(nil) if err != nil { return nil, err } @@ -101,7 +101,7 @@ func (i *IpAddresses) WriteIpTable() error { } // Get Path from config - config, err := config.ConfigInit() + config, err := config.ConfigInit(nil) if err != nil { return err } @@ -189,7 +189,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() + Config, err := config.ConfigInit(nil) if err != nil { return "", err } diff --git a/p2p/testingMetrics.go b/p2p/testingMetrics.go index 002870a..95e3772 100644 --- a/p2p/testingMetrics.go +++ b/p2p/testingMetrics.go @@ -99,7 +99,7 @@ func (s *IpAddress) UploadSpeed() error { // Get upload file path from config file // Get Path from config - config, err := config.ConfigInit() + config, err := config.ConfigInit(nil) if err != nil { return err } diff --git a/plugin/packageManager.go b/plugin/packageManager.go index 493cd4b..c651d66 100644 --- a/plugin/packageManager.go +++ b/plugin/packageManager.go @@ -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() + config, err := config.ConfigInit(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() + config, err := config.ConfigInit(nil) if err != nil { return err } diff --git a/plugin/plugin.go b/plugin/plugin.go index 6af3df7..2dd72f2 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -67,7 +67,7 @@ type Host struct { // DetectPlugins Detects all the plugins available func DetectPlugins() (*Plugins, error) { - config, err := config.ConfigInit() + config, err := config.ConfigInit(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() + config, err := config.ConfigInit(nil) if err != nil { return nil, err } diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index a95073d..c83f4f4 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -74,7 +74,7 @@ func TestExecuteIP_ModifyHost(t *testing.T) { var testip ExecuteIP // Get plugin path from config file - Config, err := config.ConfigInit() + Config, err := config.ConfigInit(nil) if err != nil { fmt.Println(err) t.Fail() diff --git a/server/docker/docker.go b/server/docker/docker.go index a60def8..c51cb76 100644 --- a/server/docker/docker.go +++ b/server/docker/docker.go @@ -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() + config, err := config.ConfigInit(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() + config, err := config.ConfigInit(nil) if err != nil { return nil, err } diff --git a/server/server.go b/server/server.go index b046765..103bd8c 100644 --- a/server/server.go +++ b/server/server.go @@ -15,13 +15,13 @@ import ( "time" ) -func Server() error { +func Server() (*gin.Engine, error) { r := gin.Default() //Get Server port based on the config file - config, err := config.ConfigInit() + config, err := config.ConfigInit(nil) if err != nil { - return err + return nil, err } // update IPTable with new port and ip address and update ip table @@ -172,7 +172,7 @@ func Server() error { if config.BehindNAT == "True" { table, err := p2p.ReadIpTable() if err != nil { - return err + return nil, err } var lowestLatency int64 @@ -192,14 +192,14 @@ func Server() error { if lowestLatency != 10000000 { serverPort, err := frp.GetFRPServerPort("http://" + lowestLatencyIpAddress.Ipv4 + ":" + lowestLatencyIpAddress.ServerPort) if err != nil { - return err + return nil, err } // Create 3 second delay to allow FRP server to start time.Sleep(1 * time.Second) // Starts FRP as a client with proxyPort, err := frp.StartFRPClientForServer(lowestLatencyIpAddress.Ipv4, serverPort, config.ServerPort) if err != nil { - return err + return nil, err } // updating with the current proxy address @@ -220,10 +220,7 @@ func Server() error { } // Run gin server on the specified port - err = r.Run(":" + config.ServerPort) - if err != nil { - return err - } + go r.Run(":" + config.ServerPort) - return nil + return r, nil }