Esempio n. 1
0
def fill_tfvars(
        image_path,
        storage_path,
        master_count,
        nodes_details,
        tf_folder,
        machine_net
):
    tfvars_json_file = os.path.join(tf_folder, consts.TFVARS_JSON_NAME)
    with open(tfvars_json_file) as _file:
        tfvars = json.load(_file)

    master_starting_ip = str(
        ipaddress.ip_address(
            ipaddress.IPv4Network(machine_net.cidr_v4).network_address
        )
        + 10
    )
    worker_starting_ip = str(
        ipaddress.ip_address(
            ipaddress.IPv4Network(machine_net.cidr_v4).network_address
        )
        + 10
        + int(tfvars["master_count"])
    )
    master_count = min(master_count, consts.NUMBER_OF_MASTERS)
    worker_count = nodes_details['worker_count']
    tfvars['image_path'] = image_path
    tfvars['master_count'] = master_count
    if machine_net.has_ip_v4:
        tfvars['libvirt_master_ips'] = utils.create_ip_address_nested_list(
            master_count, starting_ip_addr=master_starting_ip
        )
        tfvars['libvirt_worker_ips'] = utils.create_ip_address_nested_list(
            worker_count, starting_ip_addr=worker_starting_ip
        )
    else:
        tfvars['libvirt_master_ips'] = utils.create_empty_nested_list(master_count)
        tfvars['libvirt_worker_ips'] = utils.create_empty_nested_list(worker_count)

    tfvars['machine_cidr_addresses'] = machine_net.machine_cidr_addresses
    tfvars['provisioning_cidr_addresses'] = machine_net.provisioning_cidr_addresses
    tfvars['api_vip'] = _get_vips_ips(machine_net)[0]
    tfvars['libvirt_storage_pool_path'] = storage_path
    tfvars['libvirt_master_macs'] = static_network.generate_macs(master_count)
    tfvars['libvirt_worker_macs'] = static_network.generate_macs(worker_count)
    tfvars.update(nodes_details)

    tfvars.update(_secondary_tfvars(master_count, nodes_details, machine_net))

    with open(tfvars_json_file, "w") as _file:
        json.dump(tfvars, _file)
Esempio n. 2
0
def set_workers_addresses_by_type(tfvars, num_worker_nodes, master_ip_type,
                                  worker_ip_type, worker_mac_type):
    old_worker_ips_list = tfvars[worker_ip_type]
    last_master_addresses = tfvars[master_ip_type][-1]

    if last_master_addresses:
        if old_worker_ips_list:
            worker_starting_ip = ipaddress.ip_address(
                old_worker_ips_list[-1][0])
        else:
            worker_starting_ip = ipaddress.ip_address(last_master_addresses[0])

        worker_ips_list = old_worker_ips_list + utils.create_ip_address_nested_list(
            num_worker_nodes, worker_starting_ip + 1)
    else:
        log.info(
            "IPv6-only environment. IP addresses are left empty and will be allocated by libvirt "
            "DHCP because of a bug in Terraform plugin")
        worker_ips_list = old_worker_ips_list + utils.create_empty_nested_list(
            num_worker_nodes)

    tfvars[worker_ip_type] = worker_ips_list

    old_worker_mac_addresses = tfvars[worker_mac_type]
    tfvars[
        worker_mac_type] = old_worker_mac_addresses + static_network.generate_macs(
            num_worker_nodes)
Esempio n. 3
0
 def _create_address_list(self, num, starting_ip_addr):
     # IPv6 addresses can't be set alongside mac addresses using TF libvirt provider
     # Otherwise results: "Invalid to specify MAC address '<mac>' in network '<network>' IPv6 static host definition"
     # see https://github.com/dmacvicar/terraform-provider-libvirt/issues/396
     if self.is_ipv6:
         return utils.create_empty_nested_list(num)
     return utils.create_ip_address_nested_list(num, starting_ip_addr=starting_ip_addr)
