經過Rex + module的形式來分發配置文件到不一樣服務節點 以及集中控制各節點的web服務

1,全部業務的測試機不一樣的node跑不一樣的業務(經過hosts指定域名 由於只是簡單的測試沒有專門用dns)java

[root@localhost rex]# cat /etc/hosts
127.0.0.1  mail.weike.com mx localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.128    node1    caisen
192.168.0.129    node2    mail
192.168.0.45     node3    guanwang
192.168.0.164    node4    shopwap
192.168.0.193    node5    gerenwap
192.168.0.196    node6    appwap
192.168.0.224    node7    geren
192.168.0.226    node8    chaoshi
192.168.0.228    node9    quanguo
192.168.0.215    node10    boss


2,分別將不一樣的web服務以模塊的形式組織起來進行集中管理,,Rexfile配置信息以下:node

use Rex -feature => ['1.3'];
use strict;
use warnings;
#use Data::Dumper;
#針對不一樣的服務類型經過模塊化配置
require Servers::Quanguo;
require Servers::Chaoshi;
require Servers::Geren;
require Servers::Boss;
require Servers::Shopwap;
require Servers::Gerenwap;
require Servers::Appwap;
require Servers::Guanwang;
#遠程鏈接登錄方式
private_key "/root/.ssh/id_rsa";
public_key "/root/.ssh/id_rsa.pub";
#設置線程數
parallelism 'max';
#全部web服務器節點
group all_servers       =>"node[1..2,4..10]";

desc "關閉全部節點上的tomcat和weblogic服務";
task "stop_web",group=>"all_servers",sub{
        #my @ps=ps();
        #print Dumper(@ps);
        #my @list=grep{$_->{"command"}==-5}ps("command","ni");
        my $server = Rex::get_current_connection()->{server};
        for my $proc(ps()){
                if($proc->{"command"}=~/java.*tomcat/is){
                        run "./shutdown.sh",
                                cwd     =>"/usr/local/tomcat/bin",
                                only_if =>"pgrep java";
                        say "[$server]: tomcat服務關閉完成";
                        last;
                }elsif($proc->{"command"}=~/java.*?node1.*?weblogic/is){
                        run "stop_node.sh",
                                only_if=>"pgrep java";
                        say "[$server]: weblogic服務關閉完成";
                        last;
                }else{
                        next;
                }
        }
        say "[$server]: 服務已經關閉";
};

desc "啓動全部節點上的tomcat和weblogic服務";
task "start_web",group=>"all_servers",sub{
        my $server = Rex::get_current_connection()->{server};
        if(is_file("/usr/local/tomcat/bin/startup.sh")){
                run "./startup.sh",sub {
                        my($stdout,$stderr)=@_;
                        say "[$server]: $stdout";
                        },
                        cwd     =>"/usr/local/tomcat/bin",
                        unless  =>"pgrep  java";
        }else{
                run "start_node.sh",
                        unless  =>"pgrep java";
        }
        for my $proc(ps()){
                if($proc->{"command"}=~/java.*tomcat/i){
                        say "[$server]: tomcat已經啓動,進程pid:".$proc->{"pid"};
                        last;
                }elsif($proc->{"command"}=~/java.*weblogic/isg){
                        say "[$server]: weblogic服務已經啓動,進程pid:".$proc->{"pid"};
                        last;
                }else{
                        next;
                }
        }
};

desc "重啓全部節點上的tomcat和weblogic服務";
task "restart_web",group=>"all_servers",sub {
        my $server = Rex::get_current_connection()->{server};
        needs 'stop_web';
        sleep 3;
        needs 'start_web';
        say "[$server]: 服務重啓完成";
};

desc "分發數據源文件到不一樣的web服務節點上";
task "deploy_file",group=>"all_servers",sub {
        my $server = Rex::get_current_connection()->{server};
        Servers::Quanguo->deploy_quanguo if $server eq 'node9';
        Servers::Chaoshi->deploy_chaoshi if $server eq 'node8';
        Servers::Geren->deploy_geren   if $server eq 'node7';
        Servers::Boss->deploy_boss    if $server eq 'node10';
        Servers::Shopwap->deploy_shopwap if $server eq 'node4';
        Servers::Gerenwap->deploy_gerenwap if $server eq 'node5';
        Servers::Appwap->deploy_appwap  if $server eq 'node6';
        Servers::Guanwang->deploy_guanwang if $server eq 'node3';
};

