Mojo 自动发布接口

 get '/api/auto_publish/publish' => sub{
 my $c = shift;
 my $env = $c->param('env');
 my $app = $c->param('app');
 my $ip = $c->param('ip');
 my $stderr="";
 my $stdout="";
 my $XDATE = strftime("%Y%m%d%H%M%S",localtime());
 
 #and info='$app' and ip='$ip'
 use DBI;
my %hash=();
my $dbUser='DEVOPS';
my $user="root";
my $passwd="1234567";
my $dbh  = DBI->connect("dbi:mysql:database=$dbUser;host=192.168.32.161;port=3306",$user,$passwd) or die "can't connect to database ". DBI-errstr;
my $hostSql = qq{select script  from publish_info where env='$env' and info='$app' and ip='$ip'};
my $selStmt = $dbh->prepare($hostSql);
print "\$hostSql is $hostSql\n";
$selStmt->execute();
my $script = $selStmt->fetchrow_array();

print "\$script is $script\n";
use Net::SSH::Perl;
my $host="$ip";;
my $user='root';
my $passwd='xxxx';
my $cmd="sh $script";
my $ssh = Net::SSH::Perl->new($host,port=>22);
$ssh->login($user,$passwd);
my ($stdout,$stderr,$exit) = $ssh->cmd("$cmd");
$ssh->cmd("exit");
if($exit != 0){
print "ErrorCode:$exit\n";
print "ErrorMsg:$stderr";

open( F, ">>", "/home/scan/mojo/log/$app-$ip-$XDATE.log" );
print F ("ErrorCode:$exit\n");
print F ("ErrorMsg:$stderr\n");
close F;

 $c->render(text =>  "ErrorCode:$exit;ErrorMsg:$stderr")
} else {
print $stdout;
open( F, ">>", "/home/scan/mojo/log/$app-$ip-$XDATE.log" );
print F ("ErrorCode:$exit\n");
print F ("ErrorMsg:$stderr\n");
print F ("$stdout\n");
close F;
$c->render(text =>  'publish success')
}


};


脚本名字存放在数据库中:

mysql> select * from publish_info;
+----+----------------+-----------------+------+---------------------------+
| sn | ip             | info            | env  | script                    |
+----+----------------+-----------------+------+---------------------------+
|  1 | 121.xx.xx.9xx  | uat-frontend    | uat  | /root/auto_run/publish.sh |
|  2 | 1.1.1.1        | aa              | test | /root/auto_run/publish.sh |
|  3 | 121.xx.1xxx.1xx | zjzc-hy-pre-01  | uat  | /root/auto_run/publish.sh |
|  6 | 121.0.1.8       | zjzc-hy-core-01 | uat  | /root/auto_run/publish.sh |
+----+----------------+-----------------+------+---------------------------+
4 rows in set (0.00 sec)





调用的shell脚本内容:

posted @ 2016-05-05 09:31  czcb  阅读(132)  评论(0编辑  收藏  举报