Class: Kondate::PropertyBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/kondate/property_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ PropertyBuilder

Returns a new instance of PropertyBuilder.



9
10
11
# File 'lib/kondate/property_builder.rb', line 9

def initialize(host)
  @host = host
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/kondate/property_builder.rb', line 7

def host
  @host
end

Instance Method Details

#environmentObject



13
14
15
16
17
18
19
20
21
# File 'lib/kondate/property_builder.rb', line 13

def environment
  @environment ||=
    begin
      Config.host_plugin.get_environment(@host) || ''
    rescue => e
      $stderr.puts "cannot get environment for host:#{@host}, #{e.class} #{e.message}"
      ''
    end
end

#environment_file(environment) ⇒ Object



88
89
90
# File 'lib/kondate/property_builder.rb', line 88

def environment_file(environment)
  File.join(Config.environments_properties_dir, "#{environment}.yml")
end

#filter_roles(filters) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kondate/property_builder.rb', line 47

def filter_roles(filters)
  return self.roles if filters.nil? or filters.empty?
  filters = Array(filters).map {|filter| filter.gsub(':', '-') }
  if roles.empty? # maybe, development (vagrant) env
    @roles = filters # append specified roles
    @roles.each do |role|
      file = role_file(role)
      unless File.exist?(file)
        $stderr.puts "#{file} does not exist, possibly typo?"
        exit(1)
      end
    end
  else
    if (filters - roles).size > 0
      $stderr.puts "cannot specify #{(filters - roles).first}"
      exit(1)
    end
    unless filters.empty?
      # filter out for production env
      @roles = self.roles & filters
    end
  end
  @roles
end

#get_content(yaml_file) ⇒ Object



96
97
98
99
# File 'lib/kondate/property_builder.rb', line 96

def get_content(yaml_file)
  content = File.exist?(yaml_file) ? YAML.load_file(yaml_file) : {}
  content.is_a?(Hash) ? content : {}
end

#hostinfoObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kondate/property_builder.rb', line 33

def hostinfo
  @hostinfo ||=
    begin
      if Config.host_plugin.respond_to?(:get_hostinfo)
        Config.host_plugin.get_hostinfo(@host) || {}
      else
        {}
      end
    rescue => e
      $stderr.puts "cannot get hostinfo for host:#{@host}, #{e.class} #{e.message}"
      {}
    end
end

#install(role, filter_recipes = nil) ⇒ Object

Generate tmp node file (for each role)

{ environment: environment, role: role, roles: roles } +
environment_file + secret_environment_file +
role_file + secret_role_file +
node_file + node_secret_file

This file is automatically created and removed



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
137
138
139
140
141
142
143
144
145
# File 'lib/kondate/property_builder.rb', line 109

def install(role, filter_recipes = nil)
  files = [
    environment_file(environment),
    secret_environment_file(environment),
    role_file(role),
    secret_role_file(role),
    node_file,
    secret_node_file,
  ].compact.select {|f| File.readable?(f) }

  property = HashExt.new.deep_merge!({
    'environment' => environment,
    'role'        => role,
    'roles'       => roles,
    'hostinfo'    => hostinfo,
  })
  files.each do |file|
    property.deep_merge!(get_content(file))
  end
  property['attributes'] ||= {}

  # filter out the recipe
  if filter_recipes and !filter_recipes.empty?
    property['attributes'].keys.each do |key|
      property['attributes'].delete(key) unless filter_recipes.include?(key)
    end
  end

  if property['attributes'].empty?
    PropertyFile.new(nil, files)
  else
    fp = Tempfile.create("kondate_")
    YAML.dump(property.to_h, fp)
    fp.close
    PropertyFile.new(fp.path, files)
  end
end

#node_fileObject



72
73
74
# File 'lib/kondate/property_builder.rb', line 72

def node_file
  File.join(Config.nodes_properties_dir, "#{@host}.yml")
end

#role_file(role) ⇒ Object



80
81
82
# File 'lib/kondate/property_builder.rb', line 80

def role_file(role)
  RoleFile.explore(Config.roles_properties_dir, role, ".yml")
end

#rolesObject



23
24
25
26
27
28
29
30
31
# File 'lib/kondate/property_builder.rb', line 23

def roles
  @roles ||=
    begin
      Config.host_plugin.get_roles(@host) || []
    rescue => e
      $stderr.puts "cannot get roles for host:#{@host}, #{e.class} #{e.message}"
      []
    end
end

#secret_environment_file(environment) ⇒ Object



92
93
94
# File 'lib/kondate/property_builder.rb', line 92

def secret_environment_file(environment)
  File.join(Config.secret_environments_properties_dir, "#{environment}.yml")
end

#secret_node_fileObject



76
77
78
# File 'lib/kondate/property_builder.rb', line 76

def secret_node_file
  File.join(Config.secret_nodes_properties_dir, "#{@host}.yml")
end

#secret_role_file(role) ⇒ Object



84
85
86
# File 'lib/kondate/property_builder.rb', line 84

def secret_role_file(role)
  RoleFile.explore(Config.secret_roles_properties_dir, role, ".yml")
end