Skip to content

Commit 8a0da10

Browse files
authored
Dev environment (#338)
Add dev env
1 parent c3eaf02 commit 8a0da10

File tree

7 files changed

+266
-1
lines changed

7 files changed

+266
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@
55
.profraw
66
cov/
77
lcov.info
8+
9+
# Dev
10+
dev/.bash_history
11+
dev/cache
12+
!dev/cache/.keepme

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ docker compose up --exit-code-from main # This will also produce coverage report
109109
| Statistics | :white_check_mark: | :white_check_mark: | Query the admin database with `psql -h 127.0.0.1 -p 6432 -d pgbouncer -c 'SHOW STATS'`. |
110110
| Live config reloading | :white_check_mark: | :white_check_mark: | Run `kill -s SIGHUP $(pgrep pgcat)` and watch the config reload. |
111111

112+
### Dev
113+
114+
Also, you can open a 'dev' environment where you can debug tests easier by running the following command:
115+
116+
```
117+
./dev/script/console
118+
```
119+
120+
This will open a terminal in an environment similar to that used in tests. In there you can compile, run tests, do some debugging with the test environment, etc. Objects
121+
compiled inside the contaner (and bundled gems) will be placed in `dev/cache` so they don't interfere with what you have in your host.
122+
112123
## Usage
113124

114125
### Session mode

dev/Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM rust:bullseye
2+
3+
# Dependencies
4+
RUN apt-get update -y \
5+
&& apt-get install -y \
6+
llvm-11 psmisc postgresql-contrib postgresql-client \
7+
ruby ruby-dev libpq-dev python3 python3-pip lcov curl sudo iproute2 \
8+
strace ngrep iproute2 dnsutils lsof net-tools telnet
9+
10+
# Rust
11+
RUN cargo install cargo-binutils rustfilt
12+
RUN rustup component add llvm-tools-preview
13+
14+
# Ruby
15+
RUN sudo gem install bundler
16+
17+
# Toxyproxy
18+
RUN wget -O toxiproxy-2.4.0.deb https://github.com/Shopify/toxiproxy/releases/download/v2.4.0/toxiproxy_2.4.0_linux_$(dpkg --print-architecture).deb && \
19+
sudo dpkg -i toxiproxy-2.4.0.deb
20+
21+
# Config
22+
ENV APP_ROOT=/app
23+
ARG APP_USER=pgcat
24+
COPY dev_bashrc /etc/bash.bashrc
25+
26+
RUN useradd -m -o -u 999 ${APP_USER} || exit 0 && mkdir ${APP_ROOT} && chown ${APP_USER} ${APP_ROOT}
27+
RUN adduser ${APP_USER} sudo \
28+
&& echo "${APP_USER} ALL=NOPASSWD: ALL" > /etc/sudoers.d/${APP_USER} \
29+
&& chmod ugo+s /usr/sbin/usermod /usr/sbin/groupmod
30+
ENV HOME=${APP_ROOT}
31+
WORKDIR ${APP_ROOT}
32+
33+
ENTRYPOINT ["/bin/bash"]

dev/dev_bashrc

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# ~/.bashrc: executed by bash(1) for non-login shells.
2+
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
3+
# for examples
4+
5+
# FIX USER NEEDED SO WE CAN SHARE UID BETWEEN HOST AND DEV ENV
6+
usermod -o -u $(id -u) pgcat
7+
groupmod -o -g $(id -g) pgcat
8+
9+
# We fix the setuid in those commands as we now have sudo
10+
sudo chmod ugo-s /usr/sbin/usermod /usr/sbin/groupmod
11+
12+
# Environment customization
13+
export DEV_ROOT="${APP_ROOT}/dev"
14+
export HISTFILE="${DEV_ROOT}/.bash_history"
15+
export CARGO_TARGET_DIR="${DEV_ROOT}/cache/target"
16+
export CARGO_HOME="${DEV_ROOT}/cache/target/.cargo"
17+
export BUNDLE_PATH="${DEV_ROOT}/cache/bundle"
18+
19+
# Regular bashrc
20+
# If not running interactively, don't do anything
21+
case $- in
22+
*i*) ;;
23+
*) return;;
24+
esac
25+
26+
# don't put duplicate lines or lines starting with space in the history.
27+
# See bash(1) for more options
28+
HISTCONTROL=ignoreboth
29+
30+
# append to the history file, don't overwrite it
31+
shopt -s histappend
32+
33+
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
34+
HISTSIZE=1000
35+
HISTFILESIZE=2000
36+
37+
# check the window size after each command and, if necessary,
38+
# update the values of LINES and COLUMNS.
39+
shopt -s checkwinsize
40+
41+
# If set, the pattern "**" used in a pathname expansion context will
42+
# match all files and zero or more directories and subdirectories.
43+
#shopt -s globstar
44+
45+
# make less more friendly for non-text input files, see lesspipe(1)
46+
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
47+
48+
# set variable identifying the chroot you work in (used in the prompt below)
49+
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
50+
debian_chroot=$(cat /etc/debian_chroot)
51+
fi
52+
53+
# set a fancy prompt (non-color, unless we know we "want" color)
54+
case "$TERM" in
55+
xterm-color|*-256color) color_prompt=yes;;
56+
esac
57+
58+
# uncomment for a colored prompt, if the terminal has the capability; turned
59+
# off by default to not distract the user: the focus in a terminal window
60+
# should be on the output of commands, not on the prompt
61+
#force_color_prompt=yes
62+
63+
if [ -n "$force_color_prompt" ]; then
64+
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
65+
# We have color support; assume it's compliant with Ecma-48
66+
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
67+
# a case would tend to support setf rather than setaf.)
68+
color_prompt=yes
69+
else
70+
color_prompt=
71+
fi
72+
fi
73+
74+
PS1='\[\e]0;pgcat@dev-container\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]pgcat\[\033[00m\]@\[\033[01;32m\]dev-container\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;31m\]$(git branch &>/dev/null; if [ $? -eq 0 ]; then echo " ($(git branch | grep ^* |sed s/\*\ //))"; fi)\[\033[00m\]\$ '
75+
76+
unset color_prompt force_color_prompt
77+
78+
# enable color support of ls and also add handy aliases
79+
if [ -x /usr/bin/dircolors ]; then
80+
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
81+
alias ls='ls --color=auto'
82+
#alias dir='dir --color=auto'
83+
#alias vdir='vdir --color=auto'
84+
85+
alias grep='grep --color=auto'
86+
alias fgrep='fgrep --color=auto'
87+
alias egrep='egrep --color=auto'
88+
fi
89+
90+
# colored GCC warnings and errors
91+
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
92+
93+
# some more ls aliases
94+
alias ll='ls -alF'
95+
alias la='ls -A'
96+
alias l='ls -CF'
97+
98+
# Add an "alert" alias for long running commands. Use like so:
99+
# sleep 10; alert
100+
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
101+
102+
# Alias definitions.
103+
# You may want to put all your additions into a separate file like
104+
# ~/.bash_aliases, instead of adding them here directly.
105+
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
106+
107+
if [ -f ~/.bash_aliases ]; then
108+
. ~/.bash_aliases
109+
fi
110+
111+
# enable programmable completion features (you don't need to enable
112+
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
113+
# sources /etc/bash.bashrc).
114+
if ! shopt -oq posix; then
115+
if [ -f /usr/share/bash-completion/bash_completion ]; then
116+
. /usr/share/bash-completion/bash_completion
117+
elif [ -f /etc/bash_completion ]; then
118+
. /etc/bash_completion
119+
fi
120+
fi

