Skip to content

Commit

Permalink
Updated broker to be compatible with dynaconf 3.0.0
Browse files Browse the repository at this point in the history
+ Added the ability to specify a BROKER_DIRECTORY envrionment variable
+ Changed settings.yaml to broker_settings.yaml
  • Loading branch information
JacobCallahan committed Jul 1, 2020
1 parent 3f9b2d8 commit 5cf4f23
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ ENV/
.pytest_cache/

# project
settings.yaml
*settings.yaml
inventory.yaml
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ python:
- "3.8"
install: pip install .
script:
- mv settings.yaml.example settings.yaml
- mv broker_settings.yaml.example broker_settings.yaml
- pytest -v
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ RUN dnf -y install make cmake gcc-c++ zlib-devel \
openssl-devel git python3-pip python3-devel \
&& dnf clean all
WORKDIR /root/broker
ENV BROKER_DIRECTORY=/root/broker/
COPY . /root/broker/
RUN pip install .
RUN cp settings.yaml.example settings.yaml
RUN cp broker_settings.yaml.example broker_settings.yaml


ENTRYPOINT ["broker"]
Expand Down
7 changes: 7 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
History
=======

0.0.10 (2020-06-29)
==================

+ Updated broker to be compatible with dynaconf 3.0.0
+ Added the ability to specify a BROKER_DIRECTORY envrionment variable
+ Changed settings.yaml to broker_settings.yaml

0.0.9 (2020-06-19)
==================

Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ Broker is a tool designed to provide a common interface between one or many serv
dnf install cmake
cd <broker root directory>
pip install .
cp setting.yaml.example settings.yaml
cp broker_settings.yaml.example broker_settings.yaml
```
Then edit the settings.yaml file
Then edit the broker_settings.yaml file

Broker can also be ran outside of its base directory. In order to do so, specify the directory broker's files are in with the
`BROKER_DIRECTORY` envronment variable.
```BROKER_DIRECTORY=/home/jake/Programming/broker/ broker inventory```

# Usage
**Checking out a VM**
Expand Down
11 changes: 6 additions & 5 deletions broker/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Miscellaneous helpers live here"""
import os
from collections import UserDict
from collections.abc import MutableMapping
from copy import deepcopy
from pathlib import Path
from dynaconf import settings
from broker import settings
import yaml


Expand Down Expand Up @@ -73,17 +74,17 @@ def resolve_nick(nick):
:return: a dictionary mapping argument names and values
"""
nick_names = settings.get("NICKS", {})
nick_names = settings.settings.get("NICKS", {})
if nick in nick_names:
return settings.NICKS[nick].to_dict()
return settings.settings.NICKS[nick].to_dict()


def load_inventory():
"""Loads all local hosts in inventory
:return: list of dictionaries
"""
inventory_file = Path(settings.INVENTORY_FILE)
inventory_file = settings.BROKER_DIRECTORY.joinpath(settings.settings.INVENTORY_FILE)
if not inventory_file.exists():
inv_data = []
else:
Expand All @@ -101,7 +102,7 @@ def update_inventory(add=None, remove=None):
:return: no return value
"""
inventory_file = Path(settings.INVENTORY_FILE)
inventory_file = settings.BROKER_DIRECTORY.joinpath(settings.settings.INVENTORY_FILE)
if add and not isinstance(add, list):
add = [add]
if remove and not isinstance(remove, list):
Expand Down
2 changes: 1 addition & 1 deletion broker/hosts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# from functools import cached_property
from dynaconf import settings
from broker.settings import settings
from broker import session

class Host:
Expand Down
4 changes: 4 additions & 0 deletions broker/logger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# -*- encoding: utf-8 -*-
"""Module handling internal and dependency logging."""
import os
import logging
from pathlib import Path
import logzero
from broker.settings import BROKER_DIRECTORY


def setup_logzero(level="info", path="logs/broker.log"):
Expand All @@ -15,6 +18,7 @@ def setup_logzero(level="info", path="logs/broker.log"):
formatter = logzero.LogFormatter(fmt=debug_fmt if log_level is logging.DEBUG else log_fmt)
logzero.setup_default_logger(formatter=formatter)
logzero.loglevel(log_level)
path = str(BROKER_DIRECTORY.joinpath(path))
logzero.logfile(
path, loglevel=log_level, maxBytes=1e9, backupCount=3, formatter=formatter
)
Expand Down
2 changes: 1 addition & 1 deletion broker/providers/ansible_tower.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import inspect
import json
from dynaconf import settings
from broker.settings import settings
from logzero import logger

try:
Expand Down
2 changes: 1 addition & 1 deletion broker/providers/test_provider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import inspect
from dynaconf import settings
from broker.settings import settings
from logzero import logger

from broker.providers import Provider
Expand Down
13 changes: 13 additions & 0 deletions broker/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os
from pathlib import Path
from dynaconf import Dynaconf

settings_file = "broker_settings.yaml"
BROKER_DIRECTORY = Path()

if "BROKER_DIRECTORY" in os.environ:
envar_location = Path(os.environ["BROKER_DIRECTORY"])
if envar_location.is_dir():
BROKER_DIRECTORY = envar_location

settings = Dynaconf(settings_file=str(BROKER_DIRECTORY.joinpath("broker_settings.yaml")))
26 changes: 26 additions & 0 deletions broker_settings.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Broker settings
debug: False
inventory_file: "inventory.yaml"
# Host Settings
host_username: "root"
host_password: "<password>"
# Provider settings
AnsibleTower:
base_url: "https://<ansible tower host>/"
username: "<username>"
password: "<plain text password>"
release_workflow: "remove-vm"
TestProvider:
config_value: "something"
# You can set a nickname as a shortcut for arguments
nicks:
rhel7:
workflow: "deploy-base-rhel"
rhel_version: "7.7"
provider: "RHEV"
notes: "Requested by broker"
test_nick:
test_action: "fake"
arg1: "abc"
arg2: 123
arg3: True
27 changes: 0 additions & 27 deletions settings.yaml.example

This file was deleted.

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
with open("HISTORY.rst") as history_file:
history = history_file.read()

requirements = [AWXKIT, "click", "dynaconf[yaml]", "logzero", "pyyaml", "ssh2-python"]
requirements = [AWXKIT, "click", "dynaconf", "logzero", "pyyaml", "ssh2-python"]

setup(
name="broker",
version="0.0.9",
version="0.0.10",
description="The infrastructure middleman.",
long_description=readme + "\n\n" + history,
author="Jacob J Callahan",
Expand Down

0 comments on commit 5cf4f23

Please sign in to comment.