-
-
Save anonymous/d4cca6f63933997f5cd1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def boot_two_runcommand_delete(self, image, flavor, | |
script, interpreter, username, | |
fixed_network="private", | |
floating_network="public", | |
ip_version=4, port=22, | |
use_floatingip=True, **kwargs): | |
"""Boot two servers, run a script that outputs JSON, delete server. | |
:param script: script to run on the server, must output JSON mapping | |
metric names to values. See sample script below. | |
:param interpreter: The shell interpreter to use when running script | |
:param username: User to SSH to instance as | |
:param fixed_network: Network where instance is part of | |
:param floating_network: External network used to get floating ip from | |
:param ip_version: Version of ip protocol to use for connection | |
:param port: Port to use for SSH connection | |
:param use_floatingip: Whether to associate a floating ip for | |
connection | |
:returns: Dictionary containing two keys, data and errors. Data is JSON | |
data output by the script. Errors is raw data from the | |
script's standard error stream. | |
""" | |
server = None | |
server2 = None | |
floating_ip = None | |
floating_ip2 = None | |
try: | |
server = self._boot_server( | |
self._generate_random_name("rally_novaserver_"), | |
image, flavor, key_name='rally_ssh_key', **kwargs) | |
server2 = self._boot_server( | |
self._generate_random_name("rally_novaserver_"), | |
image, flavor, key_name='rally_ssh_key', **kwargs) | |
self.check_network(server, fixed_network) | |
self.check_network(server2, fixed_network) | |
server_fixed_ip = [ip for ip in server.addresses[fixed_network] if | |
ip["version"] == ip_version][0]["addr"] | |
server2_fixed_ip = [ip for ip in server2.addresses[fixed_network] if | |
ip["version"] == ip_version][0]["addr"] | |
if use_floatingip: | |
floating_ip = self._create_floating_ip(floating_network) | |
self._associate_floating_ip(server, floating_ip) | |
server_ip = floating_ip.ip | |
floating_ip2 = self._create_floating_ip(floating_network) | |
self._associate_floating_ip(server2, floating_ip2) | |
server2_ip = floating_ip2.ip | |
else: | |
server_ip = server_fixed_ip | |
server2_ip = server2_fixed_ip | |
self.run_command(server_ip, port, | |
username, interpreter, script) | |
code, out, err = self.run_command(server2_ip, port, | |
username, "%s -s %s" % (interpreter,server_fixed_ip),script) | |
if code: | |
raise exceptions.ScriptError( | |
"Error running script %(script)s." | |
"Error %(code)s: %(error)s" % { | |
"script": script, | |
"code": code, | |
"error": err | |
}) | |
try: | |
out = json.loads(out) | |
except ValueError as e: | |
raise exceptions.ScriptError( | |
"Script %(script)s did not output valid JSON: %(error)s" % | |
{ | |
"script": script, | |
"error": str(e) | |
} | |
) | |
# Always try to free resources | |
finally: | |
if use_floatingip: | |
self._release_server_floating_ip(server, floating_ip) | |
self._release_server_floating_ip(server2, floating_ip2) | |
if server: | |
self._delete_server(server) | |
self._delete_server(server2) | |
return {"data": out, "errors": err} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment