Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

Commit

Permalink
release-4.0: Support Inject cluster topology information for am and g…
Browse files Browse the repository at this point in the history
…rafana to etcd in pd (#1196)

* Support Inject cluster topology information for am and grafana to etcd in pd (#1124)

* [Init] Initialize pr and create scripts for sending request to pd

* [ADD] adding scripts to yml

* [Replace] replacing with shell arguments

* [Try] trying to testing ansible

* [Update] Updating script

* update

* [FIX] fix wrong api

* Add deploy path

* [ADD] adding another logic loading

* [ADD] adding net/deploy

* add all pd info

* [Update] Change directory name and update etcd write path.

Signed-off-by: mapleFU <1506118561@qq.com>

* change init-pd-topo to dashboart topo

* [Update] Support `port` field.

Signed-off-by: mapleFU <1506118561@qq.com>

* [Fix] Fixing errors on scripts.

Signed-off-by: mapleFU <1506118561@qq.com>

* [Final] Trying to fix all the problems

Signed-off-by: mapleFU <1506118561@qq.com>

* [Update] update port type.

Signed-off-by: mapleFU <1506118561@qq.com>

* [FIX] fix bugs for tidb-anisble deploy dir

Signed-off-by: mapleFU <1506118561@qq.com>

* [Update] Wait until testing.

Signed-off-by: mapleFU <1506118561@qq.com>

* [Revert] Revert unnescessary changes.

Signed-off-by: mapleFU <1506118561@qq.com>

* [DONE] Finish dev.

Signed-off-by: mapleFU <1506118561@qq.com>

* Update start.yml

* Update start.yml

Co-authored-by: liubo <liubo0127@qq.com>

* Support Prometheus in for dashboard topology (#1195)

* [Support] change tidb-ansible script in tidb-ansible.

Signed-off-by: mapleFU <1506118561@qq.com>

* [ADD] Adding supports in roles.

Signed-off-by: mapleFU <1506118561@qq.com>

* [Multiple] pass the first when there is multiple arguments.

Co-authored-by: mwish <1506118561@qq.com>
  • Loading branch information
liubo0127 and mapleFU authored Mar 12, 2020
1 parent 0901222 commit 9237c54
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 0 deletions.
7 changes: 7 additions & 0 deletions deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,10 @@
- always
roles:
- perf_tools

- name: deploying init pd topo scripts
hosts: localhost
tags:
- always
roles:
- dashboard_topo
6 changes: 6 additions & 0 deletions excessive_rolling_update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,12 @@
delay: 5
when: enable_tls|default(false)

- hosts: localhost
tags:
- always
tasks:
- name: init pd topo
shell: "cd {{ playbook_dir }}/scripts && ./dashboard_topo.sh"

- name: rolling update TiFlash cluster
hosts: tiflash_servers
Expand Down
7 changes: 7 additions & 0 deletions roles/dashboard_topo/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---

- name: generate init_dashboard_topo script
template:
src: "init_dashboard_topo.sh.j2"
dest: "{{ playbook_dir }}/scripts/dashboard_topo.sh"
mode: "0755"
47 changes: 47 additions & 0 deletions roles/dashboard_topo/templates/init_dashboard_topo.sh.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
set -e

{% set all_pd = [] -%}
{% set pd_hosts = groups.pd_servers %}
{% for host in pd_hosts -%}
{% set pd_ip = hostvars[host].ansible_host | default(hostvars[host].inventory_hostname) -%}
{% set pd_port = hostvars[host].pd_client_port -%}
{% set pd_path = hostvars[host].deploy_dir -%}
{% set _ = all_pd.append("%s:%s%s" % (pd_ip, pd_port, pd_path)) -%}
{% endfor -%}
{% set all_grafana = [] -%}
{% set grafana_hosts = groups.grafana_servers %}
{% for host in grafana_hosts -%}
{% set grafana_ip = hostvars[host].ansible_host | default(hostvars[host].inventory_hostname) -%}
{% set grafana_port = hostvars[host].grafana_port -%}
{% set grafana_path = hostvars[host].deploy_dir -%}
{% set _ = all_grafana.append("%s:%s%s" % (grafana_ip, grafana_port, grafana_path)) -%}
{% endfor -%}
{% set all_alertmanager = [] -%}
{% set alertmanager_hosts = groups.alertmanager_servers %}
{% for host in alertmanager_hosts -%}
{% set alertmanager_ip = hostvars[host].ansible_host | default(hostvars[host].inventory_hostname) -%}
{% set alertmanager_port = hostvars[host].alertmanager_port -%}
{% set alertmanager_path = hostvars[host].deploy_dir -%}
{% set _ = all_alertmanager.append("%s:%s%s" % (alertmanager_ip, alertmanager_port, alertmanager_path)) -%}
{% endfor -%}
{% set all_prometheus = [] -%}
{% set prometheus_hosts = groups.monitoring_servers %}
{% for host in prometheus_hosts -%}
{% set prometheus_ip = hostvars[host].ansible_host | default(hostvars[host].inventory_hostname) -%}
{% set prometheus_port = hostvars[host].prometheus_port -%}
{% set prometheus_path = hostvars[host].deploy_dir -%}
{% set _ = all_prometheus.append("%s:%s%s" % (prometheus_ip, prometheus_port, prometheus_path)) -%}
{% endfor -%}
{% set flag = "" %}
{% if all_grafana -%}
{% set flag = flag + " --grafana " + ','.join(all_grafana) -%}
{% endif -%}
{% if all_alertmanager -%}
{% set flag = flag + " --alertmanager " + ','.join(all_alertmanager) -%}
{% endif -%}
{% if all_prometheus %}
{% set flag = flag + " --prometheus " + ','.join(all_prometheus) -%}
{% endif -%}

python2 dashboard_topo.py --pd {{ all_pd | join(',') }} {{ flag }}
6 changes: 6 additions & 0 deletions rolling_update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,12 @@
delay: 5
when: enable_tls|default(false)

- hosts: localhost
tags:
- always
tasks:
- name: init pd topo
shell: "cd {{ playbook_dir }}/scripts && ./dashboard_topo.sh"

- name: rolling update TiFlash cluster
hosts: tiflash_servers
Expand Down
114 changes: 114 additions & 0 deletions scripts/dashboard_topo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env python2

from __future__ import print_function, \
unicode_literals

import urllib
import urllib2
import base64
import json
import argparse

ComponentToRegister = ('alertmanager', 'grafana', 'pd', 'prometheus')


def parse_opts():
"""
parse_opts parse the input of involved components and pd address.
"""
parser = argparse.ArgumentParser(description="Parse output.")
# pd is involved because we need to send http request
for target in ComponentToRegister:
parser.add_argument("--{}".format(target),
help="the address list of {}".format(target))
args, unknown = parser.parse_known_args()
return args


def etcd_write(etcd_url, key, value):
encoded_key = base64.b64encode(key)
encoded_value = base64.b64encode(value)
data = json.dumps({
"key": encoded_key,
"value": encoded_value,
})
req = urllib2.Request('http://' + etcd_url + '/v3/kv/put',
data=data,
headers={'Content-Type': 'application/json'})
try:
resp = urllib2.urlopen(req)
data = json.load(resp)
return data
except urllib2.HTTPError as error:
raise error


def parse_address(con):
"""
con: str for argument like "127.0.0.1:2379/deploy"
return: Tuple[str, str] like ("127.0.0.1:2379", "/deploy")
"""
pos = con.find('/')
return (con[:pos], con[pos:])


def request_topo(comp, topo, etcd_target):
"""
Sending request to etcd v3, and leave:
under {pd_target}:
write: /topology/{comp}: {ip: ip, address: address}
comp: str for component name, which will be like "tidb"
topo: str for topology address, like "127.0.0.1:4000"
pd_target: the place to send etcd request, like "127.0.0.1:2379"
"""
if topo is None:
# if topo is None, do nothing
return
if ',' in topo:
topo = topo.split(',')[0]
ip, add = parse_address(topo)
ip, port = ip.split(':')

message = json.dumps({
'ip': ip,
'binary_path': add,
'port': int(port),
})
etcd_write(etcd_target, "/topology/" + comp, message)


def concat_to_address(ip, port):
"""
ip: str for address to concat, like "127.0.0.1"
port: str for port, like "2379"
return: str like "127.0.0.1:2379"
return None if ip or port is None
"""
if ip is None or port is None:
return None
return ip.strip() + ":" + port.strip()


if __name__ == '__main__':
args = parse_opts()

# parse from args
pd_address = args.pd
pd_address_zero, _ = parse_address(pd_address.split(',')[0])

alertmanager_address = args.alertmanager
grafana_address = args.grafana
prometheus_address = args.prometheus

mapping = {
'alertmanager': alertmanager_address,
'grafana': grafana_address,
'prometheus': prometheus_address,
}

for comp in ComponentToRegister:
if comp == 'pd':
continue
request_topo(comp, mapping[comp], pd_address_zero)
7 changes: 7 additions & 0 deletions start.yml
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,10 @@
vars:
- ansible_become: false
- ansible_connection: local

- hosts: localhost
tags:
- always
tasks:
- name: init dashboard topology
shell: "cd {{ playbook_dir }}/scripts && ./dashboard_topo.sh"

0 comments on commit 9237c54

Please sign in to comment.