Module: Toquen::LocalWriter

Defined in:
lib/toquen/local_writer.rb

Class Method Summary collapse

Class Method Details

.create_node(details) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/toquen/local_writer.rb', line 3

def self.create_node(details)
  FileUtils.mkdir_p fetch(:chef_nodes_path)
  path = File.join(fetch(:chef_nodes_path), "#{details[:name]}.json")
  existing = File.exist?(path) ? JSON.parse(File.read(path)) : {}
  open(path, 'w') do |f|
    node = {
      'name' => details[:name],
      'chef_environment' => details[:environment],
      'normal' => { toquen: details },
      'run_list' => details[:roles].map { |r| "role[#{r}]" }
    }
    f.write JSON.pretty_generate(existing.merge(node))
  end
end

.create_stage(name, servers) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/toquen/local_writer.rb', line 18

def self.create_stage(name, servers)
  run_locally { info "Creating stage '#{name}' with servers: #{servers.map { |s| s[:name] }.join(', ')}" }
  open("config/deploy/#{name}.rb", 'w') do |f|
    f.write("# This file will be overwritten by toquen!  Don't put anything here.\n")
    f.write("set :stage, '#{name}'.intern\n")
    secgroups = []
    servers.each do |details|
      rstring = (details[:roles] + ['all', "server-#{details[:name]}"]).join(' ')
      f.write("server '#{details[:external_ip]}', ")
      f.write("roles: %w{#{rstring}}, ")
      f.write("environment: \"#{details[:environment]}\", ") unless details[:environment].nil?
      f.write("awsname: '#{details[:name]}'\n")
      secgroups += details[:security_groups]
    end
    secstring = secgroups.uniq.join(' ')
    f.write("set :filter, roles: %w{#{name}}, secgroups: %w{#{secstring}}\n")
  end
end

.superfluous_check!(servers, roles) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/toquen/local_writer.rb', line 37

def self.superfluous_check!(servers, roles)
  # check for superflous stages / data bag items and warn if found
  run_locally do
    Dir["#{fetch(:chef_data_bags_path)}/servers/*.json"].each do |path|
      unless servers.include? File.basename(path, '.json')
        warn "Data bag item #{path} does not represent an active server. You should delete it."
      end
    end

    stages = roles + servers.map { |n| "server-#{n}" }
    Dir['config/deploy/*.rb'].each do |path|
      unless stages.include? File.basename(path, '.rb')
        warn "Stage #{path} does not represent an active server. You should delete it."
      end
    end
  end
end