desc "查看全部節點的當前時間";
task "current_time",group=>"all_servers",sub{
        say run "date";
};

3,各個模塊的配置內容都同樣除了分發的文件名。模塊內容以下web

package Servers::Appwap;
use Rex -feature => ['1.3'];

desc "分發數據源配置文件到我的中心wap節點服務器";
task "deploy_appwap","appwap",sub{
        my $server = Rex::get_current_connection()->{server};
        if(!is_dir("/usr/local/dbsrc")){
                file "/usr/local/dbsrc",
                        ensure  =>"directory",
                        owner   =>"root",
                        group   =>"root",
                        mode    =>755;
                upload "/rex/conf/jdbc_moser.properties","/usr/local/dbsrc";
        };
        file "/usr/local/dbsrc/jdbc_moser.properties",
                source=>"/rex/conf/jdbc_moser.properties",
                owner=>"root",
                group=>"root",
                mode=>644,
                on_change=> sub {
                        say "[$server]: 數據源配置更新成功";
                };
};

1;

4,經過rex -T能夠查看本身定義的全部任務:shell

[root@localhost rex]# rex -T
Tasks
 deploy_file			   分發數據源文件到不一樣的web服務節點上
 restart_web			   重啓全部節點上的tomcat和weblogic服務
 start_web			   啓動全部節點上的tomcat和weblogic服務
 stop_web			   關閉全部節點上的tomcat和weblogic服務

 Servers:Appwap:deploy_appwap	   分發數據源配置文件到我的中心wap節點服務器
 Servers:Boss:deploy_boss	   分發數據源配置文件到Boss節點服務器
 Servers:Chaoshi:deploy_chaoshi    分發數據源配置文件到超市節點服務器
 Servers:Geren:deploy_geren	   分發數據源配置文件到我的中心節點服務器
 Servers:Gerenwap:deploy_gerenwap  分發數據源配置文件到我的中心wap節點服務器
 Servers:Guanwang:deploy_guanwang  分發數據源配置文件到官網節點服務器
 Servers:Quanguo:deploy_quanguo    分發數據源配置文件到全國節點服務器
 Servers:Shopwap:deploy_shopwap    分發數據源配置文件到商城wap節點服務器
Server Groups
 all_servers  node[1..2,4..10]

5,運行某我的物只須要用rex + task就能夠好比:我要啓動全部節點上的tomcat和weblogic服務 以下所示:tomcat

