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
import net.corda.node.internal.NodeStartup
import kotlin.system.exitProcess
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.
// 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.
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"
}
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()
assertCanNormalizeEmptyPath()
val (argsParser, cmdlineOptions) = parseArguments()
@ -51,13 +55,13 @@ open class NodeStartup(val args: Array<String>) {
println("${versionInfo.vendor} ${versionInfo.releaseVersion}")
println("Revision ${versionInfo.revision}")
println("Platform Version ${versionInfo.platformVersion}")
exitProcess(0)
return true
}
// Maybe render command line help.
if (cmdlineOptions.help) {
argsParser.printHelp(System.out)
exitProcess(0)
return true
}
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())))
} else {
println("bootstrap-raft-notaries flag not recognized, exiting...")
exitProcess(1)
return false
}
} else {
conf0
@ -79,7 +83,10 @@ open class NodeStartup(val args: Array<String>) {
banJavaSerialisation(conf)
preNetworkRegistration(conf)
maybeRegisterWithNetworkAndExit(cmdlineOptions, conf)
if (shouldRegisterWithNetwork(cmdlineOptions, conf)) {
registerWithNetwork(cmdlineOptions, conf)
return true
}
logStartupInfo(versionInfo, cmdlineOptions, conf)
try {
@ -89,13 +96,17 @@ open class NodeStartup(val args: Array<String>) {
if (e.message?.startsWith("Unknown named curve:") == true) {
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.")
} else
} else {
logger.error("Exception during node startup", e)
exitProcess(1)
}
return false
}
logger.info("Node exiting successfully")
exitProcess(0)
return true
} catch (e: Exception) {
return false
}
}
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}")
}
open protected fun maybeRegisterWithNetworkAndExit(cmdlineOptions: CmdLineOptions, conf: NodeConfiguration) {
private fun shouldRegisterWithNetwork(cmdlineOptions: CmdLineOptions, conf: NodeConfiguration): Boolean {
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("* *")
@ -165,15 +180,14 @@ open class NodeStartup(val args: Array<String>) {
println("* *")
println("******************************************************************")
NetworkRegistrationHelper(conf, HTTPNetworkRegistrationService(compatibilityZoneURL)).buildKeystore()
exitProcess(0)
}
open protected fun loadConfigFile(cmdlineOptions: CmdLineOptions): NodeConfiguration {
try {
return cmdlineOptions.loadConfig()
} catch (e: ConfigException) {
println("Unable to load the configuration file: ${e.rootCause.message}")
exitProcess(2)
} catch (configException: ConfigException) {
println("Unable to load the configuration file: ${configException.rootCause.message}")
throw configException
}
}

View File

@ -15,7 +15,6 @@ import java.io.StringWriter
import java.security.KeyPair
import java.security.KeyStore
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
@ -33,16 +32,16 @@ class NetworkRegistrationHelper(private val config: NodeConfiguration, private v
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
* some circumstances.
* Ensure the initial keystore for a node is set up.
*
* 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
* 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 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() {
config.certificatesDirectory.createDirectories()
val caKeyStore = loadOrCreateKeyStore(config.nodeKeystore, keystorePassword)
@ -62,12 +61,12 @@ class NetworkRegistrationHelper(private val config: NodeConfiguration, private v
val certificates = try {
pollServerForCertificates(requestId)
} catch (e: CertificateRequestException) {
System.err.println(e.message)
println("Please make sure the details in configuration file are correct and try again.")
println("Corda node will now terminate.")
} catch (certificateRequestException: CertificateRequestException) {
System.err.println(certificateRequestException.message)
System.err.println("Please make sure the details in configuration file are correct and try again.")
System.err.println("Corda node will now terminate.")
requestIdStore.deleteIfExists()
exitProcess(1)
throw certificateRequestException
}
println("Certificate signing request approved, storing private key with the certificate chain.")