Saltstack Official Galera Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.2KB

  1. {%- if pillar.get('mysql', {}).server is defined %}
  2. {%- set server = pillar.mysql.server %}
  3. {%- for database_name, database in server.get('database', {}).iteritems() %}
  4. mysql_database_{{ database_name }}:
  5. mysql_database.present:
  6. - name: {{ database_name }}
  7. {%- for user in database.users %}
  8. mysql_user_{{ user.name }}_{{ database_name }}_{{ user.host }}:
  9. mysql_user.present:
  10. - host: '{{ user.host }}'
  11. - name: '{{ user.name }}'
  12. - password: {{ user.password }}
  13. mysql_grants_{{ user.name }}_{{ database_name }}_{{ user.host }}:
  14. mysql_grants.present:
  15. - grant: {{ user.rights }}
  16. - database: '{{ database_name }}.*'
  17. - user: '{{ user.name }}'
  18. - host: '{{ user.host }}'
  19. - require:
  20. - mysql_user: mysql_user_{{ user.name }}_{{ database_name }}_{{ user.host }}
  21. - mysql_database: mysql_database_{{ database_name }}
  22. {%- endfor %}
  23. {%- if database.initial_data is defined %}
  24. /root/mysql/scripts/restore_{{ database_name }}.sh:
  25. file.managed:
  26. - source: salt://mysql/conf/restore.sh
  27. - mode: 770
  28. - template: jinja
  29. - defaults:
  30. database_name: {{ database_name }}
  31. database: {{ database }}
  32. - require:
  33. - file: mysql_dirs
  34. - mysql_database: mysql_database_{{ database_name }}
  35. restore_mysql_database_{{ database_name }}:
  36. cmd.run:
  37. - name: /root/mysql/scripts/restore_{{ database_name }}.sh
  38. - unless: "[ -f /root/mysql/flags/{{ database_name }}-installed ]"
  39. - cwd: /root
  40. - require:
  41. - file: /root/mysql/scripts/restore_{{ database_name }}.sh
  42. {%- endif %}
  43. {%- endfor %}
  44. {%- if not grains.get('noservices', False) %}
  45. {%- for user in server.get('users', []) %}
  46. {%- set user_hosts = user.get('hosts', user.get('host', 'localhost'))|sequence %}
  47. {%- for host in user_hosts %}
  48. mysql_user_{{ user.name }}_{{ host }}:
  49. mysql_user.present:
  50. - host: '{{ user.host }}'
  51. - name: '{{ user.name }}'
  52. {%- if user['password_hash'] is defined %}
  53. - password_hash: '{{ user.password_hash }}'
  54. {%- elif user['password'] is defined and user['password'] != None %}
  55. - password: '{{ user.password }}'
  56. {%- else %}
  57. - allow_passwordless: True
  58. {%- endif %}
  59. - connection_charset: utf8
  60. {%- if 'grants' in user %}
  61. mysql_user_{{ user.name }}_{{ host }}_grants:
  62. mysql_grants.present:
  63. - name: {{ user.name }}
  64. - grant: {{ user['grants']|sequence|join(",") }}
  65. - database: '*.*'
  66. - grant_option: {{ user['grant_option'] | default(False) }}
  67. - user: {{ user.name }}
  68. - host: '{{ host }}'
  69. - connection_charset: utf8
  70. - require:
  71. - mysql_user_{{ user.name }}_{{ host }}
  72. {%- endif %}
  73. {%- if 'databases' in user %}
  74. {% for db in user['databases'] %}
  75. mysql_user_{{ user.name }}_{{ host }}_grants_db_{{ db }} ~ '_' ~ loop.index0:
  76. mysql_grants.present:
  77. - name: {{ user.name ~ '_' ~ db['database'] ~ '_' ~ db['table'] | default('all') }}
  78. - grant: {{db['grants']|sequence|join(",")}}
  79. - database: '{{ db['database'] }}.{{ db['table'] | default('*') }}'
  80. - grant_option: {{ db['grant_option'] | default(False) }}
  81. - user: {{ user.name }}
  82. - host: '{{ host }}'
  83. - connection_charset: utf8
  84. - require:
  85. - mysql_user_{{ user.name }}_{{ host }}
  86. - mysql_database_{{ db }}
  87. {%- endfor %}
  88. {%- endif %}
  89. {%- endfor %}
  90. {%- endfor %}
  91. {%- endif %}
  92. {%- endif %}