Skip to content

Commit

Permalink
Merge pull request #134 from meshcloud/feature/ensure-git-operations-…
Browse files Browse the repository at this point in the history
…locked

feature/ensure git operations locked
  • Loading branch information
JohannesRudolph committed Jun 24, 2024
2 parents 4423ad9 + b149027 commit 439cc86
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@ package io.meshcloud.dockerosb.persistence
import io.meshcloud.dockerosb.config.MeshServiceDefinition
import mu.KotlinLogging
import org.springframework.cloud.servicebroker.model.catalog.Catalog
import org.springframework.cloud.servicebroker.model.catalog.ServiceDefinition
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

private val log = KotlinLogging.logger { }

@Configuration
class CatalogRepository(
private val yamlHandler: YamlHandler,
private val gitHandler: GitHandler
) {

@Bean
fun catalog(): Catalog {
return getCatalog()
}

fun getCatalog(): Catalog {
val catalogYml = gitHandler.fileInRepo("catalog.yml")

Expand All @@ -35,11 +25,11 @@ class CatalogRepository(
throw IllegalArgumentException("ServiceDefinitionId cannot contain any characters other than a-z, A-Z, 0-9 and - in your catalog!")
else
return Catalog.builder()
.serviceDefinitions(catalog.services)
.build()
.serviceDefinitions(catalog.services)
.build()
}

class YamlCatalog(
val services: List<MeshServiceDefinition>
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import java.io.File
private val log = KotlinLogging.logger {}

/**
* Note: consumers should use this only via a [GitOperationContext]
* Note: consumers should use this only via a [GitOperationContext] to manage concurrent access to the git repo
*/
@Service
class GitHandlerService(
open class GitHandlerService(
private val gitConfig: GitConfig
) : GitHandler {

Expand Down Expand Up @@ -191,12 +190,10 @@ class GitHandlerService(

fun hasLocalCommits(): Boolean {
val origin = git.repository.resolve("origin/${gitConfig.remoteBranch}")
val head = git.getRepository().resolve("HEAD")
val head = git.repository.resolve("HEAD")

var count = 0
for (entry in git.log().addRange(origin, head).call()) {
++count
}
val range = git.log().addRange(origin, head).call()
val count = range.count()

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

protected fun push() {
protected open fun push() {
val pushCommand = git.push()

gitConfig.username?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.meshcloud.dockerosb.persistence

import io.meshcloud.dockerosb.config.GitConfig
import org.springframework.stereotype.Service
import java.util.concurrent.locks.ReentrantLock

Expand All @@ -14,9 +15,10 @@ import java.util.concurrent.locks.ReentrantLock
*/
@Service
class GitOperationContextFactory(
private val gitHandler: GitHandler,
gitConfig: GitConfig,
private val yamlHandler: YamlHandler
) {
private val gitHandler = GitHandlerService(gitConfig)

// we have exactly one git operation that may be active at any time
private val lock = ReentrantLock(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package io.meshcloud.dockerosb.persistence
import io.meshcloud.dockerosb.model.ServiceInstance
import io.meshcloud.dockerosb.model.Status
import org.springframework.cloud.servicebroker.model.instance.OperationState
import org.springframework.jmx.support.MetricType
import org.springframework.stereotype.Component
import java.io.File
import java.time.Instant

@Component

class ServiceInstanceRepository(private val yamlHandler: YamlHandler, private val gitHandler: GitHandler) {

fun createServiceInstance(serviceInstance: ServiceInstance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ class ServiceBrokerFixture(catalogPath: String) : Closeable {

val yamlHandler = YamlHandler()

/**
* an unsafe git handler
*/
val gitHandler = GitHandlerService(gitConfig)

val contextFactory = GitOperationContextFactory(gitHandler, yamlHandler)
val contextFactory = GitOperationContextFactory(gitConfig, yamlHandler)

val builder = TestDataBuilder(catalogPath, yamlHandler)

Expand Down

0 comments on commit 439cc86

Please sign in to comment.