Subversion Repositories XServices

Rev

Rev 19 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 19 Rev 46
Line 1... Line -...
1
/*
-
 
2
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
-
 
3
 *
-
 
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
-
 
5
 *   you may not use this file except in compliance with the License.
-
 
6
 *   You may obtain a copy of the License at
-
 
7
 *
-
 
8
 *       http://www.apache.org/licenses/LICENSE-2.0
-
 
9
 *
-
 
10
 *   Unless required by applicable law or agreed to in writing, software
-
 
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
-
 
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-
 
13
 *   See the License for the specific language governing permissions and
-
 
14
 *   limitations under the License.
-
 
15
 */
-
 
16
 
-
 
17
package net.brutex.xservices.ws;
1
package net.brutex.xservices.ws;
Line 18... Line -...
18
 
-
 
19
import java.io.File;
2
 
20
import javax.jws.WebMethod;
3
import javax.jws.WebMethod;
21
import javax.jws.WebParam;
4
import javax.jws.WebParam;
-
 
5
import javax.jws.WebService;
22
import javax.jws.WebService;
6
 
23
import net.brutex.xservices.types.HostConnection;
7
import net.brutex.xservices.types.HostConnection;
24
import net.brutex.xservices.types.ReturnCode;
8
import net.brutex.xservices.types.ReturnCode;
25
import net.brutex.xservices.util.RunTask;
-
 
26
import org.apache.tools.ant.taskdefs.ExecTask;
-
 
27
import org.apache.tools.ant.taskdefs.optional.net.RExecTask;
-
 
28
import org.apache.tools.ant.taskdefs.optional.net.TelnetTask;
-
 
29
import org.apache.tools.ant.taskdefs.optional.ssh.SSHExec;
-
 
30
import org.apache.tools.ant.types.Commandline;
9
import net.brutex.xservices.util.BrutexNamespaces;
31
 
-
 
32
/**
-
 
33
 *
-
 
34
 * @author Brian Rosenberger, bru@brutex.de
-
 
35
 */
10
 
