diff --git a/mysql/client.sls b/mysql/client.sls index ed2791f..e603352 100644 --- a/mysql/client.sls +++ b/mysql/client.sls @@ -1,3 +1,6 @@ +include: + - mysql.config + {% from "mysql/defaults.yaml" import rawmap with context %} {%- set mysql = salt['grains.filter_by'](rawmap, grain='os', merge=salt['pillar.get']('mysql:lookup')) %} diff --git a/mysql/config.sls b/mysql/config.sls new file mode 100644 index 0000000..336106a --- /dev/null +++ b/mysql/config.sls @@ -0,0 +1,70 @@ +{% from "mysql/defaults.yaml" import rawmap with context %} +{%- set mysql = salt['grains.filter_by'](rawmap, grain='os', merge=salt['pillar.get']('mysql:lookup')) %} +{% set os_family = salt['grains.get']('os_family', None) %} + +{% if "config_directory" in mysql %} +mysql_config_directory: + file.directory: + - name: {{ mysql.config_directory }} + {% if os_family in ['Debian', 'Gentoo', 'RedHat'] %} + - user: root + - group: root + - mode: 755 + {% endif %} + - makedirs: True + +{% if "server_config" in mysql %} +mysql_server_config: + file.managed: + - name: {{ mysql.config_directory + mysql.server_config.file }} + - template: jinja + - source: salt://mysql/files/server.cnf + {% if os_family in ['Debian', 'Gentoo', 'RedHat'] %} + - user: root + - group: root + - mode: 644 + {% endif %} +{% endif %} + +{% if "library_config" in mysql %} +mysql_library_config: + file.managed: + - name: {{ mysql.config_directory + mysql.library_config.file }} + - template: jinja + - source: salt://mysql/files/client.cnf + {% if os_family in ['Debian', 'Gentoo', 'RedHat'] %} + - user: root + - group: root + - mode: 644 + {% endif %} +{% endif %} + +{% if "clients_config" in mysql %} +mysql_clients_config: + file.managed: + - name: {{ mysql.config_directory + mysql.clients_config.file }} + - template: jinja + - source: salt://mysql/files/mysql-clients.cnf + {% if os_family in ['Debian', 'Gentoo', 'RedHat'] %} + - user: root + - group: root + - mode: 644 + {% endif %} +{% endif %} + +{% endif %} + +mysql_config: + file.managed: + - name: {{ mysql.config.file }} + - template: jinja +{% if "config_directory" in mysql %} + - source: salt://mysql/files/my-include.cnf +{% else %} + - source: salt://mysql/files/my.cnf +{% endif %} + {% if os_family in ['Debian', 'Gentoo', 'RedHat'] %} + - user: root + - group: root + - mode: 644 + {% endif %} diff --git a/mysql/defaults.yaml b/mysql/defaults.yaml index 3b72df0..14f8997 100644 --- a/mysql/defaults.yaml +++ b/mysql/defaults.yaml @@ -98,8 +98,11 @@ CentOS: service: mysqld python: MySQL-python dev: mysql-devel + config_directory: /etc/my.cnf.d/ config: file: /etc/my.cnf + server_config: + file: server.cnf sections: mysqld_safe: log_error: /var/log/mysqld.log diff --git a/mysql/files/client.cnf b/mysql/files/client.cnf new file mode 100644 index 0000000..0dee293 --- /dev/null +++ b/mysql/files/client.cnf @@ -0,0 +1,40 @@ +# DO NOT CHANGE THIS FILE! +# This config is generated by SALTSTACK +# and all change will be overrided on next salt call +{#- +===== FETCH DATA ===== +-#} +{%- from "mysql/defaults.yaml" import rawmap with context -%} +{%- from "mysql/supported_sections.yaml" import supported_sections with context -%} +{%- set datamap = salt['grains.filter_by'](rawmap, grain='os', merge=salt['pillar.get']('mysql:library:lookup')) -%} +{#- +===== COMBINE DATA ===== +-#} +{%- set goodParamList = datamap.library_config.sections -%} +{%- for section_name in supported_sections -%} + {%- set sectdict = datamap.library_config.sections[section_name] | default({}) -%} + {%- for mparam, mvalue in salt['pillar.get']('mysql:library:'+section_name, {}).items() -%} + {%- set mparamUnderscore = mparam | replace('-','_') -%} + {%- do sectdict.update({mparamUnderscore:mvalue}) -%} + {%- endfor -%} + {%- do goodParamList.update({section_name:sectdict}) -%} +{%- endfor -%} +{#- +===== PRINT DATA ===== +-#} +{%- for sname,sdata in goodParamList.items() -%} +{%- if sdata %} + +[{{ sname }}] +{%- for mparam, mvalue in sdata.items()|default([])|sort -%} +{%- set indents = 40 - mparam|count %} +{% if mvalue == "noarg_present" -%} +{{ mparam }} +{%- else -%} +{{ mparam }}{{ '='|indent(indents, true) }} {{ mvalue }} +{%- endif -%} +{%- endfor -%} +{%- endif -%} +{%- endfor %} + +{{ datamap.library_config.append | default('') }} diff --git a/mysql/files/my-include.cnf b/mysql/files/my-include.cnf new file mode 100644 index 0000000..5e65b7c --- /dev/null +++ b/mysql/files/my-include.cnf @@ -0,0 +1,47 @@ +# DO NOT CHANGE THIS FILE! +# This config is generated by SALTSTACK +# and all change will be overrided on next salt call +{#- +===== FETCH DATA ===== +-#} +{%- from "mysql/defaults.yaml" import rawmap with context -%} +{%- from "mysql/supported_sections.yaml" import supported_sections with context -%} +{%- set datamap = salt['grains.filter_by'](rawmap, grain='os', merge=salt['pillar.get']('mysql:global:lookup')) -%} +{#- +===== COMBINE DATA ===== +-#} +{%- if "sections" in datamap.config -%} +{%- set goodParamList = datamap.config.sections -%} +{%- for section_name in supported_sections -%} + {%- set sectdict = datamap.config.sections[section_name] | default({}) -%} + {%- for mparam, mvalue in salt['pillar.get']('mysql:global:'+section_name, {}).items() -%} + {%- set mparamUnderscore = mparam | replace('-','_') -%} + {%- do sectdict.update({mparamUnderscore:mvalue}) -%} + {%- endfor -%} + {%- do goodParamList.update({section_name:sectdict}) -%} +{%- endfor -%} +{%- else -%} +{%- set goodParamList = {} -%} +{%- endif -%} +{#- +===== PRINT DATA ===== +-#} +{%- for sname,sdata in goodParamList.items() -%} +{%- if sdata %} + +[{{ sname }}] +{%- for mparam, mvalue in sdata.items()|default([])|sort -%} +{%- set indents = 40 - mparam|count %} +{% if mvalue == "noarg_present" -%} +{{ mparam }} +{%- else -%} +{{ mparam }}{{ '='|indent(indents, true) }} {{ mvalue }} +{%- endif -%} +{%- endfor -%} +{%- endif -%} +{%- endfor %} + +# +# include all files from the config directory +# +!includedir {{ datamap.config_directory }} diff --git a/mysql/files/mysql-clients.cnf b/mysql/files/mysql-clients.cnf new file mode 100644 index 0000000..d56177a --- /dev/null +++ b/mysql/files/mysql-clients.cnf @@ -0,0 +1,40 @@ +# DO NOT CHANGE THIS FILE! +# This config is generated by SALTSTACK +# and all change will be overrided on next salt call +{#- +===== FETCH DATA ===== +-#} +{%- from "mysql/defaults.yaml" import rawmap with context -%} +{%- from "mysql/supported_sections.yaml" import supported_sections with context -%} +{%- set datamap = salt['grains.filter_by'](rawmap, grain='os', merge=salt['pillar.get']('mysql:clients:lookup')) -%} +{#- +===== COMBINE DATA ===== +-#} +{%- set goodParamList = datamap.clients_config.sections -%} +{%- for section_name in supported_sections -%} + {%- set sectdict = datamap.clients_config.sections[section_name] | default({}) -%} + {%- for mparam, mvalue in salt['pillar.get']('mysql:clients:'+section_name, {}).items() -%} + {%- set mparamUnderscore = mparam | replace('-','_') -%} + {%- do sectdict.update({mparamUnderscore:mvalue}) -%} + {%- endfor -%} + {%- do goodParamList.update({section_name:sectdict}) -%} +{%- endfor -%} +{#- +===== PRINT DATA ===== +-#} +{%- for sname,sdata in goodParamList.items() -%} +{%- if sdata %} + +[{{ sname }}] +{%- for mparam, mvalue in sdata.items()|default([])|sort -%} +{%- set indents = 40 - mparam|count %} +{% if mvalue == "noarg_present" -%} +{{ mparam }} +{%- else -%} +{{ mparam }}{{ '='|indent(indents, true) }} {{ mvalue }} +{%- endif -%} +{%- endfor -%} +{%- endif -%} +{%- endfor %} + +{{ datamap.clients_config.append | default('') }} diff --git a/mysql/files/server.cnf b/mysql/files/server.cnf new file mode 100644 index 0000000..25e5e03 --- /dev/null +++ b/mysql/files/server.cnf @@ -0,0 +1,40 @@ +# DO NOT CHANGE THIS FILE! +# This config is generated by SALTSTACK +# and all change will be overrided on next salt call +{#- +===== FETCH DATA ===== +-#} +{%- from "mysql/defaults.yaml" import rawmap with context -%} +{%- from "mysql/supported_sections.yaml" import supported_sections with context -%} +{%- set datamap = salt['grains.filter_by'](rawmap, grain='os', merge=salt['pillar.get']('mysql:server:lookup')) -%} +{#- +===== COMBINE DATA ===== +-#} +{%- set goodParamList = datamap.server_config.sections -%} +{%- for section_name in supported_sections -%} + {%- set sectdict = datamap.server_config.sections[section_name] | default({}) -%} + {%- for mparam, mvalue in salt['pillar.get']('mysql:server:'+section_name, {}).items() -%} + {%- set mparamUnderscore = mparam | replace('-','_') -%} + {%- do sectdict.update({mparamUnderscore:mvalue}) -%} + {%- endfor -%} + {%- do goodParamList.update({section_name:sectdict}) -%} +{%- endfor -%} +{#- +===== PRINT DATA ===== +-#} +{%- for sname,sdata in goodParamList.items() -%} +{%- if sdata %} + +[{{ sname }}] +{%- for mparam, mvalue in sdata.items()|default([])|sort -%} +{%- set indents = 40 - mparam|count %} +{% if mvalue == "noarg_present" -%} +{{ mparam }} +{%- else -%} +{{ mparam }}{{ '='|indent(indents, true) }} {{ mvalue }} +{%- endif -%} +{%- endfor -%} +{%- endif -%} +{%- endfor %} + +{{ datamap.server_config.append | default('') }} diff --git a/mysql/server.sls b/mysql/server.sls index 8478bd7..f04ad5d 100644 --- a/mysql/server.sls +++ b/mysql/server.sls @@ -1,3 +1,7 @@ +include: + - mysql.config + - mysql.python + {% from "mysql/defaults.yaml" import rawmap with context %} {%- set mysql = salt['grains.filter_by'](rawmap, grain='os', merge=salt['pillar.get']('mysql:lookup')) %} @@ -32,9 +36,6 @@ mysql_root_password: - require: - service: mysqld -include: - - mysql.python - {% for host in ['localhost', 'localhost.localdomain', salt['grains.get']('fqdn')] %} mysql_delete_anonymous_user_{{ host }}: mysql_user: @@ -83,19 +84,10 @@ mysqld: - enable: True - watch: - pkg: mysqld - -mysql_config: - file.managed: - - name: {{ mysql.config.file }} - - template: jinja - - source: salt://mysql/files/my.cnf - - watch_in: - - service: mysqld - {% if os_family in ['Debian', 'Gentoo', 'RedHat'] %} - - user: root - - group: root - - mode: 644 - {% endif %} + - file: mysql_config +{% if "config_directory" in mysql and "server_config" in mysql %} + - file: mysql_server_config +{% endif %} # official oracle mysql repo # creates this file, that rewrites /etc/mysql/my.cnf setting diff --git a/mysql/supported_sections.yaml b/mysql/supported_sections.yaml index ba4c754..890121e 100644 --- a/mysql/supported_sections.yaml +++ b/mysql/supported_sections.yaml @@ -1,6 +1,9 @@ # vim {% load_yaml as supported_sections %} +- client-server - client +- client-mariadb +- mariadb - mysql - mysqldump - mysqld_safe @@ -9,9 +12,14 @@ - mysqlcheck - mysqlimport - mysqlshow +- mysql_upgrade +- mysqlbinlog +- mysqlslap - myisampack - myisamchk - isamchk - mysqld - mysqld_multi +- server +- xtrabackup {% endload %} diff --git a/pillar.example b/pillar.example index ac83cb1..07a8f30 100644 --- a/pillar.example +++ b/pillar.example @@ -1,4 +1,18 @@ mysql: + global: + client-server: + default_character_set: utf8 + + clients: + mysql: + default_character_set: utf8 + mysqldump: + default_character_set: utf8 + + library: + client: + default_character_set: utf8 + server: # Use this account for database admin (defaults to root) root_user: 'admin'