wmdeit-cf-wmdelib/mysql.cf

335 lines
9.1 KiB
CFEngine3
Raw Normal View History

2023-10-06 21:05:35 +00:00
#
#
#
bundle agent mysql(cfg_param)
2023-10-06 21:05:35 +00:00
{
vars:
2023-10-22 23:48:08 +00:00
"default_cfg" data => '{
"bind_address":"127.0.0.1",
"user":"mysql",
"port":"3306"
}';
"cfg" data => mergedata(@(default_cfg),@(cfg_param));
2023-10-06 21:05:35 +00:00
freebsd::
2023-10-22 23:48:08 +00:00
"user" string => "mysql";
2023-10-22 21:29:39 +00:00
# "server_pkgs" slist => {"mariadb106-server"};
# "client_pkgs" slist => {"mariadb106-client"};
"server_pkgs" slist => {"mysql80-server"};
2023-10-22 11:39:35 +00:00
"client_pkgs" slist => {"mysql80-client"};
2023-10-22 21:29:39 +00:00
"type" string => "mysql8";
2023-10-06 21:05:35 +00:00
"service_name" string => "mysql-server";
"cfg_dir" string => "/usr/local/etc/mysql";
2023-10-06 21:05:35 +00:00
"cfg_file" string => "/usr/local/etc/mysql/my.cnf";
"bin_dir" string => "/usr/local/bin";
2023-10-22 23:48:08 +00:00
"default_datadir" string => "/var/db/mysql";
2023-10-23 00:47:29 +00:00
"mysqld_cmd" string => "/usr/local/libexec/mysqld";
2023-10-06 21:05:35 +00:00
debian::
2023-10-22 23:48:08 +00:00
"user" string => "mysql";
2023-10-22 21:29:39 +00:00
"server_pkgs" slist => {"mariadb-server"};
"client_pkgs" slist => {"mariadb-client"};
2023-10-22 21:29:39 +00:00
"type" string => "mariadb";
2023-10-06 21:05:35 +00:00
"service_name" string => "mysql";
"cfg_dir" string => "/etc/mysql";
2023-10-06 21:05:35 +00:00
"cfg_file" string => "/etc/mysql/my.cnf";
"bin_dir" string => "/usr/bin";
2023-10-22 23:48:08 +00:00
"default_datadir" string => "/var/lib/mysql";
2023-10-23 00:47:29 +00:00
"mysqld_cmd" string => "/usr/sbin/mysqld";
2023-10-22 23:48:08 +00:00
2023-10-24 23:25:10 +00:00
fedora|centos::
2023-10-22 23:48:08 +00:00
"user" string => "mysql";
2023-10-22 21:29:39 +00:00
"server_pkgs" slist => {"mariadb-server"};
"client_pkgs" slist => {"mariadb-client"};
"type" string => "mariadb";
"service_name" string => "mariadb";
"cfg_dir" string => "/etc/mysql";
"cfg_file" string => "/etc/my.cnf";
2023-10-22 23:48:08 +00:00
"bin_dir" string => "/usr/bin";
"default_datadir" string => "/var/lib/mysql";
2023-10-23 00:47:29 +00:00
"mysqld_cmd" string => "/usr/sbin/mysqld";
2023-10-22 23:48:08 +00:00
any::
"mysql_cmd" string => "$(bin_dir)/mysql";
"mysqldump_cmd" string => "$(bin_dir)/mysqldump";
2023-10-23 00:47:29 +00:00
2023-10-06 21:05:35 +00:00
files:
2023-10-22 11:39:35 +00:00
freebsd::
"/etc/rc.conf.d/mysql"
create => "true",
content => "mysql_enable=YES";
any::
"$(cfg_dir)/."
create => "true",
2023-10-22 11:39:35 +00:00
perms => m("0755"),
handle => "mysql_cfg_dir_created";
2023-10-11 09:25:38 +00:00
2023-10-06 21:05:35 +00:00
"$(cfg_file)"
create => "true",
perms => m("644"),
edit_template => "$(sys.workdir)/inputs/$(def.wmde_libdir)/templates/my.cnf.mustache",
template_method => "mustache",
handle => "mysql_cfg_created",
depends_on => {"mysql_cfg_dir_created"},
2023-10-06 21:05:35 +00:00
classes => if_repaired("mysql_repaired");
services:
"$(service_name)"
depends_on => {"mysql_pkgs_installed"},
service_policy => "start",
handle => "mysql_running";
mysql_repaired::
"$(service_name)"
service_policy => "restart",
depends_on => {"mysql_running","mysql_cfg_created"};
reports:
2023-10-06 21:05:35 +00:00
}
2023-10-22 21:29:39 +00:00
bundle agent install_mysql_server(cfg)
{
2023-10-23 00:47:29 +00:00
classes:
"type_$(mysql.type)" ;
2023-10-22 21:29:39 +00:00
vars:
"mysqld_idx" slist => getindices(@(cfg[settings][mysqld]));
"settings[mysqld][$(mysqld_idx)]" string => "$(cfg[settings][mysqld][$(mysqld_idx)])";
2023-10-23 00:47:29 +00:00
"datadir" string => ifelse(isvariable("cfg[settings][mysqld][datadir]"),
"$(cfg[settings][mysqld][datadir])",
"$(mysql.default_datadir)");
type_mariadb::
"install_db_cmd" string => "if [ ! -d $(datadir)/mysql ]; then $(mysql.bin_dir)/mariadb-install-db --user=$(mysql.user)>/dev/null ; fi";
type_mysql8::
"install_db_cmd" string => "if [ ! -d $(datadir)/mysql ]; then $(mysql.mysqld_cmd) --initialize-insecure --user=$(mysql.user); fi";
2023-10-22 21:29:39 +00:00
"j" string => storejson(@(cfg));
methods:
"any" usebundle => wmde_install_packages(@(mysql.server_pkgs),"mysql_server");
files:
"$(mysql.cfg_file)"
copy_from => seed_cp("$(sys.workdir)/inputs/$(def.wmde_libdir)/templates/my.cnf.mustache"),
handle => "myslq_server_cfg_created",
depends_on => {"mysql_server_pkgs_installed"};
"$(mysql.cfg_file)"
create => "true",
perms => m("644"),
edit_line => set_variable_values_inix("$(this.bundle).settings","mysqld"),
depends_on => {"myslq_server_cfg_created"},
handle => "myslq_server_cfg_edited",
classes => if_repaired("mysql_server_repaired");
2023-10-23 00:47:29 +00:00
2023-10-22 23:48:08 +00:00
commands:
2023-10-23 00:47:29 +00:00
"$(install_db_cmd)"
2023-10-22 23:48:08 +00:00
handle=>"mysql_datadir_initialized",
contain => wmde_cmd_useshell,
inform => "false",
depends_on => {"myslq_server_cfg_edited"};
2023-10-22 21:29:39 +00:00
methods:
"any" usebundle => wmde_service("$(mysql.service_name)","mysql_server_kept","mysql_server_repaired"),
2023-10-22 23:48:08 +00:00
depends_on => {"mysql_datadir_initialized"};
2023-10-11 09:25:38 +00:00
2023-10-22 21:29:39 +00:00
reports:
}
bundle edit_line set_variable_values_inix(tab, sectionName)
{
vars:
"index" slist => getindices("$(tab)[$(sectionName)]");
# Be careful if the index string contains funny chars
"cindex[$(index)]" string => canonify("$(index)");
classes:
"edit_$(cindex[$(index)])" not => strcmp("$($(tab)[$(sectionName)][$(index)])","dontchange"),
comment => "Create conditions to make changes";
field_edits:
# If the line is there, but commented out, first uncomment it
# "#+\s*$(index)\s*=.*"
# select_region => INI_section(escape("$(sectionName)")),
# edit_field => col("\s*=\s*","1","$(index)","set"),
# if => "edit_$(cindex[$(index)])";
# match a line starting like the key something
"\s*$(index)\s*=.*"
edit_field => col("\s*=\s*","2","$($(tab)[$(sectionName)][$(index)])","set"),
select_region => INI_section(escape("$(sectionName)")),
classes => results("bundle", "set_variable_values_ini_not_$(cindex[$(index)])"),
if => "edit_$(cindex[$(index)])";
delete_lines:
"!include.*";
insert_lines:
"[$(sectionName)]"
location => start,
comment => "Insert lines";
"$(index)=$($(tab)[$(sectionName)][$(index)])"
select_region => INI_section(escape("$(sectionName)")),
if => "!(set_variable_values_ini_not_$(cindex[$(index)])_kept|set_variable_values_ini_not_$(cindex[$(index)])_repaired).edit_$(cindex[$(index)])";
}
bundle agent install_mysql_client
{
methods:
"any" usebundle => wmde_install_packages(@(mysql.client_pkgs),"mysql_client"),
handle => "mysql_client_installed";
}
2023-10-11 09:25:38 +00:00
body contain mysql_cmd
{
useshell=>"useshell";
}
bundle agent mysql_table_exists(dbdef,table_name)
{
vars:
"pass_arg" string => ifelse ( isvariable("dbdef[db_pass]"),
"-p$(dbdef[db_pass])",""
);
"host_arg" string => ifelse ( isvariable("dbdef[db_host]"),
"-h$(dbdef[db_host])",""
);
"user_arg" string => ifelse ( isvariable("dbdef[db_user]"),
"-u$(dbdef[db_user])",""
);
"xargs" string => "$(pass_arg) $(host_arg) $(user_arg)";
"classname" string => "mysql_$(dbdef[db_name])_$(table_name)_exists";
"cmd" string => 'if mysql $(xargs) -e "show tables LIKE \'$(table_name)\'" $(dbdef[db_name]) | grep -q \'$(table_name)\'; then echo "+$(classname)"; else echo "-$(classname)"; fi';
commands:
"$(cmd)"
contain => mysql_cmd,
inform => "false",
module => "true";
reports:
}
bundle agent mysql_backup_all(cfg)
{
vars:
"cmd" string => '$(mysql.mysql_cmd) -N -e \'show databases\' | while read dbname; do $(mysql.mysqldump_cmd) --complete-insert --routines --triggers --single-transaction --max_allowed_packet=512M "$dbname" > $(cfg[backup_dir])/"$dbname".sql; done';
2023-10-11 09:25:38 +00:00
reports:
# "CMD: $(cmd)";
2023-10-11 09:25:38 +00:00
}
bundle agent mysql_backup_db(cfg,file)
{
vars:
"table_exists_cmd" string => '$(mysql.mysql_cmd) -e "show tables LIKE \'$(table_name)\'" $(cdfg[db_name]) | grep -q $(table_name)';
"cmd" string => "$(mysql.mysqldump_cmd) --complete-insert --routines --triggers --single-transaction --max_allowed_packet=512M -h$(cfg[db_host]) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) >$(file)";
}
2023-10-17 07:54:06 +00:00
#
# Create cronjob
# @param cfg definition for database, db_user, db_host, db_pass, db_name
# @param file file to dump database into
# @table_name a table in the database which has to exist, if not no dump will be done
# @run "true" or "false", means create cron job or not
#
bundle agent create_mysql_backup_cron_job(cfg,file,table_name,run)
{
classes:
"create_cron" expression => strcmp("$(run)","true");
vars:
2023-10-17 11:29:33 +00:00
"table_exists_cmd" string => '$(mysql.mysql_cmd) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) -e "show tables LIKE \'$(table_name)\'" | grep -q $(table_name)';
"backup_cmd" string => "$(mysql.mysqldump_cmd) --complete-insert --routines --triggers --single-transaction --max_allowed_packet=512M -h$(cfg[db_host]) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) >$(file)";
files:
!create_cron::
2023-10-20 18:01:42 +00:00
"/etc/cron.d/dump-mysql-db-$(cfg[db_name])"
delete=>tidy;
create_cron::
2023-10-20 18:01:42 +00:00
"/etc/cron.d/dump-mysql-db-$(cfg[db_name])"
perms => m("644"),
create => "true",
content => "
#
# /etc/cron.d/roundcube
#
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
0 * * * * root $(table_exists_cmd) && $(backup_cmd)
";
}
2023-10-06 21:05:35 +00:00
bundle agent create_mysql_db(cfg)
{
2023-10-22 21:29:39 +00:00
classes:
"type_$(mysql.type)" ;
2023-10-06 21:05:35 +00:00
vars:
"db_name" string => "$(cfg[db_name])";
"db_user" string => "$(cfg[db_user])";
"host" string => "$(cfg[db_user_host])";
"db_pass" string => "$(cfg[db_pass])";
2023-10-22 21:29:39 +00:00
type_mariadb::
2023-10-06 21:05:35 +00:00
"args" string => "-e \"CREATE DATABASE IF NOT EXISTS $(db_name); GRANT ALL PRIVILEGES ON $(db_name).* TO '$(db_user)'@'$(host)' IDENTIFIED BY '$(db_pass)'; \" ";
2023-10-22 21:29:39 +00:00
type_mysql8::
"args" string => "-e \"CREATE DATABASE IF NOT EXISTS $(db_name); CREATE USER IF NOT EXISTS '$(db_user)'@'$(host)'; ALTER USER '$(db_user)'@'$(host)' IDENTIFIED BY '$(db_pass)'; GRANT ALL PRIVILEGES ON $(db_name).* TO '$(db_user)'@'$(host)';\" ";
2023-10-06 21:05:35 +00:00
commands:
2023-10-22 21:29:39 +00:00
"$(mysql.mysql_cmd)"
args => "$(args)",
inform => "false";
2023-10-06 21:05:35 +00:00
reports:
}
2023-10-17 11:29:33 +00:00
bundle agent restore_mysql_db(cfg,file)
{
commands:
"$(mysql.mysql_cmd)"
args => "-h$(cfg[db_host]) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) < $(file)",
contain => wmde_cmd_useshell;
}