Skip to content

Commit

Permalink
improve setup for smoke test; accommodate AMD64 OPA on ARM64 macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
rlxdev committed Nov 18, 2024
1 parent eb70650 commit 83b30f1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 22 deletions.
4 changes: 1 addition & 3 deletions .github/actions/setup-dependencies-macos/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,5 @@ runs:
- name: Download OPA executable
shell: bash
run: |
mkdir ~/scubagoggles
touch credentials.json
scubagoggles setup -d ~/scubagoggles -r ~/scubagoggles -c credentials.json
scubagoggles setup -m -nc -d ~/scubagoggles -r ~/scubagoggles -c credentials.json
scubagoggles getopa -v ${{ inputs.opa-version }} -c
4 changes: 1 addition & 3 deletions .github/actions/setup-dependencies-windows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,5 @@ runs:
- name: Download OPA executable
shell: powershell
run: |
mkdir ~/scubagoggles
echo "temp" > credentials.json
scubagoggles setup -d ~/scubagoggles -r ~/scubagoggles -c credentials.json
scubagoggles setup -m -nc -d ~/scubagoggles -r ~/scubagoggles -c credentials.json
scubagoggles getopa -v ${{ inputs.opa-version }} -c
18 changes: 18 additions & 0 deletions scubagoggles/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ def get_setup_args(parser: argparse.ArgumentParser, user_config: UserConfig):
type = path_parser,
help = 'OAuth2 credentials file for Google APIs')

parser.add_argument('--mkdir',
'-m',
default = False,
action = 'store_true',
help = 'Create directory(ies), if needed')

parser.add_argument('--nocheck',
'-nc',
default = False,
action = 'store_true',
help = 'Do not check for directory or file existence')

parser.add_argument('--noprompt',
'-np',
default = False,
action = 'store_true',
help = 'Do not prompt for missing items')

