设为首页 收藏本站
查看: 1543|回复: 0

[经验分享] 运维笔记

[复制链接]

尚未签到

发表于 2017-6-25 11:14:29 | 显示全部楼层 |阅读模式
Linux 运维



Linux 运维DHCPDNS源码安装bindDNS && Bind授权根线索root hint推荐的SOA时间设置邮件服务安装postfixpostifx配置文件基本配置Postfix + SASL 用户认证muttsmtpd邮件发送过程客户端访问限定实例aliasesDovecot虚拟域courier-authlib配置dovecot安装extmail邮件加密反垃圾/病毒邮件CobblerPrimativesCommands安装Operationcobbler_webAnsible安装配置YAMLNMSSNMPCatiZabbixPuppetArchRubyPuppet工作Puppet安装Puppet资源groupusercronpackageservicefileexecnotifyPuppet变量puppet正则表达式变量作用域内置变量Puppet操作符比较操作符布尔比较符算数运算符puppet控制语句if statementcaseSelectors模块puppet类模板Master/Agent模型Puppet的配置文件Puppet命令总结Puppet的模块管理:Puppet dashboardPuppet kick功能制作RPM包创建spec文件配置文件prep section: 准备阶段build section: 编译阶段install sesion: 安装阶段clean section: 清理段file section: 文件段changelog section: 改变日志段脚本段RPM SRCSPECaddiontalNaming Package定义软件的依赖关系设定build目录定义源码文件Patch安装制作RPM%packagefilesPGP附加小工具Shellinabox

DHCP
  C/S架构
  Server: DHCP Server 67/UDP Client: DHCP Clinet 68/UDP

Client                       Server
|        DHCP DISCOVER        |
|---------------------------->|
|     DHCP OFFER(ip/netmask)  |
|<----------------------------|
|        DHCP REQUEST         |
|---------------------------->|
|        DHCP ACK             |
|<----------------------------|
  如果DHCP Client和Server不在一个子网内, 可以使用DHCP relay的方式中继到Server端获取IP
  dhcpd.conf 配置文件

option domain-name "example.com";
option domain-name-servers 192.168.48.130;
subnet 192.168.48.0 netmask 255.255.255.0 {
range 192.168.48.140 192.168.48.240;
option routers 192.168.168.130;
}
next-server 192.168.48.130;
filename="pxelinux.0";
DNS

源码安装bind

./configure --prefix=/usr/local/bind9 --sysconfdir=/etc/named/ --enable-threads --enable-epoll --disable-chroot
make && make install
  创建bind配置文件/etc/named/named.conf

options{
pid-file "/usr/local/bind9.10/var/run/named.pid";
directory "/var/named";
forward only;
forwarders { 192.168.48.2; 8.8.8.8; };
recursion yes;
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
};
logging {
channel default_log {
file "/var/log/named/dns-default.log"  versions 10 size 1m;
severity debug;
print-time yes;
print-category yes;
};
category queries {
default_log ;
};
};
key "rndc-key" {
algorithm hmac-md5;
secret "ijlVav7SqHIsDQrusKqZMg==";
};
controls {
inet 127.0.0.1 port 953
allow { 127.0.0.1; } keys { "rndc-key"; };
};
include "/etc/named/named.rfc1912.zones";
  创建根named.ca

dig -t NS . @192.168.48.130 >/var/named/named.ca
  创建localhost.ca

$TTL 86400
@INSOAlocalhost.admin.localhost. (
2014031101
2H
10M
7D
1D )
INNSlocalhost.
localhost.INA127.0.0.1
  创建named.loopback

$TTL 86400
@INSOAlocalhost.admin.localhost. (
2014031101
2H
10M
7D
1D )
INNSlocalhost.
1INPTRlocalhost.
  创建配置文件example.com.zone

$TTL 600
@       IN      SOA     dns.example.com.         hugh.sun.ericsson.com(
2015091010
1H
5M
3D
12H)
@               IN      NS      dns
IN      MX  10  mail
IN      NS      ns2
tech            IN      NS      dns.tech
client2         IN      A       192.168.48.134
ns2             IN      A       192.168.48.133
dns             IN      A       192.168.48.133
www             IN      A       192.168.48.133
mail            IN      A       192.168.48.133
client1 IN      A       172.16.100.7
centos          IN      A       192.168.48.130
ftp             IN      CNAME   www
pop             IN      CNAME   mail
server          IN      CNAME   www
mysql   IN      A       192.168.48.135
mysql2  IN      A       192.168.48.136
node1   IN      A       192.168.48.131
node2   IN      A       192.168.48.132
node3   IN A   192.168.48.137
node4   IN  A   192.168.48.138
  创建192.168.48.zone

$TTL 600
@       IN      SOA     dns.example.com         hugh.sun.ericsson.com(
2015091005
1H
5M
3D
12H)
@       IN      NS      dns.example.com.
IN      NS      dns2.example.com.
133     IN      PTR     dns2.example.com.
130     IN      PTR     www.example.com.
133     IN      PTR     mail.example.com.
  创建example.right.com.zone

$TTL 600
@       IN      SOA     dns.example.com         hugh.sun.ericsson.com (
2015091009
1H
5M
3D
12H)
@               IN      NS      dns2
IN      MX  10  mail
dns2            IN      A       172.16.100.5
www             IN      A       172.16.100.5
mail            IN      A       172.16.100.5
ftp             IN      CNAME   www
pop             IN      CNAME   mail
  添加用户namd并创建环境

