-
Notifications
You must be signed in to change notification settings - Fork 1
/
system.go
211 lines (168 loc) · 4.48 KB
/
system.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
"github.com/Sirupsen/logrus"
)
type SystemClient interface {
EnvironmentDirs() ([]string, error)
DetectTimeZone() string
EnsureEnvironmentDir(envName string) (string, error)
RemoveEnvironmentDir(envName string) error
EnsureSSHKey() (SSHKey, error)
Username() string
UID() int
GID() int
RunSSH(command string, args []string) error
CheckSSHPort(host string, port int64) error
}
type RealSystemClient struct {
user string
uid int
gid int
baseDir string
envRegexp *regexp.Regexp
}
type SSHKey struct {
privatePath string
publicPath string
}
func (rsc *RealSystemClient) DetectTimeZone() string {
realLocaltime, _ := filepath.EvalSymlinks("/etc/localtime")
if _, err := os.Stat("/etc/timezone"); err == nil {
contents, err := ioutil.ReadFile("/etc/timezone")
if err != nil {
return ""
}
return strings.TrimSpace(string(contents))
}
if strings.HasPrefix(realLocaltime, "/usr/share/zoneinfo/") {
return strings.TrimPrefix(realLocaltime, "/usr/share/zoneinfo/")
}
return ""
}
func (rsc *RealSystemClient) EnvironmentDirs() ([]string, error) {
files, err := ioutil.ReadDir(rsc.baseDir)
if err != nil {
return nil, err
}
dirs := make([]string, 0)
for _, file := range files {
if file.IsDir() {
dirs = append(dirs, file.Name())
}
}
return dirs, nil
}
func (rsc *RealSystemClient) Username() string {
return rsc.user
}
func (rsc *RealSystemClient) UID() int {
return rsc.uid
}
func (rsc *RealSystemClient) GID() int {
return rsc.gid
}
func (rsc *RealSystemClient) EnsureEnvironmentDir(envName string) (string, error) {
envPath := filepath.Join(rsc.baseDir, envName)
err := os.MkdirAll(envPath, 0755)
if err != nil {
return "", err
}
return envPath, nil
}
func (rsc *RealSystemClient) RemoveEnvironmentDir(envName string) error {
envPath := filepath.Join(rsc.baseDir, envName)
err := os.RemoveAll(envPath)
if err != nil {
return err
}
return nil
}
func (rsc *RealSystemClient) EnsureSSHKey() (SSHKey, error) {
privPath := filepath.Join(rsc.baseDir, "skeg_key")
pubPath := filepath.Join(rsc.baseDir, "skeg_key.pub")
if _, err := os.Stat(privPath); os.IsNotExist(err) {
cmd := exec.Command("ssh-keygen", "-q", "-t", "rsa", "-N", "", "-C", "skeg key", "-f", privPath)
err := cmd.Run()
if err != nil {
return SSHKey{}, err
}
}
return SSHKey{privPath, pubPath}, nil
}
func (rsc *RealSystemClient) CheckSSHPort(host string, port int64) error {
address := fmt.Sprintf("%s:%d", host, port)
timeouts := []time.Duration{0, 200, 500, 1000, 2000}
var err error
var conn net.Conn
for _, timeout := range timeouts {
logrus.Debugf("Waiting for %d millis %s", timeout, address)
time.Sleep(timeout * time.Millisecond)
conn, err = net.Dial("tcp", address)
if err != nil {
logrus.Debugf("error connecting to ssh port: %s", err)
continue
}
message, err := bufio.NewReader(conn).ReadString('\n')
logrus.Debugf("message: %s (%s)", message, err)
conn.Close()
if strings.Contains(message, "SSH") {
return nil
}
}
return errors.New("Unable to connect to SSH port on environment")
}
func (rsc *RealSystemClient) RunSSH(command string, args []string) error {
cmd := exec.Command(command, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func NewSystemClient() (*RealSystemClient, error) {
var home string
if home = os.Getenv(HOME_ENV_NAME); len(home) == 0 {
return nil, fmt.Errorf("$%s environment variable not found", HOME_ENV_NAME)
}
return NewSystemClientWithBase(filepath.Join(home, ENVS_DIR))
}
func NewSystemClientWithBase(baseDir string) (*RealSystemClient, error) {
var user string
if user = os.Getenv(USER_ENV_NAME); len(user) == 0 {
return nil, fmt.Errorf("$%s environment variable not found", USER_ENV_NAME)
}
// lowercase and sanitize username (mostly for windows)
user = strings.ToLower(strings.Replace(user, " ", "_", -1))
uid := os.Getuid()
gid := os.Getgid()
if env_endpoint := os.Getenv("DOCKER_MACHINE_NAME"); len(env_endpoint) > 0 {
uid = 1000
gid = 1000
} else if runtime.GOOS == "windows" {
uid = 1000
gid = 1000
}
systemClient := RealSystemClient{
user: user,
uid: uid,
gid: gid,
baseDir: baseDir,
envRegexp: regexp.MustCompile(fmt.Sprintf("%s/(.*)dev", user)),
}
err := os.MkdirAll(baseDir, 0700)
if err != nil {
return nil, err
}
return &systemClient, nil
}