Add ssh command to show current sequence value

Change-Id: Ifcbff0943bbdacfd792a4c2454492522dec8609a
This commit is contained in:
David Ostrovsky
2019-11-09 14:12:43 -08:00
parent 8506c6fdfb
commit 4f8df3399b
9 changed files with 173 additions and 0 deletions

View File

@@ -178,6 +178,9 @@ link:cmd-reload-config.html[gerrit reload-config]::
link:cmd-set-account.html[gerrit set-account]:: link:cmd-set-account.html[gerrit set-account]::
Change an account's settings. Change an account's settings.
link:cmd-sequence-show.html[gerrit sequence show]::
Display current sequence value.
link:cmd-set-members.html[gerrit set-members]:: link:cmd-set-members.html[gerrit set-members]::
Set group members. Set group members.

View File

@@ -0,0 +1,48 @@
= gerrit sequence show
== NAME
gerrit sequence show - Display current sequence value.
== SYNOPSIS
[verse]
--
_ssh_ -p <port> <host> _gerrit sequence show_ <NAME>
--
== DESCRIPTION
Gerrit maintains the generation of the next available sequence numbers for
account, group and change entities. The sequences are stored as UTF-8 text in
a blob pointed to by the `refs/sequences/accounts`, `refs/sequences/groups`
and `refs/sequences/changes` refs. Those refs are stored in `All-Users` and
`All-Projects` git repositories correspondingly.
This command allows to display the current sequence value for those sequences.
== ACCESS
Caller must be a member of the privileged 'Administrators' group.
== SCRIPTING
This command is intended to be used in scripts.
== OPTIONS
<NAME>::
Sequence name to show the current value for.
Currently supported values:
* accounts
* groups
* changes
== EXAMPLES
Display the current value for the 'changes' sequence:
----
$ ssh -p 29418 review.example.com gerrit sequence show changes
42
----
GERRIT
------
Part of link:index.html[Gerrit Code Review]
SEARCHBOX
---------

View File

@@ -99,6 +99,7 @@ import com.google.gerrit.sshd.SshKeyCacheImpl;
import com.google.gerrit.sshd.SshModule; import com.google.gerrit.sshd.SshModule;
import com.google.gerrit.sshd.commands.DefaultCommandModule; import com.google.gerrit.sshd.commands.DefaultCommandModule;
import com.google.gerrit.sshd.commands.IndexCommandsModule; import com.google.gerrit.sshd.commands.IndexCommandsModule;
import com.google.gerrit.sshd.commands.SequenceCommandsModule;
import com.google.gerrit.sshd.plugin.LfsPluginAuthCommand; import com.google.gerrit.sshd.plugin.LfsPluginAuthCommand;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.CreationException; import com.google.inject.CreationException;
@@ -372,6 +373,7 @@ public class WebAppInitializer extends GuiceServletContextListener implements Fi
sysInjector.getInstance(DownloadConfig.class), sysInjector.getInstance(DownloadConfig.class),
sysInjector.getInstance(LfsPluginAuthCommand.Module.class))); sysInjector.getInstance(LfsPluginAuthCommand.Module.class)));
modules.add(new IndexCommandsModule(sysInjector)); modules.add(new IndexCommandsModule(sysInjector));
modules.add(new SequenceCommandsModule());
return sysInjector.createChildInjector(modules); return sysInjector.createChildInjector(modules);
} }

View File

@@ -109,6 +109,7 @@ import com.google.gerrit.sshd.SshKeyCacheImpl;
import com.google.gerrit.sshd.SshModule; import com.google.gerrit.sshd.SshModule;
import com.google.gerrit.sshd.commands.DefaultCommandModule; import com.google.gerrit.sshd.commands.DefaultCommandModule;
import com.google.gerrit.sshd.commands.IndexCommandsModule; import com.google.gerrit.sshd.commands.IndexCommandsModule;
import com.google.gerrit.sshd.commands.SequenceCommandsModule;
import com.google.gerrit.sshd.plugin.LfsPluginAuthCommand; import com.google.gerrit.sshd.plugin.LfsPluginAuthCommand;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Guice; import com.google.inject.Guice;
@@ -517,6 +518,7 @@ public class Daemon extends SiteProgram {
sysInjector.getInstance(LfsPluginAuthCommand.Module.class))); sysInjector.getInstance(LfsPluginAuthCommand.Module.class)));
if (!replica) { if (!replica) {
modules.add(new IndexCommandsModule(sysInjector)); modules.add(new IndexCommandsModule(sysInjector));
modules.add(new SequenceCommandsModule());
} }
return sysInjector.createChildInjector(modules); return sysInjector.createChildInjector(modules);
} }

