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



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
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/alpha_omega/utils.rb', line 64

def self.default_pods_tasks
  Proc.new do |config, pod_name, pod, this_node|
    %w(app echo yaml).each do |tsuffix|
      # each pod task sets the pod context for host/group tasks
      config.task "#{pod_name}.#{tsuffix}" do # task pod1
        set :current_pod, pod_name
      end
    end

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

        cap_roles = $node_filter.call(this_node, n)
        next nil unless cap_roles

        config.task "#{task_name}.#{pod_name}.app" do # task host.pod1.app
          cap_roles.each do |cap_role, cap_preds|
            if $this_host["local_pods"] && $this_host["local_pods"].member?(node["env_pod"])
              role cap_role, node["node_name"], cap_preds
            else
              role cap_role, remote_name, cap_preds
            end
            config.set :dna, node_dna[remote_name]
          end
        end
      
        config.task "#{task_name}.#{pod_name}.echo" do # task host.pod1.echo
          puts "#{$magic_prefix} #{remote_name}"
        end
      
        config.task "#{task_name}.#{pod_name}.yaml" do # task host.pod1.yaml
          StringIO.new({ remote_name => n }.to_yaml).lines.to_a[1..-1].each {|l| puts "#{$magic_prefix} #{l}" }
        end
      
        %w(app echo yaml).each do |tsuffix|
          config.task "#{task_name}.#{tsuffix}" do # task host.app
            # host.app -> host.current_pod.app
            config.after "#{task_name}.#{tsuffix}", "#{task_name}.#{current_pod}.#{tsuffix}"
          end
        end

        n
      end

    self.what_groups hosts do |task_name, nodes|
      %w(app echo yaml).each do |tsuffix|
        config.task "#{task_name}.#{pod_name}.#{tsuffix}" do # task group.pod1.app
        end

        nodes.keys.sort.each do |remote_name|
          # group.pod1.app => hosts
          config.after "#{task_name}.#{pod_name}.#{tsuffix}", "#{remote_name}.#{tsuffix}"
        end

        config.task "#{task_name}.#{tsuffix}" do # task group.app
          # group.app -> group.current_pod.app
          config.after "#{task_name}.#{tsuffix}", "#{task_name}.#{current_pod}.#{tsuffix}"
        end
      end
    end
  end
end

.interesting(config) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/alpha_omega/utils.rb', line 217

def self.interesting (config)
  config.set :repository, $deploy["repository"]
  config.set :application, $deploy["application"]

  config.set :user, (ENV['_AO_USER'] || $deploy["user"] || Etc.getpwuid.name)
  config.set :group, (ENV['_AO_GROUP'] || $deploy["group"] || Etc.getgrgid(Etc.getpwuid.gid).name)

  config.set :ruby_loader, "#{ENV['_AO_RUBY_LOADER'] || $deploy["ruby_loader"]} #{$deploy["app_ruby"]}"

  # branches
  config.set :branch, self.what_branch($deploy["branches"] + [%r(#{$deploy["branch_regex"]})])

  # pods, hosts, groups
  self.setup_pods config, (ENV['_AO_CHEF'] || $deploy["chef_path"])
end

.node_defaults(node, env_pod, node_name) ⇒ Object



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

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.deep_merge!($opsdb[env_pod][:nodes][node_name]) if $opsdb[env_pod].key?(:nodes) && $opsdb[env_pod][:nodes].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"].concat $pods_config[env_pod]["cap_group"] if $pods_config[env_pod].key? "cap_group"

  node
end

.setup_pods(config, node_home) ⇒ Object



131
132
133
134
135
# File 'lib/alpha_omega/utils.rb', line 131

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

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/alpha_omega/utils.rb', line 137

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/alpha_omega/utils.rb', line 197

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



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/alpha_omega/utils.rb', line 181

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(fname, ".yaml")

      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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/alpha_omega/utils.rb', line 152

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(fname, ".yaml")
    acc[env_pod] = YAML.load(File.read(fname))
    acc
  end

  $this_pod = ENV['_AO_THIS_POD'] || File.read("/etc/podname").strip
  config.set :current_pod, $this_pod
  
  this_host = ENV['_AO_THIS_HOST'] || Socket.gethostname.chomp.split(".")[0]
  dna = YAML.load(File.read("#{node_home}/pods/#{$this_pod}/#{this_host}.yaml"))
  this_node = self.node_defaults(dna, $this_pod, this_host)
  $this_host = this_node

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