Class: ASpaceEnvironment

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/bootstrap.rb

Class Method Summary collapse

Class Method Details

.demo_db?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'backend/app/lib/bootstrap.rb', line 69

def self.demo_db?
  AppConfig[:db_url] =~ /aspacedemo=true/
end

.download_demo_dbObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'backend/app/lib/bootstrap.rb', line 73

def self.download_demo_db
  if File.exist?(File.join(Dir.tmpdir, 'data'))
    puts "Data directory already exists at #{File.join(Dir.tmpdir, 'data')}."
    AppConfig[:data_directory] = File.join(Dir.tmpdir, 'data')
    return
  end

  zip_file = File.join( Dir.tmpdir, "archivesspace_demo_data.zip")
  File.open( zip_file, 'wb' ) do |file|
    puts "Attempting to download data from #{AppConfig[:demo_data_url]}"
    open(AppConfig[:demo_data_url], 'rb') do |zip|
      file.write(zip.read)
    end
  end

  if File.exist?(zip_file)
    puts "Extracting data to #{Dir.tmpdir} directory"
    Zip::File.open(zip_file) do |zf|
      zf.each do |entry|
        entry.extract(File.join(Dir.tmpdir, entry.name))
      end
    end
    AppConfig[:data_directory] = File.join(Dir.tmpdir, 'data')
  else
    puts <<~EOF
      
      ************************************************************************
      *
      *   WARNING: Unable to download demo data. Using database defined in config
      *
      ************************************************************************
    EOF
  end
end

.environmentObject



43
44
45
# File 'backend/app/lib/bootstrap.rb', line 43

def self.environment
  @environment
end

.init(environment = :auto) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'backend/app/lib/bootstrap.rb', line 48

def self.init(environment = :auto)
  return if @environment      # Already initialised

  if environment != :auto
    @environment = environment
  elsif ENV["ASPACE_DEMO"] == 'true'
    # to use this mechanism, put URL to demo database in AppConfig[:demo_data_url]
    download_demo_db
    @environment = :production
  else
    if ENV["ASPACE_INTEGRATION"] == "true"
      @environment = :integration
    else
      @environment = :production
    end
  end

  prepare_database
end

.prepare_databaseObject



108
109
110
111
112
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
# File 'backend/app/lib/bootstrap.rb', line 108

def self.prepare_database
    if @environment == :integration && demo_db?
      # For integration, use an in-memory database instead.
      AppConfig[:db_url] = "jdbc:derby:memory:integrationdb;create=true;aspacedemo=true"
    end

    if @environment != :unit_test
      FileUtils.mkdir_p(AppConfig[:data_directory])

      if demo_db?
        # Try to discourage Derby from locking whole tables.
        java.lang.System.set_property("derby.locks.escalationThreshold", "2147483647")

        Sequel.connect(AppConfig[:db_url]) do |db|
          puts "Running database migrations for demo database"
          DBMigrator.setup_database(db)
          puts "All done."
        end

        puts <<~EOF
          
          ************************************************************************
          ***
          *** WARNING: Running against the demo database, which is not intended
          *** for production use.
          ***
          *** Please see the README.md file for instructions on configuring MySQL.
          ***
          ************************************************************************
          
        EOF
      end
    end
  end

end