View File

@@ -291,4 +291,23 @@ public class RepoSequence {
ObjectId newId = ins.insert(OBJ_BLOB, Integer.toString(val).getBytes(UTF_8)); ObjectId newId = ins.insert(OBJ_BLOB, Integer.toString(val).getBytes(UTF_8));
return new ReceiveCommand(ObjectId.zeroId(), newId, RefNames.REFS_SEQUENCES + name); return new ReceiveCommand(ObjectId.zeroId(), newId, RefNames.REFS_SEQUENCES + name);
} }
public int current() {
counterLock.lock();
try (Repository repo = repoManager.openRepository(projectName);
RevWalk rw = new RevWalk(repo)) {
Optional<IntBlob> blob = IntBlob.parse(repo, refName, rw);
int current;
if (!blob.isPresent()) {
current = seed.get();
} else {
current = blob.get().value();
}
return current;
} catch (IOException e) {
throw new StorageException(e);
} finally {
counterLock.unlock();
}
}
} }

View File

@@ -128,4 +128,16 @@ public class Sequences {
return groupSeq.next(); return groupSeq.next();
} }
} }
public int currentChangeId() {
return changeSeq.current();
}
public int currentAccountId() {
return accountSeq.current();
}
public int currentGroupId() {
return groupSeq.current();
}
} }

View File

@@ -0,0 +1,31 @@
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.sshd.commands;
import com.google.gerrit.sshd.CommandModule;
import com.google.gerrit.sshd.CommandName;
import com.google.gerrit.sshd.Commands;
import com.google.gerrit.sshd.DispatchCommandProvider;
public class SequenceCommandsModule extends CommandModule {
@Override
protected void configure() {
CommandName gerrit = Commands.named("gerrit");
CommandName sequence = Commands.named(gerrit, "sequence");
command(sequence).toProvider(new DispatchCommandProvider(sequence));
command(sequence, SequenceShowCommand.class);
}
}

View File

@@ -0,0 +1,54 @@
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.sshd.commands;
import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.server.notedb.Sequences;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject;
import org.kohsuke.args4j.Argument;
/** Display sequence value. */
@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
@CommandMetaData(name = "show", description = "Display the sequence value")
final class SequenceShowCommand extends SshCommand {
@Argument(index = 0, metaVar = "NAME", required = true, usage = "sequence name")
private String name;
@Inject Sequences sequences;
@Override
public void run() throws Exception {
int current;
switch (name) {
case "changes":
current = sequences.currentChangeId();
break;
case "accounts":
current = sequences.currentAccountId();
break;
case "groups":
current = sequences.currentGroupId();
break;
default:
throw die("Unknown sequence name: " + name);
}
stdout.print(current);
stdout.print('\n');
stdout.flush();
}
}

View File

@@ -67,6 +67,7 @@ public class SshCommandsIT extends AbstractDaemonTest {
"receive-pack", "receive-pack",
"rename-group", "rename-group",
"review", "review",
"sequence",
"set-account", "set-account",
"set-head", "set-head",
"set-members", "set-members",
@@ -96,6 +97,7 @@ public class SshCommandsIT extends AbstractDaemonTest {
"gerrit plugin", "gerrit plugin",
ImmutableList.of("add", "enable", "install", "ls", "reload", "remove", "rm")) ImmutableList.of("add", "enable", "install", "ls", "reload", "remove", "rm"))
.put("gerrit test-submit", ImmutableList.of("rule", "type")) .put("gerrit test-submit", ImmutableList.of("rule", "type"))
.put("gerrit sequence", ImmutableList.of("show"))
.build(); .build();
private static final ImmutableMap<String, List<String>> SLAVE_COMMANDS = private static final ImmutableMap<String, List<String>> SLAVE_COMMANDS =