Skip to content

Commit

Permalink
adding help doc
Browse files Browse the repository at this point in the history
Signed-off-by: Raj Das <[email protected]>
  • Loading branch information
imrajdas committed Jul 6, 2021
1 parent bd309a6 commit efdcc4a
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 59 deletions.
11 changes: 0 additions & 11 deletions config.yaml

This file was deleted.

16 changes: 14 additions & 2 deletions pkg/agent/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func ValidateSAPermissions(mode string, kubeconfig *string) {

// Summary display the agent details based on input
func Summary(agent types.Agent, kubeconfig *string) {
fmt.Printf("\n📌 Summary -------------------------- \nAgent Name: %s\nAgent Description: %s\nPlatform Name: %s", agent.AgentName, agent.Description, agent.PlatformName)
fmt.Printf("\n📌 Summary -------------------------- \nAgent Name: %s\nAgent Description: %s\nPlatform Name: %s\n", agent.AgentName, agent.Description, agent.PlatformName)
if ok, _ := k8s.NsExists(agent.Namespace, kubeconfig); ok {
fmt.Println("Namespace: ", agent.Namespace)
} else {
Expand All @@ -178,7 +178,7 @@ func Summary(agent types.Agent, kubeconfig *string) {
fmt.Println("Service Account: ", agent.ServiceAccount, "(new)")
}

fmt.Printf("\nInstallation Mode: %s\n-------------------------------------", agent.Mode)
fmt.Printf("\nInstallation Mode: %s\n-------------------------------------\n", agent.Mode)
}

func ConfirmInstallation() {
Expand All @@ -193,3 +193,15 @@ func ConfirmInstallation() {
os.Exit(1)
}
}

func CreateRandomProject(cred types.Credentials) string {
rand, err := utils.GenerateRandomString(10)
utils.PrintError(err)

projectName := cred.Username + "-" + rand

project, err := apis.CreateProjectRequest(projectName, cred)
utils.PrintError(err)

return project.Data.CreateProject.ID
}
2 changes: 2 additions & 0 deletions pkg/apis/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ import (
type AgentData struct {
Data AgentList `json:"data"`
}

type AgentDetails struct {
AgentName string `json:"cluster_name"`
IsActive bool `json:"is_active"`
IsRegistered bool `json:"is_registered"`
ClusterID string `json:"cluster_id"`
}

type AgentList struct {
GetAgent []AgentDetails `json:"getCluster"`
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/apis/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ type createProjectResponse struct {
Data struct {
CreateProject struct {
Name string `json:"name"`
ID string `json:"id"`
} `json:"createProject"`
} `json:"data"`
}

func CreateProjectRequest(projectName string, cred types.Credentials) error {
func CreateProjectRequest(projectName string, cred types.Credentials) (createProjectResponse, error) {
query := `{"query":"mutation{createProject(projectName: \"` + projectName + `\"){name}}"}`

resp, err := SendRequest(SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, []byte(query))
if err != nil {
return err
return createProjectResponse{}, err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
return createProjectResponse{}, err
}

defer resp.Body.Close()
Expand All @@ -53,13 +54,13 @@ func CreateProjectRequest(projectName string, cred types.Credentials) error {
var project createProjectResponse
err = json.Unmarshal(bodyBytes, &project)
if err != nil {
return err
return createProjectResponse{}, err
}

fmt.Println("project/" + project.Data.CreateProject.Name + " created")
return nil
return project, nil
} else {
return errors.New("Unmatched status code:" + string(bodyBytes))
return createProjectResponse{}, errors.New("Unmatched status code:" + string(bodyBytes))
}
}

Expand Down
20 changes: 17 additions & 3 deletions pkg/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@ import (

// configCmd represents the config command
var ConfigCmd = &cobra.Command{
Use: "config",
Short: "A brief description of your command",
Long: ``,
Use: "config",
Short: `It manages multiple litmusportal accounts within a system.
Examples(s)
#set a new account
litmusctl config set-account --endpoint "" --password "" --username ""
#use an existing account from the config file
litmusctl config use-account --endpoint "" --username ""
#get all accounts in the config file
litmusctl config get-accounts
#view the config file
litmusctl config view
Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
}
80 changes: 65 additions & 15 deletions pkg/cmd/create/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,29 @@ package create

import (
"fmt"

"github.com/litmuschaos/litmusctl/pkg/agent"
"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/k8s"
"github.com/litmuschaos/litmusctl/pkg/types"
"github.com/litmuschaos/litmusctl/pkg/utils"

"os"

"github.com/spf13/cobra"
)

// agentCmd represents the agent command
var agentCmd = &cobra.Command{
Use: "agent",
Short: "Create an external agent.",
Long: `Create an external agent`,
Use: "agent",
Short: `Create an external agent.
Example(s):
#create an agent
litmusctl create agent --agent-name="new-agent" --create-project --non-interactive
#create an agent within a project
litmusctl create agent --agent-name="new-agent" --project-id="d861b650-1549-4574-b2ba-ab754058dd04" --non-interactive
Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
Run: func(cmd *cobra.Command, args []string) {
credentials, err := utils.GetCredentials(cmd)
utils.PrintError(err)
Expand All @@ -43,26 +49,59 @@ var agentCmd = &cobra.Command{
kubeconfig, err := cmd.Flags().GetString("kubeconfig")
utils.PrintError(err)

isCreateProject, err := cmd.Flags().GetBool("create-project")
utils.PrintError(err)

var newAgent types.Agent

if nonInteractive {
newAgent.ProjectId, err = cmd.Flags().GetString("project-id")
utils.PrintError(err)

// if projectid is empty and create a new project is false
if newAgent.ProjectId == "" && !isCreateProject {
fmt.Print("Error: --project-id flag is empty | Suggestion: Apply `litmusctl get projects` to see the list of projects and if the user is not part of any project, then pass --create-project flag to create a new project\n")
os.Exit(1)
}

if newAgent.ProjectId != "" && isCreateProject {
fmt.Println("Both --project-id and --create-project flags can't set together")
os.Exit(1)
}

newAgent.Mode, err = cmd.Flags().GetString("installation-mode")
utils.PrintError(err)

if newAgent.Mode == "" {
fmt.Print("Error: --installation-mode flag is empty")
os.Exit(1)
}

newAgent.AgentName, err = cmd.Flags().GetString("agent-name")
utils.PrintError(err)

if newAgent.AgentName == "" {
fmt.Print("Error: --agent-name flag is empty")
os.Exit(1)
}

newAgent.Description, err = cmd.Flags().GetString("agent-description")
utils.PrintError(err)

newAgent.PlatformName, err = cmd.Flags().GetString("platform-name")
utils.PrintError(err)

if newAgent.PlatformName == "" {
fmt.Print("Error: --platform-name flag is empty")
os.Exit(1)
}

newAgent.ClusterType, err = cmd.Flags().GetString("cluster-type")
utils.PrintError(err)
if newAgent.ClusterType == "" {
fmt.Print("Error: --cluster-type flag is empty")
os.Exit(1)
}

newAgent.Namespace, err = cmd.Flags().GetString("namespace")
utils.PrintError(err)
Expand All @@ -80,6 +119,10 @@ var agentCmd = &cobra.Command{
newAgent.Mode = utils.DefaultMode
}

if isCreateProject {
newAgent.ProjectId = agent.CreateRandomProject(credentials)
}

// Check if user has sufficient permissions based on mode
fmt.Println("\n🏃 Running prerequisites check....")
agent.ValidateSAPermissions(newAgent.Mode, &kubeconfig)
Expand All @@ -102,27 +145,33 @@ var agentCmd = &cobra.Command{
}

} else {

userDetails, err := apis.GetProjectDetails(credentials)
utils.PrintError(err)

// Fetch project id
projectID := agent.GetProjectID(userDetails)
if isCreateProject {
fmt.Println("Creating a random project...")
newAgent.ProjectId = agent.CreateRandomProject(credentials)
} else {
userDetails, err := apis.GetProjectDetails(credentials)
utils.PrintError(err)

// Fetch project id
newAgent.ProjectId = agent.GetProjectID(userDetails)
}

modeType := agent.GetModeType()

// Check if user has sufficient permissions based on mode
fmt.Println("\n🏃 Running prerequisites check....")
agent.ValidateSAPermissions(modeType, &kubeconfig)
newAgent, err = agent.GetAgentDetails(modeType, projectID, credentials, &kubeconfig)
newAgent, err = agent.GetAgentDetails(modeType, newAgent.ProjectId, credentials, &kubeconfig)
utils.PrintError(err)

newAgent.ServiceAccount, newAgent.SAExists = k8s.ValidSA(newAgent.Namespace, &kubeconfig)
newAgent.Mode = modeType
agent.Summary(newAgent, &kubeconfig)
}

agent.ConfirmInstallation()
agent.Summary(newAgent, &kubeconfig)

if !nonInteractive {
agent.ConfirmInstallation()
}

agent, err := apis.ConnectAgent(newAgent, credentials)
Expand Down Expand Up @@ -166,10 +215,11 @@ func init() {

agentCmd.Flags().BoolP("non-interactive", "n", false, "Set it to true for non interactive mode | Note: Always set the boolean flag as --non-interactive=Boolean")
agentCmd.Flags().StringP("kubeconfig", "k", "", "Set to pass kubeconfig file if it is not in the default location ($HOME/.kube/config)")
agentCmd.Flags().Bool("create-project", false, "Set it to --create-project=true for new project creation if the user is not part of any project or --project-id flag is empty | Note: Always set the boolean flag as --create-project=Boolean and this flag can be used in both interactive and non-interactive mode")
agentCmd.Flags().String("project-id", "", "Set the project-id to install agent for the particular project. To see the projects, apply litmusctl get projects")
agentCmd.Flags().String("installation-mode", "cluster", "Set the installation mode for the kind of agent | Supported=cluster/namespace")
agentCmd.Flags().String("agent-name", "", "Set the agent name")
agentCmd.Flags().String("agent-description", "", "Set the agent description")
agentCmd.Flags().String("agent-description", "---", "Set the agent description")
agentCmd.Flags().String("platform-name", "Others", "Set the platform name. Supported- AWS/GKE/Openshift/Rancher/Others")
agentCmd.Flags().String("cluster-type", "external", "Set the cluster-type to external for external agents | Supported=external/internal")
agentCmd.Flags().String("namespace", "litmus", "Set the namespace for the agent installation")
Expand Down
17 changes: 14 additions & 3 deletions pkg/cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ import (

// createCmd represents the create command
var CreateCmd = &cobra.Command{
Use: "create",
Short: "A brief description of your command",
Long: ``,
Use: "create",
Short: `Create resources for LitmusChaos agent plane.
Examples:
#create a project
litmusctl create project --name new-proj
#create an agent
litmusctl create agent --agent-name="new-agent" --create-project --non-interactive
#create an agent within a project
litmusctl create agent --agent-name="new-agent" --project-id="d861b650-1549-4574-b2ba-ab754058dd04" --non-interactive
Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
}
25 changes: 10 additions & 15 deletions pkg/cmd/create/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ import (

// projectCmd represents the project command
var projectCmd = &cobra.Command{
Use: "project",
Short: "Create a project",
Long: `Create a project`,
Use: "project",
Short: `Create a project
Example:
#create a project
litmusctl create project --name new-proj
Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
Run: func(cmd *cobra.Command, args []string) {
credentials, err := utils.GetCredentials(cmd)
utils.PrintError(err)
Expand All @@ -46,22 +51,12 @@ var projectCmd = &cobra.Command{
}
}

err = apis.CreateProjectRequest(projectName, credentials)
_, err = apis.CreateProjectRequest(projectName, credentials)
utils.PrintError(err)

},
}

func init() {
CreateCmd.AddCommand(projectCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// projectCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
projectCmd.Flags().String("name", "", "Help message for toggle")
projectCmd.Flags().String("name", "", "Set the project name to create it")
}
13 changes: 10 additions & 3 deletions pkg/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ import (

// getCmd represents the get command
var GetCmd = &cobra.Command{
Use: "get",
Short: "A brief description of your command",
Long: ``,
Use: "get",
Short: `Examples:
#get list of projects accessed by the user
litmusctl get projects
#get list of agents within the project
litmusctl get agents --project-id=""
Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
}
2 changes: 1 addition & 1 deletion pkg/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// versionCmd represents the version command
var VersionCmd = &cobra.Command{
Use: "version",
Short: "A brief description of your command",
Short: "Displays the version of litmusctl",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Litmusctl version: ", os.Getenv("CLIVersion"))
Expand Down
Loading

0 comments on commit efdcc4a

Please sign in to comment.