groupadd -g 53 -r named
useradd -g named -r named
chown root:named /etc/named/* /var/named/*
chown named:named /usr/local/bind9/ -R
mkdir -pv /var/log/named/
chown named:named /var/log/named -R
chown 640 /etc/named/named.conf /var/named/*
echo 'export PATH=/usr/local/bind9/bin:/usr/local/bind9/sbin:$PATH' > /etc/profile.d/named.sh
source /etc/profile.d/named.sh
  创建RNDC key

rndc-confgen -r /dev/urandom > /etc/named/rndc.conf
chown root:named /etc/named/rndc.conf
chmod 640 /etc/named/rndc.conf
把rndc.conf文件的后半部分复制到named.conf中并按指示启用;
  查看配置语法

named-checkconf
named-checkconf /etc/named.conf
named-checkzone "example.com" example.com.zone
  serviceV scripts

#!/bin/bash
#
# description: named daemon
# chkconfig: - 13 87
#
pidFile=/usr/local/bind9/var/run/named.pid
lockFile=/var/lock/subsys/named
confFile=/etc/named/named.conf
[ -r /etc/rc.d/init.d/functions ]&& . /etc/rc.d/init.d/functions
start() {
if [ -e $lockFile ];then
echo "Named is already running..."
exit 0
fi
echo -n " Starting named: "
daemon --pidfile $pidFile /usr/local/bind9/sbin/named -u named -c $confFile
RETVAL=$?
echo
if [ $RETVAL -eq 0 ];then
touch $lockFile
return $RETVAL
else
rm -f $lockFile $pidFile
return 3
fi
}
stop() {
if [ ! -e $lockFile ];then
echo "named is stopped."
return 0
fi
echo -n "Stopping named:"
killproc named
RETVAL=$?
echo
if [ $RETVAL -eq 0 ]; then
rm -f $lockFile $pidFile
return 0
else
echo "Canot stop named."
failure
return 1
fi
}
restart(){
stop
sleep 2
start
}
reload (){
echo -n "Reloading named: "
killproc named -HUP
RETVAL=$?
echo
return $RETVAL
}
status(){
if  pidof named &>/dev/null ;then
echo -n "named is running..."
success
echo
else
echo -n "named is stopped..."
success
echo
fi
}
usage(){
echo "Usage: named {start|stop|restart|status|reload}"
}
case $1 in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
reload)
reload;;
*)
usage;
exit 4
;;
esac

DNS && Bind
  顶级域名有七个
  com 商业组织,例如惠普( hp.com)、 Sun 公司( sun.com),还有 IBM( ibm.com)。 edu 教育机构,例如加州大学伯克利分校 ( berkeley.edu)和普渡大学 ( purdue.edu)。 gov 政府部门,例如 NASA( nasa.gov)和美国国家科学基金会( nsf.gov)。 mil 军事部门,例如美国陆军( army.mil)和美国海军( navy.mil)。 net 通常是提供网络基础设施的组织, 例如NSFNET( nsf.net)和UUNET ( uu.net)。 但是从 1996 年起, *net *与 *com *一样,向任何商业组织开放。 org 通常是非盈利性组织,例如 Electronic Frontier Foundation( eff.org)。不过, 与 *net *一样,在 1996 年对 *org *的限制也取消了。 int 国际组织,例如 NATO( nato.int)。

授权
  分散管理通过授权实现(delegation)
  权威服务器的选择:
  BIND名字服务器使用一种称为 “往返时间” ( roundtrip time)或RTT的度量 ( metric) 对同一个区的权威名字服务器进行选择。 往返时间是指远程名字服务器响应查询的时间长度。每次 BIND 名字服务器向远程名字服务器发送查询时,都启动一个内部计时器。当它收到响应时就停止计时,记录下该远程名字服务器过了多长时间才响应。当名字服务器要选择向哪个名字服务器发送查询时,它就选择具有最小 RTT的名字服务器
  在 BIND 名字服务器查询某个名字服务器之前,先给它一个随机的 RTT 值,不过这个值比任何真实的 RTT 值都要小。这就保证在根据真实 RTT 值选择之前, BIND 名字服务器会以随机的顺序查询某个区的所有名字服务器。
  注册机构 ( registry)、登记员 ( registrar)和注册( registration)

根线索root hint
  ftp.rs.internic.net(198.41.0.6)
  named.root文件
  将Bind4的配置转换成Bind8的配置
  named-bootconf

推荐的SOA时间设置
  刷新 24 小时 重试 2 小时 期满 30 天 默认 TTL 4 天

重试123



0
5s
2*5s
3*5s


1
10s
2*5s
3*3s


总数
15s
20s
24s






  sortlist指令: 如果DNS返回多个IP值时, 比如一台主机多个网卡, 可以选择优先使用的网络地址
  sortlist 128.32.42.0/255.255.255.0

邮件服务
  rhel5: sendmail
  rhel6: postfix
  SMTP: Simple Mail Transfer Protocol 无法实现用户认证
  ESMTP: 实现检测, 无法进行认证
  POP3: Post Office Protocol, 邮局协议3
  IMAP4: Internet Mail Access Protocol, IMAP4功能比POP3强大,但是IMAP3更加消耗资源
  UUCP: Unix to Unix Copy: Unix主机复制文件的协议
  SMTP本身可以实现传输路由的功能, C/S架构 sendmail, smtpd(TCP:25)
  邮件传输: MT
  邮件投递: MD
  邮件用户: MU
  MUA: Mail User Agent 邮件用户代理. 用于编写邮件的工具. eg: outlook
  lmtp: local mail transfer protocol 本地邮件传输协议
  SASL: Simple Authentication Secure Layer, 简单认证安全层
  LDAP: Lightweight Directory Access Protocol
  虚拟用户: 用于访问某服务的数字表示; 用户;字符串;凭证
  MTA: 邮件传输代理, SMTP服务器.
  ​ sendmail, UUCP协议. 单体结构, 使用SUID, 配置文件语法(m4语言编写)
  ​ qmail. 性能强, 但是维护不利
  ​ postfix: 模块化设计, IBM安全专家设计. 跟sendmail兼容. 投掷效率是sendmail的四倍以上, 效率高
  ​ exim: 剑桥大学的开源软件. (xen也是)
  ​ Exchange: windows平台的邮件服务器, 异步消息协作平台, 可以共享日历等. 使用Exchange必须要跟AD整合使用.
  MDA: 邮件投递代理
  ​ procmail
  ​ maildrop
  MRA: (pop3, imap4)
  ​ cyrus-imap
  ​ dovecot
  MUA: Mail User Agent
  ​ Outlook Express(简装版), Outlook
  ​ Foxmail (被腾讯收购)
  ​ Thunderbird
  ​ Evolution
  ​ mutt(文本界面)
  Webmail:
  ​ Openwebmail (perl)
  ​ squirrelmail (php)
  ​ Extmail(Extman)
  ​ EMOS, EXT mail OS
  SASL: V1, V2, 目前V2使用比较多
  ​ cyrus-sasl1
  ​ courier

安装postfix
  Postfix + SASL (courier-authlib)
  Dovecot + MySQL
  Extmail + Extman + httpd
  官方站点: www.postfix.org
  安装所需rpm包

yum install httpd mysql mysql-server mysql-devel openssl-devel dovecot perl-DBD-MySQL tcl tcl-devel libart_lgpl libart_lgpl-devel libtool-ltdl libtool-ltdl-devel expect cyrus-sasl-devel gcc db4-devel
  启动saslauthdfuwu,并将其加入到自动启动队列:

service saslauthd start
chkconfig saslauthd on
  安装配置postfix

groupadd -g 2525 postfix
useradd -g postfix -u 2525 -s /sbin/nologin -M postfix
groupadd -g 2526 postdrop
useradd -g postdrop -u 2526 -s /sbin/nologin -M postdrop
  编译安装postfix

make makefiles 'CCARGS=-DHAS_MYSQL -I /usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I /usr/include/sasl -DUSE_TLS ' 'AUXLIBS=-L/usr/lib64/mysql -lmysqlclient -lz -lm -L/usr/lib64/sasl2 -lsasl2 -lssl -lcrypto'
make
make install
  smtps, pop3s, imaps. 默认为明文传输

postifx配置文件
  模块化, 主进程master: /etc/postfix/master.cf
  mail: /etc/postfix/main.cf 邮件服务的配置文件
  参数=值: 参数必须写在行的绝对行首, 以空白字符开头的行被认为是上一行的延续.
  postconf: 配置postfix
  ​ -d: 显示默认的配置
  ​ -n: 显示修改了的配置
  ​ -m: 显示支持的查找表类型
  ​ -A: 显示sasl可支持的服务类型
  ​ -a: 服务器端支持的SASL插件类型
  ​ -e PARAMETER=VALUE: 更改某参数配置信息, 并保存至main.cf文件中
  清空mailq

postsuper -d ALL
基本配置
  修改/etc/postfix/main.cf
  myorigin 邮件地址伪装
  允许使用$PARAMETER来引用响应参数的值
  mydestination 指明本机域名, 如果不是mydestination的域名, 则中继
  mynetworks 定义可以中继的地址空间

myhostname = node1.example.com
myorigin = example.com
mydomain = example.com
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 192.168.48.0/24, 127.0.0.1/8
  配置范例

queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
mydestination = $myhostname, $mydomain, localhost, node1.$mydomain
unknown_local_recipient_reject_code = 550
mynetworks = 192.168.48.0/24, 127.0.0.0/8
debug_peer_level = 2
debugger_command =
sendmail_path = /usr/sbin/sendmail
newaliases_path = /usr/bin/newaliases
mailq_path = /usr/bin/mailq
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/local/man
sample_directory = /etc/postfix
readme_directory = no
inet_protocols = ipv4
  SystemV配置脚本

#/bin/bash
# postfix Postfix Mail Transfer Agent
# chkconfig: 2345 80 30

. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ $NETWORKING = "no" ] && exit 3
[ -x /usr/sbin/postfix ] || exit 4
[ -d /etc/postfix ] || exit 5
[ -d /var/spool/postfix ] || exit 6
RETVAL=0
prog="postfix"
start(){
echo -n $"Starting postfix: "
/usr/bin/newaliases >/dev/null 2>&1
/usr/sbin/postfix start 2>/dev/null 1>&2 && success||failure $"$prog start"
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/postfix
echo
return $RETVAL
}
stop(){
echo -n $"Shutting down postfix: "
/usr/sbin/postfix stop 2>/dev/null 1>&2 && success || failure $"$prog stop"
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/postfix
echo
return $RETVAL
}
reload(){
echo -n $"Reloading postfix:"
/usr/sbin/postfix reload 2>/dev/null 1>&2 && success ||failure $"$prog reload"
RETVAL=$?
echo
return $RETVAL
}
abort(){
/usr/sbin/postfix abort 2>/dev/null 1>&2 && success || failure $"$prog abort"
return $?
}
flush(){
/usr/sbin/postfix flush 2>/dev/null 1>&2 && success || failure $"$prog abort"
return $?
}
check(){
/usr/sbin/postfix check 2>/dev/null 1>&2 && success || failure $"$prog check"
return $?
}
restart(){
stop
start
}
case "$1" in
start)
start ;;
stop)
stop ;;
restart)
stop
start
;;
reload)
reload
;;
abort)
abort
;;
flush)
flush
;;
check)
check
;;
status)
status master
;;
condrestart)
[ -f /var/lock/subsys/postfix ]&& restart || :
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|abort|flush|check|status|condrestar
t}"
exit 1
esac
Postfix + SASL 用户认证
  启动sasl, 启动sasl服务
  /etc/rc.d/init.d/saslauthd
  /etc/sysconfig/saslauthd
  查看sasl支持的认证方式, sasl本身不提供认证, 他只是一个认证框架

saslauthd -v
  使用testsaslauthd测试认证

testsaslauthd -u openstack -p openstack
  在main.cf文件中添加CYRUS-SASL的相关配置

###########################CYRUS-SASL###########################################
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_invalid_hostname,reject_non_fqdn_hostname,reject_unknown_sender_domain,reject_non_fqdn_sender,reject_non_fqdn_recipient,reject_unknown_recipient_domain,reject_unauth_pipelining,reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_sasl_path = smtpd
smtpd_banner = Welcome to our $myhostname ESMTP, Warning: Version not Available!
  编辑/usr/lib/sasl2/smtpd.conf

pwcheck_method:saslauthd
mech_list: LOGIN PLAIN
  修改/etc/sysconfig/saslauthd

MECH=shadow
  验证用户名密码时要使用base64编码

echo -n "hadoop"|openssl base64
aGFkb29w
mutt
  字符型MUA
  指定邮件目录

mutt -f PROTOCOL://mailFQDN@mailserver
mutt -f pop://openstack@example.com@mail.example.com
smtpd邮件发送过程
  smtp --> smtpd HELO信息
  smtpd --> smtp Yes信息
  smtp --> smtpd 发送Mail From信息
  smtpd --> smtp 接受mail from(但是smtpd并不会去验证发件人的信息)
  smtp --> smtpd 发送rcpt To信息
  如果mail to的收件人是本地用户, 则直接发送给本地用户, 否则中继给之其他邮件服务器.
  smtp --> smtpd 邮件正文data, 空白行+ "." 表示正文发送完毕
  对邮件服务器而言,1xx说明信息, 2xx正确类信息, 3xx上一步操作尚未完成, 需要进一步补充 4xx表示暂时性错误, 5xx表示永久性错误
  smtp协议命令:
  ​ helo(SMTP协议)
  ​ ehlo(esmtp协议)
  ​ mail from: 指明发件人
  ​ rcpt to: 指定收件人 收件人的邮件服务必须要有MX, A, 以及PTR记录. 如果没有PTR记录则会被丢弃

客户端访问限定
  connection: smtpd_client_restrictions =
  helo: smtpd_helo_restrictions =
  mail from: smtpd_sender_restrictions =
  rcpt to: smtpd_recipient_restrictions =
  data: smtpd_data_restrictions =
  默认配置 smtpd_recipicent_restrictions = permit_mynetworks, reject_unauth_destination
  查找表, 访问控制文件

connection: smtpd_client_restrictions = check_client_access hash:/etc/postfix/access
helo: smtpd_helo_restrictions = check_helo_access mysql:/etc/postfix/mysql_user
/etc/postfix/access --> hash格式 --> /etc/postfix/access.db
obama@aol.com reject
microsoft.com OK
实例
  禁止192.168.48.1主机发邮件
  编辑/etc/postfix/access
  192.168.48.1 REJECT
  将此文件转换为hash格式
  postmap /etc/postfix/access
  配置/etc/postfix/main.cf

smtpd_client_restrictions = check_client_access hash: /etc/postfix/access
  禁止向某个域发送邮件
  avaya.com REJECT
  拒绝收件人
  smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/recipient, permit_mynetworks, reject_unauth_destination
  hash类的检查表格式使用如下类型
  pattern action
  parttern格式如下

user@domain
domain.tld
user@
  Action格式

OK接受其pattern匹配的邮件地址或者主机名称/地址
4NNtext
5NNtext
REJECT optional text
DEFER optional text
aliases
  修改/etc/aliaese

a:              hadoop
tomcat:         hadoop
b:              openstack
  邮件别名: alias. 实际发送到postmaster@example.com
  依赖于/etc/aliases --> hash --> /etc/aliases.db
  newaliases可以完成散列的过程

abc@example.com: postmaster@example.com
  使用telnet进行测试

telnet localhost 25
helo localhost
mail from:root
rcpt to:openstack
Subject: How are you
To: openstack@example.com
Are you doing good.
.
  postfix默认把本机的IP地址所在的网络识别为本地网络, 并且为之中继邮件

Dovecot
  支持四中协议:pop3, imap4, pops, imaps
  配置文件: /etc/dovecot.conf
  有SASL认证能力
  邮箱格式:
  ​ mbox: 一个文件存储所有邮件
  ​ maildir: 一个文件存储一封邮件, 所有邮件存储在一个目录中
  修改dovecot的配置文件/etc/dovecot/dovecot.conf

protocols = imap pop3
  vim /etc/dovecot/conf.d/10-mail.conf

mail_location = mbox:~/:INBOX=/var/spool/mail/%u
chmod 0600 /var/mail*
service dovecot restart
  收邮件

telnet mail.example.com 110
USER openstack
PASS openstack
LIST
RETR 1
虚拟域
  一台服务器为镀铬域手法邮件
  若要使用虚拟域, 则需要先取消中心域

courier-authlib
  http://www.courier-mta.org/download.html
  安装包

httpd, mysql, mysql-server, mysql-devel, openssl-devel, dovecot, perl-DBD-MySQL, tcl, tcl-devel, libart_lgpl, libart_lgpl-devel, libtool-ltdl, libtool-ltdl-devel, expect, dovecot-mysql
  编译安装courier-authlib

./configure \
--prefix=/usr/local/courier-authlib \
--sysconfdir=/etc \
--without-authpam \
--without-authshadow \
--without-authvchkpw \
--without-authpgsql \
--without-authsqlite \
--with-authmysql \
--with-mysql-libs=/usr/lib64/mysql \
--with-mysql-includes=/usr/include/mysql \
--with-redhat \
--with-authmysqlrc=/etc/authmysqlrc \
--with-authdaemonrc=/etc/authdaemonrc \
--with-mailuser=postfix \
--with-mailgroup=postfix \
--with-ltdl-lib=/usr/local/libtool-2.4.6/lib \
--with-ltdl-include=/usr/local/libtool-2.4.6/include
  --with-authdaemonvar=/var/spool/authdaemon 选项来指定进程套接字目录路径
  报错: configure: error: invalid ltdl library directory: `/usr/lib64/'

wget http://ftpmirror.gnu.org/libtool/libtool-2.4.6.tar.gz
tar zxvf libtool-2.4.6.tar.gz
./configure --prefix=/usr/local/libtool-2.4.6 --enable-ltdl-install
make && make install
  报错: configure: error: The Courier Unicode Library 1.2 appears not to be installed. You may need to install a separate development subpackage, in addition to the main package

wget http://jaist.dl.sourceforge.net/project/courier/courier-unicode/1.2/courier-unicode-1.2.tar.bz2
./configure
make && make install
  创建sysV 启动脚本

cp courier-authlib.sysvinit /etc/rc.d/init.d/courier-authlib
chmod 755 /etc/init.d/courier-authlib
chkconfig --add courier-authlib
  基本配置

chmod 755 /usr/local/courier-authlib/var/spool/authdaemon
cp /etc/authdaemonrc.dist /etc/authdaemonrc
cp /etc/authmysqlrc.dist /etc/authmysqlrc
vi /etc/authdaemonrc
authmodulelist="authmysql"
authmodulelistorig="authmysql"
daemons=10
  通过mysql进行邮箱账号认证
  编辑/etc/authmysqlrc

MYSQL_SERVER localhost
MYSQL_PORT 3306
MYSQL_USERNAME extmail
MYSQL_PASSWORD extmail
MYSQL_SOCKET /var/lib/mysql/mysql.scok
MYSQL_DATABASE extmail
MYSQL_USER_TABLE extmail
MYSQL_CRYPT_PWFILED password
MYSQL_UID_FIELD'2525'
MYSQL_GID_FIELD '2525'
MYSQL_LOGIN_FIELD username
MYSQL_HOME_FIELD concat('/var/mailbox/',homedir)
MYSQL_NAME_FIELD name
MYSQL_MAILDIR_FIELD concat('/var/mailbox/',maildir)
  concat是mysql的函数将两个字符串连接起来
  创建库连接

echo "/usr/local/courier-authlib/lib/courier-authlib/" >>/etc/ld.so.conf.d/courier-authlib.conf
ldconfig -v
  启动服务

service courier-authlib start
  创建虚拟用户邮箱所在的目录, 并将其授权赋予postfix用户:

mkdir -pv /var/mailbox
chown -R postfix /var/mailbox
  重新配置SMTP认证, 编辑/usr/lib/sasl2/smtpd.conf

pwcheck_method:authdaemond
mech_list: LOGIN PLAIN
authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket
log_level:3
  编辑/etc/postfix/main.cf, 让postfix支持虚拟域

####################Virtual Mailbox Settings####################
virtual_mailbox_base = /var/mailbox
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
virtual_alias_domains =
virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
virtual_uid_maps = static:2525
virtual_gid_maps = static:2525
######################QUOTA Settings###############################
message_size_limit = 14336000
virtual_mailbox_limit = 20971520
virtual_mailbox_domains = example.com
  在extman安装文件中提供了sql脚本

tar -xf extman-1.1.tar.gz
cd extman-1.1/docs/
mysql -uroot -pnsadm <extmail.sql
mysql -uroot -pnsadm <init.sql
GRANT ALL PRIVILEGES ON extmail.* TO extmail@localhost IDENTIFIED BY 'extmail';
GRANT ALL PRIVILEGES ON extmail.* TO extmail@'127.0.0.1' IDENTIFIED BY 'extmail';
FLUSH PRIVILEGES;
  复制extman的配置

cp mysql_virtual_* /etc/postfix
  note: 启动虚拟域以后, 需要取消中心域, 即注释掉myhostname, mydestination, mydomain, myorigin
  note: 对于MySQL-5.1 以后的版本, 其中的服务脚本extmail.sql执行可能会有语法错误.
  sed -i 's@TYPE=MyISAM@ENGINE=InnoDB@g' extmail.sql

配置dovecot
  修改/etc/dovecot.conf

mail_location = maildir:/var/mailbox/%d/%n/Maildir
auth default{
mechanisms = plain
passdb sql {
args = /etc/dovecot-mysql.conf
}
userdb sql {
args = /etc/dovecot-mysql.conf
}
}
  配置/etc/dovecot-mysql.conf

driver = mysql
connect = host=localhost dbname=extmail user=extmail password=extmail
default_pass_scheme = CRYPT
password_query = SELECT username AS user,password AS password FROM mailbox WHERE username = '%u'
user_query = SELECT maildir, uidnumber AS uid, gidnumber AS gid FROM mailbox WHERE username = '%u'
安装extmail

tar -xf extmail-1.1.0.tar.gz
mkdir -pv /var/www/extsuite
mv extmail-1.1.0 /var/www/extsuite/extmailcp /var/www/extsuite/extmail/webmail.cf.default /var/www/extsuite/extmail/webmail.cf
  修改extmail的配置文件/var/www/exsuite/extmail/webmail.cf

SYS_USER_LANG = zh_CN
SYS_MAILDIR_BASE = /var/mailbox
SYS_MYSQL_USER = extmail
SYS_MYSQL_PASS = extmail
SYS_MYSQL_HOST = localhost
SYS_MYSQL_TABLE = mailbox
SYS_MYSQL_ATTR_USERNAME = username
SYS_MYSQL_ATTR_DOMAIN = domain
SYS_MYSQL_ATTR_PASSWD = password
SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket
  配置apache /etc/httpd/conf/httpd.conf

User postfix
Group postfix
<VirtualHost *:80>
ServerName node1.example.com
DocumentRoot /var/www/extsuite/extmail/html/
ScriptAlias /extmail/cgi /var/www/extsuite/extmail/cgi
Alias /extmail /var/www/extsuite/extmail/html
# SuexecUserGroup postfix postfix
</VirtualHost>
  注释httpd.conf 中的documentroot取消中心主机
  将cgi执行文件属主为apache运行身份用户:
  由于extmail要进行本地邮件的投递操作, 故必须运行apache服务器用户的身份修改为邮件投递代理的用户.
  chown -R postifx.postfix /var/www/extsuite/extmail/cgi/
  extmail将会用到perl的Unix: syslogd功能, 可以在http://search.cpan.org 搜索源码
  安装perl-devel rpm包perl-CGI-3.51-141.el6.x86_64包

tar -xf Unix-Syslog-1.1.tar.gz
cd Unix-Syslog-1.1
perl Makefile.PL
make && make install
  配置extman

mv extman-1.1 /var/www/extsuite/extman
cd /var/www/extsuite/extman
cp webman.cf.default webman.cf
  修改webman.cf

SYS_MAILDIR_BASE = /var/mailbox
SYS_DEFAULT_UID = 2525
SYS_DEFAULT_GID = 2525
  修改cgi目录的属主

chown -R postfix.postfix /var/www/extsuite/extman/cgi
  安装perl-GD 完成验证码的部分
  创建临时session目录

mkdir -pv /tmp/extman
chown -R postfix.postfix /tmp/extman
邮件加密
  S/MIME: 多用途互联网邮件扩展, 可以传输非文本信息
  Security User<-->User. 每个用户都有一个独立的用户证书, 并且通过同一CA签发.
  使用单向加密, hash(finger print), 使用非对称加密这段指纹, 然后生成一次性对称秘钥加密文件内容.并使用对方的公钥加密对称秘钥
  OpenSSL, GPG(PGP) GPG是PGP的一种实现, PGP是一种规范, GPG是一种软件
  GPG: GNU Privacy Guard
  PGP: Privacy Guard Protocol
  真正有意义的实现则是S/MIME这种端到端的投递

cd /etc/pki/CA/
(umask 077;openssl genrsa -out private/cakey.pem 2048)
openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3566
touch index.txt
echo 01 >serial
mkdir /etc/dovecot/ssl -pv
openssl req -new -key dovecot.key -out dovecot.csr
openssl ca -in dovecot.csr -out dovecot.crt -days 3656
  配置/etc/dovecot/conf.d/10-ssl.conf

ssl = yes
ssl_cert = </etc/dovecot/ssl/dovecot.crt
ssl_key = </etc/dovecot/ssl/dovecot.key
  使用openssl s_client验证

openssl s_client -connect SERVER:PORT -CAfile /path/to/CA_FILE
  使用mutt验证

mutt -f pops://dorawei@example.edu@mail.example.com
反垃圾/病毒邮件
  内容过滤器
  Apache: Spamassassin; Perl开发的垃圾邮件分拣器, 根据其垃圾邮件特征码库
  基于RBL: 实时垃圾邮件列表
  病毒邮件网关: 判断是否有病毒
  ​ clamav: 开源杀毒软件
  呼叫器: caller
  ​ MIMEDefang, Maiscanner, Amavisd-new

Cobbler

Primatives


  • Distros
  • Profiles and Sub-Profiles
  • Systems
  • Repos
  • Images
  • Management Classes
  • File Resources
  • Package Resources

Commands


  • Import
  • Sync
  • Reposync
  • Build ISO
  • Command Line Search
  • Replication
  • Validate Kickstart
  • ACL Setup

安装

yum install -y cobbler httpd cobbler-web debmirror pykickstart syslinux
  开启httpd和cobbler服务, 关闭selinux
  修改setting, 修改next_server和server
  https://dgoodwin.fedorapeople.org/cobbler/

cd /var/lib/cobbler/loaders
wget https://dgoodwin.fedorapeople.org/loaders/menu.c32
  cobbler get-loaders
  chkconfig rsync on
  comment "#@arches="i386";" 和 "#@dists="sid";" in /etc/debmirror.conf
  产生authkey, 并在setting中修改default_password_crypted, 缺省密码是cobbler

openssl passwd -1 -salt `openssl rand -hex 4`
  需要手动安装DHCP服务

yum install dhcp
cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
配置
option domain-name "example.com";
option domain-name-servers 192.168.48.131;
subnet 192.168.48.0 netmask 255.255.255.0 {
range 192.168.48.170 192.168.48.180;
option routers 192.168.168.131;
}
next-server 192.168.48.131;
filename="pxelinux.0";
  system的主要目的是配置网络接口的
  查看primitve
  cobbler list
  cobbler在部署rhel5的时候会报错, 因为ksvalidator对RHEL5的kickstart文件在做监测的时候会产生错误.
  snippet: 为了实现在kickstart中使用变量, 官方目前没有文档
  koan: 在物理节点上安装koan, 用来引导虚拟机. 虚拟机实例, 启动.

Operation
  导入distro
  cobbler import --name=centos-6.7-x86_64 --path=/media
  添加profile
  cobbler profile add --name=centos-6.7-x86_64 --distro=centos-6.7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/ks.cfg
  修改cobbler密码
  htdigest -c /etc/cobbler/users.digest Cobbler cblradmin

cobbler_web
  依赖于Django (Python web framework)
  使用系统用户认证

修改modules.conf
module = authn_pam
修改user.conf
[admins]
admin = "xxx"
  将kickstart文件放到/var/lib/cobbler/kickstarts/

Ansible
  Ansible is a radically simple configuration-management, application deployment, task-execution, and multinode orchestration engine.
  OS Provisioning
  ​ PXE, cobbler
  OS config
  ​ cfengine, puppet, saltstack, chef
  Deployment
  ​ func(SSL)
  ​ Fabric(ssh)
  ​ ansible
  YAML: Yet another Markable Language

安装
  https://releases.ansible.com/ansible/
  编译安装

yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto python-devel
tar xf ansible-1.5.4.tar.gz
cd ansible-1.5.4
python setup.py build
python setup.py install
mkdir /etc/ansible
cp -r examples/* /etc/ansible
  rpm安装

配置
  因为ansible是基于ssh的集群配置,首先要对所有主机进行ssh-key交换

ssh-keygen -t rsa -P ''
ssh-copy-id -i .ssh/id_rsa.pub root@mysql
  执行ansible

syntax:
ansible <host-pattern> [-f forks] [-m module_name] [-a args]
eg.
ansible all -m ping
默认模块是command,命令模块
ansible all -a 'date'
  查看ansible所有支持模块

ansible-doc -l
  查看指定模块可以使用的参数

ansible-doc -s [module_name]
eg.
ansible-doc -s COPY
ansible webservers -m copy -a "src=~/shell_script/showsum.sh dest=/tmp/"
  定义crontab任务

ansible webservers -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/utpdate 192.168.48.130"'
  执行yum安装

ansible all -m yum -a "state=present name=corosync"
  使用shell解析

ansible all -m shell -a "date;echo hello"
  执行service脚本

ansible all -m service -a 'state=started name=httpd enabled=yes'
YAML
  YAML是一个可读性高的用来表达资料序列的格式。YAML参考了其他多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822等。Clark Evans在2001年在首次发表了这种语言,另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者。
  特点:


  • YAML的可读性好


  • YAML和脚本语言的交互性好


  • YAML使用实现语言的数据类型


  • YAML有一个一致的信息模型


  • YAML易于实现


  • YAML可以基于流来处理


  • YAML表达能力强,扩展性好
  语法
  YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表、标量等数据结构。其结构(Structure)通过空格来展示,序列(Sequence)里的项用"-"来代表,Map里的键值对用":"分隔。下面是一个示例
  更多语法参考规范http://www.yaml.org

name: John Smith
age: 41
gender: Male
spouse:
name: Jane Smith
age: 37
gender: Female
children:
-   name: Jimmy Smith
age: 17
gender: Male
-   name: Jenny Smith
age 13
gender: Female
  YAML文件扩展名通常为.yaml
  playbook 示例

- hosts: webnodes
vars:
http_port: 80
max_clients: 256
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: name=httpd state=latest
- name: ensure apache is running
service: name=httpd state=started
handlers:
- name: restart apache
service: name=httpd state=restarted
  Tasks,任务列表和action
  定义task的时候可以使用“action: module options" 或者 ”modules: options"的格式,推荐使用后者以实现向后兼容。

name: make sure apache is running
service: name=httpd state=running
  在众多模块中,只有command和shell模块仅需要给定一个列表而无需使用“key=value”格式,例如:

tasks:
- name: disable selinux
command: /sbin/setenforce 0
  如果命令或脚本的退出码不为零,可以使用如下方式替代:

tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand || /bin/true
  或者使用ignore_errors来忽略错误信息:

tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True
  Handler:
  用于当关注的资源发生变化时采取一定的操作。
  “notify”这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作。

- name: template configuration file
template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
  handler是task列表,这些task与前述的task并没有本质上的不同。

handlers:
- name: restart memcached
service:  name=memcached state=restarted
- name: restart apache
service: name=apache state=restarted
  配置示例

- hosts: all                                                                                    
remote_user: root                                                                             
tasks:                                                                                       
- name: ensure apache latest version                                                        
yum: state=latest name=httpd                                                              
- name: apache configure file                                                               
copy: src=/etc/ansible/httpd.conf dest=/etc/httpd/conf/httpd.conf force=yes               
notify:                                                                                   
- restart httpd                                                                        
handlers:                                                                                    
- name: restart httpd                                                                       
service: name=httpd state=restarted
  Heartbeat的任务清单

- hosts: hbhosts                                                                                
remote_user: root                                                                             
tasks:                                                                                       
- name: ensure heartbeat latest version                                                     
yum: name=heartbeat state=present                                                         
- name: authkeys configure file                                                            
copy: src=/root/hb_conf/authkeys dest=/etc/ha.d/authkeys                                 
- name: authkeys modes 600                                                                  
file: path=/etc/ha.d/authkeys mode=600                                                   
notify:                                                                                   
- restart heartbeat                                                                     
- name: ha.cf configure file                                                               
copy: src=/root/hb_conf/ha.cf dest=/etc/ha.d/ha.cf                                       
notify:                                                                                   
- restart heartbeat                                                                     
handlers:                                                                                    
- name: restart heartbeat                                                                  
service: name=heartbeat state=restarted
NMS

SNMP
  简单网络管理协议
  NMS: Network Management Server. 网络监控节点
  SNMP就是一种nms/agent框架
  三个版本V1,V2c,V3
  通行的版本是V2c, community , 安全性非常差, 数据通信是通过明文的方式.
  V3版本中增强了认证机制, private communication.
  linux组件: net-snmp
  snmp 负责收集数据, cacti可以根据snmp收集的数据进行保存和数据展示, 数据分析和报警.
  Agent: 在本地执行指令后返回给NMS
  Manager: NMS
  community: 三类, 只读, 读写, 以及Trap
  SMI: Structure of Management Information
  MIB: Management Information Base 管理信息库
  snmp管理监控对象, 使用了倒置的树状结构来定义.
  Root-Node->So(1)->org(3)->dod(6)->internet(1)->mgmt(2)->mib-II(1)->ip(4)
  ​ 1.3.6.1.2.1.4
  OID: Object ID internet.ibm.host.name
  常用mib库是MIB-II, 这是一个基本的MIB库
  Fault Management
  Configuration Management
  Accouting Management
  Performance Management
  Security Management
  Public: 只读
  Private: 读写
  只要主机使用SNMP, 默认会有Public和Private这两个community.
  SNMP Operations
  get: 获取指定OID状态信息
  getnext: 获取所有子节点, 直接到指标的节点
  getbulk: 获取所有子树信息
  set: 设置信息
  trap: 通知报警

Cati
  Cacti(php开发的程序)使用rrd数据库, round robin database. 轮询数据库
  nagios 报警能力非常强, 默认也不保存数据. 能够解决依赖关系. 数据采集的数据比较独特, nagios只关心数据是否超过警戒位, 只关心正常与否的状态. 在状态转换时, 能够实现报警.
  zabbix 1.8 版本比较经典. 2.0版本增加了更多的功能
  数据保存在RDMS: oracle mysql pgsql 都行
  Zabbix Server: 负责接收agent发送的报告信息的核心组件, 所有配置, 统计数据及操作数据均由其组织进行
  Database Storage: 专用于存储所有配置信息, 以及由zabbix收集的数据
  Web interface: zabbix的GUI接口, 通常与Server运行在同一台主机上
  Proxy: 可选组件, 代理server收集部分监控端的监控数据并统一发往Server端
  Agent: 部署在被监控主机上, 负责手机本地数据并发往server端或proxy端
  host: 主机
  host group: 主机组
  item: 监控项
  trigger: 触发器, 定义阈值
  event: 事件. 比如触发器触发阈值了
  action: 动作, 指对于特定事件事先定义的处理方法.
  escalation: 报警升级.
  media: 媒介, 发送通知的手段. Email, Jabber或SMS
  notification: 通过选定的媒介向用户发送的有关某事件的信息.
  remote command: 远程命令,
  template: 用于快速定义被监控主机的预设条目集合, 通常包含了item, trigger, graph, screen
  application: 应用程序, 一组item的集合
  web scennario: web场景, 用于检测web站点可用性的一个或多个HTTP请求
  frontend: 前端, zabbix的web接口

Zabbix
  Fabric/Ansible
  Kickstart/cobbler
  Puppet

Puppet

Arch
  自动化运维三个层次: bootstrap --> configuration(目标状态) --> command and control
  bootstrap: pxe, cobbler, 虚拟化环境下的部署方式(xen, kvm依赖于hypervisor)
  configuration: puppet, saltstack(Python), chef, cfengine
  command and control: ansible(基于SSH)非常轻量级, fabric, func
  Master/Agent 模型 Agent通常每隔三十分钟去读取一次Master的配置信息
  Puppet使用Ruby语言开发的
  Puppet: 集中式的配置管理工具, 它通过自有配置语言对节点进行目标状态定义, 并能够基于网络实现目标状态的维护.
  Master: 中心配置库
  Agent: 读取并应用配置的节点
  定义目标状态的核心组件: 资源
  Luke Kanies, Puppet Labs
  版本: 0.24, 0.25, 2.6,
  ​ 2.7, 3.x (基于2.7为原型)
  OpenStack: 云栈
  使用puppet的公司: twitter, Citrix, Oracle, Google, Redhat, Sina
  管理目标: gentoo, redhat, macos, suse, solaris, windows 几乎所有的操作系统都可以被puppet所管理.
  manifest: 清单; 用于定义并保存资源, 是一种资源组织工具
  清单之间还可以互相调用:
  ​ import: 导入, (source)
  ​ include: 调用类
  Puppet的管理视角:
  ​ 模块: mod1, mod2
  ​ 节点: node node1 {
  ​ }
  ​ 类: 层次型组织组件一种方式. 类通常需要被实例化以后才能够被使用.
  ​ 类有三个特性: 多态, 继承, 封装
  模板配置语言:
  ruby:
  ​ 为nginx配置文件模板 work_processes <%= @processcount %>;
  ​ facter: 一种应用程序, 能够基于插件的形式, 能够获得各被管理节点的资源使用信息. 如IP地址, 网卡个数, 主机名称, CPU物理核心数等.
  配置语言:
  ​ 变量(自定义变量, puppet内置变量, factor变量), 条件语句, 正则表达式
  Master与Agent之间通信: XMLRPC over HTTPS
  过程调用:过程就是没有返回值的函数.
  xml: 扩展标记语言, 跨主机使用
  版本管理系统: cvs --> svn --> git

Ruby
  Ruby 是一种面向对象、命令式、函数式、动态的通用编程语言。在20世纪90年代中期由日本人松本行弘设计并开发,遵守BSD许可证和Ruby License。它的灵感与特性来自于Perl、Smalltalk、Eiffel、Ada以及Lisp语言
  Ruby的作者--松本行弘于1993年2月24日开始编写Ruby,直至1995年12月才正式公开发布于fj(新闻组)。之所以称为Ruby是取法自Perl,因为Perl的发音与6月的诞生石pearl(珍珠)相同,Ruby选择以7月的诞生石ruby(红宝石)命名。
  特色

完全面向对象:任何东西都是对象,没有基础类型
变量没有类型(动态类型)
任何东西都有值:不管是四则运算、逻辑表达式还是一个语句,都有回传值。
运算符重载
垃圾回收
强类型
变量无需声明
在Windows上,加载DLL
  Ruby on rials: ror
  Python: Django, Flask
  Java: jsp
  Perl 6: 支持面向对象
  Ruby的包管理工具Gem

Puppet工作
  1 . Define: 使用puppet语言来定义资源的状态
  2 . 模拟: 根据资源关系图, puppet可以模拟部署(无损运行测试代码)
  3 . 强制: 比对客户端主机状态和定义的资源状态是否一致, 自动强制执行
  4 . Report: 通过puppet API, 可以将日志发送到第三方监控工具, 如: dashboard/foreman
  Puppet 层次: Resource Abstraction Layer (资源依赖关系)--> Transaction Layer --> Configuraiton Language
  manifest--> 编译 --> 伪代码(catalog) --> Agent状态查询(APPLY) --> Agent执行目标状态(APPLY) --> 返回报告

Puppet安装
  直接使用RPM安装即可
  https://yum.puppetlabs.com/el/
  https://yum.puppetlabs.com/el/6.5/products/x86_64/

Puppet资源
  资源申报的语法
  ​ type {'title':
  ​ attribute => value,
  ​ }
  核心资源: notify, package, user, group, file, exec, cron, service
  查看资源说明puppet describe
  定义第一个资源

notify {'notice':
message => 'welcome to puppet.',
}
  apply资源

puppet apply test.pp -v -d
-v verbose
-d debug
  定义资源类型必须使用小写字符, 资源名称仅为一个字符串, 但要求在同一个类型中其必须唯一
  资源引用: 资源引用要通过 "Type ['title']"的方式进行, 如 User ['testuser'] 在资源引用时, 其类型名的首字母要大写
  元参数: 用于定义资源间的依赖关系, 及应用次序; 通知机制. 特殊属性: require, before
  before: Causes a resource to be applied before the target resource
  require: Causes a resource to be applied after the target resource
  notify: Causes a resource to be applied before the target resource. The target resource will refresh if the notifying resource changes
  subscribe: Causes a resource to be applied after the target resource. The subscribing resource will refresh if the target resource changes.

require:
package {'nginx':
ensure => present,
}
service {'nginx':
ensure=> true,
enable=> true,
require => Package['nginx'],
}
before
package {'nginx':
ensure => present,
before => Service['nginx']
}
service {'nginx':
ensure=> true,
enable=> true,
}
  资源间的应用次序链
  "->"用于定义次序链
  "~>"用于定义通知链
  Package['nginx'] -> File['nginx.conf'] ~> Service ['nginx']

package {'openss-server':
ensure=> present,
} ->
file {'/etc/ssh/sshd_config':
ensure=> file,
mode=> 600,
source => 'puppet://modules/sshd/sshd_config',
} ~>
service {'sshd':
ensure=> running,
enable=> true,
}
group
  管理系统上用户组
  常用属性:
  ​ ensure: 目标状态, present, absent
  ​ name: 组名, 如果没设置可以从title中继承
  ​ gid: GID
  ​ system: 系统组

user
  管理用户
  常用属性:
  ​ ensure: 目标状态
  ​ name:
  ​ uid:
  ​ system:
  ​ home:
  ​ shell:
  ​ gid:
  ​ password: 加密后的密码串
  ​ managehome: true|false

cron
  管理crontab
  常用属性
  ​ ensure: 目标状态
  ​ command: 命令或脚本
  ​ environmnet: 运行时的环境变量
  ​ hour:
  ​ minute:
  ​ month:
  ​ monthday:
  ​ weekday:
  ​ name:
  ​ user:默认为root

cron {'ntpdate':
ensure  => present,
command => '/usr/sbin/ntpdate 192.168.48.130 &> /dev/null',
minute => '*/3',
}
package
  puppet安装方式:
  ​ yum, rpm, apt, ports, gem, msi, dpkg, pkg
  Package的常用参数
  ​ ensure: 程序包的目标状态
  ​ name: 资源的名称, 即软件包的名字
  ​ provider: 软件包管理器
  ​ source: 指定程序包文件路径
  ​ install_options: 安装选项, 最常用的是通过INSTALLDIR来指定安装目录
  windows上定义安装mysql

