From 16b105786b82edab6664dfd3381c66733d509c92 Mon Sep 17 00:00:00 2001 From: Emilien Mantel Date: Tue, 11 Aug 2015 10:22:58 +0200 Subject: [PATCH] Replication OK (transfert from another git repo) --- Vagrantfile | 67 +++++++++-------- defaults/main.yml | 45 ++++++++++++ tasks/main.yml | 18 ++++- tasks/replication_master.yml | 5 ++ tasks/replication_slave.yml | 38 ++++++++++ tasks/slave/debiancnf.yml | 21 ++++++ tasks/slave/import_data.yml | 72 +++++++++++++++++++ tasks/slave/mariadb_gtid.yml | 14 ++++ tasks/slave/replication.yml | 23 ++++++ templates/etc/mysql/conf.d/50-master.cnf.j2 | 15 ++++ templates/etc/mysql/conf.d/51-slave.cnf.j2 | 26 +++++++ templates/etc/mysql/my.cnf.j2 | 24 ++++--- tests/README.md | 49 +++++++++++++ tests/gen.pl | 64 +++++++++++++++++ tests/group_vars/default | 1 + tests/group_vars/mariadb | 1 + tests/group_vars/master | 21 ++++++ tests/group_vars/mysql | 1 + tests/group_vars/percona | 1 + tests/group_vars/slave | 9 +++ tests/group_vars/upstream | 1 + tests/host_vars/jessie-default-mariadb-slave | 1 + tests/host_vars/jessie-default-mysql-slave | 1 + tests/host_vars/jessie-upstream-mariadb-slave | 1 + tests/host_vars/jessie-upstream-percona-slave | 1 + tests/host_vars/wheezy-default-mysql-slave | 1 + tests/host_vars/wheezy-upstream-mariadb-slave | 1 + tests/host_vars/wheezy-upstream-percona-slave | 1 + tests/import1.sql | 12 ++++ tests/import2.sql | 6 ++ tests/test.yml | 50 +++++++------ 31 files changed, 527 insertions(+), 64 deletions(-) create mode 100644 tasks/replication_master.yml create mode 100644 tasks/replication_slave.yml create mode 100644 tasks/slave/debiancnf.yml create mode 100644 tasks/slave/import_data.yml create mode 100644 tasks/slave/mariadb_gtid.yml create mode 100644 tasks/slave/replication.yml create mode 100644 templates/etc/mysql/conf.d/50-master.cnf.j2 create mode 100644 templates/etc/mysql/conf.d/51-slave.cnf.j2 create mode 100644 tests/README.md create mode 100755 tests/gen.pl create mode 100644 tests/group_vars/default create mode 100644 tests/group_vars/mariadb create mode 100644 tests/group_vars/master create mode 100644 tests/group_vars/mysql create mode 100644 tests/group_vars/percona create mode 100644 tests/group_vars/slave create mode 100644 tests/group_vars/upstream create mode 100644 tests/host_vars/jessie-default-mariadb-slave create mode 100644 tests/host_vars/jessie-default-mysql-slave create mode 100644 tests/host_vars/jessie-upstream-mariadb-slave create mode 100644 tests/host_vars/jessie-upstream-percona-slave create mode 100644 tests/host_vars/wheezy-default-mysql-slave create mode 100644 tests/host_vars/wheezy-upstream-mariadb-slave create mode 100644 tests/host_vars/wheezy-upstream-percona-slave create mode 100644 tests/import1.sql create mode 100644 tests/import2.sql diff --git a/Vagrantfile b/Vagrantfile index ca3ccba..e384d2b 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -5,37 +5,44 @@ Vagrant.configure("2") do |config| - vms = [ - [ "wheezy-debian-mysql", "deb/wheezy-amd64" , "192.168.33.40" ], - [ "wheezy-upstream-mariadb", "deb/wheezy-amd64" , "192.168.33.42" ], - [ "wheezy-upstream-percona", "deb/wheezy-amd64" , "192.168.33.43" ], - [ "jessie-debian-mysql", "deb/jessie-amd64", "192.168.33.50" ], - [ "jessie-debian-mariadb", "deb/jessie-amd64", "192.168.33.51" ], - [ "jessie-upstream-mariadb", "deb/jessie-amd64", "192.168.33.52" ], - [ "jessie-upstream-percona", "deb/jessie-amd64", "192.168.33.53" ] - ] + vms = [ + ["wheezy-default-mysql-master", "deb/wheezy-amd64", "192.168.200.10", ["wheezy","default","mysql","master"]], + ["wheezy-default-mysql-slave", "deb/wheezy-amd64", "192.168.200.11", ["wheezy","default","mysql","slave"]], + ["wheezy-upstream-mariadb-master", "deb/wheezy-amd64", "192.168.200.12", ["wheezy","upstream","mariadb","master"]], + ["wheezy-upstream-mariadb-slave", "deb/wheezy-amd64", "192.168.200.13", ["wheezy","upstream","mariadb","slave"]], + ["wheezy-upstream-percona-master", "deb/wheezy-amd64", "192.168.200.14", ["wheezy","upstream","percona","master"]], + ["wheezy-upstream-percona-slave", "deb/wheezy-amd64", "192.168.200.15", ["wheezy","upstream","percona","slave"]], + ["jessie-default-mysql-master", "deb/jessie-amd64", "192.168.200.16", ["jessie","default","mysql","master"]], + ["jessie-default-mysql-slave", "deb/jessie-amd64", "192.168.200.17", ["jessie","default","mysql","slave"]], + ["jessie-upstream-mariadb-master", "deb/jessie-amd64", "192.168.200.18", ["jessie","upstream","mariadb","master"]], + ["jessie-upstream-mariadb-slave", "deb/jessie-amd64", "192.168.200.19", ["jessie","upstream","mariadb","slave"]], + ["jessie-upstream-percona-master", "deb/jessie-amd64", "192.168.200.20", ["jessie","upstream","percona","master"]], + ["jessie-upstream-percona-slave", "deb/jessie-amd64", "192.168.200.21", ["jessie","upstream","percona","slave"]], + ["jessie-default-mariadb-master", "deb/jessie-amd64", "192.168.200.22", ["jessie","default","mariadb","master"]], + ["jessie-default-mariadb-slave", "deb/jessie-amd64", "192.168.200.23", ["jessie","default","mariadb","slave"]] + ] - config.vm.provider "virtualbox" do |v| - v.cpus = 1 - v.memory = 256 - end + config.vm.provider "virtualbox" do |v| + v.cpus = 1 + v.memory = 256 + end - vms.each do |vm| - config.vm.define vm[0] do |m| - m.vm.box = vm[1] - m.vm.network "private_network", ip: vm[2] + vms.each do |vm| + config.vm.define vm[0] do |m| + m.vm.box = vm[1] + m.vm.network "private_network", ip: vm[2] - m.vm.provision "ansible" do |ansible| - ansible.playbook = "tests/test.yml" - ansible.groups = { - "debian_mysql" => ["wheezy-debian-mysql", "jessie-debian-mysql"], - "upstream_mariadb" => ["wheezy-upstream-mariadb", "jessie-upstream-mariadb"], - "upstream_percona" => ["wheezy-upstream-percona", "jessie-upstream-percona"], - "debian_mariadb" => ["jessie-debian-mariadb"] - } - ansible.verbose = 'vv' - ansible.sudo = true - end - end - end + m.vm.provision "ansible" do |ansible| + ansible.playbook = "tests/test.yml" + ansible.groups = { + vm[3][0] => vm[0], + vm[3][1] => vm[0], + vm[3][2] => vm[0], + vm[3][3] => vm[0], + } + ansible.verbose = 'vv' + ansible.sudo = true + end + end + end end diff --git a/defaults/main.yml b/defaults/main.yml index 6f686d2..8e5b321 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -70,6 +70,51 @@ mysql_extra_configuration: [] # - key: 'innodb_awsome_feature' # value: 1 + +# ------------------------------------- +# Replication +# ------------------------------------- +mysql_replication_master: false +mysql_replication_slave: false +# This formula don't work with vagrant! All boxes have same default ip! +mysql_server_id: "{{ ansible_default_ipv4.address | ipaddr('int') }}" + +mysql_replication_user: [] +mysql_replication_user: 'repl' +mysql_replication_password: 'change_me_NOW' +mysql_replication_host: '192.168.0.1' +mysql_replication_port: '3306' + +mysql_backup_dir: '/var/backups/tmp-mysql' + +# Master +mysql_max_binlog_size: "100M" +mysql_expire_logs_days: "10" + +# +# Slave +# +mysql_slave_readonly: true +# Import data from +mysql_slave_import_data: true +# host used to create a backup (can be the master or another slave) - default: use the master +mysql_slave_import_from: "{{ mysql_replication_host }}" +# Delete dump after slave configuration (set false if you have many slaves to install, it avoids to create many dumps) +mysql_slave_import_flush_dump: true +# Slave uses mysql database from master? +mysql_slave_replicate_mysqldb: true +# Don't replicate these DBs/tables +mysql_slave_ignore_db: [] +mysql_slave_ignore_table: [] +# SSH user used to copy data +mysql_backup_user: "{{ ansible_user_id }}" # TODO: change var name +## If data is already imported, you can specify position +mysql_master_log_file: null +mysql_master_log_pos: null +# If true, reconfigure slave +mysql_slave_force_setup: false + + # ------------------------------------- # Database / Users # ------------------------------------- diff --git a/tasks/main.yml b/tasks/main.yml index 026dfbc..0921a9a 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -11,9 +11,16 @@ - name: INCLUDE | Install include: install.yml +- name: SERVICE | Ensure daemon is started + service: name=mysql state=started + - name: TEMPLATE | Deploy configuration template: src=etc/mysql/my.cnf.j2 dest=/etc/mysql/my.cnf - notify: restart mysql + register: config + +- name: SERVICE | Restart now (prevent bugs) + service: name=mysql state=restarted + when: config.changed - name: TEMPLATE | Deploy extra configuration template: src=etc/mysql/conf.d/10-extra.cnf.j2 dest=/etc/mysql/conf.d/10-extra.cnf @@ -22,8 +29,13 @@ - name: TEMPLATE Create .my.cnf for root template: src=root/my.cnf dest=/root/.my.cnf owner=root group=root mode=0600 backup=yes -- name: SERVICE | Ensure daemon is started - service: name=mysql state=started +- name: INCLUDE | Replication Master + include: replication_master.yml + when: mysql_replication_master + +- name: INCLUDE | Replication slave + include: replication_slave.yml + when: mysql_replication_slave - name: INCLUDE | Secure install include: 'secure.yml' diff --git a/tasks/replication_master.yml b/tasks/replication_master.yml new file mode 100644 index 0000000..f9c1b95 --- /dev/null +++ b/tasks/replication_master.yml @@ -0,0 +1,5 @@ +--- + +- name: TEMPLATE | Deploy master configuration + template: src=etc/mysql/conf.d/50-master.cnf.j2 dest=/etc/mysql/conf.d/50-master.cnf + notify: restart mysql diff --git a/tasks/replication_slave.yml b/tasks/replication_slave.yml new file mode 100644 index 0000000..128bef6 --- /dev/null +++ b/tasks/replication_slave.yml @@ -0,0 +1,38 @@ +--- + +- name: MYSQL_VARIABLES | Set read only + mysql_variables: variable=read_only value=ON + when: mysql_slave_readonly + +- name: TEMPLATE | Deploy slave configuration + template: src=etc/mysql/conf.d/51-slave.cnf.j2 dest=/etc/mysql/conf.d/51-slave.cnf + notify: restart mysql + +- name: MYSQL_REPLICATION | Get slave status + mysql_replication: mode=getslave + ignore_errors: yes + register: slave_status + +- name: INCLUDE | Import data + include: slave/import_data.yml + when: slave_status.failed is defined and mysql_slave_import_data + +- name: INCLUDE | Configure replication + include: slave/replication.yml + when: slave_status.failed is defined or mysql_slave_force_setup + +- name: INCLUDE | Transfert /etc/mysql/debian.cnf from master + include: slave/debiancnf.yml + when: mysql_slave_replicate_mysqldb + +- name: MYSQL_REPLICATION | Get slave status + mysql_replication: mode=getslave + ignore_errors: yes + register: slave_status + +- name: Configure MariaDB GTID + include: slave/mariadb_gtid.yml + when: > + mysql_vendor == 'mariadb' and + slave_status.Using_Gtid == 'No' and + mysql_mysql_gtid diff --git a/tasks/slave/debiancnf.yml b/tasks/slave/debiancnf.yml new file mode 100644 index 0000000..7929251 --- /dev/null +++ b/tasks/slave/debiancnf.yml @@ -0,0 +1,21 @@ +--- + +- name: FETCH | Get /etc/mysql/debian.cnf on master + fetch: > + src=/etc/mysql/debian.cnf + dest=/tmp/{{ mysql_slave_import_from }}/debian.cnf + flat=yes + changed_when: false + delegate_to: "{{ mysql_slave_import_from }}" + +- name: LOCAL_ACTION FILE | Secure fetched file + local_action: file path=/tmp/{{ mysql_slave_import_from }}/debian.cnf mode=0600 + become: no + +- name: COPY | Fetched file to /etc/mysql/debian.cnf + copy: > + src=/tmp/{{ mysql_slave_import_from }}/debian.cnf + dest=/etc/mysql/debian.cnf + owner=root group=root mode=0600 + notify: restart mysql + diff --git a/tasks/slave/import_data.yml b/tasks/slave/import_data.yml new file mode 100644 index 0000000..4013e75 --- /dev/null +++ b/tasks/slave/import_data.yml @@ -0,0 +1,72 @@ +--- + +# Doc: https://www.percona.com/doc/percona-xtrabackup/2.1/howtos/recipes_ibkx_gtid.html + +- name: WAIT_FOR | source data (prevent rsync bug) - TODO find another hack + wait_for: host={{ mysql_slave_import_from }} port=22 + +- name: COMMAND | Prepare backup another server + command: innobackupex --no-timestamp {{ mysql_backup_dir }} creates={{ mysql_backup_dir }} + delegate_to: "{{ mysql_slave_import_from }}" + register: backup + +- name: SHELL | Dump + shell: innobackupex --apply-log {{ mysql_backup_dir }} + delegate_to: "{{ mysql_slave_import_from }}" + when: backup.changed + +- name: FILE | Remove mysql db from backup + file: path={{ mysql_backup_dir }}/mysql state=absent + delegate_to: "{{ mysql_slave_import_from }}" + when: backup.changed and not mysql_slave_replicate_mysqldb + +- name: MYSQL_VARIABLES | Get datadir + mysql_variables: variable=datadir + register: datadir + +- name: SET_FACT | related to mysql datadir + set_fact: + mysql_mysql_datadir: "{{ datadir.msg[0][1] }}" + mysql_binlog_info: "{{ datadir.msg[0][1] }}/xtrabackup_binlog_info" + +- name: SERVICE | Stop MySQL before importing data + service: name=mysql state=stopped + +- name: COMMAND | Sync backup to slave - TODO remove vagrant as static user (see why mysql_backup_user is not working) + shell: "sudo -E rsync --rsync-path='sudo rsync' -a -e 'ssh -o StrictHostKeyChecking=no' vagrant@{{ mysql_slave_import_from }}:{{ mysql_backup_dir }}/ {{ mysql_mysql_datadir }}/" + become: no + +- name: FILE | Re-apply owner + file: > + path={{ mysql_mysql_datadir }} + state=directory + owner=mysql + group=mysql + recurse=yes + +- name: SERVICE | Start MySQL + service: name=mysql state=started + +- name: SHELL | Get master_log_file + command: awk '{ print $1 }' {{ mysql_binlog_info }} + register: master_log_file + +- name: SHELL | Get master_log_pos + command: awk '{ print $2 }' {{ mysql_binlog_info }} + register: master_log_pos + +- name: SHELL | Get master GTID + command: awk '{ print $3 }' {{ mysql_binlog_info }} + register: master_gtid + +- name: SET_FACT | master_log_file + set_fact: + mysql_master_log_file: "{{ master_log_file.stdout }}" + mysql_master_log_pos: "{{ master_log_pos.stdout }}" + mysql_master_gtid: "{{ master_gtid.stdout }}" + +- name: FILE | Delete dump + file: path={{ mysql_backup_dir }} state=absent + delegate_to: "{{ mysql_slave_import_from }}" + when: mysql_slave_import_flush_dump + diff --git a/tasks/slave/mariadb_gtid.yml b/tasks/slave/mariadb_gtid.yml new file mode 100644 index 0000000..598e6a7 --- /dev/null +++ b/tasks/slave/mariadb_gtid.yml @@ -0,0 +1,14 @@ +--- + +# Need this hack before: +# - https://github.com/ansible/ansible-modules-extras/issues/817 +# - https://mariadb.com/kb/en/mariadb/global-transaction-id/#switching-an-existing-old-style-slave-to-use-gtid + +- name: MYSQL_REPLICATION | Stop slave + mysql_replication: mode=stopslave + +- name: COMMAND | Migrate to MariaDB GTID + command: mysql -e "CHANGE MASTER TO master_use_gtid=current_pos"; + +- name: MYSQL_REPLICATION | Start slave + mysql_replication: mode=startslave diff --git a/tasks/slave/replication.yml b/tasks/slave/replication.yml new file mode 100644 index 0000000..d8d3b8a --- /dev/null +++ b/tasks/slave/replication.yml @@ -0,0 +1,23 @@ +--- + +- name: MYSQL_REPLICATION | Stop slave + mysql_replication: mode=stopslave + +- name: MYSQL_REPLICATION | Configure master host + mysql_replication: > + mode=changemaster + master_host={{ mysql_replication_host }} + master_port={{ mysql_replication_port }} + master_user={{ mysql_replication_user }} + master_password={{ mysql_replication_password }} + +- name: MYSQL_REPLICATION | Change master + mysql_replication: > + mode=changemaster + master_log_file={{ mysql_master_log_file }} + master_log_pos={{ mysql_master_log_pos }} + when: mysql_master_log_file is defined and mysql_master_log_pos is defined + +- name: MYSQL_REPLICATION | Start slave + mysql_replication: mode=startslave + diff --git a/templates/etc/mysql/conf.d/50-master.cnf.j2 b/templates/etc/mysql/conf.d/50-master.cnf.j2 new file mode 100644 index 0000000..4e380a7 --- /dev/null +++ b/templates/etc/mysql/conf.d/50-master.cnf.j2 @@ -0,0 +1,15 @@ +# +# {{ ansible_managed }} +# + +# +# Replication master + +[mysqld] +server_id={{ mysql_server_id }} +log-bin + +# MariaDB only? +#gtid_domain_id=1 + +; vim: set ft=dosini : diff --git a/templates/etc/mysql/conf.d/51-slave.cnf.j2 b/templates/etc/mysql/conf.d/51-slave.cnf.j2 new file mode 100644 index 0000000..b625345 --- /dev/null +++ b/templates/etc/mysql/conf.d/51-slave.cnf.j2 @@ -0,0 +1,26 @@ +# +# {{ ansible_managed }} +# + +# +# Replication slave + +[mysqld] +server_id={{ mysql_server_id }} +{% if mysql_slave_readonly %} +read-only +{% endif %} + +# --> Ignore DB/tables +{% if not mysql_slave_replicate_mysqldb %} +replicate-ignore-db=mysql +{% endif %} +{% for db in mysql_slave_ignore_db %} +replicate-ignore-db={{ db }} +{% endfor %} +{% for t in mysql_slave_ignore_table %} +replicate-ignore-table={{ t }} +{% endfor %} +# <-- Ignore DB/tables + +; vim: set ft=dosini : diff --git a/templates/etc/mysql/my.cnf.j2 b/templates/etc/mysql/my.cnf.j2 index 71676e4..e4696b8 100644 --- a/templates/etc/mysql/my.cnf.j2 +++ b/templates/etc/mysql/my.cnf.j2 @@ -12,16 +12,17 @@ bind-address = {{ mysql_bind_address }} datadir = {{ mysql_datadir }} socket = {{ mysql_socket }} +# TODO: FIX later # Logging configuration. -{% if mysql_log_error == 'syslog' or mysql_log == 'syslog' %} -syslog -syslog-tag = {{ mysql_syslog_tag }} -{% else %} -{% if mysql_log %} -log = {{ mysql_log }} -{% endif %} -log-error = {{ mysql_log_error }} -{% endif %} +#{% if mysql_log_error == 'syslog' or mysql_log == 'syslog' %} +#syslog +#syslog-tag = {{ mysql_syslog_tag }} +#{% else %} +#{% if mysql_log %} +#log = {{ mysql_log }} +#{% endif %} +#log-error = {{ mysql_log_error }} +#{% endif %} {% if mysql_slow_query_log_enabled %} # Slow query log configuration. @@ -63,8 +64,9 @@ thread_concurrency = {{ mysql_thread_concurrency }} innodb_file_per_table = {{ mysql_innodb_file_per_table }} innodb_buffer_pool_size = {{ mysql_innodb_buffer_pool_size }} innodb_additional_mem_pool_size = {{ mysql_innodb_additional_mem_pool_size }} -innodb_log_file_size = {{ mysql_innodb_log_file_size }} -innodb_log_buffer_size = {{ mysql_innodb_log_buffer_size }} +# It craches after install (mysql 5.5)... TODO FIX -> http://dev.mysql.com/doc/refman/5.6/en/innodb-data-log-reconfiguration.html +#innodb_log_file_size = {{ mysql_innodb_log_file_size }} +#innodb_log_buffer_size = {{ mysql_innodb_log_buffer_size }} innodb_flush_log_at_trx_commit = {{ mysql_innodb_flush_log_at_trx_commit }} innodb_lock_wait_timeout = {{ mysql_innodb_lock_wait_timeout }} diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..23d30ad --- /dev/null +++ b/tests/README.md @@ -0,0 +1,49 @@ +About tests +=========== + +IMPORTANT +--------- + +- DO NOT `vagrant up`! My Vagrantfile provides 14 VMs (256MB each)... +- Each slave communicate to his master. You can't mix jessie and wheezy or mysql and mariadb. +- This tests uses my [MySQL role](https://github.com/HanXHX/ansible-mysql). + + +Tests +----- + +- vagrant up the master +- vagrant up the slave + +Wait master fully installed before run slave. + +Example: + +``` +sudo ansible-galaxy install HanXHX.mysql +vagrant up jessie-upstream-mariadb-master +vagrant up jessie-upstream-mariadb-slave +``` + +Supported platforms +------------------- + +- Debian Wheezy +- Debian Jessie + +Supported MySQL vendors +----------------------- + +- MariaDB 10.0 (from Debian Jessie or MariaDB repository) +- MySQL 5.5 (from Debian repositories) +- Percona Server 5.6 (from Percona Repository) + +About MySQL 5.6 +--------------- + +You can get MySQL 5.6 for Debian with [Dotdeb](https://www.dotdeb.org) with my [Dotdeb role](https://github.com/HanXHX/ansible-debian-dotdeb). I give no support with this version (pull-request accepted). It can work but without GTID. + +About MariaDB 10.1 +------------------ + +When the application is considered stable, I'll support this version. diff --git a/tests/gen.pl b/tests/gen.pl new file mode 100755 index 0000000..5de9ffc --- /dev/null +++ b/tests/gen.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +# +# This script generates ansible test config for Vagrant +# + +use strict; +use warnings; +use Socket; + +my @a = ( + "wheezy-default-mysql-master", + "wheezy-default-mysql-slave", + "wheezy-upstream-mariadb-master", + "wheezy-upstream-mariadb-slave", + "wheezy-upstream-percona-master", + "wheezy-upstream-percona-slave", + "jessie-default-mysql-master", + "jessie-default-mysql-slave", + "jessie-upstream-mariadb-master", + "jessie-upstream-mariadb-slave", + "jessie-upstream-percona-master", + "jessie-upstream-percona-slave", + "jessie-default-mariadb-master", + "jessie-default-mariadb-slave", +); + +my $start_ip = '192.168.200.10'; +my $iip = ip2long($start_ip); +my $tmp_ip = 0; + +foreach my $n (@a) +{ + my @data = split(/-/, $n); + printf( + '["%s", "deb/%s-amd64", "%s", [%s]],' . "\n", + $n, + $data[0], + long2ip($iip), + join(',', map { sprintf('"%s"', $_) } @data) + ); + + if($data[-1] eq 'slave') + { + open(FILE, '>', "host_vars/$n"); + printf FILE (qq/his_master: '%s'\n/, $tmp_ip); + close(FILE); + } + + open(FILE, '>', "group_vars/" . $data[2]); + printf FILE (qq/mysql_vendor: '%s'\n/, $data[2]); + close(FILE); + + $tmp_ip = long2ip($iip); + $iip++; +} + +sub ip2long { + return unpack("l*", pack("l*", unpack("N*", inet_aton(shift)))); +} + +sub long2ip { + return inet_ntoa(pack("N*", shift)); +} diff --git a/tests/group_vars/default b/tests/group_vars/default new file mode 100644 index 0000000..0ad8c51 --- /dev/null +++ b/tests/group_vars/default @@ -0,0 +1 @@ +mysql_origin: 'default' diff --git a/tests/group_vars/mariadb b/tests/group_vars/mariadb new file mode 100644 index 0000000..c4bf047 --- /dev/null +++ b/tests/group_vars/mariadb @@ -0,0 +1 @@ +mysql_vendor: 'mariadb' diff --git a/tests/group_vars/master b/tests/group_vars/master new file mode 100644 index 0000000..0294770 --- /dev/null +++ b/tests/group_vars/master @@ -0,0 +1,21 @@ +# Master must listen for replication +mysql_bind_address: '0.0.0.0' + +# Master durability +mysql_sync_binlog: '1' +mysql_innodb_flush_log_at_trx_commit: '1' + +mysql_databases: + - 'testrepl' + - 'norepl' + +mysql_users: + - name: 'replication' + password: '1a2b3c' + priv: '*.*:"REPLICATION SLAVE"' + host: '%' +mysql_replication_master: true +mysql_replication_slave: false +mysql_server_id: 1 + +# vim: set ft=yaml : diff --git a/tests/group_vars/mysql b/tests/group_vars/mysql new file mode 100644 index 0000000..d524c00 --- /dev/null +++ b/tests/group_vars/mysql @@ -0,0 +1 @@ +mysql_vendor: 'mysql' diff --git a/tests/group_vars/percona b/tests/group_vars/percona new file mode 100644 index 0000000..a8d9776 --- /dev/null +++ b/tests/group_vars/percona @@ -0,0 +1 @@ +mysql_vendor: 'percona' diff --git a/tests/group_vars/slave b/tests/group_vars/slave new file mode 100644 index 0000000..1af3ad2 --- /dev/null +++ b/tests/group_vars/slave @@ -0,0 +1,9 @@ +mysql_replication_master: false +mysql_replication_slave: true +mysql_replication_user: 'replication' +mysql_replication_password: '1a2b3c' +mysql_replication_host: '{{ his_master }}' +mysql_server_id: 2 +mysql_slave_ignore_db: ['norepl'] + +# vim: set ft=yaml : diff --git a/tests/group_vars/upstream b/tests/group_vars/upstream new file mode 100644 index 0000000..5b92d9b --- /dev/null +++ b/tests/group_vars/upstream @@ -0,0 +1 @@ +mysql_origin: 'upstream' diff --git a/tests/host_vars/jessie-default-mariadb-slave b/tests/host_vars/jessie-default-mariadb-slave new file mode 100644 index 0000000..fab920a --- /dev/null +++ b/tests/host_vars/jessie-default-mariadb-slave @@ -0,0 +1 @@ +his_master: '192.168.200.22' diff --git a/tests/host_vars/jessie-default-mysql-slave b/tests/host_vars/jessie-default-mysql-slave new file mode 100644 index 0000000..812a255 --- /dev/null +++ b/tests/host_vars/jessie-default-mysql-slave @@ -0,0 +1 @@ +his_master: '192.168.200.16' diff --git a/tests/host_vars/jessie-upstream-mariadb-slave b/tests/host_vars/jessie-upstream-mariadb-slave new file mode 100644 index 0000000..0dc0978 --- /dev/null +++ b/tests/host_vars/jessie-upstream-mariadb-slave @@ -0,0 +1 @@ +his_master: '192.168.200.18' diff --git a/tests/host_vars/jessie-upstream-percona-slave b/tests/host_vars/jessie-upstream-percona-slave new file mode 100644 index 0000000..db8abd8 --- /dev/null +++ b/tests/host_vars/jessie-upstream-percona-slave @@ -0,0 +1 @@ +his_master: '192.168.200.20' diff --git a/tests/host_vars/wheezy-default-mysql-slave b/tests/host_vars/wheezy-default-mysql-slave new file mode 100644 index 0000000..b9a7abf --- /dev/null +++ b/tests/host_vars/wheezy-default-mysql-slave @@ -0,0 +1 @@ +his_master: '192.168.200.10' diff --git a/tests/host_vars/wheezy-upstream-mariadb-slave b/tests/host_vars/wheezy-upstream-mariadb-slave new file mode 100644 index 0000000..5a0ff93 --- /dev/null +++ b/tests/host_vars/wheezy-upstream-mariadb-slave @@ -0,0 +1 @@ +his_master: '192.168.200.12' diff --git a/tests/host_vars/wheezy-upstream-percona-slave b/tests/host_vars/wheezy-upstream-percona-slave new file mode 100644 index 0000000..ed31632 --- /dev/null +++ b/tests/host_vars/wheezy-upstream-percona-slave @@ -0,0 +1 @@ +his_master: '192.168.200.14' diff --git a/tests/import1.sql b/tests/import1.sql new file mode 100644 index 0000000..d5f6f8a --- /dev/null +++ b/tests/import1.sql @@ -0,0 +1,12 @@ +CREATE TABLE IF NOT EXISTS `user` ( + `id` mediumint(8) unsigned NOT NULL auto_increment, + `email` varchar(255) default NULL, + PRIMARY KEY (`id`) +) AUTO_INCREMENT=1; + +INSERT IGNORE INTO `user` (`id`,`email`) VALUES (1,"tellus.Suspendisse.sed@gravida.edu"),(2,"Nulla.tempor@nonsapienmolestie.edu"),(3,"a.odio@eu.org"),(4,"adipiscing.ligula.Aenean@augueeu.com"),(5,"id.enim.Curabitur@dictumeleifendnunc.org"),(6,"est.mollis.non@laoreetlectus.net"),(7,"fringilla@mollisduiin.edu"),(8,"amet@lacusQuisquepurus.co.uk"),(9,"elementum.dui@pellentesque.org"),(10,"dictum.eu.placerat@dolorQuisque.com"); +INSERT IGNORE INTO `user` (`id`,`email`) VALUES (11,"vitae.dolor@rutrumnon.net"),(12,"semper@mattis.co.uk"),(13,"tincidunt.congue.turpis@placerateget.co.uk"),(14,"ornare.sagittis.felis@vitaesemper.ca"),(15,"luctus@sapien.ca"),(16,"tellus.id.nunc@necluctus.net"),(17,"quis.pede.Praesent@posuereenim.co.uk"),(18,"quis@metusvitaevelit.com"),(19,"in.sodales@velarcu.net"),(20,"et@sagittisplacerat.net"); +INSERT IGNORE INTO `user` (`id`,`email`) VALUES (21,"aliquam@atpretium.co.uk"),(22,"cursus@sapienmolestie.edu"),(23,"ornare.Fusce@pede.ca"),(24,"at@estac.co.uk"),(25,"sed@risusodioauctor.ca"),(26,"sit.amet.consectetuer@necorciDonec.com"),(27,"Nulla@infaucibus.co.uk"),(28,"tempus.scelerisque@utlacusNulla.com"),(29,"pellentesque.a@nostraper.com"),(30,"libero.et.tristique@Nunclaoreet.co.uk"); +INSERT IGNORE INTO `user` (`id`,`email`) VALUES (31,"dolor.sit.amet@Donecelementum.net"),(32,"ornare@massanonante.ca"),(33,"quam.elementum@semper.net"),(34,"Duis.ac.arcu@Integermollis.com"),(35,"magna.Sed.eu@magnaPraesent.co.uk"),(36,"Mauris.eu.turpis@mattis.ca"),(37,"ornare.facilisis.eget@urna.net"),(38,"Sed.diam.lorem@fringilla.co.uk"),(39,"pellentesque.Sed.dictum@Donec.edu"),(40,"faucibus.Morbi@nuncid.com"); +INSERT IGNORE INTO `user` (`id`,`email`) VALUES (41,"ullamcorper@at.co.uk"),(42,"nec.ante.blandit@utcursus.edu"),(43,"diam.vel.arcu@egestasFuscealiquet.com"),(44,"ridiculus@musAeneaneget.com"),(45,"ad@turpisvitaepurus.ca"),(46,"ultrices.posuere.cubilia@purusMaecenas.net"),(47,"luctus@libero.org"),(48,"mi@elitNulla.com"),(49,"elementum@ipsumdolorsit.edu"),(50,"orci@Donec.co.uk"); + diff --git a/tests/import2.sql b/tests/import2.sql new file mode 100644 index 0000000..aa5ae2d --- /dev/null +++ b/tests/import2.sql @@ -0,0 +1,6 @@ +INSERT IGNORE INTO `user` (`id`,`email`) VALUES (51,"vulputate.eu.odio@elitdictumeu.net"),(52,"Sed.eu@erosnec.edu"),(53,"magna@interdum.co.uk"),(54,"ornare.libero.at@Proin.net"),(55,"turpis@aneque.org"),(56,"ut.eros.non@Duisrisusodio.com"),(57,"Pellentesque.ultricies.dignissim@malesuada.edu"),(58,"vel.nisl@mifringilla.net"),(59,"dui@laoreet.com"),(60,"vitae@Suspendissedui.net"); +INSERT IGNORE INTO `user` (`id`,`email`) VALUES (61,"turpis@Curabituregestasnunc.co.uk"),(62,"arcu@enim.co.uk"),(63,"consectetuer.rhoncus.Nullam@dolordolortempus.co.uk"),(64,"sem@felisadipiscingfringilla.net"),(65,"aliquet.metus.urna@a.net"),(66,"Fusce.fermentum.fermentum@variusNam.com"),(67,"dolor@velnisl.ca"),(68,"et.netus@Duisrisusodio.edu"),(69,"malesuada@purus.edu"),(70,"gravida.sagittis@pulvinararcuet.ca"); +INSERT IGNORE INTO `user` (`id`,`email`) VALUES (71,"et.eros.Proin@Cras.co.uk"),(72,"eleifend.nunc.risus@metuseu.edu"),(73,"pede.Nunc@Phasellusnulla.net"),(74,"vitae.sodales.at@ipsumdolor.edu"),(75,"nunc.sed.pede@aliquetlobortisnisi.co.uk"),(76,"consectetuer@nonenim.ca"),(77,"ultrices@tinciduntvehicula.co.uk"),(78,"Nullam.enim.Sed@Morbiaccumsan.com"),(79,"auctor@Phasellus.net"),(80,"enim.Etiam@interdum.com"); +INSERT IGNORE INTO `user` (`id`,`email`) VALUES (81,"sapien.Cras.dolor@consectetuer.com"),(82,"malesuada.fames.ac@feugiattelluslorem.edu"),(83,"risus@vestibulum.co.uk"),(84,"Nunc@Duisgravida.ca"),(85,"ornare.egestas@sitamet.edu"),(86,"Proin.ultrices@senectus.ca"),(87,"ligula@magna.edu"),(88,"orci.tincidunt.adipiscing@sed.com"),(89,"et@venenatis.edu"),(90,"leo.Cras.vehicula@eteuismod.org"); +INSERT IGNORE INTO `user` (`id`,`email`) VALUES (91,"consequat.auctor.nunc@utsemNulla.net"),(92,"nec.leo@orci.com"),(93,"Nulla@atvelit.edu"),(94,"tempor.augue.ac@eleifend.edu"),(95,"fermentum.risus.at@penatibusetmagnis.edu"),(96,"id.erat.Etiam@porttitortellus.edu"),(97,"amet.metus.Aliquam@mus.co.uk"),(98,"dolor.tempus.non@risus.org"),(99,"vulputate.posuere.vulputate@purus.ca"),(100,"inceptos@pede.edu"); + diff --git a/tests/test.yml b/tests/test.yml index f5d1788..81677b6 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -1,29 +1,39 @@ --- -- hosts: upstream_mariadb - vars: - mysql_origin: 'upstream' - mysql_vendor: 'mariadb' - roles: - - ../../ +- hosts: all + pre_tasks: + - local_action: shell ssh-add $HOME/.vagrant.d/insecure_private_key + become: no + changed_when: false + tasks: + - apt: pkg={{ item }} state=present update_cache=yes cache_valid_time=3600 + with_items: + - strace + - vim + - get_url: > + url=https://raw.githubusercontent.com/HanXHX/ansible-debian-bootstrap/master/files/bashrc + dest=/root/.bashrc + force=yes -- hosts: debian_mariadb - vars: - mysql_origin: 'default' - mysql_vendor: 'mariadb' +- hosts: master roles: - ../../ + tasks: + - copy: src=import1.sql dest=/tmp/import1.sql + register: c + - mysql_db: name={{ item }} state=import target=/tmp/import1.sql + with_items: ['testrepl', 'norepl'] + when: c.changed -- hosts: debian_mysql - vars: - mysql_origin: 'default' - mysql_vendor: 'mysql' +- hosts: slave roles: - ../../ + tasks: + - copy: src=import2.sql dest=/tmp/import2.sql + delegate_to: "{{ his_master }}" + register: c + - mysql_db: name={{ item }} state=import target=/tmp/import2.sql + with_items: ['testrepl', 'norepl'] + delegate_to: "{{ his_master }}" + when: c.changed -- hosts: upstream_percona - vars: - mysql_origin: 'upstream' - mysql_vendor: 'percona' - roles: - - ../../