-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.go
93 lines (74 loc) · 1.79 KB
/
plugin.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
)
type (
Config struct {
Tasks string
PrivateKey string
PublicKey string
}
Plugin struct {
Config Config
}
)
// Exec executes the plugin step
func (p Plugin) Exec() error {
var err error = nil
err = writeSshKey(p.Config)
if err != nil {
return err
}
tasks := strings.Fields(p.Config.Tasks)
if len(tasks) == 0 {
return fmt.Errorf("Please provide Capistrano tasks to execute")
}
printLog("Running Bundler")
bundle := bundle("install")
if err := bundle.Run(); err != nil {
return fmt.Errorf("Bundler failed. %s", err)
}
printLog("Running Capistrano")
capistrano := capistrano(tasks...)
if err := capistrano.Run(); err != nil {
return fmt.Errorf("Capistrano failed. %s", err)
}
return nil
}
func capistrano(tasks ...string) *exec.Cmd {
args := append([]string{"exec", "cap"}, tasks...)
return bundle(args...)
}
func bundle(args ...string) *exec.Cmd {
return shellCommand("/bundle.sh", args...)
}
func shellCommand(cmd string, args ...string) *exec.Cmd {
c := exec.Command(cmd, args...)
// c.Dir = w.Workspace.Path
c.Env = os.Environ()
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c
}
func writeSshKey(c Config) error {
var err error = nil
var private_key_bytes = []byte(c.PrivateKey)
var public_key_bytes = []byte(c.PublicKey)
_ = os.MkdirAll("/root/.ssh", 0700)
err = ioutil.WriteFile("/root/.ssh/capistrano", private_key_bytes, 0600)
if err != nil {
return fmt.Errorf("Failed writing private key: %s", err)
}
err = ioutil.WriteFile("/root/.ssh/capistrano.pub", public_key_bytes, 0644)
if err != nil {
return fmt.Errorf("Failed writing public key: %s", err)
}
return nil
}
func printLog(message string, a ...interface{}) {
fmt.Printf("=> %s\n", fmt.Sprintf(message, a...))
}