package {'mysql':
ensure => installed,
provider =>'mis',
source => 'D:\software\mysql-5.5.36.msi',
install_options=>{'INSTALLDIR' => 'C:\mysql'},
}
service
  常用参数:
  ​ ensure: 服务的目标状态, true和false或running和stopped; (启动或关闭状态)
  ​ enable: 是否开机自动启动, true和false;
  ​ name: 服务名称, 一般为服务脚本名称
  ​ path: 服务脚本路径, 缺省为/etc/init.d/
  ​ start: 启动命令
  ​ stop: 关闭命令
  ​ status: 状态信息获取命令
  ​ restart: 重启命令

service {'nginx':
ensure => true,
name   => 'nginx',
enable => true,
}
file
  管理文件, 目录, 符号链接:
  生成文件内容
  管理文件权限, 属性
  通过source属性到指定位置下载文件
  通过recurse属性来获取目录, 包括目录中的所有文件
  常用属性
  ​ ensure: 目标状态, present, absent, file, directory
  ​ backup: 通过filebucket资源来备份文件, 备份根据md5sum来分类, 值通常为filebucket资源的名称
  ​ content: 生成文件内容, 生成方式有是那种(content, source, target) 三种属性彼此互斥
  ​ source: 通过指定的url下载文件至本地, 获取方式通常为puppet url, 格式: puppet:///modules/MODULE_NAME/filename;
  ​ target: 为符号链接指定目标
  ​ links: 文件为符号链接, 与target配合使用, 取值为follow|manage
  ​ path: 文件路径, 必须使用双引号.
  ​ mode: 指定权限, 八进制数字表示法, 也可以使用rwx
  owner: 属主
  ​ group: 属组
  ​ force: 强制执行删除文件, 链接或目录, 仅用于ensure为absent时
  ​ purge: 清空目录中存在的, 但未在资源中定义的文件; 修剪
  ​ recurse: 目录递归, 值true, false, inf, remote
  ​ replace: 替换, 本地存在的文件与资源中指定的文件内容不同时是否执行替换, 默认为否

