Преглед на файлове

start configuration script

Chris Mague преди 4 години
родител
ревизия
d8d079f212
променени са 5 файла, в които са добавени 80 реда и са изтрити 14 реда
  1. 2 0
      ansible/default.yaml
  2. 25 0
      ansible/nodes.yml
  3. 25 0
      ansible/templates/setup_raft_cluster.j2
  4. 11 1
      ansible/tester.yml
  5. 17 13
      main.tf

+ 2 - 0
ansible/default.yaml

@@ -0,0 +1,2 @@
+---
+cpredis_node_ips: "{{ NODE_IPS|default('fml') }}"

+ 25 - 0
ansible/nodes.yml

@@ -6,6 +6,7 @@
   become_method: sudo
   gather_facts: yes
 
+
   vars:
     raft_repo: "https://github.com/yossigo/redisraft"
     raft_branch: "cluster"
@@ -45,8 +46,22 @@
     - name: Ubuntu Packages
       package:
         name: "{{ deb_packages }}"
+    - name: Set to Realistic Hostname
+      hostname:
+        name: "cpredisnode-{{ ansible_default_ipv4.address | replace('.', '-') }}"
+    - name: Setup Hostsfile
+      lineinfile:
+        dest: /etc/hosts
+        regexp: '^127\.0\.0\.1[ \t]+localhost'
+        line: "127.0.0.1 localhost cpredisnode-{{ansible_default_ipv4.address | replace('.','-')}}"
+        state: present
 
   tasks:
+    - name: load vars
+      include_vars: default.yaml
+      tags:
+        - cluster
+
     - name: create redis user
       user:
         name: redis
@@ -106,6 +121,16 @@
         - reload_redis_services
         - restart_redis_services
 
+    - name: Cluster Setup Script
+      template:
+        src: setup_raft_cluster.j2
+        dest: /usr/local/bin/setup-raft-cluster
+        owner: redis
+        group: redis
+        mode: 0755
+      tags:
+        - cluster
+
     - name: Enable Redis Services
       systemd:
         name: redis-{{ item['port'] }}

+ 25 - 0
ansible/templates/setup_raft_cluster.j2

@@ -0,0 +1,25 @@
+#!/bin/bash
+
+#####################################################################
+# {{ ansible_managed }}
+#####################################################################
+
+{% set primary_node =  cpredis_node_ips.split(',')[0]%}
+{% set other_nodes =  cpredis_node_ips.split(',')[1:]%}
+#####################################################################
+# Data Information:
+#####################################################################
+# Primary Node: {{ primary_node }}
+# Other Nodes:  {{ other_nodes | join(",") }}
+# Raft Instance Ports: 
+{% for p in redis_ports -%}
+#   - {{ p.port }}
+{% endfor %}
+#####################################################################
+
+{% for p in redis_ports %}
+/usr/local/bin/redis-cli -h {{ primary_node }} -p {{ p.port }} raft.cluster init
+{% for node in other_nodes %}
+/usr/local/bin/redis-cli -h {{ node }} -p {{ p.port }} raft.cluster join {{ primary_node }}:{{ p.port }}
+{% endfor %}
+{% endfor %}

+ 11 - 1
ansible/tester.yml

@@ -29,9 +29,19 @@
     - name: Ubuntu Packages
       package:
         name: "{{ deb_packages }}"
-        
+    - name: Set to Realistic Hostname
+      hostname:
+        name: "tester"
+    - name: Setup Hostsfile
+      lineinfile:
+        dest: /etc/hosts
+        regexp: '^127\.0\.0\.1[ \t]+localhost'
+        line: "127.0.0.1 localhost tester"
+        state: present
 
   tasks:
+    - name: load vars
+      include_vars: default.yaml
     - name: create redis user
       user:
         name: redis

+ 17 - 13
main.tf

@@ -105,12 +105,12 @@ resource "null_resource" "raft_group_setup" {
 
 resource "local_file" "inventory" {
   content = templatefile("${path.module}/inventories/inventory.ctmpl",
-              { 
-                nodes  = module.nodes.node-ips
-                tester = aws_eip.re-eip.public_ip
-                path   = abspath(path.module)
-              }
-          )
+    {
+      nodes  = module.nodes.node-ips
+      tester = aws_eip.re-eip.public_ip
+      path   = abspath(path.module)
+    }
+  )
   filename        = "${path.module}/inventories/inventory.ini"
   file_permission = "0644"
 }
@@ -118,11 +118,11 @@ resource "local_file" "inventory" {
 
 resource "local_file" "ssh_config" {
   content = templatefile("${path.module}/inventories/ssh.ctmpl",
-              { 
-                ssh-key  = "${var.vpc-name}.pem"
-                ssh-user = "ubuntu"
-              }
-          )
+    {
+      ssh-key  = "${var.vpc-name}.pem"
+      ssh-user = "ubuntu"
+    }
+  )
   filename        = "${path.module}/inventories/ssh.cfg"
   file_permission = "0644"
 }
@@ -136,5 +136,9 @@ output "ansible1" {
 }
 
 output "ansible2" {
-  value = "Setup CP Redis Nodes\n\t\tansible-playbook -i inventories/inventory.ini  ansible/nodes.yml"
-}
+  value = "Setup CP Redis Nodes\n\t\tansible-playbook -i inventories/inventory.ini  ansible/nodes.yml -e 'NODE_IPS=${join(",", module.nodes.node-internal-ips)}'"
+}
+
+output "cpredisnodes" {
+  value = module.nodes.node-internal-ips
+}