Class: Boxcutter::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/boxcutter/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger = $stdout) ⇒ Command

Returns a new instance of Command.



3
4
5
# File 'lib/boxcutter/command.rb', line 3

def initialize(logger = $stdout)
  @logger = logger
end

Instance Method Details

#add_machine(opts = {}) ⇒ Object

options:

  • :backend

  • :hostname

  • :dryrun



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
# File 'lib/boxcutter/command.rb', line 42

def add_machine(opts = {})
  backend_name = opts.fetch(:backend, 'default')
  dryrun       = opts.fetch(:dryrun, false)
  hostname     = opts.fetch(:hostname)

  if machine = Boxcutter::Server.find_by_hostname("#{hostname}.blueboxgrid.com")

    app = Boxcutter::LoadBalancer::Application.all.first

    app.services.each do |service|
      if backend = service.backends.detect {|backend| backend.name == backend_name}
        log "Adding machine #{machine.hostname} to backend #{backend.name}"
        unless dryrun
          response = backend.add_machine(machine.id, :port => 80)
          log "Added #{machine.hostname} to #{backend.name} with response: #{response.body}"
        else
          log "#{machine.hostname} was not added to the backend because --dryrun was specified"
        end
      else
        log "Could not find default backend on #{service}"
      end
    end
  else
    log "Could not find server '#{hostname}' on BlueBoxGroup"
  end
end

#remove_machine(opts = {}) ⇒ Object

options:

  • :backend

  • :hostname

  • :dryrun



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/boxcutter/command.rb', line 11

def remove_machine(opts = {})
  backend_name = opts.fetch(:backend, 'default')
  dryrun       = opts.fetch(:dryrun, false)
  hostname     = opts.fetch(:hostname)

  app = Boxcutter::LoadBalancer::Application.all.first

  app.services.each do |service|
    if backend = service.backends.detect {|backend| backend.name == backend_name}
      if machine = backend.machines.detect {|machine| machine.hostname == hostname}
        log "Removing #{machine} from #{backend}"

        unless dryrun
          response = machine.remove!
          log "Response was: #{response.body}"
        else
          log "#{hostname} was not removed from the backend because --dryrun was specified"
        end
      else
        log "Could not find '#{hostname}' on #{backend}"
      end
    else
      log "Could not find '#{backend_name}' backend on #{service}"
    end
  end
end