1
by Mathias Gug
Initial configuration. |
1 |
#!/usr/bin/env python
|
2 |
||
3 |
import logging |
|
4 |
import logging.handlers |
|
5 |
from optparse import OptionParser |
|
6 |
import sys |
|
7 |
import urllib |
|
8 |
||
9 |
s3_bucket_url = "https://mathiaz-puppet-nodes-1.s3.amazonaws.com" |
|
10 |
||
11 |
handler = logging.handlers.SysLogHandler('/dev/log', 'daemon') |
|
12 |
fmt = logging.Formatter('%(filename)s[%(process)d]: %(levelname)s: %(message)s') |
|
13 |
handler.setFormatter(fmt) |
|
14 |
logging.getLogger().addHandler(handler) |
|
15 |
||
16 |
parser = OptionParser() |
|
17 |
parser.add_option("", "--log-level", dest="log_level", default="debug", |
|
18 |
help="log level (as defined in python logging module)") |
|
19 |
(options, args) = parser.parse_args() |
|
20 |
logging.getLogger().setLevel(logging.getLevelName(options.log_level.upper())) |
|
21 |
||
22 |
try: |
|
23 |
node_name = args[0] |
|
24 |
except IndexError: |
|
25 |
logging.error("Need node") |
|
26 |
sys.exit(1) |
|
27 |
||
28 |
u = "%s/%s" % (s3_bucket_url, node_name) |
|
29 |
logging.debug("Checking url %s" % (u)) |
|
30 |
url = urllib.urlopen(u) |
|
31 |
if url.code == 200: |
|
32 |
logging.info("Getting node configuration: %s" % (node_name)) |
|
33 |
node_config = url.read() |
|
34 |
logging.debug("Node configuration (%s): %s" % (node_name, node_config)) |
|
35 |
sys.stdout.write(node_config) |
|
36 |
sys.exit(0) |
|
37 |
elif url.code == 404: |
|
38 |
logging.warning("Unkown node: %s" % (node_name)) |
|
39 |
sys.exit(1) |
|
40 |
||
41 |
else: |
|
42 |
logging.warning("Unkown url code for %s: %s" % (node_name, url.code)) |
|
43 |
sys.exit(0) |