Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: copy SSH keys at the end of zapper_iot provisioning #446

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import json
import logging
import subprocess
from abc import ABC, abstractmethod
from typing import Any, Dict, Tuple

Expand Down Expand Up @@ -103,5 +104,35 @@ def _run(self, zapper_ip, *args, **kwargs):
**kwargs,
)

def _copy_ssh_id(self):
"""Copy the ssh id to the device"""

logger.info("Copying the agent's SSH public key to the DUT.")

try:
test_username = self.job_data.get("test_data", {}).get(
"test_username", "ubuntu"
)
test_password = self.job_data.get("test_data", {}).get(
"test_password", "ubuntu"
)
except AttributeError:
test_username = "ubuntu"
test_password = "ubuntu"

cmd = [
"sshpass",
"-p",
test_password,
"ssh-copy-id",
"-o",
"StrictHostKeyChecking=no",
"-o",
"UserKnownHostsFile=/dev/null",
f"{test_username}@{self.config['device_ip']}",
]
subprocess.check_call(cmd, timeout=60)

@abstractmethod
def _post_run_actions(self, args):
"""Run further actions after Zapper API returns successfully."""
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class MockConnector(ZapperConnector):
def _validate_configuration(self):
return (), {}

def _post_run_actions(self, args):
pass


class ZapperConnectorTests(unittest.TestCase):
"""Unit tests for ZapperConnector class."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,8 @@ def _validate_configuration(
provisioning_data["urls"] = urls

return ((), provisioning_data)

def _post_run_actions(self, args):
"""Run further actions after Zapper API returns successfully."""

self._copy_ssh_id()
Original file line number Diff line number Diff line change
Expand Up @@ -185,35 +185,6 @@ def _run_oem_script(self, args):

oemscript.provision()

def _copy_ssh_id(self):
"""Copy the ssh id to the device"""

logger.info("Copying the agent's SSH public key to the DUT.")

try:
test_username = self.job_data.get("test_data", {}).get(
"test_username", "ubuntu"
)
test_password = self.job_data.get("test_data", {}).get(
"test_password", "ubuntu"
)
except AttributeError:
test_username = "ubuntu"
test_password = "ubuntu"

cmd = [
"sshpass",
"-p",
test_password,
"ssh-copy-id",
"-o",
"StrictHostKeyChecking=no",
"-o",
"UserKnownHostsFile=/dev/null",
f"{test_username}@{self.config['device_ip']}",
]
subprocess.check_output(cmd, stderr=subprocess.STDOUT, timeout=60)

def _change_password(self, username, orig_password):
"""Change password via SSH to the one specified in the job data."""

Expand Down