-
Notifications
You must be signed in to change notification settings - Fork 2
Remote File Copy command #37
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8853b3a
Align parts of developer experience with cookiecutter-ntc
smk4664 762866e
Add pyntc remote_file_copy command.
smk4664 0a1188f
Apply suggestions
smk4664 35d085c
Add Verify File and Check File Exists
smk4664 f7b4f74
Apply suggestions from code review
smk4664 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,26 @@ | ||
| ARG PYTHON_VER | ||
| ARG PYTHON_VER="3.10" | ||
|
|
||
| FROM python:${PYTHON_VER}-slim | ||
|
|
||
| RUN apt-get update && \ | ||
| apt-get upgrade -y && \ | ||
| apt-get install --no-install-recommends -y curl gcc python3-dev && \ | ||
| apt-get autoremove -y && \ | ||
| apt-get clean all && \ | ||
| rm -rf /var/lib/apt/lists/* && \ | ||
| pip --no-cache-dir install --upgrade pip wheel | ||
|
|
||
| RUN pip install --upgrade pip | ||
|
|
||
| RUN curl -sSL https://install.python-poetry.org -o /tmp/install-poetry.py && \ | ||
| python /tmp/install-poetry.py --version 1.6.0 && \ | ||
| rm -f /tmp/install-poetry.py | ||
| # Install Poetry manually via its installer script; | ||
| # if we instead used "pip install poetry" it would install its own dependencies globally which may conflict with ours. | ||
| # https://python-poetry.org/docs/master/#installing-with-the-official-installer | ||
| # This also makes it so that Poetry will *not* be included in the "final" image since it's not installed to /usr/local/ | ||
| ARG POETRY_HOME=/opt/poetry | ||
| ARG POETRY_INSTALLER_PARALLEL=true | ||
| ARG POETRY_VERSION=2.1.3 | ||
| ARG POETRY_VIRTUALENVS_CREATE=false | ||
| ADD https://install.python-poetry.org /tmp/install-poetry.py | ||
| RUN python /tmp/install-poetry.py | ||
|
|
||
| # Add poetry install location to the $PATH | ||
| ENV PATH="${PATH}:/root/.local/bin" | ||
| ENV PATH="${POETRY_HOME}/bin:${PATH}" | ||
|
|
||
| WORKDIR /local | ||
| COPY pyproject.toml poetry.lock /local/ | ||
| RUN poetry config virtualenvs.create ${POETRY_VIRTUALENVS_CREATE} && \ | ||
| poetry config installer.parallel "${POETRY_INSTALLER_PARALLEL}" | ||
|
|
||
| RUN poetry config virtualenvs.create false \ | ||
| && poetry install --no-interaction --no-ansi | ||
| WORKDIR /local | ||
| COPY . /local | ||
|
|
||
| # Do not break dependency caching before installing project | ||
| COPY . . | ||
| RUN poetry install | ||
| # Install the app | ||
| RUN poetry install --with dev --all-extras |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| """Copy file to device.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from nornir.core.task import Result, Task | ||
| from nornir_pyntc.connections import CONNECTION_NAME | ||
|
|
||
|
|
||
| def pyntc_check_file_exists(task: Task, filename: str, **kwargs: Any) -> Result: | ||
| """Check if file exists on device. | ||
|
|
||
| Args: | ||
| task (Task): Nornir Task object. | ||
| filename (str): Name of the file. | ||
| kwargs (Any): Additional keyword args. | ||
|
|
||
| Returns: | ||
| Result object with: | ||
| * (bool): True if file exists, False otherwise. | ||
| """ | ||
| pyntc_connection = task.host.get_connection(CONNECTION_NAME, task.nornir.config) | ||
| result = pyntc_connection.check_file_exists(filename, **kwargs) | ||
| return Result(host=task.host, result=result, changed=False) | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| """Send configuration commands.""" | ||
|
|
||
| from typing import List | ||
|
|
||
| from nornir.core.task import Result, Task | ||
|
|
||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| """Copy file to device.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from nornir.core.task import Result, Task | ||
|
|
||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| """Installs the prescribed Network OS.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from nornir.core.task import Result, Task | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """Copy file to device, using the device's copy command.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from nornir.core.task import Result, Task | ||
| from pyntc.utils.models import FileCopyModel | ||
| from nornir_pyntc.connections import CONNECTION_NAME | ||
|
|
||
|
|
||
| def pyntc_remote_file_copy(task: Task, src: FileCopyModel, **kwargs: Any) -> Result: | ||
| """Execute the file copy command on a remote device using the device's native copy command. | ||
|
|
||
| Args: | ||
| task (Task): Nornir Task object. | ||
| src (FileCopyModel): Source of file. | ||
| kwargs (Any): Additional keyword args. | ||
|
smk4664 marked this conversation as resolved.
|
||
|
|
||
| Returns: | ||
| Result object with: | ||
| * (bool): True if save is successful. | ||
| """ | ||
| pyntc_connection = task.host.get_connection(CONNECTION_NAME, task.nornir.config) | ||
| result = pyntc_connection.remote_file_copy(src=src, **kwargs) | ||
| if result: | ||
| return Result(host=task.host, result=result, changed=True) | ||
| return Result(host=task.host, result=result, changed=False) | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| """Save a device's running configuration.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from nornir.core.task import Result, Task | ||
|
|
||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| """Send a non-configuration command.""" | ||
|
|
||
| from typing import Any, Union | ||
|
|
||
| from nornir.core.task import Result, Task | ||
|
|
||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| """Copy file to device.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from nornir.core.task import Result, Task | ||
| from nornir_pyntc.connections import CONNECTION_NAME | ||
|
|
||
|
|
||
| def pyntc_verify_file( | ||
| task: Task, | ||
| checksum: str, | ||
| filename: str, | ||
| hashing_algorithm: str = "md5", | ||
| **kwargs: Any, | ||
| ) -> Result: | ||
| """Check if file exists on device and the checksum matches the provided value. | ||
|
|
||
| Args: | ||
| task (Task): Nornir Task object. | ||
| checksum (str): Expected checksum of the file. | ||
| filename (str): Name of the file. | ||
| hashing_algorithm (str): Hashing algorithm to use for checksum verification (default: "md5"). | ||
| kwargs (Any): Additional keyword args. | ||
|
smk4664 marked this conversation as resolved.
|
||
|
|
||
| Returns: | ||
| Result object with: | ||
| * (bool): True if file exists and checksum matches, False otherwise. | ||
| """ | ||
| pyntc_connection = task.host.get_connection(CONNECTION_NAME, task.nornir.config) | ||
| result = pyntc_connection.verify_file( | ||
| filename, checksum, hashing_algorithm, **kwargs | ||
| ) | ||
| return Result(host=task.host, result=result, changed=False) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.