Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 7393c6a
Author: Feike Steenbergen <feikesteenbergen@gmail.com>
Initial bootstrapping of patronicli, to start work on issue #44
  • Loading branch information
feikesteenbergen committed Oct 14, 2015
1 parent 98b5935 commit 2a8a553
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
50 changes: 50 additions & 0 deletions patroni/cli.py
@@ -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
5 changes: 5 additions & 0 deletions patronicli.py
@@ -0,0 +1,5 @@
#!/usr/bin/env python
from patroni.cli import cli

if __name__ == '__main__':
cli()
1 change: 1 addition & 0 deletions requirements-py2.txt
Expand Up @@ -7,3 +7,4 @@ requests
six >= 1.7
kazoo>=2.2.1
python-etcd>=0.4.1
click
1 change: 1 addition & 0 deletions requirements-py3.txt
Expand Up @@ -7,3 +7,4 @@ requests
six
kazoo>=2.2.1
python-etcd>=0.4.1
click
37 changes: 37 additions & 0 deletions tests/test_cli.py
@@ -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)

0 comments on commit 2a8a553

Please sign in to comment.