func (zkd *Zkd) makeCfg() (string, error) { root, err := env.VtRoot() if err != nil { return "", err } cnfTemplatePaths := []string{path.Join(root, "config/zkcfg/zoo.cfg")} return MakeZooCfg(cnfTemplatePaths, zkd.config, "# generated by vt") }
// Init will create the default directory structure for the mysqld process, // generate / configure a my.cnf file, unpack a skeleton database, // and create some management tables. func (mysqld *Mysqld) Init(mysqlWaitTime time.Duration, bootstrapArchive string, skipSchema bool) error { log.Infof("mysqlctl.Init") err := mysqld.createDirs() if err != nil { log.Errorf("%s", err.Error()) return err } root, err := vtenv.VtRoot() if err != nil { log.Errorf("%s", err.Error()) return err } // Set up config files. if err = mysqld.initConfig(root); err != nil { log.Errorf("failed creating %v: %v", mysqld.config.path, err) return err } // Unpack bootstrap DB files. dbTbzPath := path.Join(root, "data/bootstrap/"+bootstrapArchive) log.Infof("decompress bootstrap db %v", dbTbzPath) args := []string{"-xj", "-C", mysqld.TabletDir, "-f", dbTbzPath} if _, err = execCmd("tar", args, []string{}, ""); err != nil { log.Errorf("failed unpacking %v: %v", dbTbzPath, err) return err } // Start mysqld. if err = mysqld.Start(mysqlWaitTime); err != nil { log.Errorf("failed starting, check %v", mysqld.config.ErrorLogPath) return err } // Load initial schema. if skipSchema { return nil } schemaPath := path.Join(root, "data/bootstrap/_vt_schema.sql") schema, err := ioutil.ReadFile(schemaPath) if err != nil { return err } sqlCmds := make([]string, 0, 10) log.Infof("initial schema: %v", string(schema)) for _, cmd := range strings.Split(string(schema), ";") { cmd = strings.TrimSpace(cmd) if cmd == "" { continue } sqlCmds = append(sqlCmds, cmd) } return mysqld.ExecuteSuperQueryList(sqlCmds) }
/* ZOO_LOG_DIR="" ZOO_CFG="/.../zoo.cfg" ZOOMAIN="org.apache.zookeeper.server.quorum.QuorumPeerMain" java -DZOO_LOG_DIR=${ZOO_LOG_DIR} -cp $CLASSPATH $ZOOMAIN $YT_ZK_CFG */ func (zkd *Zkd) Start() error { log.Infof("zkctl.Start") // NOTE(msolomon) use a script here so we can detach and continue to run // if the wrangler process dies. this pretty much the same as mysqld_safe. args := []string{ zkd.config.LogDir(), zkd.config.ConfigFile(), zkd.config.PidFile(), } root, err := env.VtRoot() if err != nil { return err } dir := path.Join(root, "bin") cmd := exec.Command(path.Join(dir, "zksrv.sh"), args...) cmd.Env = os.Environ() cmd.Dir = dir if err = cmd.Start(); err != nil { return err } // give it some time to succeed - usually by the time the socket emerges // we are in good shape for i := 0; i < StartWaitTime; i++ { zkAddr := fmt.Sprintf(":%v", zkd.config.ClientPort) conn, connErr := net.Dial("tcp", zkAddr) if connErr != nil { err = connErr time.Sleep(time.Second) continue } else { err = nil conn.Write([]byte("ruok")) reply := make([]byte, 4) conn.Read(reply) if string(reply) != "imok" { err = fmt.Errorf("local zk unhealthy: %v %v", zkAddr, reply) } conn.Close() break } } zkd.done = make(chan struct{}) go func(done chan<- struct{}) { // wait so we don't get a bunch of defunct processes cmd.Wait() close(done) }(zkd.done) return err }
func TestMycnf(t *testing.T) { os.Setenv("MYSQL_FLAVOR", "GoogleMysql") dbaConfig := dbconfigs.DefaultDBConfigs.Dba replConfig := dbconfigs.DefaultDBConfigs.Repl tablet0 := NewMysqld("Dba", NewMycnf(0, 6802), &dbaConfig, &replConfig) defer tablet0.Close() root, err := env.VtRoot() if err != nil { t.Errorf("err: %v", err) } cnfTemplatePaths := []string{ path.Join(root, "src/github.com/henryanand/vitess/config/mycnf/default.cnf"), path.Join(root, "src/github.com/henryanand/vitess/config/mycnf/replica.cnf"), path.Join(root, "src/github.com/henryanand/vitess/config/mycnf/master.cnf"), } data, err := tablet0.config.makeMycnf(cnfTemplatePaths) if err != nil { t.Errorf("err: %v", err) } else { t.Logf("data: %v", data) } err = ioutil.WriteFile(MYCNF_PATH, []byte(data), 0666) if err != nil { t.Errorf("failed creating my.cnf %v", err) } _, err = ioutil.ReadFile(MYCNF_PATH) if err != nil { t.Errorf("failed reading, err %v", err) return } mycnf, err := ReadMycnf(MYCNF_PATH) if err != nil { t.Errorf("failed reading, err %v", err) } else { t.Logf("socket file %v", mycnf.SocketFile) } }
func (hook *Hook) Execute() (result *HookResult) { result = &HookResult{} // also check for bad string here on the server side, to be sure if strings.Contains(hook.Name, "/") { result.ExitStatus = HOOK_INVALID_NAME result.Stderr = "Hooks cannot contains '/'\n" return result } // find our root root, err := vtenv.VtRoot() if err != nil { result.ExitStatus = HOOK_VTROOT_ERROR result.Stdout = "Cannot get VTROOT: " + err.Error() + "\n" return result } // see if the hook exists vthook := path.Join(root, "vthook", hook.Name) _, err = os.Stat(vthook) if err != nil { if os.IsNotExist(err) { result.ExitStatus = HOOK_DOES_NOT_EXIST result.Stdout = "Skipping missing hook: " + vthook + "\n" return result } result.ExitStatus = HOOK_STAT_FAILED result.Stderr = "Cannot stat hook: " + vthook + ": " + err.Error() + "\n" return result } // run it log.Infof("hook: executing hook: %v %v", vthook, strings.Join(hook.Parameters, " ")) cmd := exec.Command(vthook, hook.Parameters...) if len(hook.ExtraEnv) > 0 { cmd.Env = os.Environ() for key, value := range hook.ExtraEnv { cmd.Env = append(cmd.Env, key+"="+value) } } var stdout, stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr err = cmd.Run() result.Stdout = stdout.String() result.Stderr = stderr.String() if err == nil { result.ExitStatus = HOOK_SUCCESS } else { if cmd.ProcessState != nil && cmd.ProcessState.Sys() != nil { result.ExitStatus = cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() } else { result.ExitStatus = HOOK_CANNOT_GET_EXIT_STATUS } result.Stderr += "ERROR: " + err.Error() + "\n" } log.Infof("hook: result is %v", result.String()) return result }