Skip to content

Commit 2fb6dcc

Browse files
committed
feat: pg completion for subcommands and postgres CLUSTER_NAME
supports subcommands - edit-config: Edit cluster configuration - list: List the Patroni members for a given Patroni supports running anywhere and any user
1 parent b0c1c2e commit 2fb6dcc

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

roles/node/files/pg_completion

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env bash
2+
#==============================================================#
3+
# File : pg_completion
4+
# Desc : auto-completion for pg commands
5+
# Path : /etc/bash_completion.d/pg
6+
# License : MIT
7+
# Author : waitingsong
8+
#==============================================================#
9+
10+
# subcommands
11+
# - edit-config: Edit cluster configuration
12+
# - list: List the Patroni members for a given Patroni
13+
14+
15+
cache_duration=30 # seconds
16+
cache_timestamp=0
17+
cache_pg_cls=""
18+
pg_subcommands="list edit-config" # from patronictl
19+
20+
if ! command -v ansible-inventory &> /dev/null; then
21+
echo "ansible-inventory not found, skip auto-completion"
22+
return
23+
fi
24+
25+
if ! command -v jq &> /dev/null; then
26+
echo "jq not found, skip auto-completion"
27+
return
28+
fi
29+
30+
INVENTORY_FILE=''
31+
if [[ -f /home/dba/pigsty/pigsty.yml ]]; then
32+
INVENTORY_FILE="/home/dba/pigsty/pigsty.yml"
33+
fi
34+
35+
36+
# Pick up pg* clusters from inventory file
37+
_get_pg_cls() {
38+
ansible-inventory -i "$1" --list 2>/dev/null | jq -r '.all.children | map(select(type == "string" and startswith("pg"))) | .[] '
39+
}
40+
41+
_pg_completions() {
42+
local subcommands="$2"
43+
local cur prev words cword
44+
_get_comp_words_by_ref -n : cur prev words cword
45+
# echo "command: $command, subcommands: $subcommands, cur: $cur, prev: $prev, words: $words, cword: $cword"
46+
47+
if [[ $cword -eq 1 ]]; then
48+
COMPREPLY=($(compgen -W "$pg_subcommands" -- "$cur"))
49+
return
50+
fi
51+
52+
if [[ -n $prev ]] && [[ ! " $pg_subcommands " =~ " $prev " ]]; then
53+
echo $subcommands
54+
return
55+
fi
56+
57+
local FILE="${INVENTORY_FILE:-'./pigsty.yml'}"
58+
if [[ $cword -eq 2 ]] && [[ -f "$FILE" ]]; then
59+
local current_time=$(date +%s)
60+
if [[ $cache_timestamp -eq 0 ]] || (( current_time - cache_timestamp > cache_duration )); then
61+
# echo "refresh cache"
62+
cache_pg_cls=$(_get_pg_cls $FILE)
63+
cache_timestamp=$current_time
64+
fi
65+
66+
if [[ -n "$cache_pg_cls" ]]; then
67+
COMPREPLY=($(compgen -W "$cache_pg_cls" -- "$cur"))
68+
fi
69+
fi
70+
}
71+
72+
complete -F _pg_completions pg
73+

roles/node/tasks/admin.yml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,26 @@
2323
{% endfor %}
2424
2525
#--------------------------------------------------------------#
26-
# Stage 3: Setup pam ulimit for node users [node_ulimit]
26+
# Stage 3: Add node pg completion [node_alias]
27+
#--------------------------------------------------------------#
28+
- name: add pg completion
29+
copy: src={{ item.src }} dest={{ item.dest }} mode=0644
30+
tags: node_alias
31+
ignore_errors: true
32+
with_items:
33+
- { src: pg_completion ,dest: /etc/bash_completion.d/pg }
34+
35+
36+
#--------------------------------------------------------------#
37+
# Stage 4: Setup pam ulimit for node users [node_ulimit]
2738
#--------------------------------------------------------------#
2839
- name: set pam ulimit
2940
tags: node_ulimit
3041
copy: src=limits.conf dest=/etc/security/limits.d/limits.conf mode=0644
3142

3243

3344
#--------------------------------------------------------------#
34-
# Stage 4: Create data dir if not exists [node_data]
45+
# Stage 5: Create data dir if not exists [node_data]
3546
#--------------------------------------------------------------#
3647
- name: assure node data dir exists
3748
tags: node_data
@@ -45,7 +56,7 @@
4556

4657

4758
#--------------------------------------------------------------#
48-
# Stage 5: Create default users/groups [node_admin]
59+
# Stage 6: Create default users/groups [node_admin]
4960
#--------------------------------------------------------------#
5061
- name: create os node users and groups
5162
tags: node_admin
@@ -107,4 +118,4 @@
107118
key: "{{ lookup('file', item) }}"
108119
with_fileglob:
109120
- "~/.ssh/id*.pub"
110-
...
121+
...

0 commit comments

Comments
 (0)