Skip to content

Commit 2fd69c9

Browse files
committed
Add exec
- command execution with “startup” and “finish” - support for type local and remote - support for workdir
1 parent 048dec3 commit 2fd69c9

7 files changed

+123
-27
lines changed

sync/config.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package sync
22

3-
import "regexp"
3+
import (
4+
"regexp"
5+
"sync"
6+
)
7+
8+
var waitGroup sync.WaitGroup
9+
410

511
type Filter struct {
612
Exclude []string
@@ -21,6 +27,7 @@ type Connection struct {
2127
User string
2228
Password string
2329
Docker string
30+
WorkDir string
2431
}
2532

2633
type Database struct {
@@ -53,11 +60,19 @@ type Database struct {
5360
remoteConnection Connection
5461
}
5562

63+
type Execution struct {
64+
Type string
65+
Command string
66+
Workdir string
67+
When string
68+
}
69+
5670
type Server struct {
5771
Path string
5872
Connection Connection
5973
Filesystem []Filesystem
6074
Database []Database
75+
Exec []Execution
6176
}
6277

6378
type SyncConfig struct {

sync/connection.go

+11
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ package sync
33
import (
44
"strings"
55
"github.com/webdevops/go-shell"
6+
"fmt"
67
)
78

89
func (connection *Connection) CommandBuilder(command string, args ...string) []interface{} {
910
var ret []interface{}
1011

12+
if connection.WorkDir != "" {
13+
shellArgs := []string{command}
14+
shellArgs = append(shellArgs, args...)
15+
return connection.ShellCommandBuilder(shellArgs...)
16+
}
17+
1118
switch connection.GetType() {
1219
case "local":
1320
ret = connection.LocalCommandBuilder(command, args...)
@@ -35,6 +42,10 @@ func (connection *Connection) ShellCommandBuilder(args ...string) []interface{}
3542

3643
inlineCommand := shell.Quote(strings.Join(inlineArgs, " "))
3744

45+
if connection.WorkDir != "" {
46+
inlineCommand = fmt.Sprintf("cd %s ; %s", shell.Quote(connection.WorkDir), inlineCommand)
47+
}
48+
3849
switch connection.GetType() {
3950
case "local":
4051
ret = connection.LocalCommandBuilder("/bin/sh", "-c", inlineCommand)

sync/database_local.go

-26
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,6 @@ func (database *Database) localMysqldumpCmdBuilder(additionalArgs []string, useF
4040
return database.Local.Connection.CommandBuilder("mysqldump", args...)
4141
}
4242

43-
func (database *Database) localCommandInterface(command string, args ...string) []interface{} {
44-
var ret []interface{}
45-
46-
if database.Local.Connection.Type == "" {
47-
database.Local.Connection.Type = "local"
48-
49-
// autodetection
50-
if database.Local.Connection.Docker != "" {
51-
database.Local.Connection.Type = "docker"
52-
}
53-
54-
if database.Local.Connection.Hostname != "" {
55-
database.Local.Connection.Type = "ssh"
56-
}
57-
}
58-
59-
switch database.Local.Connection.Type {
60-
case "local":
61-
ret = ShellCommandInterfaceBuilder(command, args...)
62-
case "ssh":
63-
ret = database.Local.Connection.CommandBuilder(command, args...)
64-
}
65-
66-
return ret
67-
}
68-
6943
func (database *Database) localMysqlCmdBuilder(args ...string) []interface{} {
7044
args = append(args, "-BN")
7145

sync/execution.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package sync
2+
3+
import (
4+
"strings"
5+
"github.com/webdevops/go-shell"
6+
"fmt"
7+
)
8+
9+
func (execution *Execution) String(server *Server) string {
10+
var parts []string
11+
12+
parts = append(parts, fmt.Sprintf("Type:%s", execution.GetType()))
13+
14+
15+
if execution.Workdir != "" {
16+
parts = append(parts, fmt.Sprintf("Workdir:%s", execution.Workdir))
17+
}
18+
parts = append(parts, fmt.Sprintf("Command:%s", execution.Command))
19+
20+
return fmt.Sprintf("Exec[%s]", strings.Join(parts[:]," "))
21+
}
22+
23+
func (execution *Execution) Execute(server *Server) {
24+
shell.Cmd(execution.commandBuilder(server)...)
25+
}
26+
27+
func (execution *Execution) commandBuilder(server *Server) []interface{} {
28+
var connection Connection
29+
30+
switch execution.GetType() {
31+
case "local":
32+
connection = Connection{Type:"local"}
33+
case "remote":
34+
connection = server.Connection
35+
}
36+
37+
if execution.Workdir != "" {
38+
connection.WorkDir = execution.Workdir
39+
}
40+
41+
return connection.CommandBuilder(execution.Command)
42+
}
43+
44+
func (execution *Execution) GetType() string {
45+
var ret string
46+
47+
switch strings.ToLower(execution.Type) {
48+
case "":
49+
fallthrough
50+
case "local":
51+
ret = "local"
52+
case "remote":
53+
ret = "remote"
54+
default:
55+
panic(execution)
56+
}
57+
58+
return ret
59+
}

sync/server_deploy.go

+4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ func (server *Server) Deploy() {
66
//ShellErrorHandler(recover)
77
}()
88

9+
server.RunExec("startup")
910
server.DeployFilesystem()
1011
server.DeployDatabases()
12+
server.RunExec("finish")
13+
14+
waitGroup.Wait()
1115
}
1216

1317
func (server *Server) DeployFilesystem() {

sync/server_exec.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package sync
2+
3+
func (server *Server) RunExec(when string) {
4+
defer func() {
5+
recover := recover()
6+
ShellErrorHandler(recover)
7+
}()
8+
9+
execList := server.GetExecByWhen(when)
10+
11+
if len(execList) >= 1 {
12+
Logger.Main("Starting exec mode \"%s\"", when)
13+
14+
for _, exec := range execList {
15+
Logger.Step("executing >> %s", exec.String(server))
16+
exec.Execute(server)
17+
}
18+
}
19+
}
20+
21+
func (server *Server) GetExecByWhen(when string) []Execution {
22+
var execList []Execution
23+
24+
for _, val := range server.Exec {
25+
execList = append(execList, val)
26+
}
27+
28+
return execList
29+
}

sync/server_sync.go

+4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ func (server *Server) Sync() {
66
ShellErrorHandler(recover)
77
}()
88

9+
server.RunExec("startup")
910
server.SyncFilesystem()
1011
server.SyncDatabases()
12+
server.RunExec("finish")
13+
14+
waitGroup.Wait()
1115
}
1216

1317
func (server *Server) SyncFilesystem() {

0 commit comments

Comments
 (0)