zoukankan      html  css  js  c++  java
  • 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
    ";
    $selStmt->execute();
    my $script = $selStmt->fetchrow_array();
    
    print "$script is $script
    ";
    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
    ";
    print "ErrorMsg:$stderr";
    
    open( F, ">>", "/home/scan/mojo/log/$app-$ip-$XDATE.log" );
    print F ("ErrorCode:$exit
    ");
    print F ("ErrorMsg:$stderr
    ");
    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
    ");
    print F ("ErrorMsg:$stderr
    ");
    print F ("$stdout
    ");
    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脚本内容:
    

  • 相关阅读:
    《演讲之禅》助你成长为一名合格程序员
    JavaScript中设置元素class的三种方法小结
    数据类型和Json格式
    JavaScript给onclick事件赋值,动态传参数实例辩错
    JSON.parse()和JSON.stringify()使用
    javascript写在<head>和<body>里的区别
    Source Insight 相关
    串口接收字符时,最后一个字符延时发送
    嵌入式编程中调试问题全记录
    新版数采仪开发问题全记录
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6200097.html
Copyright © 2011-2022 走看看