Create dev org in database (#41)

These changes create a dev org in the database on startup.

They also plumb through a dev-org-id flag that allows specifying a chosen org ID in dev mode.

The logic will allow us to let people specify a default org ID in the config file too, although currently that's not enabled.

Several other improvements were made as well:

*    Consolidation of some code from across the server commands
*    Plumbing shutdownch through so we can use it for canceling contexts during startup
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-05-11 17:03:23 -04:00
1 parent 670abaed88
commit 062ce6079f
7 files changed
+150 -63

No files matched your search

+28 -4
View File
@@ -22,7 +22,6 @@ var _ cli.Command = (*Command)(nil)
var _ cli.CommandAutocomplete = (*Command)(nil)
type Command struct {
*base.Command
*base.Server
ExtShutdownCh chan struct{}
@@ -42,6 +41,7 @@ type Command struct {
flagDevAdminPassword string
flagDevControllerAPIListenAddr string
flagDevControllerClusterListenAddr string
flagDevOrgId string
flagCombineLogs bool
}
@@ -105,6 +105,15 @@ func (c *Command) Flags() *base.FlagSets {
"production.",
})
f.StringVar(&base.StringVar{
Name: "dev-org-id",
Target: &c.flagDevOrgId,
Default: "",
EnvVar: "WATCHTWER_DEV_ORG_ID",
Usage: "Auto-created organization ID. This only applies when running in \"dev\" " +
"mode.",
})
f.StringVar(&base.StringVar{
Name: "dev-admin-password",
Target: &c.flagDevAdminPassword,
@@ -147,7 +156,6 @@ func (c *Command) AutocompleteFlags() complete.Flags {
}
func (c *Command) Run(args []string) int {
c.Server = base.NewServer()
c.CombineLogs = c.flagCombineLogs
if result := c.ParseFlagsAndConfig(args); result > 0 {
@@ -244,7 +252,7 @@ func (c *Command) Run(args []string) int {
}
if c.flagDev {
if err := c.CreateDevDatabase(); err != nil {
if err := c.CreateDevDatabase("postgres"); err != nil {
c.UI.Error(fmt.Errorf("Error creating dev database container: %s", err.Error()).Error())
return 1
}
@@ -296,10 +304,14 @@ func (c *Command) ParseFlagsAndConfig(args []string) int {
} else {
c.Config, err = config.DevController()
if err != nil {
c.UI.Error(fmt.Errorf("Error creating dev config: %s", err).Error())
c.UI.Error(fmt.Errorf("Error creating dev config: %w", err).Error())
return 1
}
if c.flagDevOrgId != "" {
c.Config.DefaultOrgId = c.flagDevOrgId
}
for _, l := range c.Config.Listeners {
if len(l.Purpose) != 1 {
continue
@@ -318,6 +330,18 @@ func (c *Command) ParseFlagsAndConfig(args []string) int {
}
}
if c.Config.DefaultOrgId != "" {
if !strings.HasPrefix(c.Config.DefaultOrgId, "o_") {
c.UI.Error(fmt.Sprintf("Invalid default org ID, must start with %q", "o_"))
return 1
}
if len(c.Config.DefaultOrgId) != 12 {
c.UI.Error(fmt.Sprintf("Invalid default org ID, must be 10 base62 characters after %q", "o_"))
return 1
}
c.DefaultOrgId = c.Config.DefaultOrgId
}
return 0
}