exec
  执行命令, 通常在不得不用时才使用: 通常用于完成puppet自身无法完成的功能
  常用属性:
  ​ command: 要执行的命令, 通常为命令文件的完整路径;
  ​ path: 命令搜索路径
  ​ group: 以属组身份执行
  ​ user: 以属主身份执行
  ​ onlyif: 0,表示仅在命令的状态返回值为0时才执行此命令
  ​ refresh: 接受到其他资源的通知时, 重新执行此命令
  ​ refreshonly: 仅当被依赖的资源发生改变时才被触发
  ​ tries: 重试次数, 默认为1
  ​ try_sleep: 多次尝试之间的时间间隔
  创建文件和创建软连接

file {'/root/abc.txt':
ensure  => present,
content => 'Hello puppet.',
path    => '/root/abc2.txt',
}
file { 'fstab,symbolic':
ensure  => present,
target  => "/etc/fstab",
path    => "/tmp/fstab.symbolic",
links   => follow,
}
notify
  调试输出
  常用属性
  ​ message: 信息
  ​ name: 信息名称
  notify {'notice':message => 'test' }

Puppet变量
  facter变量, 使用facter查看
  puppet的变量名称需要以"$"开头, 赋值操作符为"="
  任何正常数据类型(非正则)的值都可以赋予puppet中的变量, 如字符串, 数值, 布尔值, 数组, hash以及特护的undef值(即变量未被赋值)
  puppet的每个变量都有两个名字: 简短名称和长格式完全限定名称(FQN), 完全限定名称的格式为"$scope::variable"

