Module: SNAP

Included in:
PRM::Repo
Defined in:
lib/prm/repo.rb

Instance Method Summary collapse

Instance Method Details

#snapshot_to(path, component, release, snapname, type, recent) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/prm/repo.rb', line 187

def snapshot_to(path,component,release,snapname,type,recent)
    if type != "deb"
        puts "Only deb supported"
        return
    end

    release.each do |r| 
        time = Time.new
        now = time.strftime("%Y-%m-%d-%H-%M")
        new_snap = "#{snapname}-#{now}"


        component.each do |c|
            if !File.exists?("#{path}/dists/#{r}/#{c}")
                puts "Component doesn't exist! To snapshot you need to have an existing component\n"
                return
            end
        end

        if File.exists?("#{path}/dists/#{r}/#{snapname}") && !File.symlink?("#{path}/dists/#{r}/#{snapname}") 
            puts "Snapshot target is a filesystem, remove it or rename your snap target"
            return
        end

        unless File.exists?("#{path}/dists/#{r}/#{new_snap}/")
            Dir.mkdir("#{path}/dists/#{r}/#{new_snap}")
        end

        if recent
            component.each do |c|
                arch_ar = arch.split(",")
                arch_ar.each do |a|
                    source_dir = "#{path}/dists/#{r}/#{c}/binary-#{a}"
                    target_dir = "#{path}/dists/#{r}/#{new_snap}/binary-#{a}"
                    pfiles = Dir.glob("#{source_dir}/*").sort_by { |f| File.mtime(f) }

                    package_hash = Hash.new
                    pfiles.each do |p|
                        file = p.split(/[_]/)
                        mtime = File.mtime(p)
                        date_in_mil = mtime.to_f
                        if File.directory?(p)
                            next
                        elsif !package_hash.has_key?(file[0])
                            package_hash[file[0]] = { "name" => p, "time" => date_in_mil }
                        else
                            if date_in_mil > package_hash[file[0]]["time"]
                                package_hash[file[0]] = { "name" => p, "time" => date_in_mil }
                            end
                        end
                    end

                    if !File.exists?(target_dir)
                        FileUtils.mkdir_p(target_dir)
                    end

                    package_hash.each do |key,value|
                        value["name"].each do |k|
                            target_file = k.split("/").last
                            FileUtils.cp(k, "#{target_dir}/#{target_file}")
                        end
                    end

                end
            end
        else
            FileUtils.cp_r(Dir["#{path}/dists/#{r}/#{component}/*"], "#{path}/dists/#{r}/#{new_snap}")
        end

        if File.exists?("#{path}/dists/#{r}/#{snapname}")
            FileUtils.rm("#{path}/dists/#{r}/#{snapname}")
        end

        FileUtils.ln_s "#{new_snap}", "#{path}/dists/#{r}/#{snapname}", :force => true
        puts "Created #{snapname} snapshot of #{component}\n"
    end
end