Skip to content

Commit

Permalink
[#25] Use gojsonnet python module instead of using jsonnet as externa…
Browse files Browse the repository at this point in the history
…l command, lazy initalization of github web client due to issues with go (see google/go-jsonnet#484)
  • Loading branch information
netomi committed Apr 19, 2023
1 parent fb2b6ea commit a5a0efc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 34 deletions.
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build go-jsonnet and jsonnet-bundler using a go environment
# build jsonnet-bundler using a go environment
FROM docker.io/library/golang:1.18 AS builder-go
RUN go install -a github.com/google/go-jsonnet/cmd/jsonnet@v0.20.0
RUN go install -a github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb@v0.5.1

# build otterdog using a python environment
Expand Down Expand Up @@ -42,7 +41,6 @@ ENV BW_RELEASE=${BW_RELEASE}
RUN cd /tmp/ && curl -k -L -O https://github.com/bitwarden/clients/releases/download/${BW_RELEASE}/${BW_VERSION} \
&& unzip /tmp/${BW_VERSION} -d /usr/bin/ && rm -rf /tmp/${BW_VERSION}

COPY --from=builder-go /go/bin/jsonnet /usr/bin/jsonnet
COPY --from=builder-go /go/bin/jb /usr/bin/jb
COPY --from=builder-python3 /app/.venv /app/.venv
COPY --from=builder-python3 /app/otterdog /app/otterdog
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
### System requirements:

* python3.10 (mandatory): install using `apt install python3`
* jsonnet (mandatory): install using `go install -a github.com/google/go-jsonnet/cmd/jsonnet@v0.20.0`
* jsonnet-bundler (mandatory): install using `go install -a github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb@v0.5.1`
* bitwarden cli tool (optional): install using `snap install bw`
* pass cli tool (optional): install using `apt install pass`
Expand Down
7 changes: 6 additions & 1 deletion otterdog/providers/github/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
# SPDX-License-Identifier: MIT
# *******************************************************************************

from functools import cached_property
from typing import Any

from importlib_resources import files
from playwright.sync_api import sync_playwright, Page, Error

Expand All @@ -21,12 +24,14 @@ class WebClient:
def __init__(self, credentials: Credentials):
self.credentials = credentials

@cached_property
def web_settings_definition(self) -> dict[str, Any]:
# load the definition file which describes how the web settings
# can be retrieved / modified.
utils.print_trace(f"getting web_settings config using jsonnet")

web_settings_config = files(resources).joinpath("github-web-settings.jsonnet")
self.web_settings_definition = utils.jsonnet_evaluate_file(str(web_settings_config))
return utils.jsonnet_evaluate_file(str(web_settings_config))

def get_org_settings(self, org_id: str, included_keys: set[str]) -> dict[str, str]:
utils.print_debug("retrieving settings via web interface")
Expand Down
25 changes: 12 additions & 13 deletions otterdog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# SPDX-License-Identifier: MIT
# *******************************************************************************

import subprocess
import json
from typing import Any, Callable

Expand Down Expand Up @@ -154,22 +153,22 @@ def level_down(self) -> None:


def jsonnet_evaluate_file(file: str) -> dict[str, Any]:
print_trace(f"evaluating jsonnet file {file}")
status, result = subprocess.getstatusoutput(f"jsonnet {file}")
print_trace(f"result = ({status}, {result})")
import _gojsonnet

if status != 0:
raise RuntimeError(f"failed to evaluate jsonnet file {file}: {result}")
print_trace(f"evaluating jsonnet file {file}")

return json.loads(result)
try:
return json.loads(_gojsonnet.evaluate_file(file))
except Exception as ex:
raise RuntimeError(f"failed to evaluate jsonnet file: {str(ex)}")


def jsonnet_evaluate_snippet(snippet: str) -> dict[str, Any]:
print_trace(f"evaluating jsonnet snippet {snippet}")
status, result = subprocess.getstatusoutput(f'jsonnet -e "{snippet}"')
print_trace(f"result = ({status}, {result})")
import _gojsonnet

if status != 0:
raise RuntimeError(f"failed to evaluate jsonnet snippet {snippet}: {result}")
print_trace(f"evaluating jsonnet snippet {snippet}")

return json.loads(result)
try:
return json.loads(_gojsonnet.evaluate_snippet("", snippet))
except Exception as ex:
raise RuntimeError(f"failed to evaluate snippet: {str(ex)}")
40 changes: 25 additions & 15 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ requests = "~2.28.2"
jsonschema = "~4.17.3"
JSONBender = "~0.9.3"
importlib_resources = "~5.12.0"
pytest = "~7.2.1"
pytest = "~7.3.1"
requests-cache = "~1.0.1"
gojsonnet = "~0.20.0"

[tool.poetry.scripts]
otterdog = "otterdog.main:main"
Expand Down

0 comments on commit a5a0efc

Please sign in to comment.