public asInitFile()

in lib/compute/agent-nodes.ts [55:191]


  public asInitFile(filePath: string, stackConfig: AgentNodeProps, config: AgentNodeConfig, stackRegion: string, options?: InitFileOptions): InitFile {
    return InitFile.fromString(filePath,
      `
    import hudson.model.*
    import jenkins.model.*
    import hudson.plugins.ec2.*
    import com.amazonaws.services.ec2.model.InstanceType

    def instance = Jenkins.getInstance()
    def init_script = "${this.InitScript}"
    def instance_profile_arn = "${this.AgentNodeInstanceProfileArn}"
    def sshKeysASMSecretId = "${this.SSHEC2KeySecretId}"

    // Agent node configuration specific to each node
    def ec2_cloud_name = "${config.ec2CloudName}"
    def instance_type = "${config.instanceType}"
    def worker_label_string = "${config.workerLabelString}"
    def number_of_executors = "${config.numberOfExecutors}"
    def remote_user = "${config.remoteUser}"
    def ami_id = "${config.amiId}"

    // Values that are imported from the stack
    def region = "${stackRegion}"
    def security_group = "${stackConfig.agentNodeSecurityGroup.toString()}"
    def subnet_id = "${stackConfig.subnetId.toString()}"
    def ec2_tags = [new EC2Tag('Name', 'jenkins-agent-node')]
    
    
    // SlaveTemplate ref: https://github.com/jenkinsci/ec2-plugin/blob/master/src/main/java/hudson/plugins/ec2/SlaveTemplate.java
    def agent_node_template = new SlaveTemplate(
      // String ami
      ami_id,
      // String zone
      '',
      // SpotConfiguration spotConfig
      null,
      // String securityGroups
      security_group,
      // String remoteFS
      '/var/jenkins',
      // InstanceType type
      InstanceType.fromValue(instance_type),
      // boolean ebsOptimized
      false,
      // String labelString
      worker_label_string,
      // Node.Mode mode
      Node.Mode.NORMAL,
      // String description
      'Jenkins Agent Node',
      // String initScript
      init_script,
      // String tmpDir
      '',
      // String userData
      '',
      // String numExecutors
      number_of_executors,
      // String remoteAdmin
      remote_user,
      // AMITypeData amiType
      new UnixData(null, null, null, null),
      // String jvmopts
      '',
      // boolean stopOnTerminate
      false,
      // String subnetId
      subnet_id,
      // List<EC2Tag> tags
      ec2_tags,
      // String idleTerminationMinutes
      '30',
      //int minimumNumberOfInstances,
      1,
      //int minimumNumberOfSpareInstances,
      1,
      // String instanceCapStr
      '20',
      // String iamInstanceProfile
      instance_profile_arn,
      //  boolean deleteRootOnTermination
      true,
      // boolean useEphemeralDevices
      false,
      // String launchTimeoutStr
      '1800',
      // boolean associatePublicIp
      false,
      // String customDeviceMapping
      '/dev/xvda=:100:true:::encrypted',
      // boolean connectBySSHProcess
      false,
      // boolean monitoring,
      true,
      // boolean t2Unlimited,
      false,
      // ConnectionStrategy connectionStrategy,
      null,
      // int maxTotalUses,
      -1,
      // List<? extends NodeProperty<?>> nodeProperties,
      null,
      // HostKeyVerificationStrategyEnum hostKeyVerificationStrategy,
      HostKeyVerificationStrategyEnum.OFF,
      //Tenancy tenancy,
      Tenancy.Default,
      //EbsEncryptRootVolume ebsEncryptRootVolume,
      EbsEncryptRootVolume.ENCRYPTED,
    )
    
    // AmazonEC2Cloud ref: https://github.com/jenkinsci/ec2-plugin/blob/master/src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java
    def new_cloud = new AmazonEC2Cloud(
      // String cloudName
      ec2_cloud_name,
      // boolean useInstanceProfileForCredentials
      true,
      // String credentialsId
      '',
      // String region
      region,
      // String privateKey
      null,
      // String sshKeysCredentialsId
      sshKeysASMSecretId,
      // String instanceCapStr
      "10",
      // List<? extends SlaveTemplate> templates
      [agent_node_template],
      // String roleArn: We have instance profile with a role attached to the instance
      '',
      // String roleSessionName
      ''
    )

    instance.clouds.add(new_cloud)
    instance.save()`);
  }