-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support of configuring host to use Satellite server
* Card ID: CCT-993 * Introduced new command "configure" and immediately added sub-command "satellite" to this command. "satellite" subcommand has --url CLI option and you can set hostname or URL of server Satellite server. * When valid URL or hostname is provided, then rhc tries to read /api/ping endpoint and it tries to verify that given server is Satellite server * When Satellite server is verified, then it tries to download bootstrap script from /pub/katello-rhsm-consumer * When script is downloaded, then this script is run as root user. * Is little bit risky to run some script downloaded from web server. For this reason we will try to use different approach in the future. Satellite server will provide public REST API endpoints providing CA certs and rendered rhsm.conf. We will simply download these files and install them to system. Unfortunately, these REST API endpoints will be available in some future version of Satellite. * Directory /var/lib/rhc is created during installation of RPM package
- Loading branch information
1 parent
2d11822
commit 2413479
Showing
5 changed files
with
377 additions
and
0 deletions.
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. | ||
cmd := exec.Command(satelliteScriptPath) | ||
err = cmd.Run() | ||
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.