From 9568dda9227da226fae9535d041694e972283da8 Mon Sep 17 00:00:00 2001 From: Dax Mickelson Date: Tue, 21 Jan 2020 10:16:30 -0700 Subject: [PATCH 01/11] Ignore PyCharm settings. --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index c0c219be..50eaac99 100644 --- a/.gitignore +++ b/.gitignore @@ -122,3 +122,5 @@ dmypy.json # Pyre type checker .pyre/ + +.idea/ \ No newline at end of file From efdcb128732c80a7bab35ea53ece7ee10194759a Mon Sep 17 00:00:00 2001 From: Dax Mickelson Date: Wed, 29 Jan 2020 06:59:35 -0700 Subject: [PATCH 02/11] Docker image to run dnacentersdk. --- docker/Dockerfile | 16 ++++++ docker/bootstrap.py | 33 +++++++++++ .../example_scripts/dnacentersdk_example.py | 56 +++++++++++++++++++ docker/example_scripts/hello_world.py | 1 + docker/example_scripts/hello_world_reply.py | 1 + docker/requirements.txt | 2 + 6 files changed, 109 insertions(+) create mode 100644 docker/Dockerfile create mode 100644 docker/bootstrap.py create mode 100755 docker/example_scripts/dnacentersdk_example.py create mode 100755 docker/example_scripts/hello_world.py create mode 100755 docker/example_scripts/hello_world_reply.py create mode 100644 docker/requirements.txt diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 00000000..f0cfd342 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,16 @@ +FROM dmickels/dnacentersdk:latest +MAINTAINER Dax Mickelson (dmickels@cisco.com) + +# Configure Script and GIT variables +ENV WORK_DIR /usr/src/app +WORKDIR $WORK_DIR +ENV PYTHON_SCRIPT bootstrap.py + +# Provide some examples in case the user doesn't mount their own script dir. +COPY example_scripts $WORK_DIR + +# Install Python modules needed for this script. +RUN python3 -m pip install --no-cache-dir -r $SCRIPT_PATH/requirements.txt + +# Run script. +CMD python3 $WORK_DIR/$PYTHON_SCRIPT diff --git a/docker/bootstrap.py b/docker/bootstrap.py new file mode 100644 index 00000000..b6ea5cfc --- /dev/null +++ b/docker/bootstrap.py @@ -0,0 +1,33 @@ +"""List any possible scripts to run otherwise exit(0)""" +import os + +# Where are the scripts residing? (Should be /usr/src/app/scripts that is mounted at Docker run.) +script_dir = "/usr/src/app/scripts" +if not os.path.isdir(script_dir): + script_dir = os.path.join(os.getcwd(), "example_scripts") +files_in_dir = os.listdir(script_dir) + +list_of_scripts = [] +# Ensure that we only show the python files. (Those ending in .py) +for file in files_in_dir: + if file[-3:] == ".py": + list_of_scripts.append(file) + +# Sort list alphabetically by filename. +list_of_scripts.sort() +if list_of_scripts: + # Display a menu + print("") + print("Select which Python script to run:") + for counter, file in enumerate(list_of_scripts): + print(f'\t{counter}: {file}') + qty_of_scripts = len(list_of_scripts) + try: + choice = int(input(f'Select 0-{qty_of_scripts - 1}: ')) + if 0 <= choice <= qty_of_scripts: + # A valid choice has been made. + os.system(f"/usr/bin/python3 {os.path.join(script_dir, list_of_scripts[choice])}") + except ValueError: + print("Inputted value out of range.") +else: + print(f"There were no Python scripts in {script_dir}.") diff --git a/docker/example_scripts/dnacentersdk_example.py b/docker/example_scripts/dnacentersdk_example.py new file mode 100755 index 00000000..4769285f --- /dev/null +++ b/docker/example_scripts/dnacentersdk_example.py @@ -0,0 +1,56 @@ +"""Testing the use of dnacentersdk Python package.""" +from dnacentersdk import DNACenterAPI +import requests +from requests.packages.urllib3.exceptions import InsecureRequestWarning + +# Disable annoying HTTP warnings +requests.packages.urllib3.disable_warnings(InsecureRequestWarning) + +which_dnac = "devnet" +devnet = { + "debug": False, + "username": "devnetuser", + "password": "Cisco123!", + "base_url": "https://sandboxdnac2.cisco.com:443", + "public_cert": False, +} +pov = { + "debug": True, + "username": "admin", + "password": "C1sco12345", + "base_url": "https://100.64.0.101:443", + "public_cert": False, +} + +if which_dnac == "devnet": + api = DNACenterAPI( + username=devnet["username"], + password=devnet["password"], + base_url=devnet["base_url"], + verify=devnet["public_cert"], + debug=devnet["debug"], + ) +elif which_dnac == "pov": + api = DNACenterAPI( + username=pov["username"], + password=devnet["password"], + base_url=devnet["base_url"], + verify=devnet["public_cert"], + debug=devnet["debug"], + ) +else: + print("No DNA Center connection information provided. Exiting.") + exit(0) + +results = {} + +print(api.devices.get_device_list()) # Permissions issue + +''' +results["workflows"] = api.pnp.get_workflows() +results["client_health"] = api.clients.get_overall_client_health() +results["sites"] = api.sites.get_site() +results["all_device_configs"] = api.devices.get_device_config_for_all_devices() +''' + +print(results) diff --git a/docker/example_scripts/hello_world.py b/docker/example_scripts/hello_world.py new file mode 100755 index 00000000..8e235769 --- /dev/null +++ b/docker/example_scripts/hello_world.py @@ -0,0 +1 @@ +print("Hello World") \ No newline at end of file diff --git a/docker/example_scripts/hello_world_reply.py b/docker/example_scripts/hello_world_reply.py new file mode 100755 index 00000000..3401075f --- /dev/null +++ b/docker/example_scripts/hello_world_reply.py @@ -0,0 +1 @@ +print("Well, hello right back at you!") \ No newline at end of file diff --git a/docker/requirements.txt b/docker/requirements.txt new file mode 100644 index 00000000..10cf1c62 --- /dev/null +++ b/docker/requirements.txt @@ -0,0 +1,2 @@ +dnacentersdk +requests \ No newline at end of file From db8bc488faf1be72cb71e009496e6e4bab012b85 Mon Sep 17 00:00:00 2001 From: Dax Mickelson Date: Wed, 29 Jan 2020 07:00:24 -0700 Subject: [PATCH 03/11] Remove PoV references --- docker/example_scripts/dnacentersdk_example.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/docker/example_scripts/dnacentersdk_example.py b/docker/example_scripts/dnacentersdk_example.py index 4769285f..3fd1c456 100755 --- a/docker/example_scripts/dnacentersdk_example.py +++ b/docker/example_scripts/dnacentersdk_example.py @@ -14,14 +14,6 @@ "base_url": "https://sandboxdnac2.cisco.com:443", "public_cert": False, } -pov = { - "debug": True, - "username": "admin", - "password": "C1sco12345", - "base_url": "https://100.64.0.101:443", - "public_cert": False, -} - if which_dnac == "devnet": api = DNACenterAPI( username=devnet["username"], @@ -30,14 +22,6 @@ verify=devnet["public_cert"], debug=devnet["debug"], ) -elif which_dnac == "pov": - api = DNACenterAPI( - username=pov["username"], - password=devnet["password"], - base_url=devnet["base_url"], - verify=devnet["public_cert"], - debug=devnet["debug"], - ) else: print("No DNA Center connection information provided. Exiting.") exit(0) From 8e9ade3eb4a713d74f9acddbd51f007efd329132 Mon Sep 17 00:00:00 2001 From: Dax Mickelson Date: Wed, 29 Jan 2020 07:02:34 -0700 Subject: [PATCH 04/11] Update example script --- docker/example_scripts/dnacentersdk_example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/example_scripts/dnacentersdk_example.py b/docker/example_scripts/dnacentersdk_example.py index 3fd1c456..cb82f588 100755 --- a/docker/example_scripts/dnacentersdk_example.py +++ b/docker/example_scripts/dnacentersdk_example.py @@ -28,10 +28,10 @@ results = {} -print(api.devices.get_device_list()) # Permissions issue +# print(api.devices.get_device_list()) # Permissions issue -''' results["workflows"] = api.pnp.get_workflows() +''' results["client_health"] = api.clients.get_overall_client_health() results["sites"] = api.sites.get_site() results["all_device_configs"] = api.devices.get_device_config_for_all_devices() From 139c8d0d94cd2df5c04b39a5f3bcaaf28ce12844 Mon Sep 17 00:00:00 2001 From: Dax Mickelson Date: Wed, 29 Jan 2020 07:08:00 -0700 Subject: [PATCH 05/11] Update Dockerfile --- docker/Dockerfile | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index f0cfd342..116dbed0 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,4 +1,4 @@ -FROM dmickels/dnacentersdk:latest +FROM ubuntu:18.04 MAINTAINER Dax Mickelson (dmickels@cisco.com) # Configure Script and GIT variables @@ -6,11 +6,33 @@ ENV WORK_DIR /usr/src/app WORKDIR $WORK_DIR ENV PYTHON_SCRIPT bootstrap.py -# Provide some examples in case the user doesn't mount their own script dir. -COPY example_scripts $WORK_DIR +# Running APT UPDATE +RUN apt-get -y update + +# Install APT-UTILS +RUN apt-get install -y apt-utils + +# Running APT DIST-UPGRADE +RUN apt-get -y dist-upgrade + +# Running APT AUTOREMOVE +RUN apt-get -y autoremove + +# Running APT AUTOCLEAN +RUN apt-get -y autoclean + +# Install Python modules +RUN apt-get -y install python3 python3-pip + +# Install git +RUN apt-get install -y git # Install Python modules needed for this script. -RUN python3 -m pip install --no-cache-dir -r $SCRIPT_PATH/requirements.txt +COPY requirements.txt $WORK_DIR +RUN python3 -m pip install --no-cache-dir -r $WORK_DIR/requirements.txt + +# Provide some examples in case the user doesn't mount their own script dir. +COPY example_scripts $WORK_DIR # Run script. CMD python3 $WORK_DIR/$PYTHON_SCRIPT From 2bd151fbd0fd653927649bd3794f25ff17021948 Mon Sep 17 00:00:00 2001 From: Dax Mickelson Date: Wed, 29 Jan 2020 08:17:56 -0700 Subject: [PATCH 06/11] copy in bootstrap.py file --- docker/Dockerfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docker/Dockerfile b/docker/Dockerfile index 116dbed0..fc650b02 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -34,5 +34,8 @@ RUN python3 -m pip install --no-cache-dir -r $WORK_DIR/requirements.txt # Provide some examples in case the user doesn't mount their own script dir. COPY example_scripts $WORK_DIR +# Copy over bootstrap file +COPY bootstrap.py $WORK_DIR + # Run script. CMD python3 $WORK_DIR/$PYTHON_SCRIPT From 73bfe4c8dd564cdb6519bdaf2f8a600f6de6780b Mon Sep 17 00:00:00 2001 From: Dax Mickelson Date: Wed, 29 Jan 2020 08:37:28 -0700 Subject: [PATCH 07/11] fix copy of example_scripts dir --- docker/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index fc650b02..9fb963e6 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -32,7 +32,7 @@ COPY requirements.txt $WORK_DIR RUN python3 -m pip install --no-cache-dir -r $WORK_DIR/requirements.txt # Provide some examples in case the user doesn't mount their own script dir. -COPY example_scripts $WORK_DIR +COPY example_scripts/ $WORK_DIR/example_scripts # Copy over bootstrap file COPY bootstrap.py $WORK_DIR From 29132d147cd5ea32dded0a861a4c6c65f0171e96 Mon Sep 17 00:00:00 2001 From: Dax Mickelson Date: Wed, 29 Jan 2020 08:43:43 -0700 Subject: [PATCH 08/11] can't do math --- docker/bootstrap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/bootstrap.py b/docker/bootstrap.py index b6ea5cfc..b49fd5cf 100644 --- a/docker/bootstrap.py +++ b/docker/bootstrap.py @@ -24,7 +24,7 @@ qty_of_scripts = len(list_of_scripts) try: choice = int(input(f'Select 0-{qty_of_scripts - 1}: ')) - if 0 <= choice <= qty_of_scripts: + if 0 <= choice <= qty_of_scripts - 1: # A valid choice has been made. os.system(f"/usr/bin/python3 {os.path.join(script_dir, list_of_scripts[choice])}") except ValueError: From fbc69876e8aab3d0e5ace61e59efc442b27064fd Mon Sep 17 00:00:00 2001 From: Dax Mickelson Date: Thu, 30 Jan 2020 12:06:20 -0700 Subject: [PATCH 09/11] Improving menu logic --- docker/bootstrap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/bootstrap.py b/docker/bootstrap.py index b49fd5cf..a5338827 100644 --- a/docker/bootstrap.py +++ b/docker/bootstrap.py @@ -24,7 +24,7 @@ qty_of_scripts = len(list_of_scripts) try: choice = int(input(f'Select 0-{qty_of_scripts - 1}: ')) - if 0 <= choice <= qty_of_scripts - 1: + if 0 <= choice < qty_of_scripts - 1: # A valid choice has been made. os.system(f"/usr/bin/python3 {os.path.join(script_dir, list_of_scripts[choice])}") except ValueError: From f1398da677162e642f1495d29f6524b7a3804831 Mon Sep 17 00:00:00 2001 From: Dax Mickelson Date: Fri, 21 Feb 2020 09:09:14 -0700 Subject: [PATCH 10/11] Improving menu logic and chosen file execution. --- docker/bootstrap.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/docker/bootstrap.py b/docker/bootstrap.py index a5338827..1d4383f5 100644 --- a/docker/bootstrap.py +++ b/docker/bootstrap.py @@ -9,8 +9,9 @@ list_of_scripts = [] # Ensure that we only show the python files. (Those ending in .py) +# Exclude any files that start with "__". This way you can have local "helper" scripts to import. for file in files_in_dir: - if file[-3:] == ".py": + if file[-3:] == ".py" and "__" not in file: list_of_scripts.append(file) # Sort list alphabetically by filename. @@ -26,7 +27,14 @@ choice = int(input(f'Select 0-{qty_of_scripts - 1}: ')) if 0 <= choice < qty_of_scripts - 1: # A valid choice has been made. - os.system(f"/usr/bin/python3 {os.path.join(script_dir, list_of_scripts[choice])}") + os.chdir(script_dir) + os.system( + "/usr/bin/python3 {}".format( + os.path.join(script_dir, list_of_scripts[choice].replace(" ", "\ ")) + ) + ) + else: + print("Inputted value out of range.") except ValueError: print("Inputted value out of range.") else: From 5a9b2c955bb7aae6e0e48d80b136f83b53a982d7 Mon Sep 17 00:00:00 2001 From: Dax Mickelson Date: Fri, 21 Feb 2020 09:12:59 -0700 Subject: [PATCH 11/11] replace hard coded script name to variable name --- docker/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 9fb963e6..c271b2ac 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -35,7 +35,7 @@ RUN python3 -m pip install --no-cache-dir -r $WORK_DIR/requirements.txt COPY example_scripts/ $WORK_DIR/example_scripts # Copy over bootstrap file -COPY bootstrap.py $WORK_DIR +COPY $PYTHON_SCRIPT $WORK_DIR # Run script. CMD python3 $WORK_DIR/$PYTHON_SCRIPT