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

feat(eos_downloader): Implement a command to download package with their remote path #120

Merged
merged 4 commits into from
Feb 6, 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
38 changes: 38 additions & 0 deletions docs/usage/path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Download any arista package

This command gives an option for advanced users to download any packages available on arista website and using the standard authentication mechanism.

!!! warning
This command is for advanced user only

```bash
# Get a package from arista server
ardl get path -s "/support/path/to/docker/image/cEOS-lab-4.32.3M.tar.xz"

# Get a package from arista server and import into your docker engine
ardl get path -s "/support/path/to/docker/image/cEOS-lab-4.32.3M.tar.xz" --import-docker

# Get a package from arista server and import into your docker engine with specific image and version
ardl get path -s "/support/path/to/docker/image/cEOS-lab-4.32.3M.tar.xz" --import-docker --docker-image arista/myceos --docker-version 4.32.3M
```

## ardl get path options

```bash
$ ardl get path --help
Usage: ardl get path [OPTIONS]

Download image from Arista server using direct path.

Options:
-s, --source TEXT Image path to download from Arista Website
-o, --output PATH Path to save downloaded package [env var:
ARISTA_GET_PATH_OUTPUT; default: .]
--import-docker Import docker image to local docker [env var:
ARISTA_GET_PATH_IMPORT_DOCKER]
--docker-name TEXT Docker image name [env var:
ARISTA_GET_PATH_DOCKER_NAME; default: arista/ceos:raw]
--docker-tag TEXT Docker image tag [env var: ARISTA_GET_PATH_DOCKER_TAG;
default: dev]
--help Show this message and exit.
```
1 change: 1 addition & 0 deletions eos_downloader/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def cli() -> None:
# Load group commands for get
get.add_command(get_commands.eos)
get.add_command(get_commands.cvp)
get.add_command(get_commands.path)

# Debug
debug.add_command(debug_commands.xml)
Expand Down
117 changes: 117 additions & 0 deletions eos_downloader/cli/get/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import click
from eos_downloader.models.data import RTYPE_FEATURE
from eos_downloader.logics.download import SoftManager
from eos_downloader.logics.arista_server import AristaServer
from eos_downloader.logics.arista_xml_server import (
EosXmlObject,
AristaXmlQuerier,
Expand Down Expand Up @@ -267,3 +268,119 @@ def cvp(

console.print(f"CVP file is saved under: {output}")
return 0


@click.command()
@click.option(
"--source",
"-s",
help="Image path to download from Arista Website",
type=str,
show_default=False,
show_envvar=False,
)
@click.option(
"--output",
"-o",
default=str(os.path.relpath(os.getcwd(), start=os.curdir)),
help="Path to save downloaded package",
type=click.Path(),
show_default=True,
show_envvar=True,
)
@click.option(
"--import-docker",
is_flag=True,
help="Import docker image to local docker",
default=False,
show_envvar=True,
)
@click.option(
"--docker-name",
default="arista/ceos:raw",
help="Docker image name",
show_default=True,
show_envvar=True,
)
@click.option(
"--docker-tag",
default="dev",
help="Docker image tag",
show_default=True,
show_envvar=True,
)
@click.pass_context
# pylint: disable=too-many-branches
def path(
ctx: click.Context,
output: str,
source: str,
import_docker: bool,
docker_name: str,
docker_tag: str,
) -> int:
"""Download image from Arista server using direct path."""
console, token, debug, log_level = initialize(ctx)

if source is None:
console.print("[red]Source is not set correctly ![/red]")
return 1

filename = os.path.basename(source)

console.print(f"Downloading file {filename} from source: {source}")
console.print(f"Saving file to: {output}")

ar_server = AristaServer(token=token)

try:
file_url = ar_server.get_url(source)
if log_level == "debug":
console.print(f"URL to download file is: {file_url}")
except Exception as e:
if debug:
console.print_exception(show_locals=True)
else:
console.print(f"\n[red]Exception raised: {e}[/red]")
return 1

if file_url is None:
console.print("File URL is set to None when we expect a string")
return 1

cli = SoftManager(dry_run=False)

try:
cli.download_file(file_url, output, filename=filename)
except Exception as e:
if debug:
console.print_exception(show_locals=True)
else:
console.print(f"\n[red]Exception raised: {e}[/red]")
return 1

if import_docker:
console.print(
f"Importing docker image [green]{docker_name}:{docker_tag}[/green] from [blue]{os.path.join(output, filename)}[/blue]..."
)

try:
cli.import_docker(
local_file_path=os.path.join(output, filename),
docker_name=docker_name,
docker_tag=docker_tag,
)
except FileNotFoundError:
if debug:
console.print_exception(show_locals=True)
else:
console.print(
f"\n[red]File not found: {os.path.join(output, filename)}[/red]"
)
return 1

console.print(
f"Docker image imported successfully: [green]{docker_name}:{docker_tag}[/green]"
)

return 0
2 changes: 1 addition & 1 deletion eos_downloader/cli/get/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def download_files(
"""

console.print(
f"Starting download for EOS version [green]{arista_dl_obj}[/green] for [blue]{format}[/blue] format."
f"Starting download for EOS version [green]{arista_dl_obj.version}[/green] for [blue]{arista_dl_obj.image_type}[/blue] format."
)
cli.downloads(arista_dl_obj, file_path=output, rich_interface=rich_interface)
try:
Expand Down
4 changes: 2 additions & 2 deletions eos_downloader/logics/arista_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def get_xml_data(self) -> Union[ET.ElementTree, None]:

logging.info(f"Getting XML data from server {self._session_server}")
if self._session_id is None:
logger.debug("Not authenticated to server, start authentication process")
logging.debug("Not authenticated to server, start authentication process")
self.authenticate()
jsonpost = {"sessionCode": self._session_id}
result = requests.post(
Expand Down Expand Up @@ -255,7 +255,7 @@ def get_url(self, remote_file_path: str) -> Union[str, None]:

logging.info(f"Getting download URL for {remote_file_path}")
if self._session_id is None:
logger.debug("Not authenticated to server, start authentication process")
logging.debug("Not authenticated to server, start authentication process")
self.authenticate()
jsonpost = {"sessionCode": self._session_id, "filePath": remote_file_path}
result = requests.post(
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ nav:
- Usage:
- Get EOS package: usage/eos.md
- Get CVP package: usage/cvp.md
- Get Any package: usage/path.md
- Environment variables: usage/environment.md
- Version information: usage/info.md
- Software mapping: usage/mapping.md
Expand Down