cfengineテスト


環境
3台の仮想マシン:
192.168.60.128(policy hub)  server
192.168.60.129(client)      agent1
192.168.60.130(client)      agent2
 
構成が完了しました.テストの準備ができました.次の要件があります.
1.server、agent 1はtestユーザーを確立し、agent 2はtestユーザーを削除する
2.sshを設定してtestユーザーがログインできないようにする
3.同期スクリプトディレクトリ
4.スクリプトに実行権限があることを確認する
5.lamp環境のインストール
6.apache、mysqlサービスの運行を確保する
 
次の例はテストされたもので、serverで作成されたいくつかの修正が必要かもしれません.
server、agent 1はtestユーザーを確立し、agent 2はtestユーザーを削除する

  
  
  
  
  1. bundle agent user_add_del  # user_add_del.cf
  2.     vars: 
  3.         "users" slist => {"test"}; 
  4.     classes: 
  5.         "add_$(users)" not => userexists("$(users)"); # class
  6.     commands: 
  7. # classes test , echo
  8. # cfengine ,
  9. # ifvarclass class expression
  10. # "cfengine::"
  11.         "/bin/bash $(sys.workdir)/inputs/shell/useradd.sh $(users)" 
  12.         ifvarclass => "add_$(users)&(192_168_60_128|192_168_60_129)"; 
  13. # "192_168_60_130" hard class, cfengine
  14.         "/usr/sbin/userdel -rf $(users)" 
  15.         ifvarclass => "!add_$(users)&192_168_60_130"; 

sshを構成してtestユーザーがログインできないようにする

  
  
  
  
  1. bundle agent config_ssh  # config_ssh.cf
  2.     vars: 
  3.         # ssh config file 
  4.         "ssh" string => "/etc/ssh/sshd_config"; 
  5.      
  6.         # ssh config to set  
  7.         "sshd[DenyUsers]" string => "test"; 
  8.  
  9.     methods:  # methods agent bundle,
  10.         "sshd"  usebundle => edit_sshd;  
  11.  
  12. bundle agent edit_sshd 
  13.     files: 
  14.         "$(config_ssh.ssh)" 
  15.         handle => "edit_sshd", 
  16.         comment => "Set desired sshd_config parameters", 
  17.         edit_line => set_config_values("config_ssh.sshd"), 
  18.         classes => if_repaired("restart_sshd"); # promises repaired restart_sshd
  19.      
  20.     commands: 
  21.         restart_sshd.!no_restarts:: 
  22.         "/etc/init.d/sshd reload" 
  23.         handle => "sshd_restart", 
  24.         comment => "Restart sshd if the configuration file was modified"; 
  25.  
  26. # Sets the RHS of variables in the file of the form # LHS RHS
  27. bundle edit_line set_config_values(v) 
  28.     vars: 
  29.         "index" slist => getindices("$(v)"); 
  30.         "cindex[$(index)]" string => canonify("$(index)"); #canonify class
  31.      
  32.     replace_patterns: 
  33.         "^\s*($(index)\s+(?!$($(v)[$(index)])).*|# ?$(index)\s+.*)$" 
  34.         replace_with => value("$(index) $($(v)[$(index)])"), 
  35.         classes => always("replace_attempted_$(cindex[$(index)])"); 
  36.  
  37.     insert_lines: 
  38.         "$(index) $($(v)[$(index)])" 
  39.         ifvarclass => "replace_attempted_$(cindex[$(index)])"; 
  40. body classes always(x)
  41. {
  42. promise_kept => { "$(x)" };
  43. promise_repaired => { "$(x)" };
  44. repair_failed => { "$(x)" };
  45. repair_denied => { "$(x)" };
  46. repair_timeout => { "$(x)" };
  47. }

