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
|
// vim: shiftwidth=4 tabstop=4 noexpandtab
package main
import (
"time"
"log"
"os/exec"
"fmt"
"io"
)
type Transporter struct {
statemachine Machine
}
type TransportConfig struct {
Enabled bool `json:"enabled"`
LocalPathToSshPrivateKey string `json:"local_path_to_ssh_private_key"`
FileserverHostOrIp string `json:"fileserver_host_or_ip"`
FileserverSshPort string `json:"fileserver_ssh_port"`
RemoteUser string `json:"remote_user"`
RemotePathToTargetDirectory string `json:"remote_path_to_target_directory"`
}
func NewTransporter() Transporter {
return Transporter{
statemachine: Machine{
name: "transporter",
initial: "idle",
states: StateMap{
"idle": MachineState{ // nothing to transport
on: TransitionMap{
"transport_request": MachineTransition{
to: "transport",
},
},
},
"transport": MachineState{ // rsync process for transport is running
on: TransitionMap{
"transport_request": MachineTransition{
to: "transport_queue",
},
"transport_finished": MachineTransition{
to: "idle",
},
},
},
"transport_queue": MachineState{ // like transport but also a pending transport request
on: TransitionMap{
"transport_finished": MachineTransition{
to: "transport",
},
},
},
},
api: make(chan string),
state_listeners: make([]*(chan string), 0),
hook: runTransporterHooks,
},
}
}
func (transp *Transporter) Run() {
transp.statemachine.Run()
}
func runTransporterHooks(last string, next string, m *Machine) {
if last == "idle" && next == "transport" {
go transportData(m)
}
if last == "transport_queue" && next == "transport" {
go transportData(m)
}
}
func transportData(m *Machine) {
if config.Transport.Enabled {
if !config.Flag.Debug {
// generate command string from config
cmdString := fmt.Sprintf(
"\"rsync --remove-source-files -rltgoDv -e 'ssh -p %s -i %s' /var/lib/birdscan/ %s@%s:%s\"",
config.Transport.FileserverSshPort,
config.Transport.LocalPathToSshPrivateKey,
config.Transport.RemoteUser,
config.Transport.FileserverHostOrIp,
config.Transport.RemotePathToTargetDirectory,
)
// create command
cmd := exec.Command(
"/bin/bash",
"-c",
cmdString,
)
log.Printf("Executing: '%s'", cmd.String())
// get stderr
stderr,err := cmd.StderrPipe()
if err != nil {
log.Print(err)
}
defer stderr.Close()
// execute command and fetch stderr
err = cmd.Start()
if err != nil {
log.Print(err)
}
stderrData,err := io.ReadAll(stderr)
if err != nil {
log.Print(err)
}
err = cmd.Wait()
if err != nil {
log.Print(err)
log.Println(string(stderrData))
}
} else {
time.Sleep(8 * time.Second)
}
}
m.SendEvent("transport_finished")
}
|