diff --git a/.gitignore b/.gitignore index 03d575e20..69fa79db1 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ yarn-debug.log* .yarn-integrity public/packs .idea +.vagrant diff --git a/Procfile b/Procfile index f9ade14dc..c68ea8b9a 100644 --- a/Procfile +++ b/Procfile @@ -1,4 +1,4 @@ -unicorn: bin/rails server -p 3000 +unicorn: bin/rails server -p 9000 jobs: bin/rake jobs:work recommender: bundle exec ruby script/mock_services/recommender.rb iqdbs: bundle exec ruby script/mock_services/iqdbs.rb diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 000000000..5eb64b8f6 --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,25 @@ +Vagrant.configure('2') do |config| + config.hostmanager.enabled = true + config.hostmanager.manage_host = true + + config.vm.box = 'debian/stretch64' + config.vm.box_version = '9.4.0' + + config.vm.provider 'virtualbox' do |v| + v.cpus = 2 + v.memory = 1280 + end + + VAGRANT_COMMAND = ARGV[0] + config.ssh.username = 'danbooru' if VAGRANT_COMMAND == 'ssh' + + config.vm.define 'default' do |node| + node.vm.hostname = 'e621.lc' + node.vm.network :private_network, ip: '192.168.64.78' + end + + config.vm.synced_folder '.', '/vagrant', type: 'nfs' + + config.vm.provision 'shell', path: 'vagrant/install.sh' +end + diff --git a/vagrant/danbooru.service b/vagrant/danbooru.service new file mode 100644 index 000000000..fb2dfda81 --- /dev/null +++ b/vagrant/danbooru.service @@ -0,0 +1,12 @@ +[Unit] +Description=Danbooru +After=network.target + +[Service] +Type=simple +User=danbooru +WorkingDirectory=/home/danbooru/danbooru +ExecStart=/bin/bash -c 'source /etc/profile.d/chruby.sh;/home/danbooru/danbooru/bin/bundle exec foreman start' + +[Install] +WantedBy=vagrant.mount diff --git a/vagrant/install.sh b/vagrant/install.sh new file mode 100644 index 000000000..0f6483687 --- /dev/null +++ b/vagrant/install.sh @@ -0,0 +1,162 @@ +#!/usr/bin/env bash + +APP_DIR=/home/danbooru/danbooru +CHRUBY_PATH=/etc/profile.d/chruby.sh + +echo "debconf debconf/frontend select noninteractive" | sudo debconf-set-selections +sed -i -e 's/\(AcceptEnv LANG LC_\*\)/#\1/' /etc/ssh/sshd_config +service sshd restart + +package_installed() { + if dpkg-query -f '${binary:Package}\n' -W | grep "$1" &>/dev/null; then + return 0; + else + return 1; + fi +} + +add_key() { + wget -qO - "$1" | sudo apt-key add - &>/dev/null +} + +install_packages() { + sudo apt-get install -y $@ +} + +script_log() { + echo "[install.sh] >>> $@" +} + +if ! grep danbooru /etc/passwd >/dev/null; then + script_log "Creating danbooru system user..." + useradd -m -s /bin/bash -U danbooru + cp -pr /home/vagrant/.ssh /home/danbooru/ + chown -R danbooru:danbooru /home/danbooru + echo "%danbooru ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/danbooru + ln -s /vagrant /home/danbooru/danbooru + usermod -aG vagrant,www-data danbooru +fi + +if ! package_installed postgresql-9.6; then + add_key https://www.postgresql.org/media/keys/ACCC4CF8.asc + echo "deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main" > /etc/apt/sources.list.d/pgdg.list + script_log "PostgreSQL repository added" +fi + +if ! package_installed nginx; then + add_key http://nginx.org/keys/nginx_signing.key + echo "deb http://nginx.org/packages/debian/ stretch nginx" > /etc/apt/sources.list.d/nginx.list + script_log "nginx repository added" +fi + +if ! package_installed nodejs; then + install_packages curl + curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - >/dev/null 2>&1 + script_log "Node.js repository added" +fi + +if ! package_installed yarn; then + add_key https://dl.yarnpkg.com/debian/pubkey.gpg + echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list + script_log "yarn repository added" +fi + +echo "deb http://http.debian.net/debian stretch-backports main" | sudo tee /etc/apt/sources.list.d/stretch-backports.list +sudo apt-get update + +if ! package_installed nginx; then + script_log "Installing nginx..." + install_packages nginx +fi + +if ! package_installed postgresql-9.6; then + script_log "Installing PostgreSQL..." + install_packages postgresql-11 + sed -i -e 's/md5/trust/' /etc/postgresql/11/main/pg_hba.conf + service postgresql restart +fi + +if ! package_installed yarn; then + script_log "Installing yarn..." + install_packages yarn +fi + +if ! install_packages \ + build-essential automake libxml2-dev libxslt-dev ncurses-dev \ + libreadline-dev flex bison ragel memcached libmemcached-dev git curl \ + libcurl4-openssl-dev sendmail-bin sendmail nginx ssh coreutils ffmpeg \ + mkvtoolnix cmake ffmpeg gifsicle git inkscape libcurl4-openssl-dev \ + libicu-dev libjpeg-progs libopencv-dev libpq-dev libreadline-dev \ + libxml2-dev nodejs optipng redis-server libvips-tools; then + >&2 script_log "Installation of other dependencies failed, please see the errors above and re-run \`vagrant provision\`" + exit 1 +fi + +script_log "Creating danbooru Postgres user..." +sudo -u postgres createuser -s danbooru + +if ! type ruby-install >/dev/null 2>&1; then + script_log "Installing ruby-install..." + cd /usr/local/src + wget -qO ruby-install-0.7.0.tar.gz https://github.com/postmodern/ruby-install/archive/v0.7.0.tar.gz + tar -xzvf ruby-install-0.7.0.tar.gz >/dev/null + cd ruby-install-0.7.0/ + sudo make install >/dev/null + rm /usr/local/src/ruby-install-0.7.0.tar.gz + + echo "export RAILS_ENV=development" > /etc/profile.d/rails_env.sh +fi + +if [ -f "$CHRUBY_PATH" ]; then + source $CHRUBY_PATH +fi + +if ! type chruby >/dev/null 2>&1; then + script_log "Installing chruby..." + cd /usr/local/src + wget -qO chruby-0.3.9.tar.gz https://github.com/postmodern/chruby/archive/v0.3.9.tar.gz + tar -xzvf chruby-0.3.9.tar.gz >/dev/null + cd chruby-0.3.9/ + sudo make install >/dev/null + sudo ./scripts/setup.sh >/dev/null + rm /usr/local/src/chruby-0.3.9.tar.gz + + echo -e \ +"if [ -n \"\$BASH_VERSION\" ] || [ -n \"\$ZSH_VERSION\" ]; then + source /usr/local/share/chruby/chruby.sh + source /usr/local/share/chruby/auto.sh +fi" > $CHRUBY_PATH +fi + +script_log "Linking libvips library..." +ln -s /usr/lib/x86_64-linux-gnu/libvips.so.42 /usr/lib/libvips.so + +SETUP_SCRIPT=/vagrant/vagrant/ruby-setup.sh +chmod a+x $SETUP_SCRIPT +sudo -u danbooru bash -c "$SETUP_SCRIPT '$APP_DIR' '$CHRUBY_PATH'" + + +NGINX_CONFIG_PATH=/etc/nginx/conf.d/danbooru.conf +NGINX_DEFAULT_CONFIG_PATH=/etc/nginx/conf.d/default.conf +script_log "Linking nginx config file..." +if [ -f "$NGINX_CONFIG_PATH" ]; then + rm "$NGINX_CONFIG_PATH" +fi +sudo ln -s $APP_DIR/script/install/nginx.danbooru.conf "$NGINX_CONFIG_PATH" +sed -i -e 's/__hostname__/e621.lc/' "$NGINX_CONFIG_PATH" +if [ -f "$NGINX_DEFAULT_CONFIG_PATH" ]; then + rm "$NGINX_DEFAULT_CONFIG_PATH" +fi + +service nginx restart + +script_log "Stopping danbooru systemd service..." +service danbooru stop 2>/dev/null + +script_log "Copying systemd unit file..." +cp $APP_DIR/vagrant/danbooru.service /lib/systemd/system/ +systemctl daemon-reload +systemctl enable danbooru 2>/dev/null + +script_log "Restarting danbooru systemd service..." +service danbooru restart diff --git a/vagrant/ruby-setup.sh b/vagrant/ruby-setup.sh new file mode 100755 index 000000000..26199e15d --- /dev/null +++ b/vagrant/ruby-setup.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +script_log(){ + echo -e "[setup.sh] >>> $@" +} + +APP_DIR=$1 +CHRUBY_PATH=$2 + +source $CHRUBY_PATH + +RUBY_VER_NUM=$(cat $APP_DIR/.ruby-version) +RUBY_VER="ruby-$RUBY_VER_NUM" + +cd $APP_DIR + +if ! command -v ruby >/dev/null || ruby -v | grep -v "$RUBY_VER_NUM" >/dev/null 2>&1; then + echo "Downloading, compiling and installing $RUBY_VER... (this will take a while)" + ruby-install $RUBY_VER + source $CHRUBY_PATH + chruby $RUBY_VER +fi +if ! command -v ruby >/dev/null; then + >&2 script_log "**** The install most likely worked, but you will need to run \`vagrant provision\` to finish the setup. ****" + exit 1 +else + script_log "Installed ruby version: $(ruby -v)" +fi + +script_log "Installing bundler gem..." +gem install bundler:2.0.1 >/dev/null +bundler config github.https true + +script_log "Dropping existing databases (if any)..." +dropdb danbooru2 +dropdb danbooru2_test + +script_log "Creating config files..." +sed -s "s/url: <%= .* %>/host: localhost/g" script/install/database.yml.templ > config/database.yml +cp script/install/danbooru_local_config.rb.templ config/danbooru_local_config.rb +mkdir ~/.danbooru/ +openssl rand -hex 32 > ~/.danbooru/secret_token +openssl rand -hex 32 > ~/.danbooru/session_secret_key +chmod 600 ~/.danbooru/* + +# Remove test_parser +perl -0777 -i -pe "s/CREATE FUNCTION public.testprs.*?RETURNS .*?\n LANGUAGE c STRICT\n AS.*?;//gs" db/structure.sql +perl -0777 -i -pe "s/CREATE TEXT SEARCH PARSER.*?;//gs" db/structure.sql +perl -0777 -i -pe "s/CREATE TEXT SEARCH CONFIGURATION.*?;//gs" db/structure.sql +perl -0777 -i -pe "s/ALTER TEXT SEARCH CONFIGURATION.*?;//gs" db/structure.sql + +script_log "Running yarn..." +yarn install + +script_log "Running setup..." +./bin/setup +exit