|
| 1 | +# Copyright 2020 Adobe. All rights reserved. |
| 2 | +# This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. You may obtain a copy |
| 4 | +# of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + |
| 6 | +# Unless required by applicable law or agreed to in writing, software distributed under |
| 7 | +# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 8 | +# OF ANY KIND, either express or implied. See the License for the specific language |
| 9 | +# governing permissions and limitations under the License. |
| 10 | + |
| 11 | +def _get_path_to_kustomize_on_nixos(repo_ctx): |
| 12 | + """Ephemeral download of kustomize binary.""" |
| 13 | + command = "nix-shell --pure -p bash busybox kustomize --run 'which kustomize'" |
| 14 | + result = repo_ctx.execute([ |
| 15 | + "sh", |
| 16 | + "-c", |
| 17 | + command, |
| 18 | + ], |
| 19 | + environment = { |
| 20 | + "NIX_PATH": "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos" |
| 21 | + }) |
| 22 | + kustomize_downloaded = result.return_code == 0 |
| 23 | + if not kustomize_downloaded: |
| 24 | + fail("Failed to run '%s'" % command) |
| 25 | + |
| 26 | + path_to_kustomize = result.stdout.strip() |
| 27 | + return path_to_kustomize |
| 28 | + |
| 29 | +def is_running_on_nixos(repo_ctx): |
| 30 | + """Check if Bazel is executed on NixOS. |
| 31 | +
|
| 32 | + Args: |
| 33 | + repo_ctx: context of the repository rule, |
| 34 | + containing helper functions and information about attributes |
| 35 | +
|
| 36 | + Returns: |
| 37 | + boolean: indicating if Bazel is executed on NixOS. |
| 38 | + """ |
| 39 | + result = repo_ctx.execute([ |
| 40 | + "sh", |
| 41 | + "-c", |
| 42 | + "test -f /etc/os-release && cat /etc/os-release | head -n1", |
| 43 | + ]) |
| 44 | + os_release_file_read_success = result.return_code == 0 |
| 45 | + if not os_release_file_read_success: |
| 46 | + return False |
| 47 | + |
| 48 | + os_release_first_line = result.stdout |
| 49 | + host_is_nixos = os_release_first_line.strip() == 'NAME=NixOS' |
| 50 | + return host_is_nixos |
| 51 | + |
| 52 | +def copy_kustomize_bin_from_nix_store(repo_ctx, path): |
| 53 | + """Copy kustomize binary from nix_store to given path. |
| 54 | +
|
| 55 | + Downloads kustomize binary via nix package manger, |
| 56 | + then copies the binary to Bazel cache. This operation |
| 57 | + does not impact host configuration in any way. |
| 58 | +
|
| 59 | + Args: |
| 60 | + repo_ctx: context of the repository rule, |
| 61 | + containing helper functions and information about attributes |
| 62 | + path: path under which the kustomize should be copied |
| 63 | +
|
| 64 | + Returns: |
| 65 | + None |
| 66 | + """ |
| 67 | + repo_ctx.file("%s/kustomize" % path) |
| 68 | + result = repo_ctx.execute([ |
| 69 | + "sh", |
| 70 | + "-c", |
| 71 | + "cp -f %s %s/kustomize" % (_get_path_to_kustomize_on_nixos(repo_ctx), path), |
| 72 | + ]) |
| 73 | + if result.return_code != 0: |
| 74 | + fail("Failed to copy kustomize bin from nix_store") |
0 commit comments