Module: AlphaOmega

Defined in:
lib/alpha_omega/utils.rb,
lib/alpha_omega/version.rb

Constant Summary collapse

Version =
File.read(File.expand_path('../../../VERSION',  __FILE__)).strip

Class Method Summary collapse

Class Method Details

.default_pods_tasksObject



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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/alpha_omega/utils.rb', line 67

def self.default_pods_tasks
  Proc.new do |config, pod_name, pod, this_node, &node_filter|
    %w(app echo yaml).each do |tsuffix|
       # world task accumulates all.* after tasks
      config.task "world.#{tsuffix}" do # task world
      end

      # each pod task sets the pod context for host/group tasks
      config.task "#{pod_name}.#{tsuffix}" do # task default, pod1
        set :current_pod, pod_name
      end
    end

    node_dna = { }
    hosts =
      AlphaOmega.what_hosts pod do |task_name, remote_name, node|
        n = AlphaOmega.node_defaults(node, pod_name, remote_name)
        node_dna[remote_name] = {}
        node_dna[remote_name].deep_merge!(n)

        if node_filter.nil? || node_filter.call(this_node, n)
          config.task "#{task_name}.#{pod_name}.app" do # task host.default.app, host.pod1.app
            role :app, remote_name
            set :dna, node_dna[remote_name]
          end
        
          config.task "#{task_name}.#{pod_name}.echo" do # task host.default.echo, host.pod1.echo
            puts "#{AlphaOmega.magic_prefix} #{remote_name}"
          end
        
          config.task "#{task_name}.#{pod_name}.yaml" do # task host.default.yaml, host.pod1..yaml
            StringIO.new({ remote_name => n }.to_yaml).lines.to_a[1..-1].each {|l| puts "#{AlphaOmega.magic_prefix} #{l}" }
          end
        
          %w(app echo yaml).each do |tsuffix|
            config.task "#{task_name}.#{tsuffix}" do # task host -> host.current_pod
              config.after "#{task_name}.#{tsuffix}", "#{task_name}.#{current_pod}.#{tsuffix}"
            end
          end

          n
        else
          nil
        end
      end

    AlphaOmega.what_groups hosts do |task_name, nodes|
      if task_name == "all"
        # simulate all podXX all
        %w(app echo yaml).each do |tsuffix|
          config.after "world.#{tsuffix}", "#{pod_name}.#{tsuffix}" # podXX
          config.after "world.#{tsuffix}", "#{task_name}.#{tsuffix}" # all
        end
      end

      %w(app echo yaml).each do |tsuffix|
        config.task "#{task_name}.#{pod_name}.#{tsuffix}" do
        end

        nodes.keys.sort.each do |remote_name|
          config.after "#{task_name}.#{pod_name}.#{tsuffix}", "#{remote_name}.#{tsuffix}"
        end

        config.task "#{task_name}.#{tsuffix}" do
          config.after "#{task_name}.#{tsuffix}", "#{task_name}.#{current_pod}.#{tsuffix}"
        end
      end
    end
  end
end

.magic_prefixObject



11
12
13
# File 'lib/alpha_omega/utils.rb', line 11

def self.magic_prefix
  "eea914aaa8dde6fdae29242b1084a2b0415eefaf"
end

.node_defaults(node, env_pod, node_name) ⇒ Object



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
# File 'lib/alpha_omega/utils.rb', line 15

def self.node_defaults(node, env_pod, node_name)
  node_name = node_name.split(".").first

  node["node_name"] = node_name

  # defaults
  node["run_list"] ||= []
  node["cap_group"] ||= []
  node["nagios_group"] ||= []
  node["private_aliases"] ||= []
  node["node"] = nil

  # enrich with pods config
  node["env_pod"] = env_pod
  node.deep_merge!($pods_config[env_pod])

  # enrich with opsdb
  node.deep_merge!($opsdb[env_pod][node_name]) if $opsdb[env_pod].key? node_name

  node["run_list"] = node["run_list"].clone # TODO without a clone, node.run_list also updates pods_config.env_pod.run_list

  # derive
  node["fq_domain"] = %w(env_pod env_dc dc_domain).collect {|s| node[s] }.uniq.join(".")
  node["fq_name"] = %w(node_name env_pod env_dc dc_domain).collect {|s| node[s] }.uniq.join(".")
  node["p_name"] = "#{node["node_name"]}.#{node["env_pod"]}"

  # check if managed
  if $this_pod != env_pod
    node["q_name"] = "#{node["node_name"]}.#{node["env_pod"]}"
    node["managed"] = true
  else
    node["q_name"] = node["node_name"]
    node["managed"] = false
  end

  # check if infra pod
  if node["env_pod"] == node["env_dc"]
    node["infra"] = true
  else
    node["infra"] = false
    node["infra_domain"] = "#{node["env_dc"]}.#{node["dc_domain"]}"
  end

  node["run_list"].concat $pods_config[env_pod]["run_list"] if $pods_config[env_pod].key? "run_list"

  node["cap_group"] << "all"

  node["cap_group"].concat $pods_config[env_pod]["cap_group"] if $pods_config[env_pod].key? "cap_group"

  node
