Class: Chef::Knife::WindowsListenerCreate

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/windows_listener_create.rb

Instance Method Summary collapse

Instance Method Details

#get_cert_passphraseObject



57
58
59
60
61
# File 'lib/chef/knife/windows_listener_create.rb', line 57

def get_cert_passphrase
  print "Enter given certificate's passphrase (empty for no passphrase):"
  passphrase = STDIN.gets
  passphrase.strip
end

#runObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/knife/windows_listener_create.rb', line 63

def run
  STDOUT.sync = STDERR.sync = true

  if Chef::Platform.windows?
    begin
      if config[:cert_install]
        config[:cert_passphrase] = get_cert_passphrase unless config[:cert_passphrase]
        result = `powershell.exe -Command " '#{config[:cert_passphrase]}' | certutil  -importPFX '#{config[:cert_install]}' AT_KEYEXCHANGE"`
        if $?.exitstatus
          ui.info "Certificate installed to Certificate Store"
          result = `powershell.exe -Command " echo (Get-PfxCertificate #{config[:cert_install]}).thumbprint "`
          ui.info "Certificate Thumbprint: #{result}"
          config[:cert_thumbprint] = result.strip
        else
          ui.error "Error installing certificate to Certificate Store"
          ui.error result
          exit 1
        end
      end

      unless config[:cert_thumbprint]
        ui.error "Please specify the --cert-thumbprint"
        exit 1
      end

      result = `winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname="#{config[:hostname]}";CertificateThumbprint="#{config[:cert_thumbprint]}";Port="#{config[:port]}"}`
      Chef::Log.debug result

      if $?.exitstatus == 0
        ui.info "WinRM listener created with Port: #{config[:port]} and CertificateThumbprint: #{config[:cert_thumbprint]}"
      else
        ui.error "Error creating WinRM listener. use -VV for more details."
        exit 1
      end

    rescue => e
      puts "ERROR: + #{e}"
    end
  else
    ui.error "WinRM listener can be created on Windows system only"
    exit 1
  end
end