Class: Broham

Inherits:
RightAws::ActiveSdb::Base
  • Object
show all
Defined in:
lib/broham.rb,
lib/broham/script.rb

Overview

Broham expects a hash constant Settings with values for :secret_access_key and :access_key. The configliere gem (github.com/mrflip/configliere) can help with that.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cloudera_desktop(role = 'cloudera_desktop') ⇒ Object

Hadoop: cloudera desktop node



157
# File 'lib/broham.rb', line 157

def self.cloudera_desktop( role='cloudera_desktop')  ; host(role) ; end

.diss(*args) ⇒ Object

alternative syntax for #unregister



108
# File 'lib/broham.rb', line 108

def self.diss(*args)      unregister *args   ; end

.entry_for_role(role, ip = nil) ⇒ Object



56
57
58
59
# File 'lib/broham.rb', line 56

def self.entry_for_role role, ip=nil
  ip ||= my_default_ip
  select_by_role_and_default_ip(role, ip)
end

.establish_connection(options = {}) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/broham.rb', line 132

def self.establish_connection options={}
  return @connection if @connection
  options = { :logger => Log }.merge options
  access_key        = options[:access_key]        || Settings[:access_key]
  secret_access_key = options[:secret_access_key] || Settings[:secret_access_key]
  @connection = RightAws::ActiveSdb.establish_connection(access_key, secret_access_key, options)
end

.fuck_all_yall(*args) ⇒ Object

alternative syntax for #unregister_like



110
# File 'lib/broham.rb', line 110

def self.fuck_all_yall(*args) unregister_like *args ; end

.hadoop_jobtracker(role = 'hadoop_jobtracker') ⇒ Object

Hadoop: master jobtracker node



153
# File 'lib/broham.rb', line 153

def self.hadoop_jobtracker(role='hadoop_jobtracker') ; host(role) ; end

.hadoop_namenode(role = 'hadoop_namenode') ⇒ Object

Hadoop: master namenode



155
# File 'lib/broham.rb', line 155

def self.hadoop_namenode(  role='hadoop_namenode')   ; host(role) ; end

.host(role) ⇒ Object

Returns the last-registered host in the given role



31
32
33
# File 'lib/broham.rb', line 31

def self.host role
  select_by_role(role, :order => 'timestamp DESC')
end

.host_attrs(role) ⇒ Object



40
41
42
43
# File 'lib/broham.rb', line 40

def self.host_attrs(role)
  { :role => role, :timestamp => timestamp,
    :private_ip => my_private_ip, :public_ip => my_public_ip, :default_ip => my_default_ip, :fqdn => my_fqdn  }
end

.hosts_like(role) ⇒ Object

Returns all hosts in the given role



36
37
38
# File 'lib/broham.rb', line 36

