Skip to content

Commit

Permalink
Report Artman info to metadata (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
theacodes authored Nov 16, 2018
1 parent 73d3271 commit 1481f74
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
32 changes: 32 additions & 0 deletions synthtool/gcp/artman.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import functools
import json
import os
import platform
import tempfile

from synthtool import log
from synthtool import metadata
from synthtool import shell

ARTMAN_VERSION = os.environ.get("SYNTHTOOL_ARTMAN_VERSION", "latest")
Expand All @@ -30,6 +33,30 @@ def __init__(self):
tempfile.tempdir = "/tmp"
self._ensure_dependencies_installed()
self._install_artman()
self._report_metadata()

@functools.lru_cache()
def _docker_image_info(self):
result = shell.run(
["docker", "inspect", f"googleapis/artman:{ARTMAN_VERSION}"],
hide_output=True,
)
return json.loads(result.stdout)[0]

@property
def version(self) -> str:
# The artman version is hidden in the container's environment variables.
# We could just docker run `artman --version`, but we already have the
# container info so why not? This is faster as it saves us an exec().
env_vars = dict(
value.split("=", 1) for value in self._docker_image_info()["Config"]["Env"]
)

return env_vars.get("ARTMAN_VERSION", "unknown")

@property
def docker_image(self) -> str:
return self._docker_image_info()["RepoDigests"][0]

def run(self, image, root_dir, config, *args):
"""Executes artman command in the artman container.
Expand Down Expand Up @@ -96,3 +123,8 @@ def _install_artman(self):
shell.run(
["docker", "pull", f"googleapis/artman:{ARTMAN_VERSION}"], hide_output=False
)

def _report_metadata(self):
metadata.add_generator_source(
name="artman", version=self.version, docker_image=self.docker_image
)
5 changes: 5 additions & 0 deletions synthtool/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def add_git_source(**kwargs) -> None:
_metadata.sources.add(git=metadata_pb2.GitSource(**kwargs))


def add_generator_source(**kwargs) -> None:
"""Adds a generator source to the current metadata."""
_metadata.sources.add(generator=metadata_pb2.GeneratorSource(**kwargs))


def write(outfile: str = "synth.metadata") -> None:
"""Writes out the metadata to a file."""
jsonified = google.protobuf.json_format.MessageToJson(_metadata)
Expand Down
1 change: 1 addition & 0 deletions synthtool/protos/metadata.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ message GitSource {
message GeneratorSource {
string name = 1;
string version = 2;
string docker_image = 3;
}

message TemplateSource {
Expand Down
38 changes: 28 additions & 10 deletions synthtool/protos/metadata_pb2.py

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

11 changes: 11 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ def test_add_git_source():
assert current.sources[0].git.remote == "remote"


def test_add_generator_source():
metadata.reset()

metadata.add_generator_source(name="name", version="1.2.3")

current = metadata.get()

assert current.sources[0].generator.name == "name"
assert current.sources[0].generator.version == "1.2.3"


def test_write(tmpdir):
metadata.reset()

Expand Down

0 comments on commit 1481f74

Please # to comment.