| 1 |
# -------------------------------------------------------------------------------------------- |
| 2 |
# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 |
# -------------------------------------------------------------------------------------------- |
| 5 |
|
| 6 |
import logging |
| 7 |
|
| 8 |
import vsts.release.v4_1.models as models |
| 9 |
from vsts.exceptions import VstsServiceError |
| 10 |
from ..base.base_manager import BaseManager |
| 11 |
from ..pool.pool_manager import PoolManager |
| 12 |
from ..constants import (LINUX_CONSUMPTION, LINUX_DEDICATED, WINDOWS) |
| 13 |
from ..exceptions import ReleaseErrorException |
| 14 |
|
| 15 |
|
| 16 |
class ReleaseManager(BaseManager): |
| 17 |
""" Manage DevOps releases |
| 18 |
|
| 19 |
Attributes: |
| 20 |
See BaseManager |
| 21 |
""" |
| 22 |
def __init__(self, organization_name="", project_name="", creds=None): |
| 23 |
super(ReleaseManager, self).__init__(creds, organization_name=organization_name, project_name=project_name) |
| 24 |
|
| 25 |
def create_release_definition(self, build_name, artifact_name, pool_name, service_endpoint_name, |
| 26 |
release_definition_name, app_type, functionapp_name, storage_name, |
| 27 |
resource_name, settings=None): |
| 28 |
pool = self._get_pool_by_name(pool_name) |
| 29 |
project = self._get_project_by_name(self._project_name) |
| 30 |
build = self._get_build_by_name(project, build_name) |
| 31 |
retention_policy_environment = self._get_retention_policy() |
| 32 |
artifact = self._get_artifact(build, project, artifact_name) |
| 33 |
pre_release_approvals, post_release_approvals = self._get_pre_post_approvals() |
| 34 |
service_endpoint = self._get_service_endpoint_by_name(project, service_endpoint_name) |
| 35 |
release_deploy_step = models.release_definition_deploy_step.ReleaseDefinitionDeployStep(id=2) |
| 36 |
triggers = self._get_triggers(artifact_name) |
| 37 |
deployment_input = self._get_deployment_input(pool.id) |
| 38 |
|
| 39 |
phase_inputs = self._get_phase_inputs(artifact_name) |
| 40 |
|
| 41 |
workflowtasks = [] |
| 42 |
|
| 43 |
if app_type == LINUX_CONSUMPTION: |
| 44 |
workflowtasks.append(self._blob_task(service_endpoint.id, storage_name)) |
| 45 |
workflowtasks.append(self._sas_token_task(service_endpoint.id, storage_name)) |
| 46 |
workflowtasks.append(self._app_settings_task(service_endpoint.id, functionapp_name, resource_name)) |
| 47 |
elif app_type == LINUX_DEDICATED: |
| 48 |
workflowtasks.append(self._app_service_deploy_task_linux(service_endpoint.id, functionapp_name)) |
| 49 |
elif app_type == WINDOWS: |
| 50 |
workflowtasks.append(self._app_service_deploy_task_windows(service_endpoint.id, functionapp_name)) |
| 51 |
else: |
| 52 |
logging.error("Invalid app type provided. Correct types are: " |
| 53 |
"Linux Consumption: %s, Linux Dedicated: %s, Windows: %s", |
| 54 |
LINUX_CONSUMPTION, LINUX_DEDICATED, WINDOWS) |
| 55 |
|
| 56 |
if settings is not None: |
| 57 |
settings_str = "" |
| 58 |
for setting in settings: |
| 59 |
settings_str += (setting[0] + "='" + setting[1] + "'") |
| 60 |
# Check that settings were actually set otherwise we don't want to use the task |
| 61 |
if settings_str != "": |
| 62 |
workflowtasks.append(self._app_settings_task_customized( |
| 63 |
service_endpoint.id, functionapp_name, resource_name, settings_str |
| 64 |
)) |
| 65 |
|
| 66 |
deploy_phases = self._get_deploy_phases(deployment_input, workflowtasks, phase_inputs) |
| 67 |
|
| 68 |
condition = models.condition.Condition(condition_type=1, name="ReleaseStarted", value="") |
| 69 |
|
| 70 |
release_definition_environment = models.release_definition_environment.ReleaseDefinitionEnvironment( |
| 71 |
name="deploy build", |
| 72 |
rank=1, |
| 73 |
retention_policy=retention_policy_environment, |
| 74 |
pre_deploy_approvals=pre_release_approvals, |
| 75 |
post_deploy_approvals=post_release_approvals, |
| 76 |
deploy_phases=deploy_phases, |
| 77 |
deploy_step=release_deploy_step, |
| 78 |
conditions=[condition] |
| 79 |
) |
| 80 |
|
| 81 |
release_definition = models.release_definition.ReleaseDefinition( |
| 82 |
name=release_definition_name, |
| 83 |
environments=[release_definition_environment], |
| 84 |
artifacts=[artifact], |
| 85 |
triggers=triggers |
| 86 |
) |
| 87 |
|
| 88 |
return self._release_client.create_release_definition(release_definition, project.id) |
| 89 |
|
| 90 |
def list_release_definitions(self): |
| 91 |
project = self.get_project_by_name(self._project_name) |
| 92 |
return self._release_client.get_release_definitions(project.id) |
| 93 |
|
| 94 |
def create_release(self, release_definition_name): |
| 95 |
project = self.get_project_by_name(self._project_name) |
| 96 |
release_definition = self.get_release_definition_by_name(project, release_definition_name) |
| 97 |
|
| 98 |
release_start_metadata = models.release_start_metadata.ReleaseStartMetadata( |
| 99 |
definition_id=release_definition.id, |
| 100 |
is_draft=False, |
| 101 |
properties={"ReleaseCreationSource": "ReleaseHub"} |
| 102 |
) |
| 103 |
|
| 104 |
try: |
| 105 |
new_release = self._release_client.create_release(release_start_metadata, project.id) |
| 106 |
except VstsServiceError as vse: |
| 107 |
raise ReleaseErrorException(vse.message) |
| 108 |
return new_release |
| 109 |
|
| 110 |
def list_releases(self): |
| 111 |
project = self.get_project_by_name(self._project_name) |
| 112 |
return self._release_client.get_releases(project.id) |
| 113 |
|
| 114 |
def get_latest_release(self, release_definition_name): |
| 115 |
project = self.get_project_by_name(self._project_name) |
| 116 |
build_definition = self.get_release_definition_by_name(project, release_definition_name) |
| 117 |
|
| 118 |
try: |
| 119 |
releases = self._release_client.get_releases(self._project_name, definition_id=build_definition.id) |
| 120 |
except VstsServiceError: |
| 121 |
return None |
| 122 |
|
| 123 |
releases.sort(key=lambda r: r.id, reverse=True) |
| 124 |
if releases: |
| 125 |
return releases[0] |
| 126 |
return None |
| 127 |
|
| 128 |
def _get_service_endpoint_by_name(self, project, service_endpoint_name): |
| 129 |
service_endpoints = self._service_endpoint_client.get_service_endpoints(project.id) |
| 130 |
return next((service_endpoint for service_endpoint in service_endpoints |
| 131 |
if service_endpoint.name == service_endpoint_name), None) |
| 132 |
|
| 133 |
def _get_pool_by_name(self, pool_name): |
| 134 |
"""Helper function to get the pool object from its name""" |
| 135 |
pool_manager = PoolManager(organization_name=self._organization_name, |
| 136 |
project_name=self._project_name, creds=self._creds) |
| 137 |
pools = pool_manager.list_pools() |
| 138 |
return next((pool for pool in pools.value if pool.name == pool_name), None) |
| 139 |
|
| 140 |
def get_project_by_name(self, name): |
| 141 |
for p in self._core_client.get_projects(): |
| 142 |
if p.name == name: |
| 143 |
return p |
| 144 |
return None |
| 145 |
|
| 146 |
def get_release_definition_by_name(self, project, name): |
| 147 |
for p in self._release_client.get_release_definitions(project.id): |
| 148 |
if p.name == name: |
| 149 |
return p |
| 150 |
return None |
| 151 |
|
| 152 |
def _get_triggers(self, artifact_name): |
| 153 |
trigger = {} |
| 154 |
trigger["triggerType"] = "artifactSource" |
| 155 |
trigger["triggerConditions"] = [] |
| 156 |
trigger["artifactAlias"] = artifact_name |
| 157 |
triggers = [trigger] |
| 158 |
return triggers |
| 159 |
|
| 160 |
def _get_deployment_input(self, pool_id): |
| 161 |
deployment_input = {} |
| 162 |
deployment_input["parallelExecution"] = {"parallelExecutionType": 0} |
| 163 |
deployment_input["queueId"] = pool_id |
| 164 |
return deployment_input |
| 165 |
|
| 166 |
def _get_phase_inputs(self, artifact_name): |
| 167 |
phase_inputs = {} |
| 168 |
download_input = {} |
| 169 |
download_input["artifactItems"] = [] |
| 170 |
download_input["alias"] = artifact_name |
| 171 |
download_input["artifactType"] = "Build" |
| 172 |
download_input["artifactDownloadMode"] = "All" |
| 173 |
artifacts_download_input = {} |
| 174 |
artifacts_download_input["downloadInputs"] = [download_input] |
| 175 |
phase_input_artifact_download_input = {} |
| 176 |
phase_input_artifact_download_input["skipArtifactsDownload"] = False |
| 177 |
phase_input_artifact_download_input["artifactsDownloadInput"] = artifacts_download_input |
| 178 |
phase_inputs["phaseinput_artifactdownloadinput"] = phase_input_artifact_download_input |
| 179 |
return phase_inputs |
| 180 |
|
| 181 |
def _get_deploy_phases(self, deployment_input, workflowtasks, phase_inputs): |
| 182 |
deploy_phase = {} |
| 183 |
deploy_phase["deploymentInput"] = deployment_input |
| 184 |
deploy_phase["rank"] = 1 |
| 185 |
deploy_phase["phaseType"] = 1 |
| 186 |
deploy_phase["name"] = "Agent Job" |
| 187 |
deploy_phase["workflowTasks"] = workflowtasks |
| 188 |
deploy_phase["phaseInputs"] = phase_inputs |
| 189 |
|
| 190 |
deploy_phases = [deploy_phase] |
| 191 |
|
| 192 |
return deploy_phases |
| 193 |
|
| 194 |
def _get_retention_policy(self): |
| 195 |
return models.environment_retention_policy.EnvironmentRetentionPolicy( |
| 196 |
days_to_keep=300, releases_to_keep=3, retain_build=True |
| 197 |
) |
| 198 |
|
| 199 |
def _get_artifact(self, build, project, artifact_name): |
| 200 |
artifacts = self._build_client.get_artifacts(build.id, project.id) |
| 201 |
artifact = None |
| 202 |
for a in artifacts: |
| 203 |
if a.name == artifact_name: |
| 204 |
artifact = a |
| 205 |
definition_reference = {} |
| 206 |
definition_reference["project"] = {"id": project.id, "name": project.name} |
| 207 |
definition_reference["definition"] = {"id": build.definition.id, "name": build.definition.name} |
| 208 |
definition_reference["defaultVersionType"] = {"id": "latestType", "name": "Latest"} |
| 209 |
|
| 210 |
return models.artifact.Artifact( |
| 211 |
source_id=artifact.id, |
| 212 |
alias=artifact.name, |
| 213 |
type="Build", |
| 214 |
definition_reference=definition_reference) |
| 215 |
|
| 216 |
def _get_pre_post_approvals(self): |
| 217 |
pre_approval = models.release_definition_approval_step.ReleaseDefinitionApprovalStep( |
| 218 |
id=0, |
| 219 |
rank=1, |
| 220 |
is_automated=True, |
| 221 |
is_notification_on=False |
| 222 |
) |
| 223 |
|
| 224 |
post_approval = models.release_definition_approval_step.ReleaseDefinitionApprovalStep( |
| 225 |
id=0, |
| 226 |
rank=1, |
| 227 |
is_automated=True, |
| 228 |
is_notification_on=False |
| 229 |
) |
| 230 |
|
| 231 |
pre_release_approvals = models.release_definition_approvals.ReleaseDefinitionApprovals( |
| 232 |
approvals=[pre_approval] |
| 233 |
) |
| 234 |
|
| 235 |
post_release_approvals = models.release_definition_approvals.ReleaseDefinitionApprovals( |
| 236 |
approvals=[post_approval] |
| 237 |
) |
| 238 |
|
| 239 |
return pre_release_approvals, post_release_approvals |
| 240 |
|
| 241 |
def _blob_task(self, connectedServiceNameARM, storage_name): |
| 242 |
blobtask = {} |
| 243 |
blobtask["name"] = "AzureBlob File Copy" |
| 244 |
blobtask["enabled"] = True |
| 245 |
blobtask_inputs = {} |
| 246 |
blobtask_inputs["SourcePath"] = "$(System.DefaultWorkingDirectory)/drop/drop/build$(Build.BuildId).zip" |
| 247 |
blobtask_inputs["ConnectedServiceNameSelector"] = 'ConnectedServiceNameARM' |
| 248 |
blobtask_inputs["ConnectedServiceNameARM"] = connectedServiceNameARM |
| 249 |
blobtask_inputs["Destination"] = "AzureBlob" |
| 250 |
blobtask_inputs["StorageAccountRM"] = storage_name |
| 251 |
blobtask_inputs["ContainerName"] = 'azure-build' |
| 252 |
blobtask_inputs["outputStorageUri"] = "outputstorageuri" |
| 253 |
blobtask_inputs["outputStorageContainerSasToken"] = "sastoken" |
| 254 |
blobtask["inputs"] = blobtask_inputs |
| 255 |
blobtask["version"] = "2.*" |
| 256 |
blobtask["definitionType"] = "task" |
| 257 |
blobtask["taskId"] = "eb72cb01-a7e5-427b-a8a1-1b31ccac8a43" |
| 258 |
return blobtask |
| 259 |
|
| 260 |
def _sas_token_task(self, connectedServiceNameARM, storage_name): |
| 261 |
sastokentask = {} |
| 262 |
sastokentask["name"] = "Create SAS Token for Storage Account " + storage_name |
| 263 |
sastokentask["enabled"] = True |
| 264 |
sastokentask["taskId"] = "9e0b2bda-6a8d-4215-8e8c-3d47614db813" |
| 265 |
sastokentask["version"] = "1.*" |
| 266 |
sastokentask["definitionType"] = "task" |
| 267 |
sastokentask_inputs = {} |
| 268 |
sastokentask_inputs["ConnectedServiceName"] = connectedServiceNameARM |
| 269 |
sastokentask_inputs["StorageAccountRM"] = storage_name |
| 270 |
sastokentask_inputs["SasTokenTimeOutInHours"] = 10000 |
| 271 |
sastokentask_inputs["Permission"] = "r" |
| 272 |
sastokentask_inputs["StorageContainerName"] = "azure-build" |
| 273 |
sastokentask_inputs["outputStorageUri"] = "storageUri" |
| 274 |
sastokentask_inputs["outputStorageContainerSasToken"] = "storageToken" |
| 275 |
sastokentask["inputs"] = sastokentask_inputs |
| 276 |
return sastokentask |
| 277 |
|
| 278 |
def _app_settings_task(self, connectedServiceNameARM, functionapp_name, resource_name): |
| 279 |
appsetttingstask = {} |
| 280 |
appsetttingstask["name"] = "Set App Settings: " |
| 281 |
appsetttingstask["enabled"] = True |
| 282 |
appsetttingstask["taskId"] = "9d2e4cf0-f3bb-11e6-978b-770d284f4f2d" |
| 283 |
appsetttingstask["version"] = "2.*" |
| 284 |
appsetttingstask["definitionType"] = "task" |
| 285 |
appsetttingstask_inputs = {} |
| 286 |
appsetttingstask_inputs["ConnectedServiceName"] = connectedServiceNameARM |
| 287 |
appsetttingstask_inputs["WebAppName"] = functionapp_name |
| 288 |
appsetttingstask_inputs["ResourceGroupName"] = resource_name |
| 289 |
appsetttingstask_inputs["AppSettings"] = ( |
| 290 |
"WEBSITE_RUN_FROM_PACKAGE='$(storageUri)/build$(Build.BuildId).zip$(storageToken)'" |
| 291 |
) |
| 292 |
appsetttingstask["inputs"] = appsetttingstask_inputs |
| 293 |
return appsetttingstask |
| 294 |
|
| 295 |
def _app_settings_task_customized(self, connectedServiceNameARM, functionapp_name, resource_name, settings): |
| 296 |
appsetttingstask = {} |
| 297 |
appsetttingstask["name"] = "Set App Settings: " |
| 298 |
appsetttingstask["enabled"] = True |
| 299 |
appsetttingstask["taskId"] = "9d2e4cf0-f3bb-11e6-978b-770d284f4f2d" |
| 300 |
appsetttingstask["version"] = "2.*" |
| 301 |
appsetttingstask["definitionType"] = "task" |
| 302 |
appsetttingstask_inputs = {} |
| 303 |
appsetttingstask_inputs["ConnectedServiceName"] = connectedServiceNameARM |
| 304 |
appsetttingstask_inputs["WebAppName"] = functionapp_name |
| 305 |
appsetttingstask_inputs["ResourceGroupName"] = resource_name |
| 306 |
appsetttingstask_inputs["AppSettings"] = settings |
| 307 |
appsetttingstask["inputs"] = appsetttingstask_inputs |
| 308 |
return appsetttingstask |
| 309 |
|
| 310 |
def _app_service_deploy_task_linux(self, connectedServiceNameARM, functionapp_name): |
| 311 |
appservicetask = {} |
| 312 |
appservicetask["name"] = "Azure App Service Deploy: " + functionapp_name |
| 313 |
appservicetask["enabled"] = True |
| 314 |
appservicetask["taskId"] = "497d490f-eea7-4f2b-ab94-48d9c1acdcb1" |
| 315 |
appservicetask["version"] = "4.*" |
| 316 |
appservicetask["definitionType"] = "task" |
| 317 |
appservicetask_inputs = {} |
| 318 |
appservicetask_inputs["ConnectionType"] = "AzureRM" |
| 319 |
appservicetask_inputs["ConnectedServiceName"] = connectedServiceNameARM |
| 320 |
appservicetask_inputs["PublishProfilePath"] = "$(System.DefaultWorkingDirectory)/**/*.pubxml" |
| 321 |
appservicetask_inputs["WebAppKind"] = "functionAppLinux" |
| 322 |
appservicetask_inputs["WebAppName"] = functionapp_name |
| 323 |
appservicetask_inputs["SlotName"] = "production" |
| 324 |
appservicetask_inputs["Package"] = "$(System.DefaultWorkingDirectory)/**/*.zip" |
| 325 |
appservicetask["inputs"] = appservicetask_inputs |
| 326 |
return appservicetask |
| 327 |
|
| 328 |
def _app_service_deploy_task_windows(self, connectedServiceNameARM, functionapp_name): |
| 329 |
appservicetask = {} |
| 330 |
appservicetask["name"] = "Azure App Service Deploy: " + functionapp_name |
| 331 |
appservicetask["enabled"] = True |
| 332 |
appservicetask["taskId"] = "497d490f-eea7-4f2b-ab94-48d9c1acdcb1" |
| 333 |
appservicetask["version"] = "4.*" |
| 334 |
appservicetask["definitionType"] = "task" |
| 335 |
appservicetask_inputs = {} |
| 336 |
appservicetask_inputs["ConnectionType"] = "AzureRM" |
| 337 |
appservicetask_inputs["ConnectedServiceName"] = connectedServiceNameARM |
| 338 |
appservicetask_inputs["PublishProfilePath"] = "$(System.DefaultWorkingDirectory)/**/*.pubxml" |
| 339 |
appservicetask_inputs["WebAppKind"] = "functionAppWindows" |
| 340 |
appservicetask_inputs["WebAppName"] = functionapp_name |
| 341 |
appservicetask_inputs["SlotName"] = "production" |
| 342 |
appservicetask_inputs["Package"] = "$(System.DefaultWorkingDirectory)/**/*.zip" |
| 343 |
appservicetask["inputs"] = appservicetask_inputs |
| 344 |
return appservicetask |