-
Notifications
You must be signed in to change notification settings - Fork 3
/
instance.go
170 lines (157 loc) · 3.89 KB
/
instance.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
// Copyright (c) 2020-present Cloud <cloud@txthinking.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package hancock
import (
"io"
"net"
"os"
"path/filepath"
"strings"
"time"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
type Instance struct {
Client *ssh.Client
}
func NewInstance(server, user, password string, privateKey []byte, timeout int) (*Instance, error) {
l := make([]ssh.AuthMethod, 0)
if password != "" {
l = append(l, ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) (answers []string, err error) {
if len(questions) == 0 {
return []string{}, nil
}
return []string{password}, nil
}))
l = append(l, ssh.Password(password))
}
if privateKey != nil && len(privateKey) != 0 {
signer, err := ssh.ParsePrivateKey(privateKey)
if err != nil {
return nil, err
}
l = append(l, ssh.PublicKeys(signer))
}
conn, err := net.DialTimeout("tcp", server, 60*time.Second)
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: user,
Auth: l,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
c, chans, reqs, err := ssh.NewClientConn(&Conn{Conn: conn, Timeout: timeout}, server, config)
if err != nil {
return nil, err
}
client := ssh.NewClient(c, chans, reqs)
return &Instance{
Client: client,
}, nil
}
func (i *Instance) Run(cmd string) error {
s, err := i.Client.NewSession()
if err != nil {
return err
}
defer s.Close()
s.Stdout = os.Stdout
s.Stderr = os.Stderr
err = s.Run("sudo -H -u root sh -c 'PATH=/root/.nami/bin:$PATH " + cmd + "'")
if err != nil {
return err
}
return nil
}
func (i *Instance) Start(cmd string) error {
s, err := i.Client.NewSession()
if err != nil {
return err
}
defer s.Close()
return s.Start("sudo -H -u root sh -c 'PATH=/root/.nami/bin:$PATH " + cmd + "'")
}
func (i *Instance) HasNami() (bool, error) {
s, err := i.Client.NewSession()
if err != nil {
return false, err
}
defer s.Close()
s1, err := s.CombinedOutput("sudo -H -u root sh -c '[ -f /root/.nami/bin/nami ] && echo 1'")
if err != nil {
return false, nil
}
if strings.TrimSpace(string(s1)) != "1" {
return false, nil
}
return true, nil
}
func (i *Instance) HasJoker() (bool, error) {
s, err := i.Client.NewSession()
if err != nil {
return false, err
}
defer s.Close()
s1, err := s.CombinedOutput("sudo -H -u root sh -c '[ -f /root/.nami/bin/joker ] && echo 1'")
if err != nil {
return false, nil
}
if strings.TrimSpace(string(s1)) != "1" {
return false, nil
}
return true, nil
}
func (i *Instance) InstallNami() error {
err := i.Run("curl https://bash.ooo/nami.sh | bash")
if err != nil {
return err
}
return nil
}
func (i *Instance) InstallJoker() error {
err := i.Run("nami install joker")
if err != nil {
return err
}
return nil
}
func (i *Instance) Upload(file string) error {
src, err := os.Open(file)
if err != nil {
return err
}
defer src.Close()
c, err := sftp.NewClient(i.Client)
if err != nil {
return err
}
defer c.Close()
dst, err := c.Create("/tmp/" + filepath.Base(file))
if err != nil {
return err
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
return err
}
if err := c.Chmod("/tmp/"+filepath.Base(file), 0777); err != nil {
return err
}
err = i.Run("mv /tmp/" + filepath.Base(file) + " /root/.nami/bin/" + filepath.Base(file))
if err != nil {
return err
}
return nil
}