Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a0dccfa

Browse files
committed
Provision and init done.
Now we can provision and init (including create extension) ec2 nodes or manually typed ones. Also simple ansible psql wrapper added. Collecting logs started.
1 parent f279dac commit a0dccfa

28 files changed

+305
-52
lines changed

devops/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.retry
2+
inventory/manual
3+
4+
/postgresql.conf.common
5+
/postgresql.conf.lord
6+
/postgresql.conf.worker

devops/Vagrantfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Vagrant.configure("2") do |config|
2+
3+
config.vm.define "node1" do |node|
4+
node.vm.box = "ubuntu/xenial64"
5+
node.vm.network "private_network", ip: "10.42.42.10"
6+
end
7+
8+
config.vm.define "node2" do |node|
9+
node.vm.box = "ubuntu/xenial64"
10+
node.vm.network "private_network", ip: "10.42.42.20"
11+
end
12+
13+
config.vm.define "node3" do |node|
14+
node.vm.box = "ubuntu/xenial64"
15+
node.vm.network "private_network", ip: "10.42.42.30"
16+
end
17+
18+
# ssh-copy-id
19+
config.vm.provision "shell" do |s|
20+
ssh_pub_key = File.readlines("#{Dir.home}/.ssh/id_rsa.pub").first.strip
21+
s.inline = <<-SHELL
22+
echo #{ssh_pub_key} >> /home/ubuntu/.ssh/authorized_keys
23+
echo #{ssh_pub_key} >> /root/.ssh/authorized_keys
24+
SHELL
25+
end
26+
27+
end
File renamed without changes.

devops/init.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
### initdb and send configs
2+
3+
---
4+
- hosts: nodes
5+
6+
environment:
7+
PATH: "{{ pg_dst }}/bin:{{ ansible_env.PATH }}"
8+
9+
tasks:
10+
- name: pkill pg
11+
shell: 'pkill -9 postgres || true'
12+
tags:
13+
- initdb
14+
15+
- name: remove datadirs on datanodes
16+
file: path={{pg_datadir}} state=absent
17+
tags:
18+
- initdb
19+
20+
- name: create datadirs on datanodes
21+
command: "initdb {{pg_datadir}}"
22+
tags:
23+
- initdb
24+
25+
- import_tasks: tasks/pg_ctl.yml pg_ctl_action=start
26+
tags:
27+
- initdb
28+
29+
- name: create whoami database
30+
command: "createdb {{ ansible_user }}"
31+
tags:
32+
- initdb
33+
34+
- import_playbook: send_config.yml
35+
36+
- hosts: nodes
37+
38+
environment:
39+
PATH: "{{ pg_dst }}/bin:{{ ansible_env.PATH }}"
40+
41+
tasks:
42+
- name: create extension pg_shardman
43+
command: psql -p {{ pg_port }} -c "create extension pg_shardman cascade;"
44+
tags:
45+
- create_ext

ec2/inventory/inventory renamed to devops/inventory

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
# Put here manual stuff, aws-related inventory will be generated by ec2.py
21
localhost
32

43
[nodes]
4+
# nodes consists of 'manual' (e.g. for local testing) and 'ec2' autogenerated
5+
# by ec2_elect_shardlord.py
6+
# If use the former variant, you must also choose shardlord, init_node and
7+
# worker nodes by hand. See manual.example for example
58

69
[nodes:vars]
710
ansible_user=ubuntu
@@ -17,6 +20,7 @@ pg_src="{{pg_prefix}}/src"
1720
pg_dst="{{pg_prefix}}/install"
1821
pg_datadir="{{pg_prefix}}/data_{{pg_port}}"
1922
pg_logfile="{{pg_datadir}}/pg.log"
23+
pg_conffile="{{pg_datadir}}/postgresql.conf"
2024
pg_port=5432
2125

2226
pathman_repo=https://github.com/postgrespro/pg_pathman.git
@@ -31,15 +35,34 @@ cflags="-O0"
3135

3236
[nodes:children]
3337
ec2
34-
vagrant
38+
manual
3539

36-
# generated by ec2.py
40+
[shardlord]
41+
[shardlord:children]
42+
ec2_shardlord
43+
manual_shardlord
44+
45+
[workers]
46+
[workers:children]
47+
ec2_workers
48+
manual_workers
49+
50+
# designated worker on which we will initially create tables
51+
[init_node]
52+
[init_node:children]
53+
ec2_init_node
54+
manual_init_node
55+
56+
# empty groups to be able to run without ec2/manual inventories
3757
[ec2]
58+
[ec2_shardlord]
59+
[ec2_workers]
60+
[ec2_init_node]
61+
[manual]
62+
[manual_shardlord]
63+
[manual_workers]
64+
[manual_init_node]
3865

