pyinfra-dev / pyinfra

pyinfra turns Python code into shell commands and runs them on your servers. Execute ad-hoc commands and write declarative operations. Target SSH servers, local machine and Docker containers. Fast and scales from one server to thousands.
https://pyinfra.com
MIT License
3.91k stars 383 forks source link

apt.repo fails to recognise already present repository #704

Closed yggdr closed 3 years ago

yggdr commented 3 years ago

Describe the bug

Running a deploy with an apt.repo operation results in that repository being added despite being already present from a previous run.

To Reproduce

This is part of the deploy to an Ubuntu 20.04 server:

apt.repo(
    name="Add Docker apt repository",
    src=f"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu {host.get_fact(LsbRelease)['codename']} stable",
    sudo=True,
)

Running the deploy multiple times results in identical lines being added:

deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable
deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable

Expected behavior

It should be recognised that the repo is already present and nothing needs to be done.

Meta

--> Support information:

    If you are having issues with pyinfra or wish to make feature requests, please
    check out the GitHub issues at https://github.com/Fizzadar/pyinfra/issues .
    When adding an issue, be sure to include the following:

    System: FreeBSD
      Platform: FreeBSD-13.0-RELEASE-p4-amd64-64bit-ELF
      Release: 13.0-RELEASE-p4
      Machine: amd64
    pyinfra: v1.4.18
    Executable: /home/yggdrasil/.local/bin/pyinfra
    Python: 3.8.12 (CPython, Clang 11.0.1 (git@github.com:llvm/llvm-project.git llvmorg-11.0.1-0-g43ff75f2c)

+ Include pyinfra-debug.log (if one was created)

--> Connecting to hosts... [pyinfra.api.connectors.ssh] Connecting to: executor.gfnw ({'allow_agent': True, 'look_for_keys': True, 'hostname': 'executor.gfnw', 'username': 'infrastructure', 'timeout': 10}) No host key for fd4b:5320:4954:6464:4::10.189.1.70 found in known_hosts [executor.gfnw] Connected [pyinfra.api.state] Activating host: executor.gfnw

--> Preparing operations... Loading: install_executor_apt.py [pyinfra.api.facts] Getting fact: lsb_release () (ensure_hosts: (Host(executor.gfnw),)) [pyinfra.api.connectors.ssh] Running command on executor.gfnw: (pty=False) sh -c '! command -v lsb_release >/dev/null || lsb_release -ca' [executor.gfnw] >>> sh -c '! command -v lsb_release >/dev/null || lsb_release -ca' [pyinfra.api.connectors.ssh] Waiting for exit status... [pyinfra.api.connectors.ssh] Command exit status: 0 Loaded fact lsb_release () [pyinfra.api.operation] Adding operation, {'Add Docker apt repository'}, opOrder=(5,), opHash=e7bd4757a271fd0d45eb5671af07ea672a855973 [pyinfra.api.facts] Getting fact: apt_sources () (ensure_hosts: (Host(executor.gfnw),)) [pyinfra.api.connectors.ssh] Running command on executor.gfnw: (pty=False) sudo -H -n sh -c '! command -v apt >/dev/null || (! test -f /etc/apt/sources.list || cat /etc/apt/sources.list) && (cat /etc/apt/sources.list.d/.list || true)' [executor.gfnw] >>> sudo -H -n sh -c '! command -v apt >/dev/null || (! test -f /etc/apt/sources.list || cat /etc/apt/sources.list) && (cat /etc/apt/sources.list.d/.list || true)' [pyinfra.api.connectors.ssh] Waiting for exit status... [pyinfra.api.connectors.ssh] Command exit status: 0 Loaded fact apt_sources () [pyinfra.api.facts] Getting fact: find_in_file (path=/etc/apt/sources.list, pattern=^.deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable.$, interpolate_variables=False) (ensure_hosts: (Hos t(executor.gfnw),)) [pyinfra.api.connectors.ssh] Running command on executor.gfnw: (pty=False) sudo -H -n sh -c 'grep -e '"'"'^.deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable.$'"'"' /etc/apt/sources.list 2> /dev/null || ( find /etc/apt/sources.list -type f > /dev/null && echo pyinfraexists/etc/apt/sources.list || true )' [executor.gfnw] >>> sudo -H -n sh -c 'grep -e '"'"'^.deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable.$'"'"' /etc/apt/sources.list 2> /dev/null || ( find /etc/apt/sources.list -type f > /dev /null && echo pyinfraexists/etc/apt/sources.list || true )' [pyinfra.api.connectors.ssh] Waiting for exit status... [pyinfra.api.connectors.ssh] Command exit status: 0 Loaded fact find_in_file (path=/etc/apt/sources.list, pattern=^.deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable.$, interpolate_variables=False) [executor.gfnw] Ready: install_executor_apt.py

--> Proposed changes: Groups: executor / gfnwtestbed [executor.gfnw] Operations: 1 Commands: 1

--> Beginning operation run... --> Starting operation: Add Docker apt repository [pyinfra.api.operations] Starting operation Add Docker apt repository on executor.gfnw [pyinfra.api.connectors.ssh] Running command on executor.gfnw: (pty=None) sudo -H -n sh -c 'echo '"'"'deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable'"'"' >> /etc/apt/sources.list' [executor.gfnw] >>> sudo -H -n sh -c 'echo '"'"'deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable'"'"' >> /etc/apt/sources.list' [pyinfra.api.connectors.ssh] Waiting for exit status... [pyinfra.api.connectors.ssh] Command exit status: 0 [executor.gfnw] Success

--> Results: Groups: executor / gfnwtestbed [executor.gfnw] Successful: 1 Errors: 0 Commands: 1/1

Fizzadar commented 3 years ago

Thank you for reporting this @yggdr; I've just pushed two fixes related to this (https://github.com/Fizzadar/pyinfra/commit/0abfd55fe0e33d570f6cf3251fcba315967b4dde & https://github.com/Fizzadar/pyinfra/commit/c10ddc50dcbaa82a253c87ce35cd5a77f6356bec) that will be released shortly.

Fizzadar commented 3 years ago

Now released in v1.4.19.