-
Notifications
You must be signed in to change notification settings - Fork 9
/
GoogleCloudImage.kt
189 lines (158 loc) · 6.92 KB
/
GoogleCloudImage.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package jetbrains.buildServer.clouds.google
import com.google.api.gax.rpc.InvalidArgumentException
import com.google.cloud.compute.v1.Operation
import com.intellij.openapi.diagnostic.Logger
import jetbrains.buildServer.clouds.CloudInstanceUserData
import jetbrains.buildServer.clouds.InstanceStatus
import jetbrains.buildServer.clouds.QuotaException
import jetbrains.buildServer.clouds.base.AbstractCloudImage
import jetbrains.buildServer.clouds.base.connector.AbstractInstance
import jetbrains.buildServer.clouds.base.errors.TypedCloudErrorInfo
import jetbrains.buildServer.clouds.google.connector.GoogleApiConnector
import jetbrains.buildServer.clouds.google.types.GoogleHandler
import jetbrains.buildServer.clouds.google.types.GoogleImageFamilyHandler
import jetbrains.buildServer.clouds.google.types.GoogleImageHandler
import jetbrains.buildServer.clouds.google.types.GoogleTemplateHandler
import jetbrains.buildServer.clouds.google.utils.IdProvider
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
/**
* Google cloud image.
*/
class GoogleCloudImage constructor(private val myImageDetails: GoogleCloudImageDetails,
private val myApiConnector: GoogleApiConnector,
private val myIdProvider: IdProvider)
: AbstractCloudImage<GoogleCloudInstance, GoogleCloudImageDetails>(myImageDetails.sourceId, myImageDetails.sourceId),
DisposableHandle, CoroutineScope {
private val job = Job()
override fun dispose() {
job.cancel()
}
override val coroutineContext: CoroutineContext
get() = Dispatchers.Default + job
private val myImageHandlers = mapOf(
GoogleCloudImageType.Image to GoogleImageHandler(myApiConnector),
GoogleCloudImageType.ImageFamily to GoogleImageFamilyHandler(myApiConnector),
GoogleCloudImageType.Template to GoogleTemplateHandler(myApiConnector)
)
override fun getImageDetails(): GoogleCloudImageDetails {
return myImageDetails
}
override fun createInstanceFromReal(realInstance: AbstractInstance): GoogleCloudInstance {
val zone = realInstance.properties[GoogleConstants.ZONE]!!
return GoogleCloudInstance(this, realInstance.name, zone).apply {
properties = realInstance.properties
}
}
override fun canStartNewInstance(): Boolean {
return activeInstances.size < myImageDetails.maxInstances
}
override fun startNewInstance(userData: CloudInstanceUserData): GoogleCloudInstance = runBlocking {
if (!canStartNewInstance()) {
throw QuotaException("Unable to start more instances. Limit has reached")
}
createInstance(userData)
}
/**
* Creates a new virtual machine.
*
* @param userData info about server.
* @return created instance.
*/
private fun createInstance(userData: CloudInstanceUserData): GoogleCloudInstance {
val name = getInstanceName()
val instance = GoogleCloudInstance(this, name, imageDetails.zone)
instance.status = InstanceStatus.SCHEDULED_TO_START
val data = GoogleUtils.setVmNameForTag(userData, name)
launch {
try {
LOG.info("Creating new virtual machine ${instance.name}")
val operation = handler.createInstance(instance, data)
processOperationResult(instance, operation)
} catch (e: Throwable) {
LOG.warnAndDebugDetails(e.message, e)
instance.status = InstanceStatus.ERROR
val errInfo = TypedCloudErrorInfo.fromException(e)
instance.updateErrors(errInfo)
if (e is InvalidArgumentException) {
updateErrors(errInfo)
}
LOG.info("Removing allocated resources for virtual machine ${instance.name}")
try {
myApiConnector.deleteVm(instance)
LOG.info("Allocated resources for virtual machine ${instance.name} have been removed")
removeInstance(instance.instanceId)
} catch (e: Throwable) {
val message = "Failed to delete allocated resources for virtual machine ${instance.name}: ${e.message}"
LOG.warnAndDebugDetails(message, e)
}
}
}
addInstance(instance)
return instance
}
private fun processOperationResult(instance: GoogleCloudInstance, operation: Operation) {
instance.startOperationId = operation.id
instance.status = InstanceStatus.RUNNING
}
override fun restartInstance(instance: GoogleCloudInstance) {
instance.status = InstanceStatus.RESTARTING
launch {
try {
LOG.info("Restarting virtual machine ${instance.name}")
myApiConnector.restartVm(instance)
} catch (e: Throwable) {
LOG.warnAndDebugDetails(e.message, e)
instance.status = InstanceStatus.ERROR
instance.updateErrors(TypedCloudErrorInfo.fromException(e))
}
}
}
override fun terminateInstance(instance: GoogleCloudInstance) {
instance.status = InstanceStatus.SCHEDULED_TO_STOP
launch {
try {
if (myImageDetails.behaviour.isDeleteAfterStop) {
LOG.info("Removing virtual machine ${instance.name} due to cloud image settings")
myApiConnector.deleteVm(instance)
} else {
LOG.info("Stopping virtual machine ${instance.name}")
myApiConnector.stopVm(instance)
}
instance.status = InstanceStatus.STOPPED
LOG.info("Virtual machine ${instance.name} has been successfully terminated")
} catch (e: Throwable) {
LOG.warnAndDebugDetails(e.message, e)
instance.status = InstanceStatus.ERROR
instance.updateErrors(TypedCloudErrorInfo.fromException(e))
}
}
}
override fun getAgentPoolId(): Int? {
return myImageDetails.agentPoolId
}
val handler: GoogleHandler
get() = myImageHandlers[imageDetails.type]!!
private fun getInstanceName(): String {
val sourceName = myImageDetails.sourceId.toLowerCase()
val id = if (imageDetails.growingId) {
myIdProvider.nextId
} else {
val keys = instances.map { it.instanceId.toLowerCase() }
var i = 1
while (keys.contains(sourceName + i)) i++
i
}
return sourceName + id
}
/**
* Returns active instances.
*
* @return instances.
*/
private val activeInstances: List<GoogleCloudInstance>
get() = instances.filter { instance -> instance.status.isStartingOrStarted }
companion object {
private val LOG = Logger.getInstance(GoogleCloudImage::class.java.name)
}
}