-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a lockfile to serialize blessclient invocations (#84)
Use a lockfile to serialize blessclient invocationsWhen integrated with tools like terraform or ansible, multiple concurrent runs of blessclient resulted in unnecessary work. Instead, serialize blessclient commands.
- Loading branch information
1 parent
0b4e23b
commit 9a6eedc
Showing
93 changed files
with
5,577 additions
and
625 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,33 +1,53 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/chanzuckerberg/blessclient/pkg/config" | ||
"github.com/chanzuckerberg/blessclient/pkg/util" | ||
"github.com/pkg/errors" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
flagConfig = "config" | ||
flagVerbose = "verbose" | ||
) | ||
|
||
func init() { | ||
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Use this to enable verbose mode") | ||
rootCmd.PersistentFlags().StringP(flagConfig, "c", config.DefaultConfigFile, "Use this to override the bless config file.") | ||
rootCmd.PersistentFlags().BoolP(flagVerbose, "v", false, "Use this to enable verbose mode") | ||
} | ||
|
||
var pidLock *util.Lock | ||
var rootCmd = &cobra.Command{ | ||
Use: "blessclient", | ||
Short: "", | ||
PersistentPostRunE: func(cmd *cobra.Command, args []string) error { | ||
return errors.Wrap(pidLock.Unlock(), "Error releasing lock") | ||
}, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
verbose, err := cmd.Flags().GetBool("verbose") | ||
verbose, err := cmd.Flags().GetBool(flagVerbose) | ||
if err != nil { | ||
return errors.Wrap(err, "Missing verbose flag") | ||
} | ||
if verbose { | ||
log.SetLevel(log.DebugLevel) | ||
} | ||
return nil | ||
|
||
// pid lock | ||
configPath, err := util.GetConfigPath(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
pidLock, err = util.NewLock(configPath) | ||
if err != nil { | ||
return err | ||
} | ||
return errors.Wrap(pidLock.Lock(), "Error acquiring lock") | ||
}, | ||
} | ||
|
||
// Execute executes the command | ||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
log.Fatal(err) | ||
} | ||
func Execute() error { | ||
return rootCmd.Execute() | ||
} |
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,60 @@ | ||
package cmd | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRootNoError(t *testing.T) { | ||
a := assert.New(t) | ||
err := rootCmd.Execute() | ||
a.Nil(err) | ||
} | ||
|
||
func TestRootMissingVerbose(t *testing.T) { | ||
a := assert.New(t) | ||
cmd := &cobra.Command{} | ||
err := rootCmd.PersistentPreRunE(cmd, nil) | ||
a.NotNil(err) | ||
a.Contains(err.Error(), "flag accessed but not defined: verbose") | ||
} | ||
|
||
func TestRootMissingConfig(t *testing.T) { | ||
a := assert.New(t) | ||
cmd := &cobra.Command{} | ||
cmd.PersistentFlags().BoolP("verbose", "v", false, "Use this to enable verbose mode") | ||
|
||
flags := []string{"--verbose"} | ||
err := cmd.ParseFlags(flags) | ||
a.Nil(err) | ||
|
||
err = rootCmd.PersistentPreRunE(cmd, nil) | ||
a.NotNil(err) | ||
a.Contains(err.Error(), "Missing config") | ||
} | ||
|
||
func TestRootLock(t *testing.T) { | ||
a := assert.New(t) | ||
cmd := &cobra.Command{} | ||
|
||
name, err := ioutil.TempDir("", "blessclient-tests") | ||
a.Nil(err) | ||
defer os.RemoveAll(name) | ||
|
||
cmd.PersistentFlags().BoolP("verbose", "v", false, "Use this to enable verbose mode") | ||
cmd.PersistentFlags().StringP(flagConfig, "c", name, "Use this to override the bless config file.") | ||
|
||
flags := []string{"--verbose", "--config", name} | ||
err = cmd.ParseFlags(flags) | ||
a.Nil(err) | ||
|
||
err = rootCmd.PersistentPreRunE(cmd, nil) | ||
a.Nil(err) | ||
|
||
err = rootCmd.PersistentPostRunE(cmd, nil) | ||
a.Nil(err) | ||
} |
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,20 @@ | ||
package util | ||
|
||
import ( | ||
homedir "github.com/mitchellh/go-homedir" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// GetConfigPath gets the config path from a cobra cmd | ||
func GetConfigPath(cmd *cobra.Command) (string, error) { | ||
configFile, err := cmd.Flags().GetString("config") | ||
if err != nil { | ||
return "", errors.New("Missing config") | ||
} | ||
expandedConfigFile, err := homedir.Expand(configFile) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "Could not expand %s", configFile) | ||
} | ||
return expandedConfigFile, nil | ||
} |
Oops, something went wrong.