captplanet standalone farmer setup (#202)
This commit is contained in:
parent
ab029301bd
commit
1f8db2a4c7
@ -4,6 +4,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@ -20,32 +21,75 @@ var (
|
||||
Use: "farmer",
|
||||
Short: "Farmer",
|
||||
}
|
||||
runCmd = &cobra.Command{
|
||||
Use: "run",
|
||||
Short: "Run the farmer",
|
||||
RunE: cmdRun,
|
||||
}
|
||||
setupCmd = &cobra.Command{
|
||||
Use: "setup",
|
||||
Short: "Create config files",
|
||||
RunE: cmdSetup,
|
||||
}
|
||||
|
||||
cfg struct {
|
||||
runCfg struct {
|
||||
Identity provider.IdentityConfig
|
||||
Kademlia kademlia.Config
|
||||
Storage psservice.Config
|
||||
}
|
||||
setupCfg struct {
|
||||
BasePath string `default:"$CONFDIR" help:"base path for setup"`
|
||||
CA provider.CASetupConfig
|
||||
Identity provider.IdentitySetupConfig
|
||||
}
|
||||
|
||||
defaultConfDir = "$HOME/.storj/farmer"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(&cobra.Command{
|
||||
Use: "run",
|
||||
Short: "Run the farmer",
|
||||
RunE: cmdRun,
|
||||
})
|
||||
cfgstruct.Bind(rootCmd.PersistentFlags(), &cfg,
|
||||
cfgstruct.ConfDir(defaultConfDir),
|
||||
)
|
||||
rootCmd.AddCommand(runCmd)
|
||||
rootCmd.AddCommand(setupCmd)
|
||||
cfgstruct.Bind(runCmd.Flags(), &runCfg, cfgstruct.ConfDir(defaultConfDir))
|
||||
cfgstruct.Bind(setupCmd.Flags(), &setupCfg, cfgstruct.ConfDir(defaultConfDir))
|
||||
}
|
||||
|
||||
func cmdRun(cmd *cobra.Command, args []string) (err error) {
|
||||
return cfg.Identity.Run(process.Ctx(cmd), cfg.Kademlia, cfg.Storage)
|
||||
return runCfg.Identity.Run(process.Ctx(cmd), runCfg.Kademlia, runCfg.Storage)
|
||||
}
|
||||
|
||||
func cmdSetup(cmd *cobra.Command, args []string) (err error) {
|
||||
setupCfg.BasePath, err = filepath.Abs(setupCfg.BasePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(setupCfg.BasePath, 0700)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
setupCfg.CA.CertPath = filepath.Join(setupCfg.BasePath, "ca.cert")
|
||||
setupCfg.CA.KeyPath = filepath.Join(setupCfg.BasePath, "ca.key")
|
||||
setupCfg.Identity.CertPath = filepath.Join(setupCfg.BasePath, "identity.cert")
|
||||
setupCfg.Identity.KeyPath = filepath.Join(setupCfg.BasePath, "identity.key")
|
||||
|
||||
err = provider.SetupIdentity(process.Ctx(cmd), setupCfg.CA, setupCfg.Identity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
overrides := map[string]interface{}{
|
||||
"identity.cert-path": setupCfg.Identity.CertPath,
|
||||
"identity.key-path": setupCfg.Identity.KeyPath,
|
||||
"storage.path": filepath.Join(setupCfg.BasePath, "storage"),
|
||||
}
|
||||
|
||||
return process.SaveConfig(runCmd.Flags(),
|
||||
filepath.Join(setupCfg.BasePath, "config.yaml"), overrides)
|
||||
}
|
||||
|
||||
func main() {
|
||||
process.ExecuteWithConfig(rootCmd,
|
||||
filepath.Join(defaultConfDir, "config.yaml"))
|
||||
runCmd.Flags().String("config",
|
||||
filepath.Join(defaultConfDir, "config.yaml"), "path to configuration")
|
||||
process.Exec(rootCmd)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user