Puppet扩展篇7-puppet代码与版本控制系统的结合

一、介绍

二、环境介绍

三、部署流程

1.1 安装相关软件包

  1. [root@puppetserver ~]# svnserve version #通过查看版本验证安装是否成功
  2. svnserve, version 1.6.11 (r934486)
  3. compiled Apr 12 2012, 11:09:11
  4. Copyright (C) 2000-2009 CollabNet.
  5. Subversion is open source software, see http://subversion.tigris.org/
  6. This product includes software developed by CollabNet (http://www.Collab.Net/).
  7. The following repository back-end (FS) modules are available:
  8. * fs_base : Module for working with a Berkeley DB repository.
  9. * fs_fs : Module for working with a plain file (FSFS) repository.
  10. Cyrus SASL authentication is available.

1.2 创建第一个版本库

  1. [root@puppetserver ~]# mkdir /svndata
  2. [root@puppetserver ~]# svnadmin create /svndata/puppet
  3. [root@puppetserver ~]# ll /svndata/puppet/
  4. total 24
  5. drwxr-xr-x 2 root root 4096 Oct 22 13:29 conf
  6. drwxr-sr-x 6 root root 4096 Oct 22 13:29 db
  7. -r--r--r-- 1 root root 2 Oct 22 13:29 format
  8. drwxr-xr-x 2 root root 4096 Oct 22 13:29 hooks
  9. drwxr-xr-x 2 root root 4096 Oct 22 13:29 locks
  10. -rw-r--r-- 1 root root 229 Oct 22 13:29 README.txt

2.1 安装相关软件包

  1. [root@puppetserver ~]# yum install httpd httpd-devel mod_dav_svn

2.2 创建SVN虚拟主机

  1. [root@puppetserver svndata]# vim /etc/httpd/conf.d/subversion.conf
  2. LoadModule dav_svn_module modules/mod_dav_svn.so
  3. LoadModule authz_svn_module modules/mod_authz_svn.so
  4. Listen 8142
  5. <VirtualHost *:8142>
  6. <Location /svndata>
  7. DAV svn
  8. SVNListParentPath on
  9. SVNPath "/svndata/puppet"
  10. AuthType Basic
  11. AuthName "Subversion repository"
  12. AuthUserFile "/svndata/puppet/conf/authfile"
  13. Require valid-user
  14. SVNAutoversioning on
  15. ModMimeUsePathInfo on
  16. </Location>
  17. </VirtualHost>

2.3 创建svn权限配置文件

  1. [groups]
  2. admin = puppet
  3. [admin:/]
  4. @admin = rw
  5. [/]
  6. * = r
  7. [$name:/]
  8. test = rw">>/svndata/puppet/conf/authz
  9. 2.4 创建用户名及密码并设置相应权限
  10. [root@puppetserver ~]# /usr/bin/htpasswd -c /svndata/puppet/conf/authfile puppet #创建SVN服务器账户puppet密码为redhat
  11. New password: redhat
  12. Re-type new password: redhat
  13. Adding password for user puppet
  14. [root@puppetserver ~]# chown apache /svndata/puppet -R
  15. [root@puppetserver ~]# echo "puppet = redhat" >>/svndata/puppet/conf/passwd

2.5 配置SVN服务信息

2.6 通过浏览器测试访问

  1. [root@puppetserver svndata]# /etc/rc.d/init.d/httpd restart #重启httpd服务
  2. http://192.168.100.110:8142/svndata/

svn版本控制测试界面

2.7 通过其他linux节点访问测试

  1. [root@agent1 ~]# svn checkout http://192.168.100.110:8142/svndata/ /mnt/
  2. Authentication realm: <http://192.168.100.110:8142> Puppet Subversion repository
  3. Password for 'root':
  4. Authentication realm: <http://192.168.100.110:8142> Puppet Subversion repository
  5. Username: puppet
  6. Password for 'puppet':
  7. -----------------------------------------------------------------------
  8. ATTENTION! Your password for authentication realm:
  9. <http://192.168.100.110:8142> Puppet Subversion repository
  10. can only be stored to disk unencrypted! You are advised to configure
  11. your system so that Subversion can store passwords encrypted, if
  12. possible. See the documentation for details.
  13. You can avoid future appearances of this warning by setting the value
  14. of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
  15. '/root/.subversion/servers'.
  16. -----------------------------------------------------------------------
  17. Store password unencrypted (yes/no)? no
  18. Checked out revision 0.

2.8 通过Windows客户端TortoiseSVN访问测试

svn版本控制测试界面

备注:由于还为import版本,所以查看的内容为空

3.1 将puppet server模块目录导入到版本库中

  1. [root@puppetserver ~]# svn import /etc/puppet/environments/testing
  2. Authentication realm: <http://192.168.100.110:8142> Puppet Subversion repository
  3. Password for 'root':
  4. Username: puppet
  5. Password for 'puppet':
  6. Adding /etc/puppet/environments/testing/groups
  7. Adding /etc/puppet/environments/testing/groups/modules
  8. Adding /etc/puppet/environments/testing/groups/modules/grub
  9. Adding /etc/puppet/environments/testing/groups/modules/grub/files
  10. Adding /etc/puppet/environments/testing/groups/modules/grub/manifests
  11. Committed revision 1.

备注:由于SVN服务器端和puppetserver在同一台服务器上,也可以通过以下方式进行导入

  1. [root@puppetserver ~]# svn import /etc/puppet/environments/testing
  2. file:///svndata/puppet -m "Puppet Initial repository"

3.2 通过IE浏览器访问SVN服务器

3.3 通过Windows客户端TortoiseSVN checkout最新的版本库到本地

svn版本控制测试界面

svn版本控制测试界面

  1. [root@puppetserver ~]# cd /etc/puppet/environments/testing/
  2. [root@puppetserver testing]# rm -rf * #删除之前建议备份
  3. [root@puppetserver testing]# svn checkout
  4. http://192.168.100.110:8142/svndata/puppet /etc/puppet/environments/testing
  5. Authentication realm: <http://192.168.100.110:8142> Puppet Subversion repository
  6. Password for 'puppet':
  7. Please type 'yes' or 'no': no
  8. A groups
  9. A groups/modules
  10. A groups/modules/grub
  11. A groups/modules/grub/files
  12. A groups/modules/grub/manifests
  13. Checked out revision 1.
  14. [root@puppetserver testing]# ls -a
  15. . .. agents environment groups manifests .svn
  16. [root@puppetserver testing]# ls .svn/ #每个目录下面都会生成.svn隐藏目录,用于保存当前版本的信息
  17. all-wcprops entries prop-base props text-base tmp
  18. 备注:checkout之后,在/etc/puppet/environments/testing目录下就会有一份SVN服务器上最新版本的副本。

4.1 设置pre-commit

设置pre-commit钩子可以提交文件到SNV服务器之前对puppet语法进行检查,语法通过则提交成功,语法错误则提交失败。

4.2 设置post-commit

设置post-commit钩子可以在正确提交文件至SVN服务器之后,puppetmaster的模块目录/etc/puppet/environments/testing会自动从SNV服务器上update最新的版本库到本地。

  1. #!/bin/sh
  2. # POST-COMMIT HOOK
  3. REPOS="$1"
  4. REV="$2"
  5. #mailer.py commit "$REPOS" "$REV" /path/to/mailer.conf
  6. export LANG=en_US.UTF-8
  7. SVN=/usr/bin/svn
  8. PUPPET_DIR=/etc/puppet
  9. #/usr/bin/svn up /etc/puppet -non-interactive
  10. $SVN update $PUPPET_DIR --username puppet --password 123.com >>/var/log/puppet/svn_post-commit.log

5.1 本地测试

1)导出版本数据库文件到本地

  1. [root@puppetserver ~]# svn checkout file:///svndata/puppet /puppet/puppet

2)、创建并添加新的目录及文件

  1. [root@puppetserver puppet]# svn add ssh

3)、将修改后的文件提交到SVN服务器,此时版本库版本加1

  1. [root@puppetserver .svn]# svn commit -m "add ssh modules" /puppet/puppet/*

5.2 远程测试(Linux)

5.3 客户端TortoiseSVN测试(Windows)