views:

67

answers:

5

I have a somewhat convoluted deploy procedure that I'd love to script. I'm not sure how easy or hard it will be. Here's what needs to happen:

  1. sftp myApp.war from my local machine to Server A
  2. sftp myApp.war from Server A to Server B (presumably ssh-ing in to Server A in order to run sftp on Server A)
  3. run jar xvf unab.war on Server B

What I'd LOVE from one of you is the following:

  1. a quick "yes, that's easy, and just the sort of thing shell scripts are for" or "no, that's going to take quite a lot of fiddling".
  2. If it's pretty doable, a rough outline of the steps I need to take to get it done.
A: 

It's doable but doing automated ssh/sftp is tricky (lots trickier than rsh/rcp used to be)

DVK
+2  A: 

Takes some fiddling. SSH and SCP can be used to do such a thing, but then you need to configure keys, etc.

Quotidian
+1  A: 

Just setup passwordless authentication and run this

scp localfile user@remotehost:location/          # step 1: copy the file
ssh user@remotehost bin/remote_script.sh         # step 2: run remote script
Otto Allmendinger
+1  A: 

"yes, that's easy, and just the sort of thing shell scripts are for"

The one caveat is that you'll want to set up some SSH authentication method that doesn't require a password to be typed into the console. (As a quick example of what I'm referring to, see http://www.eng.cam.ac.uk/help/jpmg/ssh/authorized_keys_howto.html.)

Here's a rough, untested bash script to do what you want:

#!/bin/bash
SERVER_ACCOUNT="username"
SERVER_A="servera.domain.com"
SERVER_B="serverb.domain.com"
TARGET_PATH="/path/to/destination/on/server"
WARFILE="myApp.war"
scp $WARFILE $SERVER_ACCOUNT@$SERVER_A:$TARGET_PATH
ssh $SERVER_A "scp $TARGET_PATH/$WARFILE $SERVER_ACCOUNT@SERVER_B:$TARGET_PATH"
ssh $SERVER_B "jar xvf $TARGET_PATH/$WARFILE"
RTBarnard
Awesome. Thanks very much.
morgancodes
Actually, seems a little uncomfortable for me to set up these keys. As I understand it, if I set it up this way, anyone who gets access to my computer will also be able to log in to these sites without a password. Is it not possible to pass the passwords as arguments to the script?
morgancodes
Maybe I can make another, higher-security user, and create the keys and run this script by doing su myHighSecurityUser
morgancodes
Two options: 1. Simply generate the keys but DO NOT load them into your keystore. ssh takes an argument to tell it where to find the key it should use to connect with.2. Get the application called sshpass. This will require your shell script to have the password in plaintext.
Freiheit
You are correct that setting up passwordless authentication can potentially be a security risk, and no, it isn't possible to provide the password as an argument. However, note that when you set up the authorized keys file, by default it only applies to the user who generated the key, so if you have proper permissions on the files in your .ssh folder and other people aren't likely to be logging in with your account, the risk is significantly mitigated.If those assumptions are not true, then your idea of another account for the script is probably the easiest route to accomplishing your goal.
RTBarnard
A: 

If you're not against some ruby instead of shell, you could use Capistrano, specifically done for this kind of tasks.

Valentin Rocher