From ca6105a91913a0fe7d3014c5f8d75fc8d7d9fb58 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Sun, 14 May 2017 19:29:58 +0300 Subject: [PATCH 1/3] Move initial setup from seeds.rb to bootstrap rake task #515 --- README.md | 1 + db/seeds.rb | 34 ---------------------------------- lib/tasks/bootstrap.rake | 11 +++++++++++ 3 files changed, 12 insertions(+), 34 deletions(-) create mode 100644 lib/tasks/bootstrap.rake diff --git a/README.md b/README.md index 8b7d5db78..a8a85a3d8 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ Manual demo install and database setup: cp config/application-example.yml config/application.yml # and edit it cp config/database-example.yml config/database.yml # and edit it bundle exec rake db:setup:all # for production, please follow deployment howto + bundle exec rake:bootstrap bundle exec rake assets:precompile ### Apache with patched mod_epp (Debian 7/Ubuntu 14.04 LTS) diff --git a/db/seeds.rb b/db/seeds.rb index 441aa9442..5dde23342 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,36 +1,2 @@ # This file should contain all the record creation needed to seed the database with its default values. # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). - -ActiveRecord::Base.transaction do - registrar = Registrar.create!( - name: 'Test', - reg_no: '1234', - street: 'test', - city: 'test', - state: 'test', - zip: '1234', - email: 'test@domain.tld', - country_code: 'US', - code: 'US1' - ) - - registrar.accounts.create!(account_type: Account::CASH, currency: 'EUR') - - ApiUser.create!( - username: 'test', - password: 'testtest', - identity_code: '51001091072', - active: true, - registrar: registrar, - roles: ['super'] - ) - - AdminUser.create!( - username: 'test', - email: 'test@domain.tld', - password: 'testtest', - password_confirmation: 'testtest', - country_code: 'US', - roles: ['admin'] - ) -end diff --git a/lib/tasks/bootstrap.rake b/lib/tasks/bootstrap.rake new file mode 100644 index 000000000..8bfff5ffc --- /dev/null +++ b/lib/tasks/bootstrap.rake @@ -0,0 +1,11 @@ +desc 'Bootstraps production-like environment' +task :bootstrap do + AdminUser.create!( + username: 'demo', + email: 'demo@domain.tld', + password: 'demodemo', + password_confirmation: 'demodemo', + country_code: 'US', + roles: ['admin'] + ) +end From c1a29ea7b52989b59caf0d47d91fc718337a81e8 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 15 May 2017 01:43:20 +0300 Subject: [PATCH 2/3] Add dev:prime rake task #515 --- lib/tasks/dev.rake | 129 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 lib/tasks/dev.rake diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake new file mode 100644 index 000000000..0820550bd --- /dev/null +++ b/lib/tasks/dev.rake @@ -0,0 +1,129 @@ +namespace :dev do + desc 'Generates dummy data in development environment' \ + ' (options: [random] for random data generation - slowest)' + + task :prime, [:random] => :environment do |t, args| + abort 'Production environment is not supported' if Rails.env.production? + + require 'factory_girl' + include FactoryGirl::Syntax::Methods + FactoryGirl.find_definitions + + PaperTrail.enabled = false + with_random_data = args[:random].present? + + def generate_default_data + create(:admin_user, username: 'test', password: 'testtest', password_confirmation: 'testtest') + + zone = create(:zone, origin: 'test') + registrar = create(:registrar, name: 'test') + registrant = create(:registrant, name: 'test', registrar: registrar) + + create(:account, registrar: registrar, balance: 1_000_000) + create(:api_user, username: 'test', password: 'testtest', registrar: registrar) + create(:domain, + name: 'test.test', + period: 1, + period_unit: 'y', + registered_at: Time.zone.now, + valid_from: Time.zone.now, + expire_time: Time.zone.now + 10.years, + registrar: registrar, + registrant: registrant) + + Billing::Price.durations.each do |duration| + Billing::Price.operation_categories.each do |operation_category| + create(:price, + price: Money.from_amount(1), + valid_from: Time.zone.now.beginning_of_day, + valid_to: Time.zone.now + 10.years, + duration: duration, + operation_category: operation_category, + zone: zone) + end + end + end + + def generate_random_data + zone_count = 10 + admin_user_count = 5 + registrar_count = 50 + api_user_count = 10 + registrant_count = 50 + domain_count = 50 + registrars = [] + registrants = [] + zones = [] + registrant_names = [ + 'John Doe', + 'John Roe', + 'Jane Doe', + 'Jane Roe', + 'John Smith', + ] + + zone_count.times do + origin = SecureRandom.hex[0..(rand(5) + 1)] + zones << create(:zone, origin: origin) + end + + zone_origins = zones.collect { |zone| zone.origin } + + admin_user_count.times do + uid = SecureRandom.hex[0..(rand(5) + 1)] + create(:admin_user, username: "test#{uid}", password: 'testtest', password_confirmation: 'testtest') + end + + registrar_count.times do + uid = SecureRandom.hex[0..(rand(5) + 1)] + registrars << create(:registrar, name: "Acme Ltd. #{uid}") + end + + registrars.each do |registrar| + create(:account, registrar: registrar, balance: rand(99999)) + + api_user_count.times do |i| + create(:api_user, username: "test#{registrar.id}#{i}", password: 'testtest', registrar: registrar) + end + + registrant_count.times do |i| + registrants << create(:registrant, name: registrant_names.sample, registrar: registrar) + end + + domain_count.times do |i| + name = "test#{registrar.id}#{i}#{rand(99999)}.#{zone_origins.sample}" + period = rand(3) + 1 + + create(:domain, + name: name, + period: period, + period_unit: 'y', + registered_at: Time.zone.now, + valid_from: Time.zone.now, + expire_time: Time.zone.now + period.years, + registrar: registrar, + registrant: registrants.sample) + end + end + + zones.each do |zone| + Billing::Price.durations.each do |duration| + Billing::Price.operation_categories.each do |operation_category| + create(:price, + price: Money.from_amount(rand(10) + 1), + valid_from: Time.zone.now.beginning_of_day, + valid_to: Time.zone.now + (rand(10) + 1).years, + duration: duration, + operation_category: operation_category, + zone: zone) + end + end + end + end + + ActiveRecord::Base.transaction do + generate_default_data + generate_random_data if with_random_data + end + end +end From 48ae54d507d2c068f84d453f32948a3b9b4661d2 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 15 May 2017 11:48:16 +0300 Subject: [PATCH 3/3] Improve rake:bootstrap task #515 --- lib/tasks/bootstrap.rake | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/tasks/bootstrap.rake b/lib/tasks/bootstrap.rake index 8bfff5ffc..36202a6c7 100644 --- a/lib/tasks/bootstrap.rake +++ b/lib/tasks/bootstrap.rake @@ -1,10 +1,10 @@ desc 'Bootstraps production-like environment' task :bootstrap do AdminUser.create!( - username: 'demo', - email: 'demo@domain.tld', - password: 'demodemo', - password_confirmation: 'demodemo', + username: 'admin', + email: 'admin@domain.tld', + password: 'adminadmin', + password_confirmation: 'adminadmin', country_code: 'US', roles: ['admin'] )