diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..961bbe4 --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,35 @@ +nodes = { + 'db' => [3, 10], + 'haproxy' => [1, 20], + 'web' => [2,30], + 'worker' => [1,40], +} + +network = "192.168.20" + +Vagrant.configure("2") do |config| + + nodes.each do |prefix, (count, ip_start)| + count.times do |i| + hostname = prefix + i.to_s + #puts "hostnames: " + hostname + #puts "ip: #{ip_start+i}" + + config.vm.define hostname do |box| + box.vm.box = "centos64" + box.vm.hostname = "#{hostname}" + puts "ip for #{hostname} #{network}.#{ip_start+i}" + box.vm.network :private_network, ip: "#{network}.#{ip_start+i}" + + # set memory + box.vm.provider :virtualbox do |vbox| + vbox.customize ["modifyvm", :id, "--memory", 512] + vbox.customize ["modifyvm", :id, "--cpus", 1] + if prefix == "db" + vbox.customize ["modifyvm", :id, "--memory", 1024] + end + end + end + end + end +end diff --git a/code-deploy.sh b/code-deploy.sh index 556030d..b3d6db4 100644 --- a/code-deploy.sh +++ b/code-deploy.sh @@ -1 +1 @@ -ansible-playbook -i hosts code-deploy.yml +ansible-playbook -i hosts playbooks/code-deploy.yml diff --git a/deploy-site.sh b/deploy-site.sh index 9cd9e95..56781ef 100644 --- a/deploy-site.sh +++ b/deploy-site.sh @@ -59,3 +59,4 @@ if [ -z ${MEMCACHIER_USERNAME} ]; then exit fi +ansible-playbook -i hosts site.yml diff --git a/group_vars/all b/group_vars/all index 71f251d..d0345af 100644 --- a/group_vars/all +++ b/group_vars/all @@ -4,4 +4,4 @@ repository: https://github.com/jcwimer/wrestlingApp.git replication_password: something read_write_password: something - +mysql_root_password: something diff --git a/hosts b/hosts index 8c1759b..59ca656 100644 --- a/hosts +++ b/hosts @@ -1,22 +1,18 @@ -[webservers] +[web] webhostname -[workers] +[worker] woker1 -[masterdb] -db1-hostname server_id=1 +[db] +db0 db1-hostname server_id=1 +db1 db2-hostname server_id=2 +db2 db3-hostname server_id=3 -[slavedbs] -db2-hostname server_id=2 -db3-hostname server_id=3 - -[haproxy] +[proxy] haproxy-hostname -[masterdb-internal] - -[slavedbs-internal] +[db-internal] [web-internal] diff --git a/code-deploy.yml b/playbooks/code-deploy.yml similarity index 100% rename from code-deploy.yml rename to playbooks/code-deploy.yml diff --git a/playbooks/galeradb.yml b/playbooks/galeradb.yml new file mode 100644 index 0000000..0bf2e80 --- /dev/null +++ b/playbooks/galeradb.yml @@ -0,0 +1,26 @@ +--- +# This playbook deploys a galera cluster + +- name: Apply common configuration to all nodes + hosts: db + user: root + tasks: + - include: ../roles/common/tasks/main.yml + +- name: Install galera and mariadb on all db nodes + hosts: db + user: root + tasks: + - include: ../roles/db/tasks/install.yml + +- name: Start and create cluser on first db node + hosts: db[0] + user: root + tasks: + - include: ../roles/db/tasks/startcluster.yml + +- name: Start mysql on other nodes + hosts: db[1-2] + user: root + tasks: + - include: ../roles/db/tasks/startmysql.yml diff --git a/playbooks/proxy.yml b/playbooks/proxy.yml new file mode 100644 index 0000000..d036c23 --- /dev/null +++ b/playbooks/proxy.yml @@ -0,0 +1,15 @@ +--- +# This playbook deploys the haproxy + +- name: Apply common configuration to all nodes + hosts: proxy + user: root + tasks: + - include: ../roles/common/tasks/main.yml + +- name: Deploy haproxy + hosts: proxy + user: root + tasks: + - include: ../roles/proxy/tasks/main.yml + diff --git a/playbooks/rails.yml b/playbooks/rails.yml new file mode 100644 index 0000000..ef69d4b --- /dev/null +++ b/playbooks/rails.yml @@ -0,0 +1,26 @@ +--- +# This playbook deploys the rails application + +- name: Apply common configuration to all nodes + hosts: web:worker + user: root + tasks: + - include: ../roles/common/tasks/main.yml + +- name: Apply variables to all nodes + hosts: web:worker + user: root + tasks: + - include: ../roles/rails/tasks/vars.yml + +- name: Apply web tasks to web nodes + hosts: web + user: root + tasks: + - include: ../roles/rails/tasks/web.yml + +- name: Apply worker tasks to worker nodes + hosts: worker + user: root + tasks: + - include: ../roles/rails/tasks/worker.yml diff --git a/roles/db/tasks/main.yml b/roles/db/tasks/install.yml similarity index 94% rename from roles/db/tasks/main.yml rename to roles/db/tasks/install.yml index 4998390..c420b50 100644 --- a/roles/db/tasks/main.yml +++ b/roles/db/tasks/install.yml @@ -29,3 +29,7 @@ - name: Modify configuration file to listen on all interfaces lineinfile: dest=/etc/mysql/my.cnf regexp="^bind-address" line="bind-address=0.0.0.0" + +- name: Stop mysql service + service: name=mysql state=stop + diff --git a/roles/db/tasks/startcluster.yml b/roles/db/tasks/startcluster.yml new file mode 100644 index 0000000..f5bd279 --- /dev/null +++ b/roles/db/tasks/startcluster.yml @@ -0,0 +1,27 @@ +--- +# This starts the galera cluster + +# Check if mysql is running +- name: bootstrap by starting mysql with gcom:// + action: shell /etc/init.d/mysql start --wsrep-cluster-address="gcomm://" + only_if: ${is_mysql_running.rc} > 0 + +- name: set mysql root password + action: shell mysql -e "UPDATE mysql.user SET password=PASSWORD('$root_mysql_password') where user='root';" + only_if: ${is_mysql_running.rc} > 0 + +- name: create state snapshot transfer user from anywhere + action: shell mysql -e "CREATE USER '$state_snapshot_transfer_user'@'%' IDENTIFIED BY '$state_snapshot_transfer_password';" + only_if: ${is_mysql_running.rc} > 0 + +- name: create state snapshot transfer user from localhost + action: shell mysql -e "CREATE USER '$state_snapshot_transfer_user'@'localhost' IDENTIFIED BY '$state_snapshot_transfer_password';" + only_if: ${is_mysql_running.rc} > 0 + +- name: set privileges for state snapshot transfer user + action: shell mysql -e "GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO '$state_snapshot_transfer_user'@'%';" + only_if: ${is_mysql_running.rc} > 0 + +- name: set privileges for state snapshot transfer user and flush privileges + action: shell mysql -e "GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO '$state_snapshot_transfer_user'@'localhost'; FLUSH PRIVILEGES;" + only_if: ${is_mysql_running.rc} > 0 diff --git a/roles/db/tasks/startmysql.yml b/roles/db/tasks/startmysql.yml new file mode 100644 index 0000000..4df566c --- /dev/null +++ b/roles/db/tasks/startmysql.yml @@ -0,0 +1,6 @@ +--- +# This starts the mysql service + +- name: Start mysql service + service: name=mysql state=start + diff --git a/roles/masterdb/tasks/main.yml b/roles/masterdb/tasks/main.yml deleted file mode 100644 index 4bac833..0000000 --- a/roles/masterdb/tasks/main.yml +++ /dev/null @@ -1,13 +0,0 @@ ---- -# This sets up the wrestlingtourney database and starts the binlogs -# https://mariadb.com/blog/devops-mariadb-part-1 -# https://mariadb.com/blog/devops-mariadb-and-ansible-part-2 - -- name: Modify configuration file to setup server ID - lineinfile: dest=/etc/mysql/my.cnf regexp="^#server-id" line="server-id=1" - -- name: Restart mysql service - service: name=mysql state=restarted - -- name: Reset master binlog - command: /usr/bin/mysql -u root -e "RESET MASTER" diff --git a/roles/proxy/templates/haproxy.cfg.j2 b/roles/proxy/templates/haproxy.cfg.j2 index 1997f6a..13cc99e 100644 --- a/roles/proxy/templates/haproxy.cfg.j2 +++ b/roles/proxy/templates/haproxy.cfg.j2 @@ -21,7 +21,7 @@ listen mysql-cluster mode tcp option mysql-check user haproxy_check balance roundrobin - {% for db_server in masterdb-internal %} + {% for db_server in db-internal %} server {{ db_server }} {{ db_server }}:3306 check {% endfor %} @@ -33,20 +33,12 @@ frontend www-http frontend www-https bind 0.0.0.0:443 ssl crt /root/server.pem reqadd X-Forwarded-Proto:\ https - - acl host_wrestlingdev hdr(host) -i wrestlingdev.com - use_backend wrestlingdev if host_wrestlingdev - - default_backend www-backend - -backend www-backend - redirect scheme https if !{ ssl_fc } - errorfile 503 /root/home.html + default_backend wrestlingdev backend wrestlingdev redirect scheme https if !{ ssl_fc } balance roundrobin cookie SERVERID insert indirect nocache - {% for web_server in web-internal-ips %} + {% for web_server in web-internal %} server {{ web_server }} {{ web_server }}:443 check cookie {{ web_server }} ssl verify none {% endfor %} diff --git a/roles/applicationvars/tasks/main.yml b/roles/rails/tasks/vars.yml similarity index 100% rename from roles/applicationvars/tasks/main.yml rename to roles/rails/tasks/vars.yml diff --git a/roles/web/tasks/main.yml b/roles/rails/tasks/web.yml similarity index 100% rename from roles/web/tasks/main.yml rename to roles/rails/tasks/web.yml diff --git a/roles/worker/tasks/main.yml b/roles/rails/tasks/worker.yml similarity index 100% rename from roles/worker/tasks/main.yml rename to roles/rails/tasks/worker.yml diff --git a/roles/slavedb/tasks/main.yml b/roles/slavedb/tasks/main.yml deleted file mode 100644 index 0adf391..0000000 --- a/roles/slavedb/tasks/main.yml +++ /dev/null @@ -1,13 +0,0 @@ ---- -# This installs mariadb and galera on db nodes -# https://mariadb.com/blog/devops-mariadb-part-1 -# https://mariadb.com/blog/devops-mariadb-and-ansible-part-2 - -- name: Modify configuration file to setup server ID - lineinfile: dest=/etc/mysql/my.cnf regexp="^#server-id" line="server-id=2" - -- name: Setup replication - command: /usr/bin/mysql -uroot -e "CHANGE MASTER TO master_host='192.168.50.2', master_user='repl', master_password='{{ replication_password }}', master_use_gtid=current_pos" - -- name: Restart mysql service - service: name=mysql state=restarted diff --git a/site.yml b/site.yml index 0b59829..ae0a024 100644 --- a/site.yml +++ b/site.yml @@ -1,48 +1,7 @@ --- +--- # This playbook deploys the whole application stack for wrestlingdev # -- name: apply common configuration to all nodes - hosts: all - remote_user: root - roles: - - common - -- name: configure and deploy the webservers and application code - hosts: webservers - remote_user: root - - roles: - - applicationvars - - web - -- name: configure and deploy the rails workers and application code - hosts: workers - remote_user: root - - roles: - - applicationvars - - worker - -- name: deploy master db - hosts: masterdb - remote_user: root - - roles: - - db - - masterdb - -- name: deploy mysql and configure database - hosts: slavedbs - remote_user: root - - roles: - - db - - slavedb - -- name: deploy haproxy - hosts: proxy - remote_user: root - - roles: - - proxy +- include: playbooks/galeradb.yml +- include: playbooks/proxy.yml +- include: playbooks/rails.yml