use strict; use warnings; use Cwd; use File::Copy; use Getopt::Long; use File::Basename; use Net::OpenSSH; my ($conf_file, $environment, $docopy, $copy, $doexec, $exec, $job, $dest_file, $user, $host, $IP, $TARGET_SERVER, $JENKINS_JOB, $wrapper, $src_file, $src_path, $src_dist_path, $src_full_path, $archive_path, $archive_host, $archive_user, $archive_user_id_file, $id_file, $ssh, $ssh_archive, @array, $line); usage() if ( @ARGV < 4 or !GetOptions( 'docopy=s' => \$copy, 'doexec=s' => \$exec, ) ); printf "Copy arg: $copy\n"; printf "Exec arg: $exec\n"; sub usage { printf "\nEither one of the following arguments are mandatory: docopy / doexec command\n"; printf "Exiting job...\n\n"; exit 1; } if($ENV{'Environment'} eq "Generic") { $IP = $ENV{'TARGET_SERVER'}; $archive_path = "/home/ec2-user/ite_builds_archive"; printf "Defined copy\n" if defined $copy; printf "Defined exec\n" if defined $exec; init(); } sub init { $JENKINS_JOB = $ENV{'JOB_NAME'}; $conf_file = "/home/ec2-user/SCM/conf/deploy_build.conf"; open (FH, "<", $conf_file) or die "Cannot open < $conf_file: $!"; while () { if ( $_ =~ /\b$JENKINS_JOB\b/ ) { push @array, $_; } else { next; } } printf "\n\nJobs to work on are:\n"; printf @array; printf "\n"; $archive_host = "10.123.123.100"; $archive_user = "ec2-user"; $archive_user_id_file = "/home/ec2-user/.ssh/sandy"; $ssh_archive = Net::OpenSSH->new($archive_host, key_path => $archive_user_id_file, user => $archive_user); $ssh_archive->error and die "Couldn't establish SSH connection: ". $ssh->error; foreach $line (@array) { ($job, $src_path, $dest_file, $user, $wrapper) = split(':', $line); if ($dest_file eq "") { ($src_file, $src_dist_path) = fileparse($src_path); $dest_file = $src_file; } printf "Job: $job\n"; printf "User: $user\n"; printf "Target Machine: $IP\n"; printf "Conf File: $conf_file\n"; printf "Source Path: $src_path\n"; printf "Dest File: $dest_file\n"; printf "Wrapper Script: $wrapper\n"; $id_file = "/home/ec2-user/.ssh/sandy"; $ssh = Net::OpenSSH->new($IP, key_path => $id_file, user => $user); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; if (defined $copy) { printf "\n"; printf "Initiating subroutine to copy distributable on remote machine...\n"; ©_distributable; } if (defined $exec) { printf "\n"; if (length $wrapper) { printf "Initiating subroutine for executing wrapper on remote machine...\n"; &exec_wrapper; } else { printf "*** No wrapper specified ****\n"; } } } } sub copy_distributable { $src_full_path = "$ENV{WORKSPACE}/$src_path"; if ( -f $src_full_path ) { if ($dest_file ne "") { printf "Distributable: $dest_file\n"; } printf "User: $user\n"; printf "Source Path: $src_full_path\n"; printf "Target Machine: $IP\n\n"; $ssh_archive->scp_put("$src_full_path", "$archive_path/$dest_file"); $ssh_archive->error and die "ERROR: Couldn't archive deliverable on Jenkins master\n". $ssh_archive->error; $ssh->scp_put("$src_full_path", "/home/$user/$dest_file"); $ssh->error and die "ERROR: SCP to target machine failed!\n". $ssh->error; printf "Deliverable copied on deployment machine. Now moving on to next task of archiving the deliverable...\n\n"; printf "mv $archive_path/$dest_file $archive_path/latest/\n\n"; my ($stdout) = $ssh_archive->capture2("mv $archive_path/$dest_file $archive_path/latest/"); $ssh_archive->error and die "Remote command to move file to $archive_path/latest/ directory failed: " . $ssh_archive->error; printf "Output: $stdout\n" if $stdout; printf "Deliverable archived on Jenkins master\n"; } else { printf "Deliverable not found at $src_full_path\n"; exit 1; } } sub exec_wrapper { printf "Wrapper to be executed: $wrapper\n"; printf "Target Machine: $IP\n"; printf "User: $user\n\n"; my ($stdout) = $ssh->capture2("~/release/$wrapper"); $ssh->error and die "Remote command failed to execute wrapper ~/release/$wrapper: " . $ssh->error; printf "Output: $stdout\n" if $stdout; } #### [ec2-user@jenkins_slave2 ~]$ uname -a Linux jenkins_slave 3.10.35-43.137.amzn1.x86_64 #1 SMP Wed Apr 2 09:36:59 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux [ec2-user@jenkins_slave2 ~]$ free -m total used free shared buffers cached Mem: 7228 5688 1539 0 72 3824 -/+ buffers/cache: 1791 5436 Swap: 16382 163 16219 #### [ec2-user@slave work]$ cat test_abc.sh #/bin/bash ssh -i /home/ec2-user/.ssh/sandy abc@some_server './deploy.sh' [ec2-user@slave work]$ ./deploy.sh Starting with abc deployment Doing deployment of abc Stopping abc server /usr/local/abc-tomcat /usr/local/abc-tomcat/bin/catalina.sh: line 437: /usr/lib/jvm/jdk1.7.0_51/bin/java: No such file or directory Tomcat already stopped for server abc-tomcat no need to forcefull shutdown Clearing ROOT directory Placing the new content in web server Archive: /home/abc/abc.war creating: /usr/local/abc-tomcat/webapps/ROOT/META-INF/ ... ... ...