[root@localhost rex]# rex start_web
[2015-09-06 06:07:51] INFO - Running task start_web on node1
[2015-09-06 06:07:51] INFO - Connecting to node1:22 (root)
[2015-09-06 06:07:51] INFO - Connected and authenticated to node1.
[2015-09-06 06:07:51] INFO - Successfully authenticated on node1.
[node1]: tomcat已經啓動,進程pid:5147
[2015-09-06 06:07:52] INFO - Running task start_web on node2
[2015-09-06 06:07:52] INFO - Connecting to node2:22 (root)
[2015-09-06 06:07:52] INFO - Connected and authenticated to node2.
[2015-09-06 06:07:52] INFO - Successfully authenticated on node2.
[node2]: tomcat已經啓動,進程pid:5220
[2015-09-06 06:07:52] INFO - Running task start_web on node4
[2015-09-06 06:07:52] INFO - Connecting to node4:22 (root)
[2015-09-06 06:07:53] INFO - Connected and authenticated to node4.
[2015-09-06 06:07:53] INFO - Successfully authenticated on node4.
[node4]: tomcat已經啓動,進程pid:13178
[2015-09-06 06:07:53] INFO - Running task start_web on node5
[2015-09-06 06:07:53] INFO - Connecting to node5:22 (root)
[2015-09-06 06:07:54] INFO - Connected and authenticated to node5.
[2015-09-06 06:07:54] INFO - Successfully authenticated on node5.
[node5]: tomcat已經啓動,進程pid:23775
[2015-09-06 06:07:54] INFO - Running task start_web on node6
[2015-09-06 06:07:54] INFO - Connecting to node6:22 (root)
[2015-09-06 06:07:55] INFO - Connected and authenticated to node6.
[2015-09-06 06:07:55] INFO - Successfully authenticated on node6.
[node6]: tomcat已經啓動,進程pid:19614
[2015-09-06 06:07:55] INFO - Running task start_web on node7
[2015-09-06 06:07:55] INFO - Connecting to node7:22 (root)
[2015-09-06 06:07:56] INFO - Connected and authenticated to node7.
[2015-09-06 06:07:56] INFO - Successfully authenticated on node7.
[node7]: weblogic服務已經啓動,進程pid:26615
[2015-09-06 06:07:56] INFO - Running task start_web on node8
[2015-09-06 06:07:56] INFO - Connecting to node8:22 (root)
[2015-09-06 06:07:56] INFO - Connected and authenticated to node8.
[2015-09-06 06:07:57] INFO - Successfully authenticated on node8.
[node8]: weblogic服務已經啓動,進程pid:24851
[2015-09-06 06:07:57] INFO - Running task start_web on node9
[2015-09-06 06:07:57] INFO - Connecting to node9:22 (root)
[2015-09-06 06:07:57] INFO - Connected and authenticated to node9.
[2015-09-06 06:07:57] INFO - Successfully authenticated on node9.
[node9]: weblogic服務已經啓動,進程pid:670
[2015-09-06 06:07:58] INFO - Running task start_web on node10
[2015-09-06 06:07:58] INFO - Connecting to node10:22 (root)
[2015-09-06 06:07:58] INFO - Connected and authenticated to node10.
[2015-09-06 06:07:58] INFO - Successfully authenticated on node10.
[node10]: weblogic服務已經啓動,進程pid:9809

6,分發不一樣文件到不一樣的web服務節點上 能夠經過同步 上傳等方式均可以 以下所示:服務器

[root@localhost rex]# rex deploy_file
[2015-09-06 06:13:36] INFO - Running task deploy_file on node1
[2015-09-06 06:13:36] INFO - Connecting to node1:22 (root)
[2015-09-06 06:13:36] INFO - Connected and authenticated to node1.
[2015-09-06 06:13:36] INFO - Successfully authenticated on node1.
[2015-09-06 06:13:37] INFO - Running task deploy_file on node2
[2015-09-06 06:13:37] INFO - Connecting to node2:22 (root)
[2015-09-06 06:13:37] INFO - Connected and authenticated to node2.
[2015-09-06 06:13:37] INFO - Successfully authenticated on node2.
[2015-09-06 06:13:37] INFO - Running task deploy_file on node4
[2015-09-06 06:13:37] INFO - Connecting to node4:22 (root)
[2015-09-06 06:13:37] INFO - Connected and authenticated to node4.
[2015-09-06 06:13:37] INFO - Successfully authenticated on node4.
[2015-09-06 06:13:38] INFO - Running task deploy_shopwap on current connection
[node4]: 數據源配置更新成功
[2015-09-06 06:13:38] INFO - Running task deploy_file on node5
[2015-09-06 06:13:38] INFO - Connecting to node5:22 (root)
[2015-09-06 06:13:38] INFO - Connected and authenticated to node5.
[2015-09-06 06:13:38] INFO - Successfully authenticated on node5.
[2015-09-06 06:13:39] INFO - Running task deploy_gerenwap on current connection
[node5]: 數據源配置更新成功
[2015-09-06 06:13:39] INFO - Running task deploy_file on node6
[2015-09-06 06:13:39] INFO - Connecting to node6:22 (root)
[2015-09-06 06:13:40] INFO - Connected and authenticated to node6.
[2015-09-06 06:13:40] INFO - Successfully authenticated on node6.
[2015-09-06 06:13:40] INFO - Running task deploy_appwap on current connection
[node6]: 數據源配置更新成功
[2015-09-06 06:13:40] INFO - Running task deploy_file on node7
[2015-09-06 06:13:40] INFO - Connecting to node7:22 (root)
[2015-09-06 06:13:41] INFO - Connected and authenticated to node7.
[2015-09-06 06:13:41] INFO - Successfully authenticated on node7.
[2015-09-06 06:13:41] INFO - Running task deploy_geren on current connection
[node7]: 數據源配置更新成功
[2015-09-06 06:13:42] INFO - Running task deploy_file on node8
[2015-09-06 06:13:42] INFO - Connecting to node8:22 (root)
[2015-09-06 06:13:42] INFO - Connected and authenticated to node8.
[2015-09-06 06:13:42] INFO - Successfully authenticated on node8.
[2015-09-06 06:13:42] INFO - Running task deploy_chaoshi on current connection
[node8]: 數據源配置更新成功
[2015-09-06 06:13:43] INFO - Running task deploy_file on node9
[2015-09-06 06:13:43] INFO - Connecting to node9:22 (root)
[2015-09-06 06:13:43] INFO - Connected and authenticated to node9.
[2015-09-06 06:13:43] INFO - Successfully authenticated on node9.
[2015-09-06 06:13:43] INFO - Running task deploy_quanguo on current connection
[node9]: 數據源配置更新成功
[2015-09-06 06:13:44] INFO - Running task deploy_file on node10
[2015-09-06 06:13:44] INFO - Connecting to node10:22 (root)
[2015-09-06 06:13:44] INFO - Connected and authenticated to node10.
[2015-09-06 06:13:44] INFO - Successfully authenticated on node10.
[2015-09-06 06:13:44] INFO - Running task deploy_boss on current connection
[node10]:數據源配置更新成功

