mirror of
https://github.com/saltstack-formulas/mysql-formula.git
synced 2025-04-17 10:10:30 +00:00
Adds meta state and also deals with the default password security issue.
This commit is contained in:
parent
e9534ffe4a
commit
b53d61adfc
6 changed files with 111 additions and 43 deletions
20
README.rst
20
README.rst
|
@ -15,6 +15,10 @@ Available states
|
||||||
.. contents::
|
.. contents::
|
||||||
:local:
|
:local:
|
||||||
|
|
||||||
|
``mysql``
|
||||||
|
|
||||||
|
Meta-state that includes all server packages in the correct order.
|
||||||
|
|
||||||
``mysql.client``
|
``mysql.client``
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
@ -25,13 +29,27 @@ Install the MySQL client package.
|
||||||
|
|
||||||
Install the MySQL server package and start the service.
|
Install the MySQL server package and start the service.
|
||||||
|
|
||||||
Debian OS family supports setting MySQL root password during install via debconf.
|
Debian OS family supports setting MySQL root password during install via
|
||||||
|
debconf.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
If no root password is provided in the pillar, a random one will
|
||||||
|
be created. As-of Hydrogen, this password uses the Python ``random``
|
||||||
|
module via ``test.rand_str``. As ``random`` is considered
|
||||||
|
cryptographically insecure, future formula versions should use the
|
||||||
|
newly available ``random.get_str`` method.
|
||||||
|
|
||||||
``mysql.database``
|
``mysql.database``
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
Create and manage MySQL databases.
|
Create and manage MySQL databases.
|
||||||
|
|
||||||
|
``mysql.python``
|
||||||
|
------------------
|
||||||
|
|
||||||
|
Install mysql python bindings.
|
||||||
|
|
||||||
``mysql.user``
|
``mysql.user``
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,20 @@
|
||||||
{% from "mysql/map.jinja" import mysql with context %}
|
{% from "mysql/map.jinja" import mysql with context %}
|
||||||
|
|
||||||
|
{% set mysql_root_pass = salt['pillar.get']('mysql:server:root_password', salt['test.rand_str'](64)) %}
|
||||||
|
{% set db_states = [] %}
|
||||||
|
|
||||||
include:
|
include:
|
||||||
- mysql.python
|
- mysql.python
|
||||||
|
|
||||||
{% for database in salt['pillar.get']('mysql:database', []) %}
|
{% for database in salt['pillar.get']('mysql:database', []) %}
|
||||||
mysql_db_{{ database }}:
|
{% set state_id = 'mysql_db_' ~ loop.index0 %}
|
||||||
|
{{ state_id }}:
|
||||||
mysql_database.present:
|
mysql_database.present:
|
||||||
- name: {{ database }}
|
- name: {{ database }}
|
||||||
- host: localhost
|
- host: localhost
|
||||||
- connection_user: root
|
- connection_user: root
|
||||||
- connection_pass: '{{ salt['pillar.get']('mysql:server:root_password', 'somepass') }}'
|
- connection_pass: '{{ mysql_root_pass }}'
|
||||||
- connection_charset: utf8
|
- connection_charset: utf8
|
||||||
|
|
||||||
|
{% do db_states.append(state_id) %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
|
||||||
|
|
29
mysql/init.sls
Normal file
29
mysql/init.sls
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
{% from 'mysql/database.sls' import db_states with context %}
|
||||||
|
{% from 'mysql/user.sls' import user_states with context %}
|
||||||
|
|
||||||
|
{% macro requisites(type, states) %}
|
||||||
|
{%- for state in states %}
|
||||||
|
- {{ type }}: {{ state }}
|
||||||
|
{%- endfor -%}
|
||||||
|
{% endmacro %}
|
||||||
|
|
||||||
|
include:
|
||||||
|
- mysql.server
|
||||||
|
- mysql.database
|
||||||
|
- mysql.user
|
||||||
|
|
||||||
|
{% if (db_states|length() + user_states()) > 0 %}
|
||||||
|
extend:
|
||||||
|
mysqld:
|
||||||
|
service:
|
||||||
|
- require_in:
|
||||||
|
{{ requisites(db_states) }}
|
||||||
|
{{ requisites(user_states) }}
|
||||||
|
{% for state in user_states %}
|
||||||
|
{{ state }}:
|
||||||
|
mysql_user:
|
||||||
|
- require:
|
||||||
|
sls: mysql.database
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{% from "mysql/map.jinja" import mysql with context %}
|
{% from "mysql/map.jinja" import mysql with context %}
|
||||||
|
|
||||||
mysql-python:
|
mysql_python:
|
||||||
pkg:
|
pkg:
|
||||||
- installed
|
- installed
|
||||||
- name: {{ mysql.python }}
|
- name: {{ mysql.python }}
|
||||||
|
|
|
@ -1,26 +1,50 @@
|
||||||
{% from "mysql/map.jinja" import mysql with context %}
|
{% from "mysql/map.jinja" import mysql with context %}
|
||||||
|
|
||||||
{% set mysql_root_password = salt['pillar.get']('mysql:server:root_password', 'somepass') %}
|
{% set os = salt['grains.get']('os', None) %}
|
||||||
|
{% set os_family = salt['grains.get']('os_family', None) %}
|
||||||
|
|
||||||
{% if grains['os'] in ['Ubuntu', 'Debian'] %}
|
{% if 'mysql:server:root_password' in pillar %}
|
||||||
mysql-debconf:
|
{% set mysql_root_password = pillar['mysql:server:root_password'] %}
|
||||||
|
{% else %}
|
||||||
|
mysql_missing_root_password:
|
||||||
|
test.configurable_test_state:
|
||||||
|
- name: mysql_missing_root_password
|
||||||
|
- changes: False
|
||||||
|
- result: False
|
||||||
|
- comment: 'MySQL pillar is missing root password data. A random password will be used.'
|
||||||
|
|
||||||
|
{% set mysql_root_password = salt['test.rand_str](64) %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% if os in ['Ubuntu', 'Debian'] %}
|
||||||
|
mysql_debconf:
|
||||||
debconf.set:
|
debconf.set:
|
||||||
- name: mysql-server
|
- name: mysql-server
|
||||||
- data:
|
- data:
|
||||||
'mysql-server/root_password': {'type': 'password', 'value': '{{ mysql_root_password }}'}
|
'mysql-server/root_password': {'type': 'password', 'value': '{{ mysql_root_password }}'}
|
||||||
'mysql-server/root_password_again': {'type': 'password', 'value': '{{ mysql_root_password }}'}
|
'mysql-server/root_password_again': {'type': 'password', 'value': '{{ mysql_root_password }}'}
|
||||||
'mysql-server/start_on_boot': {'type': 'boolean', 'value': 'true'}
|
'mysql-server/start_on_boot': {'type': 'boolean', 'value': 'true'}
|
||||||
{% elif grains['os'] in ['CentOS'] %}
|
- require_in:
|
||||||
mysql-root-password:
|
- pkg: mysqld
|
||||||
cmd:
|
{% if 'mysql:server:root_password' not in pillar %}
|
||||||
- run
|
- require:
|
||||||
|
- test: mysql_missing_root_password
|
||||||
|
{% endif %}
|
||||||
|
{% elif os == 'CentOS' %}
|
||||||
|
mysql_root_password:
|
||||||
|
cmd.run:
|
||||||
- name: mysqladmin --user root password '{{ mysql_root_password|replace("'", "'\"'\"'") }}'
|
- name: mysqladmin --user root password '{{ mysql_root_password|replace("'", "'\"'\"'") }}'
|
||||||
- unless: mysql --user root --password='{{ mysql_root_password|replace("'", "'\"'\"'") }}' --execute="SELECT 1;"
|
- unless: mysql --user root --password='{{ mysql_root_password|replace("'", "'\"'\"'") }}' --execute="SELECT 1;"
|
||||||
- require:
|
- require:
|
||||||
- service: mysqld
|
- service: mysqld
|
||||||
|
{% if 'mysql:server:root_password' not in pillar %}
|
||||||
|
- test: mysql_missing_root_password
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% for host in ['localhost', grains['fqdn']] %}
|
{% for host in ['localhost', salt['grains.get']('fqdn')] %}
|
||||||
mysql-delete-anonymous-user-{{ host }}:
|
mysql_delete_anonymous_user_{{ host }}:
|
||||||
mysql_user:
|
mysql_user:
|
||||||
- absent
|
- absent
|
||||||
- host: {{ host }}
|
- host: {{ host }}
|
||||||
|
@ -28,47 +52,37 @@ mysql-delete-anonymous-user-{{ host }}:
|
||||||
- connection_pass: {{ mysql_root_password }}
|
- connection_pass: {{ mysql_root_password }}
|
||||||
- require:
|
- require:
|
||||||
- service: mysqld
|
- service: mysqld
|
||||||
- pkg: mysql-python
|
- pkg: mysql_python
|
||||||
{%- if mysql_root_password %}
|
{%- if mysql_root_password %}
|
||||||
- cmd: mysql-root-password
|
- cmd: mysql_root_password
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
mysqld:
|
mysqld:
|
||||||
pkg:
|
pkg.installed:
|
||||||
- installed
|
|
||||||
- name: {{ mysql.server }}
|
- name: {{ mysql.server }}
|
||||||
{% if grains['os'] in ['Ubuntu', 'Debian'] %}
|
{% if os in ['Ubuntu', 'Debian'] %}
|
||||||
- require:
|
- require:
|
||||||
- debconf: mysql-debconf
|
- debconf: mysql_debconf
|
||||||
{% endif %}
|
{% endif %}
|
||||||
service:
|
service.running:
|
||||||
- running
|
|
||||||
- name: {{ mysql.service }}
|
- name: {{ mysql.service }}
|
||||||
- enable: True
|
- enable: True
|
||||||
- watch:
|
- watch:
|
||||||
- pkg: mysqld
|
- pkg: mysqld
|
||||||
|
|
||||||
{% if grains['os'] in ['Ubuntu', 'Debian', 'Gentoo', 'CentOS'] %}
|
mysql_config:
|
||||||
my.cnf:
|
|
||||||
file.managed:
|
file.managed:
|
||||||
- name: {{ mysql.config }}
|
- name: {{ mysql.config }}
|
||||||
- source: salt://mysql/files/{{ grains['os'] }}-my.cnf
|
- template: jinja
|
||||||
|
- watch_in:
|
||||||
|
- service: mysqld
|
||||||
|
{% if os in ['Ubuntu', 'Debian', 'Gentoo', 'CentOS'] %}
|
||||||
|
- source: salt://mysql/files/{{ os }}-my.cnf
|
||||||
- user: root
|
- user: root
|
||||||
- group: root
|
- group: root
|
||||||
- mode: 644
|
- mode: 644
|
||||||
- template: jinja
|
{% elif os == 'FreeBSD' %}
|
||||||
- watch_in:
|
|
||||||
- service: mysqld
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{% if grains['os'] in 'FreeBSD' %}
|
|
||||||
my.cnf:
|
|
||||||
file.managed:
|
|
||||||
- name: {{ mysql.config }}
|
|
||||||
- source: salt://mysql/files/my-{{ mysql.mysql_size }}.cnf
|
- source: salt://mysql/files/my-{{ mysql.mysql_size }}.cnf
|
||||||
- template: jinja
|
{% endif %}
|
||||||
- watch_in:
|
|
||||||
- service: mysqld
|
|
||||||
{% endif %}
|
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
{% from "mysql/map.jinja" import mysql with context %}
|
{% from "mysql/map.jinja" import mysql with context %}
|
||||||
|
|
||||||
|
{% set user_states = [] %}
|
||||||
|
|
||||||
include:
|
include:
|
||||||
- mysql.python
|
- mysql.python
|
||||||
|
|
||||||
{% for user in salt['pillar.get']('mysql:user', []) %}
|
{% for user in salt['pillar.get']('mysql:user', []) %}
|
||||||
mysql_user_{{ user['name'] }}:
|
{% set state_id = 'mysql_user_' ~ loop.index0 %}
|
||||||
|
{{ state_id }}:
|
||||||
mysql_user.present:
|
mysql_user.present:
|
||||||
- name: {{ user['name'] }}
|
- name: {{ user['name'] }}
|
||||||
- host: {{ user['host'] }}
|
- host: {{ user['host'] }}
|
||||||
|
@ -19,10 +22,9 @@ mysql_user_{{ user['name'] }}:
|
||||||
- connection_charset: utf8
|
- connection_charset: utf8
|
||||||
|
|
||||||
{% for db in user['databases'] %}
|
{% for db in user['databases'] %}
|
||||||
{% set name = user['name'] ~ '_' ~ db['database'] %}
|
{{ state_id ~ '_' ~ loop.index0 }}:
|
||||||
mysql_user_{{ name }}:
|
|
||||||
mysql_grants.present:
|
mysql_grants.present:
|
||||||
- name: {{ name }}
|
- name: {{ user['name'] ~ '_' ~ db['database'] }}
|
||||||
- grant: {{db['grants']|join(",")}}
|
- grant: {{db['grants']|join(",")}}
|
||||||
- database: {{ db['database'] }}.*
|
- database: {{ db['database'] }}.*
|
||||||
- user: {{ user['name'] }}
|
- user: {{ user['name'] }}
|
||||||
|
@ -35,6 +37,7 @@ mysql_user_{{ name }}:
|
||||||
- mysql_user: {{ user['name'] }}
|
- mysql_user: {{ user['name'] }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
{% do user_states.append(state_id) %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue