forked from splunk/splunk-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-env.py
More file actions
112 lines (95 loc) · 3.28 KB
/
build-env.py
File metadata and controls
112 lines (95 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2011-2020 Splunk, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"): you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#!/usr/bin/env python
import sys
import json
import urllib.parse
import os
from pathlib import Path
from string import Template
DEFAULT_CONFIG = {
'host': 'localhost',
'port': '8089',
'username': 'admin',
'password': 'changed!',
'scheme': 'https',
'version': '8.0'
}
DEFAULT_ENV_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '.env')
ENV_TEMPLATE_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'templates/env.template')
# {
# "server_roles": {
# "standalone": [
# {
# "host": "10.224.106.158",
# "ports": {
# "8089/tcp": "10.224.106.158:55759",
# },
# "splunk": {
# "user_roles": {
# "admin": {
# "password": "Chang3d!",
# "username": "admin"
# }
# },
# "version": "8.1.0",
# "web_url": "http://10.224.106.158:55761"
# }
# }
# ]
# }
# }
def build_config(json_string):
try:
spec_config = json.loads(json_string)
server_config = spec_config['server_roles']['standalone'][0]
splunk_config = server_config['splunk']
host, port = parse_hostport(server_config['ports']['8089/tcp'])
return {
'host': host,
'port': port,
'username': splunk_config['user_roles']['admin']['username'],
'password': splunk_config['user_roles']['admin']['password'],
'version': splunk_config['version'],
}
except Exception as e:
raise ValueError('Invalid configuration JSON string') from e
# Source: https://stackoverflow.com/a/53172593
def parse_hostport(host_port):
# urlparse() and urlsplit() insists on absolute URLs starting with "//"
result = urllib.parse.urlsplit('//' + host_port)
return result.hostname, result.port
def run(variable, env_path=None):
# read JSON from input
# parse the JSON
input_config = build_config(variable) if variable else DEFAULT_CONFIG
config = {**DEFAULT_CONFIG, **input_config}
# build a env file
with open(ENV_TEMPLATE_PATH, 'r') as f:
template = Template(f.read())
env_string = template.substitute(config)
env_path = DEFAULT_ENV_PATH if env_path is None else env_path
# if no env, dry-run
if not env_path:
print(env_string)
return
# write the .env file
with open(env_path, 'w') as f:
f.write(env_string)
if sys.stdin.isatty():
DATA = None
else:
DATA = sys.stdin.read()
run(DATA, sys.argv[1] if len(sys.argv) > 1 else None)