Skip to content

Commit

Permalink
Add warning to 'fides deploy' when installed outside of a virtual env…
Browse files Browse the repository at this point in the history
…ironment (#2641)
  • Loading branch information
NevilleS authored Feb 17, 2023
1 parent ac89777 commit 53e13d5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ The types of changes are:

## [Unreleased](https://github.com/ethyca/fides/compare/2.7.0...main)

### Changed

* Add warning to 'fides deploy' when installed outside of a virtual environment [#2641](https://github.com/ethyca/fides/pull/2641)

## [2.7.0](https://github.com/ethyca/fides/compare/2.6.6...2.7.0)

* Fides API
Expand Down
2 changes: 2 additions & 0 deletions src/fides/cli/commands/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from fides.core.deploy import (
check_docker_version,
check_fides_uploads_dir,
check_virtualenv,
print_deploy_success,
pull_specific_docker_image,
seed_example_data,
Expand Down Expand Up @@ -128,6 +129,7 @@ def up(ctx: click.Context, no_pull: bool = False, no_init: bool = False) -> None
Starts the sample project via docker compose.
"""

check_virtualenv()
check_docker_version()
config = ctx.obj["CONFIG"]
echo_green("Docker version is compatible, starting deploy...")
Expand Down
33 changes: 33 additions & 0 deletions src/fides/core/deploy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import webbrowser
from functools import partial
from os import environ, getcwd, makedirs
Expand Down Expand Up @@ -97,6 +98,38 @@ def check_docker_version() -> bool:
return version_is_valid


def check_virtualenv() -> bool:
"""
Check to see if the deploy is running from a virtual environment, and log a
warning if not.
In many cases, running outside of a virtual environment will lead to
mysterious Docker or Python errors, so warning the user helps get ahead of
some of these problems.
"""

# Adapted from https://stackoverflow.com/a/58026969/
# Detect a virtualenv by checking sys.prefix if the "base" prefixes match
real_prefix = getattr(sys, "real_prefix", None) # set by older virtualenv
base_prefix = getattr(sys, "base_prefix", sys.prefix)
running_in_virtualenv = (base_prefix or real_prefix) != sys.prefix

if not running_in_virtualenv:
echo_red(
f"WARNING: Your 'fides' CLI is installed outside of a virtual environment at: {sys.prefix}"
)
echo_red(
"Docker may not have permission to access this path, so if you encounter issues with Docker mounts, "
"try reinstalling 'ethyca-fides' using a virtual environment (see https://docs.python.org/3/library/venv.html)."
)
echo_red(
"If you did install using a virtual environment, check your PATH. "
"You may have a global version of 'ethyca-fides' installed that is taking precedence!"
)
return False
return True


def seed_example_data() -> None:
run_shell(
DOCKER_COMPOSE_COMMAND
Expand Down
19 changes: 19 additions & 0 deletions tests/ctl/cli/test_deploy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from unittest import mock

from fides.core import deploy

Expand All @@ -23,3 +24,21 @@ def test_convert_semver_to_list(self):
)
def test_compare_semvers(self, semver_1, semver_2, expected):
assert deploy.compare_semvers(semver_1, semver_2) is expected

@mock.patch("fides.core.deploy.sys")
def test_check_virtualenv(self, mock_sys):
# Emulate non-virtual environment
mock_sys.prefix = (
"/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9"
)
mock_sys.base_prefix = (
"/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9"
)
assert deploy.check_virtualenv() == False

# Emulate virtual environment
mock_sys.prefix = "/Users/fidesuser/fides/venv"
mock_sys.base_prefix = (
"/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9"
)
assert deploy.check_virtualenv() == True

0 comments on commit 53e13d5

Please sign in to comment.