end

.setup_pods(config, node_home, &node_filter) ⇒ Object



138
139
140
141
142
# File 'lib/alpha_omega/utils.rb', line 138

def self.setup_pods (config, node_home, &node_filter)
  self.what_pods(config, node_home) do |config, pod_name, pod, this_node| 
    self.default_pods_tasks.call(config, pod_name, pod, this_node, &node_filter) 
  end
end

.what_branch(allowed = %w(production staging master develop) + [%r(/)]) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/alpha_omega/utils.rb', line 144

def self.what_branch (allowed = %w(production staging master develop) + [%r(/)])
  current = `cat .git/HEAD`.strip.split(" ")
  if current[0] == "ref:"
    branch_name = current[1].split("/")[2..-1].join("/")
    if allowed.any? {|rx| rx.match(branch_name) }
      branch_name
    else
      puts "current branch must be one of #{allowed.join(', ')}"
      abort
    end
  else
    current[0]
  end
end

.what_groups(nodes) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/alpha_omega/utils.rb', line 204

def self.what_groups (nodes)
  # generalize groups
  cap_groups = {}

  nodes.each do |node_name, node|
    node["cap_group"].each do |g|
      cap_groups[g] ||= {}
      cap_groups[g][node["q_name"]] = node
    end
  end

  cap_groups.each do |group_name, nodes|
    unless nodes.member? group_name
      yield group_name, nodes
    end
  end

  cap_groups
end

.what_hosts(pod) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/alpha_omega/utils.rb', line 188

def self.what_hosts (pod)
  # load all the nodes and define cap tasks
  pod["nodes_specs"].inject({}) do |hosts, spec|
    Dir[spec].inject(hosts) do |acc, fname|
      node_name = File.basename(File.basename(fname, ".yaml"), ".json")

      node = YAML.load(IO.read(fname))
      node["node_name"] = node_name

      n = yield node_name, "#{node_name}#{pod["node_suffix"]}", node unless node["virtual"]
      acc[node_name] = n if n
      acc
    end
  end
end

.what_pods(config, node_home) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/alpha_omega/utils.rb', line 159

def self.what_pods (config, node_home)
  # pods config
  $pods_config = YAML.load(File.read("#{node_home}/config/pods.yml"))

  # opsdb config
  $opsdb = Dir["#{node_home}/config/pod/*.yaml"].inject({}) do |acc, fname|
    env_pod = File.basename(File.basename(fname, ".yaml"), ".json")
    acc[env_pod] = YAML.load(File.read(fname))
    acc
  end

  $this_pod = File.read("/etc/podname").strip
  config.set :current_pod, $this_pod
  
  this_host = Socket.gethostname.chomp.split(".")[0]
  dna_base = "#{node_home}/pods/#{$this_pod}/#{this_host}"
  dna = File.exists?("#{dna_base}.yaml") ? YAML.load(File.read("#{dna_base}.yaml")) : JSON.load(File.read("#{dna_base}.json"))
  this_node = AlphaOmega.node_defaults(dna, $this_pod, this_host)

  ((this_node["pods"] || []) + [$this_pod]).inject({}) do |pods, pod_name|
    pods[pod_name] = { 
      "nodes_specs" => [ "#{node_home}/pods/#{pod_name}/*.yaml", "#{node_home}/pods/#{pod_name}/*.json" ],
      "node_suffix" => (pod_name == $this_pod ? "" : ".#{pod_name}")
    }
    yield config, pod_name, pods[pod_name], this_node
    pods
  end
end