スクリプト・ディレクトリを同期し、スクリプトに実行権限があることを確認します.

  
  
  
  
  1. bundle agent copy_shell  # copy_shell.cf
  2.     vars: 
  3. # masterfiles/shell ,
  4. # /var/ftp/pub/shell, "bundle server access_rules"
  5.         "shell_location" string => "/var/cfengine/masterfiles/shell"; 
  6.         "inputs_location" string => "/var/cfengine/inputs"; 
  7.     files: 
  8.         "$(inputs_location)/shell" 
  9.         comment => "copy shell scripts from policy hub", 
  10.         handle  => "sync_shell_scripts", 
  11.         copy_from => secure_cp("$(shell_location)","$(sys.policy_hub)"), 
  12.         depth_search => shell_recurse("inf"), #  
  13.         file_select  => shell_files; # ,
  14.  
  15.         "$(inputs_location)/shell" 
  16.         comment => "ensure shell scripts have execute permissions", 
  17.         handle  => "update_shell_files", 
  18.         perms   => m("755"), 
  19.         depth_search => shell_recurse("inf"); 
  20.  
  21. body file_select shell_files 
  22.     leaf_name => { ".*.sh" };  # shell
  23.     file_result => "leaf_name"; # leaf_name
  24.  
  25. body depth_search shell_recurse(d) 
  26.     include_basedir => "true"; 
  27.     depth => "$(d)"; 

lamp環境のインストール

  
  
  
  
  1. #
  2. bundle agent lamp_pkgs_installed # lamp.cf 
  3.     vars: 
  4.         "desired_pkgs" slist => { "gcc", "gcc-c++",  
  5.                                   "libtermcap-devel",  
  6.                                   "libxml2-devel", 
  7.                                 };   
  8.      
  9.     packages: 
  10.         "$(desired_pkgs)" 
  11.             package_policy => "add", #  
  12.             package_method => yum, 
  13.             comment => "install desired packages"; 
  14.  
  15. # rpm packages
  16. bundle agent rpm_pkgs_remove 
  17.     vars: 
  18.         "rpm_lamp" slist => { "httpd", "httpd-devel", 
  19.                               "mysql", "mysql-server", "mysql-devel", 
  20.                               "php", "php-devel", "php-mysql", "php-common", 
  21.                             };   
  22.      
  23.     packages: 
  24.         "$(rpm_lamp)" 
  25.             package_policy => "delete", 
  26.             package_method => yum, 
  27.             comment => "remove rpm version packages"; 


  
  
  
  
  1. bundle agent mysql_install  # mysql_install.cf
  2.     vars: 
  3.         "install_dir" string => "/usr/local/lamp/mysql"; 
  4.         "config_file" string => "/etc/my.cnf"; 
  5.         "shell_dir"   string => "/mnt/public/shell"; #  
  6.     classes: 
  7.         "mysql_install_dir_exists" 
  8.             expression => fileexists("$(install_dir)"); 
  9.         "mysql_config_file_exists" 
  10.             expression => fileexists("$(config_file)"); 
  11.     reports: 
  12.         !mysql_install_dir_exists:: 
  13.             "$(install_dir) is not present."; 
  14.     commands: 
  15. # ,
  16. # ,
  17. # cf-agent,
  18.         !mysql_install_dir_exists.Hr16.Min00_05::  
  19.             "$(shell_dir)/install_mysql.sh"; 
  20. #
  21.         mysql_install_dir_exists&!mysql_config_file_exists:: 
  22.             "/bin/cp $(install_dir)/share/mysql/my-large.cf $(config_file)"; 
  23.             "/bin/chown root:root $(config_file)"; 
  24.             "/bin/chmod 644 $(config_file)"; 


  
  
  
  
  1. # mysql_install.cf
  2. bundle agent apache_install # apache_install.cf
  3.     vars: 
  4.         "install_dir" string => "/usr/local/lamp/apache"; 
  5.         "config_file" string => "/etc/httpd.conf"; 
  6.         "shell_dir"   string => "/mnt/public/shell"; 
  7.     classes: 
  8.         "apache_install_dir_exists" 
  9.             expression => fileexists("$(install_dir)"); 
  10.         "apache_config_file_exists" 
  11.             expression => fileexists("$(config_file)"); 
  12.     reports: 
  13.         !apache_install_dir_exists:: 
  14.             "$(install_dir) is not present."; 
  15.     commands: 
  16.         !apache_install_dir_exists.Hr17.Min00_05:: 
  17.             "$(shell_dir)/install_apache.sh"; 
  18.         apache_install_dir_exists&!apache_config_file_exists:: 
  19.             "/bin/ln -s $(install_dir)/conf/httpd.conf /etc/httpd.conf"; 


  
  
  
  
  1. # mysql_install.cf
  2. bundle agent php_install  # php_install.cf
  3.     vars: 
  4.         "install_dir" string => "/usr/local/lamp/php"; 
  5.         "mysql_dir" string => "/usr/local/lamp/mysql"; 
  6.         "apache_dir" string => "/usr/local/lamp/apache"; 
  7.         "config_file" string => "/etc/php.ini"; 
  8.         "shell_dir"   string => "/mnt/public/shell"; 
  9.     classes: 
  10.         "php_install_dir_exists" 
  11.             expression => fileexists("$(install_dir)"); 
  12.         "mysql_install_dir_exists" 
  13.             expression => fileexists("$(mysql_dir)"); 
  14.         "apache_install_dir_exists" 
  15.             expression => fileexists("$(apache_dir)"); 
  16.         "php_config_file_exists" 
  17.             expression => fileexists("$(config_file)"); 
  18.     reports: 
  19.         !php_install_dir_exists:: 
  20.             "$(install_dir) is not present."; 
  21.     commands: 
  22. # php mysql、apache ,
  23.         !php_install_dir_exists.mysql_install_dir_exists.apache_install_dir_exists.Hr18.Min00_05:: 
  24.             "$(shell_dir)/install_php.sh"; 
  25.         php_install_dir_exists&!php_config_file_exists:: 
  26.             "/bin/ln -s $(install_dir)/etc/php.ini /etc/php.ini"; 

