/
plugins.go
157 lines (125 loc) · 3.64 KB
/
plugins.go
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
// Package plugins implements etcd plugins.
package plugins
import (
"bytes"
"fmt"
"strconv"
"strings"
"text/template"
semver "github.com/gyuho/semver/v3"
)
// CreateInstallScript returns the etcd install script for Linux.
// Inputs are either: "v3.2.12", "master", "123" (PR number).
func CreateInstallScript(ver string) (s string, err error) {
v, err := semver.Make(ver)
if err == nil {
return createInstallRelase(etcdInfo{Version: v.String()})
}
if err != nil && strings.Contains(err.Error(), "Invalid character(s) found in major") && strings.HasPrefix(ver, "v") {
ver = ver[1:]
v, err = semver.Make(ver)
if err == nil {
return createInstallRelase(etcdInfo{Version: v.String()})
}
}
_, perr := strconv.ParseInt(ver, 10, 64)
isPR := perr == nil
return createInstallGit(gitInfo{
GitRepo: "etcd",
GitClonePath: "${GOPATH}/src/go.etcd.io",
GitCloneURL: "https://github.com/etcd-io/etcd.git",
IsPR: isPR,
GitBranch: ver,
InstallScript: `./build
sudo cp ./bin/etcd /usr/bin/etcd
sudo cp ./bin/etcdctl /usr/bin/etcdctl
/usr/bin/etcd --version
ETCDCTL_API=3 /usr/bin/etcdctl version`,
})
}
func createInstallRelase(g etcdInfo) (string, error) {
tpl := template.Must(template.New("installRelease").Parse(installRelease))
buf := bytes.NewBuffer(nil)
if err := tpl.Execute(buf, g); err != nil {
return "", err
}
return buf.String(), nil
}
type etcdInfo struct {
Version string
}
const installRelease = `
################################## install etcd
sudo systemctl stop etcd.service || true
ETCD_VER=v{{ .Version }}
# choose either URL
GOOGLE_URL=https://storage.googleapis.com/etcd
GITHUB_URL=https://github.com/etcd-io/etcd/releases/download
DOWNLOAD_URL=${GOOGLE_URL}
rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
rm -rf /tmp/etcd-download-test && mkdir -p /tmp/etcd-download-test
curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/etcd-download-test --strip-components=1
rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
sudo cp /tmp/etcd-download-test/etcd /usr/bin/etcd
sudo cp /tmp/etcd-download-test/etcdctl /usr/bin/etcdctl
/usr/bin/etcd --version
ETCDCTL_API=3 /usr/bin/etcdctl version
##################################
`
func createInstallGit(g gitInfo) (string, error) {
if g.IsPR {
_, serr := strconv.ParseInt(g.GitBranch, 10, 64)
if serr != nil {
return "", fmt.Errorf("expected PR number, got %q (%v)", g.GitBranch, serr)
}
}
tpl := template.Must(template.New("installGit").Parse(installGit))
buf := bytes.NewBuffer(nil)
if err := tpl.Execute(buf, g); err != nil {
return "", err
}
return buf.String(), nil
}
type gitInfo struct {
GitRepo string
GitClonePath string
GitCloneURL string
IsPR bool
// GitBranch name or PR number
GitBranch string
InstallScript string
}
const installGit = `
################################## install {{ .GitRepo }} via git
mkdir -p {{ .GitClonePath }}/
cd {{ .GitClonePath }}/
RETRIES=10
DELAY=10
COUNT=1
while [[ ${COUNT} -lt ${RETRIES} ]]; do
rm -rf ./{{ .GitRepo }}
git clone {{ .GitCloneURL }}
if [[ $? -eq 0 ]]; then
RETRIES=0
echo "Successfully git cloned!"
break
fi
let COUNT=${COUNT}+1
sleep ${DELAY}
done
cd {{ .GitClonePath }}/{{ .GitRepo }}
{{ if .IsPR }}echo 'git fetching:' pull/{{ .GitBranch }}/head 'to test branch'
git fetch origin pull/{{ .GitBranch }}/head:test
git checkout test
{{ else }}
git checkout origin/{{ .GitBranch }}
git checkout -B {{ .GitBranch }}
{{ end }}
git remote -v
git branch
git log --pretty=oneline -5
pwd
{{ .InstallScript }}
##################################
`