mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
cmd/cli: change "ctrld start" behavior
Without reading the documentation, users may think that "ctrld start" will just start ctrld service. However, this is not the case, and may lead to unexpected result from user's point of view. This commit changes "ctrld start" to just start already installed ctrld service, so users won't lost what they did installed before. If there are any arguments specified, performing the current behavior.
This commit is contained in:
@@ -105,9 +105,11 @@ Available Commands:
|
|||||||
start Quick start service and configure DNS on interface
|
start Quick start service and configure DNS on interface
|
||||||
stop Quick stop service and remove DNS from interface
|
stop Quick stop service and remove DNS from interface
|
||||||
restart Restart the ctrld service
|
restart Restart the ctrld service
|
||||||
|
reload Reload the ctrld service
|
||||||
status Show status of the ctrld service
|
status Show status of the ctrld service
|
||||||
uninstall Stop and uninstall the ctrld service
|
uninstall Stop and uninstall the ctrld service
|
||||||
clients Manage clients
|
clients Manage clients
|
||||||
|
upgrade Upgrading ctrld to latest version
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
-h, --help help for ctrld
|
-h, --help help for ctrld
|
||||||
|
|||||||
+63
-3
@@ -115,7 +115,7 @@ func initCLI() {
|
|||||||
&verbose,
|
&verbose,
|
||||||
"verbose",
|
"verbose",
|
||||||
"v",
|
"v",
|
||||||
`verbose log output, "-v" basic logging, "-vv" debug level logging`,
|
`verbose log output, "-v" basic logging, "-vv" debug logging`,
|
||||||
)
|
)
|
||||||
rootCmd.PersistentFlags().BoolVarP(
|
rootCmd.PersistentFlags().BoolVarP(
|
||||||
&silent,
|
&silent,
|
||||||
@@ -163,7 +163,10 @@ func initCLI() {
|
|||||||
},
|
},
|
||||||
Use: "start",
|
Use: "start",
|
||||||
Short: "Install and start the ctrld service",
|
Short: "Install and start the ctrld service",
|
||||||
Args: cobra.NoArgs,
|
Long: `Install and start the ctrld service
|
||||||
|
|
||||||
|
NOTE: running "ctrld start" without any arguments will start already installed ctrld service.`,
|
||||||
|
Args: cobra.NoArgs,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
checkStrFlagEmpty(cmd, cdUidFlagName)
|
checkStrFlagEmpty(cmd, cdUidFlagName)
|
||||||
checkStrFlagEmpty(cmd, cdOrgFlagName)
|
checkStrFlagEmpty(cmd, cdOrgFlagName)
|
||||||
@@ -187,8 +190,9 @@ func initCLI() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
status, _ := s.Status()
|
status, err := s.Status()
|
||||||
isCtrldRunning := status == service.StatusRunning
|
isCtrldRunning := status == service.StatusRunning
|
||||||
|
isCtrldInstalled := !errors.Is(err, service.ErrNotInstalled)
|
||||||
|
|
||||||
// If pin code was set, do not allow running start command.
|
// If pin code was set, do not allow running start command.
|
||||||
if isCtrldRunning {
|
if isCtrldRunning {
|
||||||
@@ -197,6 +201,54 @@ func initCLI() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !startOnly {
|
||||||
|
startOnly = len(osArgs) == 0
|
||||||
|
}
|
||||||
|
// If user run "ctrld start" and ctrld is already installed, starting existing service.
|
||||||
|
if startOnly && isCtrldInstalled {
|
||||||
|
tryReadingConfigWithNotice(false, true)
|
||||||
|
if err := v.Unmarshal(&cfg); err != nil {
|
||||||
|
mainLog.Load().Fatal().Msgf("failed to unmarshal config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
initLogging()
|
||||||
|
tasks := []task{
|
||||||
|
resetDnsTask(p, s),
|
||||||
|
{s.Stop, false},
|
||||||
|
{func() error {
|
||||||
|
// Save current DNS so we can restore later.
|
||||||
|
withEachPhysicalInterfaces("", "save DNS settings", func(i *net.Interface) error {
|
||||||
|
return saveCurrentStaticDNS(i)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}, false},
|
||||||
|
{s.Start, true},
|
||||||
|
{noticeWritingControlDConfig, false},
|
||||||
|
}
|
||||||
|
mainLog.Load().Notice().Msg("Starting existing ctrld service")
|
||||||
|
if doTasks(tasks) {
|
||||||
|
mainLog.Load().Notice().Msg("Service started")
|
||||||
|
sockDir, err := socketDir()
|
||||||
|
if err != nil {
|
||||||
|
mainLog.Load().Warn().Err(err).Msg("Failed to get socket directory")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if cc := newSocketControlClient(s, sockDir); cc != nil {
|
||||||
|
if resp, _ := cc.post(ifacePath, nil); resp != nil && resp.StatusCode == http.StatusOK {
|
||||||
|
if iface == "auto" {
|
||||||
|
iface = defaultIfaceName()
|
||||||
|
}
|
||||||
|
logger := mainLog.Load().With().Str("iface", iface).Logger()
|
||||||
|
logger.Debug().Msg("setting DNS successfully")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mainLog.Load().Error().Err(err).Msg("Failed to start existing ctrld service")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if cdUID != "" {
|
if cdUID != "" {
|
||||||
doValidateCdRemoteConfig(cdUID)
|
doValidateCdRemoteConfig(cdUID)
|
||||||
} else if uid := cdUIDFromProvToken(); uid != "" {
|
} else if uid := cdUIDFromProvToken(); uid != "" {
|
||||||
@@ -373,6 +425,8 @@ func initCLI() {
|
|||||||
startCmd.Flags().StringVarP(&nextdns, nextdnsFlagName, "", "", "NextDNS resolver id")
|
startCmd.Flags().StringVarP(&nextdns, nextdnsFlagName, "", "", "NextDNS resolver id")
|
||||||
startCmd.Flags().StringVarP(&cdUpstreamProto, "proto", "", ctrld.ResolverTypeDOH, `Control D upstream type, either "doh" or "doh3"`)
|
startCmd.Flags().StringVarP(&cdUpstreamProto, "proto", "", ctrld.ResolverTypeDOH, `Control D upstream type, either "doh" or "doh3"`)
|
||||||
startCmd.Flags().BoolVarP(&skipSelfChecks, "skip_self_checks", "", false, `Skip self checks after installing ctrld service`)
|
startCmd.Flags().BoolVarP(&skipSelfChecks, "skip_self_checks", "", false, `Skip self checks after installing ctrld service`)
|
||||||
|
startCmd.Flags().BoolVarP(&startOnly, "start_only", "", false, "Do not install new service")
|
||||||
|
_ = startCmd.Flags().MarkHidden("start_only")
|
||||||
|
|
||||||
routerCmd := &cobra.Command{
|
routerCmd := &cobra.Command{
|
||||||
Use: "setup",
|
Use: "setup",
|
||||||
@@ -739,7 +793,13 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
},
|
},
|
||||||
Use: "start",
|
Use: "start",
|
||||||
Short: "Quick start service and configure DNS on interface",
|
Short: "Quick start service and configure DNS on interface",
|
||||||
|
Long: `Quick start service and configure DNS on interface
|
||||||
|
|
||||||
|
NOTE: running "ctrld start" without any arguments will start already installed ctrld service.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if len(os.Args) == 2 {
|
||||||
|
startOnly = true
|
||||||
|
}
|
||||||
if !cmd.Flags().Changed("iface") {
|
if !cmd.Flags().Changed("iface") {
|
||||||
os.Args = append(os.Args, "--iface="+ifaceStartStop)
|
os.Args = append(os.Args, "--iface="+ifaceStartStop)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ var (
|
|||||||
deactivationPin int64
|
deactivationPin int64
|
||||||
skipSelfChecks bool
|
skipSelfChecks bool
|
||||||
cleanup bool
|
cleanup bool
|
||||||
|
startOnly bool
|
||||||
|
|
||||||
mainLog atomic.Pointer[zerolog.Logger]
|
mainLog atomic.Pointer[zerolog.Logger]
|
||||||
consoleWriter zerolog.ConsoleWriter
|
consoleWriter zerolog.ConsoleWriter
|
||||||
|
|||||||
Reference in New Issue
Block a user