Class: Process::Naf::MachineManager

Inherits:
Application
  • Object
show all
Defined in:
app/models/process/naf/machine_manager.rb

Instance Method Summary collapse

Instance Method Details

#workObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/process/naf/machine_manager.rb', line 14

def work
  if @list_affinities
    puts "Affinities:"
    ::Naf::Affinity.all.each do |affinity|
      parts = [
               affinity.affinity_classification_name,
               affinity.affinity_name,
              ]
      puts "  #{parts.join('_')}"
    end
    exit 0
  end

  if @update_machine
    machine = ::Naf::Machine.find_by_server_address(@server_address)
    if machine.blank?
      server_name = (`hostname`).strip
      machine = ::Naf::Machine.create(server_address: @server_address,
                                      server_name: server_name)
      add_default_affinities(machine)
    end

    machine.server_note = @server_note unless @server_note.nil?
    machine.server_name = @server_name unless @server_name.nil?
    machine.enabled = @enabled unless @enabled.nil?
    machine.thread_pool_size = @thread_pool_size unless @thread_pool_size.nil?
    machine.save!
  else
    machine = ::Naf::Machine.find_by_server_address(@server_address)

    unless machine.present?
      puts "Machine address #{@server_address} is not present -- use --update-machine"
      exit 1
    end
  end

  if @add_affinities
    @add_affinities.each do |affinity_string|
      #
      # Parse the argument string. It should consists of 2 or 3 words separated
      # by underscores.
      #
      # Example:
      #   - location_1_required
      #   - purpose_large
      #
      parts = affinity_string.split('_')
      if parts.length == 2
        classification_name = parts[0]
        affinity_name = parts[1]
        required = false
      elsif parts.length == 3
        classification_name = parts[0]
        affinity_name = parts[1]
        required = true
      else
        puts "no idea how to interpret affinity classification in: '#{affinity_string}'"
        exit 1
      end

      # Find the Affinity Classification in the Database
      affinity_classification = ::Naf::AffinityClassification.
        find_by_affinity_classification_name(classification_name)
      unless affinity_classification
        puts "could not find affinity classification: '#{classification_name}'"
        exit 1
      end

      # Find the Affinity in the Database
      affinity = ::Naf::Affinity.
        find_by_affinity_classification_id_and_affinity_name(affinity_classification.id,
                                                             affinity_name)
      unless affinity
        puts "could not find affinity: '#{affinity_name}' with classification: '#{classification_name}'"
        exit 1
      end

      # Create an affinity slot for the machine
      machine.machine_affinity_slots.create(affinity_id: affinity.id,
                                            required: required)
    end
  end

  puts "Address: #{machine.server_address}"
  puts "Name: #{machine.server_name}" unless machine.server_name.nil?
  puts "Note: #{machine.server_note}" unless machine.server_note.nil?
  puts "Enabled: #{machine.enabled}"
  puts "Thread Pool Size: #{machine.thread_pool_size}"

  if machine.affinities.empty?
    puts "No machine affinity slots"
  else
    puts "Machine Affinity Slots:"
    machine.machine_affinity_slots.each do |affinity_slot|
      affinity = affinity_slot.affinity
      parts = [
               affinity.affinity_classification_name,
               affinity.affinity_name,
              ]
      parts << "required" if affinity_slot.required
      puts "  #{parts.join('_')}"
    end
  end
end