class Simp::Metadata::Source

Attributes

basename[RW]
cachepath[RW]
components[RW]
data[RW]
dirty[W]
edition[RW]
engine[RW]
name[RW]
releases[RW]
url[RW]
write_url[R]

Public Class Methods

new(args) click to toggle source
# File lib/simp/metadata/source.rb, line 19
def initialize(args)
  unless args.key?(:engine)
    raise ':engine must be specified when initializing a metadata source'
  end
  unless args.key?(:name)
    raise ':name must be specified when initializing a metadata source'
  end
  unless args.key?(:component)
    raise ':component must be specified when initializing a metadata source'
  end
  unless args.key?(:edition)
    raise ':edition must be specified when initializing a metadata source'
  end

  @engine = args[:engine]
  @name = args[:name]
  @component = args[:component]
  @edition = args[:edition]

  @url = if args[:url]
           args[:url]
         else
           @component.primary.url
         end
  @write_url = @url
  cachepath = args[:cachepath]
  @components = {}
  @releases = {}
  @data = {}
  @cleanup = []

  if cachepath.nil?
    @cachepath = Dir.mktmpdir('cachedir')
    @cleanup << @cachepath
  else
    @cachepath = File.absolute_path(cachepath)
  end
  retval = Simp::Metadata.download_component(@component, 'target' => @cachepath)
  load_from_dir(retval['path'])

  @dirty = false
end

Public Instance Methods

cleanup() click to toggle source
# File lib/simp/metadata/source.rb, line 187
def cleanup
  @cleanup.each do |path|
    FileUtils.rmtree(path)
  end
end
create_release(destination, source = 'master') click to toggle source
# File lib/simp/metadata/source.rb, line 96
def create_release(destination, source = 'master')
  if @releases.key?(destination)
    raise "destination version #{destination} already exists"
  end
  unless @releases.key?(source)
    raise "source version #{source} doesn't exist"
  end
  self.dirty = true
  @releases[destination] = Marshal.load(Marshal.dump(@releases[source]))
end
deep_merge(target_hash, source_hash) click to toggle source
# File lib/simp/metadata/source.rb, line 172
def deep_merge(target_hash, source_hash)
  source_hash.each do |key, value|
    if target_hash.key?(key)
      if value.class == Hash
        deep_merge(target_hash[key], value)
      else
        target_hash[key] = value
      end
    else
      target_hash[key] = value
    end
  end
  target_hash
end
delete_release(version) click to toggle source
# File lib/simp/metadata/source.rb, line 89
def delete_release(version)
  if @releases.key?(version)
    self.dirty = true
    @releases.delete(version)
  end
end
dirty?() click to toggle source
# File lib/simp/metadata/source.rb, line 107
def dirty?
  @dirty
end
load_from_dir(path) click to toggle source
# File lib/simp/metadata/source.rb, line 158
def load_from_dir(path)
  @load_path = path
  Dir.chdir(path) do
    Dir.glob('**/*.yaml') do |filename|
      begin
        hash = YAML.load_file(filename)
        @data = deep_merge(@data, hash)
      end
    end
  end
  @releases = @data['releases'] unless @data['releases'].nil?
  @components = @data['components'] unless @data['components'].nil?
end
release(version) click to toggle source
# File lib/simp/metadata/source.rb, line 81
def release(version)
  if releases.key?(version)
    releases[version]
  else
    {}
  end
end
save(message = 'Auto-saving using simp-metadata') click to toggle source
# File lib/simp/metadata/source.rb, line 113
def save(message = 'Auto-saving using simp-metadata')
  if dirty?
    puts @load_path
    # XXX ToDo: Write files to yaml, commit and push (where appropriate)

    Simp::Metadata.run("cd #{@load_path} && rm -rf v1")
    FileUtils.mkdir_p("#{@load_path}/v1")
    File.open("#{@load_path}/v1/components.yaml", 'w') { |file| file.write({ 'components' => @components }.to_yaml) }
    @releases.each do |releasename, data|
      directory = case releasename
                  when /.*-[Aa][Ll][Pp][Hh][Aa].*/
                    'prereleases'
                  when /.*-[Bb][Ee][Tt][Aa].*/
                    'prereleases'
                  when /.*-[Rr][Cc].*/
                    'prereleases'
                  when /^nightly-/
                    'nightlies'
                  when /develop/
                    'channels'
                  when /development/
                    'channels'
                  when /master/
                    'channels'
                  when /^test-/
                    'tests'
                  else
                    'releases'
                  end
      FileUtils.mkdir_p("#{@load_path}/v1/#{directory}")
      File.open("#{@load_path}/v1/#{directory}/#{releasename}.yaml", 'w') { |file| file.write({ 'releases' => { releasename.to_s => data } }.to_yaml) }
    end
    Simp::Metadata.run("cd #{@load_path} && git remote add upstream #{write_url}")
    Simp::Metadata.run("cd #{@load_path} && git remote set-url upstream #{write_url}")
    exitcode = Simp::Metadata.run("cd #{@load_path} && git add -A && git commit -m '#{message}'; git push upstream master")
    if exitcode != 0
      Simp::Metadata.critical('error committing changes')
      raise exitcode.to_s
    else
      puts "Successfully updated #{name}"
    end
    self.dirty = false
  end
end
to_s() click to toggle source
# File lib/simp/metadata/source.rb, line 62
def to_s
  name
end
writable?() click to toggle source
# File lib/simp/metadata/source.rb, line 66
def writable?
  true
end
write_url=(value) click to toggle source
# File lib/simp/metadata/source.rb, line 72
def write_url=(value)
  if value != @url
    @write_url = value
    FileUtils.rm_r("#{@cachepath}/#{@component.name}")
    retval = Simp::Metadata.download_component(@component, 'target' => @cachepath, 'url' => value)
    load_from_dir(retval['path'])
  end
end