Class: Flintlock::Module

Inherits:
Object
  • Object
show all
Defined in:
lib/flintlock/module.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri = nil, options = {}) ⇒ Module

Returns a new instance of Module.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/flintlock/module.rb', line 24

def initialize(uri = nil, options={})
  # track temporary files and directories for deletion
  @tmpfiles = []

  # destroy tmp files on exit 
  at_exit { handle_exit }

  @uri = uri || Dir.pwd
  @log = Util.load_logger(!!options[:debug])
  @runner = Runner.new(options)
 
  @root_dir = detect_root_dir(download_from_uri(@uri))
  @metadata = (@root_dir)

  load_scripts!
  validate

  @env = load_env(@defaults_script)
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



22
23
24
# File 'lib/flintlock/module.rb', line 22

def 
  @metadata
end

#uriObject (readonly)

Returns the value of attribute uri.



22
23
24
# File 'lib/flintlock/module.rb', line 22

def uri
  @uri
end

Class Method Details

.package(directory, options = {}) ⇒ Object

Raises:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/flintlock/module.rb', line 202

def self.package(directory, options={})
  Util.depends_on 'tar'

  mod = Module.new(directory, options)
  archive = mod.package_name + '.tar.gz'

  if Util.path_split(directory).length > 1
    change_to = File.dirname(directory)
    archive_path = File.basename(directory)
  else
    change_to = '.'
    archive_path = directory
  end

  status = Runner.new(options).run(['tar', 'cfz', archive, '-C', change_to, archive_path])
  raise PackagingError.new(directory) if status != 0
  archive
end

.script_namesObject



136
137
138
# File 'lib/flintlock/module.rb', line 136

def self.script_names
  ['defaults', *Module.stages, 'stop']
end

.stagesObject



132
133
134
# File 'lib/flintlock/module.rb', line 132

def self.stages
  ['detect', 'prepare', 'stage', 'start', 'modify']
end

Instance Method Details

#create_app_dir(app_dir) ⇒ Object



197
198
199
200
# File 'lib/flintlock/module.rb', line 197

def create_app_dir(app_dir)
  FileUtils.mkdir_p(app_dir)
  raise if ! Util.empty_directory?(app_dir)
end

#current_envObject



178
179
180
# File 'lib/flintlock/module.rb', line 178

def current_env
  Hash[ENV.to_a] # get rid of ENV obj
end

#defaultsObject



193
194
195
# File 'lib/flintlock/module.rb', line 193

def defaults
  Hash[@env.to_a - ENV.to_a] 
end

#detectObject



148
149
150
151
# File 'lib/flintlock/module.rb', line 148

def detect
  @log.info("running detect stage: #{@detect_script}")
  run_script(@detect_script)
end

#download_from_uri(uri) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/flintlock/module.rb', line 44

def download_from_uri(uri)
  case Util.get_uri_scheme(uri)
  when nil, 'file' # no scheme == local file
    handle_file(uri)
  when 'git'
    handle_git_uri(uri)
  when 'svn'
    handle_svn_uri(uri)
  when 'http', 'https'
    # over these protocols, we're getting an archive
    handle_file(handle_http_uri(uri))
  else
    raise UnsupportedModuleURI, uri
  end
end

#full_nameObject



124
125
126
# File 'lib/flintlock/module.rb', line 124

def full_name
  @metadata.full_name
end

#handle_exitObject



60
61
62
# File 'lib/flintlock/module.rb', line 60

def handle_exit
  @tmpfiles.each { |x| FileUtils.rm_rf(x, :secure => true) }
end

#handle_file(filename) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/flintlock/module.rb', line 99

def handle_file(filename)
  @log.debug("handling file '#{filename}'")
  Util.depends_on 'tar'

  tmpdir = Dir.mktmpdir
  @tmpfiles << tmpdir

  mime = Util.mime_type(filename)
  @log.debug("mime-type is '#{mime}'")

  case mime 
  when 'application/x-directory'
    return filename
  when 'application/x-gzip'
    command = ['tar', 'xfz', filename, '-C', tmpdir]
  when 'application/x-tar'
    command = ['tar', 'xf', filename, '-C', tmpdir]
  else
    raise UnsupportedModuleURI, filename
  end
  status = @runner.run(command)
  raise ModuleDownloadError, filename if status != 0
  tmpdir
end

#handle_git_uri(uri) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/flintlock/module.rb', line 64

def handle_git_uri(uri)
  Util.depends_on 'git'

  root_dir = Dir.mktmpdir
  @tmpfiles << root_dir
  status = @runner.run(['git', 'clone', uri, root_dir])
  raise ModuleDownloadError, uri if status != 0 
  root_dir
end

#handle_http_uri(uri, buffer = 8192) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/flintlock/module.rb', line 84

def handle_http_uri(uri, buffer=8192)
  tmpfile = Tempfile.new(['flintlock', Util.full_extname(uri)]).path
  @tmpfiles << tmpfile
  open(uri) do |input|
    open(tmpfile, 'wb') do |output|
      while ( buf = input.read(buffer))
        output.write buf
      end
    end
  end
  tmpfile
rescue OpenURI::HTTPError, OpenSSL::SSL::SSLError
  raise ModuleDownloadError, uri
end

#handle_svn_uri(uri) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/flintlock/module.rb', line 74

def handle_svn_uri(uri)
  Util.depends_on 'svn'

  root_dir = Dir.mktmpdir
  @tmpfiles << root_dir
  status = @runner.run(['svn', 'checkout', uri, root_dir])
  raise ModuleDownloadError, uri if status != 0 
  root_dir
end

#load_env(defaults_script) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'lib/flintlock/module.rb', line 182

def load_env(defaults_script)
  # hokey, but seems to work
  env_data = %x{set -a && source #{defaults_script} && env}.split("\n").map{ |x| x.split('=', 2) }
  env = Hash[env_data]
  @log.debug("defaults script is #{defaults_script}")
  @log.debug("defaults env is #{env.inspect}")
  env = env.merge(current_env)
  @log.debug("merged env is #{env.inspect}")
  env
end

#modify(app_dir) ⇒ Object



163
164
165
166
# File 'lib/flintlock/module.rb', line 163

def modify(app_dir)
  @log.info("running modify stage: #{@modify_script}")
  run_script(@modify_script, app_dir)
end

#package_nameObject



128
129
130
# File 'lib/flintlock/module.rb', line 128

def package_name
  @metadata.package_name
end

#prepareObject



153
154
155
156
# File 'lib/flintlock/module.rb', line 153

def prepare
  @log.info("running prepare stage: #{@prepare_script}")
  run_script(@prepare_script)
end

#scriptsObject



140
141
142
# File 'lib/flintlock/module.rb', line 140

def scripts
  [@modify_script, @prepare_script, @stage_script, @start_script, @stop_script, @defaults_script, @detect_script]
end

#stage(app_dir) ⇒ Object



158
159
160
161
# File 'lib/flintlock/module.rb', line 158

def stage(app_dir)
  @log.info("running stage stage: #{@stage_script}")
  run_script(@stage_script, app_dir)
end

#start(app_dir) ⇒ Object



168
169
170
171
# File 'lib/flintlock/module.rb', line 168

def start(app_dir)
  @log.info("running start stage: #{@start_script}")
  run_script(@start_script, app_dir)
end

#stop(app_dir) ⇒ Object



173
174
175
176
# File 'lib/flintlock/module.rb', line 173

def stop(app_dir)
  @log.info("running stop stage: #{@stop_script}")
  run_script(@stop_script, app_dir)
end

#valid?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/flintlock/module.rb', line 144

def valid?
  @metadata.valid?
end