Skip to content

Commit 4e5fec8

Browse files
committed
vspadmin: Write default config file.
A new command in vspadmin writes the default config file for a new vspd deployment. The behaviour is removed from vspd and documentation has been updated to reflect the change.
1 parent 997205e commit 4e5fec8

4 files changed

Lines changed: 63 additions & 21 deletions

File tree

cmd/vspadmin/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,15 @@ used for collecting fees as a parameter.
2626
Example:
2727

2828
```no-highlight
29-
go run ./cmd/vspadmin createdatabase <xpub>
29+
$ go run ./cmd/vspadmin createdatabase <xpub>
30+
```
31+
32+
### `writeconfig`
33+
34+
Writes a config file with default values to the application home directory.
35+
36+
Example:
37+
38+
```no-highlight
39+
$ go run ./cmd/vspadmin writeconfig
3040
```

cmd/vspadmin/main.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import (
1313
"github.com/decred/dcrd/hdkeychain/v3"
1414
"github.com/decred/vspd/database"
1515
"github.com/decred/vspd/internal/config"
16+
"github.com/decred/vspd/internal/vspd"
1617
"github.com/jessevdk/go-flags"
1718
)
1819

1920
const (
20-
dbFilename = "vspd.db"
21+
configFilename = "vspd.conf"
22+
dbFilename = "vspd.db"
2123
)
2224

2325
type conf struct {
@@ -72,6 +74,32 @@ func createDatabase(homeDir string, feeXPub string, network *config.Network) err
7274
return nil
7375
}
7476

77+
func writeConfig(homeDir string) error {
78+
configFile := filepath.Join(homeDir, configFilename)
79+
80+
// Return an error if the config file already exists.
81+
if fileExists(configFile) {
82+
return fmt.Errorf("config file already exists in %s", homeDir)
83+
}
84+
85+
// Ensure the directory exists.
86+
err := os.MkdirAll(homeDir, 0700)
87+
if err != nil {
88+
return fmt.Errorf("failed to create directory: %w", err)
89+
}
90+
91+
// Write a config file with default values to the provided home directory.
92+
preParser := flags.NewParser(&vspd.DefaultConfig, flags.None)
93+
preIni := flags.NewIniParser(preParser)
94+
err = preIni.WriteFile(configFile,
95+
flags.IniIncludeComments|flags.IniIncludeDefaults)
96+
if err != nil {
97+
return fmt.Errorf("failed to create config file: %w", err)
98+
}
99+
100+
return nil
101+
}
102+
75103
// run is the real main function for vspadmin. It is necessary to work around
76104
// the fact that deferred functions do not run when os.Exit() is called.
77105
func run() int {
@@ -117,6 +145,16 @@ func run() int {
117145

118146
log("New %s vspd database created in %s", network.Name, cfg.HomeDir)
119147

148+
case "writeconfig":
149+
err = writeConfig(cfg.HomeDir)
150+
if err != nil {
151+
log("writeconfig failed: %v", err)
152+
return 1
153+
}
154+
155+
log("Config file with default values written to %s", cfg.HomeDir)
156+
log("Edit the file and fill in values specific to your vspd deployment")
157+
120158
default:
121159
log("%q is not a valid command", remainingArgs[0])
122160
return 1

docs/deployment.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@ vspd. **Do not run a voting wallet on your webserver.**
9292
receiving `blockconnected` notifications, and for broadcasting and checking
9393
the status of fee transactions.
9494

95-
1. Run `vspd` with no arguments to write a default config file. Modify the
96-
config file to set your dcrd and dcrwallet connection details, and any other
97-
required customization.
95+
1. Use [vspadmin](./cmd/vspadmin) to write a config file containing default
96+
values. Modify the config file to set your dcrd and dcrwallet connection
97+
details, and any other required customization.
98+
99+
```no-highlight
100+
$ go run ./cmd/vspadmin writeconfig
101+
```
98102
99103
1. Use [vspadmin](./cmd/vspadmin) to initialize a vpsd database. The xpub key to
100104
be used for collecting fees must be passed in as an argument.
@@ -103,7 +107,8 @@ vspd. **Do not run a voting wallet on your webserver.**
103107
$ go run ./cmd/vspadmin createdatabase tpubVppjaMjp8GEW...
104108
```
105109
106-
1. Once the database is initialized, vspd can be started for normal operation.
110+
1. Once the database is initialized and required fields in the config file have
111+
been entered, vspd can be started for normal operation.
107112
108113
1. Configure nginx with SSL and set up reverse proxy to forward requests to the
109114
vspd process. nginx must also set the `X-Forwarded-For` header to make vspd

internal/vspd/config.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,25 +240,14 @@ func LoadConfig() (*Config, error) {
240240
return nil, err
241241
}
242242

243-
// Create a default config file when one does not exist and the user did
244-
// not specify an override.
243+
// Load additional config from file.
245244
configFile := filepath.Join(cfg.HomeDir, configFilename)
246245
if !fileExists(configFile) {
247-
preIni := flags.NewIniParser(preParser)
248-
err = preIni.WriteFile(configFile,
249-
flags.IniIncludeComments|flags.IniIncludeDefaults)
250-
if err != nil {
251-
return nil, fmt.Errorf("error creating a default "+
252-
"config file: %w", err)
253-
}
254-
fmt.Printf("Config file with default values written to %s\n", configFile)
255-
256-
// File created, user now has to fill in values. Proceeding with the
257-
// default file just causes errors.
258-
os.Exit(0)
246+
err := fmt.Errorf("config file does not exist at %s", configFile)
247+
fmt.Fprintln(os.Stderr, err)
248+
return nil, err
259249
}
260250

261-
// Load additional config from file.
262251
parser := flags.NewParser(&cfg, flags.None)
263252

264253
err = flags.NewIniParser(parser).ParseFile(configFile)

0 commit comments

Comments
 (0)