---
# BCEDIT
- name: OVS | Running bcdedit
  win_shell: bcdedit /set testsigning yes
  when: bcdedit_needed

- name: OVS | Restarting computer
  win_reboot:
  when: bcdedit_needed

- name: OVS | Downloading OVS
  win_get_url:
    url: "{{custom_ovs_link}}"
    dest: "{{ovs_info.tmp_dir}}\\ovs.msi"
    timeout: 60
  retries: 3

- name: OVS | Download certificate beta
  win_get_url:
    url: "{{ovs_certs_link}}"
    dest: "{{ovs_info.tmp_dir}}\\certificate.cer"
    timeout: 60
  retries: 3
  when: ovs_certs_link is defined

- name: OVS | Extract certificate from msi
  win_shell: |
    $ErrorActionPreference = "Stop"
    $driverFile = "{{ovs_info.tmp_dir}}\\ovs.msi"
    $outputFile = "{{ovs_info.tmp_dir}}\\certificate.cer"
    $exportType = [System.Security.Cryptography.X509Certificates.X509ContentType]::Cert
    $cert = (Get-AuthenticodeSignature $driverFile).SignerCertificate
    [System.IO.File]::WriteAllBytes($outputFile, $cert.Export($exportType))
  when: ovs_certs_link is not defined

- name: OVS | Install certificate
  win_shell: |
    $ErrorActionPreference = "Stop"
    $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("{{ovs_info.tmp_dir}}\certificate.cer")
    $rootStore = Get-Item cert:\LocalMachine\TrustedPublisher
    $rootStore.Open("ReadWrite")
    $rootStore.Add($cert)
    $rootStore.Close()
    $rootStore = Get-Item cert:\LocalMachine\Root
    $rootStore.Open("ReadWrite")
    $rootStore.Add($cert)
    $rootStore.Close()

- name: OVS | Installing OVS
  win_package:
    path: "{{ovs_info.tmp_dir}}\\ovs.msi"
    wait: yes
    state: present
    arguments: ADDLOCAL="OpenvSwitchCLI,OpenvSwitchDriver,OVNHost"
