forked from minishift/minishift
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue minishift#126 Ability to create multiple instances of Minishift
Introducing --profile to minishift start and profile command Signed-off-by: Lalatendu Mohanty <lmohanty@redhat.com>
- Loading branch information
1 parent
5cfcaf0
commit cdf8ac6
Showing
24 changed files
with
904 additions
and
94 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright (C) 2017 Red Hat, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package profile | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
emptyProfileMessage = "You must provide the profile name. Run `minishift profile list` to view profiles" | ||
extraArgumentMessage = "You have provided more arguments than required. You must provide a single profile name" | ||
) | ||
|
||
var ProfileCmd = &cobra.Command{ | ||
Use: "profile SUBCOMMAND [flags]", | ||
Aliases: []string{"instance", "profiles"}, | ||
Short: "Manages Minishift profiles.", | ||
Long: "Allows you to create and manage multiple Minishift instances. Use the sub-commands to list, activate or delete existing profiles.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.Help() | ||
}, | ||
} |
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,114 @@ | ||
/* | ||
Copyright (C) 2017 Red Hat, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package profile | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/docker/machine/libmachine" | ||
cmdUtil "github.com/minishift/minishift/cmd/minishift/cmd/util" | ||
"github.com/minishift/minishift/pkg/minikube/cluster" | ||
"github.com/minishift/minishift/pkg/minikube/constants" | ||
profileActions "github.com/minishift/minishift/pkg/minishift/profile" | ||
pkgUtil "github.com/minishift/minishift/pkg/util" | ||
"github.com/minishift/minishift/pkg/util/os/atexit" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
profileDeleteCmd = &cobra.Command{ | ||
Use: "delete PROFILE_NAME", | ||
Short: "delete profiles.", | ||
Long: "deletes an existing profile.", | ||
Run: runProfileDelete, | ||
} | ||
force bool | ||
) | ||
|
||
func runProfileDelete(cmd *cobra.Command, args []string) { | ||
if len(args) == 0 { | ||
atexit.ExitWithMessage(1, emptyProfileMessage) | ||
} else if len(args) > 1 { | ||
atexit.ExitWithMessage(1, extraArgumentMessage) | ||
} | ||
|
||
profileName := args[0] | ||
var defaultProfile bool | ||
|
||
if !cmdUtil.IsValidProfile(profileName) { | ||
atexit.ExitWithMessage(1, fmt.Sprintf("Error: '%s' is not a valid profile", profileName)) | ||
} | ||
|
||
profileActions.UpdateProfileConstants(profileName) | ||
profileBaseDir := constants.Minipath | ||
|
||
if profileName == constants.DefaultProfileName { | ||
defaultProfile = true | ||
} | ||
if defaultProfile { | ||
atexit.ExitWithMessage(1, fmt.Sprintf("Default profile '%s' can not be deleted", profileName)) | ||
} | ||
|
||
if !force { | ||
var hasConfirmed bool | ||
if profileActions.GetActiveProfile() == profileName { | ||
hasConfirmed = pkgUtil.AskForConfirmation("You are deleting the active profile. It will remove the VM and all related artifacts.") | ||
} else { | ||
hasConfirmed = pkgUtil.AskForConfirmation("Will remove the VM and all the related artifacts.") | ||
} | ||
if !hasConfirmed { | ||
atexit.Exit(1) | ||
} | ||
} | ||
|
||
api := libmachine.NewClient(profileBaseDir, constants.MakeMiniPath("certs")) | ||
defer api.Close() | ||
|
||
exists := cmdUtil.VMExists(api, constants.MachineName) | ||
if !exists { | ||
fmt.Println(fmt.Sprintf("VM for profile '%s' does not exist", profileName)) | ||
} else { | ||
cmdUtil.UnregisterHost(api, false) | ||
err := cluster.DeleteHost(api) | ||
if err != nil { | ||
fmt.Println(fmt.Sprintf("Error deleting '%s': %v", constants.MakeMiniPath("machines"), err.Error())) | ||
} | ||
} | ||
|
||
err := os.RemoveAll(profileBaseDir) | ||
if err != nil { | ||
atexit.ExitWithMessage(1, fmt.Sprintf("Error deleting '%s': %v", profileBaseDir, err.Error())) | ||
} else { | ||
fmt.Println("Deleted: ", profileBaseDir) | ||
} | ||
fmt.Println(fmt.Sprintf("Profile '%s' deleted successfully", profileName)) | ||
|
||
// When active profile is deleted, reset the active profile to default profile | ||
if profileActions.GetActiveProfile() == profileName { | ||
fmt.Println(fmt.Sprintf("Switching to default profile '%s' as the active profile.", constants.DefaultProfileName)) | ||
err = profileActions.SetActiveProfile(constants.DefaultProfileName) | ||
if err != nil { | ||
atexit.ExitWithMessage(1, fmt.Sprintf("Error setting default profile '%s' as the active profile.", constants.DefaultProfileName)) | ||
} | ||
} | ||
} | ||
|
||
func init() { | ||
profileDeleteCmd.Flags().BoolVar(&force, "force", false, "Forces the deletion of profile and related files in MINISHIFT_HOME.") | ||
ProfileCmd.AddCommand(profileDeleteCmd) | ||
} |
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,57 @@ | ||
/* | ||
Copyright (C) 2017 Red Hat, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package profile | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"text/tabwriter" | ||
|
||
cmdUtil "github.com/minishift/minishift/cmd/minishift/cmd/util" | ||
profileActions "github.com/minishift/minishift/pkg/minishift/profile" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var profileListCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "Lists profiles.", | ||
Long: "Lists the existing profiles.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
profiles := profileActions.GetProfileList() | ||
displayProfiles(profiles) | ||
}, | ||
} | ||
|
||
func displayProfiles(profiles []string) { | ||
display := new(tabwriter.Writer) | ||
display.Init(os.Stdout, 0, 8, 0, '\t', 0) | ||
|
||
activeProfile := profileActions.GetActiveProfile() | ||
for _, profile := range profiles { | ||
vmStatus := cmdUtil.GetVmStatus(profile) | ||
if profile == activeProfile { | ||
fmt.Fprintln(display, fmt.Sprintf("- %s\t%s\t(active)", profile, vmStatus)) | ||
} else { | ||
fmt.Fprintln(display, fmt.Sprintf("- %s\t%s", profile, vmStatus)) | ||
} | ||
} | ||
display.Flush() | ||
} | ||
|
||
func init() { | ||
ProfileCmd.AddCommand(profileListCmd) | ||
} |
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,68 @@ | ||
/* | ||
Copyright (C) 2017 Red Hat, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package profile | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golang/glog" | ||
cmdUtil "github.com/minishift/minishift/cmd/minishift/cmd/util" | ||
profileActions "github.com/minishift/minishift/pkg/minishift/profile" | ||
"github.com/minishift/minishift/pkg/util/os/atexit" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
unableToSetOcContextErrorMessage = "Make sure the profile is in running state or restart if the problem persists." | ||
) | ||
|
||
var profileSetCmd = &cobra.Command{ | ||
Use: "set PROFILE_NAME", | ||
Short: "Sets the active profile for Minishift.", | ||
Long: "Sets the active profile for Minishift. After you set the profile, all commands will use the specified profile by default.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) == 0 { | ||
atexit.ExitWithMessage(1, emptyProfileMessage) | ||
} else if len(args) > 1 { | ||
atexit.ExitWithMessage(1, extraArgumentMessage) | ||
} | ||
|
||
profileName := args[0] | ||
|
||
if !cmdUtil.IsValidProfile(profileName) { | ||
atexit.ExitWithMessage(1, fmt.Sprintf("Error: '%s' is not a valid profile", profileName)) | ||
} | ||
|
||
err := profileActions.SetActiveProfile(profileName) | ||
if err != nil { | ||
atexit.ExitWithMessage(1, err.Error()) | ||
} else { | ||
fmt.Println(fmt.Sprintf("Profile '%s' set as active profile", profileName)) | ||
} | ||
err = cmdUtil.SetOcContext(profileName) | ||
if err != nil { | ||
if glog.V(2) { | ||
fmt.Println(fmt.Sprintf("%s", err.Error())) | ||
} | ||
fmt.Println(fmt.Sprintf("oc cli context could not changed for '%s'. %s", profileName, unableToSetOcContextErrorMessage)) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
ProfileCmd.AddCommand(profileSetCmd) | ||
} |
Oops, something went wrong.