t1
sunty
2020-03-24 c14fefa2903a54298666e1d11df0c5013c51429d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
 
import (
    "bytes"
    "encoding/json"
    "errors"
    "fmt"
    "io/ioutil"
    "net/http"
    "os/exec"
    "strings"
    "time"
)
 
func main() {
 
    oldPeers := GetOldPeers()
    fmt.Println("oldPeers: ", oldPeers)
    //AddNewMasterToPeers()
    newPeers := GetNewPeers()
    fmt.Println("newPeers: ", newPeers)
    UpdatePeers(oldPeers, newPeers)
    time.Sleep(time.Second * 3)
    nowPeers := GetOldPeers()
    fmt.Println("nowPeers: ", nowPeers)
}
 
func GetOldPeers() string {
    str := "cat /opt/vasystem/seaweedfs_start.sh | grep peers="
    peers := RunScript(str)
    return peers
}
 
func GetNewPeers() string {
    getUrl := "http://192.168.20.10:9200/basicfs/_search"
    getJson := `{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "application":"nodeOperation"
                    }
                }
            ]
        }
    },
    "size": 1
}`
 
    buf, _ := EsReq("POST", getUrl, []byte(getJson))
    source, _ := Sourcelist(buf)
    //fmt.Println(source)
    peers := source[0]["peers"].([]interface{})
    fmt.Println(peers)
    p := "peers=" + strings.Replace(strings.Trim(fmt.Sprint(peers), "[]"), " ", ",", -1)
    return p
}
 
func UpdatePeers(oldPeers string, newPeers string) {
    str := "sed -ie 's/" + oldPeers + "/" + newPeers + "/g' /opt/vasystem/seaweedfs_start.sh"
    RunScript(str)
}
 
//脚本封装
func RunScript(str string) string {
 
    cmd := exec.Command("sh", "-c", str)
    var out bytes.Buffer
    cmd.Stdout = &out
    err := cmd.Run()
    if err != nil {
        return "运行失败"
    }
    return out.String()
}
 
//解析http
func EsReq(method string, url string, parama []byte) (buf []byte, err error) {
    timeout := time.Duration(10 * time.Second)
    client := http.Client{
        Timeout: timeout,
    }
    request, err := http.NewRequest(method, url, bytes.NewBuffer(parama))
    request.Header.Set("Content-type", "application/json")
 
    if err != nil {
        fmt.Println("build request fail !")
        return nil, err
    }
 
    resp, err := client.Do(request)
    if err != nil {
        fmt.Println("request error: ", err)
        return nil, err
    }
 
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
        return nil, err
    }
    return body, nil
}
 
func Sourcelist(buf []byte) (sources []map[string]interface{}, err error) {
    var info interface{}
    json.Unmarshal(buf, &info)
    out, ok := info.(map[string]interface{})
    if !ok {
        return nil, errors.New("http response interface can not change map[string]interface{}")
    }
 
    middle, ok := out["hits"].(map[string]interface{})
    if !ok {
        return nil, errors.New("first hits change error!")
    }
    for _, in := range middle["hits"].([]interface{}) {
        tmpbuf, ok := in.(map[string]interface{})
        if !ok {
            fmt.Println("change to source error!")
            continue
        }
        source, ok := tmpbuf["_source"].(map[string]interface{})
        if !ok {
            fmt.Println("change _source error!")
            continue
        }
        sources = append(sources, source)
    }
    return sources, nil
}
 
func AddNewMasterToPeers() (result bool) {
 
    peer := "192.168.5.22:6333"
    addUrl := "http://192.168.20.10:9200/basicfs/_update_by_query"
    addJson := `{
    "script": {
        "lang": "painless",
        "inline": "ctx._source.peers.add(params.newpeer)",
        "params": {
            "newpeer": "` + peer + `"
        }
    },
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "application": "nodeOperation"
                    }
                }
            ]
        }
    }
}`
    buf, _ := EsReq("POST", addUrl, []byte(addJson))
    updateRes, _ := SourceUpdated(buf)
    if updateRes == -1 {
        result = false
    } else {
        result = true
    }
    return result
}
 
func SourceUpdated(buf []byte) (total int, err error) {
    var info interface{}
    json.Unmarshal(buf, &info)
    out, ok := info.(map[string]interface{})
    if !ok {
        return -1, errors.New("http response interface can not change map[string]interface{}")
    }
 
    middle, ok := out["updated"].(float64)
    if !ok {
        return -1, errors.New("first total change error!")
    }
    total = int(middle)
    return total, nil
}