Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
chain33-pai
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
szh
chain33-pai
Commits
6251eb0b
Commit
6251eb0b
authored
Dec 19, 2019
by
szh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加external接口 添加进度条接口
parent
914999d0
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
112 additions
and
4 deletions
+112
-4
README.md
README.md
+42
-0
process.go
pkg/app/process.go
+13
-4
util.go
pkg/util/util.go
+14
-0
pai.go
routers/api/v1/pai.go
+38
-0
router.go
routers/router.go
+5
-0
No files found.
README.md
View file @
6251eb0b
...
@@ -214,6 +214,48 @@ response
...
@@ -214,6 +214,48 @@ response
"data": ""
"data": ""
}
}
```
```
### 获取下载更新文件进度 内网接口
URL /pai/downloadpercent
请求方法 post json
|参数|类型|是否必填|说明|
|-|-|-|-|
```
response
{
"code": 200,
"msg": "ok",
"data": {
"total":123
"current":121
}
}
```
### 获取树莓派外网ip 内网接口
URL /pai/external
请求方法 post json
|参数|类型|是否必填|说明|
|-|-|-|-|
```
response
{
"code": 200,
"msg": "ok",
"data": "x.x.x.x"
}
```
## 节点部署:
## 节点部署:
监控程序代码默认放在/home/pi目录下面
监控程序代码默认放在/home/pi目录下面
...
...
pkg/app/process.go
View file @
6251eb0b
...
@@ -238,19 +238,27 @@ func MonitorServer() error {
...
@@ -238,19 +238,27 @@ func MonitorServer() error {
return
nil
return
nil
}
}
var
DPercent
DownloadPercent
type
PReader
struct
{
type
DownloadPercent
struct
{
Total
int64
Current
int64
Flag
bool
}
type
Reader
struct
{
io
.
Reader
io
.
Reader
Total
int64
Total
int64
Current
int64
Current
int64
}
}
func
(
r
*
PReader
)
PReader
(
p
[]
byte
)
(
n
int
,
err
error
)
{
func
(
r
*
Reader
)
Read
(
p
[]
byte
)
(
n
int
,
err
error
)
{
n
,
err
=
r
.
Read
(
p
)
n
,
err
=
r
.
Read
er
.
Read
(
p
)
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
err
return
0
,
err
}
}
r
.
Current
+=
int64
(
n
)
r
.
Current
+=
int64
(
n
)
DPercent
.
Current
+=
int64
(
n
)
log
.
Printf
(
"
\r
进度%.2f%%"
,
float64
(
r
.
Current
*
100000
/
r
.
Total
)
/
100
)
log
.
Printf
(
"
\r
进度%.2f%%"
,
float64
(
r
.
Current
*
100000
/
r
.
Total
)
/
100
)
return
return
}
}
...
@@ -271,7 +279,8 @@ func DownLoadFile(url string,file string) error {
...
@@ -271,7 +279,8 @@ func DownLoadFile(url string,file string) error {
defer
func
(){
defer
func
(){
f
.
Close
()
f
.
Close
()
}()
}()
reader
:=
&
PReader
{
DPercent
.
Total
=
r
.
ContentLength
reader
:=
&
Reader
{
Reader
:
r
.
Body
,
Reader
:
r
.
Body
,
Total
:
r
.
ContentLength
,
Total
:
r
.
ContentLength
,
}
}
...
...
pkg/util/util.go
View file @
6251eb0b
...
@@ -4,6 +4,8 @@ import (
...
@@ -4,6 +4,8 @@ import (
"chain33-pai/pkg/setting"
"chain33-pai/pkg/setting"
"net"
"net"
"errors"
"errors"
"net/http"
"io/ioutil"
)
)
// Setup Initialize the util
// Setup Initialize the util
...
@@ -37,4 +39,15 @@ func GetLocalIP() (ipv4 *net.IPNet, err error) {
...
@@ -37,4 +39,15 @@ func GetLocalIP() (ipv4 *net.IPNet, err error) {
err
=
errors
.
New
(
"ERR_NO_LOCAL_IP_FOUND"
)
err
=
errors
.
New
(
"ERR_NO_LOCAL_IP_FOUND"
)
return
return
}
func
GetExternal
()
(
string
,
error
)
{
resp
,
err
:=
http
.
Get
(
"http://myexternalip.com/raw"
)
if
err
!=
nil
{
return
""
,
err
}
defer
resp
.
Body
.
Close
()
content
,
_
:=
ioutil
.
ReadAll
(
resp
.
Body
)
return
string
(
content
),
nil
}
}
\ No newline at end of file
routers/api/v1/pai.go
View file @
6251eb0b
...
@@ -9,6 +9,7 @@ import (
...
@@ -9,6 +9,7 @@ import (
"chain33-pai/pkg/setting"
"chain33-pai/pkg/setting"
"os/exec"
"os/exec"
"log"
"log"
"chain33-pai/pkg/util"
)
)
func
GetDevdetail
(
c
*
gin
.
Context
)
{
func
GetDevdetail
(
c
*
gin
.
Context
)
{
...
@@ -54,6 +55,12 @@ func GetPaiVersion(c *gin.Context) {
...
@@ -54,6 +55,12 @@ func GetPaiVersion(c *gin.Context) {
//下载地址阿里云oss地址 固定ip格式+/+chain33-pai+x.x.x+.tar.gz
//下载地址阿里云oss地址 固定ip格式+/+chain33-pai+x.x.x+.tar.gz
func
UpdatePai
(
c
*
gin
.
Context
)
{
func
UpdatePai
(
c
*
gin
.
Context
)
{
appG
:=
app
.
Gin
{
C
:
c
}
appG
:=
app
.
Gin
{
C
:
c
}
if
app
.
DPercent
.
Flag
{
appG
.
Response
(
http
.
StatusOK
,
e
.
DOWNLOAD_ERROR
,
"正在升级中,不要重复点击"
)
return
}
app
.
DPercent
.
Total
=
int64
(
0
)
app
.
DPercent
.
Current
=
int64
(
0
)
var
req
pai_service
.
ReqUpdatePai
var
req
pai_service
.
ReqUpdatePai
err
:=
c
.
ShouldBindJSON
(
&
req
)
err
:=
c
.
ShouldBindJSON
(
&
req
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -66,6 +73,7 @@ func UpdatePai(c *gin.Context) {
...
@@ -66,6 +73,7 @@ func UpdatePai(c *gin.Context) {
err
=
app
.
DownLoadFile
(
url
,
name
)
err
=
app
.
DownLoadFile
(
url
,
name
)
if
err
!=
nil
{
if
err
!=
nil
{
appG
.
Response
(
http
.
StatusOK
,
e
.
DOWNLOAD_ERROR
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
DOWNLOAD_ERROR
,
err
)
app
.
DPercent
.
Flag
=
false
return
return
}
}
//解压缩文件
//解压缩文件
...
@@ -74,12 +82,14 @@ func UpdatePai(c *gin.Context) {
...
@@ -74,12 +82,14 @@ func UpdatePai(c *gin.Context) {
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
"tar"
,
err
)
log
.
Println
(
"tar"
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
TAR_XVF_ERROR
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
TAR_XVF_ERROR
,
err
)
app
.
DPercent
.
Flag
=
false
return
return
}
}
err
=
tar
.
Wait
()
err
=
tar
.
Wait
()
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
"tar"
,
err
)
log
.
Println
(
"tar"
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
TAR_XVF_ERROR
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
TAR_XVF_ERROR
,
err
)
app
.
DPercent
.
Flag
=
false
return
return
}
}
//备份原文件 确保没有alias 关联
//备份原文件 确保没有alias 关联
...
@@ -88,12 +98,14 @@ func UpdatePai(c *gin.Context) {
...
@@ -88,12 +98,14 @@ func UpdatePai(c *gin.Context) {
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
"bak"
,
err
)
log
.
Println
(
"bak"
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
CP_ERROR
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
CP_ERROR
,
err
)
app
.
DPercent
.
Flag
=
false
return
return
}
}
err
=
bak
.
Wait
()
err
=
bak
.
Wait
()
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
"bak"
,
err
)
log
.
Println
(
"bak"
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
CP_ERROR
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
CP_ERROR
,
err
)
app
.
DPercent
.
Flag
=
false
return
return
}
}
//备份配置文件
//备份配置文件
...
@@ -102,12 +114,14 @@ func UpdatePai(c *gin.Context) {
...
@@ -102,12 +114,14 @@ func UpdatePai(c *gin.Context) {
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
"bakconf"
,
err
)
log
.
Println
(
"bakconf"
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
CP_ERROR
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
CP_ERROR
,
err
)
app
.
DPercent
.
Flag
=
false
return
return
}
}
err
=
bakconf
.
Wait
()
err
=
bakconf
.
Wait
()
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
"bakconf"
,
err
)
log
.
Println
(
"bakconf"
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
CP_ERROR
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
CP_ERROR
,
err
)
app
.
DPercent
.
Flag
=
false
return
return
}
}
//替换原执行文件
//替换原执行文件
...
@@ -116,12 +130,14 @@ func UpdatePai(c *gin.Context) {
...
@@ -116,12 +130,14 @@ func UpdatePai(c *gin.Context) {
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
"mv"
,
err
)
log
.
Println
(
"mv"
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
MV_ERROR
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
MV_ERROR
,
err
)
app
.
DPercent
.
Flag
=
false
return
return
}
}
err
=
mv
.
Wait
()
err
=
mv
.
Wait
()
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
"mv"
,
err
)
log
.
Println
(
"mv"
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
MV_ERROR
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
MV_ERROR
,
err
)
app
.
DPercent
.
Flag
=
false
return
return
}
}
//对比配置文件 比较麻烦
//对比配置文件 比较麻烦
...
@@ -132,12 +148,33 @@ func UpdatePai(c *gin.Context) {
...
@@ -132,12 +148,33 @@ func UpdatePai(c *gin.Context) {
err
=
remove
.
Start
()
err
=
remove
.
Start
()
if
err
!=
nil
{
if
err
!=
nil
{
appG
.
Response
(
http
.
StatusOK
,
e
.
RM_ERROR
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
RM_ERROR
,
err
)
app
.
DPercent
.
Flag
=
false
return
return
}
}
err
=
remove
.
Wait
()
err
=
remove
.
Wait
()
if
err
!=
nil
{
if
err
!=
nil
{
appG
.
Response
(
http
.
StatusOK
,
e
.
RM_ERROR
,
err
)
appG
.
Response
(
http
.
StatusOK
,
e
.
RM_ERROR
,
err
)
app
.
DPercent
.
Flag
=
false
return
return
}
}
app
.
DPercent
.
Flag
=
false
appG
.
Response
(
http
.
StatusOK
,
e
.
SUCCESS
,
nil
)
appG
.
Response
(
http
.
StatusOK
,
e
.
SUCCESS
,
nil
)
}
func
GetDPercent
(
c
*
gin
.
Context
)
{
appG
:=
app
.
Gin
{
C
:
c
}
appG
.
Response
(
http
.
StatusOK
,
e
.
SUCCESS
,
gin
.
H
{
"total"
:
app
.
DPercent
.
Total
,
"current"
:
app
.
DPercent
.
Current
,
})
}
func
GetExternal
(
c
*
gin
.
Context
)
{
appG
:=
app
.
Gin
{
C
:
c
}
ip
,
err
:=
util
.
GetExternal
()
if
err
!=
nil
{
appG
.
Response
(
http
.
StatusOK
,
e
.
ERROR
,
"get external ip err"
)
return
}
appG
.
Response
(
http
.
StatusOK
,
e
.
SUCCESS
,
ip
)
}
}
\ No newline at end of file
routers/router.go
View file @
6251eb0b
...
@@ -29,6 +29,11 @@ func InitRouter() *gin.Engine {
...
@@ -29,6 +29,11 @@ func InitRouter() *gin.Engine {
apiv1
.
POST
(
"/paiversion"
,
v1
.
GetPaiVersion
)
apiv1
.
POST
(
"/paiversion"
,
v1
.
GetPaiVersion
)
//本程序下载新版本
//本程序下载新版本
apiv1
.
POST
(
"/updatepai"
,
v1
.
UpdatePai
)
apiv1
.
POST
(
"/updatepai"
,
v1
.
UpdatePai
)
//获取下载进度
apiv1
.
POST
(
"/dpercent"
,
v1
.
GetDPercent
)
//获取外网ip
apiv1
.
POST
(
"/external"
,
v1
.
GetExternal
)
//
return
r
return
r
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment