|
| 1 | +""" |
| 2 | +Copyright 2022 The Magma Authors. |
| 3 | +
|
| 4 | +This source code is licensed under the BSD-style license found in the |
| 5 | +LICENSE file in the root directory of this source tree. |
| 6 | +
|
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +""" |
| 13 | + |
| 14 | +import argparse |
| 15 | +import subprocess # noqa: S404 |
| 16 | +import sys |
| 17 | + |
| 18 | + |
| 19 | +def main() -> None: |
| 20 | + """Provide command-line options to flatten MAGMA-MME OAI image""" |
| 21 | + args = _parse_args() |
| 22 | + status = perform_flattening(args.tag) |
| 23 | + sys.exit(status) |
| 24 | + |
| 25 | + |
| 26 | +def _parse_args() -> argparse.Namespace: |
| 27 | + """Parse the command line args |
| 28 | +
|
| 29 | + Returns: |
| 30 | + argparse.Namespace: the created parser |
| 31 | + """ |
| 32 | + parser = argparse.ArgumentParser(description='Flattening Image') |
| 33 | + |
| 34 | + parser.add_argument( |
| 35 | + '--tag', '-t', |
| 36 | + action='store', |
| 37 | + required=True, |
| 38 | + help='Image Tag in image-name:image tag format', |
| 39 | + ) |
| 40 | + return parser.parse_args() |
| 41 | + |
| 42 | + |
| 43 | +def perform_flattening(tag): |
| 44 | + """Parse the command line args |
| 45 | +
|
| 46 | + Args: |
| 47 | + tag: Image Tag in image-name:image tag format |
| 48 | +
|
| 49 | + Returns: |
| 50 | + int: pass / fail status |
| 51 | + """ |
| 52 | + # First detect which docker/podman command to use |
| 53 | + cli = '' |
| 54 | + image_prefix = '' |
| 55 | + cmd = 'which podman || true' |
| 56 | + podman_check = subprocess.check_output(cmd, shell=True, universal_newlines=True) # noqa: S602 |
| 57 | + if podman_check.strip(): |
| 58 | + cli = 'sudo podman' |
| 59 | + image_prefix = 'localhost/' |
| 60 | + else: |
| 61 | + cmd = 'which docker || true' |
| 62 | + docker_check = subprocess.check_output(cmd, shell=True, universal_newlines=True) # noqa: S602 |
| 63 | + if docker_check.strip(): |
| 64 | + cli = 'docker' |
| 65 | + image_prefix = '' |
| 66 | + else: |
| 67 | + print('No docker / podman installed: quitting') |
| 68 | + return -1 |
| 69 | + |
| 70 | + print(f'Flattening {tag}') |
| 71 | + # Creating a container |
| 72 | + cmd = cli + ' run --name test-flatten --entrypoint /bin/true -d ' + tag |
| 73 | + print(cmd) |
| 74 | + subprocess.check_call(cmd, shell=True, universal_newlines=True) # noqa: S602 |
| 75 | + |
| 76 | + # Export / Import trick |
| 77 | + cmd = cli + ' export test-flatten | ' + cli + ' import ' |
| 78 | + # Bizarro syntax issue with podman |
| 79 | + if cli == 'docker': |
| 80 | + cmd += ' --change "ENV PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ' |
| 81 | + else: |
| 82 | + cmd += ' --change "ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ' |
| 83 | + cmd += ' --change "WORKDIR /magma-mme" ' |
| 84 | + cmd += ' --change "EXPOSE 3870/tcp" ' |
| 85 | + cmd += ' --change "EXPOSE 5870/tcp" ' |
| 86 | + cmd += ' --change "EXPOSE 2123/udp" ' |
| 87 | + cmd += ' --change "CMD [\\"sleep\\", \\"infinity\\"]" ' # noqa: WPS342 |
| 88 | + cmd += ' - ' + image_prefix + tag |
| 89 | + print(cmd) |
| 90 | + subprocess.check_call(cmd, shell=True, universal_newlines=True) # noqa: S602 |
| 91 | + |
| 92 | + # Remove container |
| 93 | + cmd = cli + ' rm -f test-flatten' |
| 94 | + print(cmd) |
| 95 | + subprocess.check_call(cmd, shell=True, universal_newlines=True) # noqa: S602 |
| 96 | + |
| 97 | + # At this point the original image is a dangling image. |
| 98 | + # CI pipeline will clean up (`image prune --force`) |
| 99 | + return 0 |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == '__main__': |
| 103 | + main() |
0 commit comments