diff --git a/.DS_Store b/.DS_Store index 987eb7e..5a9b600 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 6397c95..801c83c 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ generate/Test #ignore windows exe files *.exe dist/ + +# ignore docker image files +server/docker/containers/ diff --git a/Docs/Abstractions.md b/Docs/Abstractions.md new file mode 100644 index 0000000..c1790a7 --- /dev/null +++ b/Docs/Abstractions.md @@ -0,0 +1,6 @@ +# Abstractions +The Abstractions package consists of blackboxed functions for P2PRC. + +## Functions +- ```Init()```: Initialises P2PRC with all the needed configurations. +- ```Start()```: Starts p2prc as a server and makes it possible to extend by adding other routes and functionality to P2PRC. \ No newline at end of file diff --git a/Docs/README.md b/Docs/README.md index e7b0f9b..0c9590c 100644 --- a/Docs/README.md +++ b/Docs/README.md @@ -2,6 +2,7 @@ # Table of contents 1. [Introduction](Introduction.md) 2. [Installation](Installation.md) +3. [Abstractions](Abstractions.md) 3. [Design Architecture](DesignArchtectureIntro.md) 1. [Client Module](ClientArchitecture.md) 2. [P2P Module](P2PArchitecture.md) @@ -13,6 +14,5 @@ 4. [Config Module](ConfigImplementation.md) 5. [Cli Module](CliImplementation.md) 6. [Plugin Module](PluginImplementation.md) - 7. [Generate Module](GenerateImplementation.md) 5. [Problems](https://github.com/Akilan1999/p2p-rendering-computation/issues) diff --git a/README.md b/README.md index 630f9ad..71ee237 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,28 @@ This project aims to create a peer to peer (p2p) network, where a user can use t
+## Extend your application with P2PRC +```go +package main + +import "github.com/Akilan1999/p2p-rendering-computation/abstractions" + +func main() { + // Initialize with base p2prc config files + err := abstractions.Init("TEST") + if err != nil { + return + } + + // start p2prc + _, err = abstractions.Start() + if err != nil { + return + } +} +``` +[Read more](Docs/Abstractions.md) ... + ## Installation from source 1. Ensure the Go compiler is installed ``` diff --git a/abstractions/init.go b/abstractions/init.go new file mode 100644 index 0000000..4a5f149 --- /dev/null +++ b/abstractions/init.go @@ -0,0 +1,26 @@ +package abstractions + +import ( + "github.com/Akilan1999/p2p-rendering-computation/config/generate" + "github.com/Akilan1999/p2p-rendering-computation/server" + "github.com/gin-gonic/gin" +) + +// Init Initialises p2prc +func Init(name string) (err error) { + // set the config file with default paths + err = generate.SetDefaults(name, false) + if err != nil { + return + } + return +} + +// Start p2prc in a server mode +func Start() (*gin.Engine, error) { + engine, err := server.Server() + if err != nil { + return nil, err + } + return engine, nil +} 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/client/trackcontainers/.gitkeep b/client/trackcontainers/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/cmd/action.go b/cmd/action.go index 8f4b66c..c9fad39 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", false) 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..811d666 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2,26 +2,27 @@ package config import ( "fmt" + "github.com/Akilan1999/p2p-rendering-computation/config/generate" "os" "testing" ) func TestConfigInit(t *testing.T) { - _,err := ConfigInit() + _, err := ConfigInit(nil) if err != nil { t.Error(err) } } func TestSetDefaults(t *testing.T) { - err := SetDefaults() + err := generate.SetDefaults("") if err != nil { t.Error(err) } } func TestGetCurrentPath(t *testing.T) { - path, err := GetCurrentPath() + path, err := generate.GetCurrentPath() if err != nil { fmt.Println(err) t.Error(err) @@ -30,7 +31,7 @@ func TestGetCurrentPath(t *testing.T) { } func TestGetPathP2PRC(t *testing.T) { - path, err := GetPathP2PRC() + path, err := GetPathP2PRC("") if err != nil { fmt.Println(err) t.Error(err) @@ -54,10 +55,10 @@ func TestSetEnvName(t *testing.T) { } // Checks if the output for the default read is "lol" - path, err := GetPathP2PRC() + path, err := GetPathP2PRC("") if err != nil { fmt.Println(err) 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..bf64b87 --- /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, forceDefault bool) 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") && forceDefault { + 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..c9323b7 --- /dev/null +++ b/config/generate/generateFiles.go @@ -0,0 +1,164 @@ +package generate + +import ( + "github.com/Akilan1999/p2p-rendering-computation/p2p" + "github.com/go-git/go-git/v5" + "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 + } + } + + } + + // Clone base docker image + _, err = git.PlainClone(path+"server/docker/containers/docker-ubuntu-sshd", false, &git.CloneOptions{ + URL: "https://github.com/Akilan1999/docker-ubuntu-sshd", + Progress: os.Stdout, + }) + 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/iptable/.gitkeep b/p2p/iptable/.gitkeep deleted file mode 100644 index e69de29..0000000 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/.DS_Store b/server/.DS_Store new file mode 100644 index 0000000..2575f6f Binary files /dev/null and b/server/.DS_Store differ diff --git a/server/docker/.DS_Store b/server/docker/.DS_Store new file mode 100644 index 0000000..8f300a7 Binary files /dev/null and b/server/docker/.DS_Store differ diff --git a/server/docker/containers/README b/server/docker/containers/README deleted file mode 100644 index a5ada78..0000000 --- a/server/docker/containers/README +++ /dev/null @@ -1,7 +0,0 @@ -Containers -=========== - -Structure - |_ Containers - |_ (To ensure the name is the same as the tag name) - |_ DockerFile \ No newline at end of file diff --git a/server/docker/containers/cpuhorovod/Dockerfile b/server/docker/containers/cpuhorovod/Dockerfile deleted file mode 100644 index 90e8f43..0000000 --- a/server/docker/containers/cpuhorovod/Dockerfile +++ /dev/null @@ -1,43 +0,0 @@ -from horovod/horovod-cpu -# Switch to root user to install additional software -USER 0 - - -# Make sure we don't get notifications we can't answer during building. -env DEBIAN_FRONTEND noninteractive - -run apt-get -q -y update; apt-get -q -y upgrade -run apt install net-tools - -# Prepare scripts and configs -add ./scripts/start /start - - -# Set root password -run echo 'root:password' >> /root/passwdfile - - -# Create user and it's password -run useradd -m -G sudo master && \ - echo 'master:password' >> /root/passwdfile - - -# Apply root password -run chpasswd -c SHA512 < /root/passwdfile && \ - rm /root/passwdfile - - -# Port 22 is used for ssh -expose 22 - - -# Assign /data as static volume. -volume ["/data"] - - -# Fix all permissions -run chmod +x /start - - -# Starting sshd -cmd ["/start"] \ No newline at end of file diff --git a/server/docker/containers/cpuhorovod/description.txt b/server/docker/containers/cpuhorovod/description.txt deleted file mode 100644 index 7a100b7..0000000 --- a/server/docker/containers/cpuhorovod/description.txt +++ /dev/null @@ -1 +0,0 @@ -Running official horovod dockerfile cpu version diff --git a/server/docker/containers/cpuhorovod/scripts/start b/server/docker/containers/cpuhorovod/scripts/start deleted file mode 100644 index 9dde9d0..0000000 --- a/server/docker/containers/cpuhorovod/scripts/start +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -# ----------------------------------------------------------------------------- -# docker-ubuntu-sshd /start script -# -# Authors: Art567 -# Updated: Sep 20th, 2015 -# ----------------------------------------------------------------------------- - - -# Run OpenSSH server in daemon mode -/usr/sbin/sshd -D \ No newline at end of file diff --git a/server/docker/containers/docker-ubuntu-sshd-x11/Dockerfile b/server/docker/containers/docker-ubuntu-sshd-x11/Dockerfile deleted file mode 100644 index a3628ab..0000000 --- a/server/docker/containers/docker-ubuntu-sshd-x11/Dockerfile +++ /dev/null @@ -1,61 +0,0 @@ -# ----------------------------------------------------------------------------- -# This is base image of Ubuntu LTS with SSHD service. -# -# Authors: Art567 -# Updated: Sep 20th, 2015 -# Require: Docker (http://www.docker.io/) -# ----------------------------------------------------------------------------- - - -# Base system is the latest LTS version of Ubuntu. -# from consol/ubuntu-xfce-vnc - -# due to dependency issues vnc is still work in progress -from ubuntu:20.04 - -# Switch to root user to install additional software -USER 0 - - -# Make sure we don't get notifications we can't answer during building. -env DEBIAN_FRONTEND noninteractive - - -# Prepare scripts and configs -add ./scripts/start /start - - -# Download and install everything from the repos. -run apt-get -q -y update; apt-get -q -y upgrade && \ - apt-get -q -y install sudo openssh-server && \ - mkdir /var/run/sshd - - -# Set root password -run echo 'root:password' >> /root/passwdfile - - -# Create user and it's password -run useradd -m -G sudo master && \ - echo 'master:password' >> /root/passwdfile - - -# Apply root password -run chpasswd -c SHA512 < /root/passwdfile && \ - rm /root/passwdfile - - -# Port 22 is used for ssh -expose 22 - - -# Assign /data as static volume. -volume ["/data"] - - -# Fix all permissions -run chmod +x /start - - -# Starting sshd -cmd ["/start"] diff --git a/server/docker/containers/docker-ubuntu-sshd-x11/README.md b/server/docker/containers/docker-ubuntu-sshd-x11/README.md deleted file mode 100644 index ee62e54..0000000 --- a/server/docker/containers/docker-ubuntu-sshd-x11/README.md +++ /dev/null @@ -1,72 +0,0 @@ -Ubuntu LTS with SSH (Docker) -========= - -A Docker file to build a [docker.io] base container with Ubuntu LTS and a sshd service -[docker.io]: http://docker.io -Nice and easy way to get any server up and running using docker - - -Instructions ------------ - - Install Docker first. - Read [installation instructions] (http://docs.docker.io/en/latest/installation/). - - - - Clone this repository: - - `$ git clone https://github.com/art567/docker-ubuntu-sshd.git` - - - - Enter local directory: - - `$ cd docker-ubuntu-sshd` - - - Change passwords in Dockerfile: - - `$ vi Dockerfile` - - - Build the container: - - `$ sudo docker build -t art567/ubuntu .` - - - - Run the container: - - `$ sudo docker run -d=true --name=ubuntu --restart=always -p=2222:22 -v=/opt/data:/data art567/ubuntu /start` - - - - Your container will start and bind ssh to 2222 port. - - - - Find your local IP Address: - - `$ ifconfig` - - - - Connect to the SSH server: - - `$ ssh root@ -p 2222` - - - - If you don't want to deal with root: - - `$ ssh master@ -p 2222` - - -**VERY IMPORTANT!!!** ------------ - - **Don't forget to change passwords in Dockerfile before building image!** - - -Versions ------------ -Some branches represents Ubuntu versions. - -Branch `master` is always represented by latest Ubuntu LTS - - For example: - - 12.04 - Ubuntu 12.04 LTS - - 14.04 - Ubuntu 14.04 LTS - - 16.04 - Ubuntu 16.04 LTS - diff --git a/server/docker/containers/docker-ubuntu-sshd-x11/description.txt b/server/docker/containers/docker-ubuntu-sshd-x11/description.txt deleted file mode 100644 index dc5b3fa..0000000 --- a/server/docker/containers/docker-ubuntu-sshd-x11/description.txt +++ /dev/null @@ -1 +0,0 @@ -Simple Ubuntu 20.04 image \ No newline at end of file diff --git a/server/docker/containers/docker-ubuntu-sshd-x11/ports.json b/server/docker/containers/docker-ubuntu-sshd-x11/ports.json deleted file mode 100644 index 520565b..0000000 --- a/server/docker/containers/docker-ubuntu-sshd-x11/ports.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "Port": [ - { - "PortName": "SSH", - "InternalPort": 22, - "Type": "tcp", - "Description": "SSH Port" - }, - { - "PortName": "VNC", - "InternalPort": 5900, - "Type": "tcp", - "Description": "VNC port" - } - ] -} \ No newline at end of file diff --git a/server/docker/containers/docker-ubuntu-sshd-x11/scripts/start b/server/docker/containers/docker-ubuntu-sshd-x11/scripts/start deleted file mode 100644 index 3c9caa9..0000000 --- a/server/docker/containers/docker-ubuntu-sshd-x11/scripts/start +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash -# ----------------------------------------------------------------------------- -# docker-ubuntu-sshd /start script -# -# Authors: Art567 -# Updated: Sep 20th, 2015 -# ----------------------------------------------------------------------------- - - -# Run OpenSSH server in daemon mode -/usr/sbin/sshd -D - -# Running x11 server with password - diff --git a/server/docker/containers/docker-ubuntu-sshd/Dockerfile b/server/docker/containers/docker-ubuntu-sshd/Dockerfile deleted file mode 100644 index a3628ab..0000000 --- a/server/docker/containers/docker-ubuntu-sshd/Dockerfile +++ /dev/null @@ -1,61 +0,0 @@ -# ----------------------------------------------------------------------------- -# This is base image of Ubuntu LTS with SSHD service. -# -# Authors: Art567 -# Updated: Sep 20th, 2015 -# Require: Docker (http://www.docker.io/) -# ----------------------------------------------------------------------------- - - -# Base system is the latest LTS version of Ubuntu. -# from consol/ubuntu-xfce-vnc - -# due to dependency issues vnc is still work in progress -from ubuntu:20.04 - -# Switch to root user to install additional software -USER 0 - - -# Make sure we don't get notifications we can't answer during building. -env DEBIAN_FRONTEND noninteractive - - -# Prepare scripts and configs -add ./scripts/start /start - - -# Download and install everything from the repos. -run apt-get -q -y update; apt-get -q -y upgrade && \ - apt-get -q -y install sudo openssh-server && \ - mkdir /var/run/sshd - - -# Set root password -run echo 'root:password' >> /root/passwdfile - - -# Create user and it's password -run useradd -m -G sudo master && \ - echo 'master:password' >> /root/passwdfile - - -# Apply root password -run chpasswd -c SHA512 < /root/passwdfile && \ - rm /root/passwdfile - - -# Port 22 is used for ssh -expose 22 - - -# Assign /data as static volume. -volume ["/data"] - - -# Fix all permissions -run chmod +x /start - - -# Starting sshd -cmd ["/start"] diff --git a/server/docker/containers/docker-ubuntu-sshd/README.md b/server/docker/containers/docker-ubuntu-sshd/README.md deleted file mode 100644 index ee62e54..0000000 --- a/server/docker/containers/docker-ubuntu-sshd/README.md +++ /dev/null @@ -1,72 +0,0 @@ -Ubuntu LTS with SSH (Docker) -========= - -A Docker file to build a [docker.io] base container with Ubuntu LTS and a sshd service -[docker.io]: http://docker.io -Nice and easy way to get any server up and running using docker - - -Instructions ------------ - - Install Docker first. - Read [installation instructions] (http://docs.docker.io/en/latest/installation/). - - - - Clone this repository: - - `$ git clone https://github.com/art567/docker-ubuntu-sshd.git` - - - - Enter local directory: - - `$ cd docker-ubuntu-sshd` - - - Change passwords in Dockerfile: - - `$ vi Dockerfile` - - - Build the container: - - `$ sudo docker build -t art567/ubuntu .` - - - - Run the container: - - `$ sudo docker run -d=true --name=ubuntu --restart=always -p=2222:22 -v=/opt/data:/data art567/ubuntu /start` - - - - Your container will start and bind ssh to 2222 port. - - - - Find your local IP Address: - - `$ ifconfig` - - - - Connect to the SSH server: - - `$ ssh root@ -p 2222` - - - - If you don't want to deal with root: - - `$ ssh master@ -p 2222` - - -**VERY IMPORTANT!!!** ------------ - - **Don't forget to change passwords in Dockerfile before building image!** - - -Versions ------------ -Some branches represents Ubuntu versions. - -Branch `master` is always represented by latest Ubuntu LTS - - For example: - - 12.04 - Ubuntu 12.04 LTS - - 14.04 - Ubuntu 14.04 LTS - - 16.04 - Ubuntu 16.04 LTS - diff --git a/server/docker/containers/docker-ubuntu-sshd/description.txt b/server/docker/containers/docker-ubuntu-sshd/description.txt deleted file mode 100644 index dc5b3fa..0000000 --- a/server/docker/containers/docker-ubuntu-sshd/description.txt +++ /dev/null @@ -1 +0,0 @@ -Simple Ubuntu 20.04 image \ No newline at end of file diff --git a/server/docker/containers/docker-ubuntu-sshd/ports.json b/server/docker/containers/docker-ubuntu-sshd/ports.json deleted file mode 100644 index e24ff6a..0000000 --- a/server/docker/containers/docker-ubuntu-sshd/ports.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "Port": [ - { - "PortName": "SSH", - "InternalPort": 22, - "Type": "tcp", - "Description": "SSH Port" - }, - { - "PortName": "NoVNC", - "InternalPort": 5901, - "Type": "tcp", - "Description": "NoVNC port" - }, - { - "PortName": "NoVNC", - "InternalPort": 6081, - "Type": "tcp", - "Description": "NoVNC port" - } - ] -} \ No newline at end of file diff --git a/server/docker/containers/docker-ubuntu-sshd/scripts/start b/server/docker/containers/docker-ubuntu-sshd/scripts/start deleted file mode 100644 index 5286196..0000000 --- a/server/docker/containers/docker-ubuntu-sshd/scripts/start +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -# ----------------------------------------------------------------------------- -# docker-ubuntu-sshd /start script -# -# Authors: Art567 -# Updated: Sep 20th, 2015 -# ----------------------------------------------------------------------------- - - -# Run OpenSSH server in daemon mode -/usr/sbin/sshd -D diff --git a/server/docker/containers/gpuhorovod/Dockerfile b/server/docker/containers/gpuhorovod/Dockerfile deleted file mode 100644 index ed75ef6..0000000 --- a/server/docker/containers/gpuhorovod/Dockerfile +++ /dev/null @@ -1,41 +0,0 @@ -from horovod/horovod -# Switch to root user to install additional software -USER 0 - - -# Make sure we don't get notifications we can't answer during building. -env DEBIAN_FRONTEND noninteractive - - -# Prepare scripts and configs -add ./scripts/start /start - - -# Set root password -run echo 'root:password' >> /root/passwdfile - - -# Create user and it's password -run useradd -m -G sudo master && \ - echo 'master:password' >> /root/passwdfile - - -# Apply root password -run chpasswd -c SHA512 < /root/passwdfile && \ - rm /root/passwdfile - - -# Port 22 is used for ssh -expose 22 - - -# Assign /data as static volume. -volume ["/data"] - - -# Fix all permissions -run chmod +x /start - - -# Starting sshd -cmd ["/start"] \ No newline at end of file diff --git a/server/docker/containers/gpuhorovod/description.txt b/server/docker/containers/gpuhorovod/description.txt deleted file mode 100644 index 7a100b7..0000000 --- a/server/docker/containers/gpuhorovod/description.txt +++ /dev/null @@ -1 +0,0 @@ -Running official horovod dockerfile cpu version diff --git a/server/docker/containers/gpuhorovod/scripts/start b/server/docker/containers/gpuhorovod/scripts/start deleted file mode 100644 index 9dde9d0..0000000 --- a/server/docker/containers/gpuhorovod/scripts/start +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -# ----------------------------------------------------------------------------- -# docker-ubuntu-sshd /start script -# -# Authors: Art567 -# Updated: Sep 20th, 2015 -# ----------------------------------------------------------------------------- - - -# Run OpenSSH server in daemon mode -/usr/sbin/sshd -D \ No newline at end of file diff --git a/server/docker/containers/ubuntuvnc/Dockerfile b/server/docker/containers/ubuntuvnc/Dockerfile deleted file mode 100644 index d253d87..0000000 --- a/server/docker/containers/ubuntuvnc/Dockerfile +++ /dev/null @@ -1,61 +0,0 @@ -# ----------------------------------------------------------------------------- -# This is base image of Ubuntu LTS with SSHD service. -# -# Authors: Art567 -# Updated: Sep 20th, 2015 -# Require: Docker (http://www.docker.io/) -# ----------------------------------------------------------------------------- - - -# Base system is the latest LTS version of Ubuntu. -# from consol/ubuntu-xfce-vnc - -# due to dependency issues vnc is still work in progress -from dorowu/ubuntu-desktop-lxde-vnc - -# Switch to root user to install additional software -USER 0 - - -# Make sure we don't get notifications we can't answer during building. -env DEBIAN_FRONTEND noninteractive - - -# Prepare scripts and configs -add ./scripts/start /start - - -# Download and install everything from the repos. -run apt-get -q -y update; apt-get -q -y upgrade && \ - apt-get -q -y install sudo openssh-server && \ - mkdir /var/run/sshd - - -# Set root password -run echo 'root:password' >> /root/passwdfile - - -# Create user and it's password -run useradd -m -G sudo master && \ - echo 'master:password' >> /root/passwdfile - - -# Apply root password -run chpasswd -c SHA512 < /root/passwdfile && \ - rm /root/passwdfile - - -# Port 22 is used for ssh -expose 22 - - -# Assign /data as static volume. -volume ["/data"] - - -# Fix all permissions -run chmod +x /start - - -# Starting sshd -cmd ["/start"] diff --git a/server/docker/containers/ubuntuvnc/README.md b/server/docker/containers/ubuntuvnc/README.md deleted file mode 100644 index ee62e54..0000000 --- a/server/docker/containers/ubuntuvnc/README.md +++ /dev/null @@ -1,72 +0,0 @@ -Ubuntu LTS with SSH (Docker) -========= - -A Docker file to build a [docker.io] base container with Ubuntu LTS and a sshd service -[docker.io]: http://docker.io -Nice and easy way to get any server up and running using docker - - -Instructions ------------ - - Install Docker first. - Read [installation instructions] (http://docs.docker.io/en/latest/installation/). - - - - Clone this repository: - - `$ git clone https://github.com/art567/docker-ubuntu-sshd.git` - - - - Enter local directory: - - `$ cd docker-ubuntu-sshd` - - - Change passwords in Dockerfile: - - `$ vi Dockerfile` - - - Build the container: - - `$ sudo docker build -t art567/ubuntu .` - - - - Run the container: - - `$ sudo docker run -d=true --name=ubuntu --restart=always -p=2222:22 -v=/opt/data:/data art567/ubuntu /start` - - - - Your container will start and bind ssh to 2222 port. - - - - Find your local IP Address: - - `$ ifconfig` - - - - Connect to the SSH server: - - `$ ssh root@ -p 2222` - - - - If you don't want to deal with root: - - `$ ssh master@ -p 2222` - - -**VERY IMPORTANT!!!** ------------ - - **Don't forget to change passwords in Dockerfile before building image!** - - -Versions ------------ -Some branches represents Ubuntu versions. - -Branch `master` is always represented by latest Ubuntu LTS - - For example: - - 12.04 - Ubuntu 12.04 LTS - - 14.04 - Ubuntu 14.04 LTS - - 16.04 - Ubuntu 16.04 LTS - diff --git a/server/docker/containers/ubuntuvnc/description.txt b/server/docker/containers/ubuntuvnc/description.txt deleted file mode 100644 index dc5b3fa..0000000 --- a/server/docker/containers/ubuntuvnc/description.txt +++ /dev/null @@ -1 +0,0 @@ -Simple Ubuntu 20.04 image \ No newline at end of file diff --git a/server/docker/containers/ubuntuvnc/scripts/start b/server/docker/containers/ubuntuvnc/scripts/start deleted file mode 100644 index 5286196..0000000 --- a/server/docker/containers/ubuntuvnc/scripts/start +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -# ----------------------------------------------------------------------------- -# docker-ubuntu-sshd /start script -# -# Authors: Art567 -# Updated: Sep 20th, 2015 -# ----------------------------------------------------------------------------- - - -# Run OpenSSH server in daemon mode -/usr/sbin/sshd -D 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 }