7,若是你部署Rex的系統性能比較高的話,管理的節點不少,想要經過多線程的方式併發鏈接操做各個節點能夠設置parallelism參數 好比我設置爲最大值  parallelism 'max'; 最大值是根據你要併發鏈接的節點數,默認值是單個也就是始終只有一個鏈接。好比在個人測試裏要鏈接10個節點服務器 ,在不設置該參數的狀況下以下所示:網絡

[root@localhost rex]# rex current_time
[2015-09-06 06:22:30] INFO - Running task current_time on node1
[2015-09-06 06:22:30] INFO - Connecting to node1:22 (root)
[2015-09-06 06:22:30] INFO - Connected and authenticated to node1.
[2015-09-06 06:22:30] INFO - Successfully authenticated on node1.
Sun Sep  6 18:22:20 CST 2015
[2015-09-06 06:22:31] INFO - Running task current_time on node2
[2015-09-06 06:22:31] INFO - Connecting to node2:22 (root)
[2015-09-06 06:22:31] INFO - Connected and authenticated to node2.
[2015-09-06 06:22:31] INFO - Successfully authenticated on node2.
Sun Sep  6 18:22:30 CST 2015
[2015-09-06 06:22:31] INFO - Running task current_time on node4
[2015-09-06 06:22:31] INFO - Connecting to node4:22 (root)
[2015-09-06 06:22:31] INFO - Connected and authenticated to node4.
[2015-09-06 06:22:31] INFO - Successfully authenticated on node4.
Sun Sep  6 18:23:14 CST 2015
[2015-09-06 06:22:32] INFO - Running task current_time on node5
[2015-09-06 06:22:32] INFO - Connecting to node5:22 (root)
[2015-09-06 06:22:32] INFO - Connected and authenticated to node5.
[2015-09-06 06:22:32] INFO - Successfully authenticated on node5.
Sun Sep  6 18:23:14 CST 2015
[2015-09-06 06:22:33] INFO - Running task current_time on node6
[2015-09-06 06:22:33] INFO - Connecting to node6:22 (root)
[2015-09-06 06:22:33] INFO - Connected and authenticated to node6.
[2015-09-06 06:22:33] INFO - Successfully authenticated on node6.
Sun Sep  6 18:23:09 CST 2015
[2015-09-06 06:22:34] INFO - Running task current_time on node7
[2015-09-06 06:22:34] INFO - Connecting to node7:22 (root)
[2015-09-06 06:22:34] INFO - Connected and authenticated to node7.
[2015-09-06 06:22:34] INFO - Successfully authenticated on node7.
Sun Sep  6 18:23:46 CST 2015
[2015-09-06 06:22:34] INFO - Running task current_time on node8
[2015-09-06 06:22:34] INFO - Connecting to node8:22 (root)
[2015-09-06 06:22:35] INFO - Connected and authenticated to node8.
[2015-09-06 06:22:35] INFO - Successfully authenticated on node8.
Sun Sep  6 18:25:22 CST 2015
[2015-09-06 06:22:35] INFO - Running task current_time on node9
[2015-09-06 06:22:35] INFO - Connecting to node9:22 (root)
[2015-09-06 06:22:35] INFO - Connected and authenticated to node9.
[2015-09-06 06:22:35] INFO - Successfully authenticated on node9.
Sun Sep  6 18:23:48 CST 2015
[2015-09-06 06:22:36] INFO - Running task current_time on node10
[2015-09-06 06:22:36] INFO - Connecting to node10:22 (root)
[2015-09-06 06:22:36] INFO - Connected and authenticated to node10.
[2015-09-06 06:22:36] INFO - Successfully authenticated on node10.
Sun Sep  6 18:25:23 CST 2015

