Skip to content

Instantly share code, notes, and snippets.

@matyapiro31
Created September 2, 2017 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matyapiro31/f67dff4aafdb2947b611669ceebf7938 to your computer and use it in GitHub Desktop.
Save matyapiro31/f67dff4aafdb2947b611669ceebf7938 to your computer and use it in GitHub Desktop.
class MastodonSetUpGenerator < Rails::Generators::Base
desc "Set up Mastodon at once."
class_option :redis_host, type: :string, default: "localhost", desc: "Path for redis server"
class_option :redis_port, type: :numeric, default: 6379, desc: "Port for redis server"
class_option :redis_password, type: :string, default: "" , desc: "redis password"
class_option :redis_db, type: :numeric, default: 0, desc: "redis db path"
class_option :redis_url, type: :string, aliases: "-R", desc: "Set redis options like redis://password@host:port/path"
class_option :db_host, type: :string, default: "/var/run/postgresql", desc: "ip address or Unix domain socket path for PostgreSQL"
class_option :db_user, type: :string, default: "mastodon", desc: "PostgreSQL user"
class_option :db_pass, type: :string, default: "", desc: "PostgreSQL password"
class_option :db_name, type: :string, default: "mastodon_production", desc: "PostgreSQL database name"
class_option :db_port, type: :numeric, default: 5432, desc: "PostgresQL port"
class_option :database_url, type: :string, aliases: "-Q", desc: "Set Postgresql options like postgresql://user:password@host/path"
class_option :local_domain, type: :string, aliases: "-d", required: true, desc: "Set local domain for mastodon"
class_option :local_https, type: :boolean, default: true
class_option :web_domain, type: :string, desc: "DO NOT USE THIS UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING"
class_option :alternate_domains, type: :array, desc: "An array for alternative domains"
class_option :single_user_mode, type: :boolean, default: false, desc: "Single user mode will disable registrations and redirect frontpage to the first profile"
class_option :email_domain_blacklist, type: :array, desc: "Prevent registrations with following e-mail domains"
class_option :email_domain_whitelist, type: :array, desc: "Only allow registrations with the following e-mail domains"
class_option :default_locale, type: :string,aliases: "-l", desc: "Optionally change default language"
class_option :smtp_server, type: :string, desc: "E-mail configuration"
class_option :smtp_port, type: :numeric, default: 587
class_option :smtp_login, type: :string, desc: "Username for e-mail"
class_option :smtp_password, type: :string, desc: "Password for e-mail"
class_option :smtp_from_address, type: :string, desc: "e-mail address for notification message,default is notification@local_domain"
class_option :smtp_domain, type: :string, desc: "Do same as local_domain"
class_option :smtp_delivery_method, type: :string, default: "smtp", desc: "smtp or sendmail"
class_option :smtp_auth_method, type: :string, default: "plain"
class_option :smpt_ca_file, type: :string, default: "/etc/ssl/certs/ca-certificates.crt"
class_option :smtp_openssl_verify_mode, type: :string, default: "peer"
class_option :smtp_enable_starttls_auto, type: :boolean, default: true
class_option :smtp_tls, type: :boolean, default: true
class_option :smtp_url, type: :string, aliases: "-S", desc: "Set required SMTP configuration at once like smtp://user:pass@server:port"
class_option :paperclip_root_path, type: :string, desc: "Optional user upload path"
class_option :paperclip_root_url, type: :string, desc: "Optional user upload URL"
class_option :certbot, type: :boolean, aliases: "--letsencrypt", default: true, desc: "If use Let's Encrypt for certificate."
class_option :update_certificate, type: :numeric, aliases: "-u", default: 7773333, desc: "Run Let's Encrypt certificate file update par seconds, specify 0 if update manually"
class_option :ssl_certificate, type: :string, aliases: "--cert", desc: "Specify ssl certificate file when you use --no-certbot"
class_option :ssl_privatekey, type: :string, aliases: "--key", desc: "Specify ssl private key file when you use --no-certbot"
class_option :port, type: :hash, aliases: "-P", default: {web: 3000,streaming: 4000}, desc: "mastodon proxy ports"
class_option :node_version, type: :numeric, aliases: "-J", default: 6, desc: "Node.js version to be installed"
class_option :redis, type: :boolean, default: true, desc: "If install redis or not, please specify --redis-url or change configuration later"
class_option :postgresql, type: :boolean, default: true, desc: "If install PostgreSQL or not, please specify --database-url or change configuration later"
class_option :nginx, type: :boolean, default: true, desc: "If install Nginx or not"
class_option :apache, type: :boolean, default: false, desc: "Use Apache Httpd instead of Nginx"
class_option :pgbouncer, type: :boolean, default: false, desc: "Use PgBouncer for PostgreSQL"
class_option :git, type: :boolean, default: true, desc: "Use git for installation"
class_option :create_home, type: :boolean, default: true, desc: "Create home dir or not"
class_option :install_dir, type: :string, default: "/var/mastodon", desc: "Select directory to install"
class_option :version, type: :string, default: "latest", desc: "Version to install"
class_option :ruby_version, type: :string, default: "2.4.1", desc: "Version to install"
class_option :use_ramdisk, type: :boolean, default: false, desc: "Use /dev/shm for mastodon"
class_option :theme, type: :string, default: "dark", desc: "Mastodon theme color: dark or light"
source_root File.expand_path('../templates', __FILE__)
def SetUpPackage
if RbConfig::CONFIG['host_os']!="linux-gnu"
return 1
end #if
if File.exists?('/etc/lsb-release') || File.exists?('/etc/debian_version')
p `sudo apt update&&sudo apt install -y curl sudo`
unless (options[:node_version]==4 ||options[:node_version]==5 ||options[:node_version]==6 ||options[:node_version]==7 ||options[:node_version]==8)
STDERR.puts "Incorrect version number is set."
return 1
end #unless
unless (options[:nginx] || options[:apache])
STDERR.puts "None of Web servers is selected.Please add configuratio by yourself."
end #unless
unless (options[:redis] || !options[:redis_url].empty?)
STDERR.puts "Redis is not set.Please specify \"--redis\" or \"--redis_url=redis://password@host:port/path\"."
return 1
end #unless
unless (options[:postgresql] || !options[:database_url].empty?)
STDERR.puts "PostgreSQL is not set.Please specify \"--postgresql\" or \"--database_url=postgresql://user:password@host:port/path\"."
return 1
end #unless
p `curl -sL https://deb.nodesource.com/setup_#{options[:node_version]}.x|sudo -E bash -`
p <<` EOS`
sudo apt install -y curl
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg |sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" |sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && \
sudo apt install-y imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file libprotobuf-dev protobuf-compiler \
pkg-config nodejs autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev libncurses5-dev libffi-dev \
libgdbm-dev openssl certbot yarn libidn11-dev libicu-dev rbenv
sudo apt clean
EOS
if options[:nginx]
p `sudo apt install -y nginx`
else if options[:apache]
p `sudo apt install -y apache2`
end #elif
end #if
ENV.store("version",options[:version])
ENV.store("ruby_version",options[:ruby_version])
ENV.store("install_dir",options[:install_dir])
if options[:git]
p <<` EOS`
sudo apt install -y git
cd /tmp
git clone https://github.com/rbenv/ruby-build.git ruby-build
git clone https://github.com/tootsuite/mastodon.git live
cd ./live
if [ "$version" == "latest" ]
then
version=$(git tag -l | sort -V | tail -n 1)
fi
git checkout $version
EOS
else
p <<` EOS`
sudo apt install -y unzip
cd /tmp
curl -s https://codeload.github.com/rbenv/ruby-build/zip/master -o ruby-build.zip
unzip -q ruby-build.zip -d ruby-build
rm ruby-build.zip
if [ $version == "latest" ]
then
version=$(curl -s https://github.com/tootsuite/mastodon/releases/latest|grep -o "v[0-9]\.[0-9]\.[0-9][a-z0-9]*")
fi
curl -s https://codeload.github.com/tootsuite/mastodon/zip/${version} -o live.zip
unzip -q live.zip -d live
rm live.zip
EOS
end #if
if options[:create_home]
p <<` EOS`
sudo adduser --group mastodon
sudo -u mastodon mkdir -p /home/mastodon/.rbenv/plugins
sudo -u mastodon cp -r /tmp/ruby-build /home/mastodon/.rbenv/plugins/
sudo -u mastodon rbenv install $ruby_version
sudo -u mastodon rbenv global $ruby_version
echo 'eval "$(rbenv init -)"'|sudo -u mastodon tee -a /home/mastodon/.bashrc
EOS
else
p <<` EOS`
sudo adduser --system --no-create-home --group mastodon
sudo mkdir -p /var/rbenv/plugins
sudo cp -r /tmp/ruby-build /var/rbenv/plugins/
export RBENV_ROOT=/var/rbenv
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"
sudo rbenv install $ruby_version
sudo rbenv global $ruby_version
echo export RBENV_ROOT=/var/rbenv|sudo tee -a /etc/profile
echo export PATH=\"\$RBENV_ROOT/bin:\$PATH\"|sudo tee -a /etc/profile
echo eval \"\$(rbenv init -)\"|sudo tee sa /etc/profile
#"
EOS
end #if
#install mastodon
p <<` EOS`
mkdir -p "$install_dir"&&cp -r /tmp/live/* "$install_dir"
gem install bundler
cd $install_dir
bundle install --deployment --without development test
yarn install --pure-lockfile
EOS
end #if Ubuntu or Debian
end #SetUpPackage
end #class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment