Class: CemAcpt::Utils::WinRMRunner::WinNode

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/cem_acpt/utils/winrm_runner.rb

Overview

A class for setting up windows nodes for testing Applies puppet manifest on nodes and installs the cem_windows module

Constant Summary

Constants included from Logging

Logging::LEVEL_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

current_log_config, #current_log_config, current_log_format, #current_log_format, #current_log_level, current_log_level, included, #logger, logger, new_log_config, #new_log_config, new_log_formatter, #new_log_formatter, #new_log_level, new_log_level, #new_logger, new_logger, verbose?, #verbose?

Constructor Details

#initialize(login_info, mod_name) ⇒ WinNode

Returns a new instance of WinNode.



41
42
43
44
# File 'lib/cem_acpt/utils/winrm_runner.rb', line 41

def initialize(, mod_name)
  @login_info = 
  @mod_name = mod_name
end

Instance Attribute Details

#mod_nameObject (readonly)

Returns the value of attribute mod_name.



39
40
41
# File 'lib/cem_acpt/utils/winrm_runner.rb', line 39

def mod_name
  @mod_name
end

Instance Method Details

#runObject



154
155
156
# File 'lib/cem_acpt/utils/winrm_runner.rb', line 154

def run
  run_winrm
end

#run_winrmObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cem_acpt/utils/winrm_runner.rb', line 46

