Skip to content

Commit

Permalink
Add install-dir argument to specify location of the installation fo…
Browse files Browse the repository at this point in the history
…lder. Closes #241
  • Loading branch information
binaek authored Mar 15, 2021
1 parent ce91eb9 commit 5abe6a0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 23 deletions.
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

0 comments on commit 5abe6a0

Please # to comment.