From 9b544bbf99101a15f95cdc80840ced49db89e45a Mon Sep 17 00:00:00 2001 From: Patryk Orwat Date: Sun, 25 Oct 2020 22:27:38 +0800 Subject: [PATCH 1/5] #97 implemented generation of empty draw.io file --- Dockerfile | 2 +- cloudiscovery/provider/vpc/diagram.py | 8 +- cloudiscovery/shared/common_aws.py | 12 +- cloudiscovery/shared/diagram.py | 46 ++++ cloudiscovery/shared/diagramsnet.py | 28 ++ .../tests/shared/test_shared_diagram.py | 35 +++ env/bin/Activate.ps1 | 241 ++++++++++++++++++ env/bin/activate | 76 ++++++ env/bin/activate.csh | 37 +++ env/bin/activate.fish | 75 ++++++ env/bin/easy_install | 9 + env/bin/easy_install-3.8 | 9 + env/bin/pip | 9 + env/bin/pip3 | 9 + env/bin/pip3.8 | 9 + env/bin/python | 1 + env/bin/python3 | 1 + env/bin/python3.8 | 1 + env/pyvenv.cfg | 3 + 19 files changed, 599 insertions(+), 12 deletions(-) create mode 100644 cloudiscovery/shared/diagramsnet.py create mode 100644 cloudiscovery/tests/shared/test_shared_diagram.py create mode 100644 env/bin/Activate.ps1 create mode 100644 env/bin/activate create mode 100644 env/bin/activate.csh create mode 100644 env/bin/activate.fish create mode 100755 env/bin/easy_install create mode 100755 env/bin/easy_install-3.8 create mode 100755 env/bin/pip create mode 100755 env/bin/pip3 create mode 100755 env/bin/pip3.8 create mode 120000 env/bin/python create mode 120000 env/bin/python3 create mode 120000 env/bin/python3.8 create mode 100644 env/pyvenv.cfg diff --git a/Dockerfile b/Dockerfile index 02174bd..4f4b8de 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.7-slim as cloudiscovery +FROM python:3.8-slim as cloudiscovery LABEL maintainer_1="https://github.com/leandrodamascena/" LABEL maintainer_2="https://github.com/meshuga" diff --git a/cloudiscovery/provider/vpc/diagram.py b/cloudiscovery/provider/vpc/diagram.py index 5e7b0b7..0a626da 100644 --- a/cloudiscovery/provider/vpc/diagram.py +++ b/cloudiscovery/provider/vpc/diagram.py @@ -1,7 +1,7 @@ from typing import List, Dict, Optional from shared.common import ResourceEdge, Resource, ResourceDigest -from shared.diagram import BaseDiagram, add_resource_to_group +from shared.diagram import add_resource_to_group, DiagramsNetDiagram PUBLIC_SUBNET = "{public subnet}" PRIVATE_SUBNET = "{private subnet}" @@ -95,16 +95,14 @@ def aggregate_asg_groups( add_resource_to_group(groups, "", agg_resource) -class VpcDiagram(BaseDiagram): +class VpcDiagram(DiagramsNetDiagram): def __init__(self, vpc_id: str): """ VPC diagram :param vpc_id: """ - super().__init__( - "sfdp" - ) # Change to fdp and clusters once mingrammer/diagrams#17 is done + super().__init__() self.vpc_id = vpc_id # pylint: disable=too-many-branches diff --git a/cloudiscovery/shared/common_aws.py b/cloudiscovery/shared/common_aws.py index f467622..0385e31 100644 --- a/cloudiscovery/shared/common_aws.py +++ b/cloudiscovery/shared/common_aws.py @@ -323,11 +323,11 @@ def run( lambda data: execute_provider(options, data), providers ) - for provider_results in provider_results: - if provider_results[0] is not None: - all_resources.extend(provider_results[0]) - if provider_results[1] is not None: - resource_relations.extend(provider_results[1]) + for provider_result in provider_results: + if provider_result[0] is not None: + all_resources.extend(provider_result[0]) + if provider_result[1] is not None: + resource_relations.extend(provider_result[1]) unique_resources_dict: Dict[ResourceDigest, Resource] = dict() for resource in all_resources: @@ -368,7 +368,7 @@ def run( report = Report() report.general_report( resources=filtered_resources, resource_relations=filtered_relations - ), + ) report.html_report( resources=filtered_resources, resource_relations=filtered_relations, diff --git a/cloudiscovery/shared/diagram.py b/cloudiscovery/shared/diagram.py index c535970..aa882c2 100644 --- a/cloudiscovery/shared/diagram.py +++ b/cloudiscovery/shared/diagram.py @@ -1,9 +1,12 @@ +import base64 +import zlib from pathlib import Path from typing import List, Dict from diagrams import Diagram, Cluster, Edge from shared.common import Resource, ResourceEdge, ResourceDigest, message_handler +from shared.diagramsnet import DIAGRAM_HEADER, DIAGRAM_SUFFIX, MX_FILE from shared.error_handler import exception PATH_DIAGRAM_OUTPUT = "./assets/diagrams/" @@ -373,3 +376,46 @@ def build( filename: str, ): pass + + +class DiagramsNetDiagram(BaseDiagram): + def generate_diagram( + self, + resources: List[Resource], + initial_resource_relations: List[ResourceEdge], + title: str, + filename: str, + ): + ordered_resources = self.group_by_group(resources, initial_resource_relations) + relations = self.process_relationships( + ordered_resources, initial_resource_relations + ) + diagram = self.build_diagram(ordered_resources, relations) + output_filename = PATH_DIAGRAM_OUTPUT + filename + ".drawio" + + with open(output_filename, "w") as diagram_file: + diagram_file.write(diagram) + + @staticmethod + def decode_inflate(value: str): + decoded = base64.b64decode(value) + try: + result = zlib.decompress(decoded, -15) + # pylint: disable=broad-except + except Exception: + result = decoded + return result.decode("utf-8") + + @staticmethod + def deflate_encode(value: str): + return base64.b64encode(zlib.compress(value.encode("utf-8"))[2:-4]).decode( + "utf-8" + ) + + def build_diagram( + self, + resources: Dict[str, List[Resource]], + resource_relations: List[ResourceEdge], + ): + mx_graph_model = DIAGRAM_HEADER + DIAGRAM_SUFFIX + return MX_FILE.replace("", self.deflate_encode(mx_graph_model)) diff --git a/cloudiscovery/shared/diagramsnet.py b/cloudiscovery/shared/diagramsnet.py new file mode 100644 index 0000000..d65f4ef --- /dev/null +++ b/cloudiscovery/shared/diagramsnet.py @@ -0,0 +1,28 @@ +MX_FILE = """ + + + +""" + +DIAGRAM_HEADER = """ + + + + """ + +DIAGRAM_SUFFIX = """ + +""" + +CELL_TEMPLATE = ( + """ +;resIcon=;" vertex="1" parent="1"> + + +""" +) diff --git a/cloudiscovery/tests/shared/test_shared_diagram.py b/cloudiscovery/tests/shared/test_shared_diagram.py new file mode 100644 index 0000000..08ca76f --- /dev/null +++ b/cloudiscovery/tests/shared/test_shared_diagram.py @@ -0,0 +1,35 @@ +from unittest import TestCase + +from assertpy import assert_that + +from shared.common import Resource, ResourceDigest +from shared.diagram import DiagramsNetDiagram +from shared.diagramsnet import MX_FILE + +INFLATED_XML = "" +DEFLATED_XML = "s6nIzVHQtwMA" + + +class TestDiagramsNetDiagram(TestCase): + sut = DiagramsNetDiagram() + + def test_deflate_encode(self): + result = DiagramsNetDiagram.deflate_encode(INFLATED_XML) + assert_that(result).is_equal_to(DEFLATED_XML) + + def test_decode_inflate(self): + result = DiagramsNetDiagram.decode_inflate(DEFLATED_XML) + assert_that(result).is_equal_to(INFLATED_XML) + + def test_file_generation(self): + general_resources = [ + Resource( + digest=ResourceDigest(id="123", type="type"), + name="name", + details="details", + ) + ] + grouped_resources = {"": general_resources} + relations = [] + result = self.sut.build_diagram(grouped_resources, relations) + assert_that(result).starts_with(MX_FILE[:200]) diff --git a/env/bin/Activate.ps1 b/env/bin/Activate.ps1 new file mode 100644 index 0000000..a3bc6fb --- /dev/null +++ b/env/bin/Activate.ps1 @@ -0,0 +1,241 @@ +<# +.Synopsis +Activate a Python virtual environment for the current PowerShell session. + +.Description +Pushes the python executable for a virtual environment to the front of the +$Env:PATH environment variable and sets the prompt to signify that you are +in a Python virtual environment. Makes use of the command line switches as +well as the `pyvenv.cfg` file values present in the virtual environment. + +.Parameter VenvDir +Path to the directory that contains the virtual environment to activate. The +default value for this is the parent of the directory that the Activate.ps1 +script is located within. + +.Parameter Prompt +The prompt prefix to display when this virtual environment is activated. By +default, this prompt is the name of the virtual environment folder (VenvDir) +surrounded by parentheses and followed by a single space (ie. '(.venv) '). + +.Example +Activate.ps1 +Activates the Python virtual environment that contains the Activate.ps1 script. + +.Example +Activate.ps1 -Verbose +Activates the Python virtual environment that contains the Activate.ps1 script, +and shows extra information about the activation as it executes. + +.Example +Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv +Activates the Python virtual environment located in the specified location. + +.Example +Activate.ps1 -Prompt "MyPython" +Activates the Python virtual environment that contains the Activate.ps1 script, +and prefixes the current prompt with the specified string (surrounded in +parentheses) while the virtual environment is active. + +.Notes +On Windows, it may be required to enable this Activate.ps1 script by setting the +execution policy for the user. You can do this by issuing the following PowerShell +command: + +PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser + +For more information on Execution Policies: +https://go.microsoft.com/fwlink/?LinkID=135170 + +#> +Param( + [Parameter(Mandatory = $false)] + [String] + $VenvDir, + [Parameter(Mandatory = $false)] + [String] + $Prompt +) + +<# Function declarations --------------------------------------------------- #> + +<# +.Synopsis +Remove all shell session elements added by the Activate script, including the +addition of the virtual environment's Python executable from the beginning of +the PATH variable. + +.Parameter NonDestructive +If present, do not remove this function from the global namespace for the +session. + +#> +function global:deactivate ([switch]$NonDestructive) { + # Revert to original values + + # The prior prompt: + if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) { + Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt + Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT + } + + # The prior PYTHONHOME: + if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) { + Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME + Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME + } + + # The prior PATH: + if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) { + Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH + Remove-Item -Path Env:_OLD_VIRTUAL_PATH + } + + # Just remove the VIRTUAL_ENV altogether: + if (Test-Path -Path Env:VIRTUAL_ENV) { + Remove-Item -Path env:VIRTUAL_ENV + } + + # Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether: + if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) { + Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force + } + + # Leave deactivate function in the global namespace if requested: + if (-not $NonDestructive) { + Remove-Item -Path function:deactivate + } +} + +<# +.Description +Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the +given folder, and returns them in a map. + +For each line in the pyvenv.cfg file, if that line can be parsed into exactly +two strings separated by `=` (with any amount of whitespace surrounding the =) +then it is considered a `key = value` line. The left hand string is the key, +the right hand is the value. + +If the value starts with a `'` or a `"` then the first and last character is +stripped from the value before being captured. + +.Parameter ConfigDir +Path to the directory that contains the `pyvenv.cfg` file. +#> +function Get-PyVenvConfig( + [String] + $ConfigDir +) { + Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg" + + # Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue). + $pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue + + # An empty map will be returned if no config file is found. + $pyvenvConfig = @{ } + + if ($pyvenvConfigPath) { + + Write-Verbose "File exists, parse `key = value` lines" + $pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath + + $pyvenvConfigContent | ForEach-Object { + $keyval = $PSItem -split "\s*=\s*", 2 + if ($keyval[0] -and $keyval[1]) { + $val = $keyval[1] + + # Remove extraneous quotations around a string value. + if ("'""".Contains($val.Substring(0, 1))) { + $val = $val.Substring(1, $val.Length - 2) + } + + $pyvenvConfig[$keyval[0]] = $val + Write-Verbose "Adding Key: '$($keyval[0])'='$val'" + } + } + } + return $pyvenvConfig +} + + +<# Begin Activate script --------------------------------------------------- #> + +# Determine the containing directory of this script +$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition +$VenvExecDir = Get-Item -Path $VenvExecPath + +Write-Verbose "Activation script is located in path: '$VenvExecPath'" +Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)" +Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)" + +# Set values required in priority: CmdLine, ConfigFile, Default +# First, get the location of the virtual environment, it might not be +# VenvExecDir if specified on the command line. +if ($VenvDir) { + Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values" +} +else { + Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir." + $VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/") + Write-Verbose "VenvDir=$VenvDir" +} + +# Next, read the `pyvenv.cfg` file to determine any required value such +# as `prompt`. +$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir + +# Next, set the prompt from the command line, or the config file, or +# just use the name of the virtual environment folder. +if ($Prompt) { + Write-Verbose "Prompt specified as argument, using '$Prompt'" +} +else { + Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value" + if ($pyvenvCfg -and $pyvenvCfg['prompt']) { + Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'" + $Prompt = $pyvenvCfg['prompt']; + } + else { + Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virutal environment)" + Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'" + $Prompt = Split-Path -Path $venvDir -Leaf + } +} + +Write-Verbose "Prompt = '$Prompt'" +Write-Verbose "VenvDir='$VenvDir'" + +# Deactivate any currently active virtual environment, but leave the +# deactivate function in place. +deactivate -nondestructive + +# Now set the environment variable VIRTUAL_ENV, used by many tools to determine +# that there is an activated venv. +$env:VIRTUAL_ENV = $VenvDir + +if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) { + + Write-Verbose "Setting prompt to '$Prompt'" + + # Set the prompt to include the env name + # Make sure _OLD_VIRTUAL_PROMPT is global + function global:_OLD_VIRTUAL_PROMPT { "" } + Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT + New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt + + function global:prompt { + Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) " + _OLD_VIRTUAL_PROMPT + } +} + +# Clear PYTHONHOME +if (Test-Path -Path Env:PYTHONHOME) { + Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME + Remove-Item -Path Env:PYTHONHOME +} + +# Add the venv to the PATH +Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH +$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH" diff --git a/env/bin/activate b/env/bin/activate new file mode 100644 index 0000000..5031c86 --- /dev/null +++ b/env/bin/activate @@ -0,0 +1,76 @@ +# This file must be used with "source bin/activate" *from bash* +# you cannot run it directly + +deactivate () { + # reset old environment variables + if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then + PATH="${_OLD_VIRTUAL_PATH:-}" + export PATH + unset _OLD_VIRTUAL_PATH + fi + if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then + PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}" + export PYTHONHOME + unset _OLD_VIRTUAL_PYTHONHOME + fi + + # This should detect bash and zsh, which have a hash command that must + # be called to get it to forget past commands. Without forgetting + # past commands the $PATH changes we made may not be respected + if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then + hash -r + fi + + if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then + PS1="${_OLD_VIRTUAL_PS1:-}" + export PS1 + unset _OLD_VIRTUAL_PS1 + fi + + unset VIRTUAL_ENV + if [ ! "${1:-}" = "nondestructive" ] ; then + # Self destruct! + unset -f deactivate + fi +} + +# unset irrelevant variables +deactivate nondestructive + +VIRTUAL_ENV="/Users/patrykorwat/git/github/cloudiscovery/env" +export VIRTUAL_ENV + +_OLD_VIRTUAL_PATH="$PATH" +PATH="$VIRTUAL_ENV/bin:$PATH" +export PATH + +# unset PYTHONHOME if set +# this will fail if PYTHONHOME is set to the empty string (which is bad anyway) +# could use `if (set -u; : $PYTHONHOME) ;` in bash +if [ -n "${PYTHONHOME:-}" ] ; then + _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}" + unset PYTHONHOME +fi + +if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then + _OLD_VIRTUAL_PS1="${PS1:-}" + if [ "x(env) " != x ] ; then + PS1="(env) ${PS1:-}" + else + if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then + # special case for Aspen magic directories + # see https://aspen.io/ + PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1" + else + PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1" + fi + fi + export PS1 +fi + +# This should detect bash and zsh, which have a hash command that must +# be called to get it to forget past commands. Without forgetting +# past commands the $PATH changes we made may not be respected +if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then + hash -r +fi diff --git a/env/bin/activate.csh b/env/bin/activate.csh new file mode 100644 index 0000000..3658acc --- /dev/null +++ b/env/bin/activate.csh @@ -0,0 +1,37 @@ +# This file must be used with "source bin/activate.csh" *from csh*. +# You cannot run it directly. +# Created by Davide Di Blasi . +# Ported to Python 3.3 venv by Andrew Svetlov + +alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate' + +# Unset irrelevant variables. +deactivate nondestructive + +setenv VIRTUAL_ENV "/Users/patrykorwat/git/github/cloudiscovery/env" + +set _OLD_VIRTUAL_PATH="$PATH" +setenv PATH "$VIRTUAL_ENV/bin:$PATH" + + +set _OLD_VIRTUAL_PROMPT="$prompt" + +if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then + if ("env" != "") then + set env_name = "env" + else + if (`basename "VIRTUAL_ENV"` == "__") then + # special case for Aspen magic directories + # see https://aspen.io/ + set env_name = `basename \`dirname "$VIRTUAL_ENV"\`` + else + set env_name = `basename "$VIRTUAL_ENV"` + endif + endif + set prompt = "[$env_name] $prompt" + unset env_name +endif + +alias pydoc python -m pydoc + +rehash diff --git a/env/bin/activate.fish b/env/bin/activate.fish new file mode 100644 index 0000000..5b2b6fe --- /dev/null +++ b/env/bin/activate.fish @@ -0,0 +1,75 @@ +# This file must be used with ". bin/activate.fish" *from fish* (http://fishshell.org) +# you cannot run it directly + +function deactivate -d "Exit virtualenv and return to normal shell environment" + # reset old environment variables + if test -n "$_OLD_VIRTUAL_PATH" + set -gx PATH $_OLD_VIRTUAL_PATH + set -e _OLD_VIRTUAL_PATH + end + if test -n "$_OLD_VIRTUAL_PYTHONHOME" + set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME + set -e _OLD_VIRTUAL_PYTHONHOME + end + + if test -n "$_OLD_FISH_PROMPT_OVERRIDE" + functions -e fish_prompt + set -e _OLD_FISH_PROMPT_OVERRIDE + functions -c _old_fish_prompt fish_prompt + functions -e _old_fish_prompt + end + + set -e VIRTUAL_ENV + if test "$argv[1]" != "nondestructive" + # Self destruct! + functions -e deactivate + end +end + +# unset irrelevant variables +deactivate nondestructive + +set -gx VIRTUAL_ENV "/Users/patrykorwat/git/github/cloudiscovery/env" + +set -gx _OLD_VIRTUAL_PATH $PATH +set -gx PATH "$VIRTUAL_ENV/bin" $PATH + +# unset PYTHONHOME if set +if set -q PYTHONHOME + set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME + set -e PYTHONHOME +end + +if test -z "$VIRTUAL_ENV_DISABLE_PROMPT" + # fish uses a function instead of an env var to generate the prompt. + + # save the current fish_prompt function as the function _old_fish_prompt + functions -c fish_prompt _old_fish_prompt + + # with the original prompt function renamed, we can override with our own. + function fish_prompt + # Save the return status of the last command + set -l old_status $status + + # Prompt override? + if test -n "(env) " + printf "%s%s" "(env) " (set_color normal) + else + # ...Otherwise, prepend env + set -l _checkbase (basename "$VIRTUAL_ENV") + if test $_checkbase = "__" + # special case for Aspen magic directories + # see https://aspen.io/ + printf "%s[%s]%s " (set_color -b blue white) (basename (dirname "$VIRTUAL_ENV")) (set_color normal) + else + printf "%s(%s)%s" (set_color -b blue white) (basename "$VIRTUAL_ENV") (set_color normal) + end + end + + # Restore the return status of the previous command. + echo "exit $old_status" | . + _old_fish_prompt + end + + set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV" +end diff --git a/env/bin/easy_install b/env/bin/easy_install new file mode 100755 index 0000000..da2c2fe --- /dev/null +++ b/env/bin/easy_install @@ -0,0 +1,9 @@ +#!/Users/patrykorwat/git/github/cloudiscovery/env/bin/python3 +# -*- coding: utf-8 -*- +import re +import sys +from setuptools.command.easy_install import main + +if __name__ == "__main__": + sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + sys.exit(main()) diff --git a/env/bin/easy_install-3.8 b/env/bin/easy_install-3.8 new file mode 100755 index 0000000..da2c2fe --- /dev/null +++ b/env/bin/easy_install-3.8 @@ -0,0 +1,9 @@ +#!/Users/patrykorwat/git/github/cloudiscovery/env/bin/python3 +# -*- coding: utf-8 -*- +import re +import sys +from setuptools.command.easy_install import main + +if __name__ == "__main__": + sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + sys.exit(main()) diff --git a/env/bin/pip b/env/bin/pip new file mode 100755 index 0000000..d58b4cb --- /dev/null +++ b/env/bin/pip @@ -0,0 +1,9 @@ +#!/Users/patrykorwat/git/github/cloudiscovery/env/bin/python3 +# -*- coding: utf-8 -*- +import re +import sys +from pip._internal.cli.main import main + +if __name__ == "__main__": + sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + sys.exit(main()) diff --git a/env/bin/pip3 b/env/bin/pip3 new file mode 100755 index 0000000..d58b4cb --- /dev/null +++ b/env/bin/pip3 @@ -0,0 +1,9 @@ +#!/Users/patrykorwat/git/github/cloudiscovery/env/bin/python3 +# -*- coding: utf-8 -*- +import re +import sys +from pip._internal.cli.main import main + +if __name__ == "__main__": + sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + sys.exit(main()) diff --git a/env/bin/pip3.8 b/env/bin/pip3.8 new file mode 100755 index 0000000..d58b4cb --- /dev/null +++ b/env/bin/pip3.8 @@ -0,0 +1,9 @@ +#!/Users/patrykorwat/git/github/cloudiscovery/env/bin/python3 +# -*- coding: utf-8 -*- +import re +import sys +from pip._internal.cli.main import main + +if __name__ == "__main__": + sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + sys.exit(main()) diff --git a/env/bin/python b/env/bin/python new file mode 120000 index 0000000..b8a0adb --- /dev/null +++ b/env/bin/python @@ -0,0 +1 @@ +python3 \ No newline at end of file diff --git a/env/bin/python3 b/env/bin/python3 new file mode 120000 index 0000000..79ab74b --- /dev/null +++ b/env/bin/python3 @@ -0,0 +1 @@ +/usr/local/bin/python3 \ No newline at end of file diff --git a/env/bin/python3.8 b/env/bin/python3.8 new file mode 120000 index 0000000..2736161 --- /dev/null +++ b/env/bin/python3.8 @@ -0,0 +1 @@ +/usr/local/bin/python3.8 \ No newline at end of file diff --git a/env/pyvenv.cfg b/env/pyvenv.cfg new file mode 100644 index 0000000..4e279fb --- /dev/null +++ b/env/pyvenv.cfg @@ -0,0 +1,3 @@ +home = /usr/local/bin +include-system-site-packages = false +version = 3.8.6 From 35037018d299199880c1b1215d58a63a6a125cef Mon Sep 17 00:00:00 2001 From: Patryk Orwat Date: Sun, 25 Oct 2020 22:30:37 +0800 Subject: [PATCH 2/5] #97 switch to Python 3.8 --- env/bin/Activate.ps1 | 241 --------------------------------------- env/bin/activate | 76 ------------ env/bin/activate.csh | 37 ------ env/bin/activate.fish | 75 ------------ env/bin/easy_install | 9 -- env/bin/easy_install-3.8 | 9 -- env/bin/pip | 9 -- env/bin/pip3 | 9 -- env/bin/pip3.8 | 9 -- env/bin/python | 1 - env/bin/python3 | 1 - env/bin/python3.8 | 1 - env/pyvenv.cfg | 3 - setup.py | 2 +- 14 files changed, 1 insertion(+), 481 deletions(-) delete mode 100644 env/bin/Activate.ps1 delete mode 100644 env/bin/activate delete mode 100644 env/bin/activate.csh delete mode 100644 env/bin/activate.fish delete mode 100755 env/bin/easy_install delete mode 100755 env/bin/easy_install-3.8 delete mode 100755 env/bin/pip delete mode 100755 env/bin/pip3 delete mode 100755 env/bin/pip3.8 delete mode 120000 env/bin/python delete mode 120000 env/bin/python3 delete mode 120000 env/bin/python3.8 delete mode 100644 env/pyvenv.cfg diff --git a/env/bin/Activate.ps1 b/env/bin/Activate.ps1 deleted file mode 100644 index a3bc6fb..0000000 --- a/env/bin/Activate.ps1 +++ /dev/null @@ -1,241 +0,0 @@ -<# -.Synopsis -Activate a Python virtual environment for the current PowerShell session. - -.Description -Pushes the python executable for a virtual environment to the front of the -$Env:PATH environment variable and sets the prompt to signify that you are -in a Python virtual environment. Makes use of the command line switches as -well as the `pyvenv.cfg` file values present in the virtual environment. - -.Parameter VenvDir -Path to the directory that contains the virtual environment to activate. The -default value for this is the parent of the directory that the Activate.ps1 -script is located within. - -.Parameter Prompt -The prompt prefix to display when this virtual environment is activated. By -default, this prompt is the name of the virtual environment folder (VenvDir) -surrounded by parentheses and followed by a single space (ie. '(.venv) '). - -.Example -Activate.ps1 -Activates the Python virtual environment that contains the Activate.ps1 script. - -.Example -Activate.ps1 -Verbose -Activates the Python virtual environment that contains the Activate.ps1 script, -and shows extra information about the activation as it executes. - -.Example -Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv -Activates the Python virtual environment located in the specified location. - -.Example -Activate.ps1 -Prompt "MyPython" -Activates the Python virtual environment that contains the Activate.ps1 script, -and prefixes the current prompt with the specified string (surrounded in -parentheses) while the virtual environment is active. - -.Notes -On Windows, it may be required to enable this Activate.ps1 script by setting the -execution policy for the user. You can do this by issuing the following PowerShell -command: - -PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser - -For more information on Execution Policies: -https://go.microsoft.com/fwlink/?LinkID=135170 - -#> -Param( - [Parameter(Mandatory = $false)] - [String] - $VenvDir, - [Parameter(Mandatory = $false)] - [String] - $Prompt -) - -<# Function declarations --------------------------------------------------- #> - -<# -.Synopsis -Remove all shell session elements added by the Activate script, including the -addition of the virtual environment's Python executable from the beginning of -the PATH variable. - -.Parameter NonDestructive -If present, do not remove this function from the global namespace for the -session. - -#> -function global:deactivate ([switch]$NonDestructive) { - # Revert to original values - - # The prior prompt: - if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) { - Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt - Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT - } - - # The prior PYTHONHOME: - if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) { - Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME - Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME - } - - # The prior PATH: - if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) { - Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH - Remove-Item -Path Env:_OLD_VIRTUAL_PATH - } - - # Just remove the VIRTUAL_ENV altogether: - if (Test-Path -Path Env:VIRTUAL_ENV) { - Remove-Item -Path env:VIRTUAL_ENV - } - - # Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether: - if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) { - Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force - } - - # Leave deactivate function in the global namespace if requested: - if (-not $NonDestructive) { - Remove-Item -Path function:deactivate - } -} - -<# -.Description -Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the -given folder, and returns them in a map. - -For each line in the pyvenv.cfg file, if that line can be parsed into exactly -two strings separated by `=` (with any amount of whitespace surrounding the =) -then it is considered a `key = value` line. The left hand string is the key, -the right hand is the value. - -If the value starts with a `'` or a `"` then the first and last character is -stripped from the value before being captured. - -.Parameter ConfigDir -Path to the directory that contains the `pyvenv.cfg` file. -#> -function Get-PyVenvConfig( - [String] - $ConfigDir -) { - Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg" - - # Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue). - $pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue - - # An empty map will be returned if no config file is found. - $pyvenvConfig = @{ } - - if ($pyvenvConfigPath) { - - Write-Verbose "File exists, parse `key = value` lines" - $pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath - - $pyvenvConfigContent | ForEach-Object { - $keyval = $PSItem -split "\s*=\s*", 2 - if ($keyval[0] -and $keyval[1]) { - $val = $keyval[1] - - # Remove extraneous quotations around a string value. - if ("'""".Contains($val.Substring(0, 1))) { - $val = $val.Substring(1, $val.Length - 2) - } - - $pyvenvConfig[$keyval[0]] = $val - Write-Verbose "Adding Key: '$($keyval[0])'='$val'" - } - } - } - return $pyvenvConfig -} - - -<# Begin Activate script --------------------------------------------------- #> - -# Determine the containing directory of this script -$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition -$VenvExecDir = Get-Item -Path $VenvExecPath - -Write-Verbose "Activation script is located in path: '$VenvExecPath'" -Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)" -Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)" - -# Set values required in priority: CmdLine, ConfigFile, Default -# First, get the location of the virtual environment, it might not be -# VenvExecDir if specified on the command line. -if ($VenvDir) { - Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values" -} -else { - Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir." - $VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/") - Write-Verbose "VenvDir=$VenvDir" -} - -# Next, read the `pyvenv.cfg` file to determine any required value such -# as `prompt`. -$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir - -# Next, set the prompt from the command line, or the config file, or -# just use the name of the virtual environment folder. -if ($Prompt) { - Write-Verbose "Prompt specified as argument, using '$Prompt'" -} -else { - Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value" - if ($pyvenvCfg -and $pyvenvCfg['prompt']) { - Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'" - $Prompt = $pyvenvCfg['prompt']; - } - else { - Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virutal environment)" - Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'" - $Prompt = Split-Path -Path $venvDir -Leaf - } -} - -Write-Verbose "Prompt = '$Prompt'" -Write-Verbose "VenvDir='$VenvDir'" - -# Deactivate any currently active virtual environment, but leave the -# deactivate function in place. -deactivate -nondestructive - -# Now set the environment variable VIRTUAL_ENV, used by many tools to determine -# that there is an activated venv. -$env:VIRTUAL_ENV = $VenvDir - -if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) { - - Write-Verbose "Setting prompt to '$Prompt'" - - # Set the prompt to include the env name - # Make sure _OLD_VIRTUAL_PROMPT is global - function global:_OLD_VIRTUAL_PROMPT { "" } - Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT - New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt - - function global:prompt { - Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) " - _OLD_VIRTUAL_PROMPT - } -} - -# Clear PYTHONHOME -if (Test-Path -Path Env:PYTHONHOME) { - Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME - Remove-Item -Path Env:PYTHONHOME -} - -# Add the venv to the PATH -Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH -$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH" diff --git a/env/bin/activate b/env/bin/activate deleted file mode 100644 index 5031c86..0000000 --- a/env/bin/activate +++ /dev/null @@ -1,76 +0,0 @@ -# This file must be used with "source bin/activate" *from bash* -# you cannot run it directly - -deactivate () { - # reset old environment variables - if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then - PATH="${_OLD_VIRTUAL_PATH:-}" - export PATH - unset _OLD_VIRTUAL_PATH - fi - if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then - PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}" - export PYTHONHOME - unset _OLD_VIRTUAL_PYTHONHOME - fi - - # This should detect bash and zsh, which have a hash command that must - # be called to get it to forget past commands. Without forgetting - # past commands the $PATH changes we made may not be respected - if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then - hash -r - fi - - if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then - PS1="${_OLD_VIRTUAL_PS1:-}" - export PS1 - unset _OLD_VIRTUAL_PS1 - fi - - unset VIRTUAL_ENV - if [ ! "${1:-}" = "nondestructive" ] ; then - # Self destruct! - unset -f deactivate - fi -} - -# unset irrelevant variables -deactivate nondestructive - -VIRTUAL_ENV="/Users/patrykorwat/git/github/cloudiscovery/env" -export VIRTUAL_ENV - -_OLD_VIRTUAL_PATH="$PATH" -PATH="$VIRTUAL_ENV/bin:$PATH" -export PATH - -# unset PYTHONHOME if set -# this will fail if PYTHONHOME is set to the empty string (which is bad anyway) -# could use `if (set -u; : $PYTHONHOME) ;` in bash -if [ -n "${PYTHONHOME:-}" ] ; then - _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}" - unset PYTHONHOME -fi - -if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then - _OLD_VIRTUAL_PS1="${PS1:-}" - if [ "x(env) " != x ] ; then - PS1="(env) ${PS1:-}" - else - if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then - # special case for Aspen magic directories - # see https://aspen.io/ - PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1" - else - PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1" - fi - fi - export PS1 -fi - -# This should detect bash and zsh, which have a hash command that must -# be called to get it to forget past commands. Without forgetting -# past commands the $PATH changes we made may not be respected -if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then - hash -r -fi diff --git a/env/bin/activate.csh b/env/bin/activate.csh deleted file mode 100644 index 3658acc..0000000 --- a/env/bin/activate.csh +++ /dev/null @@ -1,37 +0,0 @@ -# This file must be used with "source bin/activate.csh" *from csh*. -# You cannot run it directly. -# Created by Davide Di Blasi . -# Ported to Python 3.3 venv by Andrew Svetlov - -alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate' - -# Unset irrelevant variables. -deactivate nondestructive - -setenv VIRTUAL_ENV "/Users/patrykorwat/git/github/cloudiscovery/env" - -set _OLD_VIRTUAL_PATH="$PATH" -setenv PATH "$VIRTUAL_ENV/bin:$PATH" - - -set _OLD_VIRTUAL_PROMPT="$prompt" - -if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then - if ("env" != "") then - set env_name = "env" - else - if (`basename "VIRTUAL_ENV"` == "__") then - # special case for Aspen magic directories - # see https://aspen.io/ - set env_name = `basename \`dirname "$VIRTUAL_ENV"\`` - else - set env_name = `basename "$VIRTUAL_ENV"` - endif - endif - set prompt = "[$env_name] $prompt" - unset env_name -endif - -alias pydoc python -m pydoc - -rehash diff --git a/env/bin/activate.fish b/env/bin/activate.fish deleted file mode 100644 index 5b2b6fe..0000000 --- a/env/bin/activate.fish +++ /dev/null @@ -1,75 +0,0 @@ -# This file must be used with ". bin/activate.fish" *from fish* (http://fishshell.org) -# you cannot run it directly - -function deactivate -d "Exit virtualenv and return to normal shell environment" - # reset old environment variables - if test -n "$_OLD_VIRTUAL_PATH" - set -gx PATH $_OLD_VIRTUAL_PATH - set -e _OLD_VIRTUAL_PATH - end - if test -n "$_OLD_VIRTUAL_PYTHONHOME" - set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME - set -e _OLD_VIRTUAL_PYTHONHOME - end - - if test -n "$_OLD_FISH_PROMPT_OVERRIDE" - functions -e fish_prompt - set -e _OLD_FISH_PROMPT_OVERRIDE - functions -c _old_fish_prompt fish_prompt - functions -e _old_fish_prompt - end - - set -e VIRTUAL_ENV - if test "$argv[1]" != "nondestructive" - # Self destruct! - functions -e deactivate - end -end - -# unset irrelevant variables -deactivate nondestructive - -set -gx VIRTUAL_ENV "/Users/patrykorwat/git/github/cloudiscovery/env" - -set -gx _OLD_VIRTUAL_PATH $PATH -set -gx PATH "$VIRTUAL_ENV/bin" $PATH - -# unset PYTHONHOME if set -if set -q PYTHONHOME - set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME - set -e PYTHONHOME -end - -if test -z "$VIRTUAL_ENV_DISABLE_PROMPT" - # fish uses a function instead of an env var to generate the prompt. - - # save the current fish_prompt function as the function _old_fish_prompt - functions -c fish_prompt _old_fish_prompt - - # with the original prompt function renamed, we can override with our own. - function fish_prompt - # Save the return status of the last command - set -l old_status $status - - # Prompt override? - if test -n "(env) " - printf "%s%s" "(env) " (set_color normal) - else - # ...Otherwise, prepend env - set -l _checkbase (basename "$VIRTUAL_ENV") - if test $_checkbase = "__" - # special case for Aspen magic directories - # see https://aspen.io/ - printf "%s[%s]%s " (set_color -b blue white) (basename (dirname "$VIRTUAL_ENV")) (set_color normal) - else - printf "%s(%s)%s" (set_color -b blue white) (basename "$VIRTUAL_ENV") (set_color normal) - end - end - - # Restore the return status of the previous command. - echo "exit $old_status" | . - _old_fish_prompt - end - - set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV" -end diff --git a/env/bin/easy_install b/env/bin/easy_install deleted file mode 100755 index da2c2fe..0000000 --- a/env/bin/easy_install +++ /dev/null @@ -1,9 +0,0 @@ -#!/Users/patrykorwat/git/github/cloudiscovery/env/bin/python3 -# -*- coding: utf-8 -*- -import re -import sys -from setuptools.command.easy_install import main - -if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) - sys.exit(main()) diff --git a/env/bin/easy_install-3.8 b/env/bin/easy_install-3.8 deleted file mode 100755 index da2c2fe..0000000 --- a/env/bin/easy_install-3.8 +++ /dev/null @@ -1,9 +0,0 @@ -#!/Users/patrykorwat/git/github/cloudiscovery/env/bin/python3 -# -*- coding: utf-8 -*- -import re -import sys -from setuptools.command.easy_install import main - -if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) - sys.exit(main()) diff --git a/env/bin/pip b/env/bin/pip deleted file mode 100755 index d58b4cb..0000000 --- a/env/bin/pip +++ /dev/null @@ -1,9 +0,0 @@ -#!/Users/patrykorwat/git/github/cloudiscovery/env/bin/python3 -# -*- coding: utf-8 -*- -import re -import sys -from pip._internal.cli.main import main - -if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) - sys.exit(main()) diff --git a/env/bin/pip3 b/env/bin/pip3 deleted file mode 100755 index d58b4cb..0000000 --- a/env/bin/pip3 +++ /dev/null @@ -1,9 +0,0 @@ -#!/Users/patrykorwat/git/github/cloudiscovery/env/bin/python3 -# -*- coding: utf-8 -*- -import re -import sys -from pip._internal.cli.main import main - -if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) - sys.exit(main()) diff --git a/env/bin/pip3.8 b/env/bin/pip3.8 deleted file mode 100755 index d58b4cb..0000000 --- a/env/bin/pip3.8 +++ /dev/null @@ -1,9 +0,0 @@ -#!/Users/patrykorwat/git/github/cloudiscovery/env/bin/python3 -# -*- coding: utf-8 -*- -import re -import sys -from pip._internal.cli.main import main - -if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) - sys.exit(main()) diff --git a/env/bin/python b/env/bin/python deleted file mode 120000 index b8a0adb..0000000 --- a/env/bin/python +++ /dev/null @@ -1 +0,0 @@ -python3 \ No newline at end of file diff --git a/env/bin/python3 b/env/bin/python3 deleted file mode 120000 index 79ab74b..0000000 --- a/env/bin/python3 +++ /dev/null @@ -1 +0,0 @@ -/usr/local/bin/python3 \ No newline at end of file diff --git a/env/bin/python3.8 b/env/bin/python3.8 deleted file mode 120000 index 2736161..0000000 --- a/env/bin/python3.8 +++ /dev/null @@ -1 +0,0 @@ -/usr/local/bin/python3.8 \ No newline at end of file diff --git a/env/pyvenv.cfg b/env/pyvenv.cfg deleted file mode 100644 index 4e279fb..0000000 --- a/env/pyvenv.cfg +++ /dev/null @@ -1,3 +0,0 @@ -home = /usr/local/bin -include-system-site-packages = false -version = 3.8.6 diff --git a/setup.py b/setup.py index f4826f4..b056d6d 100755 --- a/setup.py +++ b/setup.py @@ -69,7 +69,7 @@ def run(self): "pytest-pythonpath", ], tests_suite="cloudiscovery", - python_requires=">=3.6", + python_requires=">=3.8", scripts=["bin/cloudiscovery", "bin/cloudiscovery.cmd"], license="Apache License 2.0", classifiers=[ From 1c3afb418ab31e00fcf9b61524bd3f7c93579a68 Mon Sep 17 00:00:00 2001 From: Patryk Orwat Date: Mon, 9 Nov 2020 01:17:09 +0800 Subject: [PATCH 3/5] #97 added public and private subnet VPC case --- .prospector.yaml | 1 + cloudiscovery/provider/vpc/diagram.py | 4 +- .../provider/vpc/resource/network.py | 20 +- cloudiscovery/shared/common.py | 3 + cloudiscovery/shared/diagram.py | 215 +++++++++++++++++- cloudiscovery/shared/diagramsnet.py | 11 +- .../tests/shared/test_shared_diagram.py | 20 +- docs/.DS_Store | Bin 0 -> 6148 bytes 8 files changed, 246 insertions(+), 28 deletions(-) create mode 100644 docs/.DS_Store diff --git a/.prospector.yaml b/.prospector.yaml index d97bf2b..5c841df 100644 --- a/.prospector.yaml +++ b/.prospector.yaml @@ -15,6 +15,7 @@ pep8: - D400 - D401 - W503 + - E501 options: max-line-length: 120 diff --git a/cloudiscovery/provider/vpc/diagram.py b/cloudiscovery/provider/vpc/diagram.py index 0a626da..5f304a3 100644 --- a/cloudiscovery/provider/vpc/diagram.py +++ b/cloudiscovery/provider/vpc/diagram.py @@ -1,7 +1,7 @@ from typing import List, Dict, Optional from shared.common import ResourceEdge, Resource, ResourceDigest -from shared.diagram import add_resource_to_group, DiagramsNetDiagram +from shared.diagram import add_resource_to_group, VPCDiagramsNetDiagram PUBLIC_SUBNET = "{public subnet}" PRIVATE_SUBNET = "{private subnet}" @@ -95,7 +95,7 @@ def aggregate_asg_groups( add_resource_to_group(groups, "", agg_resource) -class VpcDiagram(DiagramsNetDiagram): +class VpcDiagram(VPCDiagramsNetDiagram): def __init__(self, vpc_id: str): """ VPC diagram diff --git a/cloudiscovery/provider/vpc/resource/network.py b/cloudiscovery/provider/vpc/resource/network.py index 7e6094c..d2f5d89 100644 --- a/cloudiscovery/provider/vpc/resource/network.py +++ b/cloudiscovery/provider/vpc/resource/network.py @@ -265,15 +265,15 @@ def get_resources(self) -> List[Resource]: message_handler("Collecting data from Route Tables...", "HEADER") # Iterate to get all route table filtered - for data in response["RouteTables"]: - nametag = get_name_tag(data) + for route_table in response["RouteTables"]: + nametag = get_name_tag(route_table) - name = data["RouteTableId"] if nametag is None else nametag + name = route_table["RouteTableId"] if nametag is None else nametag table_digest = ResourceDigest( - id=data["RouteTableId"], type="aws_route_table" + id=route_table["RouteTableId"], type="aws_route_table" ) is_main = False - for association in data["Associations"]: + for association in route_table["Associations"]: if association["Main"] is True: is_main = True if is_main: @@ -283,7 +283,7 @@ def get_resources(self) -> List[Resource]: ) ) else: - for association in data["Associations"]: + for association in route_table["Associations"]: if "SubnetId" in association: self.relations_found.append( ResourceEdge( @@ -296,7 +296,7 @@ def get_resources(self) -> List[Resource]: is_public = False - for route in data["Routes"]: + for route in route_table["Routes"]: if ( "DestinationCidrBlock" in route and route["DestinationCidrBlock"] == "0.0.0.0/0" @@ -311,7 +311,7 @@ def get_resources(self) -> List[Resource]: name=name, details="default: {}, public: {}".format(is_main, is_public), group="network", - tags=resource_tags(data), + tags=resource_tags(route_table), ) ) return resources_found @@ -591,7 +591,7 @@ def get_resources(self) -> List[Resource]: resources_found.append( Resource( digest=endpoint_digest, - name=data["ServiceName"], + name=data["VpcEndpointId"], details="Vpc Endpoint Gateway RouteTable {}".format( ", ".join(data["RouteTableIds"]) ), @@ -609,7 +609,7 @@ def get_resources(self) -> List[Resource]: resources_found.append( Resource( digest=endpoint_digest, - name=data["ServiceName"], + name=data["VpcEndpointId"], details="Vpc Endpoint Service Subnet {}".format( ", ".join(data["SubnetIds"]) ), diff --git a/cloudiscovery/shared/common.py b/cloudiscovery/shared/common.py index acb01a6..7d2e416 100644 --- a/cloudiscovery/shared/common.py +++ b/cloudiscovery/shared/common.py @@ -37,6 +37,9 @@ class ResourceDigest(NamedTuple): id: str type: str + def to_string(self): + return f"{self.type}:{self.id}" + class ResourceEdge(NamedTuple): from_node: ResourceDigest diff --git a/cloudiscovery/shared/diagram.py b/cloudiscovery/shared/diagram.py index aa882c2..196626d 100644 --- a/cloudiscovery/shared/diagram.py +++ b/cloudiscovery/shared/diagram.py @@ -6,7 +6,7 @@ from diagrams import Diagram, Cluster, Edge from shared.common import Resource, ResourceEdge, ResourceDigest, message_handler -from shared.diagramsnet import DIAGRAM_HEADER, DIAGRAM_SUFFIX, MX_FILE +from shared.diagramsnet import DIAGRAM_HEADER, DIAGRAM_SUFFIX, MX_FILE, CELL_TEMPLATE from shared.error_handler import exception PATH_DIAGRAM_OUTPUT = "./assets/diagrams/" @@ -223,6 +223,60 @@ class Mapsources: "aws_vpn_client_endpoint": "ClientVpn", } + styles = { + "aws_vpn_gateway": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;strokeColor=none;" + "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" + "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.vpn_gateway;", + "aws_general": "gradientDirection=north;outlineConnect=0;fontColor=#232F3E;gradientColor=#505863;" + "fillColor=#1E262E;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;" + "align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4.resourceIcon;" + "resIcon=mxgraph.aws4.general;", + "aws_lambda_function": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#D05C17;" + "strokeColor=none;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;" + "html=1;fontSize=12;fontStyle=0;aspect=fixed;pointerEvents=1;" + "shape=mxgraph.aws4.lambda_function;", + "aws_route_table": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;strokeColor=none;" + "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" + "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.route_table;", + "aws_internet_gateway": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;" + "strokeColor=none;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;" + "align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;pointerEvents=1;" + "shape=mxgraph.aws4.internet_gateway;", + "aws_elb": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;strokeColor=none;" + "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" + "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.application_load_balancer;", + "aws_network_acl": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;strokeColor=none;" + "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" + "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.network_access_control_list;", + "aws_vpc_endpoint_gateway": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;" + "strokeColor=none;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;" + "align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;pointerEvents=1;" + "shape=mxgraph.aws4.endpoint;", + "aws_ecs_cluster": "outlineConnect=0;fontColor=#232F3E;gradientColor=#F78E04;gradientDirection=north;" + "fillColor=#D05C17;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;" + "verticalAlign=top;align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;" + "shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.ecs;", + "aws_instance": "outlineConnect=0;fontColor=#232F3E;gradientColor=#F78E04;gradientDirection=north;" + "fillColor=#D05C17;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;" + "verticalAlign=top;align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;" + "shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.ec2;", + "aws_s3": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#277116;strokeColor=none;" + "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" + "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.bucket;", + "aws_db_instance": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#3334B9;strokeColor=none;" + "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" + "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.rds_instance;", + "aws_dynamodb": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#3334B9;strokeColor=none;" + "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" + "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.table;", + "aws_sqs": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#BC1356;strokeColor=none;dashed=0;" + "verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;fontStyle=0;" + "aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.queue;", + "aws_sns_topic": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#BC1356;strokeColor=none;" + "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" + "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.topic;", + } + def add_resource_to_group(ordered_resources, group, resource): if Mapsources.mapresources.get(resource.digest.type) is not None: @@ -378,7 +432,7 @@ def build( pass -class DiagramsNetDiagram(BaseDiagram): +class VPCDiagramsNetDiagram(BaseDiagram): def generate_diagram( self, resources: List[Resource], @@ -412,10 +466,165 @@ def deflate_encode(value: str): "utf-8" ) + # pylint: disable=too-many-locals def build_diagram( self, resources: Dict[str, List[Resource]], resource_relations: List[ResourceEdge], ): - mx_graph_model = DIAGRAM_HEADER + DIAGRAM_SUFFIX + mx_graph_model = DIAGRAM_HEADER + cell_id = 1 + + vpc_resource = None + for _, resource_group in resources.items(): + for resource in resource_group: + if resource.digest.type == "aws_vpc": + if vpc_resource is None: + vpc_resource = resource + else: + raise Exception("Only one VPC in a region is supported now") + if vpc_resource is None: + raise Exception("Only one VPC in a region is supported now") + + added_resources: List[ResourceDigest] = [] + + vpc_cell = ( + '' + "".format(cell_id, vpc_resource.name) + ) + cell_id += 1 + mx_graph_model += vpc_cell + + public_subnet_x = 80 + public_subnet_y = 80 + cell_id += 1 + # pylint: disable=line-too-long + public_subnet = ( + ''.format_map( + {"X": str(public_subnet_x), "Y": str(public_subnet_y)} + ) + ) + mx_graph_model += public_subnet + + mx_graph_model = self.render_subnet_items( + added_resources, + mx_graph_model, + "{public subnet}", + public_subnet_x, + public_subnet_y, + resource_relations, + resources, + ) + + private_subnet_x = 580 + private_subnet_y = 80 + cell_id += 1 + private_subnet = ( + ''.format_map( + {"X": str(private_subnet_x), "Y": str(private_subnet_y)} + ) + ) + mx_graph_model += private_subnet + + mx_graph_model = self.render_subnet_items( + added_resources, + mx_graph_model, + "{private subnet}", + private_subnet_x, + private_subnet_y, + resource_relations, + resources, + ) + + count = 0 + row = 0 + for _, resource_group in resources.items(): + for resource in resource_group: + if resource.digest.type in ["aws_subnet", "aws_vpc"]: + continue + if resource.digest not in added_resources: + added_resources.append(resource.digest) + style = ( + Mapsources.styles[resource.digest.type] + if resource.digest.type in Mapsources.styles + else Mapsources.styles["aws_general"] + ) + cell = CELL_TEMPLATE.format_map( + { + "CELL_IDX": resource.digest.to_string(), + "X": str(count * 140 + public_subnet_x + 40), + "Y": str(580 + row * 100 + 40), + "STYLE": style.replace("fontSize=12", "fontSize=8"), + "TITLE": resource.name, + } + ) + count += 1 + mx_graph_model += cell + if count % 6 == 0: + row += 1 + count = 0 + + mx_graph_model += DIAGRAM_SUFFIX return MX_FILE.replace("", self.deflate_encode(mx_graph_model)) + + # pylint: disable=too-many-locals,too-many-arguments + def render_subnet_items( + self, + added_resources, + mx_graph_model, + subnet_id, + subnet_x, + subnet_y, + resource_relations, + resources, + ): + count = 0 + row = 0 + # pylint: disable=too-many-nested-blocks + for relation in resource_relations: + if relation.to_node == ResourceDigest(id=subnet_id, type="aws_subnet"): + for _, resource_group in resources.items(): + for resource in resource_group: + if ( + resource.digest == relation.from_node + and relation.from_node not in added_resources + ): + added_resources.append(relation.from_node) + style = ( + Mapsources.styles[relation.from_node.type] + if relation.from_node.type in Mapsources.styles + else Mapsources.styles["aws_general"] + ) + + cell = CELL_TEMPLATE.format_map( + { + "CELL_IDX": relation.from_node.to_string(), + "X": str(count * 140 + subnet_x + 40), + "Y": str(subnet_y + row * 100 + 40), + "STYLE": style.replace("fontSize=12", "fontSize=8"), + "TITLE": resource.name, + } + ) + count += 1 + mx_graph_model += cell + if count % 3 == 0: + row += 1 + count = 0 + return mx_graph_model diff --git a/cloudiscovery/shared/diagramsnet.py b/cloudiscovery/shared/diagramsnet.py index d65f4ef..c56b36e 100644 --- a/cloudiscovery/shared/diagramsnet.py +++ b/cloudiscovery/shared/diagramsnet.py @@ -16,13 +16,8 @@ """ -CELL_TEMPLATE = ( - """ -;resIcon=;" vertex="1" parent="1"> - +CELL_TEMPLATE = """ + + """ -) diff --git a/cloudiscovery/tests/shared/test_shared_diagram.py b/cloudiscovery/tests/shared/test_shared_diagram.py index 08ca76f..b36e8b2 100644 --- a/cloudiscovery/tests/shared/test_shared_diagram.py +++ b/cloudiscovery/tests/shared/test_shared_diagram.py @@ -3,22 +3,32 @@ from assertpy import assert_that from shared.common import Resource, ResourceDigest -from shared.diagram import DiagramsNetDiagram +from shared.diagram import VPCDiagramsNetDiagram from shared.diagramsnet import MX_FILE INFLATED_XML = "" -DEFLATED_XML = "s6nIzVHQtwMA" +DEFLATED_XML = ( + "7Vbfb5swEP5reFwFNpTkMSZpN6mTKlXbHicXDFg1nGWcQvrXzwbT8CObWqkPfVgSKb7vO+7O5++QPZxU3a2isvwOGRMe8rPOw3sP" + "IRRfR+bPIieH+LFDCsWzAQvOwAN/YQ70HXrkGWtmjhpAaC7nYAp1zVI9w6hS0M7dchDzrJIWbAU8pFSs0V880+WAblB8xr8yXpRj" + "5uB6OzAVHZ3dTpqSZtBOIHzwcKIA9LCquoQJ272xL8NzN39hXwtTrNZveeCF4JO/l7jKDz/utzddgw/qi4vyTMXRbdgVq09jB+Co" + "Ba9Z8tpg38Mkh1onIED1Pth8b2xWUiiacTbjwh1B292E23NlAnGoDV+Dsm0iORdi8oxp4z4iBm+0gic2YfL+Y5iMNiXLXDnPTGlu" + "Du2OPjJxDw134R9Ba6gmDjvBC0tokAalzkpNVcwkIKWuhLEDt0MnxwCNtuuKTUkbObQj552tg5jzlZasusLOwhVtm/BKsQaOKmXf" + "UlsPMeawmntRKc1GGa1+Ixt6fbLjMZldsG4CuZO+ZVAxrU7GZWS3TnVu7iLs7PYs4ngzQOVEvyNG3dgUr5HPyjILJ653CA39F9on" + "EFoL6imDtPkYkWH8yUR2vRLZz/tkpTMJvNZ9anPyETE1J74XGSax1hWKFsDSjudAsLZsjDmwtOM5ECzDB4v8wbLACbCyZuH9RX5/" + "UqD5YXJx7JbjVUPN5rJtS67Zg6Sp7WprJPYGKV9UbqHgKPuUlxTbs7+fZXppRlG42QThYqpdqf8eRMFybSOa+nld3PXWHl944ex2" + "JCab5WvgI97QKJwNT4jWwxNt/PX0hOPV6B3jY8zzRaPnJvc1fPgD" +) class TestDiagramsNetDiagram(TestCase): - sut = DiagramsNetDiagram() + sut = VPCDiagramsNetDiagram() def test_deflate_encode(self): - result = DiagramsNetDiagram.deflate_encode(INFLATED_XML) + result = VPCDiagramsNetDiagram.deflate_encode(INFLATED_XML) assert_that(result).is_equal_to(DEFLATED_XML) def test_decode_inflate(self): - result = DiagramsNetDiagram.decode_inflate(DEFLATED_XML) + result = VPCDiagramsNetDiagram.decode_inflate(DEFLATED_XML) assert_that(result).is_equal_to(INFLATED_XML) def test_file_generation(self): diff --git a/docs/.DS_Store b/docs/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b419bb95e14c41be898d8672bc6227bbc31964dd GIT binary patch literal 6148 zcmeHKT}s115T4Oi0>0$Y$Gt+q1EeKAK`x+aq&}pi5CtD|22bJ{JcKv!9)9zqG&ax| z5s{g&^X<;hZuW!hW{8MakIjN8C!!2Zkfk#rVxDxJdGH*_I>!U`O}}aDu4^YI`imxc z_Y1nE9?u;eygxQHcC_0?wdrVk{oy(< zx?HGWAQ%V+f`MS*=NOPH4w79Oh7JaTfneZ^0XZKMnqc8L80yghr6mAx2D1wKQcFlq zax5GNLwF!;sX$9*FEQBCVNV`cI1YxEPVB`8`^uli3+w8bKWR8|VHi3X2nNOsoZB#$ z`+tp3X0pg1r^F~22nPNc13W9Ma)nL#-Fmb=xoZ>J4Vs9=4bp+2Kl%y4K+cgXb(%j( ZA901_U?{VQUDJW_BcOzY3I=|Gfe!?fG)Djc literal 0 HcmV?d00001 From 34e843793288ad4098bdfca076053b0bfd3b0455 Mon Sep 17 00:00:00 2001 From: Patryk Orwat Date: Mon, 9 Nov 2020 21:51:23 +0800 Subject: [PATCH 4/5] #97 fixed tests --- cloudiscovery/tests/shared/test_shared_diagram.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/cloudiscovery/tests/shared/test_shared_diagram.py b/cloudiscovery/tests/shared/test_shared_diagram.py index b36e8b2..fd3acff 100644 --- a/cloudiscovery/tests/shared/test_shared_diagram.py +++ b/cloudiscovery/tests/shared/test_shared_diagram.py @@ -7,17 +7,7 @@ from shared.diagramsnet import MX_FILE INFLATED_XML = "" -DEFLATED_XML = ( - "7Vbfb5swEP5reFwFNpTkMSZpN6mTKlXbHicXDFg1nGWcQvrXzwbT8CObWqkPfVgSKb7vO+7O5++QPZxU3a2isvwOGRMe8rPOw3sP" - "IRRfR+bPIieH+LFDCsWzAQvOwAN/YQ70HXrkGWtmjhpAaC7nYAp1zVI9w6hS0M7dchDzrJIWbAU8pFSs0V880+WAblB8xr8yXpRj" - "5uB6OzAVHZ3dTpqSZtBOIHzwcKIA9LCquoQJ272xL8NzN39hXwtTrNZveeCF4JO/l7jKDz/utzddgw/qi4vyTMXRbdgVq09jB+Co" - "Ba9Z8tpg38Mkh1onIED1Pth8b2xWUiiacTbjwh1B292E23NlAnGoDV+Dsm0iORdi8oxp4z4iBm+0gic2YfL+Y5iMNiXLXDnPTGlu" - "Du2OPjJxDw134R9Ba6gmDjvBC0tokAalzkpNVcwkIKWuhLEDt0MnxwCNtuuKTUkbObQj552tg5jzlZasusLOwhVtm/BKsQaOKmXf" - "UlsPMeawmntRKc1GGa1+Ixt6fbLjMZldsG4CuZO+ZVAxrU7GZWS3TnVu7iLs7PYs4ngzQOVEvyNG3dgUr5HPyjILJ653CA39F9on" - "EFoL6imDtPkYkWH8yUR2vRLZz/tkpTMJvNZ9anPyETE1J74XGSax1hWKFsDSjudAsLZsjDmwtOM5ECzDB4v8wbLACbCyZuH9RX5/" - "UqD5YXJx7JbjVUPN5rJtS67Zg6Sp7WprJPYGKV9UbqHgKPuUlxTbs7+fZXppRlG42QThYqpdqf8eRMFybSOa+nld3PXWHl944ex2" - "JCab5WvgI97QKJwNT4jWwxNt/PX0hOPV6B3jY8zzRaPnJvc1fPgD" -) +DEFLATED_XML = "s6nIzVHQtwMA" class TestDiagramsNetDiagram(TestCase): @@ -34,7 +24,7 @@ def test_decode_inflate(self): def test_file_generation(self): general_resources = [ Resource( - digest=ResourceDigest(id="123", type="type"), + digest=ResourceDigest(id="123", type="aws_vpc"), name="name", details="details", ) From 7d7ecd3c814aa17ad913863eeddb004c98ee4f7a Mon Sep 17 00:00:00 2001 From: Patryk Orwat Date: Wed, 11 Nov 2020 00:20:11 +0800 Subject: [PATCH 5/5] #97 added more diagrams.net icons --- cloudiscovery/shared/diagram.py | 74 ++------- cloudiscovery/shared/diagramsnet.py | 235 ++++++++++++++++++++++++++++ 2 files changed, 249 insertions(+), 60 deletions(-) diff --git a/cloudiscovery/shared/diagram.py b/cloudiscovery/shared/diagram.py index 196626d..3ebb297 100644 --- a/cloudiscovery/shared/diagram.py +++ b/cloudiscovery/shared/diagram.py @@ -6,7 +6,13 @@ from diagrams import Diagram, Cluster, Edge from shared.common import Resource, ResourceEdge, ResourceDigest, message_handler -from shared.diagramsnet import DIAGRAM_HEADER, DIAGRAM_SUFFIX, MX_FILE, CELL_TEMPLATE +from shared.diagramsnet import ( + DIAGRAM_HEADER, + DIAGRAM_SUFFIX, + MX_FILE, + CELL_TEMPLATE, + build_styles, +) from shared.error_handler import exception PATH_DIAGRAM_OUTPUT = "./assets/diagrams/" @@ -223,59 +229,7 @@ class Mapsources: "aws_vpn_client_endpoint": "ClientVpn", } - styles = { - "aws_vpn_gateway": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;strokeColor=none;" - "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" - "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.vpn_gateway;", - "aws_general": "gradientDirection=north;outlineConnect=0;fontColor=#232F3E;gradientColor=#505863;" - "fillColor=#1E262E;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;" - "align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4.resourceIcon;" - "resIcon=mxgraph.aws4.general;", - "aws_lambda_function": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#D05C17;" - "strokeColor=none;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;" - "html=1;fontSize=12;fontStyle=0;aspect=fixed;pointerEvents=1;" - "shape=mxgraph.aws4.lambda_function;", - "aws_route_table": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;strokeColor=none;" - "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" - "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.route_table;", - "aws_internet_gateway": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;" - "strokeColor=none;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;" - "align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;pointerEvents=1;" - "shape=mxgraph.aws4.internet_gateway;", - "aws_elb": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;strokeColor=none;" - "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" - "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.application_load_balancer;", - "aws_network_acl": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;strokeColor=none;" - "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" - "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.network_access_control_list;", - "aws_vpc_endpoint_gateway": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;" - "strokeColor=none;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;" - "align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;pointerEvents=1;" - "shape=mxgraph.aws4.endpoint;", - "aws_ecs_cluster": "outlineConnect=0;fontColor=#232F3E;gradientColor=#F78E04;gradientDirection=north;" - "fillColor=#D05C17;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;" - "verticalAlign=top;align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;" - "shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.ecs;", - "aws_instance": "outlineConnect=0;fontColor=#232F3E;gradientColor=#F78E04;gradientDirection=north;" - "fillColor=#D05C17;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;" - "verticalAlign=top;align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;" - "shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.ec2;", - "aws_s3": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#277116;strokeColor=none;" - "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" - "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.bucket;", - "aws_db_instance": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#3334B9;strokeColor=none;" - "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" - "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.rds_instance;", - "aws_dynamodb": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#3334B9;strokeColor=none;" - "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" - "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.table;", - "aws_sqs": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#BC1356;strokeColor=none;dashed=0;" - "verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;fontStyle=0;" - "aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.queue;", - "aws_sns_topic": "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#BC1356;strokeColor=none;" - "dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;" - "fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.topic;", - } + resource_styles = build_styles() def add_resource_to_group(ordered_resources, group, resource): @@ -562,9 +516,9 @@ def build_diagram( if resource.digest not in added_resources: added_resources.append(resource.digest) style = ( - Mapsources.styles[resource.digest.type] - if resource.digest.type in Mapsources.styles - else Mapsources.styles["aws_general"] + Mapsources.resource_styles[resource.digest.type] + if resource.digest.type in Mapsources.resource_styles + else Mapsources.resource_styles["aws_general"] ) cell = CELL_TEMPLATE.format_map( { @@ -608,9 +562,9 @@ def render_subnet_items( ): added_resources.append(relation.from_node) style = ( - Mapsources.styles[relation.from_node.type] - if relation.from_node.type in Mapsources.styles - else Mapsources.styles["aws_general"] + Mapsources.resource_styles[relation.from_node.type] + if relation.from_node.type in Mapsources.resource_styles + else Mapsources.resource_styles["aws_general"] ) cell = CELL_TEMPLATE.format_map( diff --git a/cloudiscovery/shared/diagramsnet.py b/cloudiscovery/shared/diagramsnet.py index c56b36e..ce1ba94 100644 --- a/cloudiscovery/shared/diagramsnet.py +++ b/cloudiscovery/shared/diagramsnet.py @@ -21,3 +21,238 @@ """ + + +# from https://github.com/jgraph/drawio/blob/master/src/main/webapp/js/diagramly/sidebar/Sidebar-AWS4.js +gn = "mxgraph.aws4" + +s = 1 +w = s * 100 +h = s * 100 +w2 = s * 78 + + +def _add_general_resources(styles): + n3 = ( + "gradientDirection=north;outlineConnect=0;fontColor=#232F3E;gradientColor=#505863;fillColor=#1E262E;" + "strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;" + "fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4." + ) + + styles["aws_general"] = n3 + "resourceIcon;resIcon=" + gn + ".general;" + + +def _add_analytics_resources(styles): + n2 = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=#945DF2;gradientDirection=north;fillColor=#5A30B5;" + "strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;" + "fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4." + ) + styles["aws_athena"] = n2 + "resourceIcon;resIcon=" + gn + ".athena;" + styles["aws_elasticsearch_domain"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".elasticsearch_service;" + ) + styles["aws_emr"] = n2 + "resourceIcon;resIcon=" + gn + ".emr;" + styles["aws_emr_cluster"] = n2 + "resourceIcon;resIcon=" + gn + ".emr;" + styles["aws_kinesis"] = n2 + "resourceIcon;resIcon=" + gn + ".kinesis;" + styles["aws_kinesisanalytics"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".kinesis_data_analytics;" + ) + styles["aws_kinesis_firehose"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".kinesis_data_firehose;" + ) + styles["aws_quicksight"] = n2 + "resourceIcon;resIcon=" + gn + ".quicksight;" + styles["aws_redshift"] = n2 + "resourceIcon;resIcon=" + gn + ".redshift;" + styles["aws_data_pipeline"] = n2 + "resourceIcon;resIcon=" + gn + ".data_pipeline;" + styles["aws_msk_cluster"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".managed_streaming_for_kafka;" + ) + styles["aws_glue"] = n2 + "resourceIcon;resIcon=" + gn + ".glue;" + styles["aws_lakeformation"] = n2 + "resourceIcon;resIcon=" + gn + ".lake_formation;" + + +def _add_application_integration_resources(styles): + n2 = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=#F34482;gradientDirection=north;fillColor=#BC1356;" + "strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;" + "fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4." + ) + + styles["aws_sns_topic"] = n2 + "resourceIcon;resIcon=" + gn + ".sns;" + styles["aws_sqs"] = n2 + "resourceIcon;resIcon=" + gn + ".sqs;" + styles["aws_appsync_graphql_api"] = n2 + "resourceIcon;resIcon=" + gn + ".appsync;" + styles["aws_events"] = n2 + "resourceIcon;resIcon=" + gn + ".eventbridge;" + + +def _add_compute_resources(styles): + n = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#D05C17;strokeColor=none;dashed=0;" + "verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;" + "pointerEvents=1;shape=mxgraph.aws4." + ) + n2 = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=#F78E04;gradientDirection=north;fillColor=#D05C17;" + "strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;" + "fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4." + ) + + styles["aws_instance"] = n2 + "resourceIcon;resIcon=" + gn + ".ec2;" + styles["aws_autoscaling_group"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".auto_scaling2;" + ) + styles["aws_batch"] = n2 + "resourceIcon;resIcon=" + gn + ".batch;" + styles["aws_elastic_beanstalk_environment"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".elastic_beanstalk;" + ) + styles["aws_lambda_function"] = n + "lambda_function;" + + +def _add_container_resources(styles): + n2 = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=#F78E04;gradientDirection=north;fillColor=#D05C17;" + "strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;" + "fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4." + ) + + styles["aws_eks_cluster"] = n2 + "resourceIcon;resIcon=" + gn + ".eks;" + styles["aws_ecr"] = n2 + "resourceIcon;resIcon=" + gn + ".ecr;" + styles["aws_ecs_cluster"] = n2 + "resourceIcon;resIcon=" + gn + ".ecs;" + + +def _add_customer_engagement_resources(styles): + n2 = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=#4D72F3;gradientDirection=north;fillColor=#3334B9;" + "strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;" + "fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4." + ) + + styles["aws_connect"] = n2 + "resourceIcon;resIcon=" + gn + ".connect;" + styles["aws_pinpoint"] = n2 + "resourceIcon;resIcon=" + gn + ".pinpoint;" + styles["aws_ses"] = n2 + "resourceIcon;resIcon=" + gn + ".simple_email_service;" + + +def _add_database_resources(styles): + n = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#3334B9;strokeColor=none;dashed=0;" + "verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;" + "pointerEvents=1;shape=mxgraph.aws4." + ) + n2 = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=#4D72F3;gradientDirection=north;fillColor=#3334B9;" + "strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;" + "fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4." + ) + + styles["aws_docdb_cluster"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".documentdb_with_mongodb_compatibility;" + ) + styles["aws_dynamodb"] = n2 + "resourceIcon;resIcon=" + gn + ".dynamodb;" + styles["aws_elasticache_cluster"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".elasticache;" + ) + styles["aws_neptune_cluster"] = n2 + "resourceIcon;resIcon=" + gn + ".neptune;" + styles["aws_redshift"] = n2 + "resourceIcon;resIcon=" + gn + ".redshift;" + + styles["aws_db_instance"] = n + "rds_instance;" + styles["aws_dax"] = n + "dynamodb_dax;" + + +def _add_ml_resources(styles): + n2 = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=#4AB29A;gradientDirection=north;fillColor=#116D5B;" + "strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;" + "fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4." + ) + + styles["aws_sagemaker"] = n2 + "resourceIcon;resIcon=" + gn + ".sagemaker;" + + +def _add_management_governance_resources(styles): + n2 = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=#F34482;gradientDirection=north;fillColor=#BC1356;" + "strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;" + "fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4." + ) + + styles["aws_cloudwatch"] = n2 + "resourceIcon;resIcon=" + gn + ".cloudwatch_2;" + styles["aws_autoscaling_group"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".autoscaling;" + ) + styles["aws_auto_scaling"] = n2 + "resourceIcon;resIcon=" + gn + ".autoscaling;" + styles["aws_cloudformation"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".cloudformation;" + ) + + +def _add_network_resources(styles): + n = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#5A30B5;strokeColor=none;dashed=0;" + "verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;" + "pointerEvents=1;shape=mxgraph.aws4." + ) + n2 = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=#945DF2;gradientDirection=north;fillColor=#5A30B5;" + "strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;" + "fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4." + ) + + styles["aws_api_gateway_rest_api"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".api_gateway;" + ) + styles["aws_cloudfront_distribution"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".cloudfront;" + ) + styles["aws_vpc"] = n2 + "resourceIcon;resIcon=" + gn + ".vpc;" + styles["aws_vpn_client_endpoint"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".client_vpn;" + ) + styles["aws_elb"] = n2 + "resourceIcon;resIcon=" + gn + ".elastic_load_balancing;" + styles["aws_directconnect"] = n2 + "resourceIcon;resIcon=" + gn + ".direct_connect;" + styles["aws_global_accelerator"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".global_accelerator;" + ) + + styles["aws_route_table"] = n + "route_table;" + styles["aws_vpc_endpoint_gateway"] = n + "gateway;" + styles["aws_internet_gateway"] = n + "internet_gateway;" + styles["aws_nat_gateway"] = n + "nat_gateway;" + styles["aws_network_acl"] = n + "network_access_control_list;" + styles["aws_elb_classic"] = n + "classic_load_balancer;" + styles["aws_vpn_connection"] = n + "vpn_connection;" + styles["aws_vpn_gateway"] = n + "vpn_gateway;" + + +def _add_storage_resources(styles): + n = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#277116;strokeColor=none;dashed=0;" + "verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=12;fontStyle=0;aspect=fixed;" + "pointerEvents=1;shape=mxgraph.aws4." + ) + n2 = ( + "outlineConnect=0;fontColor=#232F3E;gradientColor=#60A337;gradientDirection=north;fillColor=#277116;" + "strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;" + "fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4." + ) + + styles["aws_efs_file_system"] = ( + n2 + "resourceIcon;resIcon=" + gn + ".elastic_file_system;" + ) + styles["aws_fsx"] = n2 + "resourceIcon;resIcon=" + gn + ".fsx;" + + styles["aws_s3"] = n + "bucket;" + + +def build_styles(): + styles = {} + _add_general_resources(styles) + _add_analytics_resources(styles) + _add_application_integration_resources(styles) + _add_compute_resources(styles) + _add_container_resources(styles) + _add_customer_engagement_resources(styles) + _add_database_resources(styles) + _add_ml_resources(styles) + _add_management_governance_resources(styles) + _add_network_resources(styles) + _add_storage_resources(styles) + return styles