Skip to content

Commit

Permalink
ability to download and import data sets via seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
orangewolf committed Jul 16, 2021
1 parent f0f05fc commit 569595d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
24 changes: 24 additions & 0 deletions bin/export_from_fedora
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env ruby

# ./bin/export_from_fedora NAME SOURCE_URL
# This exports straight from Fedora in the format expected for import_from_fedora
# It is meant to copy FCREPO from one Samvera app to another
# NAME should be the name of the account
# SOURCE_URL should be a full fedora repo path such as
# http://fcrepo:8080/fcrepo/rest/ea85bcf6-b590-4d40-9541-d15134cb2694


def run(cmd)
puts cmd
system(cmd, out: $stdout, err: $stderr)
end

name = ARGV[0]
source_url = ARGV[1]
basename = File.basename(source_url)

cmd = "java -jar fcrepo-import-export-0.3.0.jar --mode export --resource #{source_url} --dir ./hyrax-webapp/tmp/export --binaries"
run cmd

cmd = "tar zcfv data/#{name}~#{basename}.tgz tmp/export"
run cmd
10 changes: 10 additions & 0 deletions bin/git-cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env ruby
%x{git fetch -ap}
branches = %x{git branch --merged main}.split("\n").collect {|b| b.gsub('*', '').strip}

branches -= ['main']

puts branches.inspect
branches.each do |branch|
puts %x{git branch -d #{branch}}
end
33 changes: 33 additions & 0 deletions bin/import_from_fedora
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env ruby

# ./bin/import_from_fedora FILE_PATH DESTINATION_URL
# This imports straight in to Fedora and then performes a complete reindex.
# It is meant to copy FCREPO from one Samvera app to another
# FILE_PATH = the path to a properly formated tgz. Currently files in data must
# extract to tmp/export and must be of sourced from fcrepo:8080/fcrepo/rest/FILENAME
# DESINTATION_URL should be a full fedora repo path such as
# http://fcrepo:8080/rest/ea85bcf6-b590-4d40-9541-d15134cb2694


def run(cmd)
puts cmd
system(cmd, out: $stdout, err: $stderr)
end


file = ARGV[0]
source_name, source_basename = File.basename(file).delete('.tgz').split("~")
destination_url = ARGV[1]
source_url = "http://fcrepo:8080/fcrepo/rest/#{source_basename}"

cmd = "wget https://github.com/fcrepo-exts/fcrepo-import-export/releases/download/fcrepo-import-export-0.3.0/fcrepo-import-export-0.3.0.jar"
run cmd

cmd = "tar zxfv #{file}"
run cmd

cmd = "java -jar fcrepo-import-export-0.3.0.jar --mode import --resource #{destination_url} --dir ./tmp/export --binaries --map #{source_url},#{destination_url} --legacyMode"
run cmd

cmd = %Q{bundle exec rails runner 'Apartment::Tenant.switch!(Account.find_by(name: "#{name}").tenant); ActiveFedora::Base.reindex_everything'}
run cmd
Empty file added data/.keep
Empty file.
10 changes: 10 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,13 @@
end
u.add_role(:superadmin)
end

if ENV['SEED_EXTRA_DATA']
if Account.count.zero?
Dir.glob(Rails.root.join('data/*')).each do |file|
name = File.basename(file).split("~").first
a = CreateAccount.new(Account.new(name: name), [u]).save
%x{bin/import_from_fedora #{file} #{File.join(ENV['FEDORA_URL'], a.fcrepo_endpoint.base_path)}}
end
end
end

0 comments on commit 569595d

Please sign in to comment.