Module: PoolParty::FileWriter
- Included in:
- Master, Master, RemoteInstance
- Defined in:
- lib/modules/file_writer.rb
Instance Method Summary collapse
- #base_tmp_dir ⇒ Object
- #clear_base_directory ⇒ Object
- #make_base_directory ⇒ Object
- #remote_base_tmp_dir ⇒ Object
- #with_temp_file(str = "", &block) ⇒ Object
- #write_to_file_for(f = "haproxy", node = nil, str = "", &block) ⇒ Object
-
#write_to_temp_file(str = "") ⇒ Object
Write a temp file with the content str.
Instance Method Details
#base_tmp_dir ⇒ Object
25 26 27 |
# File 'lib/modules/file_writer.rb', line 25 def base_tmp_dir File.join(user_dir, "tmp") end |
#clear_base_directory ⇒ Object
34 35 36 |
# File 'lib/modules/file_writer.rb', line 34 def clear_base_directory `rm -rf #{base_tmp_dir}/*` if File.directory?(base_tmp_dir) end |
#make_base_directory ⇒ Object
31 32 33 |
# File 'lib/modules/file_writer.rb', line 31 def make_base_directory `mkdir -p #{base_tmp_dir}` unless File.directory?(base_tmp_dir) end |
#remote_base_tmp_dir ⇒ Object
28 29 30 |
# File 'lib/modules/file_writer.rb', line 28 def remote_base_tmp_dir "~/tmp" end |
#with_temp_file(str = "", &block) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/modules/file_writer.rb', line 17 def with_temp_file(str="", &block) Tempfile.open "#{base_tmp_dir}/poolparty-#{rand(10000)}" do |fp| fp.puts str fp.flush block.call(fp) end end |
#write_to_file_for(f = "haproxy", node = nil, str = "", &block) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/modules/file_writer.rb', line 3 def write_to_file_for(f="haproxy", node=nil, str="", &block) make_base_directory File.open("#{base_tmp_dir}/#{node ? "#{node.name}-" : ""}#{f}", "w+") do |file| file << str file << block.call if block_given? end end |
#write_to_temp_file(str = "") ⇒ Object
Write a temp file with the content str
11 12 13 14 15 16 |
# File 'lib/modules/file_writer.rb', line 11 def write_to_temp_file(str="") tempfile = Tempfile.new("#{base_tmp_dir}/poolparty-#{rand(1000)}-#{rand(1000)}") tempfile.print(str) tempfile.flush tempfile end |