Class: Sortinghat::Banquet

Inherits:
Object
  • Object
show all
Defined in:
lib/sortinghat/banquet.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Banquet

Creation method



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sortinghat/banquet.rb', line 8

def initialize(options = {})
  # Check that we have write premissions
  checkpermissions()

  # Create a syslog for us to use as an instance variable
  @log = Syslog::Logger.new 'sortinghat'

  # Append a trailing dot to the zone, if there isn't one
  if options[:zone][-1, 1] != '.'
    options[:zone] << '.'
  end

  # Save the options as instance variable
  @options = options

  # Create an instance varible to contain our AWS calls
  @aws = Sortinghat::AWS.new(@options[:region])
end

Instance Method Details

#dejavu?Boolean

Method to figure out if we’ve been here before

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
# File 'lib/sortinghat/banquet.rb', line 28

def dejavu?
  # Check for sentinel file
  if File.exists?('/etc/.sorted')
    # We found it, log error and exit successfully
    @log.info('Found /etc/.sorted, refusing to sort.')
    exit 0
  end
end

#finish!Object

Last method of Sortinghat



87
88
89
90
# File 'lib/sortinghat/banquet.rb', line 87

def finish!
  # Create our sentinel file
  FileUtils.touch('/etc/.sorted')
end

#start!Object

Main method of Sortinghat



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sortinghat/banquet.rb', line 38

def start!

  # Best thing to avoid run conditions are to wait
  sleep rand(10)

  # Find out who is who, instances alive
  # If discover() returns an Array full of nil(s), alive will become an empty Array
  alive = cleanup(@aws.discover())

  # Given the alive instances, find our prefix
  # If alive an empty array, selection will return the number '1'
  @prefix = ensurezero(selection(alive))

  # Put together hostname/fqdn
  construction()

  # Set the Name tag on this instance
  @aws.settag!(@hostname)

  # Find out who is who, instances alive
  # If discover() returns an Array full of nil(s), alive will become an empty Array
  alive = cleanup(@aws.discover())

  # Only enter recursion if the uniq() length of the alive array does not equal the actual length
  # On AutoScalingGroup initalization, the cleanup() should ensure the alive array is empty not nil so uniq() works
  unless alive.uniq.length == alive.length
    # There are duplicates, remove tag, wait, restart
    @aws.removetag!()
    sleep rand(10)
    start!()
  end

  # Register in DNS
  @aws.setroute53(@options[:zone], @fqdn)

  # Set the localhost hostname
  setlocal()

  # Set /etc/hosts
  sethostsfile()

  # Throw the hostname in /etc/sysconfig/httpd (if exists)
  givetohttpd()

  # All done
  finish!()
end