package main import ( "fmt" "log" "net/http" "os" "os/exec" "time" ) func main() { lockdir:=os.Getenv("TSZDIR") exitChannel := make(chan int, 10) //线程启动ipfs fmt.Println("start ipfs") datapath := lockdir+"go-ipfs_v0.7.0_windows-amd64\\go-ipfs\\ipfs.exe" ipfsCd := exec.Command(datapath,"daemon") go func() { err := ipfsCd.Run() if err != nil { fmt.Print("ipfs") fmt.Println(err) exitChannel <- 1 } }() //线程启动fts fmt.Println("start fts") datapath = lockdir+"bin\\fts.exe" ftsCd := exec.Command(datapath) go func() { err := ftsCd.Run() if err != nil { fmt.Print("fts") fmt.Println(err) exitChannel <- 1 } }() //线程启动etcd fmt.Println("start etcd") datadir :=os.Getenv("APPDATA")+"\\"+"LOCKING" datapath = lockdir+"etcd-v3.3.25-windows-amd64\\etcd.exe" etcdCd := exec.Command(datapath,"--data-dir",datadir) go func() { err := etcdCd.Run() if err != nil { fmt.Print("etcd") fmt.Println(err) exitChannel <- 1 } }() //检测fts进程是否启动成功,最大超时5s timeOutMax := 50 timeOutIndex := 0 for true { resp, err := http.Get("http://127.0.0.1:7777") if err != nil { if timeOutIndex>timeOutMax{ fmt.Printf("http.Get()函数执行错误,错误为:%v\n", err) exitChannel <- 1 } time.Sleep(100*time.Millisecond) timeOutIndex++ continue } defer resp.Body.Close() break } //检测etcd进程是否启动成功,最大超时5s timeOutMax = 50 timeOutIndex = 0 for true { resp, err := http.Get("http://127.0.0.1:2379") if err != nil { if timeOutIndex>timeOutMax{ fmt.Printf("http.Get()函数执行错误,错误为:%v\n", err) exitChannel <- 1 } time.Sleep(100*time.Millisecond) timeOutIndex++ continue } defer resp.Body.Close() break } //检测ipfs进程是否启动成功,最大超时10s timeOutMax = 50 timeOutIndex = 0 for true { resp, err := http.Get("http://127.0.0.1:5001") if err != nil { if timeOutIndex>timeOutMax{ fmt.Printf("http.Get()函数执行错误,错误为:%v\n", err) exitChannel <- 1 } time.Sleep(200*time.Millisecond) timeOutIndex++ continue } defer resp.Body.Close() break } //启动客户端 datapath = lockdir+"locking-client-win32-x64\\locking-client.exe" lockingCd := exec.Command(datapath) err := lockingCd.Start() fmt.Println("start locking") log.Println(lockingCd.Process.Pid) if err != nil { fmt.Println(err) exitChannel <- 1 } /*go func() { for true{ log.Println(cd.Process.Pid)//client 存活情况 } }()*/ //收取退出命令 for i := range exitChannel { if i==1{ if ftsCd.Process !=nil{ ftsCd.Process.Kill() } if ipfsCd.Process !=nil{ ipfsCd.Process.Kill() } if etcdCd.Process !=nil{ etcdCd.Process.Kill() } if lockingCd.Process !=nil{ lockingCd.Process.Kill() } os.Exit(1) } } os.Exit(1) }