def run_winrm
  logger.info('CemAcpt') { 'Setting up Windows nodes for test...' }

  cem_windows_mod_dir = "C:\\ProgramData\\PuppetLabs\\code\\environments\\production\\modules\\cem_windows"

  @login_info.each do |node_name, node_info|
    username = node_info['username']
    password = node_info['password']
    test_name = node_info['test_name']
    ip = node_info['ip']

    opts = {
      endpoint: "https://#{ip}:5986/wsman",
      user: username,
      password: password,
      transport: :ssl,
      no_ssl_peer_verification: true,
      retry_limit: 5,
      retry_delay: 20,
    }

    conn = WinRM::Connection.new(opts)
    conn.shell(:powershell) do |shell|
      # Instantiate the wrapper class
      winrm_runner = RunnerWrapper.new(shell)
      # The below steps for enabling long paths, installing Puppet and make cem_acpt directory will be a part of making the custom image.
      # Enable long paths
      logger.debug('CemAcpt') { 'Enabling long paths...' }
      winrm_runner.run('Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1')
      # Download Puppet and Install it
      logger.info('CemAcpt') { 'Downloading and installing Puppet...' }
      winrm_runner.run('Invoke-WebRequest -URI http://downloads.puppetlabs.com/windows/puppet7/puppet-agent-7.25.0-x64.msi -OutFile "C:\puppet-agent-7.25.0-x64.msi"')
      winrm_runner.run("Start-process msiexec.exe -Wait -ArgumentList '/I C:\\puppet-agent-7.25.0-x64.msi PUPPET_AGENT_STARTUP_MODE=Disabled /qn /norestart'")
      # Create our cem_acpt parent directory
      logger.debug('CemAcpt') { 'Creating cem_acpt directory and downloading necessary files...' }
      winrm_runner.run("mkdir C:\\cem_acpt")
      # Download the cem_windows module from the bucket to cem_acpt directory
      winrm_runner.run("gcloud storage cp gs://win_cem_acpt/#{@mod_name} C:\\cem_acpt")
      # Install the cem_windows module
      logger.info('CemAcpt') { 'Installing cem_windows module...' }
      winrm_runner.run("Start-Process -FilePath 'C:\\Program Files\\Puppet Labs\\Puppet\\bin\\puppet.bat' -ArgumentList 'module install C:\\cem_acpt\\#{@mod_name}' -Wait -NoNewWindow")
      # Download Goss and set it to alpha mode since it's on alpha for windows
      logger.info('CemAcpt') { 'Downloading Goss...' }
      winrm_runner.run("Invoke-WebRequest -URI https://github.com/goss-org/goss/releases/download/v0.3.23/goss-alpha-windows-amd64.exe -OutFile 'C:\\goss.exe'")
      # Since we already installed the cem_windows module, we can unpack it to get the goss.yaml and the manifest.pp in spec/acceptance dir
      # Due to the pain in the ass of trying to parse a string that is yaml dumped in ruby through powershell, I'm just gonna create the yaml
      # in the cem_windows module itself and then move them to the directory at C:/cem_acpt for ease of access and use. The location for these two
      # files will be at spec/files
      winrm_runner.run("Move-Item -Path #{cem_windows_mod_dir}\\spec\\files\\puppet_idempotent.yaml -Destination C:\\cem_acpt")
      winrm_runner.run("Move-Item -Path #{cem_windows_mod_dir}\\spec\\files\\puppet_noop.yaml -Destination C:\\cem_acpt")
      # Grab the goss.yaml and the manifest.pp from the cem_windows module using the test_name from run_data and move to top level of C:/cem_acpt/
      winrm_runner.run("Move-Item -Path #{cem_windows_mod_dir}\\spec\\acceptance\\#{test_name}\\goss.yaml -Destination C:\\cem_acpt")
      winrm_runner.run("Move-Item -Path #{cem_windows_mod_dir}\\spec\\acceptance\\#{test_name}\\manifest.pp -Destination C:\\cem_acpt")
      # Open up firewall ports
      logger.debug('CemAcpt') { 'Opening up firewall ports...' }
      winrm_runner.run('netsh advfirewall firewall add rule name="Goss in" dir=in action=allow protocol=TCP localport=8080')
      winrm_runner.run('netsh advfirewall firewall add rule name="Idempotent in" dir=in action=allow protocol=TCP localport=8081')
      winrm_runner.run('netsh advfirewall firewall add rule name="Noop in" dir=in action=allow protocol=TCP localport=8082')
      winrm_runner.run('netsh advfirewall firewall add rule name="Goss out" dir=out action=allow protocol=TCP localport=8080')
      winrm_runner.run('netsh advfirewall firewall add rule name="Idempotent out" dir=out action=allow protocol=TCP localport=8081')
      winrm_runner.run('netsh advfirewall firewall add rule name="Noop out" dir=out action=allow protocol=TCP localport=8082')
      logger.info('CemAcpt') { 'Setting up NSSM and starting Goss services...' }
      # Download NSSM into cem_acpt directory
      # NSSM is a tool that allows us to create services on windows without having to create a Windows service project.
      # NSSM works with almost any executable. In our case, it will work with goss.exe
      winrm_runner.run("Invoke-WebRequest -URI http://nssm.cc/ci/nssm-2.24-101-g897c7ad.zip -OutFile 'C:\\cem_acpt\\nssm-2.24.zip'")
      # Extract NSSM into cem_acpt directory
      winrm_runner.run("Expand-Archive -Path C:\\cem_acpt\\nssm-2.24.zip -DestinationPath C:\\cem_acpt")
      winrm_runner.run("Rename-Item -Path C:\\cem_acpt\\nssm-2.24-101-g897c7ad -NewName nssm-2.24")
      # Install NSSM Goss server and start it
      winrm_runner.run('C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe install goss-server C:\\goss.exe')
      winrm_runner.run('C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe set goss-server AppStdout C:\\cem_acpt\\goss-server.log')
      winrm_runner.run('C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe set goss-server AppStderr C:\\cem_acpt\\goss-server-stderr.log')
      winrm_runner.run('C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe set goss-server AppParameters "--use-alpha=1 -g C:\\cem_acpt\\goss.yaml serve -f json --endpoint /acpt --cache 10m"')
      # There is a slight issue with starting through nssm, if the process took a bit too long, it will send an error but the process will still be running.
      # winrm_runner.run(shell, 'C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe start goss-server')
      winrm_runner.run('$GossServer= Get-Service "goss-server"')
      winrm_runner.run('$GossServer.Start()')
      winrm_runner.run('$WaitForGossServer = New-TimeSpan -Seconds 5')
      winrm_runner.run('$GossServer.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running, $WaitForGossServer)')
      # Install NSSM Idempotent server and start it
      winrm_runner.run('C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe install idempotent-server C:\\goss.exe')
      winrm_runner.run('C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe set idempotent-server AppStdout C:\\cem_acpt\\idempotent-server.log')
      winrm_runner.run('C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe set idempotent-server AppStderr C:\\cem_acpt\\idempotent-server-stderr.log')
      winrm_runner.run('C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe set idempotent-server AppParameters "--use-alpha=1 -g C:\\cem_acpt\\puppet_idempotent.yaml serve -f json -l :8081 --endpoint /idempotent --cache 10m"')
      # winrm_runner.run(shell, 'C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe start idempotent-server')
      winrm_runner.run('$IdmServer= Get-Service "idempotent-server"')
      winrm_runner.run('$IdmServer.Start()')
      winrm_runner.run('$WaitForIdmServer = New-TimeSpan -Seconds 5')
      winrm_runner.run('$IdmServer.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running, $WaitForIdmServer)')
      # Install NSSM Noop server and start it
      winrm_runner.run('C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe install noop-server C:\\goss.exe')
      winrm_runner.run('C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe set noop-server AppStdout C:\\cem_acpt\\noop-server.log')
      winrm_runner.run('C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe set noop-server AppStderr C:\\cem_acpt\\noop-server-stderr.log')
      winrm_runner.run('C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe set noop-server AppParameters "--use-alpha=1 -g C:\\cem_acpt\\puppet_noop.yaml serve -f json -l :8082 --endpoint /noop --cache 10m"')
      # winrm_runner.run(shell, 'C:\\cem_acpt\\nssm-2.24\\win64\\nssm.exe start noop-server')
      winrm_runner.run('$NoopServer= Get-Service "noop-server"')
      winrm_runner.run('$NoopServer.Start()')
      winrm_runner.run('$WaitForNoopServer = New-TimeSpan -Seconds 5')
      winrm_runner.run('$NoopServer.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running, $WaitForNoopServer)')
      # Run puppet apply
      logger.info('CemAcpt') { 'Running puppet apply...' }
      winrm_runner.run("Start-Process -FilePath 'C:\\Program Files\\Puppet Labs\\Puppet\\bin\\puppet.bat' -ArgumentList 'apply --verbose C:\\cem_acpt\\manifest.pp' -Wait -NoNewWindow -PassThru")
      logger.info('CemAcpt') { 'Puppet apply finished...' }
    end
  end
end