Saltstack Official Galera Formula
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

135 行
4.5KB

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