Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Pbckp 152 multihost#78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
demonolock merged 27 commits intomasterfromPBCKP-152-multihost
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
27 commits
Select commitHold shift + click to select a range
02c3375
PBCKP-137 update node.py
Apr 10, 2023
1512afd
PBCKP-137 up version 1.8.6
Apr 11, 2023
0d62e0e
PBCKP-137 update node.py
Apr 11, 2023
8be1b3a
PBCKP-137 update node
Apr 17, 2023
51f05de
PBCKP-152 change local function on execution by ssh
May 2, 2023
f131088
PBCKP-152 merge master
May 2, 2023
4f38bd5
PBCKP-152 multihost
May 3, 2023
0da2ee2
merge master
Jun 6, 2023
2bc17f0
testgres from PBCKP-152-multihost
Jun 6, 2023
f9b6bdb
PBCKP-152
Jun 10, 2023
ac77ef7
PBCKP-152 use black for formatting
Jun 11, 2023
b048041
PBCKP-152 fix failed tests
Jun 12, 2023
e098b97
PBCKP-152 fix failed tests
Jun 13, 2023
1c405ef
PBCKP-152 add tests for remote_ops.py
Jun 14, 2023
8c373e6
PBCKP-152 add testgres tests for remote node
Jun 14, 2023
72e6d5d
PBCKP-152 fixed test_simple and test_remote
Jun 17, 2023
2c2d2c5
PBCKP-588 test fix test_restore_after_failover
Jun 22, 2023
1b4f74a
PBCKP-588 test partially fixed test_simple_remote.py 41/43
Jun 22, 2023
2e916df
PBCKP-588 fixes after review
Jun 25, 2023
0528541
PBCKP-588 fixes after review - add ConnectionParams
Jun 26, 2023
089ab9b
PBCKP-588 fixes after review - remove f-strings
Jun 26, 2023
190d084
PBCKP-588 fixes after review - replace subprocess.run on subprocess.P…
Jun 27, 2023
0c26f77
PBCKP-588 fix failed tests - psql, set_auto_conf
Jun 28, 2023
0796bc4
PBCKP-152 - test_restore_target_time cut
Jul 26, 2023
0f14034
PBCKP-152 - node set listen address
Jul 28, 2023
12aa7ba
Add info about remote mode in README.md
Aug 1, 2023
4e7f4b0
merge master
Aug 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
PBCKP-152 change local function on execution by ssh
  • Loading branch information
v.shepard committedMay 2, 2023
commit51f05de66ebc5604dc72ff17af08dd7d4fb1c9a1
4 changes: 2 additions & 2 deletionssetup.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,7 +6,7 @@
from distutils.core import setup

# Basic dependencies
install_requires = ["pg8000", "port-for>=0.4", "six>=1.9.0", "psutil"]
install_requires = ["pg8000", "port-for>=0.4", "six>=1.9.0", "psutil", "fabric"]

# Add compatibility enum class
if sys.version_info < (3, 4):
Expand All@@ -21,7 +21,7 @@
readme = f.read()

