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



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

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

.diss(*args) ⇒ Object

alternative syntax for #unregister



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

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

.entry_for_role(role, ip = nil) ⇒ Object



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

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



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

def self.establish_connection options={}
  options = { :logger => Log }.merge options
  access_key = options[:access_key] || Settings[:access_key]
  secret_access_key = options[: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



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

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

.hadoop_jobtracker(role = 'hadoop_jobtracker') ⇒ Object

Hadoop: master jobtracker node



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

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

.hadoop_namenode(role = 'hadoop_namenode') ⇒ Object

Hadoop: master namenode



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

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

.host(role) ⇒ Object

Returns the last-registered host in the given role



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

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

.host_attrs(role) ⇒ Object



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

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



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

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

.my_availability_zoneObject



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

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

.my_default_ipObject



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

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

.my_fqdnObject



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

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

.my_private_ipObject

Registration attributes



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

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

.my_public_ipObject



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

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



145
146
147
148
# File 'lib/broham.rb', line 145

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



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

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



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

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



139
140
141
142
# File 'lib/broham.rb', line 139

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



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

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)


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

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

.sup_yall?(*args) ⇒ Boolean

alternative syntax for #hosts_like

Returns:

  • (Boolean)


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

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

.timestampObject



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

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

.unregister(role) ⇒ Object



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

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

.unregister_like(role) ⇒ Object

Removes all registrations for the given role wildcard



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

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

.yo!(*args) ⇒ Object

alternative syntax for #register



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

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

.yo_yo_yo!(*args) ⇒ Object

alternative syntax for #register_as_next



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

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

Instance Method Details

#availability_zoneObject



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

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

#default_ipObject



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

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

#fqdnObject



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

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

#idxObject



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

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

#private_ipObject



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

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

#public_ipObject



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

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

#to_hashObject



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

def to_hash() attributes ; end

#to_pretty_jsonObject



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

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

#unregisterObject



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

def unregister
  delete
end