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

Add argument to specify location of the .steampipe folder. Closes #241 #284

Merged
merged 9 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"log"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/turbot/steampipe/cmdconfig"
"github.com/turbot/steampipe/constants"
"github.com/turbot/steampipe/version"
)

Expand Down Expand Up @@ -47,6 +49,9 @@ func Execute() error {
}

func init() {
rootCmd.PersistentFlags().String(constants.ArgInstallDir, constants.DefaultInstallDir, "Path to the Config Directory")
viper.BindPFlag(constants.ArgInstallDir, rootCmd.PersistentFlags().Lookup(constants.ArgInstallDir))

cobra.OnInitialize(initGlobalConfig)
}

Expand Down
12 changes: 7 additions & 5 deletions cmdconfig/viper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmdconfig

import (
"strings"
"os"

"github.com/spf13/viper"
"github.com/turbot/steampipe/constants"
Expand All @@ -10,12 +10,14 @@ import (
// InitViper :: initializes and configures an instance of viper
func InitViper() {
v := viper.GetViper()
v.SetEnvPrefix("STEAMPIPE")
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

// set defaults
v.Set(constants.ShowInteractiveOutputConfigKey, true)

if installDir, isSet := os.LookupEnv("STEAMPIPE_INSTALL_DIR"); isSet {
v.SetDefault(constants.ArgInstallDir, installDir)
} else {
v.SetDefault(constants.ArgInstallDir, "~/.steampipe")
}
}

// Viper :: fetches the global viper instance
Expand Down
1 change: 1 addition & 0 deletions constants/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
ArgTimer = "timing"
ArgOn = "on"
ArgOff = "off"
ArgInstallDir = "install-dir"
)

/// metaquery mode arguments
Expand Down
18 changes: 9 additions & 9 deletions constants/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import (
"os"
"path/filepath"

"github.com/spf13/viper"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/steampipe/utils"
)

// Constants for Config
const (
SteampipeDirName = ".steampipe"
DefaultInstallDir = "~/.steampipe"
PluginExtension = ".plugin"
ConfigExtension = ".spc"
ConnectionsStateFileName = "connection.json"
)

// SteampipeDir :: returns the top level ~/.steampipe folder (creates if it doesnt exist)
func SteampipeDir() string {
homeDir, err := os.UserHomeDir()
utils.FailOnError(err)
ch := filepath.Join(homeDir, SteampipeDirName)
if _, err := os.Stat(ch); os.IsNotExist(err) {
err = os.MkdirAll(ch, 0755)
utils.FailOnErrorWithMessage(err, "could not create .steampipe directory")
installDir, err := helpers.Tildefy(viper.GetString(ArgInstallDir))
utils.FailOnErrorWithMessage(err, fmt.Sprintf("failed to sanitize install directory"))
if _, err := os.Stat(installDir); os.IsNotExist(err) {
err = os.MkdirAll(installDir, 0755)
utils.FailOnErrorWithMessage(err, fmt.Sprintf("could not create installation directory: %s", installDir))
}

return ch
return installDir
}

func steampipeSubDir(dirName string) string {
Expand Down
43 changes: 34 additions & 9 deletions db/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import (
"net"
"os"
"os/exec"
"strings"
"syscall"

"github.com/turbot/go-kit/helpers"
"github.com/turbot/steampipe/cmdconfig"
"github.com/turbot/steampipe/utils"

"github.com/shirou/gopsutil/process"
"github.com/turbot/steampipe/constants"
Expand Down Expand Up @@ -70,7 +73,7 @@ func (slt Invoker) IsValid() error {

// StartDB :: start the database is not already running
func StartDB(port int, listen StartListenType, invoker Invoker) (StartResult, error) {
info, err := loadRunningInstanceInfo()
info, err := GetStatus()

if err != nil {
return ServiceFailedToStart, err
Expand Down Expand Up @@ -104,6 +107,15 @@ func StartDB(port int, listen StartListenType, invoker Invoker) (StartResult, er
return ServiceFailedToStart, fmt.Errorf("Cannot listen on %d. Are you sure that the interface is free?", port)
}

checkedPreviousInstances := make(chan bool, 1)
s := utils.StartSpinnerAfterDelay("Checking for running instances", constants.SpinnerShowTimeout, checkedPreviousInstances)
previousProcess := findSteampipePostgresInstance()
checkedPreviousInstances <- true
utils.StopSpinner(s)
if previousProcess != nil {
return ServiceFailedToStart, fmt.Errorf("Another Steampipe service is already running. Use %s to kill all running instances before continuing.", constants.Bold("steampipe service stop --force"))
}

postgresCmd := exec.Command(
getPostgresBinaryExecutablePath(),
// by this time, we are sure that the port if free to listen to
Expand Down Expand Up @@ -235,19 +247,32 @@ func isPortBindable(port int) bool {

// kill all postgres processes that were started as part of steampipe (if any)
func killPreviousInstanceIfAny() bool {
p := findSteampipePostgresInstance()
if p != nil {
killProcessTree(p)
return true
}
return false
}

func findSteampipePostgresInstance() *process.Process {
allProcesses, _ := process.Processes()
for _, p := range allProcesses {
cmdLine, _ := p.CmdlineSlice()
if len(cmdLine) < 1 {
continue
if isSteampipePostgresProcess(cmdLine) {
return p
}
executable := cmdLine[0]
}
return nil
}

// this is a steampipe postgres, kill it along with it's children
if executable == getPostgresBinaryExecutablePath() {
killProcessTree(p)
return true
}
func isSteampipePostgresProcess(cmdline []string) bool {
if len(cmdline) < 1 {
return false
}
if strings.Contains(cmdline[0], "postgres") {
// this is a postgres process
return helpers.StringSliceContains(cmdline, fmt.Sprintf("application_name=%s", constants.APPNAME))
}
return false
}
Expand Down
13 changes: 13 additions & 0 deletions db/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ func StopDB(force bool) (StopStatus, error) {

if info == nil {
// we do not have a info file
if force {
// check if we have a process from another install-dir
checkedPreviousInstances := make(chan bool, 1)
s := utils.StartSpinnerAfterDelay("Checking for running instances", constants.SpinnerShowTimeout, checkedPreviousInstances)
previousProcess := findSteampipePostgresInstance()
checkedPreviousInstances <- true
utils.StopSpinner(s)
if previousProcess != nil {
// we have an errant process
killPreviousInstanceIfAny()
return ServiceStopped, nil
}
}
return ServiceNotRunning, nil
}

Expand Down