code
stringlengths
26
124k
docstring
stringlengths
23
125k
func_name
stringlengths
1
98
language
stringclasses
1 value
repo
stringlengths
5
53
path
stringlengths
7
151
url
stringlengths
50
211
license
stringclasses
7 values
def get_tld(options) if options[:tld] && !options[:tld].empty? options[:tld] else 'test' end end
TODO(kgrz): the default TLD option is duplicated in both this file and lib/invoker.rb May be assign this to a constant?
get_tld
ruby
codemancers/invoker
lib/invoker/cli.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/cli.rb
MIT
def start_manager verify_process_configuration daemonize_app if Invoker.daemonize? install_interrupt_handler unix_server_thread = Thread.new { Invoker::IPC::Server.new } @thread_group.add(unix_server_thread) process_manager.run_power_server Invoker.config.autorunnable_processes.each do |process_info| process_manager.start_process(process_info) Logger.puts("Starting process - #{process_info.label} waiting for #{process_info.sleep_duration} seconds...") sleep(process_info.sleep_duration) end at_exit { process_manager.kill_workers } start_event_loop end
Start the invoker process supervisor. This method starts a unix server in separate thread that listens for incoming commands.
start_manager
ruby
codemancers/invoker
lib/invoker/commander.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/commander.rb
MIT
def receive_line(line) tail_watchers = Invoker.tail_watchers[@command_label] color_line = "#{@command_label.colorize(color)} : #{line}" plain_line = "#{@command_label} : #{line}" if Invoker.nocolors? Invoker::Logger.puts plain_line else Invoker::Logger.puts color_line end if tail_watchers && !tail_watchers.empty? json_encoded_tail_response = tail_response(color_line) if json_encoded_tail_response tail_watchers.each { |tail_socket| send_data(tail_socket, json_encoded_tail_response) } end end end
Print the lines received over the network
receive_line
ruby
codemancers/invoker
lib/invoker/command_worker.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/command_worker.rb
MIT
def tail_response(line) tail_response = Invoker::IPC::Message::TailResponse.new(tail_line: line) tail_response.encoded_message rescue nil end
Encode current line as json and send the response.
tail_response
ruby
codemancers/invoker
lib/invoker/command_worker.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/command_worker.rb
MIT
def dead? status == 1 end
pidfile exists but process isn't running
dead?
ruby
codemancers/invoker
lib/invoker/daemon.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/daemon.rb
MIT
def start_process_by_name(process_name) if process_running?(process_name) Invoker::Logger.puts "\nProcess '#{process_name}' is already running".colorize(:red) return false end process_info = Invoker.config.process(process_name) start_process(process_info) if process_info end
Start a process given their name @param process_name [String] Command label of process specified in config file.
start_process_by_name
ruby
codemancers/invoker
lib/invoker/process_manager.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/process_manager.rb
MIT
def stop_process(remove_message) worker = workers[remove_message.process_name] command_label = remove_message.process_name return false unless worker signal_to_use = remove_message.signal || 'INT' Invoker::Logger.puts("Removing #{command_label} with signal #{signal_to_use}".colorize(:red)) kill_or_remove_process(worker.pid, signal_to_use, command_label) end
Remove a process from list of processes managed by invoker supervisor.It also kills the process before removing it from the list. @param remove_message [Invoker::IPC::Message::Remove] @return [Boolean] if process existed and was removed else false
stop_process
ruby
codemancers/invoker
lib/invoker/process_manager.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/process_manager.rb
MIT
def restart_process(reload_message) command_label = reload_message.process_name if stop_process(reload_message.remove_message) Invoker.commander.schedule_event(command_label, :worker_removed) do start_process_by_name(command_label) end else start_process_by_name(command_label) end end
Receive a message from user to restart a Process @param [Invoker::IPC::Message::Reload]
restart_process
ruby
codemancers/invoker
lib/invoker/process_manager.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/process_manager.rb
MIT
def send_data(socket, data) socket.write(data) rescue raise Invoker::Errors::ClientDisconnected end
Writes data to client socket and raises error if errors while writing
send_data
ruby
codemancers/invoker
lib/invoker/reactor.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/reactor.rb
MIT
def trigger(command_label, event_name = nil) @trigger_mutex.synchronize do triggered_events << OpenStruct.new( :command_label => command_label, :event_name => event_name) end end
Trigger an event. The event is not triggered immediately, but is just scheduled to be triggered. @param command_label [String] Command for which event should be triggered @param event_name [Symbol, nil] The optional event name
trigger
ruby
codemancers/invoker
lib/invoker/event/manager.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/event/manager.rb
MIT
def schedule_event(command_label, event_name = nil, &block) @trigger_mutex.synchronize do scheduled_events[command_label] << OpenStruct.new(:event_name => event_name, :block => block) end end
Schedule an Event. The event will only trigger when a scheduled event matches a triggered event. @param command_label [String] Command for which the event should be triggered @param event_name [String, nil] Optional event name @param block The block to execute when event actually triggers
schedule_event
ruby
codemancers/invoker
lib/invoker/event/manager.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/event/manager.rb
MIT
def run_scheduled_events filtered_events_by_name_and_command = [] triggered_events.each_with_index do |triggered_event, index| matched_events = scheduled_events[triggered_event.command_label] if matched_events && !matched_events.empty? filtered_events_by_name_and_command, unmatched_events = filter_matched_events(matched_events, triggered_event) triggered_events[index] = nil remove_scheduled_event(unmatched_events, triggered_event.command_label) end end triggered_events.compact! filtered_events_by_name_and_command.each {|event| yield event } end
On next iteration of event loop, this method is called and we try to match scheduled events with events that were triggered.
run_scheduled_events
ruby
codemancers/invoker
lib/invoker/event/manager.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/event/manager.rb
MIT
def run_command(message_object) raise "Not implemented" end
Invoke the command that actual processes incoming message returning true from this message means, command has been processed and client socket can be closed. returning false means, it is a long running command and socket should not be closed immediately @param [Invoker::IPC::Message] incoming message @return [Boolean] true or false
run_command
ruby
codemancers/invoker
lib/invoker/ipc/base_command.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/ipc/base_command.rb
MIT
def initialize(filename, port) @filename = filename || autodetect_config_file print_message_and_abort if invalid_config_file? @port = port - 1 @processes = load_config if Invoker.can_run_balancer? @power_config = Invoker::Power::Config.load_config() end end
initialize takes a port form cli and decrements it by 1 and sets the instance variable @port. This port value is used as the environment variable $PORT mentioned inside invoker.ini. When method pick_port gets fired it increments the value of port by 1, subsequently when pick_port again gets fired, for another command, it will again increment port value by 1, that way generating different ports for different commands.
initialize
ruby
codemancers/invoker
lib/invoker/parsers/config.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/parsers/config.rb
MIT
def initialize(filename=nil) @entries = [] load(filename) if filename end
Initialize a Procfile @param [String] filename (nil) An optional filename to read from
initialize
ruby
codemancers/invoker
lib/invoker/parsers/procfile.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/parsers/procfile.rb
MIT
def entries return @entries unless block_given? @entries.each do |(name, command)| yield name, command end end
Yield each +Procfile+ entry in order
entries
ruby
codemancers/invoker
lib/invoker/parsers/procfile.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/parsers/procfile.rb
MIT
def delete(name) @entries.reject! { |n,c| name == n } end
Remove a +Procfile+ entry @param [String] name The name of the +Procfile+ entry to remove
delete
ruby
codemancers/invoker
lib/invoker/parsers/procfile.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/parsers/procfile.rb
MIT
def load(filename) @entries.replace parse(filename) end
Load a Procfile from a file @param [String] filename The filename of the +Procfile+ to load
load
ruby
codemancers/invoker
lib/invoker/parsers/procfile.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/parsers/procfile.rb
MIT
def save(filename) File.open(filename, 'w') do |file| file.puts self.to_s end end
Save a Procfile to a file @param [String] filename Save the +Procfile+ to this file
save
ruby
codemancers/invoker
lib/invoker/parsers/procfile.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/parsers/procfile.rb
MIT
def on_headers_complete(&block) @on_headers_complete_callback = block end
define a callback for invoking when complete header is parsed
on_headers_complete
ruby
codemancers/invoker
lib/invoker/power/http_parser.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/power/http_parser.rb
MIT
def on_message_complete(&block) @on_message_complete_callback = block end
define a callback to invoke when a full http message is received
on_message_complete
ruby
codemancers/invoker
lib/invoker/power/http_parser.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/power/http_parser.rb
MIT
def complete_headers_received if @on_headers_complete_callback @on_headers_complete_callback.call(@header) end end
gets invoker when complete header is received
complete_headers_received
ruby
codemancers/invoker
lib/invoker/power/http_parser.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/power/http_parser.rb
MIT
def build_power_config config = { http_port: port_finder.http_port, https_port: port_finder.https_port, tld: tld } config end
Builds and returns power config hash. Override this method in subclasses if necessary.
build_power_config
ruby
codemancers/invoker
lib/invoker/power/setup.rb
https://github.com/codemancers/invoker/blob/master/lib/invoker/power/setup.rb
MIT
def websocket_server require 'websocket-eventmachine-server' EM.run do WebSocket::EventMachine::Server.start(host: "0.0.0.0", port: 28080) do |ws| ws.onerror { |e| p e } ws.onmessage { ws.send "pong" } end EM.add_timer(2) { EM.stop } end end
Full integration test. Start a server, and client. Let client interact with server do a ping-pong. Client checks whether ping pong is successful or not. Also, mock rewriter so that it returns valid port for request proxying. - Server will run on port 28080. - Balancer will run on port 28081 proxying to 28080 - Client will connect to 28081 performing ping-pong
websocket_server
ruby
codemancers/invoker
spec/invoker/power/web_sockets_spec.rb
https://github.com/codemancers/invoker/blob/master/spec/invoker/power/web_sockets_spec.rb
MIT
def phrase(*args) if args[0].class.in? [String, Symbol] key, options = args[0].to_s, (args[1] || {}) inline(phrasing_extract_record(key, options), :value, options) else record, attribute, options = args[0], args[1], args[2] inline(record, attribute, options || {}) end end
Normal phrase phrase("headline", url: www.infinum.co/yabadaba, inverse: true, scope: "models.errors") Data model phrase phrase(record, :title, inverse: true, class: phrase-record-title)
phrase
ruby
infinum/phrasing
app/helpers/inline_helper.rb
https://github.com/infinum/phrasing/blob/master/app/helpers/inline_helper.rb
MIT
def can_edit_phrases? raise NotImplementedError.new("You must implement the can_edit_phrases? method") end
You must implement the can_edit_phrases? method. Example: def can_edit_phrases? current_user.is_admin? end
can_edit_phrases?
ruby
infinum/phrasing
lib/generators/phrasing/templates/app/helpers/phrasing_helper.rb
https://github.com/infinum/phrasing/blob/master/lib/generators/phrasing/templates/app/helpers/phrasing_helper.rb
MIT
def probe_payload_builder(node, path, problems, field_name) if node.is_a?(Hash) node.each do |name, val| if name.end_with? '.$' if !is_intrinsic_invocation?(val) && !is_valid_parameters_path?(val) problems << "Field \"#{name}\" of #{field_name} at \"#{path}\" is not a JSONPath or intrinsic function expression" end else probe_payload_builder(val, "#{path}.#{name}", problems, field_name) end end elsif node.is_a?(Array) node.size.times {|i| probe_payload_builder(node[i], "#{path}[#{i}]", problems, field_name) } end end
Search through Parameters for object nodes and check field semantics
probe_payload_builder
ruby
awslabs/statelint
lib/statelint/state_node.rb
https://github.com/awslabs/statelint/blob/master/lib/statelint/state_node.rb
Apache-2.0
def ips(*args) if args[0].is_a?(Hash) time, warmup, quiet = args[0].values_at(:time, :warmup, :quiet) else time, warmup, quiet = args end sync, $stdout.sync = $stdout.sync, true job = Job.new job_opts = {} job_opts[:time] = time unless time.nil? job_opts[:warmup] = warmup unless warmup.nil? job_opts[:quiet] = quiet unless quiet.nil? job.config job_opts yield job job.load_held_results job.run if job.run_single? && job.all_results_have_been_run? job.clear_held_results else job.save_held_results puts '', 'Pausing here -- run Ruby again to measure the next benchmark...' if job.run_single? end $stdout.sync = sync job.run_comparison job.generate_json report = job.full_report if ENV['SHARE'] || ENV['SHARE_URL'] require 'benchmark/ips/share' share = Share.new report, job share.share end report end
Measure code in block, each code's benchmarked result will display in iteration per second with standard deviation in given time. @param time [Integer] Specify how long should benchmark your code in seconds. @param warmup [Integer] Specify how long should Warmup time run in seconds. @return [Report]
ips
ruby
evanphx/benchmark-ips
lib/benchmark/ips.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips.rb
MIT
def ips_quick(*methods, on: Kernel, **opts) ips(opts) do |x| x.compare! methods.each do |name| x.report(name) do |iter| iter.times { on.__send__ name } end end end end
Quickly compare multiple methods on the same object. @param methods [Symbol...] A list of method names (as symbols) to compare. @param receiver [Object] The object on which to call the methods. Defaults to Kernel. @param opts [Hash] Additional options for customizing the benchmark. @option opts [Integer] :warmup The number of seconds to warm up the benchmark. @option opts [Integer] :time The number of seconds to run the benchmark. @example Compare String#upcase and String#downcase ips_quick(:upcase, :downcase, on: "hello") @example Compare two methods you just defined, with a custom warmup. def add; 1+1; end def sub; 2-1; end ips_quick(:add, :sub, warmup: 10)
ips_quick
ruby
evanphx/benchmark-ips
lib/benchmark/ips.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips.rb
MIT
def config opts @warmup = opts[:warmup] if opts[:warmup] @time = opts[:time] if opts[:time] @iterations = opts[:iterations] if opts[:iterations] @stats = opts[:stats] if opts[:stats] @confidence = opts[:confidence] if opts[:confidence] self.quiet = opts[:quiet] if opts.key?(:quiet) self.suite = opts[:suite] if opts[:suite] end
Job configuration options, set +@warmup+ and +@time+. @option opts [Integer] :warmup Warmup time. @option opts [Integer] :time Calculation time. @option iterations [Integer] :time Warmup and calculation iterations.
config
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job.rb
MIT
def hold!(held_path) @held_path = held_path @run_single = true end
Hold after each iteration. @param held_path [String] File name to store hold file.
hold!
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job.rb
MIT
def save!(held_path) @held_path = held_path @run_single = false end
Save interim results. Similar to hold, but all reports are run The report label must change for each invocation. One way to achieve this is to include the version in the label. @param held_path [String] File name to store hold file.
save!
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job.rb
MIT
def json!(path="data.json") @json_path = path end
Generate json to given path, defaults to "data.json".
json!
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job.rb
MIT
def item(label="", str=nil, &blk) # :yield: if blk and str raise ArgumentError, "specify a block and a str, but not both" end action = str || blk raise ArgumentError, "no block or string" unless action @max_width = label.size if label.size > @max_width @list.push Entry.new(label, action) self end
Registers the given label and block pair in the job list. @param label [String] Label of benchmarked code. @param str [String] Code to be benchmarked. @param blk [Proc] Code to be benchmarked. @raise [ArgumentError] Raises if str and blk are both present. @raise [ArgumentError] Raises if str and blk are both absent.
item
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job.rb
MIT
def cycles_per_100ms time_msec, iters cycles = ((MICROSECONDS_PER_100MS / time_msec) * iters).to_i cycles <= 0 ? 1 : cycles end
Calculate the cycles needed to run for approx 100ms, given the number of iterations to run the given time. @param [Float] time_msec Each iteration's time in ms. @param [Integer] iters Iterations. @return [Integer] Cycles per 100ms.
cycles_per_100ms
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job.rb
MIT
def time_us before, after (after.to_f - before.to_f) * MICROSECONDS_PER_SECOND end
Calculate the time difference of before and after in microseconds. @param [Time] before time. @param [Time] after time. @return [Float] Time difference of before and after.
time_us
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job.rb
MIT
def iterations_per_sec cycles, time_us MICROSECONDS_PER_SECOND * (cycles.to_f / time_us.to_f) end
Calculate the iterations per second given the number of cycles run and the time in microseconds that elapsed. @param [Integer] cycles Cycles. @param [Integer] time_us Time in microsecond. @return [Float] Iteration per second.
iterations_per_sec
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job.rb
MIT
def create_report(label, measured_us, iter, samples, cycles) @full_report.add_entry label, measured_us, iter, samples, cycles end
Create report by add entry to +@full_report+. @param label [String] Report item label. @param measured_us [Integer] Measured time in microsecond. @param iter [Integer] Iterations. @param samples [Array<Float>] Sampled iterations per second. @param cycles [Integer] Number of Cycles. @return [Report::Entry] Entry with data.
create_report
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job.rb
MIT
def initialize(label, us, iters, stats, cycles) @label = label @microseconds = us @iterations = iters @stats = stats @measurement_cycle = cycles @show_total_time = false end
Instantiate the Benchmark::IPS::Report::Entry. @param [#to_s] label Label of entry. @param [Integer] us Measured time in microsecond. @param [Integer] iters Iterations. @param [Object] stats Statistics. @param [Integer] cycles Number of Cycles.
initialize
ruby
evanphx/benchmark-ips
lib/benchmark/ips/report.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/report.rb
MIT
def show_total_time! @show_total_time = true end
Control if the total time the job took is reported. Typically this value is not significant because it's very close to the expected time, so it's suppressed by default.
show_total_time!
ruby
evanphx/benchmark-ips
lib/benchmark/ips/report.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/report.rb
MIT
def seconds @microseconds.to_f / 1_000_000.0 end
Return entry's microseconds in seconds. @return [Float] +@microseconds+ in seconds.
seconds
ruby
evanphx/benchmark-ips
lib/benchmark/ips/report.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/report.rb
MIT
def initialize(label, action) @label = label # We define #call_times on the singleton class of each Entry instance. # That way, there is no polymorphism for `@action.call` inside #call_times. if action.kind_of? String compile_string action @action = self else unless action.respond_to? :call raise ArgumentError, "invalid action, must respond to #call" end @action = action if action.respond_to? :arity and action.arity > 0 compile_block_with_manual_loop else compile_block end end end
Instantiate the Benchmark::IPS::Job::Entry. @param label [#to_s] Label of Benchmarked code. @param action [String, Proc] Code to be benchmarked. @raise [ArgumentError] Raises when action is not String or not responding to +call+.
initialize
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job/entry.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job/entry.rb
MIT
def call_times(times) raise '#call_times should be redefined per Benchmark::IPS::Job::Entry instance' end
Call action by given times. @param times [Integer] Times to call +@action+. @return [Integer] Number of times the +@action+ has been called.
call_times
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job/entry.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job/entry.rb
MIT
def compile_string(str) m = (class << self; self; end) code = <<-CODE def call_times(__total); __i = 0 while __i < __total #{str}; __i += 1 end end CODE m.class_eval code end
Compile code into +call_times+ method. @param str [String] Code to be compiled. @return [Symbol] :call_times.
compile_string
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job/entry.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job/entry.rb
MIT
def initialize(out = nil) @out = [] self << out end
@param out [Array<StreamReport>] list of reports to send output
initialize
ruby
evanphx/benchmark-ips
lib/benchmark/ips/job/multi_report.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/job/multi_report.rb
MIT
def slowdown(baseline) low, slowdown, high = baseline.data.bootstrap_quotient(@data, @iterations, @confidence) error = Timing.mean([slowdown - low, high - slowdown]) [slowdown, error] end
Determines how much slower this stat is than the baseline stat if this average is lower than the faster baseline, higher average is better (e.g. ips) (calculate accordingly) @param baseline [SD|Bootstrap] faster baseline @returns [Array<Float, nil>] the slowdown and the error (not calculated for standard deviation)
slowdown
ruby
evanphx/benchmark-ips
lib/benchmark/ips/stats/bootstrap.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/stats/bootstrap.rb
MIT
def slowdown(baseline) if baseline.central_tendency > central_tendency [baseline.central_tendency.to_f / central_tendency, nil] else [central_tendency.to_f / baseline.central_tendency, nil] end end
Determines how much slower this stat is than the baseline stat if this average is lower than the faster baseline, higher average is better (e.g. ips) (calculate accordingly) @param baseline [SD|Bootstrap] faster baseline @returns [Array<Float, nil>] the slowdown and the error (not calculated for standard deviation)
slowdown
ruby
evanphx/benchmark-ips
lib/benchmark/ips/stats/sd.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/stats/sd.rb
MIT
def error_percentage 100.0 * (error.to_f / central_tendency) end
Return entry's standard deviation of iteration per second in percentage. @return [Float] +@ips_sd+ in percentage.
error_percentage
ruby
evanphx/benchmark-ips
lib/benchmark/ips/stats/stats_metric.rb
https://github.com/evanphx/benchmark-ips/blob/master/lib/benchmark/ips/stats/stats_metric.rb
MIT
def test_quiet_false_change_mind $stdout = @old_stdout # all reports are run after block is defined # so changing the value does not matter. last value wins for all out, err = capture_io do Benchmark.ips(:time => 1, :warmup => 0, :quiet => true) do |x| x.report("sleep 0.25") { sleep(0.25) } x.quiet = false end end assert_match(/Calculating -+/, out) assert_empty err end
all reports are run after block is fully defined so last value wins for all tests
test_quiet_false_change_mind
ruby
evanphx/benchmark-ips
test/test_report.rb
https://github.com/evanphx/benchmark-ips/blob/master/test/test_report.rb
MIT
def test_quiet_true_change_mind $stdout = @old_stdout out, err = capture_io do Benchmark.ips(:time => 1, :warmup => 0, :quiet => false) do |x| x.report("sleep 0.25") { sleep(0.25) } x.quiet = true end end refute_match(/Calculating -+/, out) assert_empty err end
all reports are run after block is fully defined so last value wins for all tests
test_quiet_true_change_mind
ruby
evanphx/benchmark-ips
test/test_report.rb
https://github.com/evanphx/benchmark-ips/blob/master/test/test_report.rb
MIT
def parallel_do(enum, options = {}, &block) parallelizer.parallelize(enum, options, &block).to_a end
TODO in many of these cases, the order of the results only matters because you want to match it up with the input. Make a parallelize method that doesn't care about order and spits back results as quickly as possible.
parallel_do
ruby
chef-boneyard/chef-provisioning
lib/chef/provider/machine_batch.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provider/machine_batch.rb
Apache-2.0
def new_driver @new_driver ||= run_context.chef_provisioning.driver_for(new_resource.driver) end
Get the driver specified in the resource
new_driver
ruby
chef-boneyard/chef-provisioning
lib/chef/provider/machine_image.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provider/machine_image.rb
Apache-2.0
def updated! @updated = true end
This should be replaced with whatever records the update; by default it essentially does nothing here.
updated!
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/action_handler.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/action_handler.rb
Apache-2.0
def perform_action(description) if should_perform_actions result = yield else result = nil end performed_action(description) result end
This should perform the actual action (e.g., converge) if there is an action that needs to be done.
perform_action
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/action_handler.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/action_handler.rb
Apache-2.0
def open_stream(name) if block_given? yield STDOUT else STDOUT end end
Open a stream which can be printed to and closed
open_stream
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/action_handler.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/action_handler.rb
Apache-2.0
def get_data(resource_type, name) begin if resource_type == :machine chef_api.get("nodes/#{name}") else chef_api.get("data/#{resource_type}/#{name}") end rescue Net::HTTPServerException => e if e.response.code == '404' backcompat_type = ChefManagedEntryStore.type_names_for_backcompat[resource_type] if backcompat_type && backcompat_type != resource_type get_data(backcompat_type, name) else nil end else raise end end end
Get the given data @param resource_type [Symbol] The type of thing to retrieve (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, ...) @param name [String] The unique identifier of the thing to retrieve @return [Hash,Array] The data, or `nil` if the data does not exist. Will be JSON- and YAML-compatible (Hash, Array, String, Integer, Boolean, Nil)
get_data
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/chef_managed_entry_store.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/chef_managed_entry_store.rb
Apache-2.0
def save_data(resource_type, name, data, action_handler) _chef_server = self.chef_server Chef::Provisioning.inline_resource(action_handler) do if resource_type == :machine chef_node name do chef_server _chef_server raw_json data end else chef_data_bag resource_type.to_s do chef_server _chef_server end chef_data_bag_item name do chef_server _chef_server data_bag resource_type.to_s raw_data data end end end backcompat_type = ChefManagedEntryStore.type_names_for_backcompat[resource_type] if backcompat_type && backcompat_type != resource_type delete_data(backcompat_type, name, action_handler) end end
Save the given data @param resource_type [Symbol] The type of thing to save (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet ...) @param name [String] The unique identifier of the thing to save @param data [Hash,Array] The data to save. Must be JSON- and YAML-compatible (Hash, Array, String, Integer, Boolean, Nil)
save_data
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/chef_managed_entry_store.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/chef_managed_entry_store.rb
Apache-2.0
def delete_data(resource_type, name, action_handler) _chef_server = self.chef_server Chef::Provisioning.inline_resource(action_handler) do if resource_type == :machine chef_node name do chef_server _chef_server action :delete end else chef_data_bag_item name do chef_server _chef_server data_bag resource_type.to_s action :delete end end end backcompat_type = ChefManagedEntryStore.type_names_for_backcompat[resource_type] if backcompat_type && backcompat_type != resource_type delete_data(backcompat_type, name, action_handler) end end
Delete the given data @param resource_type [Symbol] The type of thing to delete (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, ...) @param name [String] The unique identifier of the thing to delete @return [Boolean] Whether anything was deleted or not.
delete_data
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/chef_managed_entry_store.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/chef_managed_entry_store.rb
Apache-2.0
def initialize(convergence_options, config) @convergence_options = convergence_options || {} @config = config end
convergence_options - a freeform hash of options to the converger. config - a Chef::Config-like object with global config like :log_level
initialize
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/convergence_strategy.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/convergence_strategy.rb
Apache-2.0
def setup_convergence(action_handler, machine) raise "setup_convergence not overridden on #{self.class}" end
Get the machine ready to converge, but do not converge.
setup_convergence
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/convergence_strategy.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/convergence_strategy.rb
Apache-2.0
def initialize(driver_url, config) @driver_url = driver_url @config = config end
Inflate a driver from a driver URL. @param [String] driver_url the URL to inflate the driver config - a configuration hash. See "config" for a list of known keys. == Returns A Driver representing the given driver_url.
initialize
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/driver.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/driver.rb
Apache-2.0
def allocate_machine(action_handler, machine_spec, machine_options) raise "#{self.class} does not implement allocate_machine" end
Allocate a machine from the underlying service. This method does not need to wait for the machine to boot or have an IP, but it must store enough information in machine_spec.reference to find the machine later in ready_machine. If a machine is powered off or otherwise unusable, this method may start it, but does not need to wait until it is started. The idea is to get the gears moving, but the job doesn't need to be done :) @param [Chef::Provisioning::ActionHandler] action_handler The action_handler object that is calling this method @param [Chef::Provisioning::ManagedEntry] machine_spec A machine specification representing this machine. @param [Hash] machine_options A set of options representing the desired options when constructing the machine @return [Chef::Provisioning::ManagedEntry] Modifies the passed-in machine_spec. Anything in here will be saved back after allocate_machine completes.
allocate_machine
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/driver.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/driver.rb
Apache-2.0
def ready_machine(action_handler, machine_spec, machine_options) raise "#{self.class} does not implement ready_machine" end
Ready a machine, to the point where it is running and accessible via a transport. This will NOT allocate a machine, but may kick it if it is down. This method waits for the machine to be usable, returning a Machine object pointing at the machine, allowing useful actions like setup, converge, execute, file and directory. @param [Chef::Provisioning::ActionHandler] action_handler The action_handler object that is calling this method @param [Chef::Provisioning::ManagedEntry] machine_spec A machine specification representing this machine. @param [Hash] machine_options A set of options representing the desired state of the machine @return [Machine] A machine object pointing at the machine, allowing useful actions like setup, converge, execute, file and directory.
ready_machine
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/driver.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/driver.rb
Apache-2.0
def connect_to_machine(machine_spec, machine_options) raise "#{self.class} does not implement connect_to_machine" end
Connect to a machine without allocating or readying it. This method will NOT make any changes to anything, or attempt to wait. @param [Chef::Provisioning::ManagedEntry] machine_spec ManagedEntry representing this machine. @param [Hash] machine_options @return [Machine] A machine object pointing at the machine, allowing useful actions like setup, converge, execute, file and directory.
connect_to_machine
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/driver.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/driver.rb
Apache-2.0
def destroy_machine(action_handler, machine_spec, machine_options) raise "#{self.class} does not implement destroy_machine" end
Delete the given machine -- destroy the machine, returning things to the state before allocate_machine was called. @param [Chef::Provisioning::ActionHandler] action_handler The action_handler object that is calling this method @param [Chef::Provisioning::ManagedEntry] machine_spec A machine specification representing this machine. @param [Hash] machine_options A set of options representing the desired state of the machine
destroy_machine
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/driver.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/driver.rb
Apache-2.0
def stop_machine(action_handler, machine_spec, machine_options) raise "#{self.class} does not implement stop_machine" end
Stop the given machine. @param [Chef::Provisioning::ActionHandler] action_handler The action_handler object that is calling this method @param [Chef::Provisioning::ManagedEntry] machine_spec A machine specification representing this machine. @param [Hash] machine_options A set of options representing the desired state of the machine
stop_machine
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/driver.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/driver.rb
Apache-2.0
def allocate_image(action_handler, image_spec, image_options, machine_spec, machine_options) raise "#{self.class} does not implement create_image" end
Allocate an image. Returns quickly with an ID that tracks the image. @param [Chef::Provisioning::ActionHandler] action_handler The action_handler object that is calling this method @param [Chef::Provisioning::ManagedEntry] image_spec An image specification representing this image. @param [Hash] image_options A set of options representing the desired state of the image @param [Chef::Provisioning::ManagedEntry] machine_spec A machine specification representing this machine. @param [Hash] machine_options A set of options representing the desired state of the machine used to create the image
allocate_image
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/driver.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/driver.rb
Apache-2.0
def ready_image(action_handler, image_spec, image_options) raise "#{self.class} does not implement ready_image" end
Ready an image, waiting till the point where it is ready to be used. @param [Chef::Provisioning::ActionHandler] action_handler The action_handler object that is calling this method @param [Chef::Provisioning::ManagedEntry] image_spec An image specification representing this image. @param [Hash] image_options A set of options representing the desired state of the image
ready_image
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/driver.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/driver.rb
Apache-2.0
def destroy_image(action_handler, image_spec, image_options, machine_options={}) raise "#{self.class} does not implement destroy_image" end
Destroy an image using this service. @param [Chef::Provisioning::ActionHandler] action_handler The action_handler object that is calling this method @param [Chef::Provisioning::ManagedEntry] image_spec An image specification representing this image. @param [Hash] image_options A set of options representing the desired state of the image @param [Hash] machine_options A set of options representing the desired state of the machine used to create the image
destroy_image
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/driver.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/driver.rb
Apache-2.0
def ready_machines(action_handler, specs_and_options, parallelizer) parallelizer.parallelize(specs_and_options) do |machine_spec, machine_options| machine = ready_machine(add_prefix(machine_spec, action_handler), machine_spec, machine_options) yield machine if block_given? machine end.to_a end
Ready machines in batch, in parallel if possible.
ready_machines
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/driver.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/driver.rb
Apache-2.0
def stop_machines(action_handler, specs_and_options, parallelizer) parallelizer.parallelize(specs_and_options) do |machine_spec, machine_options| stop_machine(add_prefix(machine_spec, action_handler), machine_spec, machine_options) yield machine_spec if block_given? end.to_a end
Stop machines in batch, in parallel if possible.
stop_machines
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/driver.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/driver.rb
Apache-2.0
def destroy_machines(action_handler, specs_and_options, parallelizer) parallelizer.parallelize(specs_and_options) do |machine_spec, machine_options| destroy_machine(add_prefix(machine_spec, action_handler), machine_spec, machine_options) yield machine_spec if block_given? end.to_a end
Delete machines in batch, in parallel if possible.
destroy_machines
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/driver.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/driver.rb
Apache-2.0
def setup_convergence(action_handler) raise "setup_convergence not overridden on #{self.class}" end
Sets up everything necessary for convergence to happen on the machine. The node MUST be saved as part of this procedure. Other than that, nothing is guaranteed except that converge() will work when this is done.
setup_convergence
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/machine.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/machine.rb
Apache-2.0
def is_directory?(path) raise "is_directory? not overridden on #{self.class}" end
Return true if directory, false/nil if not
is_directory?
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/machine.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/machine.rb
Apache-2.0
def file_exists?(path) raise "file_exists? not overridden on #{self.class}" end
Return true or false depending on whether file exists
file_exists?
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/machine.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/machine.rb
Apache-2.0
def files_different?(path, local_path, content=nil) raise "file_different? not overridden on #{self.class}" end
Return true or false depending on whether remote file differs from local path or content
files_different?
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/machine.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/machine.rb
Apache-2.0
def set_attributes(action_handler, path, attributes) raise "set_attributes not overridden on #{self.class}" end
Set file attributes { mode, :owner, :group }
set_attributes
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/machine.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/machine.rb
Apache-2.0
def get_attributes(path) raise "get_attributes not overridden on #{self.class}" end
Get file attributes { :mode, :owner, :group }
get_attributes
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/machine.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/machine.rb
Apache-2.0
def make_url_available_to_remote(local_url) raise "make_url_available_to_remote not overridden on #{self.class}" end
Ensure the given URL can be reached by the remote side (possibly by port forwarding) Must return the URL that the remote side can use to reach the local_url
make_url_available_to_remote
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/machine.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/machine.rb
Apache-2.0
def detect_os(action_handler) raise "detect_os not overridden on #{self.class}" end
TODO get rid of the action_handler attribute, that is ridiculous Detect the OS on the machine (assumes the machine is up) Returns a triplet: platform, platform_version, machine_architecture = machine.detect_os(action_handler) This triplet is suitable for passing to the Chef metadata API: https://www.chef.io/chef/metadata?p=PLATFORM&pv=PLATFORM_VERSION&m=MACHINE_ARCHITECTURE
detect_os
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/machine.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/machine.rb
Apache-2.0
def reference attrs['reference'] || attrs['location'] end
Location of this machine. This should be a freeform hash, with enough information for the driver to look it up and create a Machine object to access it. This MUST include a 'driver_url' attribute with the driver's URL in it. chef-provisioning will do its darnedest to not lose this information.
reference
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/machine_spec.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/machine_spec.rb
Apache-2.0
def id managed_entry_store.identifier(resource_type, name) end
Globally unique identifier for this machine. Does not depend on the machine's reference or existence.
id
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry.rb
Apache-2.0
def reference # Backcompat: old data bags didn't have the "reference" field. If we have # no reference field in the data, and the data bag is non-empty, return # the root of the data bag. attrs['reference'] || attrs['location'] || (attrs == {} ? nil : attrs) end
Reference to this managed thing. This should be a freeform hash, with enough information for the driver to look it up and create a Machine object to access it. This MUST include a 'driver_url' attribute with the driver's URL in it. chef-provisioning will do its darnedest to not lose this information.
reference
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry.rb
Apache-2.0
def save(action_handler) managed_entry_store.save_data(resource_type, name, data, action_handler) end
Save this node to the server. If you have significant information that could be lost, you should do this as quickly as possible. Data will be saved automatically for you after allocate_machine and ready_machine.
save
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry.rb
Apache-2.0
def get_data(resource_type, name) raise NotImplementedError, :delete_data end
Subclass interface Get the given data @param resource_type [Symbol] The type of thing to retrieve (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, ...) @param name [String] The unique identifier of the thing to retrieve @return [Hash,Array] The data, or `nil` if the data does not exist. Will be JSON- and YAML-compatible (Hash, Array, String, Integer, Boolean, Nil)
get_data
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry.rb
Apache-2.0
def save_data(resource_type, name, data, action_handler) raise NotImplementedError, :delete_data end
Save the given data @param resource_type [Symbol] The type of thing to save (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet ...) @param name [String] The unique identifier of the thing to save @param data [Hash,Array] The data to save. Must be JSON- and YAML-compatible (Hash, Array, String, Integer, Boolean, Nil)
save_data
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry.rb
Apache-2.0
def delete_data(resource_type, name, action_handler) raise NotImplementedError, :delete_data end
Delete the given data @param resource_type [Symbol] The type of thing to delete (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, ...) @param name [String] The unique identifier of the thing to delete @return [Boolean] Whether anything was deleted or not.
delete_data
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry.rb
Apache-2.0
def get_data(resource_type, name) raise NotImplementedError, :get_data end
Get the given data @param resource_type [Symbol] The type of thing to retrieve (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, ...) @param name [String] The unique identifier of the thing to retrieve @return [Hash,Array] The data. Will be JSON- and YAML-compatible (Hash, Array, String, Integer, Boolean, Nil)
get_data
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry_store.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry_store.rb
Apache-2.0
def save_data(resource_type, name, data, action_handler) raise NotImplementedError, :save_data end
Save the given data @param resource_type [Symbol] The type of thing to save (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet ...) @param name [String] The unique identifier of the thing to save @param data [Hash,Array] The data to save. Must be JSON- and YAML-compatible (Hash, Array, String, Integer, Boolean, Nil)
save_data
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry_store.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry_store.rb
Apache-2.0
def delete_data(resource_type, name, action_handler) raise NotImplementedError, :delete_data end
Delete the given data @param resource_type [Symbol] The type of thing to delete (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, ...) @param name [String] The unique identifier of the thing to delete @return [Boolean] Whether anything was deleted or not.
delete_data
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry_store.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry_store.rb
Apache-2.0
def identifier(resource_type, name) raise NotImplementedError, :identifier end
Get a globally unique identifier for this resource. @param resource_type [Symbol] The type of spec to retrieve (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, ...) @param name [String] The unique identifier of the spec to retrieve @return [String] The identifier. @example ChefManagedEntry does this: chef_managed_entry_store.identifier(:machine, 'mario') # => https://my.chef.server/organizations/org/nodes/mario
identifier
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry_store.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry_store.rb
Apache-2.0
def get(resource_type, name) data = get_data(resource_type, name) if data new_entry(resource_type, name, data) end end
Get a spec. @param resource_type [Symbol] The type of spec to retrieve (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, ...) @param name [String] The unique identifier of the spec to retrieve @return [ManagedEntry] The entry, or `nil` if the data does not exist.
get
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry_store.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry_store.rb
Apache-2.0
def get_or_new(resource_type, name) data = get_data(resource_type, name) new_entry(resource_type, name, data) end
Get a spec, or create a new one, depending on whether an entry exists. @param resource_type [Symbol] The type of spec to retrieve (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, ...) @param name [String] The unique identifier of the spec to retrieve @return [ManagedEntry] The entry.
get_or_new
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry_store.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry_store.rb
Apache-2.0
def get!(resource_type, name) result = get(resource_type, name) if !result raise "#{identifier(resource_type, name)} not found!" end result end
Get a spec, erroring out if the data does not exist. @param resource_type [Symbol] The type of spec to retrieve (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, ...) @param name [String] The unique identifier of the spec to retrieve @return [ManagedEntry] The entry.
get!
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry_store.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry_store.rb
Apache-2.0
def delete(resource_type, name, action_handler) delete_data(resource_type, name, action_handler) end
Delete the given spec. @param resource_type [Symbol] The type of spec to delete (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, ...) @param name [String] The unique identifier of the spec to delete @return [Boolean] Whether anything was deleted or not.
delete
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry_store.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry_store.rb
Apache-2.0
def new_entry(resource_type, name, data=nil) case resource_type when :machine MachineSpec.new(self, resource_type, name, data) when :machine_image MachineImageSpec.new(self, resource_type, name, data) when :load_balancer LoadBalancerSpec.new(self, resource_type, name, data) else ManagedEntry.new(self, resource_type, name, data) end end
Create a new managed entry of the given type.
new_entry
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/managed_entry_store.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/managed_entry_store.rb
Apache-2.0
def read_file(path) raise "read_file not overridden on #{self.class}" end
TODO: make exceptions for these instead of just returning nil / silently failing
read_file
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/transport.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/transport.rb
Apache-2.0
def config raise "config not overridden on #{self.class}" end
Config hash, including :log_level and :logger as keys
config
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/transport.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/transport.rb
Apache-2.0
def stream_chunk(options, stdout_chunk, stderr_chunk) if options[:stream].is_a?(Proc) options[:stream].call(stdout_chunk, stderr_chunk) else if stdout_chunk if options.has_key?(:stream_stdout) stream = options[:stream_stdout] elsif options[:stream] || config[:log_level] == :debug stream = config[:stdout] || STDOUT end stream.print stdout_chunk if stream end if stderr_chunk if options.has_key?(:stream_stderr) stream = options[:stream_stderr] elsif options[:stream] || config[:log_level] == :debug stream = config[:stderr] || STDERR end stream.print stderr_chunk if stream end end end
Helper to implement stdout/stderr streaming in execute
stream_chunk
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/transport.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/transport.rb
Apache-2.0
def initialize(convergence_options, config) convergence_options = Cheffish::MergedConfig.new(convergence_options.to_hash, { :client_rb_path => '/etc/chef/client.rb', :client_pem_path => '/etc/chef/client.pem' }) super(convergence_options, config) @client_rb_path ||= convergence_options[:client_rb_path] @chef_version ||= convergence_options[:chef_version] @prerelease ||= convergence_options[:prerelease] @package_cache_path ||= convergence_options[:package_cache_path] || "#{ENV['HOME']}/.chef/package_cache" @package_cache = {} @tmp_dir = '/tmp' @chef_client_timeout = convergence_options.has_key?(:chef_client_timeout) ? convergence_options[:chef_client_timeout] : 120*60 # Default: 2 hours FileUtils.mkdir_p(@package_cache_path) @package_cache_lock = Mutex.new @package_metadata ||= convergence_options[:package_metadata] end
convergence_options is a hash of setup convergence_options, including: - :chef_server - :allow_overwrite_keys - :source_key, :source_key_path, :source_key_pass_phrase - :private_key_options - :ohai_hints - :public_key_path, :public_key_format - :admin, :validator - :chef_client_timeout - :client_rb_path, :client_pem_path - :chef_version, :prerelease, :package_cache_path - :package_metadata
initialize
ruby
chef-boneyard/chef-provisioning
lib/chef/provisioning/convergence_strategy/install_cached.rb
https://github.com/chef-boneyard/chef-provisioning/blob/master/lib/chef/provisioning/convergence_strategy/install_cached.rb
Apache-2.0