puppet正则表达式
  使用(?:)
  可用选项有: i忽略字符大小写; m把.当做换行符;x忽略模式中的空白符和注释; -:表示不支持

$packages = $operatingsystem ? {
/(?i-mx:ubuntu|debian)/ => 'apache2',
/(?i-mx:centos|fedora|redhat)/ => 'httpd',
}
变量作用域
  puppet模块: 自定义变量
  top scope:
  node scope:
  class scope:
  $::表示全局的
  $::Class_parrent::Class_child: 变量的绝对访问路径. 变量的完全限定名称

内置变量
  一般用于M/A模型下
  Agent端:
  ​ $environment
  ​ $clientcert
  ​ $clientversion
  Master端:
  ​ $servername
  ​ $serverip
  ​ $serverversion

Puppet操作符

比较操作符

==: 等值比较
!=: 不等比较
<: 小于比较
>:大于比较
<=: 小等
>=:大等
=~:正则表达式匹配
!~:正则表达式非匹配
in: 判断是否在某个集合中
布尔比较符

and: 与
or:  或
!: 非
算数运算符

+: 加
-: 减
*: 乘
/: 除
<<: left shift, 左移(补零)
>>: right shift, 右移(补零)
puppet控制语句

if statement

if CONDITION {
statement
}
elsif CONDITION{
statement
}
else {
statement
}
  eg.

