Help Me Script This Task in Bash

Carlosinfl

Loves the juice
Joined
Sep 25, 2002
Messages
6,633
Every week, I deploy updates to our web server / developer portal running JBoss on CentOS 6.5. The .war file updates for the web portal are developed by our engineering team and it's my task as a System Administrator to do the following:

STAGING_DIR=/usr/local/tmp/depoy/
TARGET_DIR=/usr/local/jboss/standalone/deployments/

Code:
service jboss stop
cp /usr/local/tmp/deploy/web-3.0-XXXXX.war /usr/local/jboss/standalone/deployments/web-3.0.war
service jboss start

^Above^ are the '3' commands I manually run in Bash. The 'web-3.0-XXXXX.war' file is the highest revision / number / release in the /.../deploy/ directory. What I am looking for is for someone to please help me automate these simple commands for this task in #!/bin/bash. I have never written a script and don't intend to sound / look lazy but I am currently reading UNIX Shell Scripting 2nd Edition. I would need the scripts to execute the following commands in order w/ the variables defined above and the cp command needs to sort the *.war files in order to locate the latest build or sort from the newest time stamp on the file listing and copy that file into the $TARGET_DIR as 'web-3.0.war'.

I would greatly appreciate seeing how people who know anything more about scripting would write something like this in Bash. I know there are several different ways in *NIX to achieve the same goal but I really would love assistance in this.
 
As an Amazon Associate, HardForum may earn from qualifying purchases.
Wouldn't it be easier to have the latest .war file be in its own directory inside of /usr/local/tmp/deploy, such as /usr/local/tmp/deploy/latest, rotate the .war file that was in there previously and insert the latest, then just cp web-3.0-*.war ?

This is just quick&dirty of course, there are better ways to do it, just thought I'd offer that idea.
 
I don't know what would be easier as I caveat my post above w/ "I have never written a scipt..." however that being said, since I am automating this task, I think having a:

/.../deploy/latest/
/.../deploy/archive/

and staging only a single new buld in /.../latest/ is ideal for avoiding scipt-snafu by sorting and electing the wrong .war file for whatever reason.

Thanks for the suggestion. Still appreciate examples of how I would write something like this in .sh script.
 
I have a very large script that does deploys to many tomcat and jboss servers, i ripped out a small piece that should cover what you are looking for. I sanitized it a bit so there may be typos, and we ssh from a master server to transfer files around, but you could really do this with a one-liner.


#!/bin/bash

sourceFile="/var/opt/jboss/server/node1/deploy/filename.war"
destFile="/var/opt/jboss/server/all/deploy/filename.war
destJbossServer=destinationserver.yourdomain.com
scp path/to/filename.war $destJbossServer:/var/tmp/
echo Copied filename.war from staging to to $destJbossServer
ssh $destJbossServer "/etc/init.d/jboss stop && mv /var/tmp/filename.war $destFile && md5sum $destFile && /etc/init.d/jboss cleanstart"
 
Last edited:
This is off the top of my head and is untested.

Code:
#!/bin/bash

staging_dir="/usr/local/tmp/depoy"
target_dir="/usr/local/jboss/standalone/deployments"

latest_file=`ls -t ${staging_dir}/web-3.0-*.war | head -1`

service jboss stop
cp ${staging_dir}/${latest_file} ${target_dir}/web-3.0.war
service jboss start

That should get you started at least.
 
This is off the top of my head and is untested.

Code:
#!/bin/bash

staging_dir="/usr/local/tmp/depoy"
target_dir="/usr/local/jboss/standalone/deployments"

latest_file=`ls -t ${staging_dir}/web-3.0-*.war | head -1`

service jboss stop
cp ${staging_dir}/${latest_file} ${target_dir}/web-3.0.war
service jboss start

That should get you started at least.

If the XXXXX versioning scheme is a simple sortable incrementing number/letter scheme, this works for version sorting vs timestamp sorting:

Code:
latest_file=`ls web-3.0*.war | sort -r | head -n 1`
 
Back
Top