forked from saltstack-formulas/mysql-formula
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.sls
131 lines (120 loc) · 4.06 KB
/
server.sls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# vim: set ft=yaml:
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')) %}
{% set os = salt['grains.get']('os', None) %}
{% set os_family = salt['grains.get']('os_family', None) %}
{% set mysql_root_user = salt['pillar.get']('mysql:server:root_user', 'root') %}
{% set mysql_root_password = salt['pillar.get']('mysql:server:root_password', salt['grains.get']('server_id')) %}
{% set mysql_host = salt['pillar.get']('mysql:server:host', 'localhost') %}
{% set mysql_salt_user = salt['pillar.get']('mysql:salt_user:salt_user_name', mysql_root_user) %}
{% set mysql_salt_password = salt['pillar.get']('mysql:salt_user:salt_user_password', mysql_root_password) %}
{% if mysql_root_password %}
{% if os_family == 'Debian' %}
mysql_debconf_utils:
pkg.installed:
- name: {{ mysql.debconf_utils }}
mysql_debconf:
debconf.set:
- name: {{ mysql.debconf_package }}
- data:
'{{ mysql.debconf }}/root_password': {'type': 'password', 'value': '{{ mysql_root_password }}'}
'{{ mysql.debconf }}/root_password_again': {'type': 'password', 'value': '{{ mysql_root_password }}'}
'{{ mysql.debconf }}/start_on_boot': {'type': 'boolean', 'value': 'true'}
- require_in:
- pkg: {{ mysql.server }}
- require:
- pkg: mysql_debconf_utils
{% elif os_family == 'RedHat' or 'Suse' %}
mysql_root_password:
cmd.run:
- name: mysqladmin --user {{ mysql_root_user }} password '{{ mysql_root_password|replace("'", "'\"'\"'") }}'
- unless: mysql --user {{ mysql_root_user }} --password='{{ mysql_root_password|replace("'", "'\"'\"'") }}' --execute="SELECT 1;"
- require:
- service: mysqld
{% for host in ['localhost', 'localhost.localdomain', salt['grains.get']('fqdn')] %}
mysql_delete_anonymous_user_{{ host }}:
mysql_user:
- absent
- host: {{ host or "''" }}
- name: ''
- connection_host: '{{ mysql_host }}'
- connection_user: '{{ mysql_salt_user }}'
{% if mysql_salt_password %}
- connection_pass: '{{ mysql_salt_password }}'
{% endif %}
- connection_charset: utf8
- require:
- service: mysqld
- pkg: mysql_python
{%- if (mysql_salt_user == mysql_root_user) and mysql_root_password %}
- cmd: mysql_root_password
{%- endif %}
{% endfor %}
{% endif %}
{% endif %}
{% if os_family == 'Arch' %}
# on arch linux: inital mysql datadirectory is not created
mysql_install_datadir:
cmd.run:
- name: mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
- user: root
- creates: /var/lib/mysql/mysql/user.frm
- require:
- pkg: mysqld
- file: mysql_config
- require_in:
- service: mysqld
{% endif %}
mysqld-packages:
pkg.installed:
- name: {{ mysql.server }}
{% if os_family == 'Debian' and mysql_root_password %}
- require:
- debconf: mysql_debconf
{% endif %}
mysqld:
service.running:
- name: {{ mysql.service }}
- enable: True
- require:
- pkg: {{ mysql.server }}
- watch:
- pkg: {{ mysql.server }}
- 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
# so, make it empty
mysql_additional_config:
file.managed:
- name: /usr/my.cnf
- source: salt://mysql/files/usr-my.cnf
- create: False
- watch_in:
- service: mysqld
# This create a passwordless access for root
mysql_root_my_cnf:
file.managed:
- name: /root/.my.cnf
- source: salt://mysql/files/root-my.cnf
- template: jinja
- user: root
- group: root
- mode: 600
- create: True
# This use above config file to store mysql's root password for salt
mysql_minion_root_my_cnf:
file.managed:
- name: /etc/salt/minion.d/55-mysql-cnf.conf
# use quote for the content
- contents:
- "mysql.default_file: '/root/.my.cnf'"
- user: root
- group: root
- mode: 600
- create: True