|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- {%- if pillar.get('mysql', {}).server is defined %}
- {%- from "mysql/map.jinja" import mysql_connection_args as connection with context %}
- {%- set server = pillar.mysql.server %}
-
- {%- for database_name, database in server.get('database', {}).iteritems() %}
-
- {%- if not grains.get('noservices', False) %}
- mysql_database_{{ database_name }}:
- mysql_database.present:
- - name: {{ database_name }}
- - character_set: {{ database.get('encoding', 'utf8') }}
- #- connection_user: {{ connection.user }}
- #- connection_pass: {{ connection.password }}
- #- connection_charset: {{ connection.charset }}
- {%- endif %}
-
- {%- for user in database.users %}
- {%- if not grains.get('noservices', False) %}
- mysql_user_{{ user.name }}_{{ database_name }}_{{ user.host }}:
- mysql_user.present:
- - host: '{{ user.host }}'
- - name: '{{ user.name }}'
- {%- if user.password is defined %}
- - password: {{ user.password }}
- {%- else %}
- - allow_passwordless: true
- {%- endif %}
- #- connection_user: {{ connection.user }}
- #- connection_pass: {{ connection.password }}
- #- connection_charset: {{ connection.charset }}
-
- mysql_grants_{{ user.name }}_{{ database_name }}_{{ user.host }}:
- mysql_grants.present:
- - grant: {{ user.rights }}
- - database: '{{ database_name }}.*'
- - user: '{{ user.name }}'
- - host: '{{ user.host }}'
- #- connection_user: {{ connection.user }}
- #- connection_pass: {{ connection.password }}
- #- connection_charset: {{ connection.charset }}
- - require:
- - mysql_user: mysql_user_{{ user.name }}_{{ database_name }}_{{ user.host }}
- - mysql_database: mysql_database_{{ database_name }}
- {%- endif %}
- {%- endfor %}
-
- {%- if database.initial_data is defined %}
- /root/mysql/scripts/restore_{{ database_name }}.sh:
- file.managed:
- - source: salt://mysql/conf/restore.sh
- - mode: 770
- - template: jinja
- - defaults:
- database_name: {{ database_name }}
- database: {{ database }}
- - require:
- - file: mysql_dirs
- - mysql_database: mysql_database_{{ database_name }}
-
- restore_mysql_database_{{ database_name }}:
- cmd.run:
- - name: /root/mysql/scripts/restore_{{ database_name }}.sh
- - unless: "[ -f /root/mysql/flags/{{ database_name }}-installed ]"
- - cwd: /root
- - require:
- - file: /root/mysql/scripts/restore_{{ database_name }}.sh
- {%- endif %}
-
- {%- endfor %}
-
- {%- for user in server.get('users', []) %}
- {%- for host in user.get('hosts', user.get('host', 'localhost'))|sequence %}
- mysql_user_{{ user.name }}_{{ host }}:
- mysql_user.present:
- - host: '{{ host }}'
- - name: '{{ user.name }}'
- {%- if user['password_hash'] is defined %}
- - password_hash: '{{ user.password_hash }}'
- {%- elif user['password'] is defined and user['password'] != None %}
- - password: '{{ user.password }}'
- {%- else %}
- - allow_passwordless: True
- {%- endif %}
- #- connection_user: {{ connection.user }}
- #- connection_pass: {{ connection.password }}
- #- connection_charset: {{ connection.charset }}
- {%- if grains.get('noservices') %}
- - onlyif: /bin/false
- {%- endif %}
-
- {%- if 'grants' in user %}
- mysql_user_{{ user.name }}_{{ host }}_grants:
- mysql_grants.present:
- - name: {{ user.name }}
- - grant: {{ user['grants']|sequence|join(",") }}
- - database: '{{ user.get('database','*.*') }}'
- - grant_option: {{ user['grant_option'] | default(False) }}
- - user: {{ user.name }}
- - host: '{{ host }}'
- #- connection_user: {{ connection.user }}
- #- connection_pass: {{ connection.password }}
- #- connection_charset: {{ connection.charset }}
- - require:
- - mysql_user_{{ user.name }}_{{ host }}
- {%- if grains.get('noservices') %}
- - onlyif: /bin/false
- {%- endif %}
- {%- endif %}
-
- {%- if 'databases' in user %}
- {%- for db in user['databases'] %}
- mysql_user_{{ user.name }}_{{ host }}_grants_db_{{ db.database }}_{{ loop.index0 }}:
- mysql_grants.present:
- - name: {{ user.name ~ '_' ~ db['database'] ~ '_' ~ db['table'] | default('all') }}
- - grant: {{ db['grants']|sequence|join(",") }}
- - database: '{{ db['database'] }}.{{ db['table'] | default('*') }}'
- - grant_option: {{ db['grant_option'] | default(False) }}
- - user: {{ user.name }}
- - host: '{{ host }}'
- #- connection_user: {{ connection.user }}
- #- connection_pass: {{ connection.password }}
- #- connection_charset: {{ connection.charset }}
- - require:
- - mysql_user_{{ user.name }}_{{ host }}
- - mysql_database_{{ db.database }}
- {%- if grains.get('noservices') %}
- - onlyif: /bin/false
- {%- endif %}
- {%- endfor %}
- {%- endif %}
-
- {%- endfor %}
- {%- endfor %}
-
- {%- endif %}
|