setup(
version='1.8.6',
version='1.9.0',
name='testgres',
packages=['testgres'],
description='Testing utility for PostgreSQL and its extensions',
Expand Down
6 changes: 6 additions & 0 deletionstestgres/api.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,13 +30,19 @@
PostgresNode(name='...', port=..., base_dir='...')
[(3,)]
"""
from defaults import default_username
from .node import PostgresNode


def get_new_node(name=None, base_dir=None, **kwargs):
"""
Simply a wrapper around :class:`.PostgresNode` constructor.
See :meth:`.PostgresNode.__init__` for details.
For remote connection you can add next parameters:
host='127.0.0.1',
hostname='localhost',
ssh_key=None,
username=default_username()
"""
# NOTE: leave explicit 'name' and 'base_dir' for compatibility
return PostgresNode(name=name, base_dir=base_dir, **kwargs)
13 changes: 7 additions & 6 deletionstestgres/backup.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@
from six import raise_from
from tempfile import mkdtemp

from os_ops import OsOperations
from .enums import XLogMethod

from .consts import \
Expand DownExpand Up@@ -47,7 +48,7 @@ def __init__(self,
username: database user name.
xlog_method: none | fetch | stream (see docs)
"""

self.os_ops = node.os_ops
if not node.status():
raise BackupException('Node must be running')

Expand All@@ -60,8 +61,8 @@ def __init__(self,
raise BackupException(msg)

# Set default arguments
username = username ordefault_username()
base_dir = base_dir or mkdtemp(prefix=TMP_BACKUP)
username = username orself.os_ops.get_user()
base_dir = base_dir orself.os_ops.mkdtemp(prefix=TMP_BACKUP)

# public
self.original_node = node
Expand All@@ -81,7 +82,7 @@ def __init__(self,
"-D", data_dir,
"-X", xlog_method.value
] # yapf: disable
execute_utility(_params, self.log_file)
execute_utility(_params, self.log_file, hostname=node.hostname, ssh_key=node.ssh_key)

def __enter__(self):
return self
Expand All@@ -107,7 +108,7 @@ def _prepare_dir(self, destroy):
available = not destroy

if available:
dest_base_dir = mkdtemp(prefix=TMP_NODE)
dest_base_dir =self.os_ops.mkdtemp(prefix=TMP_NODE)

data1 = os.path.join(self.base_dir, DATA_DIR)
data2 = os.path.join(dest_base_dir, DATA_DIR)
Expand DownExpand Up@@ -185,4 +186,4 @@ def cleanup(self):

if self._available:
self._available = False
rmtree(self.base_dir, ignore_errors=True)
self.os_ops.rmdirs(self.base_dir, ignore_errors=True)
20 changes: 12 additions & 8 deletionstestgres/cache.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@
from shutil import copytree
from six import raise_from

from os_ops import OsOperations
from .config import testgres_config

from .consts import XLOG_CONTROL_FILE
Expand All@@ -21,14 +22,16 @@
execute_utility


def cached_initdb(data_dir, logfile=None, params=None):
def cached_initdb(data_dir, logfile=None,hostname='localhost', ssh_key=None,params=None):
"""
Perform initdb or use cached node files.
"""
os_ops = OsOperations(hostname=hostname, ssh_key=ssh_key)

def call_initdb(initdb_dir, log=None):
try:
_params = [get_bin_path("initdb"), "-D", initdb_dir, "-N"]
execute_utility(_params + (params or []), log)
execute_utility(_params + (params or []), log, hostname=hostname, ssh_key=ssh_key)
except ExecUtilException as e:
raise_from(InitNodeException("Failed to run initdb"), e)

Expand All@@ -39,26 +42,27 @@ def call_initdb(initdb_dir, log=None):
cached_data_dir = testgres_config.cached_initdb_dir

# Initialize cached initdb
if not os.path.exists(cached_data_dir) or \
not os.listdir(cached_data_dir):

if not os_ops.path_exists(cached_data_dir) or \
not os_ops.listdir(cached_data_dir):
call_initdb(cached_data_dir)

try:
# Copy cached initdb to current data dir
copytree(cached_data_dir, data_dir)
os_ops.copytree(cached_data_dir, data_dir)

# Assign this node a unique system id if asked to
if testgres_config.cached_initdb_unique:
# XXX: write new unique system id to control file
# Some users might rely upon unique system ids, but
# our initdb caching mechanism breaks this contract.
pg_control = os.path.join(data_dir, XLOG_CONTROL_FILE)
with io.open(pg_control, "r+b") as f:
f.write(generate_system_id()) # overwrite id
system_id = generate_system_id()
os_ops.write(pg_control, system_id, truncate=True, binary=True, read_and_write=True)

# XXX: build new WAL segment with our system id
_params = [get_bin_path("pg_resetwal"), "-D", data_dir, "-f"]
execute_utility(_params, logfile)
execute_utility(_params, logfile, hostname=hostname, ssh_key=ssh_key)

except ExecUtilException as e:
msg = "Failed to reset WAL for system id"
Expand Down
21 changes: 15 additions & 6 deletionstestgres/config.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -43,6 +43,9 @@ class GlobalConfig(object):

_cached_initdb_dir = None
""" underlying class attribute for cached_initdb_dir property """

os_ops = None
""" OsOperation object that allows work on remote host """
@property
def cached_initdb_dir(self):
""" path to a temp directory for cached initdb. """
Expand All@@ -52,8 +55,15 @@ def cached_initdb_dir(self):
def cached_initdb_dir(self, value):
self._cached_initdb_dir = value

# NOTE: assign initial cached dir for initdb
if self.os_ops:
testgres_config.cached_initdb_dir = self.os_ops.mkdtemp(prefix=TMP_CACHE)
else:
testgres_config.cached_initdb_dir = mkdtemp(prefix=TMP_CACHE)

if value:
cached_initdb_dirs.add(value)
return testgres_config.cached_initdb_dir

@property
def temp_dir(self):
Expand DownExpand Up@@ -133,9 +143,12 @@ def copy(self):


@atexit.register
def _rm_cached_initdb_dirs():
def _rm_cached_initdb_dirs(os_ops=None):
for d in cached_initdb_dirs:
rmtree(d, ignore_errors=True)
if os_ops:
os_ops.rmtree(d, ignore_errors=True)
else:
rmtree(d, ignore_errors=True)


def push_config(**options):
Expand DownExpand Up@@ -195,7 +208,3 @@ def configure_testgres(**options):
"""

testgres_config.update(options)


# NOTE: assign initial cached dir for initdb
testgres_config.cached_initdb_dir = mkdtemp(prefix=TMP_CACHE)
39 changes: 24 additions & 15 deletionstestgres/connection.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
# coding: utf-8
from os_ops import OsOperations

# we support both pg8000 and psycopg2
try:
Expand DownExpand Up@@ -41,11 +42,12 @@ def __init__(self,

self._node = node

self._connection = pglib.connect(database=dbname,
user=username,
password=password,
host=node.host,
port=node.port)
self.os_ops = OsOperations(node.host, node.hostname, node.ssh_key, node.username)
self._connection = self.os_ops.db_connect(dbname=dbname,
user=username,
password=password,
host=node.host,
port=node.port)

self._connection.autocommit = autocommit
self._cursor = self.connection.cursor()
Expand DownExpand Up@@ -102,17 +104,24 @@ def rollback(self):
return self

def execute(self, query, *args):
self.cursor.execute(query, args)

try:
res = self.cursor.fetchall()

# pg8000 might return tuples
if isinstance(res, tuple):
res = [tuple(t) for t in res]

return res
except Exception:
with self.connection.cursor() as cursor:
cursor.execute(query, args)
try:
res = cursor.fetchall()

# pg8000 might return tuples
if isinstance(res, tuple):
res = [tuple(t) for t in res]

return res
except (pglib.ProgrammingError, pglib.InternalError) as e:
# An error occurred while trying to fetch results (e.g., no results to fetch)
print(f"Error fetching results: {e}")
return None
except (pglib.Error, Exception) as e:
# Handle other database errors
print(f"Error executing query: {e}")
return None

def close(self):
Expand Down
16 changes: 11 additions & 5 deletionstestgres/defaults.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,12 +13,15 @@ def default_dbname():
return 'postgres'


def default_username():
def default_username(os_ops=None):
"""
Return default username (current user).
"""

return getpass.getuser()
if os_ops:
user = os_ops.get_user()
else:
user = getpass.getuser()
return user


def generate_app_name():
Expand All@@ -29,7 +32,7 @@ def generate_app_name():
return 'testgres-{}'.format(str(uuid.uuid4()))


def generate_system_id():
def generate_system_id(os_ops=None):
"""
Generate a new 64-bit unique system identifier for node.
"""
Expand All@@ -44,7 +47,10 @@ def generate_system_id():
system_id = 0
system_id |= (secs << 32)
system_id |= (usecs << 12)
system_id |= (os.getpid() & 0xFFF)
if os_ops:
system_id |= (os_ops.get_pid() & 0xFFF)
else:
system_id |= (os.getpid() & 0xFFF)

# pack ULL in native byte order
return struct.pack('=Q', system_id)
14 changes: 14 additions & 0 deletionstestgres/logger.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,20 @@
import time


# create logger
log = logging.getLogger('Testgres')
log.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('\n%(asctime)s - %(name)s[%(levelname)s]: %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
log.addHandler(ch)


class TestgresLogger(threading.Thread):
"""
Helper class to implement reading from log files.
Expand Down
23 changes: 18 additions & 5 deletionstestgres/node.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,7 @@
import subprocess
import time

from os_ops import OsOperations

try:
from collections.abc import Iterable
Expand DownExpand Up@@ -129,7 +130,8 @@ def __repr__(self):


class PostgresNode(object):
def __init__(self, name=None, port=None, base_dir=None):
def __init__(self, name=None, port=None, base_dir=None,
host='127.0.0.1', hostname='localhost', ssh_key=None, username=default_username()):
"""
PostgresNode constructor.

Expand All@@ -147,10 +149,14 @@ def __init__(self, name=None, port=None, base_dir=None):
self._master = None

# basic
self.host = '127.0.0.1'
self.name = name or generate_app_name()
self.port = port or reserve_port()

self.host = host
self.hostname = hostname
self.ssh_key = ssh_key
self.os_ops = OsOperations(host, hostname, ssh_key, username=username)

# defaults for __exit__()
self.cleanup_on_good_exit = testgres_config.node_cleanup_on_good_exit
self.cleanup_on_bad_exit = testgres_config.node_cleanup_on_bad_exit
Expand DownExpand Up@@ -455,9 +461,12 @@ def init(self, initdb_params=None, **kwargs):
"""

# initialize this PostgreSQL node
cached_initdb(data_dir=self.data_dir,
logfile=self.utils_log_file,
params=initdb_params)
cached_initdb(
data_dir=self.data_dir,
logfile=self.utils_log_file,
hostname=self.hostname,
ssh_key=self.ssh_key,
params=initdb_params)

# initialize default config files
self.default_conf(**kwargs)
Expand DownExpand Up@@ -514,6 +523,10 @@ def get_auth_method(t):
new_lines = [
u"local\treplication\tall\t\t\t{}\n".format(auth_local),
u"host\treplication\tall\t127.0.0.1/32\t{}\n".format(auth_host),

u"host\treplication\tall\t0.0.0.0/0\t{}\n".format(auth_host),
u"host\tall\tall\t0.0.0.0/0\t{}\n".format(auth_host),

u"host\treplication\tall\t::1/128\t\t{}\n".format(auth_host)
] # yapf: disable

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp