Distributed deployment with Hudson & SSH

Source: Internet
Author: User
Tags jboss jboss server pscp ssh access
Document directory
  • Prepare for installation of SSH
  • Installing and enable remote access using SSH

Have you already implemented an multi-server artifact deployment using acontinuous integration engine? If not, then read ahead and maybe this article is of help.

The need for continuous integration

A good practice in a software development methodology and lifecycle is the use of a continuous integration engine. the adoption of continuous integration improves you software quality by quickly reporting failed builds so you can modify/correct your code. popular continuous integration engines can often be extended with software quality tooling so you can report on specific quality aspect of your software. thus informing developers and even other people who take an interest in the status of the latest build.

IMHOA failed build can also be identified as code that compiles but that does not meet the quality standards set by your organization. you are off-course totally free in defining what in your opinion a failed build actually means! A good build compiles, quality requirements have been met and automatic functional and unit testing has been successful.

There a few popular continuous integration engines available:

  • Hudson extensible continuous Integration Server
  • Cruisecontrol
  • Bamboo

Probably there a some more, but for me needs the [Hudson extensible continuous Integration Server] works perfect.

My needs

My continuous integration Engine

  • Must support multiple programming extensions ages Java/. Net/Ruby
  • Runs on multiple operating systems (Windows/MAC/Linux)
  • Pluggable in the sense that there must be integration with for example JUnit, jmeter, cobertura, checkstyle etc.
  • Must be able to send out notifications using email, Twitter, instant messaging
  • Seemless integration with CVS/subversion and git
  • Simple and Easy Configuration
  • Must support timed builds & trigger builds from SCM commits
  • Maintain a link between modified code and
  • And some more ..

All of these requirements and more have been succesfully fulfilled by usinghudson

Hudson and automatic deployment

In every project a recurring problem arises, artifacts of a software build have to be distributed accross different servers and environments. How are the Build artifacts going to be distributed and deployed? Why not let the hudsonserver give a helping hand !!

Deployment scenario

In the following scenario we will be distributing Build artifacts from a centralhudson server to three different JBoss application servers al running Windows 2003 Server as operating system. next to the Hudson server we have asubversion system which is used for SCM purposes.

Distribution of Build artifacts

After a succesfull build, the artifacts have to be distributed to remote machines. offcourse the latest code has been checked out from the svn repository, compiled and tested. the distribution of the artifacts can be done in varous ways. one can use ftp, shared folders, SCM checkin Etc .. for me the most easiest way to distribute Build artifacts is using secure shell access aka ssh. this is a secure and a standardized manner for distribution.

Lets assume we have the Build artifacts somewhere on our Hudson server, we need a way of transfering them using SSH to a remote machine. to accomplish this we need SSH access to the remote machine. with the help off copsshinstalling SSH is a breeze!

Prepare for installation of SSH

Prepare yourself by downloading:

  • Copssh-SSH service for Windows
  • Putty-ssh client (used for connection to the SSH service)
  • Pscp-ssh client for File Transfer
  • Plink-ssh client for executing remote commands
Installing and enable remote access using SSH
  1. Install copssh on the remote system
  2. On the remote system enable a user for SSH access, see the Installation Guide of copssh
  3. Start putty on your local machine
  4. Using putty connect to the remote system and exchange security credentials
  5. You are now officially ready to remotely access the system using SSH

If you want to enable the Hudson server to access the remote system, startputty on the Hudson server and repeat Step 4!

Note:Make sure that your Hudson uses the same credentials then the account in which you exchange security credentials, otherwise remote access from Hudson server to the remote system will not work!

Execute remote commands and exchange files using SSH

If you can succesfully access the remote server using putty, it is time to exchange files or execute remote commands. this can be done by using 2 small CommandLine utilities called pscp for file transfer and plink for executing remote commands such as remotely deleting files etc. make sure these are in you path settings so you can execute them everywhere!

Examples for executing a remote command (substitute the % parameters % with your own ones)

'create a directory' plink -batch -pw %PASSWORD% %USERNAME%@%HOSTNAME%  mkdir C:/tmp  'delete a directory' plink -batch -pw %PASSWORD% %USERNAME%@%HOSTNAME%  rm -rf C:/tmp  'stop a windows service' plink -batch -pw %PASSWORD% %USERNAME%@%HOSTNAME%  net stop %SERVICENAME%

Upload a file

pscp -pw %PASSWORD% %SOURCE% %USERNAME%@%HOSTNAME%:%DESTINATION%

Upload multiple files

