File: base.rb

package info (click to toggle)
ruby-foreman 0.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 584 kB
  • sloc: ruby: 2,020; sh: 88; makefile: 8
file content (135 lines) | stat: -rw-r--r-- 3,441 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require "foreman/export"
require "pathname"
require "shellwords"

class Foreman::Export::Base

  attr_reader :location
  attr_reader :engine
  attr_reader :options
  attr_reader :formation

  # deprecated
  attr_reader :port

  def initialize(location, engine, options={})
    @location  = location
    @engine    = engine
    @options   = options.dup
    @formation = engine.formation
  end

  def export
    error("Must specify a location") unless location
    FileUtils.mkdir_p(location) rescue error("Could not create: #{location}")
    chown user, log
    chown user, run
  end

  def app
    options[:app] || "app"
  end

  def log
    options[:log] || "/var/log/#{app}"
  end

  def run
    options[:run] || "/var/run/#{app}"
  end

  def user
    options[:user] || app
  end

private ######################################################################

  def chown user, dir
    FileUtils.chown user, nil, dir
  rescue
    error("Could not chown #{dir} to #{user}") unless File.writable?(dir) || ! File.exist?(dir)
  end

  def error(message)
    raise Foreman::Export::Exception.new(message)
  end

  def say(message)
    puts "[foreman export] %s" % message
  end
  
  def clean(filename)
    return unless File.exist?(filename)
    say "cleaning up: #{filename}"
    FileUtils.rm(filename)
  end

  def clean_dir(dirname)
    return unless File.exist?(dirname)
    say "cleaning up directory: #{dirname}"
    FileUtils.rm_r(dirname)
  end

  def shell_quote(value)
    Shellwords.escape(value)
  end

  # deprecated
  def old_export_template(exporter, file, template_root)
    if template_root && File.exist?(file_path = File.join(template_root, file))
      File.read(file_path)
    elsif File.exist?(file_path = File.expand_path(File.join("~/.foreman/templates", file)))
      File.read(file_path)
    else
      File.read(File.expand_path("../../../../data/export/#{exporter}/#{file}", __FILE__))
    end
  end

  def export_template(name, file=nil, template_root=nil)
    if file && template_root
      old_export_template name, file, template_root
    else
      name_without_first = name.split("/")[1..-1].join("/")
      matchers = []
      matchers << File.join(options[:template], name_without_first) if options[:template]
      matchers << File.expand_path("~/.foreman/templates/#{name}")
      matchers << File.expand_path("../../../../data/export/#{name}", __FILE__)
      File.read(matchers.detect { |m| File.exist?(m) })
    end
  end

  def write_template(name, target, binding)
    compiled = if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
                 ERB.new(export_template(name), trim_mode: '-').result(binding)
               else
                 ERB.new(export_template(name), nil, '-').result(binding)
               end
    write_file target, compiled
  end

  def chmod(mode, file)
    say "setting #{file} to mode #{mode}"
    FileUtils.chmod mode, File.join(location, file)
  end

  def create_directory(dir)
    say "creating: #{dir}"
    FileUtils.mkdir_p(File.join(location, dir))
  end

  def create_symlink(link, target)
    say "symlinking: #{link} -> #{target}"
    FileUtils.symlink(target, File.join(location, link))
  end

  def write_file(filename, contents)
    say "writing: #{filename}"

    filename = File.join(location, filename) unless Pathname.new(filename).absolute?

    File.open(filename, "w") do |file|
      file.puts contents
    end
  end

end