forked from cloudwan/gohan
/
gohan_remote.go
189 lines (182 loc) · 5.34 KB
/
gohan_remote.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
// Copyright (C) 2015 NTT Innovation Institute, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package otto
import (
"bytes"
"github.com/dop251/otto"
"golang.org/x/crypto/ssh"
"github.com/Juniper/go-netconf/netconf"
"github.com/cloudwan/gohan/util"
//Import otto underscore lib
_ "github.com/dop251/otto/underscore"
)
//SetUp sets up vm to with environment
func init() {
gohanRemoteInit := func(env *Environment) {
vm := env.VM
builtins := map[string]interface{}{
"gohan_netconf_open": func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 2 {
panic("Wrong number of arguments in gohan_netconf_open call.")
}
rawHost, _ := call.Argument(0).Export()
host, ok := rawHost.(string)
if !ok {
return otto.NullValue()
}
rawUserName, _ := call.Argument(1).Export()
userName, ok := rawUserName.(string)
if !ok {
return otto.NullValue()
}
config := util.GetConfig()
publicKeyFile := config.GetString("ssh/key_file", "")
if publicKeyFile == "" {
return otto.NullValue()
}
sshConfig := &ssh.ClientConfig{
User: userName,
Auth: []ssh.AuthMethod{
util.PublicKeyFile(publicKeyFile),
},
}
s, err := netconf.DialSSH(host, sshConfig)
if err != nil {
ThrowOttoException(&call, "Error during gohan_netconf_open: %s", err.Error())
}
value, _ := vm.ToValue(s)
return value
},
"gohan_netconf_close": func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 1 {
panic("Wrong number of arguments in gohan_netconf_close call.")
}
rawSession, _ := call.Argument(0).Export()
s, ok := rawSession.(*netconf.Session)
if !ok {
ThrowOttoException(&call, "Error during gohan_netconf_close")
}
s.Close()
return otto.NullValue()
},
"gohan_netconf_exec": func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 2 {
panic("Wrong number of arguments in gohan_netconf_exec call.")
}
rawSession, _ := call.Argument(0).Export()
s, ok := rawSession.(*netconf.Session)
if !ok {
return otto.NullValue()
}
rawCommand, _ := call.Argument(1).Export()
command, ok := rawCommand.(string)
if !ok {
return otto.NullValue()
}
reply, err := s.Exec(netconf.RawMethod(command))
resp := map[string]interface{}{}
if err != nil {
resp["status"] = "error"
resp["output"] = err.Error()
} else {
resp["status"] = "success"
resp["output"] = reply
}
value, _ := vm.ToValue(resp)
return value
},
"gohan_ssh_open": func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 2 {
panic("Wrong number of arguments in gohan_netconf_open call.")
}
rawHost, _ := call.Argument(0).Export()
host, ok := rawHost.(string)
if !ok {
return otto.NullValue()
}
rawUserName, _ := call.Argument(1).Export()
userName, ok := rawUserName.(string)
if !ok {
return otto.NullValue()
}
config := util.GetConfig()
publicKeyFile := config.GetString("ssh/key_file", "")
if publicKeyFile == "" {
return otto.NullValue()
}
sshConfig := &ssh.ClientConfig{
User: userName,
Auth: []ssh.AuthMethod{
util.PublicKeyFile(publicKeyFile),
},
}
conn, err := ssh.Dial("tcp", host, sshConfig)
if err != nil {
ThrowOttoException(&call, "Error during gohan_ssh_open %s", err)
}
session, err := conn.NewSession()
if err != nil {
ThrowOttoException(&call, "Error during gohan_ssh_open %s", err)
}
value, _ := vm.ToValue(session)
return value
},
"gohan_ssh_close": func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 1 {
panic("Wrong number of arguments in gohan_netconf_close call.")
}
rawSession, _ := call.Argument(0).Export()
s, ok := rawSession.(*ssh.Session)
if !ok {
ThrowOttoException(&call, "Error during gohan_ssh_close")
}
s.Close()
return otto.NullValue()
},
"gohan_ssh_exec": func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 2 {
panic("Wrong number of arguments in gohan_netconf_exec call.")
}
rawSession, _ := call.Argument(0).Export()
s, ok := rawSession.(*ssh.Session)
if !ok {
return otto.NullValue()
}
rawCommand, _ := call.Argument(1).Export()
command, ok := rawCommand.(string)
if !ok {
return otto.NullValue()
}
var stdoutBuf bytes.Buffer
s.Stdout = &stdoutBuf
err := s.Run(command)
resp := map[string]interface{}{}
if err != nil {
resp["status"] = "error"
resp["output"] = err.Error()
} else {
resp["status"] = "success"
resp["output"] = stdoutBuf.String()
}
value, _ := vm.ToValue(resp)
return value
},
}
for name, object := range builtins {
vm.Set(name, object)
}
}
RegisterInit(gohanRemoteInit)
}