apache、mysqlサービスの実行を確認

  
  
  
  
  1. bundle agent start_process 
  2.     vars: 
  3.         "processes" slist => {"httpd", "mysqld"}; 
  4.     classes: 
  5.         "$(processes)_exist" expression => fileexists("/etc/init.d/$(processes)"); 
  6.     processes: 
  7.         "$(processes)" restart_class => canonify("start_$(processes)"); 
  8.     commands: 
  9.         "/etc/init.d/$(processes) start" 
  10.         ifvarclass => "start_$(processes)&$(processes)_exist"; 
  11.     reports: 
  12.         cfengine:: 
  13.             "--> apache is not running on $(sys.fqhost)" 
  14.             ifvarclass => "!httpd_exist"; 
  15.             "--> mysql is not running on $(sys.fqhost)" 
  16.             ifvarclass => "!mysqld_exist"; 

上のファイルをmasterfilesディレクトリに置いてmasterfilesでpromisesを変更します.cf

  
  
  
  
  1. bundlesequence => {  
  2.                     "main", "user_add_del", "config_ssh", 
  3. "copy_shell",
  4.                     "lamp_pkgs_installed", "mysql_install", "apache_install", 
  5.                     "php_install",   
  6.                     
  7.                     };   
  8.  
  9.  inputs => {  
  10.             "cfengine_stdlib.cf",  
  11.            "user_add_del.cf", 
  12. "config_ssh.cf",
  13. "copy_shell.cf",
  14.             "lamp/lamp.cf", 
  15.             "lamp/mysql_install.cf", 
  16.             "lamp/apache_install.cf", 
  17.             "lamp/php_install.cf", 
  18.            };   

Lampインストールスクリプトは添付ファイルにあり、Notepad++を使用して開き、スクリプトを/mnt/public/shellにコピーします.