node: Load config from disk in Driver, further cleanup

This commit is contained in:
Andras Slemmer 2016-08-02 10:23:27 +01:00
parent 303858c3e4
commit 773d53b4c8
4 changed files with 65 additions and 38 deletions

View File

@ -7,10 +7,15 @@ import com.r3corda.core.node.NodeInfo
import com.r3corda.core.node.services.ServiceType import com.r3corda.core.node.services.ServiceType
import com.r3corda.node.services.config.NodeConfiguration import com.r3corda.node.services.config.NodeConfiguration
import com.r3corda.node.services.messaging.ArtemisMessagingClient import com.r3corda.node.services.messaging.ArtemisMessagingClient
import com.r3corda.node.services.config.NodeConfigurationFromConfig
import com.r3corda.node.services.config.copy
import com.r3corda.node.services.network.InMemoryNetworkMapCache import com.r3corda.node.services.network.InMemoryNetworkMapCache
import com.r3corda.node.services.network.NetworkMapService import com.r3corda.node.services.network.NetworkMapService
import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigParseOptions
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.io.File
import java.net.ServerSocket import java.net.ServerSocket
import java.net.Socket import java.net.Socket
import java.net.SocketException import java.net.SocketException
@ -66,6 +71,7 @@ private val log: Logger = LoggerFactory.getLogger("Driver")
*/ */
fun <A> driver( fun <A> driver(
baseDirectory: String = "build/${getTimestampAsDirectoryName()}", baseDirectory: String = "build/${getTimestampAsDirectoryName()}",
nodeConfigurationPath: String = "reference.conf",
quasarJarPath: String = "lib/quasar.jar", quasarJarPath: String = "lib/quasar.jar",
portAllocation: PortAllocation = PortAllocation.Incremental(10000), portAllocation: PortAllocation = PortAllocation.Incremental(10000),
debugPortAllocation: PortAllocation = PortAllocation.Incremental(5005), debugPortAllocation: PortAllocation = PortAllocation.Incremental(5005),
@ -75,6 +81,7 @@ fun <A> driver(
portAllocation = portAllocation, portAllocation = portAllocation,
debugPortAllocation = debugPortAllocation, debugPortAllocation = debugPortAllocation,
baseDirectory = baseDirectory, baseDirectory = baseDirectory,
nodeConfigurationPath = nodeConfigurationPath,
quasarJarPath = quasarJarPath quasarJarPath = quasarJarPath
) )
driverDsl.start() driverDsl.start()
@ -125,6 +132,7 @@ class DriverDSL(
private val portAllocation: PortAllocation, private val portAllocation: PortAllocation,
private val debugPortAllocation: PortAllocation, private val debugPortAllocation: PortAllocation,
val baseDirectory: String, val baseDirectory: String,
val nodeConfigurationPath: String,
val quasarJarPath: String val quasarJarPath: String
) : DriverDSLInterface { ) : DriverDSLInterface {
@ -134,15 +142,19 @@ class DriverDSL(
private lateinit var networkMapNodeInfo: NodeInfo private lateinit var networkMapNodeInfo: NodeInfo
private val registeredProcesses = LinkedList<Process>() private val registeredProcesses = LinkedList<Process>()
val nodeConfiguration =
NodeConfigurationFromConfig(
ConfigFactory.parseResources(
nodeConfigurationPath,
ConfigParseOptions.defaults().setAllowMissing(false)
)
).copy(
myLegalName = "driver-artemis"
)
val messagingService = ArtemisMessagingClient( val messagingService = ArtemisMessagingClient(
Paths.get(baseDirectory, "driver-artemis"), Paths.get(baseDirectory, "driver-artemis"),
object : NodeConfiguration { nodeConfiguration,
override val myLegalName = "driver-artemis"
override val exportJMXto = ""
override val nearestCity = "Zion"
override val keyStorePassword = "keypass"
override val trustStorePassword = "trustpass"
},
serverHostPort = networkMapAddress, serverHostPort = networkMapAddress,
myHostPort = portAllocation.nextHostAndPort() myHostPort = portAllocation.nextHostAndPort()
) )
@ -178,7 +190,6 @@ class DriverDSL(
val apiAddress = portAllocation.nextHostAndPort() val apiAddress = portAllocation.nextHostAndPort()
val debugPort = debugPortAllocation.nextPort() val debugPort = debugPortAllocation.nextPort()
val name = providedName ?: "${pickA(name)}-${messagingAddress.port}" val name = providedName ?: "${pickA(name)}-${messagingAddress.port}"
val nearestCity = pickA(city)
val driverCliParams = NodeRunner.CliParams( val driverCliParams = NodeRunner.CliParams(
services = advertisedServices, services = advertisedServices,
@ -188,7 +199,7 @@ class DriverDSL(
messagingAddress = messagingAddress, messagingAddress = messagingAddress,
apiAddress = apiAddress, apiAddress = apiAddress,
baseDirectory = baseDirectory, baseDirectory = baseDirectory,
nearestCity = nearestCity, nodeConfigurationPath = nodeConfigurationPath,
legalName = name legalName = name
) )
registerProcess(startNode(driverCliParams, quasarJarPath, debugPort)) registerProcess(startNode(driverCliParams, quasarJarPath, debugPort))
@ -228,6 +239,8 @@ class DriverDSL(
} }
} }
private fun startNetworkMapService() { private fun startNetworkMapService() {
val apiAddress = portAllocation.nextHostAndPort() val apiAddress = portAllocation.nextHostAndPort()
val debugPort = debugPortAllocation.nextPort() val debugPort = debugPortAllocation.nextPort()
@ -239,7 +252,7 @@ class DriverDSL(
messagingAddress = networkMapAddress, messagingAddress = networkMapAddress,
apiAddress = apiAddress, apiAddress = apiAddress,
baseDirectory = baseDirectory, baseDirectory = baseDirectory,
nearestCity = pickA(city), nodeConfigurationPath = nodeConfigurationPath,
legalName = networkMapName legalName = networkMapName
) )
log.info("Starting network-map-service") log.info("Starting network-map-service")
@ -248,12 +261,6 @@ class DriverDSL(
companion object { companion object {
val city = arrayOf(
"London",
"Paris",
"New York",
"Tokyo"
)
val name = arrayOf( val name = arrayOf(
"Alice", "Alice",
"Bob", "Bob",

View File

@ -7,9 +7,12 @@ import com.r3corda.core.crypto.toBase58String
import com.r3corda.core.node.NodeInfo import com.r3corda.core.node.NodeInfo
import com.r3corda.core.node.services.ServiceType import com.r3corda.core.node.services.ServiceType
import com.r3corda.node.internal.Node import com.r3corda.node.internal.Node
import com.r3corda.node.services.config.NodeConfiguration
import com.r3corda.node.services.messaging.ArtemisMessagingClient import com.r3corda.node.services.messaging.ArtemisMessagingClient
import com.r3corda.node.services.config.NodeConfigurationFromConfig
import com.r3corda.node.services.config.copy
import com.r3corda.node.services.network.NetworkMapService import com.r3corda.node.services.network.NetworkMapService
import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigParseOptions
import joptsimple.ArgumentAcceptingOptionSpec import joptsimple.ArgumentAcceptingOptionSpec
import joptsimple.OptionParser import joptsimple.OptionParser
import joptsimple.OptionSet import joptsimple.OptionSet
@ -45,18 +48,21 @@ class NodeRunner {
} else { } else {
null null
} }
val nodeConfiguration =
NodeConfigurationFromConfig(
ConfigFactory.parseResources(
nodeConfigurationPath,
ConfigParseOptions.defaults().setAllowMissing(false)
)
).copy(
myLegalName = legalName
)
val node = Node( val node = Node(
dir = nodeDirectory, dir = nodeDirectory,
p2pAddr = messagingAddress, p2pAddr = messagingAddress,
webServerAddr = apiAddress, webServerAddr = apiAddress,
configuration = object : NodeConfiguration { configuration = nodeConfiguration,
override val myLegalName = legalName
override val exportJMXto = ""
override val nearestCity = cliParams.nearestCity
override val keyStorePassword = "keypass"
override val trustStorePassword = "trustpass"
},
networkMapAddress = networkMapNodeInfo, networkMapAddress = networkMapNodeInfo,
advertisedServices = services.toSet() advertisedServices = services.toSet()
) )
@ -75,8 +81,8 @@ class NodeRunner {
val messagingAddress: HostAndPort, val messagingAddress: HostAndPort,
val apiAddress: HostAndPort, val apiAddress: HostAndPort,
val baseDirectory: String, val baseDirectory: String,
val legalName: String, val nodeConfigurationPath: String,
val nearestCity: String val legalName: String
) { ) {
companion object { companion object {
@ -95,8 +101,8 @@ class NodeRunner {
parser.accepts("api-address").withRequiredArg().ofType(String::class.java) parser.accepts("api-address").withRequiredArg().ofType(String::class.java)
val baseDirectory = val baseDirectory =
parser.accepts("base-directory").withRequiredArg().ofType(String::class.java) parser.accepts("base-directory").withRequiredArg().ofType(String::class.java)
val nearestCity = val nodeConfigurationPath =
parser.accepts("nearest-city").withRequiredArg().ofType(String::class.java) parser.accepts("node-configuration-path").withRequiredArg().ofType(String::class.java)
val legalName = val legalName =
parser.accepts("legal-name").withRequiredArg().ofType(String::class.java) parser.accepts("legal-name").withRequiredArg().ofType(String::class.java)
@ -114,7 +120,7 @@ class NodeRunner {
val messagingAddress = requiredArgument(optionSet, messagingAddress) val messagingAddress = requiredArgument(optionSet, messagingAddress)
val apiAddress = requiredArgument(optionSet, apiAddress) val apiAddress = requiredArgument(optionSet, apiAddress)
val baseDirectory = requiredArgument(optionSet, baseDirectory) val baseDirectory = requiredArgument(optionSet, baseDirectory)
val nearestCity = requiredArgument(optionSet, nearestCity) val nodeConfigurationPath = requiredArgument(optionSet, nodeConfigurationPath)
val legalName = requiredArgument(optionSet, legalName) val legalName = requiredArgument(optionSet, legalName)
return CliParams( return CliParams(
@ -125,8 +131,8 @@ class NodeRunner {
networkMapName = networkMapName, networkMapName = networkMapName,
networkMapPublicKey = networkMapPublicKey, networkMapPublicKey = networkMapPublicKey,
networkMapAddress = networkMapAddress?.let { HostAndPort.fromString(it) }, networkMapAddress = networkMapAddress?.let { HostAndPort.fromString(it) },
legalName = legalName, nodeConfigurationPath = nodeConfigurationPath,
nearestCity = nearestCity legalName = legalName
) )
} }
} }
@ -153,8 +159,8 @@ class NodeRunner {
cliArguments.add(apiAddress.toString()) cliArguments.add(apiAddress.toString())
cliArguments.add("--base-directory") cliArguments.add("--base-directory")
cliArguments.add(baseDirectory.toString()) cliArguments.add(baseDirectory.toString())
cliArguments.add("--nearest-city") cliArguments.add("--node-configuration-path")
cliArguments.add(nearestCity) cliArguments.add(nodeConfigurationPath)
cliArguments.add("--legal-name") cliArguments.add("--legal-name")
cliArguments.add(legalName) cliArguments.add(legalName)
return cliArguments return cliArguments
@ -162,7 +168,5 @@ class NodeRunner {
} }
} }
fun createNodeRunDirectory(directory: Path) { fun createNodeRunDirectory(directory: Path) = directory.toFile().mkdirs()
directory.toFile().mkdirs()
}

View File

@ -96,4 +96,20 @@ class FullNodeConfiguration(conf: Config) : NodeConfiguration {
messagingServerAddress messagingServerAddress
) )
} }
} }
fun NodeConfiguration.copy(
myLegalName: String = this.myLegalName,
exportJMXto: String = this.exportJMXto,
nearestCity: String = this.nearestCity,
keyStorePassword: String = this.keyStorePassword,
trustStorePassword: String = this.trustStorePassword
): NodeConfiguration {
return object : NodeConfiguration {
override val myLegalName: String = myLegalName
override val exportJMXto: String = exportJMXto
override val nearestCity: String = nearestCity
override val keyStorePassword: String = keyStorePassword
override val trustStorePassword: String = trustStorePassword
}
}

View File

@ -13,7 +13,7 @@ class DriverTests {
fun simpleNodeStartupShutdownWorks() { fun simpleNodeStartupShutdownWorks() {
// Start a notary // Start a notary
val (handle, notaryNodeInfo) = driver(quasarPath = "../lib/quasar.jar") { val (handle, notaryNodeInfo) = driver(quasarJarPath = "../lib/quasar.jar") {
startNode(setOf(NotaryService.Type), "TestNotary") startNode(setOf(NotaryService.Type), "TestNotary")
} }
// Check that the node is registered in the network map // Check that the node is registered in the network map