Class: Cheftacular::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/cheftacular/file_system.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, config) ⇒ FileSystem

Returns a new instance of FileSystem.



3
4
5
# File 'lib/cheftacular/file_system.rb', line 3

def initialize options, config
  @options, @config  = options, config
end

Instance Method Details

#check_nodes_file_cache(nodes = []) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/cheftacular/file_system.rb', line 21

def check_nodes_file_cache nodes=[]
  Dir.entries(current_nodes_file_cache_path).each do |location|
    next if is_junk_filename?(location)

    nodes << @config['ridley'].node.from_file("#{ current_nodes_file_cache_path }/#{ location }" )
  end

  nodes
end

#cleanup_file_caches(mode = 'old', check_current_day_entry = false) ⇒ Object



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
# File 'lib/cheftacular/file_system.rb', line 74

def cleanup_file_caches mode='old', check_current_day_entry=false
  base_dir = File.join( @config['locs']['app-root'], 'tmp', @config['helper'].declassify )

  Dir.entries(base_dir).each do |entry|
    next if is_junk_filename?(entry)
    next if File.file?("#{ base_dir }/#{ entry }") && entry == 'local_cheftacular_cache' && mode != 'all'

    case mode
    when 'old'
      FileUtils.rm("#{ base_dir }/#{ entry }") if File.file?("#{ base_dir }/#{ entry }") && !entry.include?(Time.now.strftime("%Y%m%d"))
    when 'current-nodes'
      check_current_day_entry = true
    when 'all'
      FileUtils.rm("#{ base_dir }/#{ entry }") if File.file?("#{ base_dir }/#{ entry }")

      FileUtils.rm_rf("#{ base_dir }/#{ entry }") if File.exists?("#{ base_dir }/#{ entry }") && File.directory?("#{ base_dir }/#{ entry }")
    when 'current-audit-only'
      FileUtils.rm("#{ base_dir }/#{ entry }") if File.file?("#{ base_dir }/#{ entry }") && entry.include?(Time.now.strftime("%Y%m%d"))
    else
      puts "Warning, received unknown mode (#{ mode })! Please post your logs as an issue on github!"
    end

    if File.exists?("#{ base_dir }/#{ entry }") && File.directory?("#{ base_dir }/#{ entry }")
      FileUtils.rm_rf("#{ base_dir }/#{ entry }") if !check_current_day_entry && !entry.include?(Time.now.strftime("%Y%m%d"))
      
      FileUtils.rm_rf("#{ base_dir }/#{ entry }") if check_current_day_entry && entry.include?(Time.now.strftime("%Y%m%d"))

      FileUtils.mkdir_p current_nodes_file_cache_path
    end
  end
end

#compare_file_node_cache_against_chef_nodes(mode = 'include?') ⇒ Object



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

def compare_file_node_cache_against_chef_nodes mode='include?'
  chef_server_names, nodes_file_cache_names = [],[]
  included = true 

  @config['chef_nodes'].each { |node| chef_server_names << node.name }

  check_nodes_file_cache.each { |node| nodes_file_cache_names << node.name }

  nodes_file_cache_names.each do |node_name|
    unless chef_server_names.include?(node_name)
      included = false

      break
    end
  end

  case mode
  when 'include?'  then return included
  when 'not_equal' then return check_nodes_file_cache.count != names.count
  when 'equal'     then return chef_server_names.sort == nodes_file_cache_names.sort
  end
end

#current_audit_file_pathObject



35
36
37
# File 'lib/cheftacular/file_system.rb', line 35

def current_audit_file_path
  current_file_path 'audit-check.txt'
end

#current_chef_repo_cheftacular_file_cache_pathObject



116
117
118
# File 'lib/cheftacular/file_system.rb', line 116

def current_chef_repo_cheftacular_file_cache_path
  current_file_path "chef_repo_cheftacular_cache"
end

#current_environment_config_cache_file_pathObject



39
40
41
# File 'lib/cheftacular/file_system.rb', line 39

def current_environment_config_cache_file_path
  current_file_path 'environment_config-check.txt'
end

#current_nodes_file_cache_pathObject



70
71
72
# File 'lib/cheftacular/file_system.rb', line 70

def current_nodes_file_cache_path
  current_file_path 'node_cache'
end

#current_version_file_pathObject



31
32
33
# File 'lib/cheftacular/file_system.rb', line 31

def current_version_file_path
  current_file_path 'version-check.txt'
end

#is_junk_filename?(filename) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/cheftacular/file_system.rb', line 43

def is_junk_filename? filename
  filename =~ /.DS_Store|.com.apple.timemachine.supported|README.*/ || filename == '.' || filename == '..' && File.directory?(filename)
end

#local_cheftacular_file_cache_pathObject



120
121
122
# File 'lib/cheftacular/file_system.rb', line 120

def local_cheftacular_file_cache_path
  current_file_path "local_cheftacular_cache", false
end

#parse_chef_repo_cookbook_versions(chef_repo_cookbooks = {}) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cheftacular/file_system.rb', line 164

def parse_chef_repo_cookbook_versions chef_repo_cookbooks={}
  Dir.foreach(@config['locs']['cookbooks']) do |chef_repo_cookbook|
    next if is_junk_filename?(chef_repo_cookbook)
      
    new_name = chef_repo_cookbook.rpartition('-').first

    chef_repo_cookbooks[chef_repo_cookbook] = if File.exists?(File.expand_path("#{ @config['locs']['cookbooks'] }/#{ chef_repo_cookbook }/metadata.rb"))
                                                File.read(File.expand_path("#{ @config['locs']['cookbooks'] }/#{ chef_repo_cookbook }/metadata.rb")).gsub('"',"'").gsub(/^version[\s]*('\d[.\d]+')/).peek[/('\d[.\d]+')/].gsub("'",'')
                                              else
                                                JSON.parse(File.read(File.expand_path("#{ @config['locs']['cookbooks'] }/#{ chef_repo_cookbook }/metadata.json"))).to_hash['version']
                                              end
  end

  chef_repo_cookbooks
