Class: Docka::App

Inherits:
Object
  • Object
show all
Defined in:
lib/docka/app.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



3
4
5
# File 'lib/docka/app.rb', line 3

def self.instance
  @@instance ||= self.new
end

Instance Method Details

#configObject



55
56
57
# File 'lib/docka/app.rb', line 55

def config
  @config ||= AppConfig.new(dir + '/docka.yml', env)
end

#dirObject



11
12
13
# File 'lib/docka/app.rb', line 11

def dir
  @dir ||= Dir.pwd
end

#dot_dirObject



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

#envObject



7
8
9
# File 'lib/docka/app.rb', line 7

def env
  @env ||= ::ENV['DOCKA_ENV'] || 'development'
end

#log_dirObject



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

#loggerObject



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

#machineObject



102
103
104
# File 'lib/docka/app.rb', line 102

def machine
  @machine ||= Machine.new(self)
end

#setupObject



106
107
108
# File 'lib/docka/app.rb', line 106

def setup
  machine.create
end

#setup_syncObject



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, options = {})
  status = ::Open3.popen3(command) do |i, o, e, t|
    i.write options[:in] if options[:in]
    i.close
    ios = {}
    ios[:debug] = o if options[: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 options[: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, options = {})
  opts = {}
  opts[:stdin_data] = options[:in] if options[:in]
  o, e, s = ::Open3.capture3(command, opts)
  logger.debug o.chomp if options[:debug] && o.size > 0
  logger.error e.chomp if e.size > 0
  raise ::Docka::ShellError.new(command) if options[:raise] && !s.success?
  [o, e, s]
end

#sync_dirObject



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_entryObject



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

#uuidObject



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_fileObject



39
40
41
# File 'lib/docka/app.rb', line 39

def uuid_file
  @uuid_file ||= "#{dot_dir}/uuid"
end