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 diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 00000000..c271b2ac --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,41 @@ +FROM ubuntu:18.04 +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 + +# 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. +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/example_scripts + +# Copy over bootstrap file +COPY $PYTHON_SCRIPT $WORK_DIR + +# Run script. +CMD python3 $WORK_DIR/$PYTHON_SCRIPT diff --git a/docker/bootstrap.py b/docker/bootstrap.py new file mode 100644 index 00000000..1d4383f5 --- /dev/null +++ b/docker/bootstrap.py @@ -0,0 +1,41 @@ +"""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) +# 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" and "__" not in file: + 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 - 1: + # A valid choice has been made. + 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: + 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..cb82f588 --- /dev/null +++ b/docker/example_scripts/dnacentersdk_example.py @@ -0,0 +1,40 @@ +"""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, +} +if which_dnac == "devnet": + api = DNACenterAPI( + username=devnet["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