Commit 4806eba6 authored by szh's avatar szh

升级版本以及重置钱包操作

parent 78b9cc90
......@@ -4,3 +4,5 @@
runtime/*
!runtime/qrcode/bg.jpg
!runtime/fonts
*.log
*.out
......@@ -29,14 +29,28 @@ WriteTimeout = 60
[database]
Type = mysql
User = root
Password = rootroot
Host = 127.0.0.1:3306
Password = 199837
Host = 39.106.117.88:3306
Name = blog
TablePrefix = blog_
[redis]
Host = 127.0.0.1:6379
Host = 39.106.117.88:6379
Password =
MaxIdle = 30
MaxActive = 30
IdleTimeout = 200
[bityuan]
Name=bityuan
Path=/media/pi/bityuan/
GitPath=https://github.com/bityuan/bityuan.git
VersionPath=https://raw.githubusercontent.com/bityuan/bityuan/master/bityuan.go
[chain33-pai]
Name=chain33-pai
UpdateDir=/home/pi/
Path=/home/pi/chain33-pai/
Auto=/home/pi/chain33-pai/auto.sh
Start=/home/pi/chain33-pai/star.sh
StartUp=/home/pi/chain33-pai/startup.sh
package main
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
"chain33-pai/pkg/app"
"chain33-pai/pkg/logging"
"chain33-pai/pkg/setting"
"chain33-pai/routers"
"chain33-pai/pkg/util"
"chain33-pai/routers"
"chain33-pai/service/pai_service"
"fmt"
"github.com/gin-gonic/gin"
"log"
"net"
"net/http"
"time"
"chain33-pai/service/pai_service"
)
func init() {
......@@ -32,6 +32,7 @@ func init() {
func main() {
gin.SetMode(setting.ServerSetting.RunMode)
go broadcast()
go app.CornProcessJob(time.NewTicker(time.Second*10))
routersInit := routers.InitRouter()
readTimeout := setting.ServerSetting.ReadTimeout
writeTimeout := setting.ServerSetting.WriteTimeout
......@@ -49,8 +50,6 @@ func main() {
log.Printf("[info] start http server listening %s", endPoint)
server.ListenAndServe()
select {}
// If you want Graceful Restart, you need a Unix system and download github.com/fvbock/endless
//endless.DefaultReadTimeOut = readTimeout
......
package app
import (
"bufio"
"bytes"
"chain33-pai/pkg/setting"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path"
"regexp"
"strings"
"time"
)
type ProcessInfo struct {
PName string `json:"p_name"`
Pid string `json:"pid"`
User string `json:"user"`
Path string `json:"path"`
}
type updateInfo struct {
Status bool `json:"status"`
Info string `json:"info"`
}
var Wallet ProcessInfo
var UpdateInfo updateInfo
var NodeError error
func getProcessInfo(keyfile string)string{
var buffer bytes.Buffer //
var buffer_res bytes.Buffer //
lsof := exec.Command("ps","aux")
grep := exec.Command("grep", keyfile)
head := exec.Command("grep", "-v", "grep")
read, write := io.Pipe() // Create a pipe
defer read.Close()
defer write.Close()
lsof.Stdout = write //write output into pipe write
grep.Stdin = read // grep read from pipe
grep.Stdout = &buffer // grep output
lsof.Start()
grep.Start()
lsof.Wait()
write.Close()
grep.Wait()
head.Stdin = &buffer
head.Stdout = &buffer_res
head.Start()
head.Wait()
return buffer_res.String()
}
func getWalletInfo()error{
rawProcessInfo:=getProcessInfo(setting.BityuanSetting.Name)
log.Println(rawProcessInfo)
if rawProcessInfo==""{
StartProcess(setting.Chain33Pai.Auto)
var buffer bytes.Buffer
cmd:=exec.Command("find","/media","-name",setting.BityuanSetting.Name)
cmd.Stdout=&buffer
cmd.Start()
cmd.Wait()
if buffer.String()==""{
log.Println("node dose not exist")
return fmt.Errorf("node dose not exist")
}else{
fmt.Println(buffer.String())
rawProcessInfo=getProcessInfo(setting.BityuanSetting.Name)
if rawProcessInfo==""{
log.Println("node down")
return errors.New("node down")
}
}
}
record:=StrFilter(strings.Split(rawProcessInfo," "),"")
{
Wallet.User=record[0]
Wallet.Pid=record[1]
Wallet.Path=record[10]
Wallet.PName=path.Base(Wallet.Path)
}
return nil
}
func StrFilter(str []string,filter string)(res []string){
for i:=0;i<len(str);i++{
if str[i]!=filter{
res=append(res,str[i])
}
}
return
}
//corn job for collecting chain33 process info
func CornProcessJob(ticker *time.Ticker){
NodeError = getWalletInfo()
for {
select {
case <-ticker.C:
NodeError = getWalletInfo()
}
}
}
func GetLatestVersion()string{
resp, _ := http.Get(setting.BityuanSetting.VersionPath)
rd:=bufio.NewReader(resp.Body)
for{
line,err:=rd.ReadString('\n')
if err==io.EOF{
break
}
if ok, _ := regexp.MatchString("version", line); ok {
return strings.Split( strings.Split(line, "=")[1], "\"")[1]
break
}
}
return ""
}
func DownLoadLatestVersion()error{
git:=exec.Command("git","clone",setting.BityuanSetting.GitPath,"./"+setting.BityuanSetting.Name)
sed:=exec.Command("sed", fmt.Sprintf("15,18 s/bityuan/",setting.BityuanSetting.Name,"/1") ,"Makefile")
makeb:=exec.Command("make")
git.Start()
err:=git.Wait()
if err!=nil{
log.Println(err)
return errors.New("download latest version failed")
}
os.Chdir("./"+setting.BityuanSetting.Name)
sed.Start()
sed.Wait()
makeb.Start()
err=makeb.Wait()
if err!=nil {
log.Println(err)
return errors.New("failed to compile latest version for some reason")
}
return nil
}
func Copy(src,dst string){
cp:=exec.Command("cp","-r",src,dst)
cp.Start()
cp.Wait()
}
func StartProcess(scriptsAddr string)(err error){
cmd:=exec.Command(scriptsAddr)
err=cmd.Start()
if err!=nil{
log.Println(err)
return
}
err=cmd.Wait()
if err!=nil{
log.Println(err)
return
}
return
}
package app
import (
"bytes"
"io"
"os/exec"
"time"
)
type ProcessInfo struct {
PName string `json:"p_name"`
Pid string `json:"pid"`
User string `json:"user"`
Path string `json:"path"`
}
func GetProcessInfo(keyfile string)bytes.Buffer{
var buffer bytes.Buffer
var buffer_res bytes.Buffer
lsof := exec.Command("lsof")
grep := exec.Command("grep", keyfile)
head := exec.Command("head","-n","1")
read, write := io.Pipe() // Create a pipe
defer read.Close()
defer write.Close()
lsof.Stdout = write //write output into pipe write
grep.Stdin = read // grep read from pipe
grep.Stdout = &buffer// grep output
lsof.Start()
grep.Start()
lsof.Wait()
write.Close()
grep.Wait()
head.Stdin=&buffer
head.Stdout=&buffer_res
head.Start()
head.Wait()
return buffer_res
}
func StrFilter(str []string)(res []string){
for i:=0;i<len(str);i++{
if str[i]!=""{
res=append(res,str[i])
}
}
return
}
func CornProcessJob(times ){
}
\ No newline at end of file
##### 检测节点的异常信息
* 找不到wallet.db
- 找v7-bityuan,找到说明没问题
× 找到wallet.db找不到对应的
###### 钱包接口
× 节点在运行,找到进程,找到钱包,杀死进程删除钱包重启
× 节点未运行 
###### 错误约定
借口执行可能的错误有:
1 系统命令调用错误
通用命令调用错误如:
2 磁盘问题  
找不到wallet.db则寻找v7-bityuan,找到则顺利执行,找不到---->节点重启失败-->属于磁盘问题
\ No newline at end of file
package chain33
import (
"github.com/33cn/chain33/types"
"google.golang.org/grpc"
"context"
"google.golang.org/grpc"
"github.com/33cn/chain33/types"
)
type PaiClient struct {
......@@ -12,6 +13,7 @@ type PaiClient struct {
var (
paiClient types.Chain33Client
paiNetgrpcAddr = "localhost:8802"
)
......@@ -39,3 +41,7 @@ func (p *PaiClient) IsNtpClockSync() (*types.Reply,error) {
func (p *PaiClient) GetNetInfo() (*types.NodeNetInfo,error) {
return paiClient.NetInfo(context.Background(),&types.ReqNil{})
}
func (p *PaiClient) Version()(*types.VersionInfo,error){
return paiClient.Version(context.Background(),&types.ReqNil{})
}
\ No newline at end of file
......@@ -3,10 +3,14 @@ package setting
import (
"log"
"time"
"os/exec"
"chain33-pai/pkg/chain33"
"github.com/go-ini/ini"
)
var PaiClient *chain33.PaiClient
type App struct {
JwtSecret string
PageSize int
......@@ -57,9 +61,26 @@ type Redis struct {
MaxActive int
IdleTimeout time.Duration
}
var RedisSetting = &Redis{}
type Bityuan struct {
Path string
Name string
GitPath string
Version string
VersionPath string
}
var BityuanSetting=&Bityuan{}
type Chain33_pai struct {
Name string
Path string
Auto string
Start string
StartUp string
}
var Chain33Pai=&Chain33_pai{}
var cfg *ini.File
// Setup initialize the configuration instance
......@@ -69,16 +90,33 @@ func Setup() {
if err != nil {
log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
}
mapTo("app", AppSetting)
mapTo("redis", RedisSetting)
mapTo("server", ServerSetting)
mapTo("chain33-pai",Chain33Pai)
mapTo("bityuan",BityuanSetting)
mapTo("database", DatabaseSetting)
mapTo("redis", RedisSetting)
AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024
ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
ServerSetting.WriteTimeout = ServerSetting.WriteTimeout * time.Second
RedisSetting.IdleTimeout = RedisSetting.IdleTimeout * time.Second
}
func FreshVersion(){
version,err:=PaiClient.Version()
if err!=nil{
auto:=exec.Command(Chain33Pai.Auto)
err=auto.Start()
if err != nil {
log.Fatalf("start node failed,need to start manually")
}
err=auto.Wait()
if err != nil {
log.Fatalf("start node failed,need to start manually")
}
}
BityuanSetting.Version=version.App
}
// mapTo map section
......@@ -87,4 +125,5 @@ func mapTo(section string, v interface{}) {
if err != nil {
log.Fatalf("Cfg.MapTo %s err: %v", section, err)
}
}
package v1
import (
"fmt"
"log"
"os"
"path"
"os/exec"
"net/http"
"chain33-pai/pkg/e"
"chain33-pai/pkg/app"
"chain33-pai/pkg/setting"
"github.com/gin-gonic/gin"
)
var flag bool//判断是否正在更新
func ResetWallet(c *gin.Context) {
appG := app.Gin{C: c}
if app.NodeError!=nil{
appG.Response(http.StatusOK, e.ERROR, app.NodeError.Error())
return
}
cmd:=exec.Command("kill","-9",app.Wallet.Pid)
err:=cmd.Start()
if err!=nil{
appG.Response(http.StatusOK, e.ERROR, "fail to reset wallet")
log.Fatalln(err)
return
}
err=os.RemoveAll(fmt.Sprintf(path.Dir(app.Wallet.Path),"/wallet"))
if err!=nil{
appG.Response(http.StatusOK, e.ERROR, "fail to reset wallet")
log.Fatalln(err)
return
}
err=app.StartProcess(setting.Chain33Pai.Auto)
if err!=nil{
appG.Response(http.StatusOK, e.ERROR,"fail to restart node")
log.Fatalln(err)
return
}
appG.Response(http.StatusOK, e.SUCCESS,nil)
}
func UpdateNode(c *gin.Context){
appG := app.Gin{C: c}
if flag{
appG.Response(http.StatusOK,e.SUCCESS,"node is updating")
}
setting.FreshVersion()
app.UpdateInfo.Status=false
app.UpdateInfo.Info="updating..."
latestVersion:=app.GetLatestVersion()
if latestVersion==setting.BityuanSetting.Version{
appG.Response(http.StatusOK,e.SUCCESS,"current version is latest")
return
}
if flag{
appG.Response(http.StatusOK,e.SUCCESS,"node is updating")
return
}
if latestVersion==""{
flag=false
appG.Response(http.StatusOK,e.ERROR,"network error")
return
}
appG.Response(http.StatusOK,e.SUCCESS,"update job started")
go func(){
flag=true
err:=app.DownLoadLatestVersion()
if err!=nil{
flag=false
app.UpdateInfo.Info=err.Error()
return
}
app.Copy(setting.BityuanSetting.Name,setting.BityuanSetting.Path)
app.Copy(setting.BityuanSetting.Name+"-cli",setting.BityuanSetting.Path)
os.Chdir("..")
os.RemoveAll("bityuan")
kill:=exec.Command("kill","-9",app.Wallet.Pid)
kill.Start()
err=kill.Wait()
if err!=nil{
flag=false
app.UpdateInfo.Info="fail to update node "
return
}
err=app.StartProcess(setting.Chain33Pai.Auto)
if err!=nil{
flag=false
app.UpdateInfo.Info="fail to update node "
return
}
app.UpdateInfo.Status=true
app.UpdateInfo.Info="complete"
flag=false
}()
}
func ResetNode(c* gin.Context){
appG := app.Gin{C: c}
app.UpdateInfo.Status=false
app.UpdateInfo.Info="updating..."
if flag{
appG.Response(http.StatusOK,e.SUCCESS,"node is resetting")
return
}
appG.Response(http.StatusOK,e.SUCCESS,"reset job started")
go func() {
flag=true
err := app.DownLoadLatestVersion()
if err != nil {
flag=false
app.UpdateInfo.Info = err.Error()
return
}
app.Copy(setting.BityuanSetting.Name, path.Dir(setting.BityuanSetting.Path))
app.Copy(setting.BityuanSetting.Name+"-cli", path.Dir(setting.BityuanSetting.Path))
os.Chdir("..")
os.RemoveAll("bityuan")
os.Chdir(setting.BityuanSetting.Path)
os.RemoveAll("datadir")
os.Chdir(setting.Chain33Pai.Path)
kill := exec.Command("kill", "-9", app.Wallet.Pid)
//start := exec.Command(path.Dir(app.Wallet.Path) + "/" + app.Wallet.PName)
err=kill.Start()
if err != nil {
app.UpdateInfo.Info = fmt.Sprintf("fail to reset node:%s",err.Error())
return
}
err=kill.Wait()
if err != nil {
flag=false
app.UpdateInfo.Info = fmt.Sprintf("fail to reset node:%s",err.Error())
return
}
err=app.StartProcess(setting.Chain33Pai.Auto)
//err = start.Start()
if err != nil {
flag=false
app.UpdateInfo.Info = fmt.Sprintf("fail to reset node:%s",err.Error())
return
}
app.UpdateInfo.Status=true
app.UpdateInfo.Info = "complete"
flag=false
}()
}
func UpdateDetail(c* gin.Context){
appG := app.Gin{C: c}
appG.Response(http.StatusOK,e.SUCCESS,app.UpdateInfo)
}
//get current version and latest version
func Version(c* gin.Context){
appG := app.Gin{C: c}
appG.Response(http.StatusOK,e.SUCCESS,gin.H{
"current":setting.BityuanSetting.Version,
"latest" :app.GetLatestVersion(),
})
}
func NodeInfo(c *gin.Context){
appG:=app.Gin{C:c}
if app.NodeError!=nil{
appG.Response(http.StatusOK,e.ERROR,app.NodeError.Error())
return
}
appG.Response(http.StatusOK,e.SUCCESS,"node is running")
}
\ No newline at end of file
package v1
import (
"github.com/gin-gonic/gin"
"net/http"
"chain33-pai/pkg/app"
"chain33-pai/pkg/e"
"chain33-pai/pkg/app"
"github.com/gin-gonic/gin"
"chain33-pai/service/pai_service"
)
......@@ -20,7 +20,6 @@ func GetDevdetail(c *gin.Context) {
})
}
func GetDevstatus(c *gin.Context) {
appG := app.Gin{C: c}
var pai pai_service.Pai
......
package v1
import (
"os"
"path"
"os/exec"
"strings"
"net/http"
"chain33-pai/pkg/e"
"chain33-pai/pkg/app"
"github.com/gin-gonic/gin"
"chain33-pai/service/pai_service"
)
func GetDevdetail(c *gin.Context) {
appG := app.Gin{C: c}
var pai pai_service.Pai
ok := pai.GetConfig()
if !ok {
appG.Response(http.StatusOK, e.ERROR, nil)
}
appG.Response(http.StatusOK, e.SUCCESS, map[string]interface{}{
"serial": pai.Serial,
})
}
func GetDevstatus(c *gin.Context) {
appG := app.Gin{C: c}
var pai pai_service.Pai
err := pai.GetDevstatus()
if err != nil {
appG.Response(http.StatusOK, e.ERROR, nil)
}
appG.Response(http.StatusOK, e.SUCCESS, pai)
}
func ResetWallet(c *gin.Context) {
appG := app.Gin{C: c}
buffer:=app.GetProcessInfo("wallet.db")
if buffer.String()==""{
appG.Response(http.StatusInternalServerError, e.ERROR, gin.H{
"info":"node is not running",
})
return
}
record:=app.StrFilter(strings.Split(buffer.String()," "))
wallet:=app.ProcessInfo{
PName:record[0],
Pid: record[1],
User: record[2],
Path: path.Dir(path.Dir(record[8])),
}
cmd:=exec.Command("kill","-9",wallet.Pid)
err:=cmd.Start()
if err!=nil{
appG.Response(http.StatusInternalServerError, e.ERROR, gin.H{
"info":"failed to reset wallet",
})
return
}
err=os.RemoveAll(wallet.Path)
if err!=nil{
appG.Response(http.StatusInternalServerError, e.ERROR, gin.H{
"info":"failed to reset wallet",
})
return
}
cmd=exec.Command(path.Dir( wallet.Path)+"/"+wallet.PName)
err=cmd.Start()
if err!=nil{
appG.Response(http.StatusInternalServerError, e.ERROR, gin.H{
"info":"failed to restart node",
})
return
}
appG.Response(http.StatusOK, e.SUCCESS,nil)
}
......@@ -24,7 +24,13 @@ func InitRouter() *gin.Engine {
//获取树莓派基本信息
apiv1.POST("/devdetail",v1.GetDevdetail)
//获取树莓派基本信息
apiv1.POST("/resetwallet",v1.ResetWallet)
apiv1.POST("/updatenode",v1.UpdateNode)
apiv1.POST("/devstatus",v1.GetDevstatus)
apiv1.POST("/updatedetail",v1.UpdateDetail)
apiv1.POST("/version",v1.Version)
apiv1.POST("/reset",v1.ResetNode)
apiv1.POST("/nodeinfo",v1.NodeInfo)
//apiv1.Use(jwt.JWT())
//{
//
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment