Saltstack Official Galera Formula
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

145 lines
4.8KB

  1. {%- if pillar.get('mysql', {}).server is defined %}
  2. {%- from "mysql/map.jinja" import mysql_connection_args as connection with context %}
  3. {%- set server = pillar.mysql.server %}
  4. {%- for database_name, database in server.get('database', {}).iteritems() %}
  5. mysql_database_{{ database_name }}:
  6. mysql_database.present:
  7. - name: {{ database_name }}
  8. - character_set: {{ database.get('encoding', 'utf8') }}
  9. #- connection_user: {{ connection.user }}
  10. #- connection_pass: {{ connection.password }}
  11. #- connection_charset: {{ connection.charset }}
  12. {%- if grains.get('noservices') %}
  13. - onlyif: /bin/false
  14. {%- endif %}
  15. {%- for user in database.get('users', {}) %}
  16. mysql_user_{{ user.name }}_{{ database_name }}_{{ user.host }}:
  17. mysql_user.present:
  18. - host: '{{ user.host }}'
  19. - name: '{{ user.name }}'
  20. {%- if user.password is defined %}
  21. - password: {{ user.password }}
  22. {%- else %}
  23. - allow_passwordless: true
  24. {%- endif %}
  25. #- connection_user: {{ connection.user }}
  26. #- connection_pass: {{ connection.password }}
  27. #- connection_charset: {{ connection.charset }}
  28. {%- if grains.get('noservices') %}
  29. - onlyif: /bin/false
  30. {%- endif %}
  31. mysql_grants_{{ user.name }}_{{ database_name }}_{{ user.host }}:
  32. mysql_grants.present:
  33. - grant: {{ user.rights }}
  34. - database: {{ user.get('database', database_name + '.*') }}
  35. - user: '{{ user.name }}'
  36. - host: '{{ user.host }}'
  37. - ssl_option: {{ user.get('ssl_option', False) }}
  38. #- connection_user: {{ connection.user }}
  39. #- connection_pass: {{ connection.password }}
  40. #- connection_charset: {{ connection.charset }}
  41. - require:
  42. - mysql_user: mysql_user_{{ user.name }}_{{ database_name }}_{{ user.host }}
  43. - mysql_database: mysql_database_{{ database_name }}
  44. {%- if grains.get('noservices') %}
  45. - onlyif: /bin/false
  46. {%- endif %}
  47. {%- endfor %}
  48. {%- if database.initial_data is defined %}
  49. /root/mysql/scripts/restore_{{ database_name }}.sh:
  50. file.managed:
  51. - source: salt://mysql/conf/restore.sh
  52. - mode: 770
  53. - template: jinja
  54. - defaults:
  55. database_name: {{ database_name }}
  56. database: {{ database }}
  57. - require:
  58. - file: mysql_dirs
  59. - mysql_database: mysql_database_{{ database_name }}
  60. restore_mysql_database_{{ database_name }}:
  61. cmd.run:
  62. - name: /root/mysql/scripts/restore_{{ database_name }}.sh
  63. - unless: "[ -f /root/mysql/flags/{{ database_name }}-installed ]"
  64. - cwd: /root
  65. - require:
  66. - file: /root/mysql/scripts/restore_{{ database_name }}.sh
  67. {%- endif %}
  68. {%- endfor %}
  69. {%- for user in server.get('users', []) %}
  70. {%- for host in user.get('hosts', user.get('host', 'localhost'))|sequence %}
  71. mysql_user_{{ user.name }}_{{ host }}:
  72. mysql_user.present:
  73. - host: '{{ host }}'
  74. - name: '{{ user.name }}'
  75. {%- if user['password_hash'] is defined %}
  76. - password_hash: '{{ user.password_hash }}'
  77. {%- elif user['password'] is defined and user['password'] != None %}
  78. - password: '{{ user.password }}'
  79. {%- else %}
  80. - allow_passwordless: True
  81. {%- endif %}
  82. #- connection_user: {{ connection.user }}
  83. #- connection_pass: {{ connection.password }}
  84. #- connection_charset: {{ connection.charset }}
  85. {%- if grains.get('noservices') %}
  86. - onlyif: /bin/false
  87. {%- endif %}
  88. {%- if 'grants' in user %}
  89. mysql_user_{{ user.name }}_{{ host }}_grants:
  90. mysql_grants.present:
  91. - name: {{ user.name }}
  92. - grant: {{ user['grants']|sequence|join(",") }}
  93. - database: '{{ user.get('database','*.*') }}'
  94. - grant_option: {{ user['grant_option'] | default(False) }}
  95. - user: {{ user.name }}
  96. - host: '{{ host }}'
  97. - ssl_option: {{ user.get('ssl_option', False) }}
  98. #- connection_user: {{ connection.user }}
  99. #- connection_pass: {{ connection.password }}
  100. #- connection_charset: {{ connection.charset }}
  101. - require:
  102. - mysql_user_{{ user.name }}_{{ host }}
  103. {%- if grains.get('noservices') %}
  104. - onlyif: /bin/false
  105. {%- endif %}
  106. {%- endif %}
  107. {%- if 'databases' in user %}
  108. {%- for db in user['databases'] %}
  109. mysql_user_{{ user.name }}_{{ host }}_grants_db_{{ db.database }}_{{ loop.index0 }}:
  110. mysql_grants.present:
  111. - name: {{ user.name ~ '_' ~ db['database'] ~ '_' ~ db['table'] | default('all') }}
  112. - grant: {{ db['grants']|sequence|join(",") }}
  113. - database: '{{ db['database'] }}.{{ db['table'] | default('*') }}'
  114. - grant_option: {{ db['grant_option'] | default(False) }}
  115. - user: {{ user.name }}
  116. - host: '{{ host }}'
  117. - ssl_option: {{ db.get('ssl_option', False) }}
  118. #- connection_user: {{ connection.user }}
  119. #- connection_pass: {{ connection.password }}
  120. #- connection_charset: {{ connection.charset }}
  121. - require:
  122. - mysql_user_{{ user.name }}_{{ host }}
  123. # the following line is not mandatory as database might not be managed by salt formula
  124. #- mysql_database_{{ db.database }}
  125. {%- if grains.get('noservices') %}
  126. - onlyif: /bin/false
  127. {%- endif %}
  128. {%- endfor %}
  129. {%- endif %}
  130. {%- endfor %}
  131. {%- endfor %}
  132. {%- endif %}