Class: Docka::App
- Inherits:
-
Object
- Object
- Docka::App
- Defined in:
- lib/docka/app.rb
Class Method Summary collapse
Instance Method Summary collapse
- #config ⇒ Object
- #dir ⇒ Object
- #dot_dir ⇒ Object
- #env ⇒ Object
- #log_dir ⇒ Object
- #logger ⇒ Object
- #machine ⇒ Object
- #setup ⇒ Object
- #setup_sync ⇒ Object
- #shell(command, options = {}) ⇒ Object
- #shell!(command, options = {}) ⇒ Object
- #sync_dir ⇒ Object
- #sync_entry ⇒ Object
- #uuid ⇒ Object
- #uuid_file ⇒ Object
Class Method Details
.instance ⇒ Object
3 4 5 |
# File 'lib/docka/app.rb', line 3 def self.instance @@instance ||= self.new end |
Instance Method Details
#config ⇒ Object
55 56 57 |
# File 'lib/docka/app.rb', line 55 def config @config ||= AppConfig.new(dir + '/docka.yml', env) end |
#dir ⇒ Object
11 12 13 |
# File 'lib/docka/app.rb', line 11 def dir @dir ||= Dir.pwd end |
#dot_dir ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/docka/app.rb', line 15 def dot_dir @dot_dir ||= begin dir = "#{self.dir}/.docka" ::FileUtils.mkdir_p dir dir end end |
#env ⇒ Object
7 8 9 |
# File 'lib/docka/app.rb', line 7 def env @env ||= ::ENV['DOCKA_ENV'] || 'development' end |
#log_dir ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/docka/app.rb', line 31 def log_dir @log_dir ||= begin dir = "#{self.dir}/docka/log" ::FileUtils.mkdir_p dir dir end end |
#logger ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/docka/app.rb', line 59 def logger @logger ||= begin multi = Util::MultiLogger.new multi.loggers << ::Logger.new("#{log_dir}/#{env}.log").tap do |l| l.level = %w[development test].include?(env) ? ::Logger::DEBUG : ::Logger::INFO l.formatter = ::Proc.new do |severity, time, progname, msg| msg.split(/\n/).map{|i| "%-6s %s" % ["#{severity}:", "#{i}"]}.join("\n") + "\n" end end multi end end |
#machine ⇒ Object
102 103 104 |
# File 'lib/docka/app.rb', line 102 def machine @machine ||= Machine.new(self) end |
#setup ⇒ Object
106 107 108 |
# File 'lib/docka/app.rb', line 106 def setup machine.create end |
#setup_sync ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/docka/app.rb', line 110 def setup_sync text = ::File.exist?('/etc/exports') ? ::File.read('/etc/exports') : '' text.gsub! /# Docka #{uuid}\n.+(\n|$)/, '' a = [] a << text if text.size > 0 a << "# Docka #{uuid}" a << sync_entry shell! %( cat | sudo tee /etc/exports ), raise: true, in: "#{a.join("\n")}\n" shell! %( sudo nfsd update ), raise: true end |
#shell(command, options = {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/docka/app.rb', line 72 def shell(command, = {}) status = ::Open3.popen3(command) do |i, o, e, t| i.write [:in] if [:in] i.close ios = {} ios[:debug] = o if [:debug] ios[:error] = e ios.each do |key, io| ::Thread.new do while l = io.gets logger.__send__ key, l.chomp end end end t.value end raise ::Docka::ShellError.new(command) if [:raise] && !status.success? status end |
#shell!(command, options = {}) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/docka/app.rb', line 92 def shell!(command, = {}) opts = {} opts[:stdin_data] = [:in] if [:in] o, e, s = ::Open3.capture3(command, opts) logger.debug o.chomp if [:debug] && o.size > 0 logger.error e.chomp if e.size > 0 raise ::Docka::ShellError.new(command) if [:raise] && !s.success? [o, e, s] end |
#sync_dir ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/docka/app.rb', line 23 def sync_dir @sync_dir ||= begin dir = "#{self.dir}/docka/sync" ::FileUtils.mkdir_p dir dir end end |
#sync_entry ⇒ Object
121 122 123 |
# File 'lib/docka/app.rb', line 121 def sync_entry @sync_entry ||= "#{sync_dir} -mapall=#{::Process.uid}:#{::Process.gid} #{machine.ip}" end |
#uuid ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/docka/app.rb', line 43 def uuid @uuid ||= begin if ::File.exist?(uuid_file) ::File.read(uuid_file).strip else uuid = SecureRandom.uuid ::File.write uuid_file, uuid uuid end end end |
#uuid_file ⇒ Object
39 40 41 |
# File 'lib/docka/app.rb', line 39 def uuid_file @uuid_file ||= "#{dot_dir}/uuid" end |