Skip to content

Commit 16f91aa

Browse files
committed
fix: include reattaching into serverhost registration
1 parent 8f05a9d commit 16f91aa

File tree

4 files changed

+80
-23
lines changed

4 files changed

+80
-23
lines changed

controller-runtime/src/main/kotlin/app/simplecloud/controller/runtime/ControllerRuntime.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import app.simplecloud.controller.runtime.host.ServerHostRepository
1010
import app.simplecloud.controller.runtime.launcher.ControllerStartCommand
1111
import app.simplecloud.controller.runtime.oauth.OAuthServer
1212
import app.simplecloud.controller.runtime.reconciler.Reconciler
13+
import app.simplecloud.controller.runtime.server.ServerHostAttacher
1314
import app.simplecloud.controller.runtime.server.ServerNumericalIdRepository
1415
import app.simplecloud.controller.runtime.server.ServerRepository
1516
import app.simplecloud.controller.runtime.server.ServerService
@@ -32,11 +33,12 @@ class ControllerRuntime(
3233
private val database = DatabaseFactory.createDatabase(controllerStartCommand.databaseUrl)
3334
private val authCallCredentials = AuthCallCredentials(controllerStartCommand.authSecret)
3435

35-
private val dropletRepository = DropletRepository()
3636
private val groupRepository = GroupRepository(controllerStartCommand.groupPath)
3737
private val numericalIdRepository = ServerNumericalIdRepository()
3838
private val serverRepository = ServerRepository(database, numericalIdRepository)
3939
private val hostRepository = ServerHostRepository()
40+
private val serverHostAttacher = ServerHostAttacher(hostRepository, serverRepository)
41+
private val dropletRepository = DropletRepository(authCallCredentials, serverHostAttacher, hostRepository)
4042
private val pubSubService = PubSubService()
4143
private val controlPlaneServer = ControlPlaneServer(controllerStartCommand, dropletRepository)
4244
private val authServer = OAuthServer(controllerStartCommand, database)
@@ -162,7 +164,8 @@ class ControllerRuntime(
162164
controllerStartCommand.grpcHost,
163165
controllerStartCommand.pubSubGrpcPort,
164166
authCallCredentials
165-
)
167+
),
168+
serverHostAttacher
166169
)
167170
)
168171
.addService(ControllerDropletService(dropletRepository))

controller-runtime/src/main/kotlin/app/simplecloud/controller/runtime/droplet/DropletRepository.kt

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ package app.simplecloud.controller.runtime.droplet
22

33
import app.simplecloud.controller.runtime.Repository
44
import app.simplecloud.controller.runtime.envoy.DropletCache
5+
import app.simplecloud.controller.runtime.host.ServerHostRepository
6+
import app.simplecloud.controller.runtime.server.ServerHostAttacher
7+
import app.simplecloud.controller.shared.host.ServerHost
8+
import app.simplecloud.droplet.api.auth.AuthCallCredentials
59
import app.simplecloud.droplet.api.droplet.Droplet
10+
import build.buf.gen.simplecloud.controller.v1.ServerHostServiceGrpcKt
611
import kotlinx.coroutines.CoroutineScope
712
import kotlinx.coroutines.Dispatchers
813
import kotlinx.coroutines.launch
914

