summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorScott Moser <smoser@brickies.net>2017-01-25 14:55:38 -0500
committerScott Moser <smoser@brickies.net>2017-01-25 14:55:38 -0500
commitdc6e7b49bac8b87a38fe57ee621177a8177fa2c0 (patch)
tree84a51bea344d4f2d9bbece0e45453ce8a0053785 /tools
parent65e01b463cee0bdb8c8b415e78abfcc3262aad89 (diff)
downloadvyos-cloud-init-dc6e7b49bac8b87a38fe57ee621177a8177fa2c0.tar.gz
vyos-cloud-init-dc6e7b49bac8b87a38fe57ee621177a8177fa2c0.zip
tools/mock-meta: support python2 or python3 and ipv6 in both.
Fix mock-meta to work with python2 or python3. Additionally, it will now listen to ipv6 connections, where previously it would only work with ipv4.
Diffstat (limited to 'tools')
-rwxr-xr-xtools/mock-meta.py32
1 files changed, 21 insertions, 11 deletions
diff --git a/tools/mock-meta.py b/tools/mock-meta.py
index d74f9e31..a0d99441 100755
--- a/tools/mock-meta.py
+++ b/tools/mock-meta.py
@@ -18,10 +18,10 @@ Then:
"""
import functools
-import httplib
import json
import logging
import os
+import socket
import random
import string
import sys
@@ -29,7 +29,13 @@ import yaml
from optparse import OptionParser
-from BaseHTTPServer import (HTTPServer, BaseHTTPRequestHandler)
+try:
+ from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
+ import httplib as hclient
+except ImportError:
+ from http.server import HTTPServer, BaseHTTPRequestHandler
+ from http import client as hclient
+
log = logging.getLogger('meta-server')
@@ -183,6 +189,10 @@ def get_ssh_keys():
return keys
+class HTTPServerV6(HTTPServer):
+ address_family = socket.AF_INET6
+
+
class MetaDataHandler(object):
def __init__(self, opts):
@@ -250,7 +260,7 @@ class MetaDataHandler(object):
key_id = int(mybe_key)
key_name = key_ids[key_id]
except:
- raise WebException(httplib.BAD_REQUEST,
+ raise WebException(hclient.BAD_REQUEST,
"Unknown key id %r" % mybe_key)
# Extract the possible sub-params
result = traverse(nparams[1:], {
@@ -342,13 +352,13 @@ class Ec2Handler(BaseHTTPRequestHandler):
return self._get_versions
date = segments[0].strip().lower()
if date not in self._get_versions():
- raise WebException(httplib.BAD_REQUEST,
+ raise WebException(hclient.BAD_REQUEST,
"Unknown version format %r" % date)
if len(segments) < 2:
- raise WebException(httplib.BAD_REQUEST, "No action provided")
+ raise WebException(hclient.BAD_REQUEST, "No action provided")
look_name = segments[1].lower()
if look_name not in func_mapping:
- raise WebException(httplib.BAD_REQUEST,
+ raise WebException(hclient.BAD_REQUEST,
"Unknown requested data %r" % look_name)
base_func = func_mapping[look_name]
who = self.address_string()
@@ -371,16 +381,16 @@ class Ec2Handler(BaseHTTPRequestHandler):
data = func()
if not data:
data = ''
- self.send_response(httplib.OK)
+ self.send_response(hclient.OK)
self.send_header("Content-Type", "binary/octet-stream")
self.send_header("Content-Length", len(data))
log.info("Sending data (len=%s):\n%s", len(data),
format_text(data))
self.end_headers()
- self.wfile.write(data)
+ self.wfile.write(data.encode())
except RuntimeError as e:
log.exception("Error somewhere in the server.")
- self.send_error(httplib.INTERNAL_SERVER_ERROR, message=str(e))
+ self.send_error(hclient.INTERNAL_SERVER_ERROR, message=str(e))
except WebException as e:
code = e.code
log.exception(str(e))
@@ -408,7 +418,7 @@ def extract_opts():
help=("port from which to serve traffic"
" (default: %default)"))
parser.add_option("-a", "--addr", dest="address", action="store", type=str,
- default='0.0.0.0', metavar="ADDRESS",
+ default='::', metavar="ADDRESS",
help=("address from which to serve traffic"
" (default: %default)"))
parser.add_option("-f", '--user-data-file', dest='user_data_file',
@@ -444,7 +454,7 @@ def run_server():
setup_fetchers(opts)
log.info("CLI opts: %s", opts)
server_address = (opts['address'], opts['port'])
- server = HTTPServer(server_address, Ec2Handler)
+ server = HTTPServerV6(server_address, Ec2Handler)
sa = server.socket.getsockname()
log.info("Serving ec2 metadata on %s using port %s ...", sa[0], sa[1])
server.serve_forever()