parser.add_argument('--opa_directory',
'-r',
metavar = '<directory>',
Expand Down
11 changes: 8 additions & 3 deletions scubagoggles/run_rego.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,23 @@ def find_opa(opa_path: Path = None):

opa_filenames = []

system_type = platform.system().lower()
os_type = platform.system().lower()

architectures = [platform.machine().lower()]
if architectures[0] == 'x64_64':
architectures.append('amd64')

# An ARM-based Mac can supposedly run the AMD64 version
# of OPA.
if os_type == 'darwin' and 'amd64' not in architectures:
architectures.append('amd64')

for architecture in architectures:
opa_filename = f'opa_{system_type}_{architecture}'
opa_filename = f'opa_{os_type}_{architecture}'
opa_filenames.append(opa_filename)
opa_filenames.append(f'{opa_filename}_static')

extension = '.exe' if system_type == 'windows' else ''
extension = '.exe' if os_type == 'windows' else ''

# The user may have renamed the "long" OPA executable name to shorten it,
# or may have followed the instructions for downloading OPA, which includes
Expand Down
52 changes: 39 additions & 13 deletions scubagoggles/user_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def user_directory(arguments: argparse.Namespace):
print('Setup: output directory')

config = arguments.user_config

create_dir = arguments.mkdir
prompt = not arguments.noprompt
user_dir = arguments.work_directory

if user_dir:
Expand All @@ -88,9 +89,11 @@ def user_directory(arguments: argparse.Namespace):
user_dir = user_dir.resolve()

if not user_dir.exists():
answer = prompt_boolean(f'Create directory {user_dir}')
answer = (create_dir or (prompt
and prompt_boolean(f'Create directory {user_dir}')))

if answer:
print(f' creating: {user_dir}')
user_dir.mkdir(exist_ok = True)
else:
print(f' specified directory: {user_dir}')
Expand All @@ -116,7 +119,8 @@ def user_directory(arguments: argparse.Namespace):
# Confirm with the user that this is the output directory they want
# to use. The user is also asked whether the directory should be
# created if it doesn't exist. We don't exit the loop until we
# have a valid directory.
# have a valid directory. Because the user hasn't entered any
# directory, we're forced to prompt (ignoring --noprompt).

verified = False

Expand All @@ -129,8 +133,10 @@ def user_directory(arguments: argparse.Namespace):
user_dir = Path(os.path.expandvars(answer)).expanduser()

if not user_dir.exists():
answer = prompt_boolean(f'Create directory {user_dir}')
answer = (create_dir
or prompt_boolean(f'Create directory {user_dir}'))
if answer:
print(f' creating: {user_dir}')
user_dir.mkdir(exist_ok = True)

verified = user_dir.is_dir()
Expand All @@ -155,8 +161,9 @@ def user_directory(arguments: argparse.Namespace):
user_dir = config.output_dir

if not user_dir.exists():
answer = prompt_boolean(f'Create directory {user_dir}')
answer = create_dir or prompt_boolean(f'Create directory {user_dir}')
if answer:
print(f' creating: {user_dir}')
user_dir.mkdir(exist_ok = True)
else:
print(f' {user_dir}')
Expand All @@ -179,8 +186,9 @@ def opa_directory(arguments: argparse.Namespace):

print('Setup: OPA executable directory')

check = not arguments.nocheck
config = arguments.user_config

create_dir = arguments.mkdir
opa_dir = arguments.opa_directory

if opa_dir:
Expand All @@ -189,7 +197,14 @@ def opa_directory(arguments: argparse.Namespace):
# We only need to check whether the OPA executable is indeed in that
# location.

validate_opa_dir(opa_dir)
if not opa_dir.exists() and create_dir:
print(f' creating: {opa_dir}')
opa_dir.mkdir(exist_ok = True)

if check:
validate_opa_dir(opa_dir)
else:
print(f' {opa_dir}')

config.opa_dir = opa_dir.resolve()

Expand All @@ -201,7 +216,10 @@ def opa_directory(arguments: argparse.Namespace):
# executable directory. We just validate the directory and don't
# change anything.

validate_opa_dir(config.opa_dir)
if check:
validate_opa_dir(config.opa_dir)
else:
print(f' {config.opa_dir}')

return False

Expand All @@ -213,8 +231,9 @@ def opa_directory(arguments: argparse.Namespace):
return False

# There's no OPA executable directory defined, and we haven't found the
# executable in the User's PATH, so we have to ask for the location.
# Suggest the output directory as the default location.
# executable in the User's PATH, so we have to ask for the location. Suggest
# the output directory as the default location. Because the user hasn't
# entered any directory, we're forced to prompt (ignoring --noprompt).

default_dir = config.output_dir
while not opa_dir:
Expand All @@ -225,6 +244,10 @@ def opa_directory(arguments: argparse.Namespace):

answer = Path(os.path.expandvars(answer)).expanduser().absolute()

if not answer.exists() and create_dir:
print(f' creating: {answer}')
answer.mkdir(exist_ok = True)

if validate_opa_dir(answer) or answer.is_dir():
opa_dir = answer

Expand Down Expand Up @@ -277,16 +300,17 @@ def credentials_file(arguments: argparse.Namespace):

print('Setup: Google API credentials file')

check = not arguments.nocheck
config = arguments.user_config

credentials = arguments.credentials
prompt = not arguments.noprompt

if credentials:

# The user has explicitly specified the credentials file. We only
# need to check whether the file exists.

if not credentials.is_file():
if check and not credentials.is_file():
raise FileNotFoundError(f'? {credentials} - credentials not found')

print(f' specified file: {credentials}')
Expand All @@ -308,12 +332,14 @@ def credentials_file(arguments: argparse.Namespace):
config.credentials_file = legacy_credentials
return True

elif config.credentials_file.is_file():
elif config.credentials_file.is_file() or not check:
print(f' {config.credentials_file}')
return False
else:
log.error('? %s - Google credential files missing',
config.credentials_file)
if not prompt:
return False

# There's no configuration file found, so we have to ask for the location.

Expand Down

0 comments on commit 83b30f1

Please sign in to comment.