summaryrefslogtreecommitdiff
path: root/bin/setup
blob: 4e9db444a30a63a78c7f6b30f9e47fff3f21223b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'fileutils'
require 'optparse'
require 'pathname'

def die(message)
  abort "\n== #{message.strip} =="
end

def log(message)
  puts "\n== #{message.strip} =="
end

def prompt(message)
  print "#{message.strip} "
  gets.strip
end

def confirm(message)
  prompt("#{message.strip} [y/N]").casecmp('y') == 0
end

def system!(*args)
  system(*args) || die("Command #{args} failed")
end

APP_ROOT = Pathname.new File.expand_path('..', __dir__)

options = {}
OptionParser.new do |opt|
  opt.banner = 'Usage: bin/setup [-a] [--root URL] [--seed] [--boot]'
  opt.on('-a', 'automated setup (do not prompt for information not given on the command line)') { |o| options[:auto] = o }
  opt.on('--root URL', 'the URL root ("localhost" in development environments, your domain in staging environments)') { |o| options[:root] = o }
  opt.on('--seed', 'Seed the database with sample users, images, comments, and posts') { |o| options[:seed] = o }
  opt.on('--boot', 'Boot the application server and background processing') { |o| options[:boot] = o }
end.parse!

log 'Beginning setup'

Dir.chdir APP_ROOT do
  log 'Installing gem dependencies'
  system! 'bin/bundle', 'install'

  files = []
  files << %w[config/nginx.conf.example config/nginx.local.conf] unless File.exist? 'config/nginx.local.conf'
  files << %w[config/booru/settings.yml.sample config/settings.yml] unless File.exist? 'config/booru/settings.yml'
  files << %w[config/booru/footer.yml.sample config/booru/footer.yml] unless File.exist? 'config/booru/footer.yml'

  unless files.empty?
    log 'Creating local configuration'
    options[:root] ||= prompt 'Enter the URL root (your domain in staging environments, empty/default is "localhost"):' unless options[:auto]
    options[:root] ||= 'localhost'
    files.each { |old, new| File.write new, File.read(old).gsub(/\bderpibooru\.org\b|\b(?:camo\.)?derpicdn\.net\b/, options[:root]) }
  end

  log 'Preparing database'
  options[:seed] ||= confirm 'Seed the database with sample users, images, comments, and posts?' unless options[:auto]
  ENV['DEV_SEED'] = 'y' if options[:seed]
  system! 'bin/rails', 'db:setup', out: '/dev/null'
  system! 'bin/rails', 'db:environment:set', 'RAILS_ENV=development'

  log 'Initializing system flags (Flipper)'
  system! 'bin/rails', 'booru:features:enable_defaults'

  log 'Removing old logs and temporary files'
  system! 'bin/rails', 'log:clear', 'tmp:clear'

  log 'Done'

  options[:boot] ||= confirm 'Boot the application server and background processing?' unless options[:auto]
  system! 'bin/bundle', 'exec', 'foreman', 'start' if options[:boot]
end