Module: PuppetDebugServer

Defined in:
lib/puppet_debugserver.rb,
lib/puppet-debugserver/hooks.rb,
lib/puppet-debugserver/message_handler.rb,
lib/puppet-debugserver/puppet_debug_session.rb,
lib/puppet-debugserver/debug_session/break_points.rb,
lib/puppet-debugserver/debug_session/flow_control.rb,
lib/puppet-debugserver/debug_session/hook_handlers.rb,
lib/puppet-debugserver/debug_session/puppet_session_state.rb,
lib/puppet-debugserver/debug_session/puppet_session_run_mode.rb

Defined Under Namespace

Modules: DebugSession Classes: CommandLineParser, Hooks, LogMessageAggregator, MessageHandler, PuppetDebugSession, SourcePosition

Class Method Summary collapse

Class Method Details

.execute(rpc_thread) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/puppet_debugserver.rb', line 149

def self.execute(rpc_thread)
  debug_session = PuppetDebugServer::PuppetDebugSession.instance
  debug_session.initialize_session

  # TODO: Can I use a real mutex here? might be hard with the rpc_thread.alive? call
  sleep(0.5) while !debug_session.flow_control.flag?(:start_puppet) && rpc_thread.alive? && !debug_session.flow_control.terminate?
  return unless rpc_thread.alive? || debug_session.flow_control.terminate?

  debug_session.run_puppet

  return unless rpc_thread.alive?

  debug_session.close
  rpc_thread.join
end

.init_puppet(options) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/puppet_debugserver.rb', line 106

def self.init_puppet(options)
  PuppetEditorServices.init_logging(options)
  log_message(:info, "Debug Server is v#{PuppetDebugServer.version}")
  log_message(:debug, 'Loading gems...')
  require_gems(options)
  require 'puppet'
  log_message(:info, "Using Puppet v#{::Puppet.version}")

  raise("Detected Puppet #{Puppet.version} however the Debug Server requires Puppet 5.0 and above") if Gem::Version.new(Puppet.version) < Gem::Version.new('5.0.0')

  true
end

.log_message(severity, message) ⇒ Object



102
103
104
# File 'lib/puppet_debugserver.rb', line 102

def self.log_message(severity, message)
  PuppetEditorServices.log_message(severity, message)
end

.require_gems(options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/puppet_debugserver.rb', line 14

def self.require_gems(options)
  original_verbose = $VERBOSE
  $VERBOSE = nil

  # Use specific Puppet Gem version if possible
  # Note that puppet is required implicitly in the monkey patches
  # so we don't need to explicity require it here
  unless options[:puppet_version].nil?
    available_puppet_gems = Gem::Specification
                            .select { |item| item.name.casecmp('puppet').zero? }
                            .map { |item| item.version.to_s }
    if available_puppet_gems.include?(options[:puppet_version])
      gem 'puppet', options[:puppet_version]
    else
      log_message(:warn, "Unable to use puppet version #{options[:puppet_version]}, as only the following versions are available [#{available_puppet_gems.join(', ')}]")
    end
  end

  %w[
    message_handler
    hooks
    puppet_debug_session
    debug_session/break_points
    debug_session/hook_handlers
    debug_session/flow_control
    debug_session/puppet_session_run_mode
    debug_session/puppet_session_state
    puppet_monkey_patches
  ].each do |lib|
    require "puppet-debugserver/#{lib}"
  rescue LoadError
    require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-debugserver', lib))
  end
ensure
  $VERBOSE = original_verbose
end

.rpc_server_async(options) ⇒ Object



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
# File 'lib/puppet_debugserver.rb', line 119

def self.rpc_server_async(options)
  log_message(:info, 'Starting RPC Server (Async)...')

  Thread.new do
    Thread.current.abort_on_exception = true

    require 'puppet_editor_services/protocol/debug_adapter'
    require 'puppet_editor_services/server/tcp'

    server_options = options.dup
    protocol_options = { class: PuppetEditorServices::Protocol::DebugAdapter }.merge(options)
    handler_options = { class: PuppetDebugServer::MessageHandler }.merge(options)
    # TODO: Add max threads?
    server_options[:servicename] = 'DEBUG SERVER'

    log_message(:debug, 'Using TCP Server')
    server = ::PuppetEditorServices::Server::Tcp.new(server_options, protocol_options, handler_options)
    trap('INT') do
      server.stop_services(true)
      PuppetDebugServer::PuppetDebugSession.instance.flow_control.assert_flag(:terminate)
    end
    server.start

    log_message(:info, 'Debug Server exited.')
    # Forcibly kill the Debug Session
    log_message(:info, 'Signalling Debug Session to terminate with extreme prejudice')
    PuppetDebugServer::PuppetDebugSession.instance.force_terminate
  end
end

.versionObject



10
11
12
# File 'lib/puppet_debugserver.rb', line 10

def self.version
  PuppetEditorServices.version
end