Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit 7393c6a Author: Feike Steenbergen <feikesteenbergen@gmail.com> Initial bootstrapping of patronicli, to start work on issue #44
- Loading branch information
1 parent
98b5935
commit 2a8a553
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python3 | ||
''' | ||
Patroni Command Line Client | ||
''' | ||
|
||
import click | ||
import os | ||
import yaml | ||
import logging | ||
|
||
# from patroni.dcs import AbstractDCS, Cluster, Failover, Leader, Member | ||
|
||
CONFIG_DIR_PATH = click.get_app_dir('patroni') | ||
CONFIG_FILE_PATH = os.path.join(CONFIG_DIR_PATH, 'patronicli.yaml') | ||
LOGLEVEL = 'DEBUG' | ||
|
||
|
||
def load_config(path): | ||
logging.debug('Loading configuration from file {}'.format(path)) | ||
config = None | ||
try: | ||
with open(path, 'rb') as fd: | ||
config = yaml.safe_load(fd) | ||
except: | ||
logging.exception('Could not load configuration file') | ||
return config or {} | ||
|
||
|
||
def store_config(config, path): | ||
dir_path = os.path.dirname(path) | ||
if dir_path: | ||
if not os.path.isdir(dir_path): | ||
os.makedirs(dir_path) | ||
with open(path, 'w') as fd: | ||
yaml.dump(config, fd) | ||
|
||
|
||
@click.group() | ||
@click.option('--config-file', '-c', help='Use alternative configuration file', | ||
default=CONFIG_FILE_PATH) | ||
@click.pass_context | ||
def cli(ctx, config_file): | ||
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=LOGLEVEL) | ||
ctx.obj = load_config(CONFIG_FILE_PATH) | ||
|
||
|
||
@cli.command('list') | ||
@click.pass_obj | ||
def list_haclusters(obj): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env python | ||
from patroni.cli import cli | ||
|
||
if __name__ == '__main__': | ||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ requests | |
six >= 1.7 | ||
kazoo>=2.2.1 | ||
python-etcd>=0.4.1 | ||
click |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ requests | |
six | ||
kazoo>=2.2.1 | ||
python-etcd>=0.4.1 | ||
click |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
import pytest | ||
|
||
from click.testing import CliRunner | ||
from patroni.cli import cli, list_haclusters, store_config, load_config | ||
|
||
CONFIG_FILE_PATH="./test-cli.yaml" | ||
|
||
def test_rw_config(): | ||
runner = CliRunner() | ||
with runner.isolated_filesystem(): | ||
os.mkdir(CONFIG_FILE_PATH) | ||
with pytest.raises(Exception): | ||
result = load_config(CONFIG_FILE_PATH) | ||
assert 'Could not load configuration file' in result.output | ||
|
||
with pytest.raises(Exception): | ||
store_config(config, CONFIG_FILE_PATH) | ||
os.rmdir(CONFIG_FILE_PATH) | ||
|
||
config = "a:b" | ||
store_config(config, "abc/CONFIG_FILE_PATH") | ||
load_config(CONFIG_FILE_PATH) | ||
|
||
def test_cli(): | ||
runner = CliRunner() | ||
|
||
runner.invoke(cli, ['list']) | ||
|
||
result = runner.invoke(cli, ['--help']) | ||
assert 'Usage:' in result.output | ||
|
||
def test_list_hacluster(): | ||
runner = CliRunner() | ||
|
||
result = runner.invoke(list_haclusters) | ||
|