if $operatingsystem =~ /(?i-mx:(centos|redhat|fedora))/{
notice("Welcome to $1 linux.")
}
case

case CONTROL_EXPRESSION {
case1,...: {statement...}
case2,...: {statement...}
......
default: {satement...}
}
  eg.

case $operatingsystem {
'Solaris': {notice("Welcome to Solaris")}
'Redhat','CentOS': {notice("Welcome to Redhat OSFamily")}
/^(Debian|Ubuntu)$/:{notice("Welcome to $1 Linux")}
defautl:{notice("Welcome, testing")}
}
case $operatingsystem {
/^(?i-mx:redhat|centos|fedora)/: { package {'httpd': ensure => present, provider => yum, } }
/^(?i-mx:ubuntu|debian)/: { package {'apache2': ensure => present, provider => apt, } }
default: { notify {'notice': message => "unknown system.", }}
}
Selectors
  Selector返回一个值, 而不是执行一段代码

CONTROL_VARIABLE ? {
case1 => value1
case2 => value2
...
default => valueN
}
  eg.

$webserver = $operationsystem ?{
/(?i-mx:ubuntu|debain)/=> 'apache2',
/(?i-mx:centos|fedora|redhat)/=> 'httpd',
}
$webserver = $operatingsystem ?{
/^(?i-mx:centos|fedora|redhat)/     => 'httpd',
/^(?i-mx:ubuntu|debian)/            => 'apache2',
}
notify{ 'webserver':
message => "The package is $webserver"
}
package { "$webserver":
ensure  => present,
}
模块
  清单: manifest *.pp
  导入机制: import

node1.pp
import nginx.pp
import tomcat.pp
import mysql.pp
  nginx.pp
  依赖于外部资源: 文件, 模板文件(生成适用于目标节点的文件)
  为了实现某种完备功能而组织成的一个独立的, 自我包含的目录结构: 模块
  模块: 目录结构, 目录名称即为模块名.

/
files/: 文件存储目录
manifests/: 清单存储目录
templates/: 模板存储目录
lib/: ruby插件存储目录, 用于实现一些自定义的功能
  mkdir -pv nginx/{manifests,files,templates,lib}
  示例:

/tmp/modules/文件存储目录
nignx.conf
manifests/
nginx/
/
files/:文件存储目录
nignx.conf 访问路径: puppet:///modules/module_name/file_name
manifests/:清单存储目录
init.pp
必须包含且只能包含一个与模块同名的类
*.pp访问路径: module_name::manifest_file_name
nginx.pp
templates/: 模板存储目录
*.erb使用模板函数template()装载并运行其中模板语言, 运行后会生成静态文件:
访问路径及方式: template('module_name/template_name')
lib/插件
tests/当前模块的使用说明和样例:
spec/为lib目录中的插件提供使用说明和样例
在模块根目录下, 通常还应该有几个文档:
LICENSE
Modulefile
README
puppet类
  命名的代码块, 为了通用目标或目的组织在一起的一个或多个资源, 类可以继承.
  只有被调用才会执行, 调用称作: 声明一个类

class my_class{
...puppet code
}
  include class_name
  eg

$webserver = $operatingsystem ? {
/^(?i-mx:redhat|centos|fedora)/ => 'httpd',
/^(?i-mx:ubuntu|debian)/        => 'apache2',
}
class httpd ($pkgname ='apache2') {
package { "$pkgname":
ensure      => present,
}
service {"$pkgname":
ensure      => true,
require     => Package["$pkgname"],
}
}
class {'httpd':
pkgname => $webserver,
}
  类继承:
  ​ class C_NAME inherits PARENT_CLASS_NAME {
  ​ }
  子类的命名方式: nginx::rproxy
  基类: 安装nginx
  子类1: 提供web配置的配置文件
  子类2: 提供反向代理专用的配置文件

##nginx.pp
class nginx{
package {"nginx":
ensure  => present,
}
service {'nginx':
ensure  => running,
enable  => true,
}
}
class nginx::rproxy inherits nginx {
file {'/etc/nginx/nginx.conf':
ensure      => file,
source      => '/tmp/nginx/nginx.reverse_proxy.conf',
require     => Package['nginx'],
notify      => Service['nginx'],
}
}

