Skip to content

Commit 439cc86

Browse files
Merge pull request #134 from meshcloud/feature/ensure-git-operations-locked
feature/ensure git operations locked
2 parents 4423ad9 + b149027 commit 439cc86

File tree

5 files changed

+17
-28
lines changed

5 files changed

+17
-28
lines changed

src/main/kotlin/io/meshcloud/dockerosb/persistence/CatalogRepository.kt

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,13 @@ package io.meshcloud.dockerosb.persistence
33
import io.meshcloud.dockerosb.config.MeshServiceDefinition
44
import mu.KotlinLogging
55
import org.springframework.cloud.servicebroker.model.catalog.Catalog
6-
import org.springframework.cloud.servicebroker.model.catalog.ServiceDefinition
7-
import org.springframework.context.annotation.Bean
8-
import org.springframework.context.annotation.Configuration
96

107
private val log = KotlinLogging.logger { }
118

12-
@Configuration
139
class CatalogRepository(
1410
private val yamlHandler: YamlHandler,
1511
private val gitHandler: GitHandler
1612
) {
17-
18-
@Bean
19-
fun catalog(): Catalog {
20-
return getCatalog()
21-
}
22-
2313
fun getCatalog(): Catalog {
2414
val catalogYml = gitHandler.fileInRepo("catalog.yml")
2515

@@ -35,11 +25,11 @@ class CatalogRepository(
3525
throw IllegalArgumentException("ServiceDefinitionId cannot contain any characters other than a-z, A-Z, 0-9 and - in your catalog!")
3626
else
3727
return Catalog.builder()
38-
.serviceDefinitions(catalog.services)
39-
.build()
28+
.serviceDefinitions(catalog.services)
29+
.build()
4030
}
4131

4232
class YamlCatalog(
4333
val services: List<MeshServiceDefinition>
4434
)
45-
}
35+
}

src/main/kotlin/io/meshcloud/dockerosb/persistence/GitHandlerService.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ import java.io.File
1616
private val log = KotlinLogging.logger {}
1717

1818
/**
19-
* Note: consumers should use this only via a [GitOperationContext]
19+
* Note: consumers should use this only via a [GitOperationContext] to manage concurrent access to the git repo
2020
*/
21-
@Service
22-
class GitHandlerService(
21+
open class GitHandlerService(
2322
private val gitConfig: GitConfig
2423
) : GitHandler {
2524

@@ -191,12 +190,10 @@ class GitHandlerService(
191190

192191
fun hasLocalCommits(): Boolean {
193192
val origin = git.repository.resolve("origin/${gitConfig.remoteBranch}")
194-
val head = git.getRepository().resolve("HEAD")
193+
val head = git.repository.resolve("HEAD")
195194

196-
var count = 0
197-
for (entry in git.log().addRange(origin, head).call()) {
198-
++count
199-
}
195+
val range = git.log().addRange(origin, head).call()
196+
val count = range.count()
200197

201198
if (count > 0) {
202199
log.info { "Your branch is ahead of 'origin/${gitConfig.remoteBranch}' by $count commit(s)." }
@@ -246,7 +243,7 @@ class GitHandlerService(
246243
.call()
247244
}
248245

249-
protected fun push() {
246+
protected open fun push() {
250247
val pushCommand = git.push()
251248

252249
gitConfig.username?.let {

src/main/kotlin/io/meshcloud/dockerosb/persistence/GitOperationContextFactory.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.meshcloud.dockerosb.persistence
22

3+
import io.meshcloud.dockerosb.config.GitConfig
34
import org.springframework.stereotype.Service
45
import java.util.concurrent.locks.ReentrantLock
56

@@ -14,9 +15,10 @@ import java.util.concurrent.locks.ReentrantLock
1415
*/
1516
@Service
1617
class GitOperationContextFactory(
17-
private val gitHandler: GitHandler,
18+
gitConfig: GitConfig,
1819
private val yamlHandler: YamlHandler
1920
) {
21+
private val gitHandler = GitHandlerService(gitConfig)
2022

2123
// we have exactly one git operation that may be active at any time
2224
private val lock = ReentrantLock(true)

src/main/kotlin/io/meshcloud/dockerosb/persistence/ServiceInstanceRepository.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ package io.meshcloud.dockerosb.persistence
33
import io.meshcloud.dockerosb.model.ServiceInstance
44
import io.meshcloud.dockerosb.model.Status
55
import org.springframework.cloud.servicebroker.model.instance.OperationState
6-
import org.springframework.jmx.support.MetricType
7-
import org.springframework.stereotype.Component
86
import java.io.File
9-
import java.time.Instant
107

11-
@Component
8+
129
class ServiceInstanceRepository(private val yamlHandler: YamlHandler, private val gitHandler: GitHandler) {
1310

1411
fun createServiceInstance(serviceInstance: ServiceInstance) {

src/test/kotlin/io/meshcloud/dockerosb/ServiceBrokerFixture.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ class ServiceBrokerFixture(catalogPath: String) : Closeable {
3636

3737
val yamlHandler = YamlHandler()
3838

39+
/**
40+
* an unsafe git handler
41+
*/
3942
val gitHandler = GitHandlerService(gitConfig)
4043

41-
val contextFactory = GitOperationContextFactory(gitHandler, yamlHandler)
44+
val contextFactory = GitOperationContextFactory(gitConfig, yamlHandler)
4245

4346
val builder = TestDataBuilder(catalogPath, yamlHandler)
4447

0 commit comments

Comments
 (0)