Skip to content

Instantly share code, notes, and snippets.

@TvL2386
TvL2386 / gist:896112
Created March 31, 2011 09:35
Workshop ruby 1: client.rb
require 'socket'
require 'thread'
class Client
def initialize server_name, server_port
@server_name, @server_port = server_name, server_port
end
def connect
@socket = TCPSocket.new(@server_name, @server_port)
@TvL2386
TvL2386 / gist:897777
Created April 1, 2011 05:13
print_message function from client.rb
def print_message
Thread.new do
loop do
# monitor socket and print data if there is any
result = IO.select([@socket], nil, nil, nil)
if not result.nil?
socket = result[0].first # result[0] is the socket array. In this example there can only be one
data = socket.readpartial 4096
# use print here, because puts automatically appends a newline
@TvL2386
TvL2386 / gist:897859
Created April 1, 2011 07:41
respond_to_command method
def respond_to_command data
if data =~ /^([^\s]+):\s(?:[^\s]+):\s([^\s]+):\s(.*)$/
sender = $1
command = $3.rstrip
# directed to me?
return nil if not $2.split(',').include?(@my_name)
# if I'm still here, I guess so ;-)
# lets see if uptime is in the allowed commands array
socket = TCPSocket.new server_name, server_port
socket.write "hello? someone there?\n"
@TvL2386
TvL2386 / keepalived-notifier.rb
Created October 27, 2011 05:25
A Keepalived script
#!/usr/bin/ruby
# This is in my keepalived.conf
# ...
# notify_master "/usr/tools/keepalived/notify.sh primary"
# notify_backup "/usr/tools/keepalived/notify.sh backup"
# notify_fault "/usr/tools/keepalived/notify.sh fault"
# ...
require 'socket'
@TvL2386
TvL2386 / my_rvm_install.sh
Created November 4, 2011 21:26
rvm issue: Cannot select ruby-1.9.2-head-shay (patched ruby-1.9.2-head)
# Reproduction path:
# This was done on 2011-07-17
# Ubuntu 10.04 LTS x64
bash < <( curl https://rvm.beginrescueend.com/releases/rvm-install-head )
apt-get install build-essential bison openssl libreadline5 libreadline-dev curl git-core zlib1g zlib1g-dev libssl-dev vim libsqlite3-0 libsqlite3-dev sqlite3 libreadline-dev libxml2-dev git-core subversion autoconf
rvm version
# rvm 1.6.23 by Wayne E. Seguin (wayneeseguin@gmail.com) [https://rvm.beginrescueend.com/]
@TvL2386
TvL2386 / dcell test.rb
Created June 3, 2012 18:01
DCell: Communication with <node> interrupted
### node42.rb
require 'dcell'
DCell.start :id => "node42", :addr => "tcp://127.0.0.1:2042",
:registry => {
:adapter => 'redis',
:host => '127.0.0.1',
:port => 6379
}
require 'thread'
require 'ffi-rzmq'
def broker
ctx = ZMQ::Context.new
socket = ctx.socket(ZMQ::ROUTER)
socket.setsockopt ZMQ::LINGER, 0
socket.bind 'tcp://*:55555'
poller = ZMQ::Poller.new
source 'http://rubygems.org'
gem 'octokit' , :git => 'https://github.com/ajonas04/octokit.git'
@TvL2386
TvL2386 / gist:5195772
Last active December 15, 2015 03:39
using popen4
require 'popen4'
cmd = "ls /tmp"
status = POpen4::popen4(cmd) do |stdout,stderr,stdin,pid|
stdin.close
# get the stdout
puts "stdout: #{stdout.read.inspect}"
# get the stdin