-
Notifications
You must be signed in to change notification settings - Fork 18
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
feat: Add support of configuring host to use Satellite server #170
Open
jirihnidek
wants to merge
1
commit into
main
Choose a base branch
from
jhnidek/satellite
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/briandowns/spinner" | ||
"github.com/urfave/cli/v2" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
// beforeSatelliteAction is run before satelliteAction is run. It is used | ||
// for checking CLI options. | ||
func beforeSatelliteAction(ctx *cli.Context) error { | ||
// First check if machine-readable format is used | ||
err := setupFormatOption(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
satelliteUrlStr := ctx.String("url") | ||
if satelliteUrlStr == "" { | ||
return fmt.Errorf("no url provided using --url CLI option") | ||
} | ||
|
||
return checkForUnknownArgs(ctx) | ||
} | ||
|
||
// satelliteAction tries to get bootstrap script from Satellite server and run it. | ||
// When it is not possible to download the script or running script returns | ||
// non-zero exit code, then error is returned. | ||
// | ||
// It is really risky to download to run some script downloaded from the URL as a | ||
// root user without any restriction. For this reason, we at least check that | ||
// provided URL is URL of Satellite server. | ||
// | ||
// We would like to use different approach in the future. We would like to use | ||
// some API endpoints not restricted by username & password for getting CA certs | ||
// and rendered rhsm.conf, because it would be more secure, but it is not possible ATM | ||
func satelliteAction(ctx *cli.Context) error { | ||
var configureSatelliteResult ConfigureSatelliteResult | ||
configureSatelliteResult.format = ctx.String("format") | ||
satelliteUrlStr := ctx.String("url") | ||
|
||
uid := os.Getuid() | ||
if uid != 0 { | ||
errMsg := "non-root user cannot connect system" | ||
exitCode := 1 | ||
return cli.Exit(fmt.Errorf("error: %satSpinner", errMsg), exitCode) | ||
} | ||
|
||
hostname, err := os.Hostname() | ||
if uiSettings.isMachineReadable { | ||
configureSatelliteResult.Hostname = hostname | ||
} | ||
if err != nil { | ||
exitCode := 1 | ||
if uiSettings.isMachineReadable { | ||
configureSatelliteResult.HostnameError = err.Error() | ||
return cli.Exit(configureSatelliteResult, exitCode) | ||
} else { | ||
return cli.Exit(err, exitCode) | ||
} | ||
} | ||
|
||
satelliteUrl, err := normalizeSatelliteScriptUrl(satelliteUrlStr) | ||
if err != nil { | ||
return cli.Exit(fmt.Errorf("could not parse satellite url: %w", err), 1) | ||
} | ||
|
||
configureSatelliteResult.SatelliteServerHostname = satelliteUrl.Hostname() | ||
configureSatelliteResult.SatelliteServerScriptUrl = satelliteUrl.String() | ||
|
||
var satSpinner *spinner.Spinner = nil | ||
if uiSettings.isRich { | ||
satSpinner = spinner.New(spinner.CharSets[9], 100*time.Millisecond) | ||
satSpinner.Suffix = fmt.Sprintf(" Configuring '%v' to use Satellite %v", hostname, satelliteUrl.Host) | ||
satSpinner.Start() | ||
// Stop spinner after running function | ||
defer func() { satSpinner.Stop() }() | ||
} | ||
|
||
err = pingSatelliteServer(satelliteUrl, satSpinner) | ||
if err != nil { | ||
return cli.Exit(fmt.Errorf("unable to verify that given server is Satellite server: %v", err), 1) | ||
} | ||
|
||
configureSatelliteResult.IsServerSatellite = true | ||
|
||
// Create file for script file | ||
satelliteScriptPath := filepath.Join(VarLibDir, "katello-rhsm-consumer") | ||
defer os.Remove(satelliteScriptPath) | ||
scriptFile, err := os.Create(satelliteScriptPath) | ||
if err != nil { | ||
return cli.Exit(fmt.Errorf("could not create %v file: %w", satelliteScriptPath, err), 1) | ||
} | ||
defer scriptFile.Close() | ||
err = os.Chmod(satelliteScriptPath, 0700) | ||
if err != nil { | ||
return cli.Exit(fmt.Errorf("could not set permissions on %v file: %w", satelliteScriptPath, err), 1) | ||
} | ||
|
||
err = downloadSatelliteScript(scriptFile, satelliteUrl, satSpinner) | ||
if err != nil { | ||
return cli.Exit(fmt.Errorf("could not download satellite bootstrap script: %w", err), 1) | ||
} | ||
|
||
if satSpinner != nil { | ||
satSpinner.Suffix = fmt.Sprintf(" Configuring '%v' to use Satellite server: %v", hostname, satelliteUrl.Host) | ||
} | ||
// Run the script. It should install CA certificate, change configuration of rhsm.conf. | ||
// In theory, it can do almost anything. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scary. We should follow this up with a distributed SELinux policy to restrict the scope execution for |
||
cmd := exec.Command(satelliteScriptPath) | ||
err = cmd.Run() | ||
subpop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return cli.Exit(fmt.Errorf("execution of %v script failed: %w", satelliteScriptPath, err), 1) | ||
} | ||
|
||
configureSatelliteResult.HostConfigured = true | ||
|
||
if uiSettings.isRich { | ||
satSpinner.Suffix = "" | ||
satSpinner.Stop() | ||
} | ||
|
||
interactivePrintf("Host '%v' configured to use Satellite server: %v\n", hostname, satelliteUrl.Host) | ||
|
||
return cli.Exit(configureSatelliteResult, 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need a wrapping error message here?