* Renamed fakeRepo to mockRepo * Changing around TODOs to capture current plans with the code. * Remove the repo interface and use a real DB for tests. * Fixed a few tests. * Adding checks for properly formatted identifiers. * Updating code to account for recent merges. Relaxing some restrictions on what an id can look like. * Added small comment explaining why RegisterGrpcGatewayer interface exists. * Move project service up a call level. Long term I think it would make sense to define these services even higher up the call stack, potentially at the initialization of the controller server, near where the DB is initialized. * Making these fields align with those in the resources under hosts/... * Hard code the service initialization in the handler registration code. * Regenerate project related code proto code. * Removing type field for project. * Adds basic project handling in CLI/SDK (#47) * Adds SDK for creating and reading projects * Reorganizes a bit * Adds CLI for creating and reading projects * Fixes some some generation bugs * Adds redirect support for default org ID * Fix protobuf import references being moved. * Update client test to look for "orgs" and "projects" instead of "org" and "project" * Update expected segments test * Fix setAddr handling when default organization is in use * Set client in target after create or read Co-authored-by: Jim Lambert <jimlambrt@Jims-MBP-3.home> Co-authored-by: Jim <jlambert@hashicorp.com> Co-authored-by: Michael Gaffney <mgaffney@users.noreply.github.com> Co-authored-by: Jeff Mitchell <jeffrey.mitchell@gmail.com>
118 lines
2.9 KiB
Go
118 lines
2.9 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/hashicorp/watchtower/internal/cmd/base"
|
|
"github.com/hashicorp/watchtower/internal/cmd/commands/controller"
|
|
"github.com/hashicorp/watchtower/internal/cmd/commands/dev"
|
|
"github.com/hashicorp/watchtower/internal/cmd/commands/hosts"
|
|
"github.com/hashicorp/watchtower/internal/cmd/commands/scopes"
|
|
"github.com/hashicorp/watchtower/internal/cmd/commands/worker"
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
// Commands is the mapping of all the available commands.
|
|
var Commands map[string]cli.CommandFactory
|
|
|
|
func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
|
|
getBaseCommand := func() *base.Command {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
ret := &base.Command{
|
|
UI: ui,
|
|
ShutdownCh: MakeShutdownCh(),
|
|
Context: ctx,
|
|
}
|
|
|
|
go func() {
|
|
<-ret.ShutdownCh
|
|
cancel()
|
|
}()
|
|
|
|
return ret
|
|
}
|
|
|
|
Commands = map[string]cli.CommandFactory{
|
|
"controller": func() (cli.Command, error) {
|
|
return &controller.Command{
|
|
Server: base.NewServer(&base.Command{
|
|
UI: serverCmdUi,
|
|
ShutdownCh: MakeShutdownCh(),
|
|
}),
|
|
SighupCh: MakeSighupCh(),
|
|
SigUSR2Ch: MakeSigUSR2Ch(),
|
|
}, nil
|
|
},
|
|
"worker": func() (cli.Command, error) {
|
|
return &worker.Command{
|
|
Server: base.NewServer(&base.Command{
|
|
UI: serverCmdUi,
|
|
ShutdownCh: MakeShutdownCh(),
|
|
}),
|
|
SighupCh: MakeSighupCh(),
|
|
SigUSR2Ch: MakeSigUSR2Ch(),
|
|
}, nil
|
|
},
|
|
"dev": func() (cli.Command, error) {
|
|
return &dev.Command{
|
|
Server: base.NewServer(&base.Command{
|
|
UI: serverCmdUi,
|
|
ShutdownCh: MakeShutdownCh(),
|
|
}),
|
|
SighupCh: MakeSighupCh(),
|
|
SigUSR2Ch: MakeSigUSR2Ch(),
|
|
}, nil
|
|
},
|
|
"hosts create": func() (cli.Command, error) {
|
|
return &hosts.CreateCommand{
|
|
Command: getBaseCommand(),
|
|
}, nil
|
|
},
|
|
"projects create": func() (cli.Command, error) {
|
|
return &scopes.CreateProjectCommand{
|
|
Command: getBaseCommand(),
|
|
}, nil
|
|
},
|
|
"projects read": func() (cli.Command, error) {
|
|
return &scopes.ReadProjectCommand{
|
|
Command: getBaseCommand(),
|
|
}, nil
|
|
},
|
|
}
|
|
}
|
|
|
|
// MakeShutdownCh returns a channel that can be used for shutdown
|
|
// notifications for commands. This channel will send a message for every
|
|
// SIGINT or SIGTERM received.
|
|
func MakeShutdownCh() chan struct{} {
|
|
resultCh := make(chan struct{})
|
|
|
|
shutdownCh := make(chan os.Signal, 4)
|
|
signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM)
|
|
go func() {
|
|
<-shutdownCh
|
|
close(resultCh)
|
|
}()
|
|
return resultCh
|
|
}
|
|
|
|
// MakeSighupCh returns a channel that can be used for SIGHUP
|
|
// reloading. This channel will send a message for every
|
|
// SIGHUP received.
|
|
func MakeSighupCh() chan struct{} {
|
|
resultCh := make(chan struct{})
|
|
|
|
signalCh := make(chan os.Signal, 4)
|
|
signal.Notify(signalCh, syscall.SIGHUP)
|
|
go func() {
|
|
for {
|
|
<-signalCh
|
|
resultCh <- struct{}{}
|
|
}
|
|
}()
|
|
return resultCh
|
|
}
|