10-
class DropletRepository : Repository<Droplet, String> {
15+
class DropletRepository(
16+
private val authCallCredentials: AuthCallCredentials,
17+
private val serverHostAttacher: ServerHostAttacher,
18+
private val serverHostRepository: ServerHostRepository,
19+
) : Repository<Droplet, String> {
1120

1221
private val currentDroplets = mutableListOf<Droplet>()
1322
private val dropletCache = DropletCache(this)
@@ -29,11 +38,27 @@ class DropletRepository : Repository<Droplet, String> {
2938
val droplet = find(element.type, element.id)
3039
if (droplet != null) {
3140
currentDroplets[currentDroplets.indexOf(droplet)] = updated
41+
postUpdate(updated)
3242
return
3343
}
3444
currentDroplets.add(updated)
45+
postUpdate(updated)
46+
}
47+
48+
private fun postUpdate(droplet: Droplet) {
3549
CoroutineScope(Dispatchers.IO).launch {
3650
dropletCache.update()
51+
if (droplet.type != "serverhost") return@launch
52+
serverHostAttacher.attach(
53+
ServerHost(
54+
droplet.id, droplet.host, droplet.port, ServerHostServiceGrpcKt.ServerHostServiceCoroutineStub(
55+
ServerHost.createChannel(
56+
droplet.host,
57+
droplet.port
58+
)
59+
).withCallCredentials(authCallCredentials)
60+
)
61+
)
3762
}
3863
}
3964

@@ -46,6 +71,10 @@ class DropletRepository : Repository<Droplet, String> {
4671
val found = find(element.type, element.id) ?: return false
4772
if (!currentDroplets.remove(found)) return false
4873
dropletCache.update()
74+
if (element.type == "serverhost") {
75+
val host = serverHostRepository.findServerHostById(element.id) ?: return true
76+
serverHostRepository.delete(host)
77+
}
4978
return true
5079
}
5180

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package app.simplecloud.controller.runtime.server
2+
3+
import app.simplecloud.controller.runtime.host.ServerHostRepository
4+
import app.simplecloud.controller.shared.host.ServerHost
5+
import app.simplecloud.controller.shared.server.Server
6+
import io.grpc.Status
7+
import io.grpc.StatusException
8+
import kotlinx.coroutines.coroutineScope
9+
import org.apache.logging.log4j.LogManager
10+
11+
class ServerHostAttacher(
12+
private val hostRepository: ServerHostRepository,
13+
private val serverRepository: ServerRepository
14+
) {
15+
16+
private val logger = LogManager.getLogger(ServerHostAttacher::class.java)
17+
18+
suspend fun attach(serverHost: ServerHost) {
19+
hostRepository.delete(serverHost)
20+
hostRepository.save(serverHost)
21+
logger.info("Successfully registered ServerHost ${serverHost.id}.")
22+
23+
coroutineScope {
24+
serverRepository.findServersByHostId(serverHost.id).forEach { server ->
25+
logger.info("Reattaching Server ${server.uniqueId} of group ${server.group}...")
26+
try {
27+
val result = serverHost.stub?.reattachServer(server.toDefinition())
28+
?: throw StatusException(Status.INTERNAL.withDescription("Could not reattach server, is the host misconfigured?"))
29+
serverRepository.save(Server.fromDefinition(result))
30+
logger.info("Success!")
31+
} catch (e: Exception) {
32+
logger.error("Server was found to be offline, unregistering...")
33+
serverRepository.delete(server)
34+
}
35+
}
36+
}
37+
}
38+
}

controller-runtime/src/main/kotlin/app/simplecloud/controller/runtime/server/ServerService.kt

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import app.simplecloud.pubsub.PubSubClient
1111
import build.buf.gen.simplecloud.controller.v1.*
1212
import io.grpc.Status
1313
import io.grpc.StatusException
14-
import kotlinx.coroutines.coroutineScope
1514
import org.apache.logging.log4j.LogManager
1615
import java.time.LocalDateTime
1716
import java.util.*
@@ -23,6 +22,7 @@ class ServerService(
2322
private val groupRepository: GroupRepository,
2423
private val authCallCredentials: AuthCallCredentials,
2524
private val pubSubClient: PubSubClient,
25+
private val serverHostAttacher: ServerHostAttacher,
2626
) : ControllerServerServiceGrpcKt.ControllerServerServiceCoroutineImplBase() {
2727

2828
private val logger = LogManager.getLogger(ServerService::class.java)
@@ -31,25 +31,9 @@ class ServerService(
3131
override suspend fun attachServerHost(request: AttachServerHostRequest): ServerHostDefinition {
3232
val serverHost = ServerHost.fromDefinition(request.serverHost, authCallCredentials)
3333
try {
34-
hostRepository.delete(serverHost)
35-
hostRepository.save(serverHost)
34+
serverHostAttacher.attach(serverHost)
3635
} catch (e: Exception) {
37-
throw StatusException(Status.INTERNAL.withDescription("Could not save serverhost").withCause(e))
38-
}
39-
logger.info("Successfully registered ServerHost ${serverHost.id}.")
40-
41-
coroutineScope {
42-
serverRepository.findServersByHostId(serverHost.id).forEach { server ->
43-
logger.info("Reattaching Server ${server.uniqueId} of group ${server.group}...")
44-
try {
45-
val result = serverHost.stub?.reattachServer(server.toDefinition()) ?: throw StatusException(Status.INTERNAL.withDescription("Could not reattach server, is the host misconfigured?"))
46-
serverRepository.save(Server.fromDefinition(result))
47-
logger.info("Success!")
48-
} catch (e: Exception) {
49-
logger.error("Server was found to be offline, unregistering...")
50-
serverRepository.delete(server)
51-
}
52-
}
36+
throw StatusException(Status.INTERNAL.withDescription("Could not attach serverhost").withCause(e))
5337
}
5438
return serverHost.toDefinition()
5539
}
@@ -214,7 +198,10 @@ class ServerService(
214198
}
215199
}
216200

217-
private suspend fun stopServer(server: ServerDefinition, cause: ServerStopCause = ServerStopCause.NATURAL_STOP): ServerDefinition {
201+
private suspend fun stopServer(
202+
server: ServerDefinition,
203+
cause: ServerStopCause = ServerStopCause.NATURAL_STOP
204+
): ServerDefinition {
218205
val host = hostRepository.findServerHostById(server.hostId)
219206
?: throw Status.NOT_FOUND
220207
.withDescription("No server host was found matching this server.")

0 commit comments

Comments
 (0)