3966
[ec2:vars]
4067
# Your aws key
4168
ansible_ssh_private_key_file=~/.ssh/aws_rsa
42-
43-
# For local development
44-
[vagrant]
45-
# 10.42.42.10

ec2/inventory/ec2_elect_shardlord.py renamed to devops/inventory_ec2/ec2_elect_shardlord.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env python
22

33
'''
4-
Run ec2.py stored in .. directory and appends ec2_shardlord ec2_workers groups
5-
to it.
4+
Run ec2.py stored in .. directory and append ec2_shardlord, ec2_workers and
5+
ec2_init_node groups to it.
66
'''
77

88
import os
@@ -18,12 +18,12 @@ def ec2_elect_shardlord():
1818
hosts = list(ec2_inv["_meta"]["hostvars"].keys())
1919
# sort to choose the same shardlord each time
2020
hosts.sort(key = socket.inet_aton)
21-
shardlord, workers = [hosts[0]], hosts[1:]
21+
shardlord, workers, init_node = hosts[0:1], hosts[1:], hosts[1:2]
2222
ec2_inv["ec2_shardlord"] = shardlord
2323
ec2_inv["ec2_workers"] = workers
24+
ec2_inv["ec2_init_node"] = init_node
2425
print json.dumps(ec2_inv, sort_keys=True, indent=2)
2526

2627

2728
if __name__ == '__main__':
28-
# Run the script
2929
ec2_elect_shardlord()

devops/inventory_ec2/inventory

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../inventory

devops/inventory_manual/inventory

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../inventory

devops/inventory_manual/manual

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# For local development
2+
3+
[manual]
4+
[manual:children]
5+
manual_shardlord
6+
manual_workers
7+
8+
[manual_shardlord]
9+
10.42.42.10
10+
11+
[manual_workers]
12+
10.42.42.20
13+
# 10.42.42.30
14+
15+
[manual_init_node]
16+
10.42.42.20
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# For local development
2+
3+
# [manual]
4+
# [manual:children]
5+
# manual_shardlord
6+
# manual_workers
7+
8+
# [manual_shardlord]
9+
# 10.42.42.10
10+
11+
# [manual_workers]
12+
# 10.42.42.20
13+
# 10.42.42.30
14+
15+
# [manual_init_node]
16+
# 10.42.42.20

devops/logs.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### Collect logs
2+
3+
---
4+
- hosts: nodes
5+
6+
environment:
7+
PATH: "{{ pg_dst }}/bin:{{ ansible_env.PATH }}"
8+
9+
tasks:
10+
- name: fetch logs
11+
fetch:
12+
src: "{{ pg_logfile }}"
13+
dest: /tmp
14+
validate_checksum: no # new logs might constantly arrive

devops/pg_ctl.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
### start, stop, restart all nodes. We call task here because in some places
2+
# it is more convenient to import task instead of whole role.
3+
4+
---
5+
- hosts: nodes
6+
7+
tasks:
8+
- import_tasks: tasks/pg_ctl.yml

ec2/provision.yml renamed to devops/provision.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
tags:
4747
- build_pg
4848

49+
- name: build and install postgres_fdw
50+
shell: make clean && make -j {{makejobs}} && make install
51+
args:
52+
chdir: "{{pg_src}}/contrib/postgres_fdw"
53+
tags:
54+
- build_pg_fdw
55+
4956
- name: clone pathman
5057
git: repo={{pathman_repo}}
5158
dest={{pathman_src}}

devops/psql.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### execute psql cmd and print res
2+
3+
---
4+
- hosts: nodes
5+
6+
environment:
7+
PATH: "{{ pg_dst }}/bin:{{ ansible_env.PATH }}"
8+
9+
tasks:
10+
- name: psql cmd
11+
command: psql -p {{ pg_port }} -c {{ cmd }}
12+
register: out
13+
14+
- debug: var=out.stdout_lines

