Skip to content

Commit

Permalink
first step in fixing smoke test workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
rlxdev committed Nov 15, 2024
1 parent 2647b45 commit eb70650
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
11 changes: 6 additions & 5 deletions .github/actions/setup-dependencies-macos/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inputs:
default: "macos"
opa-version:
required: true
default: "0.60.0"
default: "v0.60.0"
python-version:
required: true

Expand All @@ -15,8 +15,7 @@ runs:
- name: Setup virtualenv
shell: bash
run: |
pip install virtualenv
virtualenv -p python .venv
python -m venv .venv
source .venv/bin/activate
- name: Install dependencies
Expand All @@ -30,5 +29,7 @@ runs:
- name: Download OPA executable
shell: bash
run: |
python download_opa.py -v ${{ inputs.opa-version }} -os ${{ inputs.operating-system }}
chmod +x opa_darwin_amd64
mkdir ~/scubagoggles
touch credentials.json
scubagoggles setup -d ~/scubagoggles -r ~/scubagoggles -c credentials.json
scubagoggles getopa -v ${{ inputs.opa-version }} -c
11 changes: 7 additions & 4 deletions .github/actions/setup-dependencies-windows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inputs:
default: "windows"
opa-version:
required: true
default: "0.60.0"
default: "v0.60.0"
python-version:
required: true

Expand All @@ -15,9 +15,8 @@ runs:
- name: Setup virtualenv
shell: powershell
run: |
pip install virtualenv
python -m venv .venv
.venv\Scripts\activate
.venv\Scripts\activate.ps1
- name: Install dependencies
shell: powershell
Expand All @@ -29,4 +28,8 @@ runs:
- name: Download OPA executable
shell: powershell
run: python download_opa.py -v ${{ inputs.opa-version }} -os ${{ inputs.operating-system }}
run: |
mkdir ~/scubagoggles
echo "temp" > credentials.json
scubagoggles setup -d ~/scubagoggles -r ~/scubagoggles -c credentials.json
scubagoggles getopa -v ${{ inputs.opa-version }} -c
44 changes: 38 additions & 6 deletions scubagoggles/getopa.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from hashlib import sha256
from pathlib import Path
from tempfile import TemporaryDirectory
from urllib.error import HTTPError
from urllib.parse import urljoin, urlsplit
from urllib.request import Request, urlcleanup, urlopen, urlretrieve

Expand Down Expand Up @@ -74,7 +75,10 @@ def download_opa(opa_dir: Path,
by OPA does not look like a version (vM.m.b).
"""

# pylint: disable=too-many-branches

opa_base_url = 'https://github.com/open-policy-agent/opa/releases/'
version_re = re.compile(r'v\d+(?:\.\d+){2}')

if not version:
latest_version_url = urljoin(opa_base_url, 'latest')
Expand All @@ -85,9 +89,12 @@ def download_opa(opa_dir: Path,
version = urlsplit(response.url).path.split('/')[-1]
log.debug(' Version returned: %s', version)

if not re.match(r'v\d+(?:\.\d+){2}', version):
if not version_re.match(version):
raise RuntimeError(f'? "{version}" - unrecognized version string '
'returned as "latest" OPA version')
elif not version_re.match(version):
raise UserRuntimeError(f'? "{version}" - unrecognized version string - '
'expected "v<X>.<Y>.<Z>"')

os_type = platform.system().lower()

Expand Down Expand Up @@ -117,8 +124,23 @@ def download_opa(opa_dir: Path,
log.debug('Overwriting existing file: %s', str(output_file))
output_file.unlink()

urlretrieve(download_url, output_file)
urlcleanup()
log.debug('Download URL: %s', download_url)

try:
urlretrieve(download_url, output_file)
except HTTPError as http_error:
log.error('HTTP error %d returned trying to access %s',
http_error.code,
download_url)
if http_error.code == 404:
# I want the error I'm raising in this instance ONLY.
# pylint: disable-next=raise-missing-from
raise UserRuntimeError('Unable to download OPA executable for '
f'version {version} - check version')
raise UserRuntimeError('Failure downloading OPA executable') \
from http_error
finally:
urlcleanup()

mode = output_file.stat().st_mode
new_mode = mode | stat.S_IXUSR | stat.S_IRUSR
Expand Down Expand Up @@ -183,9 +205,19 @@ def verify_opa(download_url: str, opa_exe_file: Path):
hash_file_name = opa_exe_file.name + hash_suffix
hash_file = Path(temp_dir, hash_file_name)

log.debug('Downloading %s to %s', hash_file_name, temp_dir)
urlretrieve(hash_file_url, hash_file)
urlcleanup()
log.debug(' Downloading %s to %s', hash_file_name, temp_dir)
log.debug(' Download URL: %s', download_url)

try:
urlretrieve(hash_file_url, hash_file)
except HTTPError as http_error:
log.error('HTTP error %d returned trying to access %s',
http_error.code,
download_url)
raise UserRuntimeError('Failure downloading OPA hash file') \
from http_error
finally:
urlcleanup()

contents = hash_file.read_text(encoding = 'utf-8')

Expand Down
2 changes: 1 addition & 1 deletion scubagoggles/user_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def user_directory(arguments: argparse.Namespace):
if not user_dir.exists():
answer = prompt_boolean(f'Create directory {user_dir}')

if strtobool(answer):
if answer:
user_dir.mkdir(exist_ok = True)
else:
print(f' specified directory: {user_dir}')
Expand Down

0 comments on commit eb70650

Please sign in to comment.