• 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脚本内容:
    

  • 相关阅读:
    php多进程和多线程的比较
    设计模式学习系列——建造者模式
    设计模式学习系列——单例模式
    设计模式学习系列——前言
    设计模式学习系列——工厂模式
    记一次给nginx的web服务器目录加软链接
    某公司后端开发工程师面试题学习
    2010年腾讯前端面试题学习(jquery,html,css部分)
    2010年腾讯前端面试题学习(js部分)
    winfrom 隐藏任务栏(win7)
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6200097.html
Copyright © 2020-2023  润新知