---

# You do NOT need to restart resalloc after change
# daemon will fetch it automatically after an update.
#
# Note: higher priority pools are used first
# default priority is 0
# reserved instances in cloud has > 0
# on-premise instance < 0
# high performance instances <= 40

{% macro aws(arch, max, max_starting, max_prealloc, spot=False, on_demand=none, priority=0, reserved=False) %}
aws_{{ arch }}_{{ on_demand + '_' if on_demand is not none else '' }}{% if spot %}spot{% else %}normal{% endif %}{% if reserved %}reserved{% endif %}_{% if devel %}dev{% else %}prod{% endif %}:
{% if on_demand %}
    max: 10
    max_starting: 4
{% elif reserved and devel %}
    max: 0
{% else %}
    max: {{ max }}
    max_starting: {{ max_starting }}
    max_prealloc: {{ max_prealloc }}
{% endif %}
    tags:
    - name: copr_builder
      priority: {{ priority }}
    - aws
    - arch_noarch
{% if arch == 'x86_64' %}
    - arch_x86_64
    - arch_x86_64_native
    - arch_i386
    - arch_i386_native
    - arch_i586
    - arch_i586_native
    - arch_i686
    - arch_i686_native
    - arch_armhfp
    - arch_armhfp_emulated
{% else %}
    - arch_aarch64
    - arch_aarch64_native
{% endif %}
{% if on_demand %}

    tags_on_demand:
    - on_demand_{{ on_demand }}
{% endif %}

{% if arch == 'x86_64' and on_demand is none %}
{% set itype='c7i.xlarge' %}
{% elif arch == 'x86_64' and on_demand == 'powerful' %}
{% set itype='c7i.8xlarge' %}
{% elif arch == 'aarch64' and on_demand is none %}
{% set itype='c7g.xlarge' %}
{% elif arch == 'aarch64' and on_demand == 'powerful' %}
{% set itype='c7g.8xlarge' %}
{% endif %}

{% if spot and on_demand == 'powerful' %}
    cmd_new: copr-resalloc-aws-new-{{ arch }} --instance-type {{ itype }} --spot-price 3.4 --root-volume-size 18
{% elif spot %}
    cmd_new: copr-resalloc-aws-new-{{ arch }} --instance-type {{ itype }} --spot-price 0.156
{% else %}
    cmd_new: copr-resalloc-aws-new-{{ arch }} --instance-type {{ itype }}
{% endif %}
    cmd_delete: "/var/lib/resallocserver/resalloc_provision/vm-delete"
    cmd_livecheck: "resalloc-check-vm-ip"
    cmd_release: "/var/lib/resallocserver/resalloc_provision/vm-release"
    cmd_list: resalloc-aws-list
    livecheck_period: 180
    reuse_opportunity_time: 90
    reuse_max_count: 8
    reuse_max_time: 1800
{% endmacro %}

# x86_64 hypervisors
{% for hv in ["01", "02", "03", "04"] %}
{% if "x86_hypervisor_" + hv in builders %}
copr_hv_x86_64_{{ hv }}_{% if devel %}dev{% else %}prod{% endif %}:
{% if hv == "02" %}
    max: 0
{% else %}
    max: {{ builders["x86_hypervisor_" + hv]["x86_64"][0] }}
{% endif %}
    max_starting: {{ builders["x86_hypervisor_" + hv]["x86_64"][1] }}
    max_prealloc: {{ builders["x86_hypervisor_" + hv]["x86_64"][2] }}
    tags:
    - copr_builder
    - arch_noarch
    - arch_x86_64
    - arch_x86_64_native
    - arch_i386
    - arch_i386_native
    - arch_i586
    - arch_i586_native
    - arch_i686
    - arch_i686_native
    - arch_armhfp
    - arch_armhfp_emulated
    - hypervisor
    - hypervisor_x86_64
    - hypervisor_x86_64_{{ hv }}
    cmd_new: "/var/lib/resallocserver/provision/libvirt-new --ram-size 16384 --swap-vol-size 168"
    cmd_delete: "/var/lib/resallocserver/resalloc_provision/vm-delete"
    cmd_livecheck: "resalloc-check-vm-ip"
    cmd_release: "/var/lib/resallocserver/resalloc_provision/vm-release"
    cmd_list: "/var/lib/resallocserver/provision/libvirt-list"
    livecheck_period: 180
    reuse_opportunity_time: 90
    reuse_max_count: 8
    reuse_max_time: 1800
{% else %}
#
# x86_hypervisor_{{ hv }} not configured in group_vars
#
{% endif %}
{% endfor %}

# Power8 hypervisors
{% for hv in ["01", "02", "03"] %}
{% if "ppc64le_hypervisor_" + hv in builders %}

