Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lib/constants.py #22

Open
deadbits opened this issue Aug 28, 2018 · 0 comments
Open

Add lib/constants.py #22

deadbits opened this issue Aug 28, 2018 · 0 comments
Assignees
Labels
enhancement testing Features being tested before deployment

Comments

@deadbits
Copy link
Contributor

Creating a constants.py file to contain path to root dir, API keys file, app configuration file, app version info.

Found this snippet from the threatshell project by @tstallings at Salesforce and it's a great way to track versioning if users will be filing tickets. I modified the output a bit for build_version and app_version for our needs.
Users could reference the app_version provided and we'll know exactly what they are on for debugging.

import os
import subprocess

__app_stage = 'beta'
__app_major = '1.0'

__cwd = os.path.abspath(os.path.dirname(__file__))

root_path = os.path.abspath(os.path.join(__cwd, '..'))
api_conf = os.path.join(root_path, 'etc/apikeys.json')
app_conf = os.path.join(root_path, 'etc/omnibus.conf')

rev_hash = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"])
rev_count = subprocess.check_output(["git", "rev-list", "HEAD", "--count"])

build_version = 'v%s.%s-%s_%s' % (__app_major, rev_count.strip(), rev_hash.strip(), __app_stage)
app_version = 'v%s.%s_%s' % (__app_major, rev_count.strip(), __app_stage)

example output from my current local repo:

build: v1.0.125-e73bcf8_beta
app: v1.0.125_beta

Inside omnibus-cli.py and common.py, we'd then just do:

from constants import api_conf
from constants import app_conf
from constants import build_version
from constants import app_version

as needed for printing the current build_version, initializing DB classes with the config files, etc

Example of updated common.py

from constants import api_conf
from constants import app_conf

...

def get_option(section, name):
    config = ConfigParser.ConfigParser()
    if not os.path.exists(app_conf):
        error('configuration file %s does not exist!' % app_conf)
        return None
    config.read(app_conf)
    answer = None
    try:
        answer = config.get(section, name)
    except:
        pass
    return answer


def get_apikey(service):
    """ Read API key config file and return API key by service name """
    if os.path.exists(api_conf):
        api_keys = load_json(api_conf)
        if service in api_keys.keys():
            return api_keys[service]
    else:
        error('cannot find API keys file: %s' % api_conf)
@deadbits deadbits added enhancement testing Features being tested before deployment labels Aug 28, 2018
@deadbits deadbits self-assigned this Aug 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement testing Features being tested before deployment
Projects
None yet
Development

No branches or pull requests

1 participant