Skip to content

Commit

Permalink
Fix minishift#489 Handle subscription manager proxy from shell enviro…
Browse files Browse the repository at this point in the history
…nment variable
  • Loading branch information
praveenkumar committed Mar 3, 2017
1 parent fa8b19c commit c3cb785
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 272 deletions.
7 changes: 0 additions & 7 deletions cmd/minishift/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ func runDelete(cmd *cobra.Command, args []string) {
api := libmachine.NewClient(constants.Minipath, constants.MakeMiniPath("certs"))
defer api.Close()

setSubcriptionManagerParameters()

if err := cluster.DeleteHost(api); err != nil {
fmt.Println("Error deleting the VM: ", err)
atexit.Exit(1)
Expand All @@ -50,11 +48,6 @@ func runDelete(cmd *cobra.Command, args []string) {
}

func init() {
deleteCmd.Flags().String(username, "", "Username for the virtual machine unregistration.")
deleteCmd.Flags().String(password, "", "Password for the virtual machine unregistration.")

deleteCmd.Flags().AddFlagSet(subscriptionManagerFlagSet)

viper.BindPFlags(deleteCmd.Flags())
RootCmd.AddCommand(deleteCmd)
}
44 changes: 1 addition & 43 deletions cmd/minishift/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func runStart(cmd *cobra.Command, args []string) {
setDockerProxy()
setOcProxy()
setShellProxy()
SetRegistrationProxyParameters()
setSubcriptionManagerParameters()

config := cluster.MachineConfig{
Expand All @@ -147,6 +146,7 @@ func runStart(cmd *cobra.Command, args []string) {
RegistryMirror: registryMirror,
HostOnlyCIDR: viper.GetString(hostOnlyCIDR),
OpenShiftVersion: viper.GetString(openshiftVersion),
ShellProxyEnv: shellProxyEnv,
}

fmt.Printf("Starting local OpenShift cluster using '%s' hypervisor...\n", config.VMDriver)
Expand All @@ -165,14 +165,6 @@ func runStart(cmd *cobra.Command, args []string) {
atexit.Exit(1)
}

// Set Proxy to as Shell Env
if viper.IsSet("http-proxy") || viper.IsSet("https-proxy") {
if err := minishiftUtil.SetProxyToShellEnv(host, shellProxyEnv); err != nil {
fmt.Printf("Error setting proxy to VM: %s", err)
atexit.Exit(1)
}
}

// Making sure the required Docker environment variables are set to make 'cluster up' work
envMap, err := cluster.GetHostDockerEnv(libMachineClient)
for k, v := range envMap {
Expand Down Expand Up @@ -240,40 +232,6 @@ func updateNoProxyForDocker() string {
return "localhost,127.0.0.1,172.30.1.1"
}

//Update RegistrationProxyParameters with proxy information
func SetRegistrationProxyParameters() {
var proxyUri string

if viper.IsSet("https-proxy") {
proxyUri = viper.GetString(httpsProxy)
} else if viper.IsSet("http-proxy") {
proxyUri = viper.GetString(httpProxy)
}

if proxyUri != "" {
server, serverPort, user, password, err := minishiftUtil.ParseProxyUri(proxyUri)
if err != nil {
glog.Errorf("Not able to parse the proxy URI: %s\n", err)
atexit.Exit(1)
}

if server != "" {
cluster.RegistrationParameters.ProxyServer = server
}
if serverPort != "" {
cluster.RegistrationParameters.ProxyServerPort = serverPort
}

if user != "" {
cluster.RegistrationParameters.ProxyUsername = user
}

if password != "" {
cluster.RegistrationParameters.ProxyPassword = password
}
}
}

// calculateDiskSizeInMB converts a human specified disk size like "1000MB" or "1GB" and converts it into Megabits
func calculateDiskSizeInMB(humanReadableDiskSize string) int {
diskSize, err := units.FromHumanSize(humanReadableDiskSize)
Expand Down
7 changes: 0 additions & 7 deletions cmd/minishift/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ func runStop(cmd *cobra.Command, args []string) {
api := libmachine.NewClient(constants.Minipath, constants.MakeMiniPath("certs"))
defer api.Close()

setSubcriptionManagerParameters()

if err := cluster.StopHost(api); err != nil {
fmt.Println("Error stopping cluster: ", err)
atexit.Exit(1)
Expand All @@ -51,11 +49,6 @@ func runStop(cmd *cobra.Command, args []string) {
}

func init() {
stopCmd.Flags().String(username, "", "Username for the virtual machine unregistration.")
stopCmd.Flags().String(password, "", "Password for the virtual machine unregistration.")

stopCmd.Flags().AddFlagSet(subscriptionManagerFlagSet)

viper.BindPFlags(stopCmd.Flags())
RootCmd.AddCommand(stopCmd)
}
7 changes: 0 additions & 7 deletions docs/minishift_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ Deletes the Minishift VM, including the local OpenShift cluster and all associat
minishift delete
```

### Options

```
--password string Password for the virtual machine unregistration.
--username string Username for the virtual machine unregistration.
```

### Options inherited from parent commands

```
Expand Down
7 changes: 0 additions & 7 deletions docs/minishift_stop.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ VM but does not delete any associated files. To start the cluster again, use the
minishift stop
```

### Options

```
--password string Password for the virtual machine unregistration.
--username string Username for the virtual machine unregistration.
```

### Options inherited from parent commands

```
Expand Down
8 changes: 8 additions & 0 deletions pkg/minikube/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/golang/glog"
"github.com/minishift/minishift/pkg/minikube/constants"
"github.com/minishift/minishift/pkg/minishift/registration"
minishiftUtil "github.com/minishift/minishift/pkg/minishift/util"
"github.com/minishift/minishift/pkg/util"
pb "gopkg.in/cheggaaa/pb.v1"
kubeapi "k8s.io/kubernetes/pkg/api"
Expand Down Expand Up @@ -192,6 +193,7 @@ type MachineConfig struct {
RegistryMirror []string
HostOnlyCIDR string // Only used by the virtualbox driver
OpenShiftVersion string
ShellProxyEnv string // Only used for proxy purpose
}

func engineOptions(config MachineConfig) *engine.Options {
Expand Down Expand Up @@ -338,6 +340,12 @@ func createHost(api libmachine.API, config MachineConfig) (*host.Host, error) {
return nil, fmt.Errorf("Error attempting to save store: %s", err)
}

if config.ShellProxyEnv != "" {
if err := minishiftUtil.SetProxyToShellEnv(h, config.ShellProxyEnv); err != nil {
return nil, fmt.Errorf("Error setting proxy to VM: %s", err)
}
}

if err := registration.RegisterHostVM(h, RegistrationParameters); err != nil {
return nil, fmt.Errorf("Error registering the VM: %s", err)
}
Expand Down
29 changes: 5 additions & 24 deletions pkg/minishift/registration/redhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,19 @@ func (registrator *RedHatRegistrator) CompatibleWithDistribution(osReleaseInfo *
if osReleaseInfo.ID != "rhel" {
return false
}
if _, err := registrator.SSHCommand("sudo subscription-manager version"); err != nil {
if _, err := registrator.SSHCommand("sudo -E subscription-manager version"); err != nil {
return false
} else {
return true
}
}

func (registrator *RedHatRegistrator) Register(param *RegistrationParameters) error {
if output, err := registrator.SSHCommand("sudo subscription-manager version"); err != nil {
if output, err := registrator.SSHCommand("sudo -E subscription-manager version"); err != nil {
return err
} else {
if strings.Contains(output, "not registered") {

//Configure subscription-manager for proxy enviornments
if param.ProxyServer != "" {
configCommand := fmt.Sprintf("sudo subscription-manager config ")
configCommand = configCommand +
fmt.Sprintf("--server.proxy_hostname %s "+
"--server.proxy_port %s ", param.ProxyServer, param.ProxyServerPort)
if param.ProxyUsername != "" {
configCommand = configCommand +
fmt.Sprintf("--server.proxy_user %s ", param.ProxyUsername)
if param.Password != "" {
configCommand = configCommand +
fmt.Sprintf("--server.proxy_password %s ", param.ProxyPassword)
}
}
if _, err := registrator.SSHCommand(configCommand); err != nil {
return err
}
}
subscriptionCommand := fmt.Sprintf("sudo subscription-manager register --auto-attach "+
subscriptionCommand := fmt.Sprintf("sudo -E subscription-manager register --auto-attach "+
"--username %s "+
"--password %s ", param.Username, param.Password)
if _, err := registrator.SSHCommand(subscriptionCommand); err != nil {
Expand All @@ -86,12 +67,12 @@ func (registrator *RedHatRegistrator) Register(param *RegistrationParameters) er
}

func (registrator *RedHatRegistrator) Unregister(param *RegistrationParameters) error {
if output, err := registrator.SSHCommand("sudo subscription-manager version"); err != nil {
if output, err := registrator.SSHCommand("sudo -E subscription-manager version"); err != nil {
return err
} else {
if !strings.Contains(output, "not registered") {
if _, err := registrator.SSHCommand(
"sudo subscription-manager unregister"); err != nil {
"sudo -E subscription-manager unregister"); err != nil {
return err
}
}
Expand Down
18 changes: 7 additions & 11 deletions pkg/minishift/registration/redhat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,18 @@ import (

var (
param = &RegistrationParameters{
Username: "foo",
Password: "foo",
ProxyUsername: "foo",
ProxyPassword: "foo",
ProxyServer: "foo",
ProxyServerPort: "foo",
Username: "foo",
Password: "foo",
}
expectedCMDRegistration = fmt.Sprintf("sudo subscription-manager register --auto-attach --username %s --password %s ",
expectedCMDRegistration = fmt.Sprintf("sudo -E subscription-manager register --auto-attach --username %s --password %s ",
param.Username, param.Password)
expectedCMDUnregistration = "sudo subscription-manager unregister"
expectedCMDUnregistration = "sudo -E subscription-manager unregister"
)

func setup(t *testing.T) (registrator Registrator) {
s, _ := tests.NewSSHServer()
s.CommandToOutput = make(map[string]string)
s.CommandToOutput["sudo subscription-manager version"] = `server type: This system is currently not registered.`
s.CommandToOutput["sudo -E subscription-manager version"] = `server type: This system is currently not registered.`
port, err := s.Start()
if err != nil {
t.Fatalf("Error starting ssh server: %s", err)
Expand Down Expand Up @@ -102,7 +98,7 @@ func TestRedHatRegistratorRegister(t *testing.T) {
commander := provision.GenericSSHCommander{Driver: d}
registrator := NewRedHatRegistrator(commander)

s.CommandToOutput["sudo subscription-manager version"] = `server type: This system is currently not registered.`
s.CommandToOutput["sudo -E subscription-manager version"] = `server type: This system is currently not registered.`
if err := registrator.Register(param); err != nil {
t.Fatal("Distribution should able to register")
} else {
Expand Down Expand Up @@ -130,7 +126,7 @@ func TestRedHatRegistratorUnregister(t *testing.T) {
commander := provision.GenericSSHCommander{Driver: d}
registrator := NewRedHatRegistrator(commander)

s.CommandToOutput["sudo subscription-manager version"] = `server type: RedHat Subscription Management`
s.CommandToOutput["sudo -E subscription-manager version"] = `server type: RedHat Subscription Management`
if err := registrator.Unregister(param); err != nil {
t.Fatal("Distribution should be able to unregister")
} else {
Expand Down
8 changes: 2 additions & 6 deletions pkg/minishift/registration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ import (
)

type RegistrationParameters struct {
Username string
Password string
ProxyUsername string
ProxyPassword string
ProxyServer string
ProxyServerPort string
Username string
Password string
}

// Register host VM
Expand Down
79 changes: 0 additions & 79 deletions pkg/minishift/util/proxy_util.go

This file was deleted.

0 comments on commit c3cb785

Please sign in to comment.