copr_hv_ppc64le_{{ hv }}_{% if devel %}dev{% else %}prod{% endif %}:
    max: {{ builders["ppc64le_hypervisor_" + hv]["ppc64le"][0] }}
    max_starting: {{ builders["ppc64le_hypervisor_" + hv]["ppc64le"][1] }}
    max_prealloc: {{ builders["ppc64le_hypervisor_" + hv]["ppc64le"][2] }}
    tags:
    - copr_builder
    - name: arch_noarch
      priority: -8
    - arch_ppc64le
    - arch_ppc64le_native
    - hypervisor
    - hypervisor_ppc64le
    - hypervisor_ppc64le_{{ hv }}
    - arch_power8
    cmd_new: "/var/lib/resallocserver/provision/libvirt-new --swap-vol-size 168"
    cmd_delete: "/var/lib/resallocserver/resalloc_provision/vm-delete"
    cmd_livecheck: "resalloc-check-vm-ip"
    cmd_release: "/var/lib/resallocserver/resalloc_provision/vm-release"
    cmd_list: "/var/lib/resallocserver/provision/libvirt-list"
    livecheck_period: 180
    reuse_opportunity_time: 90
    reuse_max_count: 8
    reuse_max_time: 1800

{% endif %}
{% endfor %}


# Power9 hypervisors
{% for hv in ["01", "02", "03"] %}
{% if "p09_hypervisor_" + hv in builders %}

copr_p09_{{ hv }}_{% if devel %}dev{% else %}prod{% endif %}:
    max: {{ builders["p09_hypervisor_" + hv]["ppc64le"][0] }}
    max_starting: {{ builders["p09_hypervisor_" + hv]["ppc64le"][1] }}
    max_prealloc: {{ builders["p09_hypervisor_" + hv]["ppc64le"][2] }}
    tags:
    - copr_builder
    - name: arch_noarch
      priority: -8
    - arch_ppc64le
    - arch_ppc64le_native
    - hypervisor
    - hypervisor_p09
    - hypervisor_p09_{{ hv }}
    - arch_power9
    # The Power9 machine has 160 threads.  The bottleneck is certainly small
    # disk, so try to waste the CPUs appropriately.
    cmd_new: "/var/lib/resallocserver/provision/libvirt-new --cpu-count 5 --ram-size 16384 --swap-vol-size 168"
    cmd_delete: "/var/lib/resallocserver/resalloc_provision/vm-delete"
    cmd_livecheck: "resalloc-check-vm-ip"
    cmd_release: "/var/lib/resallocserver/resalloc_provision/vm-release"
    cmd_list: "/var/lib/resallocserver/provision/libvirt-list"
    livecheck_period: 180
    reuse_opportunity_time: 90
    reuse_max_count: 8
    reuse_max_time: 1800

{% endif %}
{% endfor %}

{% if False %}
# Z Cloud instances Tokyo
copr_ibm_cloud_s390x_tokyo_{% if devel %}dev{% else %}prod{% endif %}:
  max: {{ builders.ibm_cloud_tokyo.s390x[0] }}
{% if not devel %}
  max_prealloc: {{ builders.ibm_cloud_tokyo.s390x[1] }}
{% endif %}
  max_starting: {{ builders.ibm_cloud_tokyo.s390x[2] }}
  tags:
    - copr_builder
    - name: arch_noarch
      priority: -30
    - ibm_cloud
    - tokyo
{% if devel %}
  tags_on_demand:
{% endif %}
    - arch_s390x
    - arch_s390x_native
  cmd_new: '/var/lib/resallocserver/resalloc_provision/ibm-cloud-vm create "$RESALLOC_NAME"'
  cmd_delete: "/var/lib/resallocserver/resalloc_provision/vm-delete"
  cmd_livecheck: "resalloc-check-vm-ip"
  cmd_release: "/var/lib/resallocserver/resalloc_provision/vm-release"
  cmd_list: "/var/lib/resallocserver/resalloc_provision/ibm-cloud-list-vms"
  livecheck_period: 180
  reuse_opportunity_time: 90
  reuse_max_count: 8
  reuse_max_time: 1800
{% endif %}


