PerlChina Advent Day 1

15 visningar
Hoppa till det första olästa meddelandet

Fayland Lam

oläst,
1 dec. 2014 21:39:582014-12-01
till perl...@googlegroups.com
Rexify

=for advent_year 2014

=for advent_day 1

=for advent_title Rexify

=for advent_author Chenlin Rao

=encoding utf8

Perl 一直以来都是系统管理员们的好朋友。早在L<09 年的 advent Day 5|../../2009/05/SSHBatch.pod> 上,就介绍过一个 Perl 工具叫 L<SSH::Batch>。随着时代变迁,更完备的集群架构管理工具 Puppet、SaltStack、Ansible 等大行其道。在这方面,Perl 社区也出现了新的项目,这就是 (R)?ex 项目。

(R)?ex 项目的设计以配置管理和部署为核心。这个 (R)? ,可以是 SSH 到远程主机,可以是直接到本地主机,也可以是调用 API 控制各大云平台。

(R)?ex 本身的 DSL 设计,是仿照了 Puppet 的语法。(作者本身是德国一位 IDC 运维,本身手上也有 Puppet 环境)

=head2 资源定义语法示例

=begin code

user "ubuntu";
group frontend => "frontend[01..05]";
sudo TRUE;

desc "Prepare Frontend Server";
task "prepare", group => "frontend", sub {
  pkg "apache2",
    ensure => "latest";

  service "apache2",
    ensure => "started";
};

desc "Keep Configuration in sync";
task "configure", group => "frontend", sub {
  prepare();

  file "/etc/apache2/apache2.conf",
    source    => "files/etc/apache2/apache2.conf",
    on_change => sub { service apache2 => "reload"; };
};

=end code

非常一目了然,之前没用过的人也可以一眼看懂这段配置是要干嘛——安装最新的 apache 软件包,启动 apache 服务,传输本地的 "files/etc/apache2/apache2.conf" 到远程主机的 "/etc/apache2/apache2.conf",然后重载 apache 服务。

=head2 即时运行

如果 Rex 止于这种程度,那么我就建议大家直接使用 Puppet 好了。Puppet 的一个问题:无法即时生效。在 Rex 中,实际就是一个很类似之前 SSH::Batch 的用法:

=begin code

$ rex -qw -H "frontend[01..15] middleware[01..05] db[01..04]" -e "say run 'uptime'"

=end code

=head2 云平台 API

云平台,其实也就是一种 Rex 内部资源,所以其使用方式跟普通的 pkg/file/service/cron/ps/tail 等资源并无不同:

=begin code

use Rex::Commands::Cloud;

user "root";
public_key "/path/to/your/just/created/amazon-public.key";
private_key "/path/to/your/just/downloaded/amazon-private-key.pem";

my $access_key = "your-access-key";
my $secret_access_key = "your-secret-key";

cloud_service "Amazon";
cloud_auth "$access_key", "$secret_access_key";

task "create", sub {
   cloud_instance create => { 
            image_id => "ami-02103876",
            name     => "static01",
            key      => "dev-test",
         };
};

=end code

大家可能注意到上面配置中多了一行 C<use Rex::Commands::Cloud>,事实上大家可以任意自己扩展 Rex::Commands 接口,实现自己的资源(名字空间叫 Rex::Commands 而不是 Rex::Resources,倒也可以说明 Rex 的实质,依然是一个命令执行式的工具)。

事实上,Rex 有专门的 modules.rexify.org 来收集发布各种第三方资源。之前,就有国内的朋友制作了 Rex::Commands::Zabbix2 模块。

=head2 web 界面

任何自动化工具最终都会走向 web 界面展示。Rex 最近用 Mojolicious 框架完成了官方的 JobControl 界面。这里就不占用篇幅了,大家可以自行访问官网文档:L<http://www.rexify.org/howtos/jobcontrol/create_project.html>

最后值得说明的是:Rex 官网有中文同步翻译,地址见 L<http://rex.perl-china.com>

http://perlchina-advent.herokuapp.com/calendar/2014/01/


Thanks

Hao Wu

oläst,
1 dec. 2014 22:51:422014-12-01
till perl...@googlegroups.com
使用rex,可能会遇到一个问题:
如何获得命令行参数? 参考。




--
您收到此邮件是因为您订阅了Google网上论坛中的“PerlChina Mongers 讨论组”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到perlchina+...@googlegroups.com
要发帖到此论坛,请发送电子邮件至perl...@googlegroups.com
通过http://groups.google.com/group/perlchina访问此论坛。
要查看更多选项,请访问https://groups.google.com/d/optout

Ken Lam

oläst,
2 dec. 2014 08:04:112014-12-02
till perl...@googlegroups.com
感谢!!!

于 14/12/2 上午10:39, Fayland Lam 写道:

chenlin rao

oläst,
4 dec. 2014 09:42:362014-12-04
till perl...@googlegroups.com
jfried 刚说他看到这篇advent了~

wind.chh

oläst,
7 dec. 2014 22:59:282014-12-07
till perl...@googlegroups.com
正好借地问个问题, Rex里面怎么获得传入的host值,

比如,我在代码里面需要对不同的主机做出不同的动作, Rex传入的主机怎么获取到呢 ?

有段时间没有用Rex了, 不知道有没有更新。

在 2014年12月2日星期二UTC+8上午10时39分58秒,Fayland Lam写道:

chenlin rao

oläst,
7 dec. 2014 23:17:242014-12-07
till perl...@googlegroups.com
task里直接用connection->server就可以获得执行task的当前主机的地址。比如:

```perl
task "dltxt" => sub {
    my $host = connection->server;
    download "/var/log/system.log", "./syslog/$host.log";
};
```

--

wind.chh

oläst,
8 dec. 2014 03:23:482014-12-08
till perl...@googlegroups.com
Thanks.

在 2014年12月8日星期一UTC+8下午12时17分24秒,chenlinux写道:
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到perlchina+unsubscribe@googlegroups.com
Svara alla
Svara författaren
Vidarebefordra
0 nya meddelanden