Class: Cheftacular::FileSystem
- Inherits:
-
Object
- Object
- Cheftacular::FileSystem
- Defined in:
- lib/cheftacular/file_system.rb
Instance Method Summary collapse
- #check_nodes_file_cache(nodes = []) ⇒ Object
- #cleanup_file_caches(mode = 'old', check_current_day_entry = false) ⇒ Object
- #compare_file_node_cache_against_chef_nodes(mode = 'include?') ⇒ Object
- #current_audit_file_path ⇒ Object
- #current_chef_repo_cheftacular_file_cache_path ⇒ Object
- #current_environment_config_cache_file_path ⇒ Object
- #current_nodes_file_cache_path ⇒ Object
- #current_version_file_path ⇒ Object
- #generate_report_from_node_hash(report_name, node_hash = {}, out = []) ⇒ Object
-
#initialize(options, config) ⇒ FileSystem
constructor
A new instance of FileSystem.
- #initialize_log_directories(should_cleanup_file_caches = true) ⇒ Object
- #is_junk_filename?(filename) ⇒ Boolean
- #local_cheftacular_file_cache_path ⇒ Object
- #log_directories ⇒ Object
- #parse_berkshelf_cookbook_versions(version = 'latest', berkshelf_cookbooks = {}) ⇒ Object
- #parse_chef_repo_cookbook_versions(chef_repo_cookbooks = {}) ⇒ Object
- #parse_gemfile_gem_version(gem_name) ⇒ Object
- #parse_version_from_berkshelf_cookbook(berkshelf_cookbook) ⇒ Object
- #remove_current_file_node_cache ⇒ Object
- #remove_log_directories(directories_to_not_remove_array = []) ⇒ Object
- #scrub_from_known_hosts(target) ⇒ Object
- #write_chef_repo_cheftacular_cache_file(hash) ⇒ Object
- #write_chef_repo_cheftacular_yml_file(file_location) ⇒ Object
- #write_config_cheftacular_yml_file(to_be_created_filename = 'cheftacular.yml', example_filename = 'cheftacular.yml') ⇒ Object
- #write_environment_config_cache ⇒ Object
- #write_local_cheftacular_cache_file(hash_string) ⇒ Object
- #write_nodes_file_cache(nodes) ⇒ Object
- #write_version_file(version) ⇒ Object
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 , config @options, @config = , config end |
Instance Method Details
#check_nodes_file_cache(nodes = []) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/cheftacular/file_system.rb', line 25 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
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 |
# File 'lib/cheftacular/file_system.rb', line 78 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cheftacular/file_system.rb', line 51 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_path ⇒ Object
39 40 41 |
# File 'lib/cheftacular/file_system.rb', line 39 def current_audit_file_path current_file_path 'audit-check.txt' end |
#current_chef_repo_cheftacular_file_cache_path ⇒ Object
120 121 122 |
# File 'lib/cheftacular/file_system.rb', line 120 def current_chef_repo_cheftacular_file_cache_path current_file_path "chef_repo_cheftacular_cache" end |
#current_environment_config_cache_file_path ⇒ Object
43 44 45 |
# File 'lib/cheftacular/file_system.rb', line 43 def current_environment_config_cache_file_path current_file_path 'environment_config-check.txt' end |
#current_nodes_file_cache_path ⇒ Object
74 75 76 |
# File 'lib/cheftacular/file_system.rb', line 74 def current_nodes_file_cache_path current_file_path 'node_cache' end |
#current_version_file_path ⇒ Object
35 36 37 |
# File 'lib/cheftacular/file_system.rb', line 35 def current_version_file_path current_file_path 'version-check.txt' end |
#generate_report_from_node_hash(report_name, node_hash = {}, out = []) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/cheftacular/file_system.rb', line 239 def generate_report_from_node_hash report_name, node_hash={}, out=[] node_hash.each_pair do |serv_name, output| out << "#{ serv_name }:" output.join("\n").split("\n").each do |line| out << " #{ line }" end out << "\n" end puts(out) if @options['no_logs'] || @options['verbose'] log_loc, = @config['helper']. puts("Generating log file for #{ report_name } at #{ log_loc }/#{ report_name.gsub(' ', '-') }-#{ }.txt") unless @options['quiet'] File.open("#{ log_loc }/#{ report_name.gsub(' ', '-') }-#{ }.txt", "w") { |f| f.write(out.join("\n").scrub_pretty_text) } unless @options['no_logs'] end |
#initialize_log_directories(should_cleanup_file_caches = true) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/cheftacular/file_system.rb', line 221 def initialize_log_directories should_cleanup_file_caches=true log_directories.each do |sub_log_directory| FileUtils.mkdir_p File.join( @config['locs']['chef-log'], sub_log_directory ) end FileUtils.mkdir_p File.join( @config['locs']['app-tmp'], @config['helper'].declassify) FileUtils.mkdir_p @config['filesystem'].current_nodes_file_cache_path cleanup_file_caches if should_cleanup_file_caches end |
#is_junk_filename?(filename) ⇒ Boolean
47 48 49 |
# File 'lib/cheftacular/file_system.rb', line 47 def is_junk_filename? filename filename =~ /.DS_Store|.com.apple.timemachine.supported|README.*/ || filename == '.' || filename == '..' && File.directory?(filename) end |
#local_cheftacular_file_cache_path ⇒ Object
124 125 126 |
# File 'lib/cheftacular/file_system.rb', line 124 def local_cheftacular_file_cache_path current_file_path "local_cheftacular_cache", false end |
#log_directories ⇒ Object
7 8 9 |
# File 'lib/cheftacular/file_system.rb', line 7 def log_directories ['applog', 'deploy', 'failed-deploy', 'rolelog', 'rvm', 'server-setup', 'stashedlog', 'server-update', 'ssh-exec'] end |
#parse_berkshelf_cookbook_versions(version = 'latest', berkshelf_cookbooks = {}) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/cheftacular/file_system.rb', line 144 def parse_berkshelf_cookbook_versions version='latest', 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) if version == 'latest' use_current_cookbook = @config['helper'].is_higher_version?(cookbook_version, berkshelf_cookbooks[true_cookbook_name]['version']) if berkshelf_cookbooks.has_key?(true_cookbook_name) else use_current_cookbook = true if berkshelf_cookbooks.has_key?(true_cookbook_name) && cookbook_version == version end 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.("#{ @config['locs']['berks'] }/#{ berkshelf_cookbook }")) end end berkshelf_cookbooks end |
#parse_chef_repo_cookbook_versions(chef_repo_cookbooks = {}) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/cheftacular/file_system.rb', line 173 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.("#{ @config['locs']['cookbooks'] }/#{ chef_repo_cookbook }/metadata.rb")) File.read(File.("#{ @config['locs']['cookbooks'] }/#{ chef_repo_cookbook }/metadata.rb")).gsub('"',"'").gsub(/^version[\s]*('\d[.\d]+')/).peek[/('\d[.\d]+')/].gsub("'",'') else JSON.parse(File.read(File.("#{ @config['locs']['cookbooks'] }/#{ chef_repo_cookbook }/metadata.json"))).to_hash['version'] end end chef_repo_cookbooks end |
#parse_gemfile_gem_version(gem_name) ⇒ Object
197 198 199 |
# File 'lib/cheftacular/file_system.rb', line 197 def parse_gemfile_gem_version gem_name File.read(File.("#{ @config['locs']['root'] }/Gemfile"))[/#{ gem_name }.*([\d\.]+)/][/([\d\.]+)/] end |
#parse_version_from_berkshelf_cookbook(berkshelf_cookbook) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/cheftacular/file_system.rb', line 189 def parse_version_from_berkshelf_cookbook berkshelf_cookbook if File.exists?(File.("#{ @config['locs']['berks'] }/#{ berkshelf_cookbook }/metadata.rb")) File.read(File.("#{ @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_cache ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/cheftacular/file_system.rb', line 110 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 |
#remove_log_directories(directories_to_not_remove_array = []) ⇒ Object
233 234 235 236 237 |
# File 'lib/cheftacular/file_system.rb', line 233 def remove_log_directories directories_to_not_remove_array=[] (log_directories - directories_to_not_remove_array).each do |log_directory| FileUtils.rm_rf File.join( @config['locs']['chef-log'], log_directory.strip ) end end |
#scrub_from_known_hosts(target) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/cheftacular/file_system.rb', line 201 def scrub_from_known_hosts target puts "Clearing #{ target } from known_hosts file." case RbConfig::CONFIG['host_os'] when /mswin|windows/i puts "#{ __method__ } does not support this operating system at this time" when /linux|arch/i puts "#{ __method__ } does not support this operating system at this time" when /sunos|solaris/i puts "#{ __method__ } does not support this operating system at this time" when /darwin/i #Removes the entire line containing the string `sed -i '' "s/#{ target }.*//g" ~/.ssh/known_hosts` #remove empty lines `sed -i '' "/^$/d" ~/.ssh/known_hosts` else puts "#{ __method__ } does not support this operating system at this time" end end |
#write_chef_repo_cheftacular_cache_file(hash) ⇒ Object
128 129 130 |
# File 'lib/cheftacular/file_system.rb', line 128 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
136 137 138 |
# File 'lib/cheftacular/file_system.rb', line 136 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
140 141 142 |
# File 'lib/cheftacular/file_system.rb', line 140 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_cache ⇒ Object
21 22 23 |
# File 'lib/cheftacular/file_system.rb', line 21 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
132 133 134 |
# File 'lib/cheftacular/file_system.rb', line 132 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
15 16 17 18 19 |
# File 'lib/cheftacular/file_system.rb', line 15 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
11 12 13 |
# File 'lib/cheftacular/file_system.rb', line 11 def write_version_file version File.open( current_version_file_path, "w") { |f| f.write(version) } end |