2017-07-26 03:57:20 +00:00
|
|
|
#!/usr/bin/env groovy
|
|
|
|
|
|
|
|
/*
|
2017-08-26 07:22:53 +00:00
|
|
|
* Set up the Azure VM Cloud plugin.
|
|
|
|
*
|
|
|
|
* This file is generated from an ERB template!
|
2017-07-26 03:57:20 +00:00
|
|
|
*/
|
2017-08-22 04:45:18 +00:00
|
|
|
|
2017-07-26 03:57:20 +00:00
|
|
|
import jenkins.model.*
|
|
|
|
import com.microsoft.azure.vmagent.*
|
|
|
|
import com.microsoft.azure.util.*
|
|
|
|
|
|
|
|
import com.cloudbees.plugins.credentials.*
|
|
|
|
import com.cloudbees.plugins.credentials.impl.*
|
|
|
|
import com.cloudbees.plugins.credentials.domains.Domain
|
|
|
|
|
2017-08-26 07:22:53 +00:00
|
|
|
final String maxAgents = System.env.get('MAX_AGENTS') ?: '2'
|
2017-08-19 23:44:51 +00:00
|
|
|
final String cloudName = 'Azure'
|
2017-07-27 01:35:32 +00:00
|
|
|
final String githubUser = System.env.get('GITHUB_USER') ?: 'max-the-code-monkey'
|
2017-08-22 03:59:43 +00:00
|
|
|
final String resourceGroup = "azureagents-for-codevalet"
|
2017-07-27 01:35:32 +00:00
|
|
|
final String credentialsId = 'azure-agents-credential'
|
|
|
|
final String adminCredentialsId = 'azure-agent-admin-credential'
|
|
|
|
final String tenantId = System.env.get('AZURE_TENANT_ID') ?: 'dummy-tenant-id'
|
|
|
|
final String subscriptionId = System.env.get('AZURE_SUBSCRIPTION_ID') ?: 'dummy-subscription-id'
|
|
|
|
final String clientId = System.env.get('AZURE_CLIENT_ID') ?: 'dummy-client-id'
|
|
|
|
final String clientSecret = System.env.get('AZURE_CLIENT_SECRET') ?: 'dummy-secret'
|
|
|
|
CredentialsScope scope = CredentialsScope.valueOf('SYSTEM')
|
|
|
|
AzureCredentials.ServicePrincipal principle = AzureCredentials.getServicePrincipal(credentialsId)
|
|
|
|
final String id = java.util.UUID.randomUUID().toString()
|
|
|
|
final Credentials c = new UsernamePasswordCredentialsImpl(scope,
|
|
|
|
adminCredentialsId,
|
|
|
|
adminCredentialsId,
|
|
|
|
'azureuser',
|
|
|
|
id)
|
2017-07-26 03:57:20 +00:00
|
|
|
|
2017-07-27 01:35:32 +00:00
|
|
|
SystemCredentialsProvider.instance.store.addCredentials(Domain.global(), c)
|
2017-07-26 03:57:20 +00:00
|
|
|
|
2017-07-27 01:35:32 +00:00
|
|
|
println ">> Credential: ${id}"
|
2017-07-26 03:57:20 +00:00
|
|
|
|
|
|
|
/* If the credentials hasn't already been defined, let's create one! */
|
|
|
|
if (principle.isBlank()) {
|
|
|
|
AzureCredentials credential = new AzureCredentials(scope, /* Scope for the credential */
|
|
|
|
credentialsId, /* */
|
|
|
|
'Azure credentials for provisioning agent', /* description */
|
|
|
|
subscriptionId, /* subscriptionId */
|
|
|
|
clientId, /* clientId */
|
2017-11-30 02:48:09 +00:00
|
|
|
clientSecret) /* clientSecret */
|
|
|
|
credential.tenant = tenantId
|
2017-07-26 03:57:20 +00:00
|
|
|
|
|
|
|
SystemCredentialsProvider.instance.store.addCredentials(Domain.global(), credential)
|
|
|
|
}
|
|
|
|
|
2017-08-22 04:45:18 +00:00
|
|
|
|
2017-08-22 03:59:43 +00:00
|
|
|
Jenkins.instance.clouds.clear()
|
2017-08-20 20:30:37 +00:00
|
|
|
def cloud = Jenkins.instance.clouds.find { it.name == cloudName }
|
2017-08-19 23:44:51 +00:00
|
|
|
|
|
|
|
/* Avoid adding the AzureVMCloud over and over and over again */
|
|
|
|
if (cloud == null) {
|
2017-08-20 20:30:37 +00:00
|
|
|
cloud = new AzureVMCloud(cloudName, /* Cloud Name */
|
2017-08-19 23:44:51 +00:00
|
|
|
credentialsId, /* credentials id */
|
|
|
|
maxAgents, /* Max Agents */
|
|
|
|
'1200', /* Deployment Timeout (s) */
|
2017-08-22 03:59:43 +00:00
|
|
|
'existing', /* Resource group reference type */
|
2017-08-23 02:55:21 +00:00
|
|
|
'', /* New resource group name */
|
2017-08-22 03:59:43 +00:00
|
|
|
resourceGroup, /* Existing resource group name */
|
2017-08-19 23:44:51 +00:00
|
|
|
null) /* VM Templates */
|
|
|
|
Jenkins.instance.clouds.add(cloud)
|
|
|
|
}
|
2017-07-27 01:35:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Nuke all our templates */
|
|
|
|
cloud.clearVmTemplates()
|
|
|
|
|
2017-08-26 09:18:02 +00:00
|
|
|
final String agentWorkspace = '/home/azureuser/workspace'
|
2017-10-02 22:16:07 +00:00
|
|
|
def retentionTime = null
|
2017-08-26 07:22:53 +00:00
|
|
|
def imageReference = null
|
|
|
|
def vmTemplate = null
|
|
|
|
def vhd = null
|
|
|
|
|
|
|
|
<% agents.each_pair do |name, d| %>
|
2017-07-27 01:35:32 +00:00
|
|
|
|
2017-08-26 07:22:53 +00:00
|
|
|
vhd = '<%= d['image']['vhd'] %>'
|
2017-10-02 22:16:07 +00:00
|
|
|
retentionTime = new AzureVMCloudRetensionStrategy(<%= d['retention'] %>)
|
2017-08-26 07:22:53 +00:00
|
|
|
imageReference = new AzureVMAgentTemplate.ImageReferenceTypeClass(vhd, vhd, vhd, vhd, vhd)
|
|
|
|
vmTemplate = new AzureVMAgentTemplate('<%= name %>',
|
|
|
|
'<%= d['description'] %>', /* description */
|
|
|
|
'<%= d['labels'].join(' ') %>', /* labels */
|
2017-11-30 02:48:09 +00:00
|
|
|
'West US', /* location */
|
2017-08-26 07:22:53 +00:00
|
|
|
'<%= d['size'] %>', /* VM Size */
|
2017-08-23 02:55:21 +00:00
|
|
|
'existing', /* Storage account Name reference type */
|
2017-07-27 01:35:32 +00:00
|
|
|
'Standard_LRS', /* Storage account type */
|
|
|
|
'', /* new storage account name */
|
2017-11-30 03:37:40 +00:00
|
|
|
'codevaletimages', /* existing storage account name */
|
2017-08-22 03:59:43 +00:00
|
|
|
'unmanaged', /* disk type */
|
2017-08-26 07:22:53 +00:00
|
|
|
'<%= d['executors'] %>', /* number of executors */
|
2017-07-27 01:35:32 +00:00
|
|
|
'NORMAL', /* Usage mode */
|
2017-08-22 04:45:18 +00:00
|
|
|
'', /* built-in image */
|
2017-08-22 03:59:43 +00:00
|
|
|
false, /* install git */
|
2017-07-27 01:35:32 +00:00
|
|
|
false, /* install maven */
|
2017-08-22 03:59:43 +00:00
|
|
|
false, /* install docker */
|
2017-07-27 01:35:32 +00:00
|
|
|
'Linux', /* OS type */
|
2017-08-22 04:45:18 +00:00
|
|
|
'custom', /* image top level type */
|
2017-08-23 02:55:21 +00:00
|
|
|
false, /* image reference? */
|
2017-07-27 01:35:32 +00:00
|
|
|
imageReference, /* image reference class */
|
|
|
|
'SSH', /* agent launch method */
|
2017-08-22 04:45:18 +00:00
|
|
|
false, /* pre install SSH */
|
2017-08-26 07:22:53 +00:00
|
|
|
'<%= d['initscript']%>', /* init script */
|
2017-08-24 04:42:19 +00:00
|
|
|
adminCredentialsId, /* admin credential Id */
|
|
|
|
'', /* virtual network name */
|
|
|
|
'', /* virtual network resource group name */
|
|
|
|
'', /* subnet name */
|
|
|
|
false, /* use private IP */
|
|
|
|
'', /* Network security group name */
|
|
|
|
agentWorkspace, /* agent workspace */
|
|
|
|
'', /* JVM options */
|
2017-10-02 22:16:07 +00:00
|
|
|
retentionTime, /* retention time */
|
2017-08-24 04:42:19 +00:00
|
|
|
false, /* shutdown on idle */
|
|
|
|
false, /* template disabled */
|
|
|
|
'', /* template status details */
|
|
|
|
true, /* execute init script as root */
|
|
|
|
true /* do not use machine if init fails */
|
|
|
|
)
|
2017-08-26 07:22:53 +00:00
|
|
|
vmTemplate.azureCloud = cloud
|
|
|
|
cloud.addVmTemplate(vmTemplate)
|
2017-08-24 04:42:19 +00:00
|
|
|
|
2017-08-26 07:22:53 +00:00
|
|
|
<% end %>
|