mirror of https://github.com/hyperledger/besu
Prints configuration overview (#4451)
* print configuration overview at startup Signed-off-by: Daniel Lehrner <daniel.lehrner@consensys.net>pull/4721/head
parent
376ce82181
commit
49f32ca22d
@ -0,0 +1,140 @@ |
|||||||
|
/* |
||||||
|
* Copyright Hyperledger Besu Contributors. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.cli; |
||||||
|
|
||||||
|
import org.hyperledger.besu.BesuInfo; |
||||||
|
import org.hyperledger.besu.util.log.FramedLogMessage; |
||||||
|
import org.hyperledger.besu.util.platform.PlatformDetector; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import oshi.PlatformEnum; |
||||||
|
import oshi.SystemInfo; |
||||||
|
import oshi.hardware.HardwareAbstractionLayer; |
||||||
|
|
||||||
|
public class ConfigurationOverviewBuilder { |
||||||
|
private String network; |
||||||
|
private String dataStorage; |
||||||
|
private String syncMode; |
||||||
|
private Integer rpcPort; |
||||||
|
private Collection<String> rpcHttpApis; |
||||||
|
private Integer enginePort; |
||||||
|
private Collection<String> engineApis; |
||||||
|
private boolean isHighSpec = false; |
||||||
|
|
||||||
|
public ConfigurationOverviewBuilder setNetwork(final String network) { |
||||||
|
this.network = network; |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public ConfigurationOverviewBuilder setDataStorage(final String dataStorage) { |
||||||
|
this.dataStorage = dataStorage; |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public ConfigurationOverviewBuilder setSyncMode(final String syncMode) { |
||||||
|
this.syncMode = syncMode; |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public ConfigurationOverviewBuilder setRpcPort(final Integer rpcPort) { |
||||||
|
this.rpcPort = rpcPort; |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public ConfigurationOverviewBuilder setRpcHttpApis(final Collection<String> rpcHttpApis) { |
||||||
|
this.rpcHttpApis = rpcHttpApis; |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public ConfigurationOverviewBuilder setEnginePort(final Integer enginePort) { |
||||||
|
this.enginePort = enginePort; |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public ConfigurationOverviewBuilder setEngineApis(final Collection<String> engineApis) { |
||||||
|
this.engineApis = engineApis; |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public ConfigurationOverviewBuilder setHighSpecEnabled() { |
||||||
|
isHighSpec = true; |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public String build() { |
||||||
|
final List<String> lines = new ArrayList<>(); |
||||||
|
lines.add("Besu " + BesuInfo.class.getPackage().getImplementationVersion()); |
||||||
|
lines.add(""); |
||||||
|
lines.add("Configuration:"); |
||||||
|
|
||||||
|
if (network != null) { |
||||||
|
lines.add("Network: " + network); |
||||||
|
} |
||||||
|
|
||||||
|
if (dataStorage != null) { |
||||||
|
lines.add("Data storage: " + dataStorage); |
||||||
|
} |
||||||
|
|
||||||
|
if (syncMode != null) { |
||||||
|
lines.add("Sync mode: " + syncMode); |
||||||
|
} |
||||||
|
|
||||||
|
if (rpcHttpApis != null) { |
||||||
|
lines.add("RPC HTTP APIs: " + String.join(",", rpcHttpApis)); |
||||||
|
} |
||||||
|
if (rpcPort != null) { |
||||||
|
lines.add("RPC HTTP port: " + rpcPort); |
||||||
|
} |
||||||
|
|
||||||
|
if (engineApis != null) { |
||||||
|
lines.add("Engine APIs: " + String.join(",", engineApis)); |
||||||
|
} |
||||||
|
if (enginePort != null) { |
||||||
|
lines.add("Engine port: " + enginePort); |
||||||
|
} |
||||||
|
|
||||||
|
if (isHighSpec) { |
||||||
|
lines.add("High spec configuration enabled"); |
||||||
|
} |
||||||
|
|
||||||
|
lines.add(""); |
||||||
|
lines.add("Host:"); |
||||||
|
|
||||||
|
lines.add("Java: " + PlatformDetector.getVM()); |
||||||
|
lines.add("Maximum heap size: " + normalizeSize(Runtime.getRuntime().maxMemory())); |
||||||
|
lines.add("OS: " + PlatformDetector.getOS()); |
||||||
|
|
||||||
|
if (SystemInfo.getCurrentPlatform() == PlatformEnum.LINUX) { |
||||||
|
final String glibcVersion = PlatformDetector.getGlibc(); |
||||||
|
if (glibcVersion != null) { |
||||||
|
lines.add("glibc: " + glibcVersion); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
final HardwareAbstractionLayer hardwareInfo = new SystemInfo().getHardware(); |
||||||
|
|
||||||
|
lines.add("Total memory: " + normalizeSize(hardwareInfo.getMemory().getTotal())); |
||||||
|
lines.add("CPU cores: " + hardwareInfo.getProcessor().getLogicalProcessorCount()); |
||||||
|
|
||||||
|
return FramedLogMessage.generate(lines); |
||||||
|
} |
||||||
|
|
||||||
|
private String normalizeSize(final long size) { |
||||||
|
return String.format("%.02f", (double) (size) / 1024 / 1024 / 1024) + " GB"; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,118 @@ |
|||||||
|
/* |
||||||
|
* Copyright Hyperledger Besu Contributors. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.cli; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collection; |
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
class ConfigurationOverviewBuilderTest { |
||||||
|
private ConfigurationOverviewBuilder builder; |
||||||
|
|
||||||
|
@BeforeEach |
||||||
|
void setUp() { |
||||||
|
builder = new ConfigurationOverviewBuilder(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void setNetwork() { |
||||||
|
final String noNetworkSet = builder.build(); |
||||||
|
assertThat(noNetworkSet).doesNotContain("Network:"); |
||||||
|
|
||||||
|
builder.setNetwork("foobar"); |
||||||
|
final String networkSet = builder.build(); |
||||||
|
assertThat(networkSet).contains("Network: foobar"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void setDataStorage() { |
||||||
|
final String noDataStorageSet = builder.build(); |
||||||
|
assertThat(noDataStorageSet).doesNotContain("Data storage:"); |
||||||
|
|
||||||
|
builder.setDataStorage("bonsai"); |
||||||
|
final String dataStorageSet = builder.build(); |
||||||
|
assertThat(dataStorageSet).contains("Data storage: bonsai"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void setSyncMode() { |
||||||
|
final String noSyncModeSet = builder.build(); |
||||||
|
assertThat(noSyncModeSet).doesNotContain("Sync mode:"); |
||||||
|
|
||||||
|
builder.setSyncMode("fast"); |
||||||
|
final String syncModeSet = builder.build(); |
||||||
|
assertThat(syncModeSet).contains("Sync mode: fast"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void setRpcPort() { |
||||||
|
final String noRpcPortSet = builder.build(); |
||||||
|
assertThat(noRpcPortSet).doesNotContain("RPC HTTP port:"); |
||||||
|
|
||||||
|
builder.setRpcPort(42); |
||||||
|
final String rpcPortSet = builder.build(); |
||||||
|
assertThat(rpcPortSet).contains("RPC HTTP port: 42"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void setRpcHttpApis() { |
||||||
|
final String noRpcApisSet = builder.build(); |
||||||
|
assertThat(noRpcApisSet).doesNotContain("RPC HTTP APIs:"); |
||||||
|
|
||||||
|
final Collection<String> rpcApis = new ArrayList<>(); |
||||||
|
rpcApis.add("api1"); |
||||||
|
rpcApis.add("api2"); |
||||||
|
builder.setRpcHttpApis(rpcApis); |
||||||
|
final String rpcApisSet = builder.build(); |
||||||
|
assertThat(rpcApisSet).contains("RPC HTTP APIs: api1,api2"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void setEnginePort() { |
||||||
|
final String noEnginePortSet = builder.build(); |
||||||
|
assertThat(noEnginePortSet).doesNotContain("Engine port:"); |
||||||
|
|
||||||
|
builder.setEnginePort(42); |
||||||
|
final String enginePortSet = builder.build(); |
||||||
|
assertThat(enginePortSet).contains("Engine port: 42"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void setEngineApis() { |
||||||
|
final String noEngineApisSet = builder.build(); |
||||||
|
assertThat(noEngineApisSet).doesNotContain("Engine APIs:"); |
||||||
|
|
||||||
|
final Collection<String> engineApis = new ArrayList<>(); |
||||||
|
engineApis.add("api1"); |
||||||
|
engineApis.add("api2"); |
||||||
|
builder.setEngineApis(engineApis); |
||||||
|
final String engineApisSet = builder.build(); |
||||||
|
assertThat(engineApisSet).contains("Engine APIs: api1,api2"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void setHighSpecEnabled() { |
||||||
|
final String highSpecNotEnabled = builder.build(); |
||||||
|
assertThat(highSpecNotEnabled).doesNotContain("High spec configuration enabled"); |
||||||
|
|
||||||
|
builder.setHighSpecEnabled(); |
||||||
|
final String highSpecEnabled = builder.build(); |
||||||
|
assertThat(highSpecEnabled).contains("High spec configuration enabled"); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,72 @@ |
|||||||
|
/* |
||||||
|
* Copyright Hyperledger Besu Contributors. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.util.log; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import com.google.common.base.Splitter; |
||||||
|
import org.apache.commons.lang3.StringUtils; |
||||||
|
|
||||||
|
public class FramedLogMessage { |
||||||
|
private static final int MAX_LINE_LENGTH = 100; |
||||||
|
|
||||||
|
private FramedLogMessage() {} |
||||||
|
|
||||||
|
public static String generate(final List<String> logLines) { |
||||||
|
final StringBuilder builder = new StringBuilder("\n"); |
||||||
|
appendHeader(builder); |
||||||
|
|
||||||
|
logLines.forEach( |
||||||
|
logLine -> |
||||||
|
Splitter.fixedLength(76) |
||||||
|
.split(logLine) |
||||||
|
.forEach( |
||||||
|
splitLogLine -> |
||||||
|
builder.append( |
||||||
|
String.format( |
||||||
|
"# %s #\n", |
||||||
|
StringUtils.rightPad(splitLogLine, MAX_LINE_LENGTH - 4))))); |
||||||
|
|
||||||
|
appendFooter(builder); |
||||||
|
|
||||||
|
return builder.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
public static String generateCentered(final List<String> logLines) { |
||||||
|
final StringBuilder builder = new StringBuilder("\n"); |
||||||
|
appendHeader(builder); |
||||||
|
|
||||||
|
logLines.forEach( |
||||||
|
logLine -> |
||||||
|
builder.append( |
||||||
|
String.format("#%s#\n", StringUtils.center(logLine, MAX_LINE_LENGTH - 2)))); |
||||||
|
|
||||||
|
appendFooter(builder); |
||||||
|
|
||||||
|
return builder.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
private static void appendHeader(final StringBuilder builder) { |
||||||
|
builder.append("#".repeat(MAX_LINE_LENGTH) + "\n").append(emptyLine()); |
||||||
|
} |
||||||
|
|
||||||
|
private static void appendFooter(final StringBuilder builder) { |
||||||
|
builder.append(emptyLine()).append("#".repeat(MAX_LINE_LENGTH)); |
||||||
|
} |
||||||
|
|
||||||
|
private static String emptyLine() { |
||||||
|
return String.format("#%s#\n", StringUtils.center("", MAX_LINE_LENGTH - 2)); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue