Skip to content

Commit

Permalink
Reuse cloud agent identifiers
Browse files Browse the repository at this point in the history
Issue: #35
  • Loading branch information
dtretyakov committed Mar 29, 2017
1 parent 07d4123 commit 804bfc6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,17 @@ package jetbrains.buildServer.clouds.azure.arm

import jetbrains.buildServer.clouds.CloudClientParameters
import jetbrains.buildServer.clouds.azure.AzureCloudClientBase
import jetbrains.buildServer.clouds.azure.FileIdProvider
import jetbrains.buildServer.clouds.azure.arm.connector.AzureApiConnector
import jetbrains.buildServer.clouds.base.connector.CloudApiConnector
import jetbrains.buildServer.serverSide.crypt.EncryptUtil

import java.io.File

/**
* ARM cloud client.
*/
class AzureCloudClient(params: CloudClientParameters,
apiConnector: CloudApiConnector<AzureCloudImage, AzureCloudInstance>,
private val myAzureIdxStorage: File)
apiConnector: CloudApiConnector<AzureCloudImage, AzureCloudInstance>)
: AzureCloudClientBase<AzureCloudInstance, AzureCloudImage, AzureCloudImageDetails>(params, apiConnector) {

override fun checkAndCreateImage(imageDetails: AzureCloudImageDetails): AzureCloudImage {
val fileName = EncryptUtil.md5(imageDetails.sourceName) + ".idx"
val idProvider = FileIdProvider(File(myAzureIdxStorage, fileName))
return AzureCloudImage(imageDetails, myApiConnector as AzureApiConnector, idProvider)
return AzureCloudImage(imageDetails, myApiConnector as AzureApiConnector)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,18 @@ import jetbrains.buildServer.clouds.base.AbstractCloudClientFactory
import jetbrains.buildServer.clouds.base.errors.TypedCloudErrorInfo
import jetbrains.buildServer.serverSide.AgentDescription
import jetbrains.buildServer.serverSide.PropertiesProcessor
import jetbrains.buildServer.serverSide.ServerPaths
import jetbrains.buildServer.serverSide.ServerSettings
import jetbrains.buildServer.web.openapi.PluginDescriptor
import java.io.File
import java.util.*

/**
* Constructs Azure ARM cloud clients.
*/
class AzureCloudClientFactory(cloudRegistrar: CloudRegistrar,
private val myPluginDescriptor: PluginDescriptor,
serverPaths: ServerPaths,
private val mySettings: ServerSettings)
: AbstractCloudClientFactory<AzureCloudImageDetails, AzureCloudClient>(cloudRegistrar) {

private val myAzureStorage: File

init {
myAzureStorage = File(serverPaths.pluginDataDirectory, "cloud-$cloudCode/indices")
if (!myAzureStorage.exists()) {
myAzureStorage.mkdirs()
}
}

override fun createNewClient(state: CloudState,
images: Collection<AzureCloudImageDetails>,
params: CloudClientParameters): AzureCloudClient {
Expand All @@ -69,7 +57,7 @@ class AzureCloudClientFactory(cloudRegistrar: CloudRegistrar,
apiConnector.setProfileId(state.profileId)
apiConnector.setLocation(location)

val azureCloudClient = AzureCloudClient(params, apiConnector, myAzureStorage)
val azureCloudClient = AzureCloudClient(params, apiConnector)
azureCloudClient.updateErrors(*errors)

return azureCloudClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import jetbrains.buildServer.clouds.CloudInstanceUserData
import jetbrains.buildServer.clouds.InstanceStatus
import jetbrains.buildServer.clouds.QuotaException
import jetbrains.buildServer.clouds.azure.AzureUtils
import jetbrains.buildServer.clouds.azure.IdProvider
import jetbrains.buildServer.clouds.azure.arm.connector.AzureApiConnector
import jetbrains.buildServer.clouds.azure.arm.connector.AzureInstance
import jetbrains.buildServer.clouds.base.AbstractCloudImage
Expand All @@ -35,8 +34,7 @@ import kotlinx.coroutines.experimental.async
* Azure cloud image.
*/
class AzureCloudImage constructor(private val myImageDetails: AzureCloudImageDetails,
private val myApiConnector: AzureApiConnector,
private val myIdProvider: IdProvider)
private val myApiConnector: AzureApiConnector)
: AbstractCloudImage<AzureCloudInstance, AzureCloudImageDetails>(myImageDetails.sourceName, myImageDetails.sourceName) {
init {
try {
Expand Down Expand Up @@ -81,7 +79,7 @@ class AzureCloudImage constructor(private val myImageDetails: AzureCloudImageDet
* @return created instance.
*/
private fun startInstance(userData: CloudInstanceUserData): AzureCloudInstance {
val name = myImageDetails.sourceName.toLowerCase() + myIdProvider.nextId
val name = getInstanceName()
val instance = AzureCloudInstance(this, name)
instance.status = InstanceStatus.SCHEDULED_TO_START
val data = AzureUtils.setVmNameForTag(userData, name)
Expand Down Expand Up @@ -185,6 +183,16 @@ class AzureCloudImage constructor(private val myImageDetails: AzureCloudImageDet
return myImageDetails.agentPoolId
}

private fun getInstanceName(): String {
val keys = myInstances.keys.map(String::toLowerCase)
val sourceName = myImageDetails.sourceName.toLowerCase()
var i: Int = 1

while (keys.contains(sourceName + i)) i++

return sourceName + i
}

/**
* Returns active instances.
*
Expand Down

0 comments on commit 804bfc6

Please sign in to comment.