
// Configure EC2 cloud auto-scailing workers
def create_ec2_slave = { ami_id, availability_zone, security_groups, remote_fs, instance_type, labels, description, num_executors, remote_user, subnet_id, tag_name, tag_env, tag_hostname, idle_termination, instance_cap, launch_timeout, ec2_workers ->
    def worker_ami = new hudson.plugins.ec2.SlaveTemplate(
      ami_id,                                            // AMI ID
      availability_zone,                                 // Availability Zone
      null,                                              // Spot Configuration
      security_groups,                                   // Security group names
      remote_fs,                                         // Remote FS root
      com.amazonaws.services.ec2.model.InstanceType.fromValue(instance_type),             // Instance Type
      false,                                             // EBS Optimized
      labels,                                            // Labels
      hudson.model.Node.Mode.NORMAL,                     // Node.Mode mode
      description,                                       // Description
      '',                                                // Init script
      '',                                                // temporary dir location
      '',                                                // User Data
      num_executors,                                     // Number of Executors
      remote_user,                                       // Remote user
      new hudson.plugins.ec2.UnixData(null, null),                          // AMI Type
      '',                                                // JVM Options
      false,                                             // Stop/Disconnect on Idle Timeout
      subnet_id,                                         // Subnet ID for VPC
      [ new hudson.plugins.ec2.EC2Tag('Name', tag_name), // Tags
        new hudson.plugins.ec2.EC2Tag('env',  tag_env),
        new hudson.plugins.ec2.EC2Tag('hostname', tag_hostname) ],
      idle_termination,                                  // Idle termination time
      false,                                             // Use private NDS
      instance_cap,                                      // Instance Cap
      '',                                                // IAM Instance Profile
      false,                                             // Use ephemeral devices
      false,                                             // Use dedicated tenancy
      launch_timeout,                                    // Launch Timeout in seconds
      false,                                             // Associate Public IP
      '',                                                // Block device mapping
      false,                                             // Connect by SSH Process
      false                                              // Connect using Public IP
    )
    ec2_workers.add(worker_ami)
    println 'WORKER ' + ami_id + ' added...'
}

