Class: Simp::Metadata::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/simp/metadata/source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Source

Returns a new instance of Source.



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
# 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

Instance Attribute Details

#basenameObject

Returns the value of attribute basename.



11
12
13
# File 'lib/simp/metadata/source.rb', line 11

def basename
  @basename
end

#cachepathObject

Returns the value of attribute cachepath.



8
9
10
# File 'lib/simp/metadata/source.rb', line 8

def cachepath
  @cachepath
end

#componentsObject

Returns the value of attribute components.



9
10
11
# File 'lib/simp/metadata/source.rb', line 9

def components
  @components
end

#dataObject

Returns the value of attribute data.



12
13
14
# File 'lib/simp/metadata/source.rb', line 12

def data
  @data
end

#dirty=(value) ⇒ Object (writeonly)

Sets the attribute dirty

Parameters:

  • the value to set the attribute dirty to.



111
112
113
# File 'lib/simp/metadata/source.rb', line 111

def dirty=(value)
  @dirty = value
end

#editionObject

Returns the value of attribute edition.



16
17
18
# File 'lib/simp/metadata/source.rb', line 16

def edition
  @edition
end

#engineObject

Returns the value of attribute engine.



17
18
19
# File 'lib/simp/metadata/source.rb', line 17

def engine
  @engine
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/simp/metadata/source.rb', line 15

def name
  @name
end

#releasesObject

Returns the value of attribute releases.



10
11
12
# File 'lib/simp/metadata/source.rb', line 10

def releases
  @releases
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/simp/metadata/source.rb', line 7

def url
  @url
end

#write_urlObject

Returns the value of attribute write_url.



70
71
72
# File 'lib/simp/metadata/source.rb', line 70

def write_url
  @write_url
end

Instance Method Details

#cleanupObject



187
188
189
190
191
# File 'lib/simp/metadata/source.rb', line 187

def cleanup
  @cleanup.each do |path|
    FileUtils.rmtree(path)
  end
end

#create_release(destination, source = 'master') ⇒ Object



96
97
98
99
100
101
102
103
104
105
# 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) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# 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) ⇒ Object



89
90
91
92
93
94
# 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?Boolean

Returns:



107
108
109
# File 'lib/simp/metadata/source.rb', line 107

def dirty?
  @dirty
end

#load_from_dir(path) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
# 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) ⇒ Object



81
82
83
84
85
86
87
# 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') ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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_sObject



62
63
64
# File 'lib/simp/metadata/source.rb', line 62

def to_s
  name
end

#writable?Boolean

Returns:



66
67
68
# File 'lib/simp/metadata/source.rb', line 66

def writable?
  true
end