mirror of
https://github.com/rancher/cli.git
synced 2026-02-05 09:48:36 +01:00
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/rancher/cli/config"
|
||||
"github.com/rancher/norman/clientbase"
|
||||
ntypes "github.com/rancher/norman/types"
|
||||
clusterClient "github.com/rancher/types/client/cluster/v3"
|
||||
managementClient "github.com/rancher/types/client/management/v3"
|
||||
projectClient "github.com/rancher/types/client/project/v3"
|
||||
@@ -135,26 +134,6 @@ func (mc *MasterClient) newProjectClient() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mc *MasterClient) ByID(resource *ntypes.Resource, respObject interface{}) error {
|
||||
if _, ok := mc.ManagementClient.APIBaseClient.Types[resource.Type]; ok {
|
||||
err := mc.ManagementClient.ByID(resource.Type, resource.ID, &respObject)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if _, ok := mc.ProjectClient.APIBaseClient.Types[resource.Type]; ok {
|
||||
err := mc.ProjectClient.ByID(resource.Type, resource.ID, &respObject)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if _, ok := mc.ClusterClient.APIBaseClient.Types[resource.Type]; ok {
|
||||
err := mc.ClusterClient.ByID(resource.Type, resource.ID, &respObject)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return errors.New("unknown resource type")
|
||||
}
|
||||
|
||||
func createClientOpts(config *config.ServerConfig) *clientbase.ClientOpts {
|
||||
serverURL := config.URL
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ import (
|
||||
"github.com/rancher/norman/clientbase"
|
||||
ntypes "github.com/rancher/norman/types"
|
||||
managementClient "github.com/rancher/types/client/management/v3"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -295,7 +294,6 @@ func Lookup(c *cliclient.MasterClient, name string, types ...string) (*ntypes.Re
|
||||
for _, schemaType := range types {
|
||||
rt, err := GetResourceType(c, schemaType)
|
||||
if err != nil {
|
||||
logrus.Debugf("Error GetResourceType: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
var schemaClient clientbase.APIBaseClientInterface
|
||||
@@ -320,7 +318,6 @@ func Lookup(c *cliclient.MasterClient, name string, types ...string) (*ntypes.Re
|
||||
var resource ntypes.Resource
|
||||
|
||||
if err := schemaClient.ByID(schemaType, name, &resource); !clientbase.IsNotFound(err) && err != nil {
|
||||
logrus.Debugf("Error schemaClient.ByID: %v", err)
|
||||
return nil, err
|
||||
} else if err == nil && resource.ID == name {
|
||||
return &resource, nil
|
||||
@@ -336,8 +333,7 @@ func Lookup(c *cliclient.MasterClient, name string, types ...string) (*ntypes.Re
|
||||
},
|
||||
}
|
||||
|
||||
if err := schemaClient.List(schemaType, listOpts, &collection); !clientbase.IsNotFound(err) && err != nil {
|
||||
logrus.Debugf("Error schemaClient.List: %v", err)
|
||||
if err := schemaClient.List(schemaType, listOpts, &collection); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package cmd
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -70,9 +71,23 @@ func inspectResources(ctx *cli.Context) error {
|
||||
}
|
||||
mapResource := map[string]interface{}{}
|
||||
|
||||
err = c.ByID(resource, &mapResource)
|
||||
if err != nil {
|
||||
return err
|
||||
if _, ok := c.ManagementClient.APIBaseClient.Types[resource.Type]; ok {
|
||||
err = c.ManagementClient.ByID(resource.Type, resource.ID, &mapResource)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if _, ok := c.ProjectClient.APIBaseClient.Types[resource.Type]; ok {
|
||||
err = c.ProjectClient.ByID(resource.Type, resource.ID, &mapResource)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if _, ok := c.ClusterClient.APIBaseClient.Types[resource.Type]; ok {
|
||||
err = c.ClusterClient.ByID(resource.Type, resource.ID, &mapResource)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return errors.New("unknown resource type")
|
||||
}
|
||||
|
||||
if !ctx.Bool("links") {
|
||||
|
||||
40
cmd/login.go
40
cmd/login.go
@@ -57,10 +57,6 @@ func LoginCommand() cli.Command {
|
||||
Name: "name",
|
||||
Usage: "Name of the Server",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "skip-verify",
|
||||
Usage: "Skip verification of the CACerts presented by the Server",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -123,7 +119,7 @@ func loginSetup(ctx *cli.Context) error {
|
||||
if _, ok := err.(*url.Error); ok && strings.Contains(err.Error(), "certificate signed by unknown authority") {
|
||||
// no cert was provided and it's most likely a self signed cert if
|
||||
// we get here so grab the cacert and see if the user accepts the server
|
||||
c, err = getCertFromServer(ctx, serverConfig)
|
||||
c, err = getCertFromServer(serverConfig)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
@@ -169,34 +165,16 @@ func getProjectContext(ctx *cli.Context, c *cliclient.MasterClient) (string, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
projDataLen := len(projectCollection.Data)
|
||||
|
||||
if projDataLen == 0 {
|
||||
if len(projectCollection.Data) == 0 {
|
||||
logrus.Warn("No projects found, context could not be set. Please create a project and run `rancher login` again.")
|
||||
return "", nil
|
||||
}
|
||||
|
||||
if projDataLen == 1 {
|
||||
if len(projectCollection.Data) == 1 {
|
||||
logrus.Infof("Only 1 project available: %s", projectCollection.Data[0].Name)
|
||||
return projectCollection.Data[0].ID, nil
|
||||
}
|
||||
|
||||
if projDataLen == 2 {
|
||||
var sysProj bool
|
||||
var defaultID string
|
||||
for _, proj := range projectCollection.Data {
|
||||
if proj.Name == "Default" {
|
||||
defaultID = proj.ID
|
||||
}
|
||||
if proj.Name == "System" {
|
||||
sysProj = true
|
||||
}
|
||||
if sysProj && defaultID != "" {
|
||||
return defaultID, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clusterNames, err := getClusterNames(ctx, c)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -254,7 +232,7 @@ func getProjectContext(ctx *cli.Context, c *cliclient.MasterClient) (string, err
|
||||
return projectCollection.Data[selection].ID, nil
|
||||
}
|
||||
|
||||
func getCertFromServer(ctx *cli.Context, cf *config.ServerConfig) (*cliclient.MasterClient, error) {
|
||||
func getCertFromServer(cf *config.ServerConfig) (*cliclient.MasterClient, error) {
|
||||
req, err := http.NewRequest("GET", cf.URL+"/v3/settings/cacerts", nil)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
@@ -296,14 +274,12 @@ func getCertFromServer(ctx *cli.Context, cf *config.ServerConfig) (*cliclient.Ma
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !ctx.Bool("skip-verify") {
|
||||
if ok := verifyUserAcceptsCert(serverCerts, cf.URL); !ok {
|
||||
return nil, errors.New("CACert of server was not accepted, unable to login")
|
||||
}
|
||||
if ok := verifyUserAcceptsCert(serverCerts, cf.URL); ok {
|
||||
cf.CACerts = cert
|
||||
} else {
|
||||
return nil, errors.New("CACert of server was not accepted, unable to login")
|
||||
}
|
||||
|
||||
cf.CACerts = cert
|
||||
|
||||
return cliclient.NewManagementClient(cf)
|
||||
}
|
||||
|
||||
|
||||
89
cmd/wait.go
89
cmd/wait.go
@@ -1,89 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
ntypes "github.com/rancher/norman/types"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var (
|
||||
waitTypes = []string{"cluster", "app", "project"}
|
||||
)
|
||||
|
||||
func WaitCommand() cli.Command {
|
||||
return cli.Command{
|
||||
Name: "wait",
|
||||
Usage: "Wait for resources " + strings.Join(waitTypes, ", "),
|
||||
ArgsUsage: "[ID/NAME]",
|
||||
Action: defaultAction(wait),
|
||||
Flags: []cli.Flag{
|
||||
cli.IntFlag{
|
||||
Name: "timeout",
|
||||
Usage: "Time in seconds to wait for a resource",
|
||||
Value: 120,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func wait(ctx *cli.Context) error {
|
||||
if ctx.NArg() == 0 {
|
||||
return cli.ShowCommandHelp(ctx, "wait")
|
||||
}
|
||||
|
||||
c, err := GetClient(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resource, err := Lookup(c, ctx.Args().First(), waitTypes...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mapResource := map[string]interface{}{}
|
||||
timeout := time.After(time.Duration(ctx.Int("timeout")) * time.Second)
|
||||
every := time.Tick(10 * time.Second)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-timeout:
|
||||
return fmt.Errorf("Timeout reached %s:%s failed: %s", resource.Type, resource.ID, mapResource["transitioningMessage"])
|
||||
case <-every:
|
||||
err = c.ByID(resource, &mapResource)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ok, err := checkDone(resource, mapResource)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ok {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func checkDone(resource *ntypes.Resource, data map[string]interface{}) (bool, error) {
|
||||
transitioning := fmt.Sprint(data["transitioning"])
|
||||
logrus.Debugf("%s:%s transitioning=%s state=%v", resource.Type, resource.ID, transitioning,
|
||||
data["state"])
|
||||
|
||||
switch transitioning {
|
||||
case "yes":
|
||||
return false, nil
|
||||
case "error":
|
||||
if data["state"] == "provisioning" {
|
||||
break
|
||||
}
|
||||
return false, fmt.Errorf("%s:%s failed: %s", resource.Type, resource.ID, data["transitioningMessage"])
|
||||
}
|
||||
|
||||
return data["state"] == "active", nil
|
||||
}
|
||||
Reference in New Issue
Block a user