This is an automated email from the ASF dual-hosted git repository.

sijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 009eb5d  Issue #1977: Migrate command 'bookieinit'
009eb5d is described below

commit 009eb5d5ba5c4269235aa6727a9e50fe87afbdfe
Author: Yong Zhang <[email protected]>
AuthorDate: Wed Mar 13 00:30:43 2019 +0800

    Issue #1977: Migrate command 'bookieinit'
    
    Descriptions of the changes in this PR:
    
    Migrate command `bookieinit` from shell to bkctl
    
    ### Motivation
    
     Issue #1977
    
    ### Changes
    
    - Replace command `bookieinit` in shell
    
    
    Reviewers: Sijie Guo <[email protected]>
    
    This closes #1978 from zymap/command-bookieinit, closes #1977
---
 .../org/apache/bookkeeper/bookie/BookieShell.java  |  4 +-
 .../tools/cli/commands/bookie/InitCommand.java     | 54 ++++++++++++++++++++
 .../tools/cli/commands/BookieCommandGroup.java     |  2 +
 .../tools/cli/commands/bookie/InitCommandTest.java | 57 ++++++++++++++++++++++
 4 files changed, 116 insertions(+), 1 deletion(-)

diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
index 628690e..4a75d56 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
@@ -110,6 +110,7 @@ import 
org.apache.bookkeeper.replication.ReplicationException.CompatibilityExcep
 import 
org.apache.bookkeeper.replication.ReplicationException.UnavailableException;
 import org.apache.bookkeeper.stats.NullStatsLogger;
 import org.apache.bookkeeper.tools.cli.commands.bookie.FormatCommand;
+import org.apache.bookkeeper.tools.cli.commands.bookie.InitCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookies.InfoCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand;
@@ -498,7 +499,8 @@ public class BookieShell implements Tool {
         @Override
         int runCmd(CommandLine cmdLine) throws Exception {
             ServerConfiguration conf = new ServerConfiguration(bkConf);
-            boolean result = BookKeeperAdmin.initBookie(conf);
+            InitCommand initCommand = new InitCommand();
+            boolean result = initCommand.apply(conf, new CliFlags());
             return (result) ? 0 : 1;
         }
     }
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/InitCommand.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/InitCommand.java
new file mode 100644
index 0000000..f18b475
--- /dev/null
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/InitCommand.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.bookkeeper.tools.cli.commands.bookie;
+
+import com.google.common.util.concurrent.UncheckedExecutionException;
+import org.apache.bookkeeper.client.BookKeeperAdmin;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.tools.cli.helpers.BookieCommand;
+import org.apache.bookkeeper.tools.framework.CliFlags;
+import org.apache.bookkeeper.tools.framework.CliSpec;
+
+/**
+ * A command to initialize new bookie.
+ */
+public class InitCommand extends BookieCommand<CliFlags> {
+
+    private static final String NAME = "init";
+    private static final String DESC = "Initialize new bookie.";
+
+    public InitCommand() {
+        super(CliSpec.newBuilder()
+                .withName(NAME)
+                .withDescription(DESC)
+                .withFlags(new CliFlags())
+                .build());
+    }
+
+    @Override
+    public boolean apply(ServerConfiguration conf, CliFlags cmdFlags) {
+
+        boolean result = false;
+        try {
+            result = BookKeeperAdmin.initBookie(conf);
+        } catch (Exception e) {
+            throw new UncheckedExecutionException(e.getMessage(), e);
+        }
+        return result;
+    }
+}
diff --git 
a/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java
 
b/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java
index cfa37e3..5680759 100644
--- 
a/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java
+++ 
b/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java
@@ -22,6 +22,7 @@ import static 
org.apache.bookkeeper.tools.common.BKCommandCategories.CATEGORY_IN
 
 import org.apache.bookkeeper.tools.cli.BKCtl;
 import org.apache.bookkeeper.tools.cli.commands.bookie.FormatCommand;
+import org.apache.bookkeeper.tools.cli.commands.bookie.InitCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand;
 import org.apache.bookkeeper.tools.common.BKFlags;
 import org.apache.bookkeeper.tools.framework.CliCommandGroup;
@@ -41,6 +42,7 @@ public class BookieCommandGroup extends 
CliCommandGroup<BKFlags> {
         .withParent(BKCtl.NAME)
         .withCategory(CATEGORY_INFRA_SERVICE)
         .addCommand(new LastMarkCommand())
+        .addCommand(new InitCommand())
         .addCommand(new FormatCommand())
         .build();
 
diff --git 
a/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookie/InitCommandTest.java
 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookie/InitCommandTest.java
new file mode 100644
index 0000000..8d5e7fd
--- /dev/null
+++ 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookie/InitCommandTest.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.bookkeeper.tools.cli.commands.bookie;
+
+import static org.junit.Assert.fail;
+
+import org.apache.bookkeeper.client.BookKeeperAdmin;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.tools.cli.helpers.BookieCommandTestBase;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+/**
+ * Unit test for {@link InitCommand}.
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({BookKeeperAdmin.class})
+public class InitCommandTest extends BookieCommandTestBase {
+
+    public InitCommandTest() {
+        super(3, 0);
+    }
+
+    public void setup() throws Exception {
+        
PowerMockito.whenNew(ServerConfiguration.class).withNoArguments().thenReturn(conf);
+        PowerMockito.mockStatic(BookKeeperAdmin.class);
+        PowerMockito.when(BookKeeperAdmin.initBookie(conf)).thenReturn(true);
+    }
+
+    @Test
+    public void testInitCommand() {
+        InitCommand initCommand = new InitCommand();
+        try {
+            initCommand.apply(bkFlags, new String[] { "" });
+        } catch (Exception e) {
+            fail("Should not throw any exception here.");
+        }
+    }
+}

Reply via email to