From d6d3a108170127f7f1b9d4b6efcd7aaebd608d65 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Mon, 31 Dec 2018 01:17:44 -0500 Subject: [PATCH 1/3] implement #322 Automatically create the datastore on `oragono run` if it doesn't exist. See also #302. --- irc/database.go | 30 ++++++++++++++++++------------ irc/server.go | 9 +++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/irc/database.go b/irc/database.go index 6fef867b..635ffee5 100644 --- a/irc/database.go +++ b/irc/database.go @@ -52,14 +52,26 @@ func (err *incompatibleSchemaError) Error() string { return fmt.Sprintf("Database requires update. Expected schema v%s, got v%s", err.requiredVersion, err.currentVersion) } -// InitDB creates the database. +// InitDB creates the database, implementing the `oragono initdb` command. func InitDB(path string) { - // prepare kvstore db - //TODO(dan): fail if already exists instead? don't want to overwrite good data - os.Remove(path) + _, err := os.Stat(path) + if err == nil { + log.Fatal("Datastore already exists (delete it manually to continue): ", path) + } else if !os.IsNotExist(err) { + log.Fatal("Datastore path is inaccessible: ", err.Error()) + } + + err = initializeDB(path) + if err != nil { + log.Fatal("Could not save datastore: ", err.Error()) + } +} + +// internal database initialization code +func initializeDB(path string) error { store, err := buntdb.Open(path) if err != nil { - log.Fatal(fmt.Sprintf("Failed to open datastore: %s", err.Error())) + return err } defer store.Close() @@ -69,9 +81,7 @@ func InitDB(path string) { return nil }) - if err != nil { - log.Fatal("Could not save datastore:", err.Error()) - } + return err } // OpenDatabase returns an existing database, performing a schema version check. @@ -81,10 +91,6 @@ func OpenDatabase(config *Config) (*buntdb.DB, error) { // open the database, giving it at most one chance to auto-upgrade the schema func openDatabaseInternal(config *Config, allowAutoupgrade bool) (db *buntdb.DB, err error) { - _, err = os.Stat(config.Datastore.Path) - if os.IsNotExist(err) { - return - } db, err = buntdb.Open(config.Datastore.Path) if err != nil { return diff --git a/irc/server.go b/irc/server.go index 1afee925..e1e29ae0 100644 --- a/irc/server.go +++ b/irc/server.go @@ -885,6 +885,15 @@ func (server *Server) loadDatastore(config *Config) error { // open the datastore and load server state for which it (rather than config) // is the source of truth + _, err := os.Stat(config.Datastore.Path) + if os.IsNotExist(err) { + server.logger.Warning("startup", "database does not exist, creating it", config.Datastore.Path) + err = initializeDB(config.Datastore.Path) + if err != nil { + return err + } + } + db, err := OpenDatabase(config) if err == nil { server.store = db From 9e87d6dd10568841c68fabb37313f16fb8770958 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Mon, 31 Dec 2018 01:33:46 -0500 Subject: [PATCH 2/3] remove explicit initdb instructions from the docs --- README | 1 - README.md | 1 - docs/MANUAL.md | 22 ++++++++++------------ 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/README b/README index 2e3abe2d..fcaeac4a 100644 --- a/README +++ b/README @@ -34,7 +34,6 @@ To generate passwords for opers and connect passwords, you can use this command: Run these commands in order -- these will setup each section of the server: - $ oragono initdb $ oragono mkcerts $ oragono run diff --git a/README.md b/README.md index 93811b4b..6371e145 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,6 @@ Extract it into a folder, then run the following commands: ```sh cp oragono.yaml ircd.yaml vim ircd.yaml # modify the config file to your liking -oragono initdb oragono mkcerts ``` diff --git a/docs/MANUAL.md b/docs/MANUAL.md index a6aeb949..11c22edf 100644 --- a/docs/MANUAL.md +++ b/docs/MANUAL.md @@ -78,12 +78,11 @@ In this section, we'll explain how to install and use the Oragono IRC server. To get started with Oragono on Windows: 1. Make sure you have the [latest release](https://github.com/oragono/oragono/releases/latest) downloaded. -2. Extract the zip file to a folder. -3. Copy and rename `oragono.yaml` to `ircd.yaml`. -4. Open up `ircd.yaml` using any text editor, and then save it once you're happy. -5. Open up a `cmd.exe` window, then `cd` to where you have Oragono extracted. -6. Run `oragono.exe initdb` (this creates the database). -7. Run `oragono.exe mkcerts` if you want to generate new self-signed SSL/TLS certificates (note that you can't enable STS if you use self-signed certs). +1. Extract the zip file to a folder. +1. Copy and rename `oragono.yaml` to `ircd.yaml`. +1. Open up `ircd.yaml` using any text editor, and then save it once you're happy. +1. Open up a `cmd.exe` window, then `cd` to where you have Oragono extracted. +1. Run `oragono.exe mkcerts` if you want to generate new self-signed SSL/TLS certificates (note that you can't enable STS if you use self-signed certs). To start the server, type `oragono.exe run` and hit enter, and the server should start! @@ -93,12 +92,11 @@ To start the server, type `oragono.exe run` and hit enter, and the server should To get started with Oragono on macOS, Linux, or on a Raspberry Pi: 1. Make sure you have the [latest release](https://github.com/oragono/oragono/releases/latest) for your OS/distro downloaded. -2. Extract the tar.gz file to a folder. -3. Copy and rename `oragono.yaml` to `ircd.yaml`. -4. Open up `ircd.yaml` using any text editor, and then save it once you're happy. -5. Open up a Terminal window, then `cd` to where you have Oragono extracted. -6. Run `./oragono initdb` (this creates the database). -7. Run `./oragono mkcerts` if you want to generate new self-signed SSL/TLS certificates (note that you can't enable STS if you use self-signed certs). +1. Extract the tar.gz file to a folder. +1. Copy and rename `oragono.yaml` to `ircd.yaml`. +1. Open up `ircd.yaml` using any text editor, and then save it once you're happy. +1. Open up a Terminal window, then `cd` to where you have Oragono extracted. +1. Run `./oragono mkcerts` if you want to generate new self-signed SSL/TLS certificates (note that you can't enable STS if you use self-signed certs). To start the server, type `./oragono run` and hit enter, and the server should be ready to use! From 6402a69fb92cba97de6524836340f08e49936f7b Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Mon, 31 Dec 2018 01:39:58 -0500 Subject: [PATCH 3/3] use the rehash log category consistently --- irc/server.go | 8 ++++---- oragono.yaml | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/irc/server.go b/irc/server.go index e1e29ae0..d888d018 100644 --- a/irc/server.go +++ b/irc/server.go @@ -887,7 +887,7 @@ func (server *Server) loadDatastore(config *Config) error { _, err := os.Stat(config.Datastore.Path) if os.IsNotExist(err) { - server.logger.Warning("startup", "database does not exist, creating it", config.Datastore.Path) + server.logger.Warning("rehash", "database does not exist, creating it", config.Datastore.Path) err = initializeDB(config.Datastore.Path) if err != nil { return err @@ -902,7 +902,7 @@ func (server *Server) loadDatastore(config *Config) error { } // load *lines (from the datastores) - server.logger.Debug("startup", "Loading D/Klines") + server.logger.Debug("rehash", "Loading D/Klines") server.loadDLines() server.loadKLines() @@ -974,7 +974,7 @@ func (server *Server) setupListeners(config *Config) (err error) { } if len(tlsListeners) == 0 { - server.logger.Warning("startup", "You are not exposing an SSL/TLS listening port. You should expose at least one port (typically 6697) to accept TLS connections") + server.logger.Warning("rehash", "You are not exposing an SSL/TLS listening port. You should expose at least one port (typically 6697) to accept TLS connections") } var usesStandardTLSPort bool @@ -985,7 +985,7 @@ func (server *Server) setupListeners(config *Config) (err error) { } } if 0 < len(tlsListeners) && !usesStandardTLSPort { - server.logger.Warning("startup", "Port 6697 is the standard TLS port for IRC. You should (also) expose port 6697 as a TLS port to ensure clients can connect securely") + server.logger.Warning("rehash", "Port 6697 is the standard TLS port for IRC. You should (also) expose port 6697 as a TLS port to ensure clients can connect securely") } return diff --git a/oragono.yaml b/oragono.yaml index 03ce36d4..ba54bbbd 100644 --- a/oragono.yaml +++ b/oragono.yaml @@ -340,6 +340,7 @@ logging: # commands command calling and operations # opers oper actions, authentication, etc # password password hashing and comparing + # rehash server startup and rehash events # userinput raw lines sent by users # useroutput raw lines sent to users type: "* -userinput -useroutput -localconnect -localconnect-ip"