Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for setting a custom database name. Closes #936 #988

Merged
merged 23 commits into from
Oct 11, 2021

Conversation

binaek
Copy link
Contributor

@binaek binaek commented Oct 6, 2021

When we run steampipe for the first time, if it finds the environment variable STEAMPIPE_INSTALL_DATABASE set to a non-empty value, it will install the database with the given name - if it's not present, or is empty, it will default to steampipe

@binaek binaek added the enhancement New feature or request label Oct 6, 2021
@binaek binaek self-assigned this Oct 6, 2021
@binaek binaek linked an issue Oct 6, 2021 that may be closed by this pull request
@binaek binaek marked this pull request as draft October 6, 2021 10:35
@binaek binaek marked this pull request as ready for review October 6, 2021 16:11
db/db_local/create_client.go Outdated Show resolved Hide resolved
}

func installSteampipeDatabaseAndUser(steampipePassword string, rootPassword string) error {
func installDatabaseAndSetupPermissions() (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment explaining what the return values are

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact I'd be tempted to pull out the database name logic and pass the name in from the calling code

functions should do 1 thing where possible

}

func installForeignServer() error {
func installForeignServer(dbName string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dbName -> databaseName

}
return sig
}
// func getInstalledBinarySignature() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we delete this?

@@ -142,7 +142,7 @@ func StartDB(port int, listen StartListenType, invoker constants.Invoker) (start

// startPostgresProcessAndSetPassword starts the postgres process and writes out the state file
// after it is convinced that the process is started and is accepting connections
func startPostgresProcessAndSetPassword(port int, listen StartListenType, invoker constants.Invoker) (e error) {
func startPostgresProcessAndSetPassword(port int, listen StartListenType, invoker constants.Invoker) (datName string, e error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need named parameters?

}

err = postgresCmd.Process.Release()
if err != nil {
postgresCmd.Process.Kill()
return err
return "", err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this doing? add comment

postgresCmd.Process.Release()

}

// connect to the service and retrieve the database name
datName, err = retrieveDatabaseNameFromService(invoker)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

databaseName

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and please use this consistently

@@ -142,7 +142,7 @@ func StartDB(port int, listen StartListenType, invoker constants.Invoker) (start

// startPostgresProcessAndSetPassword starts the postgres process and writes out the state file
// after it is convinced that the process is started and is accepting connections
func startPostgresProcessAndSetPassword(port int, listen StartListenType, invoker constants.Invoker) (e error) {
func startPostgresProcessAndSetPassword(port int, listen StartListenType, invoker constants.Invoker) (datName string, e error) {
utils.LogTime("startPostgresProcess start")
defer utils.LogTime("startPostgresProcess end")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function should read as follows: (with error handling of course)

func startPostgresAndSetPassword(port int, listen StartListenType, invoker constants.Invoker) (string, error) {
 
 process, err := startPostgreProcess(port, listen, invoker)

  databaseName, err = retrieveDatabaseName()

  err := setPassword()
}

}

constants/env.go Outdated
@@ -5,6 +5,7 @@ const (
EnvUpdateCheck = "STEAMPIPE_UPDATE_CHECK"
EnvInstallDir = "STEAMPIPE_INSTALL_DIR"
EnvConnectionString = "STEAMPIPE_CONNECTION_STRING"
EnvInstallDatabase = "STEAMPIPE_INSTALL_DATABASE"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have STEAMPIPE_INSTALL_DATABASE and STEAMPIPE_DATABASE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STEAMPIPE_DATABASE is used by the remote connection layer to connect to Steampipe Cloud, using the API_KEY

Copy link
Contributor

@kaidaguerre kaidaguerre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getting there

_, err := executeSqlAsRoot(statements...)

for _, statement := range statements {
// NOTE: This may print a password to the log file, but it doesn't matter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I agree that it doesn't matter - can we do anything about this?

// after it is convinced that the process is started and is accepting connections
func startPostgresProcessAndSetPassword(port int, listen StartListenType, invoker constants.Invoker) (e error) {
func startPostgresProcessAndSetup(port int, listen StartListenType, invoker constants.Invoker) (databaseName string, e error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e -> err


return psqlInfo, nil
}

// createRootDbClient connects as a superuser to the
// installed database, if available, otherwise to the default
// "postgres" database
func createRootDbClient() (*sql.DB, error) {
utils.LogTime("db.createSteampipeRootDbClient start")
defer utils.LogTime("db™.createSteampipeRootDbClient end")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

were you going to factor this out? I hate passing the empty name to createLocalDbClient

}
}()
// release the process - let the OS adopt it, so that we can exit
// DO AT END? IN DEFER?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove/resolve this comment

// DO AT END? IN DEFER?

return ServiceStarted, err
}

// startPostgresProcessAndSetup starts the postgres process and writes out the state file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove


err = setupServicePassword(invoker, password)
// set the password on the database
// we can't do this during installation, since the 'steampipe` user isn't setup yet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inconsistent quote marks - do not use slanty quotes in code comments

@kaidaguerre kaidaguerre merged commit b94a5fd into main Oct 11, 2021
@kaidaguerre kaidaguerre deleted the service_db_name_936 branch October 11, 2021 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for configurable DATABASE_NAME during installation.
2 participants