-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (143 loc) · 3.07 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"errors"
"fmt"
"log"
"os"
"strconv"
dit "coreunit.net/wgg/internal"
"coreunit.net/wgg/lib/sshutils"
"github.com/joho/godotenv"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
var DisplayName string = "Unset"
var ShortName string = "unset"
var Version string = "?.?.?"
var Commit string = "???????"
func main() {
fmt.Println(DisplayName + " version v" + Version + ", build " + Commit)
err := godotenv.Load()
if err == nil {
fmt.Println("Environment variables from .env loaded")
}
sshTasks := map[string][]string{}
var j int
var i int = 0
for {
connecitonUrl := os.Getenv("DIT_NODE" + strconv.Itoa(i+1))
if len(connecitonUrl) <= 0 {
if i == 0 {
log.Fatalln("no ssh config for node " + strconv.Itoa(i+1))
}
break
}
sshTasks[connecitonUrl] = []string{}
j = 0
for {
sshTask := os.Getenv("DIT_NODE" + strconv.Itoa(i+1) + "_TASK" + strconv.Itoa(j+1))
if len(sshTask) <= 0 {
if j == 0 {
log.Fatalln("no ssh task for node " + strconv.Itoa(i+1) + " and task " + strconv.Itoa(j+1))
}
break
}
sshTasks[connecitonUrl] = append(sshTasks[connecitonUrl], sshTask)
j++
}
i++
}
hosts := []SshTaskHost{}
i = 0
for connecitonUrl, sshTaskList := range sshTasks {
taskHost, err := NewSshTaskHost(
i,
connecitonUrl,
sshTaskList,
)
if err != nil {
log.Fatalln(err)
}
hosts = append(hosts, taskHost)
i++
}
for _, host := range hosts {
err := host.PrecheckAll()
if err != nil {
log.Fatalln(err)
}
}
for _, host := range hosts {
err := host.Deploy()
if err != nil {
log.Fatalln(err)
}
}
fmt.Println("done")
}
type SshTaskHost struct {
ID int
connecitonUrl string
sshConfig sshutils.SshConfig
tasks []dit.Task
}
func NewSshTaskHost(
id int,
connecitonUrl string,
rawTasks []string,
) (SshTaskHost, error) {
sshConfig, err := sshutils.NewSshConfig(connecitonUrl)
if err != nil {
return SshTaskHost{}, err
}
tasks := []dit.Task{}
var newTask dit.Task
for _, rawTask := range rawTasks {
newTask, err = dit.ParseTask(rawTask)
if err != nil {
return SshTaskHost{}, err
}
tasks = append(tasks, newTask)
}
return SshTaskHost{
ID: id,
connecitonUrl: connecitonUrl,
sshConfig: sshConfig,
tasks: tasks,
}, nil
}
func (taskHost *SshTaskHost) PrecheckAll() error {
for _, task := range taskHost.tasks {
err := task.Precheck()
if err != nil {
return errors.New(
"precheck failed for '" + strconv.Itoa(taskHost.ID) +
"' task '" + task.Raw() + "': " +
err.Error(),
)
}
}
return nil
}
func (taskHost *SshTaskHost) Deploy() error {
return sshutils.HandleSftp(
taskHost.sshConfig,
func(
sftp *sftp.Client,
session *ssh.Session,
) error {
for id, task := range taskHost.tasks {
fmt.Println("Execute task " + task.Raw())
err := task.Execute(sftp, session)
if err != nil {
return errors.New(
"error host-" + strconv.Itoa(taskHost.ID) +
" executing task-" + strconv.Itoa(id) + ": " +
err.Error(),
)
}
}
return nil
},
)
}