Esempio n. 4
0
def _secondary_tfvars(master_count, nodes_details, machine_net):
    vars_dict = {'libvirt_secondary_master_macs': static_network.generate_macs(master_count)}
    if machine_net.has_ip_v4:
        secondary_master_starting_ip = str(
            ipaddress.ip_address(
                ipaddress.IPv4Network(machine_net.provisioning_cidr_v4).network_address
            )
            + 10
        )
        secondary_worker_starting_ip = str(
            ipaddress.ip_address(
                ipaddress.IPv4Network(machine_net.provisioning_cidr_v4).network_address
            )
            + 10
            + int(master_count)
        )
    else:
        secondary_master_starting_ip = str(
            ipaddress.ip_address(
                ipaddress.IPv6Network(machine_net.provisioning_cidr_v6).network_address
            )
            + 16
        )
        secondary_worker_starting_ip = str(
            ipaddress.ip_address(
                ipaddress.IPv6Network(machine_net.provisioning_cidr_v6).network_address
            )
            + 16
            + int(master_count)
        )

    worker_count = nodes_details['worker_count']
    vars_dict['libvirt_secondary_worker_macs'] = static_network.generate_macs(worker_count)
    if machine_net.has_ip_v4:
        vars_dict['libvirt_secondary_master_ips'] = utils.create_ip_address_nested_list(
            master_count,
            starting_ip_addr=secondary_master_starting_ip
        )
        vars_dict['libvirt_secondary_worker_ips'] = utils.create_ip_address_nested_list(
            worker_count,
            starting_ip_addr=secondary_worker_starting_ip
        )
    else:
        vars_dict['libvirt_secondary_master_ips'] = utils.create_empty_nested_list(master_count)
        vars_dict['libvirt_secondary_worker_ips'] = utils.create_empty_nested_list(worker_count)
    return vars_dict
Esempio n. 5
0
def _secondary_tfvars(master_count, nodes_details, machine_net):
    if machine_net.has_ip_v4:
        secondary_master_starting_ip = str(
            ipaddress.ip_address(
                ipaddress.IPv4Network(
                    machine_net.provisioning_cidr_v4).network_address) + 10)
        secondary_worker_starting_ip = str(
            ipaddress.ip_address(
                ipaddress.IPv4Network(
                    machine_net.provisioning_cidr_v4).network_address) + 10 +
            int(master_count))
    else:
        secondary_master_starting_ip = str(
            ipaddress.ip_address(
                ipaddress.IPv6Network(
                    machine_net.provisioning_cidr_v6).network_address) + 16)
        secondary_worker_starting_ip = str(
            ipaddress.ip_address(
                ipaddress.IPv6Network(
                    machine_net.provisioning_cidr_v6).network_address) + 16 +
            int(master_count))

    worker_count = nodes_details['worker_count']
    if machine_net.has_ip_v4:
        return {
            'libvirt_secondary_worker_ips':
            utils.create_ip_address_nested_list(
                worker_count, starting_ip_addr=secondary_worker_starting_ip),
            'libvirt_secondary_master_ips':
            utils.create_ip_address_nested_list(
                master_count, starting_ip_addr=secondary_master_starting_ip)
        }
    else:
        return {
            'libvirt_secondary_worker_ips':
            utils.create_empty_nested_list(worker_count),
            'libvirt_secondary_master_ips':
            utils.create_empty_nested_list(master_count)
        }
Esempio n. 6
0
 def _create_address_list(self, num, starting_ip_addr):
     return utils.create_empty_nested_list(
         num) if self.ipv6 else utils.create_ip_address_nested_list(
             num, starting_ip_addr=starting_ip_addr)
Esempio n. 7
0
 def _create_address_list(self, num, starting_ip_addr):
     if self.config.is_ipv6:
         return utils.create_empty_nested_list(num)
     return utils.create_ip_address_nested_list(
         num, starting_ip_addr=starting_ip_addr)