Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
WIP Rm already installed if --force is used
Browse files Browse the repository at this point in the history
TODO: do already_installed check with open ended version spec

Fixes ansible#266
  • Loading branch information
alikins committed May 30, 2019
1 parent be85287 commit 740db5e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
71 changes: 48 additions & 23 deletions ansible_galaxy/actions/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ansible_galaxy import install
from ansible_galaxy import installed_repository_db
from ansible_galaxy import matchers
from ansible_galaxy import repository
from ansible_galaxy import repository_spec_parse
from ansible_galaxy import requirements
from ansible_galaxy.fetch import fetch_factory
Expand Down Expand Up @@ -365,29 +366,6 @@ def install_repository(galaxy_context,

log.debug('About to find() requested requirement_spec_to_install: %s', requirement_spec_to_install)

# potential_repository_spec is a repo spec for the install candidate we potentially found.
irdb = installed_repository_db.InstalledRepositoryDatabase(galaxy_context)
log.debug('Checking to see if %s is already installed', requirement_spec_to_install)

already_installed_iter = irdb.by_requirement_spec(requirement_spec_to_install)
already_installed = sorted(list(already_installed_iter))

log.debug('already_installed: %s', already_installed)

if already_installed:
for already_installed_repository in already_installed:
display_callback('%s is already installed at %s' % (already_installed_repository.repository_spec.label,
already_installed_repository.path),
level='warning')
log.debug('Stuff %s was already installed. In %s', requirement_spec_to_install, already_installed)

return None

# TODO: The already installed check above verifies that nothing that matches the requirement spec is installed,
# but just because the name+version required wasn't installed, that doesn't mean that name at a different
# version isn't installed.
# To catch that, also need to check if the irdb by name to see if anything with that name is installed.
#
# We dont have anything that matches the RequirementSpec installed
fetcher = fetch_factory.get(galaxy_context=galaxy_context,
requirement_spec=requirement_spec_to_install)
Expand Down Expand Up @@ -431,6 +409,27 @@ def install_repository(galaxy_context,

log.debug('found_repository_spec: %s', found_repository_spec)

# See if the found collection spec is already installed and either warn or 'force_overwrite'
# to remove existing first.

# cheap 'update' is to consider anything already installed that matches the request repo_spec
# as 'installed' and let force override that.

# potential_repository_spec is a repo spec for the install candidate we potentially found.
irdb = installed_repository_db.InstalledRepositoryDatabase(galaxy_context)
log.debug('Checking to see if %s is already installed', requirement_spec_to_install)

already_installed_iter = irdb.by_requirement_spec(requirement_spec_to_install)
already_installed = sorted(list(already_installed_iter))

log.debug('already_installed: %s', already_installed)

# TODO: The already installed check above verifies that nothing that matches the requirement spec is installed,
# but just because the name+version required wasn't installed, that doesn't mean that name at a different
# version isn't installed.
# To catch that, also need to check if the irdb by name to see if anything with that name is installed.
#

repository_spec_to_install = found_repository_spec
log.debug('About to download repository requested by %s: %s', requirement_spec_to_install, repository_spec_to_install)

Expand Down Expand Up @@ -463,6 +462,32 @@ def install_repository(galaxy_context,
#
# FIXME: exc handling

# Remove the already installed version, via --force

for already_installed_repository in already_installed:
repo_label = '%s,%s' % (already_installed_repository.repository_spec.label,
already_installed_repository.repository_spec.version)

# bail if we are not overwriting already installed content
if not force_overwrite:
display_callback('%s is already installed at %s' %
(repo_label,
already_installed_repository.path),
level='warning')

log.debug('A collection providing %s was already installed. In %s', requirement_spec_to_install, already_installed)

return None

display_callback('Removing previously installed %s from %s' %
(repo_label,
already_installed_repository.path),
level='info')

log.debug('Removing already_installed %s', already_installed_repository)

repository.remove(already_installed_repository)

installed_repositories = []

try:
Expand Down
2 changes: 2 additions & 0 deletions ansible_galaxy/repository_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ def install(repository_archive, repository_spec, destination_info, display_callb
if destination_info.editable:
all_installed_files = []
else:
log.debug('destination_info.force_overrite: %s', destination_info.force_overwrite)

all_installed_files = extract(repository_spec,
collections_path=destination_info.collections_path,
extract_archive_to_dir=destination_info.path,
Expand Down

0 comments on commit 740db5e

Please sign in to comment.