Created
September 21, 2019 09:36
-
-
Save gm3dmo/cbc4f7a2d64199e40bc09a10785f2d0d to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import print_function | |
""" | |
Emit Consul environment variables so the can be eval'led | |
eval `python envsetup.py` | |
/consul/config/server_details | |
Purpose | |
======= | |
Allow easy setup of the appropriate consul environment variables when you are in a hurry to run the following: | |
$ consul members | |
$ consul operator raft list-peers | |
These are the commands that Hashicorp support will ask you to run when troubleshooting an incident. | |
""" | |
__author__ = "David Morris" | |
__version__ = "0.1.0" | |
__license__ = "MIT" | |
import os | |
import sys | |
import json | |
def get_master_token(consul_dir, consul_config='consul-config.json'): | |
"""consul-config.json file contains a value: | |
{ | |
"acl" : { | |
"enabled": true, | |
"default_policy": "deny", | |
"down_policy": "extend-cache", | |
"policy_ttl": "5m", | |
"token_ttl": "8h", | |
"enable_token_replication": true, | |
"tokens": { | |
"master": "99a9b999-099d-9969-9ea9-99e49ea9db9e", | |
"agent_master" : "999d999-ea9c-9c99-9e9f-a99eb999be99" | |
} | |
""" | |
token = False | |
f = os.path.join(consul_dir, 'config', consul_config) | |
if os.path.exists(f): | |
with open(f) as json_file: | |
config = json.load(json_file) | |
token = config["acl"]["tokens"]["master"] | |
return token | |
def main(): | |
consul_dir = '/consul' | |
sd = 'server_details' | |
f = os.path.join(consul_dir, sd) | |
vars_to_set = [] | |
if os.path.exists(f): | |
with open(f) as consul_server_details: | |
for line in consul_server_details: | |
vars_to_set.append("export {0}".format(line)) | |
token = get_master_token(consul_dir) | |
print("export CONSUL_HTTP_TOKEN={0}".format(token)) | |
print(''.join(vars_to_set)) | |
else: | |
print("no file: {0}".format(f)) | |
sys.exit() | |
if __name__ == "__main__": | |
""" This is executed when run from the command line """ | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment