2023-10-06 21:05:35 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2023-10-13 18:09:33 +00:00
|
|
|
bundle agent mysql(cfg_param)
|
2023-10-06 21:05:35 +00:00
|
|
|
{
|
|
|
|
vars:
|
2023-10-13 18:09:33 +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::
|
|
|
|
# "pkg" string => "mariadb106-server";
|
|
|
|
"pkg" string => "mysql80-server";
|
|
|
|
"service_name" string => "mysql-server";
|
2023-10-13 18:09:33 +00:00
|
|
|
"cfg_dir" string => "/usr/local/etc/mysql";
|
2023-10-06 21:05:35 +00:00
|
|
|
"cfg_file" string => "/usr/local/etc/mysql/my.cnf";
|
|
|
|
"mysql_cmd" string => "/usr/local/bin/mysql";
|
|
|
|
debian::
|
2023-10-13 18:09:33 +00:00
|
|
|
"pkg" slist => {"mariadb-server"};
|
2023-10-06 21:05:35 +00:00
|
|
|
"service_name" string => "mysql";
|
2023-10-13 18:09:33 +00:00
|
|
|
"cfg_dir" string => "/etc/mysql";
|
2023-10-06 21:05:35 +00:00
|
|
|
"cfg_file" string => "/etc/mysql/my.cnf";
|
|
|
|
"mysql_cmd" string => "/usr/bin/mysql";
|
|
|
|
files:
|
2023-10-13 18:09:33 +00:00
|
|
|
"$(cfg_dir)/."
|
|
|
|
create => "true",
|
|
|
|
perms => mog("0755","root","root"),
|
|
|
|
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",
|
2023-10-13 18:09:33 +00:00
|
|
|
depends_on => {"mysql_cfg_dir_created"},
|
2023-10-06 21:05:35 +00:00
|
|
|
classes => if_repaired("mysql_repaired");
|
|
|
|
|
|
|
|
|
|
|
|
methods:
|
2023-10-11 09:25:38 +00:00
|
|
|
"any" usebundle => wmde_install_packages(@(mysql.pkg),"mysql"),
|
|
|
|
depends_on => { "mysql_cfg_created" };
|
2023-10-06 21:05:35 +00:00
|
|
|
|
|
|
|
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"};
|
|
|
|
|
2023-10-13 18:09:33 +00:00
|
|
|
reports:
|
2023-10-06 21:05:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
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';
|
|
|
|
#"cmd" string => '"if mysql -e \\\"show tables"';
|
|
|
|
# "cmdfile" string => hash("$cmd)","sha256");
|
|
|
|
|
|
|
|
#files:
|
|
|
|
# "/tmp/$(cmdfile)"
|
|
|
|
# create => "true",
|
|
|
|
# content => "$(cmd)",
|
|
|
|
# handle => "mysql_$(cmdfile)_created";
|
|
|
|
|
|
|
|
|
|
|
|
commands:
|
|
|
|
"$(cmd)"
|
|
|
|
contain => mysql_cmd,
|
|
|
|
inform => "false",
|
|
|
|
module => "true";
|
|
|
|
|
|
|
|
reports:
|
|
|
|
# "CMD: $(cmd)";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-06 21:05:35 +00:00
|
|
|
bundle agent create_mysql_db(cfg)
|
|
|
|
{
|
|
|
|
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])";
|
|
|
|
|
|
|
|
"args" string => "-e \"CREATE DATABASE IF NOT EXISTS $(db_name); GRANT ALL PRIVILEGES ON $(db_name).* TO '$(db_user)'@'$(host)' IDENTIFIED BY '$(db_pass)'; \" ";
|
|
|
|
|
|
|
|
commands:
|
|
|
|
debian::
|
|
|
|
"$(mysql.mysql_cmd)"
|
|
|
|
args => "$(args)",
|
|
|
|
inform => "false";
|
|
|
|
|
2023-10-11 09:25:38 +00:00
|
|
|
#mysql -u <username> -p -e "USE <database_name>;" && mysql -u <username> -p -e "SHOW TABLES LIKE '<table_name>';" | grep -q '<table_name>' && exit 0 || exit 1
|
|
|
|
|
2023-10-06 21:05:35 +00:00
|
|
|
reports:
|
|
|
|
}
|