Skip to content

Commit

Permalink
Merge pull request #154 from sol-eng/add-user-check-and-verify
Browse files Browse the repository at this point in the history
Add user step to setup and add prompt user with lookup and validation
  • Loading branch information
samcofer authored May 11, 2023
2 parents 55d7453 + 8bf08c9 commit 427d3e4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
10 changes: 8 additions & 2 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,15 @@ func newSetup(setupOpts setupOpts) error {
return fmt.Errorf("issue selecting if verification is to be run: %w", err)
}
if verifyChoice {
err = workbench.VerifyInstallation()
username, skip, err := operatingsystem.PromptAndVerifyUser()
if err != nil {
return fmt.Errorf("issue running verification: %w", err)
return err
}
if !skip {
err = workbench.VerifyInstallation(username)
if err != nil {
return fmt.Errorf("issue running verification: %w", err)
}
}
}
step = "done"
Expand Down
9 changes: 9 additions & 0 deletions internal/operatingsystem/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package operatingsystem
import (
"errors"
"os"
"os/user"
"runtime"
"strings"

Expand Down Expand Up @@ -49,3 +50,11 @@ func DetectOS() (config.OperatingSystem, error) {
return config.Unknown, errors.New("unsupported operating system")
}
}

func UserLookup(username string) (*user.User, error) {
user, err := user.Lookup(username)
if err != nil {
return nil, err
}
return user, nil
}
29 changes: 23 additions & 6 deletions internal/operatingsystem/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ func PromptInstallPrereqs() (bool, error) {
var name bool
messageText := "In order to install Workbench from start to finish, you will need the following things\n" +
"1. Internet access for this server\n" +
"2. The versions of R and Python you would like to install\n" +
"3. The version of R and Python you would like to set as defaults\n" +
"4. Your Workbench license key string in this form: XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX\n" +
"5. The location on this server of your SSL key and certificate files (optional)\n" +
"6. The URL and repo name for your instance of Posit Package Manager (optional)\n" +
"7. The URL for your instance of Posit Connect (optional)\n\n" +
"2. At least one non-root local Linux user account with a home directory\n" +
"3. The versions of R and Python you would like to install\n" +
"4. The version of R and Python you would like to set as defaults\n" +
"5. Your Workbench license key string in this form: XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX\n" +
"6. The location on this server of your SSL key and certificate files (optional)\n" +
"7. The URL and repo name for your instance of Posit Package Manager (optional)\n" +
"8. The URL for your instance of Posit Connect (optional)\n\n" +
"Please confirm that you're ready to install Workbench"
prompt := &survey.Confirm{
Message: messageText,
Expand All @@ -79,3 +80,19 @@ func PromptInstallPrereqs() (bool, error) {
log.Info(fmt.Sprintf("%v", name))
return name, nil
}

// PromptUserAccount prompts the user for the name of a local Linux user account to use for verifying the installation
func PromptUserAccount() (string, error) {
target := ""
messageText := "Enter a non-root local Linux account username to use for testing the Workbench installation:"
prompt := &survey.Input{
Message: messageText,
}
err := survey.AskOne(prompt, &target)
if err != nil {
return "", fmt.Errorf("issue prompting for a local user account: %w", err)
}
log.Info(messageText)
log.Info(target)
return target, nil
}
38 changes: 38 additions & 0 deletions internal/operatingsystem/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package operatingsystem

import (
"fmt"
"strings"

"github.com/sol-eng/wbi/internal/system"
)

func PromptAndVerifyUser() (string, bool, error) {
var overallSkip bool
var userAccount string
for {
username, err := PromptUserAccount()
userAccount = username
if err != nil {
return "", false, err
}
if strings.Contains(userAccount, "skip") {
overallSkip = true
break
}
// lookup user account details
user, err := UserLookup(userAccount)
if err != nil {
system.PrintAndLogInfo(fmt.Sprintf(`The user account "%s" you entered cannot be found. Please try again. To skip this section type "skip".`, userAccount))
} else if user.Uid == "0" {
system.PrintAndLogInfo(fmt.Sprintf(`The user account "%s" is root. A non-root user is required. Please try again. To skip this section type "skip".`, userAccount))
} else if user.HomeDir == "" {
system.PrintAndLogInfo(fmt.Sprintf(`The user account "%s" does not have a home directory. A home directory is required. Please try again. To skip this section type "skip".`, userAccount))
} else {
system.PrintAndLogInfo(fmt.Sprintf("user %s account found and validated", userAccount))
break
}
}

return userAccount, overallSkip, nil
}
7 changes: 4 additions & 3 deletions internal/workbench/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ func VerifyWorkbench() bool {
}

// Runs verify-installation command
func VerifyInstallation() error {
func VerifyInstallation(username string) error {
// stop rstudio-server
err := StopRStudioServer()
if err != nil {
return fmt.Errorf("issue stopping rstudio-server: %w", err)
}
// run verify-installation
err = system.RunCommand("rstudio-server verify-installation", true, 1)
verifyCommand := "rstudio-server verify-installation --verify-user=" + username
err = system.RunCommand(verifyCommand, true, 1)
if err != nil {
return fmt.Errorf("issue running verify-installation command 'rstudio-server verify-installation': %w", err)
return fmt.Errorf("issue running verify-installation command '%s': %w", verifyCommand, err)
}
// start rstudio-server
err = StartRStudioServer()
Expand Down

0 comments on commit 427d3e4

Please sign in to comment.