class nginx::web inherits nginx {
file {'/etc/nginx/nginx.conf':
ensure      => file,
source      => '/tmp/nginx/nginx.web.conf',
require     => Package['nginx'],
notify      => Service['nginx'],
}
}
###node.pp
import "/root/puppet/nginx.pp"
include nginx:web

  查看配置变量
  puppet agent -genconfig
  启用puppet模块
  puppet apply -v -d --modulepath=/etc/puppet/modules -e 'include nginx:web'
  模块文件访问路径: module_name::mainfest_file_name, module_name::subdir_name::manifest_file_name
  files/访问路径: puppet:///modules/module_name/file_name
  =>: 在子类覆盖父类中的资源
  +>: 在子类中为父类的资源新增额外的属性
  带参数类: 建议参数要有默认值
  声明类(调用类):
  ​ include
  ​ require
  ​ class{'class_name':
  ​ para1 => value1,
  ​ para2 => value2,
  ​ }
  定义节点: 也需要在清单文件, 文件后缀为.pp, 在master/agent模型中, 所有节点清单文件的入口文件为site.pp

node 'node_name' {
节点专用变量
类声明
}
  建议一类节点使用一个清单文件, 所有的清单文件都在site.pp中使用import包含进来

模板
  语法:
  替换为表达式值的语法

<% %> 自我封装的标签
<%= Ruby Expression %>: 替换为表达式的值
<%= @processorcount %>: 变量要用@声明
  执行代码

<% ruby code %>: 表示仅执行代码, 不做任何替换操作, 常用于条件判断或循环语句, 设定变量以及在输出之前度数据进行处理
  注释

<%# commnent %>: 注释
<%%: 显示<%
%%>: 显示%>
  调用模块变量: 变量完全限定名称, $::scope_name
  迭代和条件判断
  在/etc/puppet/modules/nginx/templates下创建nginx.erb, 其中

worker_processes  <%= @processorcount %>;
  特别的在init.pp中引用时, file中使用content声明template
  ​ content => template('module_name/template_file_name')

file {'nginx.conf':
ensure      =>  file,
content     =>  template('nginx/nginx.conf.erb'),
path        => '/etc/nginx/nginx.conf',
require     => Package['nginx'],
mode        => '0644',
}
Master/Agent模型
  大量节点时主机命名方式:
  ​ 角色名-运营商(ISP)-机房名-机器IP.域名
  ​ web-cnc-sh-192.168.48.130.example.com
  server端自动生成配置信息

puppet master --genconfig >>/etc/puppet/puppet.conf
  agent端先以测试的方式来查看

puppet agent --help
puppet agent --server node1.example.com -d -v --noop --test
info: Creating a new SSL key for node2.example.com
info: Caching certificate for ca
info: Creating a new SSL certificate request for node2.example.com
info: Certificate Request fingerprint (md5): 62:9A:34:02:3E:52:42:32:1E:9D:A1:8F:AA:AC:C1:21
  此时在server端可以查看证书请求的信息

puppet cert list
  再从client端对server发起测试请求

puppet agent --server node1.example.com -d -v --test
  server端可以签发证书

puppet cert sign NODE_NAME
puppet cert sign --all
  此时在server端并没有给client分配清单文件, 于是要在server端创建站点清单

创建/etc/puppet/manifests/node2.example.com.pp
node 'node2.example.com' {
include nginx:web
}
创建/etc/puppet/manifests/site.pp
import "*.pp"
  也可以使其在配置中生效, /etc/puppet/puppet.conf文件中添加如下配置

server = node1.example.com 在[agent]添加
  注意: master端的任何修改, 都要重新装载puppetmaster服务.
  节点的定义和使用方式类似类的使用; 支持继承
  定义通用的节点

node 'base' {
include ntp
include iptables
include zabbix_agent
}
node 'node1.example.com' inherits base {
include nginx
}
  节点名称支持使用正则表达式
  node /^node[1234]\.example.com$/{
  }
  关于节点的定义
  /etc/puppet/manifests/
  ​ site.pp
  ​ base.pp
  ​ import "nginx/*.pp"
  ​ import "tomcat/*.pp"
  ​ import "varnish/*.pp"
  ​ nginx/
  ​ node1.example.com.pp
  ​ nginx.pp
  ​ tomcat/
  ​ varnish/
  使用外部的节点分类器. ENC, external node classifier
  ​ 使用yaml语法: PyYAML
  自动签发证书 可以设置master自动签发所有的证书,我们只需要在/etc/puppet 目录下创建 autosign.conf 文件。(不需要修改 /etc/puppet/puppet.conf文件,因为我默认的autosign.conf 文件的位置没有修改)
  cat > /etc/puppet/autosign.conf <<EOF *.example.com EOF 这样就会对所有来自 example.com 的机器的请求,都自动签名。
  检查语法错误

puppet parser validate /path/to/some/manifest
Puppet的配置文件
  puppet.conf: 主配置文件
  ​ [main]
  ​ [agent] puppet agent --genconfig
  ​ [master] puppet master --genconfig
  manifests/site.pp: 站点清单(master的清单入口)
  puppet apply --genmanifest 生成样例式的站点清单
  namespaceauth.conf: 名称空间认证, 实现名称空间访问控制机制
  ​ kick模式: 服务器主动推送给客户端. 此模式必须依赖namesapceauth.conf文件
  ​ [puppetmaster]
  ​ allow *.example.com
  auth.conf:实现puppet的ACL功能. 根据URL路径
  ​ RESTful风格的API: 根据 http://master:8140/{environment}/{resource}/{key}路径, 做访问控制

path  /
auth  any
enviornment production
allow *.example.com
  autosign.conf: 客户端自动认证的配置文件
  fileserver.conf: 文件系统配置文件

Puppet命令总结
  apply, agent, master, cert, describe, help
  agent: 客户端进程
  master: 服务端进程
  apply: 应用本地清单文件
  cert: 证书管理
  describe: 资源帮助信息
  module: 模块管理
  kick: master触发模式
  ​ 帮助类命令:
  ​ describe
  ​ doc: 生成puppet文档
  ​ help: 查看帮助
  ​ resource: 查看资源帮助
  ​ status: 查看puppet状态
  ​ master命令选项:
  ​ --no-daemonize
  ​ --daemonize, -D
  ​ --debug, -d
  ​ --verbose, -v
  ​ --genconfig
  ​ agent命令的常用选项:
  ​ --daemonize, -D
  ​ --no-daemonize
  ​ --debug, -d
  ​ --verbose, -v
  ​ --noop: no operation模式, 不真正应用catalog
  ​ --test: 测试
  ​ --waitforcert: 等待证书签署成功才退出
  ​ apply命令的常用选项:
  ​ --debug, -d
  ​ --verbose, -v
  ​ --execute, -e
  ​ --modulepath, 模块路径
  ​ cert命令的常用操作:
  ​ list:
  ​ sign: 签署
  ​ clean: 清除证书
  ​ pirnt: 打印证书信息
  ​ revoke: 吊销证书
  ​ verify: 验证证书
  ​ generate: 为客户端生成证书

Puppet的模块管理:
  http://forge.puppetlabs.com

puppet module search httpd
puppet module install eshamow-pe_httpd
  puppet运行环境定制:
  ​ 开发: development
  ​ 测试: testing
  ​ 线上: production

puppet.conf
[master]
environment = production, testing, development
[production]
manifest = /etc/puppet/manifests/production/site.pp
modulepath = /etc/puppet/modules/production
fileserverconfig = /etc/puppet/fileserver.conf.production
[testing]
manifest = /etc/puppet/manifests/testing/site.pp
modulepath = /etc/puppet/modules/testing
fileserverconfig = /etc/puppet/fileserver.conf.testing
[development]
manifest = /etc/puppet/manifests/development/site.pp
modulepath = /etc/puppet/modules/development
fileserverconfig = /etc/puppet/fileserver.conf.development
auth.conf定制其访问控制机制:
path /
auth any
environment production
allow localhost,*.example.com
  客户端配置: puppet.conf
  [agent] 中添加environment
  environment = prodcution
  手动测试不同环境, puppet agent --test -d -v --noop --environment testing
  git版本管理系统:

Puppet dashboard
  参考笔记

Puppet kick功能
  puppet客户端默认每30分钟跟服务器通讯一次,但是有时,我们希望服务端能给客户端紧急推送一些任务,于是就有了puppet kick(puppet 2.6以前叫puppetrun)。
  编辑客户端/etc/puppet/puppet.conf 在[agent]后面添加

listen = true  //这个是让puppet监听8139端口
  在服务器端编辑或新建文件/etc/puppet/namespaceauth.conf,包含下面内容
[puppetrunner]
allow *example.com
  编辑文件auth.conf
path /run
method save
allow puppet.magedu.com
  推送方法,在服务端运行命令
puppet kick -p 10 –host 客户端
puppetrun -p 10 –host 客户端

制作RPM包
  使用普通用户进行
  1 . Set up the directory Structure
  2 . Place the sources in the right directory
  3 . Create a spec file that tells the rpmbuild command what to do
  4 . Build the source and binary RPM.
  查看变量和宏

rpmbuild --showrc
  五个目录

BUILD:The rpmbuild command builds software in this directory
RPMS: The rpmbuild command stores binary RPMs it creates in this directory
SOURCES: You should put the sources for the application in this directory
SPECS: You should place the spec file for each RPM you plan to make in this directory
SRPMS: The rpmbuild command places source RPMs in this directory
BUILDROOT: 临时安装文件存放于此, 清理段会清楚这个目录
  红帽系统中,built目录为/usr/src/redhat

mkdir -pv rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS,BUILDROOT}
  设置rpmmarco, vi ~/.rpmmacros

%_topdir        /root/rpmbuild
创建spec文件
  introduction section: 介绍阶段
  查看rpm组

/usr/share/doc/rpm-4.8.0/GROUPS
配置文件

Summary:
Name:
Version:
Release:
License:
Group:
URL:
Packager:
Vendor:
Source:
BuildRoot:
BuildRequires:
prep section: 准备阶段

%prep
%setup
  {?_smp_mflags} 宏粗存在则使用, 不存在则不使用

build section: 编译阶段

