osrf / rocker

A tool to run docker containers with overlays and convenient options for things like GUIs etc.
Apache License 2.0
559 stars 73 forks source link

DeprecationWarning: distro.linux_distribution() is deprecated. #160

Closed guriandoro closed 2 years ago

guriandoro commented 3 years ago

Hi all!

I've been tinkering with osrf/drone_demo's Dockerfile to update the ROS2 version used to Foxy. This is the first step into introducing other changes, which will eventually let me test adding some custom code to it. So far, I've been successful in creating the docker image (I'll send a pull request to that project once I have something that is more polished and tested) but when I try to run it I get a deprecation warning and the rocker command exits.

The command I'm using is pretty much the basic one, but with my custom image:

rocker --x11 --nvidia --user --home --pulse local_docker_deploy

I'm pasting full outputs here, in case all the context is needed:

Active extensions ['home', 'nvidia', 'pulse', 'x11', 'user']
Step 1/12 : FROM python:3-stretch as detector
 ---> b9d77e48a75c
Step 2/12 : RUN mkdir -p /tmp/distrovenv
 ---> Using cache
 ---> a9f16abcf75f
Step 3/12 : RUN python3 -m venv /tmp/distrovenv
 ---> Using cache
 ---> 0b8f77cb635c
Step 4/12 : RUN . /tmp/distrovenv/bin/activate && pip install distro pyinstaller==4.0 staticx
 ---> Using cache
 ---> 3fdfb9d2ef58
Step 5/12 : RUN apt-get update && apt-get install patchelf #needed for staticx
 ---> Using cache
 ---> de105359af53
Step 6/12 : RUN echo 'import distro; import sys; output = distro.linux_distribution(); print(output) if output[0] else sys.exit(1)' > /tmp/distrovenv/detect_os.py
 ---> Using cache
 ---> 21b996945d95
Step 7/12 : RUN . /tmp/distrovenv/bin/activate && pyinstaller --onefile /tmp/distrovenv/detect_os.py
 ---> Using cache
 ---> cd4e57f83a4f
Step 8/12 : RUN . /tmp/distrovenv/bin/activate && staticx /dist/detect_os /dist/detect_os_static
 ---> Using cache
 ---> a457573d1b79
Step 9/12 : FROM local_docker_deploy
 ---> 5bed86df7f56
Step 10/12 : COPY --from=detector /dist/detect_os_static /tmp/detect_os
 ---> Using cache
 ---> 874a4dbed8f2
Step 11/12 : ENTRYPOINT [ "/tmp/detect_os" ]
 ---> Using cache
 ---> 1d055776bab8
Step 12/12 : CMD [ "" ]
 ---> Using cache
 ---> fef90784dda1
Successfully built fef90784dda1
running,  docker run -it --rm fef90784dda1
output:  detect_os.py:1: DeprecationWarning: distro.linux_distribution() is deprecated. It should only be used as a compatibility shim with Python's platform.linux_distribution(). Please use distro.id(), distro.version() and distro.name() instead.
('Ubuntu', '20.04', 'focal')

Traceback (most recent call last):
  File "/usr/bin/rocker", line 11, in <module>
    load_entry_point('rocker==0.2.3', 'console_scripts', 'rocker')()
  File "/usr/lib/python3/dist-packages/rocker/cli.py", line 65, in main
    dig = DockerImageGenerator(active_extensions, args_dict, base_image)
  File "/usr/lib/python3/dist-packages/rocker/core.py", line 202, in __init__
    self.dockerfile = generate_dockerfile(active_extensions, self.cliargs, base_image)
  File "/usr/lib/python3/dist-packages/rocker/core.py", line 319, in generate_dockerfile
    dockerfile_str += el.get_preamble(args_dict) + '\n'
  File "/usr/lib/python3/dist-packages/rocker/nvidia_extension.py", line 117, in get_preamble
    return em.expand(preamble, self.get_environment_subs(cliargs))
  File "/usr/lib/python3/dist-packages/rocker/nvidia_extension.py", line 97, in get_environment_subs
    detected_os = detect_os(cliargs['base_image'], print, nocache=cliargs.get('nocache', False))
  File "/usr/lib/python3/dist-packages/rocker/os_detector.py", line 70, in detect_os
    _detect_os_cache[image_name] = literal_eval(output.strip())
  File "/usr/lib/python3.8/ast.py", line 59, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python3.8/ast.py", line 47, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    detect_os.py:1: DeprecationWarning: distro.linux_distribution() is deprecated. It should only be used as a compatibility shim with Python's platform.linux_distribution(). Please use distro.id(), distro.version() and distro.name() instead.
                ^
SyntaxError: invalid syntax

I was able to trace this down to this project, in the detect_nvidia_docker branch:

agustin@zork-scf:~/src/rocker/src/rocker$ git checkout -b detect_nvidia_docker remotes/origin/detect_nvidia_docker
Branch 'detect_nvidia_docker' set up to track remote branch 'detect_nvidia_docker' from 'origin'.
Switched to a new branch 'detect_nvidia_docker'

agustin@zork-scf:~/src/rocker/src/rocker$ grep linux_distribution *.py
os_detector.py:RUN echo 'import distro; import sys; output = distro.linux_distribution(); print(output) if output[0] else sys.exit(1)' > /tmp/distrovenv/detect_os.py

I think the fix should be simple enough, by using the recommended functions instead:

DeprecationWarning: distro.linux_distribution() is deprecated. 
It should only be used as a compatibility shim with Python's platform.linux_distribution(). 
Please use distro.id(), distro.version() and distro.name() instead.

But I'm unsure as to how I could test these changes myself. Can anyone help in either double-checking and implementing, or to point me in the right direction as to how I can do it?

I hope this is the right place to send this. Let me know if I should add anything else to the description, or if I should be sending this message somewhere else.

Thanks in advance! Agustín.

tfoote commented 2 years ago

The fix for that is in #156 which was released into v0.2.4 make sure you're up to date with your installation of rocker. That branch is out of date and still has the old code but you shouldn't need that branch for the demo.

guriandoro commented 2 years ago

Hi @tfoote,

Great! Indeed, I was using v0.2.3. After upgrading to latest, it is working as expected. I'm closing this one out.

Thanks again!