dev/docker-compose.yaml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
version: "3"
2+
3+
x-common-definition-pg:
4+
&common-definition-pg
5+
image: postgres:14
6+
network_mode: "service:main"
7+
healthcheck:
8+
test: [ "CMD-SHELL", "pg_isready -U postgres -d postgres" ]
9+
interval: 5s
10+
timeout: 5s
11+
retries: 5
12+
volumes:
13+
- type: bind
14+
source: ../tests/sharding/query_routing_setup.sql
15+
target: /docker-entrypoint-initdb.d/query_routing_setup.sql
16+
- type: bind
17+
source: ../tests/sharding/partition_hash_test_setup.sql
18+
target: /docker-entrypoint-initdb.d/partition_hash_test_setup.sql
19+
20+
x-common-env-pg:
21+
&common-env-pg
22+
POSTGRES_USER: postgres
23+
POSTGRES_DB: postgres
24+
POSTGRES_PASSWORD: postgres
25+
26+
services:
27+
main:
28+
image: kubernetes/pause
29+
30+
pg1:
31+
<<: *common-definition-pg
32+
environment:
33+
<<: *common-env-pg
34+
POSTGRES_INITDB_ARGS: --auth-local=md5 --auth-host=md5 --auth=md5
35+
PGPORT: 5432
36+
command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all", "-p", "5432"]
37+
38+
pg2:
39+
<<: *common-definition-pg
40+
environment:
41+
<<: *common-env-pg
42+
POSTGRES_INITDB_ARGS: --auth-local=scram-sha-256 --auth-host=scram-sha-256 --auth=scram-sha-256
43+
PGPORT: 7432
44+
command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all", "-p", "7432"]
45+
pg3:
46+
<<: *common-definition-pg
47+
environment:
48+
<<: *common-env-pg
49+
POSTGRES_INITDB_ARGS: --auth-local=scram-sha-256 --auth-host=scram-sha-256 --auth=scram-sha-256
50+
PGPORT: 8432
51+
command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all", "-p", "8432"]
52+
pg4:
53+
<<: *common-definition-pg
54+
environment:
55+
<<: *common-env-pg
56+
POSTGRES_INITDB_ARGS: --auth-local=scram-sha-256 --auth-host=scram-sha-256 --auth=scram-sha-256
57+
PGPORT: 9432
58+
command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all", "-p", "9432"]
59+
60+
toxiproxy:
61+
build: .
62+
network_mode: "service:main"
63+
container_name: toxiproxy
64+
environment:
65+
LOG_LEVEL: info
66+
entrypoint: toxiproxy-server
67+
depends_on:
68+
- pg1
69+
- pg2
70+
- pg3
71+
- pg4
72+
73+
pgcat-shell:
74+
stdin_open: true
75+
user: "${HOST_UID}:${HOST_GID}"
76+
build: .
77+
network_mode: "service:main"
78+
depends_on:
79+
- toxiproxy
80+
volumes:
81+
- ../:/app/
82+
entrypoint:
83+
- /bin/bash
84+
- -i

dev/script/console

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
4+
export HOST_UID="$(id -u)"
5+
export HOST_GID="$(id -g)"
6+
docker-compose -f "${DIR}/../docker-compose.yaml" run --rm pgcat-shell

tests/ruby/helpers/pgcat_process.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ def initialize(log_level)
2424
@log_filename = "/tmp/pgcat_log_#{SecureRandom.urlsafe_base64}.log"
2525
@config_filename = "/tmp/pgcat_cfg_#{SecureRandom.urlsafe_base64}.toml"
2626

27-
@command = "../../target/debug/pgcat #{@config_filename}"
27+
command_path = if ENV['CARGO_TARGET_DIR'] then
28+
"#{ENV['CARGO_TARGET_DIR']}/debug/pgcat"
29+
else
30+
'../../target/debug/pgcat'
31+
end
32+
33+
@command = "#{command_path} #{@config_filename}"
2834

2935
FileUtils.cp("../../pgcat.toml", @config_filename)
3036
cfg = current_config

0 commit comments

Comments
 (0)