- """Create fresh virtualenv to a directory, install pip requirements."""
- logger.console('Extracting virtualenv, installing requirements.txt '
- 'on {0}'.format(node['host']))
- ssh = SSH()
- ssh.connect(node)
- (ret_code, stdout, stderr) = ssh.exec_command(
- 'cd {0} && rm -rf env && virtualenv env && '
- '. env/bin/activate && '
- 'pip install -r requirements.txt'.format(con.REMOTE_FW_DIR), timeout=100)
- if 0 != ret_code:
- logger.error('Virtualenv creation error: {0}'.format(stdout + stderr))
- raise Exception('Virtualenv setup failed')
+ """Create fresh virtualenv to a directory, install pip requirements.
+
+ Return stdout and stderr of the command,
+ so we see which installs are behaving weird (e.g. attempting download).
+
+ :param node: Node to create virtualenv on.
+ :type node: dict
+ :returns: Stdout and stderr.
+ :rtype: str, str
+ :raises RuntimeError: When failed to setup virtualenv.
+ """
+ logger.console(
+ f"Virtualenv setup including requirements.txt on {node[u'type']} "
+ f"host {node[u'host']}, port {node[u'port']} starts."
+ )
+ cmd = f"cd {con.REMOTE_FW_DIR} && rm -rf env && virtualenv " \
+ f"-p $(which python3) --system-site-packages --never-download env " \
+ f"&& source env/bin/activate && ANSIBLE_SKIP_CONFLICT_CHECK=1 " \
+ f"pip3 install -r requirements.txt"
+ stdout, stderr = exec_cmd_no_error(
+ node, cmd, timeout=300, include_reason=True, export=False,
+ message=f"Failed install at node {node[u'type']} host {node[u'host']}, "
+ f"port {node[u'port']}"
+ )
+ logger.console(
+ f"Virtualenv setup on {node[u'type']} host {node[u'host']}, "
+ f"port {node[u'port']} done."
+ )
+ return stdout, stderr
+
+
+def setup_node(node, tarball, remote_tarball, results=None, logs=None):
+ """Copy a tarball to a node and extract it.
+
+ :param node: A node where the tarball will be copied and extracted.
+ :param tarball: Local path of tarball to be copied.
+ :param remote_tarball: Remote path of the tarball.
+ :param results: A list where to store the result of node setup, optional.
+ :param logs: A list where to store anything that should be logged.
+ :type node: dict
+ :type tarball: str
+ :type remote_tarball: str
+ :type results: list
+ :type logs: list
+ :returns: True - success, False - error
+ :rtype: bool
+ """
+ try:
+ copy_tarball_to_node(tarball, node)
+ extract_tarball_at_node(remote_tarball, node)
+ if node[u"type"] == NodeType.TG:
+ stdout, stderr = create_env_directory_at_node(node)
+ if isinstance(logs, list):
+ logs.append(f"{node[u'host']} Env stdout: {stdout}")
+ logs.append(f"{node[u'host']} Env stderr: {stderr}")
+ except Exception:
+ # any exception must result in result = False
+ # since this runs in a thread and can't be caught anywhere else
+ err_msg = f"Node {node[u'type']} host {node[u'host']}, " \
+ f"port {node[u'port']} setup failed."
+ logger.console(err_msg)
+ if isinstance(logs, list):
+ logs.append(f"{err_msg} Exception: {traceback.format_exc()}")
+ result = False