def self.hosts_like role
  select(:all, :order => 'timestamp DESC').select{|bro| bro[:role].to_s =~ /^#{role}/ }
end

.my_availability_zoneObject



120
# File 'lib/broham.rb', line 120

def self.my_availability_zone() OHAI_INFO[:ec2][:availability_zone]              ; end

.my_default_ipObject



118
# File 'lib/broham.rb', line 118

def self.my_default_ip()        OHAI_INFO[:ipaddress]                            ; end

.my_fqdnObject



119
# File 'lib/broham.rb', line 119

def self.my_fqdn()              OHAI_INFO[:fqdn]                                 ; end

.my_private_ipObject

Registration attributes



116
# File 'lib/broham.rb', line 116

def self.my_private_ip()        OHAI_INFO[:cloud][:private_ips].first rescue nil ; end

.my_public_ipObject



117
# File 'lib/broham.rb', line 117

def self.my_public_ip()         OHAI_INFO[:cloud][:public_ips].first  rescue nil ; end

.nfs_device_path(role = 'nfs_server') ⇒ Object

NFS: device path, for stuffing into /etc/fstab



147
148
149
150
# File 'lib/broham.rb', line 147

def self.nfs_device_path role='nfs_server'
  nfs_server = host(role) or return
  [nfs_server.private_ip, nfs_server[:server_path]].join(':')
end

.register(role, attrs = {}) ⇒ Object



45
46
47
48
49
50
# File 'lib/broham.rb', line 45

def self.register role, attrs={}
  ahost = host(role) || new
  ahost.attributes = (host_attrs(role).merge(attrs))
  success = ahost.save
  success ? self.new(success) : false
end

.register_as_next(role, attrs = {}) ⇒ Object

Enlists as the next among many machines filling the given role.

This is just a simple counter: it doesn’t check whether the machine is already enlisted under a different index, or whether there are missing indices.

It uses conditional save to be sure that the count is consistent



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/broham.rb', line 70

def self.register_as_next role, attrs={}
  my_idx = 0
  if (registered_entry = entry_for_role(role)) then return registered_entry end
  100.times do
    ahost = host(role) || new
    current_max_idx  = ahost[:idx] && ahost[:idx].first
    my_idx           = current_max_idx ? current_max_idx.to_i + 1 : 0
    ahost.attributes = host_attrs(role).merge({ :idx => my_idx.to_s }.merge(attrs))
    expected         = current_max_idx ? {:idx => (current_max_idx.to_i).to_s} : {}
    registered_entry = ahost.save_if(expected)
    break if registered_entry
  end
  register role+'-'+my_idx.to_s, { :idx => my_idx }.merge(attrs)
  new registered_entry
end

.register_nfs_share(server_path, client_path = nil, role = 'nfs_server') ⇒ Object

Register an nfs server share



141
142
143
144
# File 'lib/broham.rb', line 141

def self.register_nfs_share server_path, client_path=nil, role='nfs_server'
  client_path ||= server_path
  register(role, :server_path => server_path, :client_path => client_path)
end

.roles(ip = nil) ⇒ Object



52
53
54
55
# File 'lib/broham.rb', line 52

def self.roles ip=nil
  ip ||= my_default_ip
  select_all_by_default_ip(ip).map{|entry| entry['role'] }
end

.sup?(*args) ⇒ Boolean

alternative syntax for #host

Returns:

  • (Boolean)


104
# File 'lib/broham.rb', line 104

def self.sup?(*args)      host *args    ; end

.sup_yall?(*args) ⇒ Boolean

alternative syntax for #hosts_like

Returns:

  • (Boolean)


106
# File 'lib/broham.rb', line 106

def self.sup_yall?(*args) hosts_like *args   ; end

.timestampObject



121
# File 'lib/broham.rb', line 121

def self.timestamp()            Time.now.utc.strftime("%Y%m%d%H%M%SZ")            ; end

.unregister(role) ⇒ Object



92
93
94
# File 'lib/broham.rb', line 92

def self.unregister role
  host(role).each(&:unregister)
end

.unregister_like(role) ⇒ Object

Removes all registrations for the given role wildcard



89
90
91
# File 'lib/broham.rb', line 89

def self.unregister_like role
  hosts_like(role).each(&:unregister)
end

.yo!(*args) ⇒ Object

alternative syntax for #register



100
# File 'lib/broham.rb', line 100

def self.yo!(*args)       register *args ; end

.yo_yo_yo!(*args) ⇒ Object

alternative syntax for #register_as_next



102
# File 'lib/broham.rb', line 102

def self.yo_yo_yo!(*args) register_as_next *args ; end

Instance Method Details

#availability_zoneObject



127
# File 'lib/broham.rb', line 127

def availability_zone() self['availability_zone'].first ; end

#default_ipObject



125
# File 'lib/broham.rb', line 125

def default_ip()        self['default_ip'       ].first ; end

#fqdnObject



126
# File 'lib/broham.rb', line 126

def fqdn()              self['fqdn'             ].first ; end

#idxObject



128
129
130
# File 'lib/broham.rb', line 128

def idx()
  self['idx'].first
end

#private_ipObject



123
# File 'lib/broham.rb', line 123

def private_ip()        self['private_ip'       ].first || default_ip ; end

#public_ipObject



124
# File 'lib/broham.rb', line 124

def public_ip()         self['public_ip'        ].first || default_ip ; end

#to_hashObject



159
# File 'lib/broham.rb', line 159

def to_hash() attributes ; end

#to_pretty_jsonObject



160
161
162
# File 'lib/broham.rb', line 160

def to_pretty_json
  to_hash.reject{|k,v| k.to_s == 'id'}.to_json
end

#unregisterObject



95
96
97
# File 'lib/broham.rb', line 95

def unregister
  delete
end