-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add e2 command line tool #339
Draft
danielledeleo
wants to merge
5
commits into
main
Choose a base branch
from
jagger/e2-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# e2 | ||
|
||
A simple CLI tool to deploy and interact with `e2core`. It is released in lockstep with E2 Core. |
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,287 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/suborbital/e2core/e2/templater" | ||
"github.com/suborbital/e2core/e2/util" | ||
"github.com/suborbital/e2core/e2core/release" | ||
) | ||
|
||
type deployData struct { | ||
E2CoreTag string | ||
EnvToken string | ||
BuilderDomain string | ||
StorageClassName string | ||
} | ||
|
||
const defaultRepo string = "suborbital/e2core" | ||
const defaultBranch = "v" + release.E2CoreServerDotVersion | ||
|
||
// DeployCommand returns the SE2 deploy command. | ||
func DeployCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "deploy", | ||
Short: "Deploy E2 Core to Kubernetes", | ||
Long: "Deploy E2 Core to Kubernetes", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
shouldReset := cmd.Flags().Changed(resetFlag) | ||
repo, _ := cmd.Flags().GetString(repoFlag) | ||
branch, _ := cmd.Flags().GetString(branchFlag) | ||
forceUpdateTemplates, _ := cmd.Flags().GetBool(updateTemplatesFlag) | ||
|
||
if err := introAcceptance(); err != nil { | ||
return err | ||
} | ||
|
||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return errors.Wrap(err, "🚫 failed to Getwd") | ||
} | ||
|
||
workingDirectory, err := filepath.Abs(cwd) | ||
if err != nil { | ||
return errors.Wrap(err, "🚫 failed to get absolute path") | ||
} | ||
|
||
if forceUpdateTemplates { | ||
util.LogInfo(fmt.Sprintf("updating (forced) templates from %s (%s)", repo, branch)) | ||
|
||
_, err = templater.UpdateTemplates(repo, branch) | ||
if err != nil { | ||
return errors.Wrap(err, "🚫 failed to UpdateTemplates") | ||
} | ||
} | ||
|
||
templatesPath, err := templater.TemplatesExist(repo, branch) | ||
// Fetch templates if they don't exist | ||
if err != nil { | ||
util.LogInfo(fmt.Sprintf("updating templates from %s (%s)", repo, branch)) | ||
templatesPath, err = templater.UpdateTemplates(repo, branch) | ||
|
||
if err != nil { | ||
return errors.Wrap(err, "🚫 failed to UpdateTemplates") | ||
} | ||
} | ||
|
||
// if the --reset flag was passed or there's no existing manifests | ||
// then we need to 'build the world' from scratch. | ||
if shouldReset || !manifestsExist(workingDirectory) { | ||
util.LogStart("preparing deployment") | ||
|
||
// if there are any existing deployment manifests sitting around, let's replace them. | ||
if err := removeExistingManifests(workingDirectory); err != nil { | ||
return errors.Wrap(err, "🚫 failed to removeExistingManifests") | ||
} | ||
|
||
_, err = util.Mkdir(workingDirectory, ".suborbital") | ||
if err != nil { | ||
return errors.Wrap(err, "🚫 failed to Mkdir") | ||
} | ||
|
||
envToken, err := getEnvToken() | ||
if err != nil { | ||
return errors.Wrap(err, "🚫 failed to getEnvToken") | ||
} | ||
|
||
data := deployData{ | ||
E2CoreTag: "v" + release.E2CoreServerDotVersion, | ||
EnvToken: envToken, | ||
} | ||
|
||
templateName := "e2core-k8s" | ||
|
||
data.StorageClassName, err = getStorageClass() | ||
if err != nil { | ||
return errors.Wrap(err, "🚫 failed to getStorageClass") | ||
} | ||
|
||
if err := templater.ExecTmplDir(workingDirectory, "", templatesPath, templateName, data); err != nil { | ||
return errors.Wrap(err, "🚫 failed to ExecTmplDir") | ||
} | ||
|
||
util.LogDone("ready to start installation") | ||
} | ||
|
||
dryRun, _ := cmd.Flags().GetBool(dryRunFlag) | ||
|
||
if dryRun { | ||
util.LogInfo("aborting due to dry-run, manifest files remain in " + workingDirectory) | ||
return nil | ||
} | ||
|
||
util.LogStart("installing...") | ||
|
||
if _, err := util.Command.Run("kubectl apply -f https://github.com/kedacore/keda/releases/download/v2.4.0/keda-2.4.0.yaml"); err != nil { | ||
return errors.Wrap(err, "🚫 failed to install KEDA") | ||
} | ||
|
||
// we don't care if this fails, so don't check error. | ||
util.Command.Run("kubectl create ns suborbital") | ||
|
||
if err := createConfigMap(cwd); err != nil { | ||
return errors.Wrap(err, "🚫 failed to createConfigMap") | ||
} | ||
|
||
if _, err := util.Command.Run("kubectl apply -f .suborbital/"); err != nil { | ||
return errors.Wrap(err, "🚫 failed to kubectl apply") | ||
} | ||
|
||
util.LogDone("installation complete!") | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().String(repoFlag, defaultRepo, "git repo to download templates from") | ||
cmd.Flags().String(branchFlag, defaultBranch, "git branch to download templates from") | ||
cmd.Flags().Bool(dryRunFlag, false, "prepare the deployment in the .suborbital directory, but do not apply it") | ||
cmd.Flags().Bool(resetFlag, false, "reset the deployment to default (replaces Kubernetes manifests)") | ||
cmd.Flags().Bool(updateTemplatesFlag, false, "forces templates to be updated") | ||
|
||
return cmd | ||
} | ||
|
||
// TODO: update this | ||
func introAcceptance() error { | ||
fmt.Print(` | ||
Suborbital Extension Engine (SE2) Installer | ||
|
||
BEFORE YOU CONTINUE: | ||
- You must first run "subo se2 create token <email>" to get an environment token | ||
|
||
- You must have kubectl installed in PATH, and it must be connected to the cluster you'd like to use | ||
|
||
- You must be able to set up DNS records for the builder service after this installation completes | ||
- Choose the DNS name you'd like to use before continuing, e.g. builder.acmeco.com | ||
|
||
- Subo will attempt to determine the default storage class for your Kubernetes cluster, | ||
but if is unable to do so you will need to provide one | ||
- See the SE2 documentation for more details | ||
|
||
- Subo will install the KEDA autoscaler into your cluster. It will not affect any existing deployments. | ||
|
||
Are you ready to continue? (y/N): `) | ||
|
||
answer, err := util.ReadStdinString() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to ReadStdinString") | ||
} | ||
|
||
if !strings.EqualFold(answer, "y") { | ||
return errors.New("aborting") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// getEnvToken gets the environment token from stdin. | ||
func getEnvToken() (string, error) { | ||
existing, err := util.ReadEnvironmentToken() | ||
if err == nil { | ||
util.LogInfo("using cached environment token") | ||
return existing, nil | ||
} | ||
|
||
fmt.Print("Enter your environment token: ") | ||
token, err := util.ReadStdinString() | ||
|
||
if err != nil { | ||
return "", errors.Wrap(err, "failed to ReadStdinString") | ||
} | ||
|
||
if len(token) != 32 { | ||
return "", errors.New("token must be 32 characters in length") | ||
} | ||
|
||
if err := util.WriteEnvironmentToken(token); err != nil { | ||
util.LogWarn(err.Error()) | ||
return token, nil | ||
|
||
} else { | ||
util.LogInfo("saved environment token to cache") | ||
} | ||
|
||
return token, nil | ||
} | ||
|
||
// getStorageClass gets the storage class to use. | ||
func getStorageClass() (string, error) { | ||
defaultClass, err := detectStorageClass() | ||
if err != nil { | ||
// that's fine, continue. | ||
fmt.Println("failed to automatically detect Kubernetes storage class:", err.Error()) | ||
} else if defaultClass != "" { | ||
fmt.Println("using default storage class: ", defaultClass) | ||
return defaultClass, nil | ||
} | ||
|
||
fmt.Print("Enter the Kubernetes storage class to use: ") | ||
storageClass, err := util.ReadStdinString() | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to ReadStdinString") | ||
} | ||
|
||
if len(storageClass) == 0 { | ||
return "", errors.New("storage class must not be empty") | ||
} | ||
|
||
return storageClass, nil | ||
} | ||
|
||
func detectStorageClass() (string, error) { | ||
output, err := util.Command.Run("kubectl get storageclass --output=name") | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to get default storageclass") | ||
} | ||
|
||
// output will look like: storageclass.storage.k8s.io/do-block-storage | ||
// so split on the / and return the last part. | ||
|
||
outputParts := strings.Split(output, "/") | ||
if len(outputParts) != 2 { | ||
return "", errors.New("could not automatically determine storage class") | ||
} | ||
|
||
return outputParts[1], nil | ||
} | ||
|
||
func createConfigMap(cwd string) error { | ||
configFilepath := filepath.Join(cwd, "config", "se2-config.yaml") | ||
|
||
_, err := os.Stat(configFilepath) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to Stat se2-config.yaml") | ||
} | ||
|
||
if _, err := util.Command.Run(fmt.Sprintf("kubectl create configmap se2-config --from-file=se2-config.yaml=%s -n suborbital", configFilepath)); err != nil { | ||
return errors.Wrap(err, "failed to create configmap (you may need to run `kubectl delete configmap se2-config -n suborbital`)") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func manifestsExist(workingDirectory string) bool { | ||
if _, err := os.Stat(filepath.Join(workingDirectory, ".suborbital")); err == nil { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func removeExistingManifests(workingDirectory string) error { | ||
// start with a clean slate. | ||
if _, err := os.Stat(filepath.Join(workingDirectory, ".suborbital")); err == nil { | ||
if err := os.RemoveAll(filepath.Join(workingDirectory, ".suborbital")); err != nil { | ||
return errors.Wrap(err, "failed to RemoveAll .suborbital") | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,9 @@ | ||
package command | ||
|
||
const ( | ||
branchFlag = "branch" | ||
repoFlag = "repo" | ||
updateTemplatesFlag = "update-templates" | ||
dryRunFlag = "dryrun" | ||
resetFlag = "reset" | ||
) |
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,27 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/suborbital/e2core/e2/util" | ||
) | ||
|
||
func StatusCommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "status", | ||
Short: "Get K8s deployment status", | ||
Long: "Get Kubernetes pods and service status for your E2 Core deployment", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
util.LogInfo("Pods:") | ||
util.Command.Run("kubectl get pods -n suborbital") | ||
|
||
fmt.Println() | ||
util.LogInfo("Services:") | ||
util.Command.Run("kubectl get svc -n suborbital") | ||
|
||
return nil | ||
}, | ||
} | ||
} |
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,34 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/suborbital/e2core/e2/command" | ||
"github.com/suborbital/e2core/e2/util" | ||
"github.com/suborbital/e2core/e2core/release" | ||
) | ||
|
||
func main() { | ||
cmd := &cobra.Command{ | ||
Use: "e2", | ||
Short: "E2 Core CLI", | ||
Version: release.E2CoreServerDotVersion, | ||
Long: `e2 is a simple deployment tool to manage your E2 Core Kubernetes deployments.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cmd.Help() | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.SetVersionTemplate("E2 Core deployment CLI v{{.Version}}\n") | ||
|
||
cmd.AddCommand(command.DeployCommand()) | ||
cmd.AddCommand(command.StatusCommand()) | ||
|
||
if err := cmd.Execute(); err != nil { | ||
util.LogFail(err.Error()) | ||
os.Exit(1) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also try starting up the kubectl proxy and pinging the e2core health endpoint to make sure it's actually started and serving?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'd just pull metadata from Kubernetes itself to ensure the services are healthy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll make some changes to the templates to support this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suborbital/templates#20