Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions pulp-glue/pulp_glue/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -1632,21 +1632,30 @@ def upload(
self.needs_capability("upload")
size = os.path.getsize(file.name)
body: t.Dict[str, t.Any] = {**kwargs}
if not self.pulp_ctx.fake_mode: # Skip the uploading part in fake_mode

if not self.pulp_ctx.fake_mode:
if chunk_size > size:
# Small file: direct upload
body["file"] = file
elif self.pulp_ctx.has_plugin(PluginRequirement("core", specifier=">=3.20.0")):
from pulp_glue.core.context import PulpUploadContext

upload_href = PulpUploadContext(self.pulp_ctx).upload_file(file, chunk_size)
body["upload"] = upload_href
else:
from pulp_glue.core.context import PulpArtifactContext
# Large file: chunked upload
if self.pulp_ctx.has_plugin(PluginRequirement("core", specifier=">=3.20.0")):
from pulp_glue.core.context import PulpUploadContext

upload_href = PulpUploadContext(self.pulp_ctx).upload_file(file, chunk_size)
body["upload"] = upload_href
else:
from pulp_glue.core.context import PulpArtifactContext

artifact_href = PulpArtifactContext(self.pulp_ctx).upload(file, chunk_size)
body["artifact"] = artifact_href

# If no repository is provided, use synchronous upload endpoint
if repository is None:
return self.call("upload", body=body)

artifact_href = PulpArtifactContext(self.pulp_ctx).upload(file, chunk_size)
body["artifact"] = artifact_href
if repository:
body["repository"] = repository
# Repository is specified: use create endpoint (async path)
body["repository"] = repository
return self.create(body=body)


Expand Down
57 changes: 39 additions & 18 deletions pulpcore/cli/rpm/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,14 @@ def upload(
_("You must specify one (and only one) of --file or --directory.")
)

# Sanity: If directory, repository required
# Sanity: If directory with temp repo, repository required
final_dest_repo_ctx = kwargs["repository"]
if directory and not final_dest_repo_ctx:
if directory and kwargs["use_temp_repository"] and not final_dest_repo_ctx:
raise click.ClickException(
_("You must specify a --repository to use --directory uploads.")
_(
"You must specify a --repository to use --use-temp-repository "
"with --directory uploads."
)
)

# Sanity: ignore publish|use_temp unless directory has been specified
Expand All @@ -416,17 +419,20 @@ def upload(
)
else:
# Upload a directory-full of RPMs
dest_repo_ctx = None
try:
dest_repo_ctx = _determine_upload_repository(final_dest_repo_ctx, pulp_ctx, use_tmp)
result = _upload_rpms(entity_ctx, dest_repo_ctx, directory, chunk_size)
if use_tmp:
if use_tmp and dest_repo_ctx:
result = _copy_to_final(dest_repo_ctx, final_dest_repo_ctx, pulp_ctx)
finally:
if use_tmp and dest_repo_ctx:
# Remove the tmp-upload repo
dest_repo_ctx.delete()

final_version_number = _latest_version_number(final_dest_repo_ctx)
final_version_number = (
_latest_version_number(final_dest_repo_ctx) if final_dest_repo_ctx else None
)
if final_version_number:
click.echo(
_(
Expand Down Expand Up @@ -488,23 +494,36 @@ def _copy_to_final(

def _upload_rpms(
entity_ctx: PulpContentContext,
dest_repo_ctx: PulpRpmRepositoryContext,
dest_repo_ctx: t.Optional[PulpRpmRepositoryContext],
directory: t.Any,
chunk_size: int,
) -> t.Any:
rpms_path = f"{directory}/*.rpm"
rpm_names = glob.glob(rpms_path)
if not rpm_names:
raise click.ClickException(_("Directory {} has no .rpm files in it.").format(directory))
click.echo(
_(
"About to upload {} files for {}.".format(
len(rpm_names),
dest_repo_ctx.entity["name"],
)
),
err=True,
)

# Build message based on whether we have a repository or not
if dest_repo_ctx:
click.echo(
_(
"About to upload {} files for {}.".format(
len(rpm_names),
dest_repo_ctx.entity["name"],
)
),
err=True,
)
else:
click.echo(
_(
"About to upload {} files from {}.".format(
len(rpm_names),
directory,
)
),
err=True,
)
# Upload all *.rpm into the destination
successful_uploads = 0
for name in rpm_names:
Expand All @@ -525,9 +544,11 @@ def _upload_rpms(


def _determine_upload_repository(
final_dest_repo_ctx: PulpRpmRepositoryContext, pulp_ctx: PulpCLIContext, use_tmp: bool
) -> PulpRpmRepositoryContext:
if use_tmp:
final_dest_repo_ctx: t.Optional[PulpRpmRepositoryContext],
pulp_ctx: PulpCLIContext,
use_tmp: bool,
) -> t.Optional[PulpRpmRepositoryContext]:
if use_tmp and final_dest_repo_ctx:
dest_repo_ctx = PulpRpmRepositoryContext(pulp_ctx)
body: t.Dict[str, t.Any] = {
"name": f"uploadtmp_{final_dest_repo_ctx.entity['name']}_{uuid4()}",
Expand Down
Loading