|
1 | 1 | import json |
2 | 2 | import os |
3 | | - |
| 3 | +import urllib.parse |
4 | 4 | import boto3 |
5 | 5 | import click |
6 | 6 | from table2ascii import Alignment, table2ascii |
|
9 | 9 | from .bootstrap import BootstrapManager, BootstrapParameters |
10 | 10 | from .redis_config import RedisConfig |
11 | 11 |
|
| 12 | +from http.server import BaseHTTPRequestHandler, HTTPServer |
| 13 | + |
| 14 | + |
12 | 15 | try: |
13 | 16 | from yaml import CDumper as Dumper |
14 | 17 | except ImportError: |
@@ -138,6 +141,90 @@ def get_config( |
138 | 141 | print(output) |
139 | 142 |
|
140 | 143 |
|
| 144 | +@cli.command() |
| 145 | +@click.pass_context |
| 146 | +@click.option("--environment-name", help="The environment name", required=True) |
| 147 | +@click.option("--app-name", help="The app name", required=True) |
| 148 | +@click.option("--prefix", default="/appconfig", help="The path prefix") |
| 149 | +@click.option("--region", default="eu-west-2", help="The AWS region") |
| 150 | +@click.option( |
| 151 | + "--include-common/--ignore-common", |
| 152 | + default=True, |
| 153 | + is_flag=True, |
| 154 | + help="Include shared variables", |
| 155 | +) |
| 156 | +@click.option( |
| 157 | + "--output-format", |
| 158 | + default="json", |
| 159 | + type=click.Choice(["json", "yaml", "environment", "environment-export"]), |
| 160 | +) |
| 161 | +@click.option( |
| 162 | + "--parse-redis-param/--ignore-redis-param", |
| 163 | + default=True, |
| 164 | + is_flag=True, |
| 165 | + help="Parse redis host and allocate a redis database number", |
| 166 | +) |
| 167 | +@click.option( |
| 168 | + "--server-port", |
| 169 | + default="9099" |
| 170 | +) |
| 171 | +def config_server( |
| 172 | + ctx, |
| 173 | + environment_name, |
| 174 | + app_name, |
| 175 | + prefix, |
| 176 | + region, |
| 177 | + include_common, |
| 178 | + output_format, |
| 179 | + parse_redis_param, |
| 180 | + server_port |
| 181 | +): |
| 182 | + bootstrap = BootstrapParameters( |
| 183 | + environment_name=environment_name, |
| 184 | + app_name=app_name, |
| 185 | + prefix=prefix, |
| 186 | + region=region, |
| 187 | + include_common=include_common, |
| 188 | + click=click, |
| 189 | + endpoint_url=ctx.obj["AWS_ENDPOINT_URL"], |
| 190 | + parse_redis=parse_redis_param, |
| 191 | + bucket_name=ctx.obj["BUCKET_NAME"], |
| 192 | + ) |
| 193 | + |
| 194 | + class ConfigServer(BaseHTTPRequestHandler): |
| 195 | + def do_GET(self): |
| 196 | + if output_format == "json": |
| 197 | + # self.send_header("Content-type", "text/json") |
| 198 | + output = json.dumps(bootstrap.get_raw_params(), indent=2) |
| 199 | + elif output_format == "yaml": |
| 200 | + # self.send_header("Content-type", "text/yaml") |
| 201 | + output = dump(bootstrap.get_raw_params(), Dumper=Dumper) |
| 202 | + elif output_format == "environment": |
| 203 | + # self.send_header("Content-type", "text/plain") |
| 204 | + output = bootstrap.params_to_env() |
| 205 | + elif output_format == "environment-export": |
| 206 | + # self.send_header("Content-type", "text/plain") |
| 207 | + output = bootstrap.params_to_env(export=True) |
| 208 | + |
| 209 | + self.send_response(200) |
| 210 | + self.send_header("Content-type", "text/plain") |
| 211 | + self.end_headers() |
| 212 | + for line in output.split("\n"): |
| 213 | + self.wfile.write(bytes(line + "\n", "utf-8")) |
| 214 | + raise KeyboardInterrupt() |
| 215 | + |
| 216 | + |
| 217 | + webServer = HTTPServer(("0.0.0.0", int(server_port)), ConfigServer) |
| 218 | + print("Server started http://%s:%s" % ("0.0.0.0", server_port)) |
| 219 | + try: |
| 220 | + webServer.serve_forever() |
| 221 | + except KeyboardInterrupt: |
| 222 | + pass |
| 223 | + |
| 224 | + webServer.server_close() |
| 225 | + print("Server stopped.") |
| 226 | + |
| 227 | + |
141 | 228 | @cli.command() |
142 | 229 | @click.pass_context |
143 | 230 | @click.option("--prefix", default="/appconfig", help="The path prefix") |
|
0 commit comments