36
@WebService(targetNamespace="http://ws.xservices.brutex.net", name="ExecuteService")
11
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES)
Line 37... Line 12...
37
public class ExecuteService {
12
public interface ExecuteService {
-
 
13
 
38
 
14
	@WebMethod(operationName = "runCommand")
39
    @WebMethod(operationName = "runCommand")
15
	public abstract ReturnCode runCommand(
40
    public ReturnCode runCommand(@WebParam(name = "executable") String cmd,
16
			@WebParam(name = "executable") String cmd,
41
            @WebParam(name = "argline") String args,
-
 
42
            @WebParam(name = "timeout") long timeout) {
-
 
43
 
-
 
44
        return executeCommand(cmd,
-
 
45
                Commandline.translateCommandline(args),
-
 
46
                null,
-
 
47
                false,
-
 
48
                null,
-
 
49
                false,
-
 
50
                true,
-
 
51
                false,
-
 
Line 52... Line 17...
52
                timeout);
17
			@WebParam(name = "argline") String args,
-
 
18
			@WebParam(name = "timeout") long timeout);
53
    }
19
 
54
 
20
	@WebMethod(operationName = "runCommandWithArgs")
55
    @WebMethod(operationName = "runCommandWithArgs")
21
	public abstract ReturnCode runCommandWithArgs(
56
    public ReturnCode runCommandWithArgs(@WebParam(name = "executable") String cmd,
-
 
57
            @WebParam(name = "arg") String[] args,
-
 
58
            @WebParam(name = "timeout") long timeout) {
-
 
59
 
-
 
60
        return executeCommand(cmd,
-
 
61
                args,
-
 
62
                null,
-
 
63
                false,
-
 
64
                null,
-
 
65
                false,
-
 
66
                true,
-
 
Line 67... Line 22...
67
                false,
22
			@WebParam(name = "executable") String cmd,
68
                timeout);
23
			@WebParam(name = "arg") String[] args,
69
    }
24
			@WebParam(name = "timeout") long timeout);
70
 
-
 
71
    @WebMethod(operationName = "runCommandAsync")
-
 
72
    public ReturnCode runCommandAsync(@WebParam(name = "executable") String cmd,
25
 
73
            @WebParam(name = "argline") String args) {
-
 
74
 
-
 
75
        return executeCommand(cmd,
-
 
76
                Commandline.translateCommandline(args),
-
 
77
                null,
-
 
78
                true,
-
 
79
                null,
-
 
80
                false,
-
 
Line 81... Line 26...
81
                true,
26
	@WebMethod(operationName = "runCommandAsync")
82
                false,
27
	public abstract ReturnCode runCommandAsync(
83
                0);
28
			@WebParam(name = "executable") String cmd,
84
    }
-
 
85
 
29
			@WebParam(name = "argline") String args);
86
    @WebMethod(operationName = "runCommandAsyncWithArgs")
-
 
87
    public ReturnCode runCommandAsyncWithArgs(@WebParam(name = "executable") String cmd,
-
 
88
            @WebParam(name = "arg") String[] args) {
-
 
89
 
-
 
90
        return executeCommand(cmd,
-
 
91
                args,
-
 
92
                null,
-
 
93
                true,
-
 
94
                null,
-
 
Line 95... Line 30...
95
                false,
30
 
-
 
31
	@WebMethod(operationName = "runCommandAsyncWithArgs")
96
                true,
32
	public abstract ReturnCode runCommandAsyncWithArgs(
97
                false,
33
			@WebParam(name = "executable") String cmd,
98
                0);
34
			@WebParam(name = "arg") String[] args);
99
    }
35
 
100
 
36
	@WebMethod(operationName = "runCommandWithSSH")
101
    @WebMethod(operationName = "runCommandWithSSH")
37
	public abstract ReturnCode runCommandWithSSH(
102
    public ReturnCode runCommandWithSSH(@WebParam(name = "host") String host,
-
 
103
            @WebParam(name = "port") int port,
-
 
104
            @WebParam(name = "username") String username,
-
 
Line 105... Line 38...
105
            @WebParam(name = "password") String password,
38
			@WebParam(name = "host") String host,
106
            @WebParam(name = "command") String cmd,
39
			@WebParam(name = "port") int port,
-
 
40
			@WebParam(name = "username") String username,
107
            @WebParam(name = "timeout") long timeout) {
41
			@WebParam(name = "password") String password,
108
 
42
			@WebParam(name = "command") String cmd,
109
        return sshExec(host, username, password, port, cmd, timeout);
43
			@WebParam(name = "timeout") long timeout);
110
    }
44
 
111
 
45
	@WebMethod(operationName = "runCommandWithSSHKeyAuth")
112
    @WebMethod(operationName = "runCommandWithSSHKeyAuth")
46
	public abstract ReturnCode runCommandWithSSHKeyAuth(
113
    public ReturnCode runCommandWithSSHKeyAuth(@WebParam(name = "host") String host,
-
 
114
            @WebParam(name = "port") int port,
-
 
115
            @WebParam(name = "username") String username,
-
 
Line 116... Line 47...
116
            @WebParam(name = "passphrase") String passphrase,
47
			@WebParam(name = "host") String host,
-
 
48
			@WebParam(name = "port") int port,
117
            @WebParam(name = "keyfile") String keyfile,
49
			@WebParam(name = "username") String username,
118
            @WebParam(name = "command") String cmd,
50
			@WebParam(name = "passphrase") String passphrase,
119
            @WebParam(name = "timeout") long timeout) {
51
			@WebParam(name = "keyfile") String keyfile,
120
 
-
 
121
        return sshExecWithCert(host, username, passphrase, keyfile, port, cmd, timeout);
-
 
Line 122... Line 52...
122
    }
52
			@WebParam(name = "command") String cmd,
-
 
53
			@WebParam(name = "timeout") long timeout);
123
 
54
 
124
    @WebMethod(operationName = "rExec")
55
	@WebMethod(operationName = "rExec")
125
    public ReturnCode rExec(@WebParam(name = "host") HostConnection host,
56
	public abstract ReturnCode rExec(
126
            @WebParam(name = "command") String cmd,
57
			@WebParam(name = "host") HostConnection host,
127
            @WebParam(name = "timeout") long timeout) {
58
			@WebParam(name = "command") String cmd,
128
        return rexec(host.hostname, host.port, host.user, host.password, cmd, timeout);
-
 
129
    }
-
 
Line 130... Line -...
130
 
-
 
131
    @WebMethod(operationName = "telnet")
-
 
132
    public ReturnCode runTelnet(@WebParam(name = "host") HostConnection host,
-
 
133
            @WebParam(name = "prompt") String prompt,
-
 
134
            @WebParam(name = "command") String cmd,
-
 
135
            @WebParam(name = "expect") String expect,
-
 
136
            @WebParam(name = "timeout") long timeout) {
-
 
137
        return telnet(host.hostname, host.port, host.user, host.password, cmd, timeout, prompt, expect);
-
 
138
    }
-
 
139
 
-
 
140
    @WebMethod(exclude = true)
-
 
141
    private ReturnCode executeCommand(String executable,
-
 
142
            String[] args,
-
 
143
            File dir,
-
 
144
            boolean spawn,
-
 
145
            String inputstring,
-
 
146
            boolean newenvironment,
-
 
147
            boolean vmlauncher,
-
 
148
            boolean searchpath,
-
 
149
            long timeout) {
-
 
150
        ExecTask exe = new ExecTask();
-
 
151
        RunTask runner = new RunTask(exe);
-
 
152
 
-
 
153
        /*
-
 
154
        Commandline cmdl = new Commandline();
-
 
155
        cmdl.setExecutable(executable);
-
 
156
        cmdl.addArguments(args);
-
 
157
        System.out.println(cmdl.describeCommand());
-
 
158
        */
-
 
159
        
-
 
160
        exe.setExecutable(executable);
-
 
161
        for (String s : args) {
-
 
162
        exe.createArg().setValue(s);
-
 
163
        }
-
 
164
 
-
 
165
        exe.setDir(dir);
-
 
166
        if (spawn) {
-
 
167
            exe.setSpawn(spawn);
-
 
168
        } else {
-
 
169
            exe.setTimeout(timeout);
-
 
170
            exe.setInputString(inputstring);
-
 
171
            exe.setOutputproperty("ExecuteService.stdout");
-
 
172
            exe.setErrorProperty("ExecuteService.stderr");
-
 
173
            exe.setResultProperty("ExecuteService.result");
-
 
174
        }
-
 
175
 
-
 
176
        exe.setNewenvironment(newenvironment);
-
 
177
        exe.setVMLauncher(vmlauncher);
-
 
178
        exe.setSearchPath(searchpath);
-
 
179
 
-
 
180
        return runner.postTask();
-
 
181
    }
-
 
182
 
-
 
183
    @WebMethod(exclude = true)
-
 
184
    private ReturnCode sshExec(String host,
-
 
185
            String username,
-
 
186
            String password,
-
 
187
            int port,
-
 
188
            String command,
-
 
189
            long timeout) {
-
 
190
        SSHExec sshexec = new SSHExec();
-
 
191
        RunTask runner = new RunTask(sshexec);
-
 
192
        sshexec.setHost(host);
-
 
193
        sshexec.setUsername(username);
-
 
194
        sshexec.setPassword(password);
-
 
195
        sshexec.setPort(port);
-
 
196
        sshexec.setCommand(command);
-
 
197
        sshexec.setTrust(true);
-
 
198
        sshexec.setTimeout(timeout);
-
 
199
        sshexec.setOutputproperty("SSHExec.stdout");
-
 
200
        return runner.postTask();
-
 
201
    }
-
 
202
 
-
 
203
    @WebMethod(exclude = true)
-
 
204
    private ReturnCode sshExecWithCert(String host,
-
 
205
            String username,
-
 
206
            String passphrase,
-
 
207
            String keyfile,
-
 
208
            int port,
-
 
209
            String command,
-
 
210
            long timeout) {
-
 
211
        SSHExec sshexec = new SSHExec();
-
 
212
        RunTask runner = new RunTask(sshexec);
-
 
213
        sshexec.setHost(host);
-
 
214
        sshexec.setUsername(username);
-
 
215
        sshexec.setKeyfile(keyfile);
-
 
216
        sshexec.setPassphrase(passphrase);
-
 
217
        sshexec.setPort(port);
-
 
218
        sshexec.setCommand(command);
-
 
219
        sshexec.setTrust(true);
-
 
220
        sshexec.setTimeout(timeout);
-
 
221
        sshexec.setOutputproperty("SSHExec.stdout");
-
 
222
        return runner.postTask();
-
 
223
    }
-
 
224
 
-
 
225
    @WebMethod(exclude = true)
-
 
226
    private ReturnCode rexec(String host,
-
 
227
            int port,
-
 
228
            String username,
-
 
229
            String password,
-
 
230
            String command,
-
 
231
            long timeout) {
-
 
232
        RExecTask rexec = new RExecTask();
-
 
233
        RunTask runner = new RunTask(rexec);
-
 
234
        rexec.setServer(host);
-
 
235
        rexec.setPort(port);
-
 
236
        rexec.setUserid(username);
-
 
237
        rexec.setPassword(password);
-
 
238
        rexec.setCommand(command);
-
 
239
        rexec.setTimeout((int) Math.round(timeout));
-
 
240
 
-
 
241
        return runner.postTask();
-
 
242
    }
-
 
243
 
-
 
244
    @WebMethod(exclude = true)
-
 
245
    private ReturnCode telnet(String host,
-
 
246
            int port,
-
 
247
            String username,
-
 
248
            String password,
-
 
249
            String command,
-
 
250
            long timeout, String prompt, String expect) {
-
 
251
        TelnetTask rexec = new TelnetTask();
-
 
252
        RunTask runner = new RunTask(rexec);
-
 
253
        rexec.setServer(host);
-
 
254
        rexec.setPort(port);
-
 
255
        rexec.setUserid(username);
59
			@WebParam(name = "timeout") long timeout);