這樣的狀況下就會一個一個的進行操做 若是管理的節點多的話 很是浪費時間。在設置最大值的狀況下,由於個人服務器是10臺(該參數也能夠指定數字)我直接設置成max 運行後以下所示:多線程

[root@localhost rex]# rex current_time
[2015-09-06 06:25:25] INFO - Running task current_time on node2
[2015-09-06 06:25:25] INFO - Connecting to node2:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node1
[2015-09-06 06:25:25] INFO - Connecting to node1:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node4
[2015-09-06 06:25:25] INFO - Connecting to node4:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node5
[2015-09-06 06:25:25] INFO - Connecting to node5:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node7
[2015-09-06 06:25:25] INFO - Connecting to node7:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node8
[2015-09-06 06:25:25] INFO - Connecting to node8:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node6
[2015-09-06 06:25:25] INFO - Connecting to node6:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node9
[2015-09-06 06:25:25] INFO - Connecting to node9:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node10
[2015-09-06 06:25:25] INFO - Connecting to node10:22 (root)
[2015-09-06 06:25:25] INFO - Connected and authenticated to node2.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node1.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node2.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node1.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node4.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node7.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node8.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node10.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node9.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node5.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node6.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node4.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node10.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node8.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node6.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node5.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node7.
[2015-09-06 06:25:26] INFO - Successfully authenticated on node9.
Sun Sep  6 18:25:24 CST 2015
Sun Sep  6 18:25:15 CST 2015
Sun Sep  6 18:28:12 CST 2015
Sun Sep  6 18:28:12 CST 2015
Sun Sep  6 18:26:08 CST 2015
Sun Sep  6 18:26:08 CST 2015
Sun Sep  6 18:26:01 CST 2015
Sun Sep  6 18:26:38 CST 2015
Sun Sep  6 18:26:38 CST 2015

速度很是快 ,固然實際生產中,能夠根據系統的負載能力 網絡情況 管理服務節點數量 等來設置適合的值併發


至於鏈接超時時間 線程數 認證登錄方式 服務器信息配置,文件操做,系統服務管理,文件管理等等均可以在Rexfile中定義也能夠以模塊的形式來進行定義。rex還提供模板功能,具體還在研究。後續會繼續更新。。。app

建議學習rex的時候要認真的看perldoc文檔可能有些函數在新版本中已經被幹掉了,rex的管理很簡單就是經過ssh鏈接而後經過一堆命令去管理對端的服務器。各類命令以及模塊能夠經過perldoc Rex::Commands查看,基本上介紹的也很細,而後經過實驗多實踐很快就能掌握!


若是任何一個鏈接操做失敗的話 都會打印出相關的信息,根據信息能夠排查錯誤的緣由!

相關文章
相關標籤/搜索