geezmo
geezmo

Reputation: 161

Executing remote commands with sshsession Ant task

I'm having issues with sshsession Ant task.

I'm writing a deploy script which bzip sources, scp the archive to a remote server and then unzip sources on the remote server and copy files in the right directory tree.

(...)

<bzip2 src="/tmp/${selected.target}.tar" destfile="/tmp/${selected.target}.tar.bz2"/>
<scp file="/tmp/${selected.target}.tar.bz2" todir="user@server:/tmp/" keyfile="${user.home}/.ssh/id_rsa" trust="yes" passphrase="thisisasecret"/>

<sshsession host="server" username="user" keyfile="${user.home}/.ssh/id_rsa" trust="yes">
    <sequential>
        <untar src="/tmp/${selected.target}.tar.bz2" dest="/tmp/prova_untar" compression="bzip2"/>
        (copy files)
    </sequential>
</sshsession>

(...)

My problem is with tasks specified inside the sshsession, since they are executed locally and not on the remote server. I thought (but clearly i'm wrong) that command specified within sshsession tag would be executed on the server i just connected to. I read in the documentation that you can specify tunnels with localtunnel and remotetunnel, but i'm not sure i'm understanding correctly what those tunnels are for. Does anyone has encountered the same problem, or has a solution for that? The Ant version i'm working with is 1.8.2. Thanks,

Alberto

Upvotes: 2

Views: 10151

Answers (2)

SebastianH
SebastianH

Reputation: 2182

Although this question is not exactly new, I found myself in a similar situation today. My goal is to upload files and run commands on a remote server to which I have to tunnel (through another server). The answer given (requiring ant on the remote server) is totally unacceptable for me, but the comments here helped me forge this solution

The sshsession only creates a tunnel that you can use for the tasks within. The tasks within are not automatically run on the remote server but you can use the sshexec task together with the tunnel to achieve that. Also the scp task can now upload through the tunnel to the remote server. Here is an example:

<sshsession host="${jumphost}" port="22" username="${user}" password="${password}" trust="true">
    <localtunnel lport="${localTunnelPort}" rhost="${targethost}" rport="22"/>
    <sequential>
        <!-- run a command on the remote server (here mkdir) -->
        <sshexec host="localhost" port="${localTunnelPort}" username="${user.param}" password="${password.param}" command="mkdir ${home}/foobar" trust="true" />
        <!-- upload a file to the remote server -->
        <scp port="${localTunnelPort}" file="test_file.txt" todir="${user.param}:${password.param}@localhost:${home}/foobar/" trust="true" />
    </sequential>
</sshsession>

Upvotes: 2

Dante WWWW
Dante WWWW

Reputation: 2739

I add a answer to show how we use the remote-build.xml.

For example, we want to deploy some pre-built SQL files to the database server, insert the sql files into a database in MySQL and restart mysql.

<?xml version="1.0" encoding="UTF-8"?>
<project name="remote-build" basedir=".">

    <target name="run">
        <antcall target="extract" />
        <antcall target="build-database" />
        <antcall target="restart-mysql" />
    </target>

    <target name="extract">
        <unzip src="sample-sql.zip" dest="."/>
    </target>

    <target name="build-database">
        <taskdef name="insert" classname="my.company.ant.ParallelInsertSQL" classpath="my-company-lib.jar"/>
        <insert dbName="${database.name}" masterSQLs="${basedir}/sample-sql.sql" sqlDir="${basedir}/tables"/>
        <antcall target="restart-database"/>
    </target>

    <target name="restart-database">
        <exec executable="bash" inputstring="echo '${database.password}' | sudo -S /etc/init.d/mysql restart"/>
    </target>

</project>

So when deploying, we packed the sql file archive (sample-sql.zip) together with "my-company-lib.jar" and "remote-build.xml" and then transfer the archive file with scp.
Then, sshexec "ant -f remote-build.xml run" (you can either put the properties in a properties file or transfer them in the sshexec commandline).

Upvotes: 3

Related Questions