pscp -pw %PASSWORD% %SOURCE%/*.* %USERNAME%@%HOSTNAME%:%DESTINATION%
The return of the. BAT file

So far we have enabled remote access using SSH/copssh, executed remote commands and transferred files. all the needed ingredients are in place to enable our Hudson server to remotely deploy Build artifacts. in the job configuration of Hudson you can trigger a batch file after a succesfull build, so whenever a succesfull build occurs trigger a batch that executes a few commands to quickly deploy Build artifacts to any number of remote servers.

In our case all deployment artifacts are copied to a central directory per project. So if we need to deploy a build, we can copy parts or the whole directory contents to a remote server.

To give an example see the following batch files:

Main example for a batchfile that triggers stopping of the Remote Windows Services gives the instruction on which files need to be remotely deployed and start the services again.

-> filename = upload-project-to-development.bat  @CLS @ECHO OFF  SET USERNAME=%1 SET PASSWORD=%2 SET HOSTNAME=%3 SET JBOSSDIR=%4 SET SOURCEDIR=%5  @ECHO  : - start upload procedure @ECHO  : -- stopping servers @plink -batch -pw %PASSWORD% %USERNAME%@%HOSTNAME% net stop JBoss  @ECHO  : -- uploading  files CALL upload-files.bat %USERNAME% %PASSWORD% %HOSTNAME% %JBOSSDIR% %SOURCEDIR%/*.*  @ECHO  : -- starting servers @plink -batch -pw %PASSWORD% %USERNAME%@%HOSTNAME% net start JBoss  ECHO  : - finished upload procedure

The above script can be called easily by the Hudson server after a succesfull deployment.

Example:  upload-project-to-development scott tiger 10.0.0.100 d:/java/server/jboss-v5.0 d:/build_artifacts/projectx

The example above:

  1. Stops the JBoss server on the 10.0.0.100 host
  2. Passes some parameters to a file called "upload-files.bat" Script
  3. Starts the JBoss servers again

The script that executes the actual maintenance and uploads is the "upload-files.bat File". All parameters are passed in by the calling script.

-> filename = upload-files.bat  @ECHO OFF  SET USERNAME=%1 SET PASSWORD=%2 SET HOSTNAME=%3 SET JBOSSDIR=%4 SET SOURCE=%5 SET DESTINATION=%JBOSSDIR%/deploy  @ECHO  : - %HOSTNAME% - starting file copy  @ECHO  : -- %HOSTNAME% - deleting JBOSS tmp @plink -batch -pw %PASSWORD% %USERNAME%@%HOSTNAME%  rm -rf %JBOSSDIR%/tmp @plink -batch -pw %PASSWORD% %USERNAME%@%HOSTNAME% mkdir %JBOSSDIR%/tmp  @ECHO  : -- %HOSTNAME% - deleting JBOSS work @plink -batch -pw %PASSWORD% %USERNAME%@%HOSTNAME%  rm -rf %JBOSSDIR%/work @plink -batch -pw %PASSWORD% %USERNAME%@%HOSTNAME%  mkdir %JBOSSDIR%/work  @ECHO  : -- %HOSTNAME% - deleting previous ears + jars @plink -batch  -pw %PASSWORD% %USERNAME%@%HOSTNAME% rm %JBOSSDIR%/deploy/*.ear @plink -batch  -pw %PASSWORD% %USERNAME%@%HOSTNAME% rm %JBOSSDIR%/deploy/*.jar  @ECHO  : -- %HOSTNAME% - copy remote files @pscp -pw %PASSWORD% %SOURCE% %USERNAME%@%HOSTNAME%:%DESTINATION%  @ECHO  : - %HOSTNAME% - finishing file copy

The example above:

  1. Removes the JBoss TMP & work directory
  2. Removes artifacts from previous builds
  3. Copies the artifacts to the remote JBoss deploy directory
Steps taken

So the list of tasks executed by calling the batch files with the correct parameters are:

  1. Stopping the remote JBoss Server
  2. Removing the remote JBoss TMP & work Directories
  3. Removing the remote JBoss artifacts from previous deployments
  4. Copy files to the remote JBoss Server
  5. Starting the JBoss server again
From build to deployment

So with a quick installation of SSH/Putty/plink/pscp we now have a modular and easy way of distributing files to remote systems. offcourse there are lots of improvements to make, but for now it works without any problems!

The given examples can be easily modified so that after a succesfull build the artifact deployment to all of your servers can be done in a very simple and easy way.

Notes
  • Wikipedia-Article On Continous Integration
  • Hudson continuus Integration Server
  • Copssh
  • OpenSSH
  • Cruisecontrol
  • JBoss Jee Application Server
  • Putty/plink/pscp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.