%build
%configure \
--etcdir="%{_sysconfdir}"
--mandir="%{_mandir}" \
--i18n="0" \
--scrip="0"
%{_make}%{?_smp_mflags}
install sesion: 安装阶段

%install
%{__rm} -rf %{buildroot}
%{__make} install DESTDIR="%{buildroot}"
%find_lang %{name}
clean section: 清理段

%clean
%{__rm} -rf %{buildroot}
file section: 文件段

%files -f %{name}.lang
%defattr(-,root,root,0755)
%doc API CHANGES COPYING CREDITS README test.example
%doc %{_mandir}/man1/test1.1*
%doc %{_mandir}/*/man1/axe1.1*
%config %{_sysconfdir}/axelrc
/usr/local/bin/axel
changelog section: 改变日志段

* 时间版本
- 修改者
脚本段

%pre
%post
%preun
%postun
  %1有三个取值, 1代表安装, 0代表卸载, 2代表升级
  nginx的specs

# %define nginx_usernginx
# %define nginx_group%{nginx_user}
# %define nginx_confdir/etc/nginx
Name:nginx
Version: 1.10.0
Release:3%{?dist}
Summary:A free, open-source,high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server
Group:System Environment/Daemons
License:BSD
URL:http://www.nginx.org/
BuildRoot:%{_tmppath}/%{name}-%{version}-%{release}-root
Source0:http://sysoev.ru/nginx/nginx-%{version}.tar.gz
Source1:nginx.sysinit
Source2: nginx.fastcgi_params
BuildRequires:  pcre-devel,zlib-devel,openssl-devel
BuildRequires:libxslt-devel,gd-devel
Requires:pcre,openssl,gd
# for /usr/sbin/useradd
Requires(pre):shadow-utils
Requires(post): chkconfig
# for /sbin/service
Requires(preun): chkconfig,initscripts
Requires(postun): initscripts
Provides:webserver
%description
RPM build version build by Hugh for nginx 1.10.0
%prep
%setup -q

%build
export DESTDIR=%{buildroot}
./configure \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--with-pcre
make %{?_smp_mflags}
%install
rm -rf %{buildroot}
%make_install DESTDIR=%{buildroot}
%{__install} -p -d -m 0755 %{buildroot}/var/run/nginx
%{__install} -p -d -m 0755 %{buildroot}/var/log/nginx
%{__install} -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/rc.d/init.d/nginx
mv %{buildroot}/etc/nginx/fastcgi_params %{buildroot}/etc/nginx/fastcgi_params.orig
%{__install} -p -D -m 0644 %{SOURCE2} %{buildroot}/etc/nginx/fastcgi_params
%clean
rm -rf %{buildroot}
%pre
if [ %1 == 1 ];then
/usr/sbin/useradd -s /bin/false -r nginx 2>/dev/null ||:
fi
%post
if [ %1 == 1 ];then
/sbin/chkconfig --add %{name}
fi
%preun
if [ %1 == 0 ];then
/sbin/service %{name} stop >/dev/null 2>&1
/sbin/chkconfig --del %{name}
fi
%files
%defattr(-,root,root,-)
%doc LICENSE CHANGES README
%{_sbindir}/%{name}
%dir /var/run/nginx
%dir /var/log/nginx
%dir /etc/nginx
%config(noreplace) /etc/nginx/win-utf
%config(noreplace) /etc/nginx/mime.types.default
%config(noreplace) /etc/nginx/fastcgi.conf
%config(noreplace) /etc/nginx/fastcgi.conf.default
%config(noreplace) /etc/nginx/fastcgi_params
%config(noreplace) /etc/nginx/fastcgi_params.orig
%config(noreplace) /etc/nginx/fastcgi_params.default
%config(noreplace) /etc/nginx/%{name}.conf
%config(noreplace) /etc/nginx/mime.types
%config(noreplace) /etc/nginx/scgi_params
%config(noreplace) /etc/nginx/scgi_params.default
%config(noreplace) /etc/nginx/uwsgi_params
%config(noreplace) /etc/nginx/uwsgi_params.default
%config(noreplace) /etc/nginx/koi-win
%config(noreplace) /etc/nginx/koi-utf
%config(noreplace) /etc/nginx/%{name}.conf.default
/usr/local/nginx/html/50x.html
/usr/local/nginx/html/index.html
%attr(0755,root,root) /etc/rc.d/init.d/nginx
%changelog
* Thu Nov 9 2016 avaya.com <sun59@avaya.com> - 1.10.0-3
- Add fastcgi_params for php-fpm
* Thu Nov 9 2016 avaya.com <sun59@avaya.com> - 1.10.0-2
- Add sysV script /etc/rc.d/init.d/nginx
* Thu Nov 8 2016 avaya.com <sun59@avaya.com> - 1.10.0-1
- Initial Version
RPM SRC
  rpmfind.net
  rpmpone.com
  SRC的源码文件, 实际上是把所有的sources文件和spec文件集合到一个源码包中.

rpm2cpio nginx-1.10.0-3.el6.src.rpm|cpio -t
nginx-1.10.0.tar.gz
nginx.fastcgi_params
nginx.spec
nginx.sysinit
  如果安装src.rpm文件, 则会将源码文件解压到指定的工作目录中去

tree .
.
├── BUILD
├── BUILDROOT
├── RPMS
│   └── x86_64
│       ├── nginx-1.10.0-3.el6.x86_64.rpm
│       └── nginx-debuginfo-1.10.0-3.el6.x86_64.rpm
├── SOURCES
├── SPECS
└── SRPMS
  也可以使用rpmbuild的rebuild选项直接产生rpm包

rpmbuild --rebuild nginx-1.10.0-3.el6.src.rpm
  直接展开src.rpm包在当前目录

rpm2cpio nginx-1.10.0-3.el6.src.rpm|cpio -id
SPECaddiontal
  定义标签

TagName:value
  定义宏

%define macro_name value
  引用宏

%{marco_name}
  note: 注释当中不可以使用%来表示, 如果非要表示, 可以使用%%

Naming Package
  在Naming package中, 软件包相关信息
  Name, Version, Replease, Group是必须的.
  Name: 中间不可以使用"-" , 命名的时候会按照name-version-release.{arch}.rpm来定义
  Version: 也不可以使用dash
  Release:
  Group : /usr/share/doc/rpm-version/GROUPS
  公司相关信息
  Vendor: 公司
  URL: RPM 源码
  Packager 制作者
  License: 源码包是有版权的, 所以License一定要加版权
  Summary: 一般低于50个字符
  Description: 每行最好也不要超过50个字符

定义软件的依赖关系
  Requires: 安装的依赖,

Requires: bash >=2.0
Requires: perl(Carp) >=3.2
  Provides: 能力,如果不定义能力, 则能力是软件包名
  BuildRequires: 编译时所以来的能力. 多个能力使用逗号隔开

设定build目录
  buildroot: RPM包编译安装之后, 假象的根目录, 即安装目录

Buildroot: %{_tmppath}/%{name}-%{version}-root
$RPM_BUILD_ROOT 和 %{buildroot} 表示相同意义
定义源码文件

Source0:        http://sysoev.ru/nginx/nginx-%{version}.tar.gz
Source1:        nginx.sysinit
Source2:        nginx.fastcgi_params
Patch
  Patch和source很类似, 但是patch可以直接使用patch命令升级

Patch1:httpd-2.2.22-pcre830.patch
安装制作RPM
  Prep
  %prep
  %setup
  此阶段将源码包解压缩到build目录
  %patch可以用于打补丁

%patch1
%patch2
  %setup -q -n ngios 指定解压后的文件名称
  -a, 在cd进build目录后展开 source, -a 0 展开Source0
  -b, 在cd进build目录前展开source, -b 1 展开 Source1
  Build
  %build其实就是configure && make
  make %{? _smp_mflags} 如果是多处理模式则使用多处理器的标签
  Install
  install -p 保留原有的时间戳
  Clean
  主要作用是清理buildroot的目录
  rm -rf %{buildroot}
  Installation Script
  %pre: 安装前脚本
  %post: 安装后脚本
  %preun: 删除前脚本
  %postun: 删除后脚本

if [ $1 == 1 ];then
%/usr/sbin/useradd -c "Nginx user" -s /sbin/nologin -r nginx 2>/dev/null ||:
fi
%1 == 1: Install the first time
%1 == 2: Upgrade
%1 == 0: Remove the patch
%package
  拆分子包
  %package libs
  %description libs

files
  %files
  任何安装中的文件生成文件必须在此处列出
  可以使用通配符, 也可以直接写目录
  /etc/nginx/nignx.*
  %dir /etc/nginx/
  %doc nginx ---> /usr/share/doc/nginx-1.10.0/
  %docdir 把整个目录中的所有文件都当做文档
  %config 如果此文件已经存在,则会替代并将旧配置文件重新命名. 可以使用(noreplace, missingok)
  %attr: 指明权限属性 %attr(mode,user,group)
  %deattr: 定义默认权限
  %changelog: 时间作者版本号, 并声明修改内容

PGP
  PGP: Pretty Good Privacy
  GnuPG: 简称GPG
  产生秘钥

gpg --gen-key
  查看秘钥

gpg --list-keys
  提取公钥

gpg --export -a "Test on GPG" >RPM-GPG-Key-example
  添加数字证书, 此时要在~/.rpmmacros下定义gpg_name, 此时在制作rpm包时会自动做gpg签署

%_signature gpg
%_gpg_name Ying Sun
rpm --addsign nginx-1.10.0-x86.rpm
  添加秘钥

rpm --import RPM-GPG-KEY-example
  查看签名

rpm --checksig nginx-1.10.0-x86.rpm
附加小工具

Shellinabox
  https://github.com/shellinabox/shellinabox

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-387918-1-1.html 上篇帖子: Awesome Go (http://awesome-go.com/) 下篇帖子: 面向对象的程序设计
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表