# Z Cloud instances in Washington (hp == high performance)
{% for t in ['', 'hp'] %}
copr_ic_s390x_{{ t + '_' if t or '' }}us_east_{% if devel %}dev{% else %}prod{% endif %}:
{% if not t %}
  max: {{ builders.ibm_cloud_us_east.s390x[0] }}
{% if not devel %}
  max_prealloc: {{ builders.ibm_cloud_us_east.s390x[1] }}
{% endif %}
  max_starting: {{ builders.ibm_cloud_us_east.s390x[2] }}
{% else %}
  max: 2
  max_starting: 2
{% endif %}
  tags:
    - copr_builder
    - name: arch_noarch
      priority: -30
    - ibm_cloud
    - us_east
{% if devel %}
  tags_on_demand:
{% endif %}
    - name: arch_s390x_native
    - name: arch_s390x
{% if t == 'hp' %}
{% if devel %}
      priority: -30
{% else %}
  tags_on_demand:
{% endif %}
    - on_demand_powerful
{% endif %}
  cmd_new: '/var/lib/resallocserver/resalloc_provision/ibm-cloud-vm create "$RESALLOC_NAME"'
  cmd_delete: "/var/lib/resallocserver/resalloc_provision/vm-delete"
  cmd_livecheck: "resalloc-check-vm-ip"
  cmd_release: "/var/lib/resallocserver/resalloc_provision/vm-release"
  cmd_list: "/var/lib/resallocserver/resalloc_provision/ibm-cloud-list-vms"
  livecheck_period: 180
  reuse_opportunity_time: 45
  reuse_max_count: 8
  reuse_max_time: 1800

{% endfor %}

# Power9 VMs in openpower-controller.osuosl.org
copr_osuosl_p09_{% if devel %}dev{% else %}prod{% endif %}:
{% if devel %}
  max: 3
  max_prealloc: 1
  max_starting: 1
{% else %}
  max: 15
  max_prealloc: 6
  max_starting: 4
{% endif %}
  tags:
    - copr_builder
    - name: arch_noarch
      priority: -25
    - name: arch_ppc64le
      priority: -10
    - arch_ppc64le_native
    - in_osuosl
    - name: arch_power9
      priority: -10
  cmd_new: '/var/lib/resallocserver/resalloc_provision/osuosl-vm'
  cmd_delete: "/var/lib/resallocserver/resalloc_provision/vm-delete"
  cmd_livecheck: "resalloc-check-vm-ip"
  cmd_release: "/var/lib/resallocserver/resalloc_provision/vm-release"
  cmd_list: '/var/lib/resallocserver/resalloc_provision/osuosl-list'
  livecheck_period: 180
  reuse_opportunity_time: 90
  reuse_max_count: 8
  reuse_max_time: 1800

#### AWS x86_64
# on-premise HV has priority 0 and above
# AWS should have priority < 0 unless it is reserved instance for which we pay anyway

# reserved instances
{% if not devel %}
{{ aws('x86_64', 1, 1, 1, priority=20, reserved=True) }}
{% endif %}

{{ aws('x86_64', builders.aws_spot.x86_64[0], builders.aws_spot.x86_64[1],
       builders.aws_spot.x86_64[2], True, priority=-2) }}

{% if not devel %}
{{ aws('x86_64', builders.aws.x86_64[0], builders.aws.x86_64[1],
       builders.aws.x86_64[2], priority=-10 ) }}
{% endif %}

### AARCH 64 builders

{% if not devel %}
# reserved instance
{{ aws('aarch64', 3, 2, 3, priority=20, reserved=True) }}
{% endif %}

{{ aws('aarch64', builders.aws_spot.aarch64[0], builders.aws_spot.aarch64[1],
       builders.aws_spot.aarch64[2], spot=True, priority=-1) }}

{% if not devel %}
{{ aws('aarch64', builders.aws.aarch64[0], builders.aws.aarch64[1],
       builders.aws.aarch64[2], priority=-5) }}
{% endif %}


#### High performance builders
# priority should be less than any other normal builder. i.e., <= -40

{{ aws('x86_64', builders.aws_spot.x86_64[0], builders.aws_spot.x86_64[1],
       builders.aws_spot.x86_64[2], spot=True, on_demand='powerful', priority=-40) }}

{{ aws('aarch64', builders.aws_spot.aarch64[0], builders.aws_spot.aarch64[1],
       builders.aws_spot.aarch64[2], spot=True, on_demand='powerful', priority=-40) }}

{{ aws('x86_64', builders.aws_spot.x86_64[0], builders.aws_spot.x86_64[1],
       builders.aws_spot.x86_64[2], on_demand='powerful', priority=-60) }}

{{ aws('aarch64', builders.aws_spot.aarch64[0], builders.aws_spot.aarch64[1],
       builders.aws_spot.aarch64[2], on_demand='powerful', priority=-60) }}


#### Evacuate machines in older pools

{% for poolname in [] %}
{{ poolname }}_{% if devel %}dev{% else %}prod{% endif %}:
    max: 0
    cmd_new: /bin/true
    cmd_delete: "/var/lib/resallocserver/resalloc_provision/vm-delete"
    cmd_list: resalloc-aws-list
{% endfor %}
