-
Notifications
You must be signed in to change notification settings - Fork 26
[WIP] Update to Gitbase v0.20.0 #439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2ca6e9b
cf1581d
08072c8
57c3767
baf3b29
a8f3495
535e6ce
cf6f9f3
8f602fc
ea992d2
4304ec3
d3a5a0c
9ae3c1c
6508793
a495ab9
15cd0be
602fbe7
4db06e2
0369669
352d0f2
1926b78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,22 +17,16 @@ package cmd | |
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
|
||
"os" | ||
"os/signal" | ||
"strings" | ||
"time" | ||
|
||
"github.com/src-d/engine/api" | ||
"github.com/src-d/engine/cmd/srcd/daemon" | ||
"github.com/src-d/engine/components" | ||
"github.com/src-d/engine/docker" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/pkg/term" | ||
"gopkg.in/src-d/go-log.v1" | ||
) | ||
|
||
// sqlCmd represents the sql command | ||
|
@@ -55,7 +49,7 @@ func (c *sqlCmd) Execute(args []string) error { | |
return humanizef(err, "could not get daemon client") | ||
} | ||
|
||
if err := startGitbaseWithClient(client); err != nil { | ||
if err := startComponent(client, &components.Gitbase); err != nil { | ||
return err | ||
} | ||
|
||
|
@@ -86,35 +80,11 @@ func (c *sqlCmd) Execute(args []string) error { | |
} | ||
} | ||
|
||
resp, exit, err := runMysqlCli(context.Background(), query) | ||
if err != nil { | ||
return humanizef(err, "could not run mysql client") | ||
} | ||
defer resp.Close() | ||
defer stopMysqlClient() | ||
|
||
// in case of Ctrl-C or kill defer wouldn't work | ||
ch := make(chan os.Signal, 1) | ||
signal.Notify(ch, os.Interrupt, os.Kill) | ||
go func() { | ||
<-ch | ||
stopMysqlClient() | ||
}() | ||
|
||
if query != "" { | ||
if _, err = io.Copy(os.Stdout, resp.Reader); err != nil { | ||
return err | ||
} | ||
|
||
cd := int(<-exit) | ||
if cd != 0 { | ||
return fmt.Errorf("MySQL exited with status %d", cd) | ||
} | ||
|
||
return nil | ||
if err = runMysqlCli(context.Background(), query); err != nil { | ||
return err | ||
} | ||
|
||
return attachStdio(resp) | ||
return nil | ||
} | ||
|
||
func ensureConnReady(client api.EngineClient) error { | ||
|
@@ -175,98 +145,29 @@ func pingDB(ctx context.Context, client api.EngineClient, queryTimeoutSeconds ti | |
} | ||
} | ||
|
||
func startGitbaseWithClient(client api.EngineClient) error { | ||
started := logAfterTimeoutWithServerLogs("this is taking a while, "+ | ||
"if this is the first time you launch sql client, "+ | ||
"it might take a few more minutes while we install all the required images", | ||
5*time.Second) | ||
defer started() | ||
|
||
// Download & run dependencies | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) | ||
defer cancel() | ||
_, err := client.StartComponent(ctx, &api.StartComponentRequest{ | ||
Name: components.Gitbase.Name, | ||
}) | ||
if err != nil { | ||
return humanizef(err, "could not start gitbase") | ||
} | ||
|
||
if err := docker.EnsureInstalled(components.MysqlCli.Image, components.MysqlCli.Version); err != nil { | ||
return humanizef(err, "could not install mysql client") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func runMysqlCli(ctx context.Context, query string, opts ...docker.ConfigOption) (*types.HijackedResponse, chan int64, error) { | ||
cmd := []string{"mysql", "-h", components.Gitbase.Name} | ||
func runMysqlCli(ctx context.Context, query string) error { | ||
interactive := true | ||
cmd := []string{"mysql"} | ||
if query != "" { | ||
cmd = append(cmd, "-e", query) | ||
} | ||
|
||
config := &container.Config{ | ||
Image: components.MysqlCli.ImageWithVersion(), | ||
Cmd: cmd, | ||
cmd = append(cmd, "-t", "-e", query) | ||
interactive = false | ||
} | ||
host := &container.HostConfig{} | ||
docker.ApplyOptions(config, host, opts...) | ||
|
||
return docker.Attach(context.Background(), config, host, components.MysqlCli.Name) | ||
} | ||
|
||
func attachStdio(resp *types.HijackedResponse) (err error) { | ||
inputDone := make(chan error) | ||
outputDone := make(chan error) | ||
insResp, err := docker.ExecAndAttach(context.Background(), interactive, components.Gitbase.Name, cmd...) | ||
|
||
in, out, _ := term.StdStreams() | ||
// set terminal into raw mode to propagate special characters | ||
fd, isTerminal := term.GetFdInfo(in) | ||
if isTerminal { | ||
var prevState *term.State | ||
prevState, err = term.SetRawTerminal(fd) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
err = term.RestoreTerminal(fd, prevState) | ||
}() | ||
if err != nil { | ||
return humanizef(err, "a problem occurred while trying to run mysql client") | ||
} | ||
|
||
go func() { | ||
_, err := io.Copy(out, resp.Reader) | ||
outputDone <- err | ||
resp.CloseWrite() | ||
}() | ||
|
||
go func() { | ||
_, err := io.Copy(resp.Conn, in) | ||
|
||
if err := resp.CloseWrite(); err != nil { | ||
log.Debugf("Couldn't send EOF: %s", err) | ||
} | ||
|
||
inputDone <- err | ||
}() | ||
|
||
select { | ||
case err := <-outputDone: | ||
return err | ||
case err := <-inputDone: | ||
if err == nil { | ||
// Wait for output to complete streaming. | ||
return <-outputDone | ||
} | ||
|
||
return err | ||
if insResp.Running { | ||
return fmt.Errorf("MySQL cli is still running") | ||
} | ||
} | ||
|
||
func stopMysqlClient() { | ||
err := docker.RemoveContainer(components.MysqlCli.Name) | ||
if err != nil { | ||
log.Warningf("could not stop mysql client: %v", err) | ||
if insResp.ExitCode != 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure if we have tests for it. What happens when you exit with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch! Now it exits with 0 on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe it's okay to exit with error on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a test for |
||
return fmt.Errorf("MySQL cli returned with exit code %d", insResp.ExitCode) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function has name
startGitbaseWithClient
but it doesn't start client anymore. Just gitbase.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done here and here.