Skip to content
Open
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
18 changes: 18 additions & 0 deletions crt/src/crt/cmds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
class Ctx:
github_token: str | None
patches_repo_path: Path | None
run_locally: bool

def __init__(self) -> None:
self.github_token = None
self.patches_repo_path = None
self.run_locally = False


pass_ctx = click.make_pass_decorator(Ctx, ensure=True)
Expand All @@ -47,6 +49,22 @@ def __init__(self) -> None:
_P = ParamSpec("_P")


def with_run_locally(
f: Callable[Concatenate[bool, _P], _R],
) -> Callable[_P, _R]:
"""Pass the flag to don't access remotes from the context to the function."""

def inner(*args: _P.args, **kwargs: _P.kwargs) -> _R:
curr_ctx = click.get_current_context()
ctx = curr_ctx.find_object(Ctx)
if not ctx:
perror(f"missing context for '{f.__name__}'")
sys.exit(errno.ENOTRECOVERABLE)
return f(ctx.run_locally, *args, **kwargs)

return update_wrapper(inner, f)


def with_patches_repo_path(f: Callable[Concatenate[Path, _P], _R]) -> Callable[_P, _R]:
"""Pass the CES patches repo path from the context to the function."""

Expand Down
11 changes: 11 additions & 0 deletions crt/src/crt/cmds/crt.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,23 @@
required=True,
help="Path to CES patches git repository.",
)
@click.option(
"-l",
"--local-run",
"run_locally",
is_flag=True,
default=False,
required=False,
help="run without accessing remotes",
)
@pass_ctx
def cmd_crt(
ctx: Ctx,
debug: bool,
verbose: bool,
github_token: str | None,
patches_repo_path: Path,
run_locally: bool,
) -> None:
if verbose:
set_verbose_logging()
Expand All @@ -97,6 +107,7 @@ def cmd_crt(

ctx.github_token = github_token
ctx.patches_repo_path = patches_repo_path
ctx.run_locally = run_locally

if debug or verbose:
table = Table(show_header=False, show_lines=False, box=None)
Expand Down
90 changes: 87 additions & 3 deletions crt/src/crt/cmds/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@
NoSuchManifestError,
)
from crt.crtlib.errors.patchset import NoSuchPatchSetError, PatchSetError
from crt.crtlib.errors.release import NoSuchReleaseError
from crt.crtlib.errors.release import NoSuchReleaseError, ReleaseError
from crt.crtlib.git_utils import (
GitError,
git_get_remote_ref,
git_prepare_remote,
git_push,
git_remote,
git_tag_exists_in_remote,
)
from crt.crtlib.github import gh_get_pr
from crt.crtlib.manifest import (
ManifestExecuteResult,
Expand Down Expand Up @@ -646,7 +654,12 @@ def _check_repo(repo_path: Path, what: str) -> None:
progress.new_task("applying patch set to manifest")
try:
_, added, skipped = patches_apply_to_manifest(
manifest, patchset, ceph_repo_path, patches_repo_path, ctx.github_token
manifest,
patchset,
ceph_repo_path,
patches_repo_path,
ctx.github_token,
run_locally=ctx.run_locally,
)
except (ApplyError, Exception) as e:
perror(f"unable to apply to manifest: {e}")
Expand Down Expand Up @@ -683,6 +696,7 @@ def _manifest_execute(
ceph_repo_path: Path,
patches_repo_path: Path,
no_cleanup: bool = True,
run_locally: bool = False,
progress: CRTProgress,
) -> tuple[ManifestExecuteResult, RenderableType]:
"""
Expand All @@ -694,7 +708,12 @@ def _manifest_execute(

try:
res = manifest_execute(
manifest, ceph_repo_path, patches_repo_path, token, no_cleanup=no_cleanup
manifest,
ceph_repo_path,
patches_repo_path,
token,
no_cleanup=no_cleanup,
run_locally=run_locally,
)
except ApplyConflictError as e:
progress.stop_error()
Expand Down Expand Up @@ -870,6 +889,7 @@ def cmd_manifest_validate(
ceph_repo_path=ceph_repo_path,
patches_repo_path=patches_repo_path,
no_cleanup=no_cleanup,
run_locally=ctx.run_locally,
progress=progress,
)
progress.stop()
Expand Down Expand Up @@ -904,6 +924,15 @@ def cmd_manifest_validate(
default="release-dev",
help="Prefix to use for published branch.",
)
@click.option(
"-r",
"--release",
"release_name",
type=str,
required=True,
metavar="RELEASE",
help="Release associated with the manifest.",
)
@with_patches_repo_path
@pass_ctx
def cmd_manifest_publish(
Expand All @@ -912,6 +941,7 @@ def cmd_manifest_publish(
ceph_repo_path: Path,
release_branch_prefix: str,
manifest_name_or_uuid: str,
release_name: str,
) -> None:
"""
Publish a manifest.
Expand Down Expand Up @@ -945,6 +975,27 @@ def cmd_manifest_publish(
progress = CRTProgress(console)
progress.start()

try:
_prepare_release_repo(
ceph_repo_path,
patches_repo_path,
manifest,
release_name,
ctx.github_token,
)
except NoSuchReleaseError:
msg = f"release {release_name} does not exist"
logger.error(msg)
sys.exit(errno.ENOENT)
except ReleaseError as e:
msg = f"can't load release {release_name}: '{e}'"
logger.error(msg)
sys.exit(errno.EBADMSG)
except GitError as e:
msg = f"can't publish manifest {manifest.name}: '{e}'"
logger.error(msg)
sys.exit(e.ec)

execute_res, execute_summary = _manifest_execute(
manifest,
token=ctx.github_token,
Expand Down Expand Up @@ -1084,3 +1135,36 @@ def cmd_manifest_update(patches_repo_path: Path, manifest_name_or_uuid: str) ->
sys.exit(errno.ENOTRECOVERABLE)

psuccess(f"updated manifest '{manifest_name_or_uuid}' on-disk representation")


def _prepare_release_repo(
ceph_repo_path: Path,
ces_patch_path: Path,
manifest: ReleaseManifest,
release_name: str,
token: str,
) -> None:
release_branch_name = manifest.base_ref
release_tag_name = release_branch_name.replace("/", "-")
remote_name = manifest.dst_repo

if (
git_remote(ceph_repo_path, remote_name)
and git_get_remote_ref(ceph_repo_path, release_branch_name, remote_name)
and git_tag_exists_in_remote(ceph_repo_path, remote_name, release_tag_name)
):
pinfo("release repo allready prepared")
return

release = load_release(ces_patch_path, release_name)
release_repo_name = release.base_repo

git_prepare_remote(
ceph_repo_path, f"github.com/{release_repo_name}", release_repo_name, token
)
if remote_name != release_repo_name:
git_prepare_remote(
ceph_repo_path, f"github.com/{remote_name}", remote_name, token
)
git_push(ceph_repo_path, release_branch_name, remote_name)
git_push(ceph_repo_path, release_tag_name, remote_name)
24 changes: 13 additions & 11 deletions crt/src/crt/cmds/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,18 @@ def _check_repo(repo_path: Path, what: str) -> None:
else:
src_ceph_repo_path = ceph_repo_path

# update remote repo, maybe patches are not yet in the current repo state
try:
_ = git_prepare_remote(
src_ceph_repo_path,
f"github.com/{src_gh_repo}",
src_gh_repo,
ctx.github_token,
)
except Exception as e:
perror(f"unable to update remote '{src_gh_repo}': {e}")
sys.exit(errno.ENOTRECOVERABLE)
if not ctx.run_locally:
# update remote repo, maybe patches are not yet in the current repo state
try:
_ = git_prepare_remote(
src_ceph_repo_path,
f"github.com/{src_gh_repo}",
src_gh_repo,
ctx.github_token,
)
except Exception as e:
perror(f"unable to update remote '{src_gh_repo}': {e}")
sys.exit(errno.ENOTRECOVERABLE)

try:
shas = [git_revparse(src_ceph_repo_path, sha) for sha in patch_sha]
Expand Down Expand Up @@ -230,6 +231,7 @@ def _check_repo(repo_path: Path, what: str) -> None:
ceph_repo_path,
patches_repo_path,
ctx.github_token,
run_locally=ctx.run_locally,
)
except (ApplyError, Exception) as e:
perror(f"unable to apply patch sha '{sha}' to manifest: {e}")
Expand Down
61 changes: 39 additions & 22 deletions crt/src/crt/cmds/patchset.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
)
from crt.crtlib.git_utils import (
SHA,
GitError,
git_branch_delete,
git_branch_from,
git_get_patch_sha_title,
git_patches_in_interval,
git_prepare_remote,
Expand Down Expand Up @@ -547,29 +549,40 @@ def cmd_patchset_add(
progress.start()
progress.new_task(f"add patches from '{gh_repo}' branch '{patches_branch}'")

# ensure we have the specified branch in the ceph repo, so we can actually obtain
# the right shas
progress.new_task("prepare remote")
try:
remote = git_prepare_remote(
ceph_repo_path, f"github.com/{gh_repo}", gh_repo, ctx.github_token
)
except Exception as e:
progress.stop_error()
perror(f"unable to update remote '{gh_repo}': {e}")
sys.exit(errno.ENOTRECOVERABLE)

progress.done_task()
progress.new_task("fetch patches")

seq = dt.now(datetime.UTC).strftime("%Y%m%d%H%M%S")
dst_branch = f"patchset/branch/{gh_repo.replace('/', '--')}--{patches_branch}-{seq}"
try:
_ = remote.fetch(refspec=f"{patches_branch}:{dst_branch}")
except Exception as e:
progress.stop_error()
perror(f"unable to fetch branch '{patches_branch}': {e}")
sys.exit(errno.ENOTRECOVERABLE)

if not ctx.run_locally:
# ensure we have the specified branch in the ceph repo, so we can actually
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch must be squashed/fixed up with the original commit introducing this change. We want a clean history of logical changes, not a history of all your changes regardless of what they are.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rewrote commits to squash some work in progress

# obtain the right shas
progress.new_task("prepare remote")
try:
remote = git_prepare_remote(
ceph_repo_path, f"github.com/{gh_repo}", gh_repo, ctx.github_token
)
except Exception as e:
progress.stop_error()
perror(f"unable to update remote '{gh_repo}': {e}")
sys.exit(errno.ENOTRECOVERABLE)

progress.done_task()

progress.new_task("fetch patches")

try:
_ = remote.fetch(refspec=f"{patches_branch}:{dst_branch}")
except Exception as e:
progress.stop_error()
perror(f"unable to fetch branch '{patches_branch}': {e}")
sys.exit(errno.ENOTRECOVERABLE)
else:
progress.new_task("create branch patches")
try:
git_branch_from(ceph_repo_path, patches_branch, dst_branch)
except GitError as e:
progress.stop_error()
perror(f"unable to create branch '{dst_branch} from {patches_branch}': {e}")
sys.exit(errno.ENOTRECOVERABLE)

def _cleanup() -> None:
try:
Expand Down Expand Up @@ -781,7 +794,11 @@ def cmd_patchset_publish(

try:
patches = fetch_custom_patchset_patches(
ceph_repo_path, patches_repo_path, patchset, ctx.github_token
ceph_repo_path,
patches_repo_path,
patchset,
ctx.github_token,
run_locally=ctx.run_locally,
)
except Exception as e:
progress.stop_error()
Expand Down
Loading