Skip to content

Commit

Permalink
fix: improve jira and vault commands
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveRuble committed Aug 25, 2021
1 parent 9257d0d commit 37d10c8
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 49 deletions.
83 changes: 66 additions & 17 deletions cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/naveego/bosun/pkg/command"
"github.com/naveego/bosun/pkg/semver"
"github.com/naveego/bosun/pkg/util"
"github.com/naveego/bosun/pkg/util/stringsn"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -174,7 +175,6 @@ var releaseShowPreviousCmd = addCommand(releaseCmd, &cobra.Command{
},
})


var releaseDotCmd = addCommand(releaseCmd, &cobra.Command{
Use: "dot",
Short: "Prints a dot diagram of the release.",
Expand Down Expand Up @@ -348,6 +348,59 @@ var releaseAddCmd = addCommand(releaseCmd, &cobra.Command{
cmd.Flags().String(ArgReleaseAddBump, "none", "The version bump to apply to the app.")
}, withFilteringFlags)

var releaseReloadCmd = addCommand(releaseCmd, &cobra.Command{
Use: "reload {stable|unstable} [apps...]",
Args: cobra.MinimumNArgs(1),
Short: "Reloads an app (or all apps) into a release from the location declared in the manifest.",
RunE: func(cmd *cobra.Command, args []string) error {
b := MustGetBosun()

p, err := b.GetCurrentPlatform()
if err != nil {
return err
}

r := mustGetRelease(p, args[0], bosun.SlotUnstable, bosun.SlotStable)

requestedApps := args[1:]

ctx := b.NewContext()

for appName, appMetadata := range r.AppMetadata {

if len(requestedApps) > 0 && !stringsn.Contains(appName, requestedApps) {
ctx.Log().Info("Skipping app %s because it wasn't requested.", appName)
continue
}
ctx.Log().Info("Reloading app %s.", appName)

app, appErr := b.ProvideApp(bosun.AppProviderRequest{
Name: appName,
Branch: appMetadata.Branch,
})

if appErr != nil {
ctx.Log().WithError(err).Warnf("Couldn't provide app %s from branch %s", appName, appMetadata.Branch)
}

manifest, appErr := app.GetManifest(ctx)
if appErr != nil {
ctx.Log().WithError(err).Warnf("Couldn't get manifest from app %s from branch %s", appName, appMetadata.Branch)
}

appErr = r.AddOrReplaceApp(manifest, false)
if appErr != nil {
ctx.Log().WithError(err).Warnf("Couldn't add app to release for app %s from branch %s", appName, appMetadata.Branch)
}
}

err = p.Save(b.NewContext())
return err
},
}, func(cmd *cobra.Command) {
cmd.Flags().String(ArgReleaseAddBranch, "", "The branch to add the app from (defaults to the branch pattern for the slot).")
cmd.Flags().String(ArgReleaseAddBump, "none", "The version bump to apply to the app.")
}, withFilteringFlags)

const (
ArgReleaseAddBranch = "branch"
Expand Down Expand Up @@ -588,8 +641,6 @@ only those apps will be deployed. Otherwise, all apps in the release will be dep
withFilteringFlags,
withValueSetFlags)



var releaseUpdateCmd = addCommand(releaseCmd, &cobra.Command{
Use: "update {stable|unstable} [apps...]",
Short: "Updates the release with the correct values from the apps in it.",
Expand Down Expand Up @@ -666,29 +717,28 @@ var releaseChangelogCmd = addCommand(releaseCmd, &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {

return errors.New("not implemented")
/* viper.BindPFlags(cmd.Flags())
/* viper.BindPFlags(cmd.Flags())
b := MustGetBosun()
ctx := b.NewContext()
b := MustGetBosun()
ctx := b.NewContext()
p, err := b.GetCurrentPlatform()
if err != nil {
return err
}
p, err := b.GetCurrentPlatform()
if err != nil {
return err
}
err = p.CommitCurrentRelease(ctx)
if err != nil {
return err
}
err = p.CommitCurrentRelease(ctx)
if err != nil {
return err
}
return nil*/
return nil*/
},
}, withFilteringFlags)

const ArgReleaseSkipValidate = "skip-validation"
const ArgReleaseRecycle = "recycle"


func diffStrings(a, b string) []difflib.DiffRecord {
left := strings.Split(a, "\n")
right := strings.Split(b, "\n")
Expand All @@ -706,4 +756,3 @@ func renderDiff(diff difflib.DiffRecord) string {
}
panic(fmt.Sprintf("invalid delta %v", diff.Delta))
}

61 changes: 56 additions & 5 deletions cmd/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import (
"github.com/fatih/color"
"github.com/google/uuid"
"github.com/naveego/bosun/pkg/core"
"github.com/naveego/bosun/pkg/kube/kubeclient"
"github.com/naveego/bosun/pkg/templating"
"github.com/naveego/bosun/pkg/vault"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os/user"
"strings"
"time"
Expand Down Expand Up @@ -228,7 +230,6 @@ var vaultUnsealCmd = &cobra.Command{
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {


namespace := "default"
if len(args) > 0 {
namespace = args[0]
Expand All @@ -249,7 +250,6 @@ var vaultUnsealCmd = &cobra.Command{

core.Log.Infof("Unsealing vault in namespace %q", namespace)


initializer := vault.VaultInitializer{
Client: vaultClient,
VaultNamespace: namespace,
Expand All @@ -268,7 +268,6 @@ var vaultInstallJoseCmd = addCommand(vaultCmd, &cobra.Command{
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {


namespace := "default"
if len(args) > 0 {
namespace = args[0]
Expand All @@ -289,7 +288,6 @@ var vaultInstallJoseCmd = addCommand(vaultCmd, &cobra.Command{

core.Log.Infof("Installing jose in namespace %q", namespace)


initializer := vault.VaultInitializer{
Client: vaultClient,
VaultNamespace: namespace,
Expand Down Expand Up @@ -405,6 +403,57 @@ var vaultJWTCmd = &cobra.Command{
},
}

var vaultK8sRootTokenCmd = addCommand(vaultCmd, &cobra.Command{
Use: "get-root-token-from-k8s",
Short: "Tries to pull the root token from the conventional location in k8s. This will not return an error if it fails.",
Long: ``,
Example: "vault init-dev",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {

err := func() error {

cluster := viper.GetString(ArgGlobalCluster)

k8sClient, err := kubeclient.GetKubeClientWithContext("", cluster)

if err != nil {
return err
}

namespace := viper.GetString(ArgVaultNamespace)

secret, err := k8sClient.CoreV1().Secrets(namespace).Get("vault-root-token", v1.GetOptions{})
if err != nil {
return errors.Wrap(err, "couldn't get vault-root-token secret")
}

encodedToken, hasToken := secret.Data["root"]
if !hasToken {
return errors.Wrap(err, "secret vault-root-token did not contain root token")
}

fmt.Println(string(encodedToken))

return nil
}()

if err != nil {
if viper.GetBool(argSuppressErrors) {
core.Log.Warnf("Error getting root token from k8s: %s", err)
return nil
}
return err
}

return nil
},
}, func(cmd *cobra.Command) {

cmd.Flags().Bool(argSuppressErrors, false, "Don't return errors if something fails.")
cmd.Flags().String(ArgVaultNamespace, "default", "The namespace vault is deployed into.")
})

const (
ArgVaultAddr = "vault-addr"
ArgVaultToken = "vault-token"
Expand All @@ -417,7 +466,9 @@ const (
ArgVaultSecretOverwrite = "overwrite"
ArgVaultSecretDefault = "default"
ArgVaultNamespace = "vault-namespace"
ArgVaultCluster = "cluster"

argK8sNamespace = "namespace"
argSuppressErrors = "no-errors"
)

func init() {
Expand Down
5 changes: 5 additions & 0 deletions pkg/actions/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"github.com/naveego/bosun/pkg/actions"
"github.com/naveego/bosun/pkg/cli"
"github.com/naveego/bosun/pkg/command"
"github.com/naveego/bosun/pkg/core"
"github.com/naveego/bosun/pkg/ioc"
"github.com/naveego/bosun/pkg/templating"
Expand Down Expand Up @@ -46,6 +47,10 @@ type TestActionContext struct {
log *logrus.Entry
}

func (t TestActionContext) GetWorkspaceCommand(name string, hint string) *command.CommandValue {
panic("implement me")
}

func (t TestActionContext) ResolvePath(path string, expansions ...string) string {
panic("implement me")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/bosun/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ func (p *Platform) CommitPlan(ctx BosunContext) (*ReleaseManifest, error) {
if err != nil {
return nil, err
}
currentProvider := NewReleaseManifestAppProvider(currentRelease)
// currentProvider := NewReleaseManifestAppProvider(currentRelease)
plan, err := currentRelease.GetPlan()
if err != nil {
return nil, err
Expand Down Expand Up @@ -765,7 +765,7 @@ func (p *Platform) CommitPlan(ctx BosunContext) (*ReleaseManifest, error) {
case SlotStable:
log.Infof("App %q has already been added to this release.", appName)

app, getAppErr = currentProvider.ProvideApp(AppProviderRequest{Name: appName})
app, getAppErr = previousProvider.ProvideApp(AppProviderRequest{Name: appName})
if getAppErr != nil {
return nil, getAppErr
}
Expand Down
1 change: 1 addition & 0 deletions pkg/command/command_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type CommandValue struct {
OS map[string]*CommandValue `yaml:"os,omitempty" json:"os,omitempty"`
WorkspaceCommand string `yaml:"workspaceCommand,omitempty"`
WorkspaceCommandHint string `yaml:"workspaceCommandHint,omitempty"`
Disabled bool `yaml:"disabled"`

resolvedValue string
}
Expand Down
25 changes: 0 additions & 25 deletions pkg/jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,31 +170,6 @@ func (c *Client) HandleEvent(event *stories.ValidatedEvent) error {
}

func (c *Client) handleBranchCreated(event *stories.ValidatedEvent, payload stories.EventBranchCreated, story *jira.Issue) (error, bool) {
subtaskName := SubtaskName(event.Issue(), payload.Branch)
var subtask = &jira.Issue{
Fields: &jira.IssueFields{
Parent: &jira.Parent{Key: story.Key, ID: story.ID},
Project: story.Fields.Project,
Summary: subtaskName.String(),
Type: jira.IssueType{ID: "5"},
Assignee: &jira.User{AccountID: c.AccountID},
Description: fmt.Sprintf(`This subtask tracks development in the %s repo.
The branch is %s
Link: %s
`, event.Issue().RepoRef.String(), payload.Branch, event.URL()),
},
}
var err error
var res *jira.Response

subtask, res, err = c.jira.Issue.Create(subtask)
if err != nil {
return errors.Wrapf(detailedErr(res, err), "create subtask documenting branch %q for story with key %q, id %q", subtaskName, story.Key, story.ID), true
}

err = c.doTransition(subtask.ID, c.transitions.InDevelopment)
return nil, false
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/values/value_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ func (v ValueSet) WithDynamicValuesResolved(ctx command.ExecutionContext) (Value

for k, value := range out.Dynamic {

if value.Disabled {
continue
}

if value.Script != "" {
ctx.Log().Debugf("Resolving dynamic value %q using script:\n %s", k, value.Script)
}
Expand Down

0 comments on commit 37d10c8

Please sign in to comment.