// createBootstrap will create a bootstrap file for the server. func (srv *Server) writeBootstrapFile(bs *os.File) error { log.Debugf("Creating bootstrap file %q\n", bs.Name()) // Write the header to the bootstrap file if _, err := appendLines(bs, bsHeader); err != nil { return err } // Append bootstrap files from distribution for _, fname := range sqlFiles { fullname := filepath.Join(srv.Dist.Root, "share", fname) rd, err := os.Open(fullname) if err != nil { return err } _, err = io.Copy(bs, rd) rd.Close() if err != nil { return err } } // Append the footer lines to the bootstrap file if _, err := appendLines(bs, bsFooter); err != nil { return err } return nil }
// create will create the necessary files and directories to set up // the stable. Distributions are stored under the "dist" directory, // where there is one directory for each distribution. Server data is // stored under the "server" directory, where there is one directory // for each server. func (stable *Stable) setup() error { log.Debugf("Creating files and directories for stable in %q", stable.Root) // Create the stable directory if err := os.Mkdir(stable.Root, 0755); err != nil { errno := err.(*os.PathError).Err.(syscall.Errno) if errno == syscall.EEXIST { return ErrStableExists } else { return err } } dirs := []string{ stable.distDir, stable.serverDir, stable.tmpDir, } for _, dir := range dirs { if err := os.Mkdir(dir, 0755); err != nil { os.RemoveAll(stable.Root) return err } } return nil }
// Execute is used to execute a command using the mysql client for the // server and return the result. func (srv *Server) Execute(commands ...string) error { argv := srv.mysqlArgs("-e" + strings.Join(commands, ";")) cmd := exec.Command(srv.bin("mysql"), argv...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr log.Debugf("Executing %v", cmd.Args) return cmd.Run() }
// Connect is used to connect a terminal to the server and run a // prompt. func (srv *Server) Connect(args ...string) error { argv := srv.mysqlArgs(args...) cmd := exec.Command(srv.bin("mysql"), argv...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr log.Debugf("Executing %v", srv.Name, cmd.Args) return cmd.Run() }
func TestDistSetup(t *testing.T) { if len(flagDist) == 0 { t.Skip("No distribution provided with -dist flag, skipping test") } if len(flagVersion) == 0 { t.Skip("No expected version provided with -version flag, skipping test") } stable, err := CreateStable(".") if err != nil { t.Errorf("CreateStable: error returned, none expected: %v", err.Error()) return } if _, err := stable.AddDist("invalid-name"); err == nil { t.Errorf("AddDist: expected error, didn't got one") return } dist, err := stable.AddDist(flagDist) if err != nil { t.Fatalf("Error returned, none expected:\n%s", err.Error()) } if dist.Version != flagVersion { t.Errorf("Version %v expected, was %v", flagVersion, dist.Version) } log.Debugf("Distribution with name %q created", dist.Name) if len(dist.Name) == 0 { t.Errorf("Name was expected, none assigned") } if len(stable.Distro) != 1 { t.Errorf("Number of registered distributions is %v, expected 1", len(stable.Distro)) } if stable.Distro[dist.Name] != dist { t.Errorf("Distribution not registered correctly") } stable.Destroy() }
The command will open a prompt to that server.`, Synopsis: "[ OPTION ] SERVER", Body: func(ctx *cmd.Context, cmd *cmd.Command, args []string) error { // Find matching servers servers, err := ctx.Stable.FindMatchingServers(args[0:1]) if err != nil { return err } else if len(servers) == 0 { return fmt.Errorf("No servers matching %q", args[0]) } else if len(servers) > 1 { return fmt.Errorf("Pattern %q match more than one server", args[0]) } log.Debugf("Found matching servers %v", servers) // Providing more than one server and not a command is // not allowed. We don't support sending SQL to // multiple servers using a command prompt (yet). if len(args) == 1 && len(servers) > 1 { return ErrTooManyServers } return servers[0].Connect() }, Init: func(cmd *cmd.Command) { cmd.Flags.String("database", "test", "Database to use when connecting") }, }