devops/readme.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
I used ansible 2.5.0.
2+
3+
This stuff allows to deploy and configure pg_shardman on ec2 nodes or on
4+
manually hardcoded ones, inventories are inventory_ec2 and inventory_manual.
5+
6+
For manual setup, type nodes to inventory_manual/manual file, see manual.example
7+
file there and comments in ./inventory. You can also check out Vagrantfile I
8+
used for testing this.
9+
10+
EC2 setup:
11+
Download aws ssh key in needed region
12+
chmod 0400 ~/.ssh/aws_rsa
13+
14+
install python packages for ec2.py: (venv is recommended):
15+
python3 -m venv ~/venv/aws
16+
As of writing this, python 3.5 has a nasty bug, so better use python 2:
17+
virtualenv -p /usr/bin/python ~/venv/aws_2.7
18+
source ~/venv/aws_2.7/bin/activate
19+
pip install boto boto3 six ansible
20+
21+
Download and set up ec2.ini:
22+
sudo wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini
23+
rds = False # we don't need rds
24+
regions = eu-central-1 # set region
25+
set aws_access_key_id = xxx
26+
set aws_secret_access_key = xxx
27+
export EC2_INI_PATH=/etc/ansible/ec2.ini
28+
29+
Now launch some instances and check that ec2.py works:
30+
inventory_ec2/ec2_elect_shardlord.py --list
31+
32+
Check that ansible works:
33+
ansible -i inventory_ec2 -m ping all
34+
For manually configured nodes:
35+
ansible -i inventory_manual -m ping all
36+
Now you are ready to go.
37+
38+
Ansible cheatsheet:
39+
40+
Build only PG on ec2:
41+
ansible-playbook -i inventory_ec2 --tags "build_pg"
42+
Same on hardcoded nodes:
43+
ansible-playbook -i inventory_manual --tags "build_pg"
44+
Provision only the second node:
45+
ansible-playbook -i inventory_ec2 provision.yml --limit nodes[1]
46+
47+
Reload configs:
48+
ansible-playbook -i inventory_ec2/ send_config.yml
49+
Restart nodes:
50+
ansible-playbook -i inventory_ec2 pg_ctl.yml -e "pg_ctl_action=restart"
51+
52+
Read cmd log on shardlord:
53+
ansible-playbook -i inventory_ec2/ psql.yml --limit 'shardlord' -e "cmd='\'table shardman.cmd_log\''"
54+
55+
Things that made me wonder during writing this:
56+
* Reusability of tasks. Playbooks, files with tasks, roles, includes, imports,
57+
old include API...probably working directly from Python would be easier? There
58+
seems to be no way to run only one play from playbook, and the only way to run
59+
several tasks from play is to use tags, which is a bit ugly. This is the reason
60+
why projects like
61+
https://github.com/larsks/ansible-toolbox
62+
emerge. Printing cmd output via saved var is pretty horrible too.
63+
* No way to specify multiple inventory files, only whole directory or one file.
64+
* No way to append file to file, except for j2 templates, which doesn't work
65+
with files outside the template dir, really?

devops/send_config.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### send configs and restart nodes
2+
# Templates in the root of the project are copied automatically. You can append
3+
# something to them by creating same-titled in this (devops/) directory, they
4+
# are not tracked by git.
5+
6+
---
7+
- hosts: shardlord
8+
tasks:
9+
- name: copy postgresql.conf to shardlord
10+
template:
11+
src=templates/postgresql.conf.lord.j2
12+
dest="{{ pg_conffile }}"
13+
14+
- hosts: workers
15+
tasks:
16+
- name: copy postgresql.conf to workers
17+
template:
18+
src=templates/postgresql.conf.worker.j2
19+
dest="{{ pg_conffile }}"
20+
21+
- import_playbook: pg_ctl.yml pg_ctl_action=restart

devops/tasks/pg_ctl.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
3+
- name: pg_ctl {{pg_ctl_action}}
4+
command: "{{pg_dst}}/bin/pg_ctl {{pg_ctl_action}} -D {{pg_datadir}} -l {{pg_logfile}}"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../postgresql.conf.common
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../postgresql.conf.common
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../postgresql.conf.lord
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../postgresql.conf.worker

devops/templates/postgresql.conf.lord

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../postgresql.conf.lord
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{# jinja2 won't work with outside file, so I've created a bunch of symlinks #}
2+
{# pointing to them, oh my... #}
3+
{# see https://github.com/ansible/ansible/issues/7106 #}
4+
{% include "postgresql.conf.common" ignore missing %}
5+
6+
{% include "postgresql.conf.lord" ignore missing %}
7+
8+
{% include "postgresql.conf.devops.common" ignore missing %}
9+
10+
{% include "postgresql.conf.devops.lord" ignore missing %}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../postgresql.conf.worker
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{# jinja2 won't work with outside file, so I've created a bunch of symlinks #}
2+
{# pointing to them, oh my... #}
3+
{# see https://github.com/ansible/ansible/issues/7106 #}
4+
{% include "postgresql.conf.common" %}
5+
6+
{% include "postgresql.conf.worker" %}
7+
8+
{% include "postgresql.conf.devops.common" ignore missing %}
9+
10+
{% include "postgresql.conf.devops.worker" ignore missing %}

0 commit comments

Comments
 (0)