Skip to content

Commit

Permalink
Merge remote-tracking branch 'wainersm/utils_cloudinit_improvements'
Browse files Browse the repository at this point in the history
Signed-off-by: Cleber Rosa <[email protected]>
  • Loading branch information
clebergnu committed Mar 5, 2020
2 parents e8d683c + 5329bad commit e2d8351
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions avocado/utils/cloudinit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
This module can be easily used with :mod:`avocado.utils.vmimage`,
to configure operating system images via the cloudinit tooling.
:see: http://cloudinit.readthedocs.io.
"""

from http.server import HTTPServer
Expand All @@ -27,6 +29,7 @@


#: The meta-data file template
#:
#: Positional template variables are: instance-id, hostname
METADATA_TEMPLATE = """instance-id: {0}
hostname: {1}
Expand All @@ -36,6 +39,7 @@
USERDATA_HEADER = "#cloud-config"

#: A username configuration as per cloudinit/config/cc_set_passwords.py
#:
#: Positional template variables : username
USERNAME_TEMPLATE = """
ssh_pwauth: True
Expand All @@ -46,20 +50,24 @@
"""

#: A username configuration as per cloudinit/config/cc_set_passwords.py
#: Positional template variables : password
#:
#: Positional template variables are: password
PASSWORD_TEMPLATE = """
password: {0}
chpasswd:
expire: False
"""

#: An authorized key configuration for the default user
#:
#: Positional template variables are: ssh_authorized_keys
AUTHORIZED_KEY_TEMPLATE = """
ssh_authorized_keys:
- {0}
"""

#: A phone home configuration that will post just the instance id
#:
#: Positional template variables are: address, port
PHONE_HOME_TEMPLATE = """
phone_home:
Expand Down Expand Up @@ -122,8 +130,13 @@ def iso(output_path, instance_id, username=None, password=None,


class PhoneHomeServerHandler(BaseHTTPRequestHandler):
"""Handles HTTP requests to the phone home server."""

def do_POST(self):
"""Handles an HTTP POST request.
Respond with status 200 if the instance phoned back.
"""
path = self.path[1:]
if path[-1] == '/':
path = path[:-1]
Expand All @@ -132,12 +145,28 @@ def do_POST(self):
self.send_response(200)

def log_message(self, format_, *args): # pylint: disable=W0221
pass
"""Logs an arbitrary message.
:note: It currently disables any message logging.
"""


class PhoneHomeServer(HTTPServer):
"""Implements the phone home HTTP server.
Wait the phone home from a given instance.
"""

def __init__(self, address, instance_id):
"""Initialize the server.
:param address: a hostname or IP address and port, in the same format
given to socket and other servers
:type address: tuple
:param instance_id: the identification for the instance that should be
calling back, and the condition for the wait to end
:type instance_id: str
"""
HTTPServer.__init__(self, address, PhoneHomeServerHandler)
self.instance_id = instance_id
self.instance_phoned_back = False
Expand Down

0 comments on commit e2d8351

Please sign in to comment.