zoukankan      html  css  js  c++  java
  • 使用模块和模板:

    
    使用模块和模板:
    
    上传硬编码配置文件到服务器通常是不够的.
    
    有时候你必须动态的增加至到一个文件
    
    比如  如果你需要绑定一个服务到一个指定的网络接口或者配置不同的数据库对于不同的应用环境
    
    这个可以通过模板实现
    
    在这个例子你可以学会创建一个ntp 模块,上传自定义的ntp.conf文件用于你的测试 ,
    
    预生产环境和生产环境
    
    
    首先,我们创建一个新的模块。对于这个指南你需要至少Rex0.41(因为--create-module命令和case关键字)
    
    执行下面的命令在相同的目录 
    
    [root@node01 my-first-rex-project]# rexify Service::NTP --create-module
    Creating module Service::NTP...
       mkdir lib/Service/NTP
       Creating template file: __module__.pm
    11111111111112222334414
       Creating template file: meta.yml
    11111111111112222334414
    
    Your module has been created in lib/Service/NTP.
    [root@node01 my-first-rex-project]# 
    
    
    This will create some new directories and files in your lib directory.
    
    .
    ├── Rexfile
    └── lib
        └── Service
            └── NTP
                ├── __module__.pm
                └── meta.yml
    			
    meta.yml 文件可以被忽略, 这个文件是重要的只有你需要共享i的模块在Rex模块目录
    
    最重要的文件是 __module__.pm.  打开这个文件使用一个文本编辑器,
    
    这个文件是一个普通的Perl模块。
    
    唯一特别的是文件名,但是一开始不要想太多
    
    package Service::NTP;
    use Rex -feature => ['1.3'];
    
    task prepare => sub {
       my $service_name = case operating_system, {
                             Debian  => "ntp",
                             default => "ntpd",
                          };
       pkg "ntp",
         ensure => "present";
    
       file "/etc/ntp.conf",
          source    => "files/etc/ntp.conf",
          on_change => sub {
             service $service_name => "restart";
          };
    
       service $service_name,
         ensure => "started";
    };
    
    1;
    
    
    首先这个模块检查如果OS是一个debian(或者ubuntu) 设置服务名为ntp 否则为ntpd
    
    
    然后它安装ntp包和上传配置文件。
    
    注意 on_change 这里,这个会重启ntp 服务如果文件改变
    
    最后Rex 验证服务会启动在系统启动时, 现在创建基本的ntp.conf文件 
    
    
    创建目录 lib/Service/NTP/files/etc 防止ntp.conf文件
    
    如果你需要分配不同的ntp.conf 文件  你可以增加不同的ntp.conf files到不同的目录 
    
    Rex 会然后 选择-E $env cli 参数 来绝对实用哪个文件
    
    Rex 首先尝试找到文件名为ntp.conf.
    
    
    使用模块:
    
    使用你的模块 ,你需要增加它到你的Rexfile.
    
    use Rex -feature => ['1.3'];
    user "root";
    password "foo";
    
    require Service::NTP;
    
    1;
    
    
    
    
    
    [root@node01 NTP]# cat __module__.pm
    package Service::NTP;
    use Rex -feature => ['1.3'];
    
    task prepare => sub {
       my $service_name = case operating_system, {
                             Debian  => "ntp",
                             default => "ntpd",
                          };
       pkg "ntp",
         ensure => "present";
    
       file "/etc/ntp.conf",
          source    => "files/etc/ntp.conf",
          on_change => sub {
             service $service_name => "restart";
          };
    
       service $service_name,
         ensure => "started";
    };
    
    1;
    
    
    
    
    
    [root@node01 my-first-rex-project]# rex -H 192.168.137.2  Service:NTP:prepare
    [2019-06-18 08:14:55] INFO - Running task Service:NTP:prepare on 192.168.137.2
    [2019-06-18 08:14:57] INFO - Installing ntp.
    [2019-06-18 08:15:42] INFO - Service ntpd restarted.
    [2019-06-18 08:15:43] INFO - All tasks successful on all hosts
    [root@node01 my-first-rex-project]# 
    
    
    [root@node01 my-first-rex-project]# chkconfig --list | grep ntp
    ntpd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
    ntpdate        	0:off	1:off	2:off	3:off	4:off	5:off	6:off
    [root@node01 my-first-rex-project]# chkconfig ntpd off
    [root@node01 my-first-rex-project]# chkconfig --list | grep ntp
    ntpd           	0:off	1:off	2:off	3:off	4:off	5:off	6:off
    ntpdate        	0:off	1:off	2:off	3:off	4:off	5:off	6:off
    [root@node01 my-first-rex-project]# rex -H 192.168.137.2  Service:NTP:prepare
    [2019-06-18 08:17:18] INFO - Running task Service:NTP:prepare on 192.168.137.2
    [2019-06-18 08:17:21] INFO - All tasks successful on all hosts
    [root@node01 my-first-rex-project]# chkconfig --list | grep ntp
    ntpd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
    ntpdate        	0:off	1:off	2:off	3:off	4:off	5:off	6:off
    [root@node01 my-first-rex-project]# 
    
    
    
    
    
  • 相关阅读:
    IDEA 配置Springboot项目热部署
    一文读懂类加载机制
    面试必问的MySQL锁与事务隔离级别
    工作中遇到的99%SQL优化,这里都能给你解决方案(三)
    谁有好的oracle数据库学习书籍,麻烦提供一下,感激不尽
    静态资源上传至远程ftp服务器,ftp工具类封装
    进程和线程,并发和并行,同步和异步,高并发和多线程,理一理概念
    使用springboot集成腾讯云短信服务,解决配置文件读取乱码问题
    曾经天真的以为单例只有懒汉和饿汉两种!原来单例模式还能被破解!!!
    了解一下zookeeper,搭建单机版和集群版的环境玩玩,需要手稿的,留下邮箱
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348710.html
Copyright © 2011-2022 走看看