Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit 828bcf5

Browse files
committed
kd/daemon: add installation initial check
1 parent cf64c1b commit 828bcf5

6 files changed

Lines changed: 51 additions & 17 deletions

File tree

go/src/koding/klientctl/auth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func AuthLogin(c *cli.Context, log logging.Logger, _ string) (int, error) {
3939
opts := &auth.LoginOptions{
4040
Team: c.String("team"),
4141
Token: c.String("token"),
42+
Force: true,
4243
}
4344

4445
resp, err := f.Login(opts)

go/src/koding/klientctl/daemon/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ func (c *Client) Stop() error {
6767
return svc.Stop()
6868
}
6969

70+
func (c *Client) Installed() bool {
71+
c.init()
72+
73+
return len(c.d.Installation) == len(script)
74+
}
75+
7076
func (c *Client) Ping() error {
7177
timeout := time.NewTimer(c.timeout())
7278
defer timeout.Stop()
@@ -197,3 +203,4 @@ func Update(opts *Opts) error { return DefaultClient.Update(opts) }
197203
func Start() error { return DefaultClient.Start() }
198204
func Restart() error { return DefaultClient.Restart() }
199205
func Stop() error { return DefaultClient.Stop() }
206+
func Installed() bool { return DefaultClient.Installed() }

go/src/koding/klientctl/daemon/install.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ var script = []InstallStep{{
400400
Team: opts.Team,
401401
Token: opts.Token,
402402
Prefix: "\t",
403+
Force: true,
403404
})
404405

405406
fmt.Println()
@@ -513,6 +514,10 @@ var script = []InstallStep{{
513514
return "", err
514515
}
515516

517+
// Stop the daemon if it's running, for the new configuration
518+
// to take effect.
519+
_ = svc.Stop()
520+
516521
return "", svc.Start()
517522
},
518523
RunOnUpdate: true,

go/src/koding/klientctl/endpoint/auth/auth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type LoginOptions struct {
4545
Username string
4646
Password string
4747
Prefix string
48+
Force bool
4849
}
4950

5051
func (opts *LoginOptions) AskUserPass() (err error) {

go/src/koding/klientctl/endpoint/auth/facade.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,19 @@ func NewFacade(opts *FacadeOpts) (*Facade, error) {
6060
}
6161

6262
func (f *Facade) Login(opts *LoginOptions) (*stack.PasswordLoginResponse, error) {
63-
// If we already own a valid kite.key, it means we were already
64-
// authenticated and we just call kloud using kite.key authentication.
65-
err := f.Kloud.Transport.(stack.Validator).Valid()
63+
newLogin := opts.Force
6664

67-
f.log().Debug("auth: transport test: %s", err)
65+
if !newLogin {
66+
// If we already own a valid kite.key, it means we were already
67+
// authenticated and we just call kloud using kite.key authentication.
68+
err := f.Kloud.Transport.(stack.Validator).Valid()
69+
f.log().Debug("auth: transport test: %s", err)
6870

69-
if err != nil && opts.Token == "" {
70-
if err = opts.AskUserPass(); err != nil {
71+
newLogin = err != nil && opts.Token == ""
72+
}
73+
74+
if newLogin {
75+
if err := opts.AskUserPass(); err != nil {
7176
return nil, err
7277
}
7378
}

go/src/koding/klientctl/main.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"koding/klientctl/auth"
2323
"koding/klientctl/config"
2424
"koding/klientctl/ctlcli"
25+
"koding/klientctl/daemon"
2526
"koding/klientctl/endpoint/kloud"
2627
"koding/klientctl/util"
2728

@@ -105,15 +106,32 @@ func run(args []string) {
105106
log.SetLevel(logging.DEBUG)
106107
}
107108

109+
kloud.DefaultLog = log
110+
testKloudHook(kloud.DefaultClient)
111+
defer ctlcli.Close()
112+
113+
// TODO(leeola): deprecate this default, instead passing it as a dependency
114+
// to the users of it.
115+
//
116+
// init the defaultHealthChecker with the log.
117+
defaultHealthChecker = NewDefaultHealthChecker(log)
118+
108119
// Check if the command the user is giving requires sudo.
109120
if err := AdminRequired(os.Args, sudoRequiredFor, util.NewPermissions()); err != nil {
110121
// In the event of an error, simply print the error to the user
111122
// and exit.
112-
fmt.Println("Error: this command requires sudo.")
123+
fmt.Fprintln(os.Stderr, "This command requires sudo.")
113124
ctlcli.Close()
114125
os.Exit(10)
115126
}
116127

128+
if !daemon.Installed() && !isDaemonCommand(os.Args[1:]) {
129+
fmt.Fprintln(os.Stderr, "This command requires a daemon to be installed. Please install it "+
130+
"with the following command:\n\n\tsudo kd daemon install [--team <name>]\n")
131+
ctlcli.Close()
132+
os.Exit(1)
133+
}
134+
117135
sig := make(chan os.Signal, 1)
118136

119137
go func() {
@@ -124,16 +142,6 @@ func run(args []string) {
124142

125143
signal.Notify(sig, signals...)
126144

127-
kloud.DefaultLog = log
128-
testKloudHook(kloud.DefaultClient)
129-
defer ctlcli.Close()
130-
131-
// TODO(leeola): deprecate this default, instead passing it as a dependency
132-
// to the users of it.
133-
//
134-
// init the defaultHealthChecker with the log.
135-
defaultHealthChecker = NewDefaultHealthChecker(log)
136-
137145
app := cli.NewApp()
138146
app.Name = config.Name
139147
app.Version = getReadableVersion(config.Version)
@@ -900,3 +908,10 @@ func find(cmds cli.Commands, names ...string) cli.Command {
900908

901909
return cli.Command{}
902910
}
911+
912+
func isDaemonCommand(args []string) bool {
913+
if len(args) == 0 {
914+
return false
915+
}
916+
return args[0] == "daemon"
917+
}

0 commit comments

Comments
 (0)