remove calls to exitnodes from NodeStartup (#2080)

This commit is contained in:
Alberto Arri 2017-12-04 15:44:18 +00:00 committed by GitHub
parent ca06132c49
commit b6427a3128
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 76 deletions

View File

@ -4,10 +4,11 @@
package net.corda.node package net.corda.node
import net.corda.node.internal.NodeStartup import net.corda.node.internal.NodeStartup
import kotlin.system.exitProcess
fun main(args: Array<String>) { fun main(args: Array<String>) {
// Pass the arguments to the Node factory. In the Enterprise edition, this line is modified to point to a subclass. // Pass the arguments to the Node factory. In the Enterprise edition, this line is modified to point to a subclass.
// It will exit the process in case of startup failure and is not intended to be used by embedders. If you want // It will exit the process in case of startup failure and is not intended to be used by embedders. If you want
// to embed Node in your own container, instantiate it directly and set up the configuration objects yourself. // to embed Node in your own container, instantiate it directly and set up the configuration objects yourself.
NodeStartup(args).run() exitProcess(if (NodeStartup(args).run()) 0 else 1)
} }

View File

@ -34,7 +34,11 @@ open class NodeStartup(val args: Array<String>) {
val LOGS_CAN_BE_FOUND_IN_STRING = "Logs can be found in" val LOGS_CAN_BE_FOUND_IN_STRING = "Logs can be found in"
} }
open fun run() { /**
* @return true if the node startup was successful. This value is intended to be the exit code of the process.
*/
open fun run(): Boolean {
try {
val startTime = System.currentTimeMillis() val startTime = System.currentTimeMillis()
assertCanNormalizeEmptyPath() assertCanNormalizeEmptyPath()
val (argsParser, cmdlineOptions) = parseArguments() val (argsParser, cmdlineOptions) = parseArguments()
@ -51,13 +55,13 @@ open class NodeStartup(val args: Array<String>) {
println("${versionInfo.vendor} ${versionInfo.releaseVersion}") println("${versionInfo.vendor} ${versionInfo.releaseVersion}")
println("Revision ${versionInfo.revision}") println("Revision ${versionInfo.revision}")
println("Platform Version ${versionInfo.platformVersion}") println("Platform Version ${versionInfo.platformVersion}")
exitProcess(0) return true
} }
// Maybe render command line help. // Maybe render command line help.
if (cmdlineOptions.help) { if (cmdlineOptions.help) {
argsParser.printHelp(System.out) argsParser.printHelp(System.out)
exitProcess(0) return true
} }
drawBanner(versionInfo) drawBanner(versionInfo)
@ -71,7 +75,7 @@ open class NodeStartup(val args: Array<String>) {
conf0.copy(notary = conf0.notary?.copy(raft = conf0.notary?.raft?.copy(clusterAddresses = emptyList()))) conf0.copy(notary = conf0.notary?.copy(raft = conf0.notary?.raft?.copy(clusterAddresses = emptyList())))
} else { } else {
println("bootstrap-raft-notaries flag not recognized, exiting...") println("bootstrap-raft-notaries flag not recognized, exiting...")
exitProcess(1) return false
} }
} else { } else {
conf0 conf0
@ -79,7 +83,10 @@ open class NodeStartup(val args: Array<String>) {
banJavaSerialisation(conf) banJavaSerialisation(conf)
preNetworkRegistration(conf) preNetworkRegistration(conf)
maybeRegisterWithNetworkAndExit(cmdlineOptions, conf) if (shouldRegisterWithNetwork(cmdlineOptions, conf)) {
registerWithNetwork(cmdlineOptions, conf)
return true
}
logStartupInfo(versionInfo, cmdlineOptions, conf) logStartupInfo(versionInfo, cmdlineOptions, conf)
try { try {
@ -89,13 +96,17 @@ open class NodeStartup(val args: Array<String>) {
if (e.message?.startsWith("Unknown named curve:") == true) { if (e.message?.startsWith("Unknown named curve:") == true) {
logger.error("Exception during node startup - ${e.message}. " + logger.error("Exception during node startup - ${e.message}. " +
"This is a known OpenJDK issue on some Linux distributions, please use OpenJDK from zulu.org or Oracle JDK.") "This is a known OpenJDK issue on some Linux distributions, please use OpenJDK from zulu.org or Oracle JDK.")
} else } else {
logger.error("Exception during node startup", e) logger.error("Exception during node startup", e)
exitProcess(1) }
return false
} }
logger.info("Node exiting successfully") logger.info("Node exiting successfully")
exitProcess(0) return true
} catch (e: Exception) {
return false
}
} }
open protected fun preNetworkRegistration(conf: NodeConfiguration) = Unit open protected fun preNetworkRegistration(conf: NodeConfiguration) = Unit
@ -155,9 +166,13 @@ open class NodeStartup(val args: Array<String>) {
logger.info("Starting as node on ${conf.p2pAddress}") logger.info("Starting as node on ${conf.p2pAddress}")
} }
open protected fun maybeRegisterWithNetworkAndExit(cmdlineOptions: CmdLineOptions, conf: NodeConfiguration) { private fun shouldRegisterWithNetwork(cmdlineOptions: CmdLineOptions, conf: NodeConfiguration): Boolean {
val compatibilityZoneURL = conf.compatibilityZoneURL val compatibilityZoneURL = conf.compatibilityZoneURL
if (!cmdlineOptions.isRegistration || compatibilityZoneURL == null) return return !(!cmdlineOptions.isRegistration || compatibilityZoneURL == null)
}
open protected fun registerWithNetwork(cmdlineOptions: CmdLineOptions, conf: NodeConfiguration) {
val compatibilityZoneURL = conf.compatibilityZoneURL!!
println() println()
println("******************************************************************") println("******************************************************************")
println("* *") println("* *")
@ -165,15 +180,14 @@ open class NodeStartup(val args: Array<String>) {
println("* *") println("* *")
println("******************************************************************") println("******************************************************************")
NetworkRegistrationHelper(conf, HTTPNetworkRegistrationService(compatibilityZoneURL)).buildKeystore() NetworkRegistrationHelper(conf, HTTPNetworkRegistrationService(compatibilityZoneURL)).buildKeystore()
exitProcess(0)
} }
open protected fun loadConfigFile(cmdlineOptions: CmdLineOptions): NodeConfiguration { open protected fun loadConfigFile(cmdlineOptions: CmdLineOptions): NodeConfiguration {
try { try {
return cmdlineOptions.loadConfig() return cmdlineOptions.loadConfig()
} catch (e: ConfigException) { } catch (configException: ConfigException) {
println("Unable to load the configuration file: ${e.rootCause.message}") println("Unable to load the configuration file: ${configException.rootCause.message}")
exitProcess(2) throw configException
} }
} }

View File

@ -15,7 +15,6 @@ import java.io.StringWriter
import java.security.KeyPair import java.security.KeyPair
import java.security.KeyStore import java.security.KeyStore
import java.security.cert.Certificate import java.security.cert.Certificate
import kotlin.system.exitProcess
/** /**
* Helper for managing the node registration process, which checks for any existing certificates and requests them if * Helper for managing the node registration process, which checks for any existing certificates and requests them if
@ -33,16 +32,16 @@ class NetworkRegistrationHelper(private val config: NodeConfiguration, private v
private val privateKeyPassword = config.keyStorePassword private val privateKeyPassword = config.keyStorePassword
/** /**
* Ensure the initial keystore for a node is set up; note that this function may cause the process to exit under * Ensure the initial keystore for a node is set up.
* some circumstances.
* *
* This checks the "config.certificatesDirectory" field for certificates required to connect to a Corda network. * This checks the "config.certificatesDirectory" field for certificates required to connect to a Corda network.
* If the certificates are not found, a PKCS #10 certification request will be submitted to the * If the certificates are not found, a PKCS #10 certification request will be submitted to the
* Corda network permissioning server via [NetworkRegistrationService]. This process will enter a polling loop until * Corda network permissioning server via [NetworkRegistrationService]. This process will enter a polling loop until
* the request has been approved, and then the certificate chain will be downloaded and stored in [KeyStore] reside in * the request has been approved, and then the certificate chain will be downloaded and stored in [KeyStore] reside in
* the certificates directory. * the certificates directory.
*
* @throws CertificateRequestException if the certificate retrieved by doorman is invalid.
*/ */
// TODO: Stop killing the calling process from within a called function.
fun buildKeystore() { fun buildKeystore() {
config.certificatesDirectory.createDirectories() config.certificatesDirectory.createDirectories()
val caKeyStore = loadOrCreateKeyStore(config.nodeKeystore, keystorePassword) val caKeyStore = loadOrCreateKeyStore(config.nodeKeystore, keystorePassword)
@ -62,12 +61,12 @@ class NetworkRegistrationHelper(private val config: NodeConfiguration, private v
val certificates = try { val certificates = try {
pollServerForCertificates(requestId) pollServerForCertificates(requestId)
} catch (e: CertificateRequestException) { } catch (certificateRequestException: CertificateRequestException) {
System.err.println(e.message) System.err.println(certificateRequestException.message)
println("Please make sure the details in configuration file are correct and try again.") System.err.println("Please make sure the details in configuration file are correct and try again.")
println("Corda node will now terminate.") System.err.println("Corda node will now terminate.")
requestIdStore.deleteIfExists() requestIdStore.deleteIfExists()
exitProcess(1) throw certificateRequestException
} }
println("Certificate signing request approved, storing private key with the certificate chain.") println("Certificate signing request approved, storing private key with the certificate chain.")