end

#parse_latest_berkshelf_cookbook_versions(berkshelf_cookbooks = {}) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cheftacular/file_system.rb', line 140

def parse_latest_berkshelf_cookbook_versions berkshelf_cookbooks={}

  Dir.foreach(@config['locs']['berks']) do |berkshelf_cookbook|
    next if is_junk_filename?(berkshelf_cookbook)
    use_current_cookbook = false

    true_cookbook_name = berkshelf_cookbook.rpartition('-').first
    cookbook_version   = parse_version_from_berkshelf_cookbook(berkshelf_cookbook)

    #get only the latest version, berkshelf pulls in multiple commits from git repos for SOME REASON
    use_current_cookbook = !berkshelf_cookbooks.has_key?(true_cookbook_name)
    use_current_cookbook = @config['helper'].is_higher_version?(cookbook_version, berkshelf_cookbooks[true_cookbook_name]['version']) if berkshelf_cookbooks.has_key?(true_cookbook_name)

    if use_current_cookbook
      berkshelf_cookbooks[true_cookbook_name] ||= {}
      berkshelf_cookbooks[true_cookbook_name]['version']  = cookbook_version
      berkshelf_cookbooks[true_cookbook_name]['location'] = berkshelf_cookbook
      berkshelf_cookbooks[true_cookbook_name]['mtime']    = File.mtime(File.expand_path("#{ @config['locs']['berks'] }/#{ berkshelf_cookbook }"))
    end
  end

  berkshelf_cookbooks
end

#parse_version_from_berkshelf_cookbook(berkshelf_cookbook) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/cheftacular/file_system.rb', line 180

def parse_version_from_berkshelf_cookbook berkshelf_cookbook
  if File.exists?(File.expand_path("#{ @config['locs']['berks'] }/#{ berkshelf_cookbook }/metadata.rb"))
    File.read(File.expand_path("#{ @config['locs']['berks'] }/#{ berkshelf_cookbook }/metadata.rb")).gsub('"',"'").gsub(/^version[\s]*('\d[.\d]+')/).peek[/('\d[.\d]+')/].gsub("'",'')
  else
    berkshelf_cookbook.split('-').last
  end
end

#remove_current_file_node_cacheObject



106
107
108
109
110
111
112
113
114
# File 'lib/cheftacular/file_system.rb', line 106

def remove_current_file_node_cache
  base_dir = File.join( @config['locs']['app-root'], 'tmp', @config['helper'].declassify )

  Dir.entries(base_dir).each do |entry|
    next if is_junk_filename?(entry)

    FileUtils.rm_rf("#{ base_dir }/#{ entry }") if File.directory?("#{ base_dir }/#{ entry }") && entry.include?(Time.now.strftime("%Y%m%d"))
  end      
end

#write_chef_repo_cheftacular_cache_file(hash) ⇒ Object



124
125
126
# File 'lib/cheftacular/file_system.rb', line 124

def write_chef_repo_cheftacular_cache_file hash
  File.open( current_chef_repo_cheftacular_file_cache_path, "w") { |f| f.write(hash) }
end

#write_chef_repo_cheftacular_yml_file(file_location) ⇒ Object



132
133
134
# File 'lib/cheftacular/file_system.rb', line 132

def write_chef_repo_cheftacular_yml_file file_location
  File.open( file_location, "w") { |f| f.write(@config['helper'].compile_chef_repo_cheftacular_yml_as_hash.to_yaml) }
end

#write_config_cheftacular_yml_file(to_be_created_filename = 'cheftacular.yml', example_filename = 'cheftacular.yml') ⇒ Object



136
137
138
# File 'lib/cheftacular/file_system.rb', line 136

def write_config_cheftacular_yml_file to_be_created_filename='cheftacular.yml', example_filename='cheftacular.yml'
  File.open( File.join(@config['locs']['chef-repo'], "config", to_be_created_filename), "w") { |f| f.write(File.read(File.join(@config['locs']['examples'], example_filename))) }
end

#write_environment_config_cacheObject



17
18
19
# File 'lib/cheftacular/file_system.rb', line 17

def write_environment_config_cache
  File.open( current_environment_config_cache_file_path, "w") { |f| f.write("set for #{ Time.now.strftime("%Y%m%d") }") }
end

#write_local_cheftacular_cache_file(hash_string) ⇒ Object



128
129
130
# File 'lib/cheftacular/file_system.rb', line 128

def write_local_cheftacular_cache_file hash_string
  File.open( local_cheftacular_file_cache_path, 'w') { |f| f.write(hash_string) }
end

#write_nodes_file_cache(nodes) ⇒ Object



11
12
13
14
15
# File 'lib/cheftacular/file_system.rb', line 11

def write_nodes_file_cache nodes
  nodes.each do |node|
    File.open( File.join( current_nodes_file_cache_path, "#{ node.name }.json"), "w") { |f| f.write(node.to_json) }
  end
end

#write_version_file(version) ⇒ Object



7
8
9
# File 'lib/cheftacular/file_system.rb', line 7

def write_version_file version
  File.open( current_version_file_path, "w") { |f| f.write(version) }
end