Vagrantfile 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. ENV["PORT"] ||= "3000"
  4. $provisionA = <<SCRIPT
  5. # Add the yarn repo + yarn repo keys
  6. curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
  7. sudo apt-add-repository 'deb https://dl.yarnpkg.com/debian/ stable main'
  8. # Add repo for NodeJS
  9. sudo mkdir -p /etc/apt/keyrings
  10. curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
  11. NODE_MAJOR=20
  12. echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
  13. sudo apt-get update
  14. # Add firewall rule to redirect 80 to PORT and save
  15. sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port #{ENV["PORT"]}
  16. echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo debconf-set-selections
  17. echo iptables-persistent iptables-persistent/autosave_v6 boolean true | sudo debconf-set-selections
  18. sudo apt-get install iptables-persistent -y
  19. # Add packages to build and run Mastodon
  20. sudo apt-get install \
  21. git-core \
  22. g++ \
  23. libpq-dev \
  24. libxml2-dev \
  25. libxslt1-dev \
  26. imagemagick \
  27. nodejs \
  28. redis-server \
  29. redis-tools \
  30. postgresql \
  31. postgresql-contrib \
  32. libicu-dev \
  33. libidn11-dev \
  34. libreadline6-dev \
  35. autoconf \
  36. bison \
  37. build-essential \
  38. ffmpeg \
  39. file \
  40. gcc \
  41. libffi-dev \
  42. libgdbm-dev \
  43. libjemalloc-dev \
  44. libncurses5-dev \
  45. libprotobuf-dev \
  46. libssl-dev \
  47. libyaml-dev \
  48. pkg-config \
  49. protobuf-compiler \
  50. zlib1g-dev \
  51. -y
  52. # Install rvm
  53. sudo apt-add-repository -y ppa:rael-gc/rvm
  54. sudo apt-get install rvm -y
  55. sudo usermod -a -G rvm $USER
  56. SCRIPT
  57. $provisionElasticsearch = <<SCRIPT
  58. # Install Elastic Search
  59. sudo apt install openjdk-17-jre-headless -y
  60. sudo wget -O /usr/share/keyrings/elasticsearch.asc https://artifacts.elastic.co/GPG-KEY-elasticsearch
  61. sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/elasticsearch.asc] https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'
  62. sudo apt update
  63. sudo apt install elasticsearch -y
  64. sudo systemctl daemon-reload
  65. sudo systemctl enable --now elasticsearch
  66. echo 'path.data: /var/lib/elasticsearch
  67. path.logs: /var/log/elasticsearch
  68. network.host: 0.0.0.0
  69. http.port: 9200
  70. discovery.seed_hosts: ["localhost"]
  71. cluster.initial_master_nodes: ["node-1"]
  72. xpack.security.enabled: false' > /etc/elasticsearch/elasticsearch.yml
  73. sudo systemctl restart elasticsearch
  74. # Install Kibana
  75. sudo apt install kibana -y
  76. sudo systemctl enable --now kibana
  77. echo 'server.host: "0.0.0.0"
  78. elasticsearch.hosts: ["http://localhost:9200"]' > /etc/kibana/kibana.yml
  79. sudo systemctl restart kibana
  80. SCRIPT
  81. $provisionB = <<SCRIPT
  82. source "/etc/profile.d/rvm.sh"
  83. # Install Ruby
  84. read RUBY_VERSION < /vagrant/.ruby-version
  85. rvm install ruby-$RUBY_VERSION --disable-binary
  86. # Configure database
  87. sudo -u postgres createuser -U postgres vagrant -s
  88. sudo -u postgres createdb -U postgres mastodon_development
  89. cd /vagrant # This is where the host folder/repo is mounted
  90. # Install gems
  91. gem install bundler foreman
  92. bundle install
  93. # Install node modules
  94. sudo corepack enable
  95. corepack prepare
  96. yarn install
  97. # Build Mastodon
  98. export RAILS_ENV=development
  99. export $(cat ".env.vagrant" | xargs)
  100. bundle exec rails db:setup
  101. # Configure automatic loading of environment variable
  102. echo 'export RAILS_ENV=development' >> ~/.bash_profile
  103. echo 'export $(cat "/vagrant/.env.vagrant" | xargs)' >> ~/.bash_profile
  104. SCRIPT
  105. VAGRANTFILE_API_VERSION = "2"
  106. Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  107. config.vm.box = "ubuntu/focal64"
  108. config.vm.provider :virtualbox do |vb|
  109. vb.name = "mastodon"
  110. vb.customize ["modifyvm", :id, "--memory", "8192"]
  111. vb.customize ["modifyvm", :id, "--cpus", "3"]
  112. # Disable VirtualBox DNS proxy to skip long-delay IPv6 resolutions.
  113. # https://github.com/mitchellh/vagrant/issues/1172
  114. vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"]
  115. vb.customize ["modifyvm", :id, "--natdnshostresolver1", "off"]
  116. # Use "virtio" network interfaces for better performance.
  117. vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
  118. vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
  119. end
  120. # This uses the vagrant-hostsupdater plugin, and lets you
  121. # access the development site at http://mastodon.local.
  122. # If you change it, also change it in .env.vagrant before provisioning
  123. # the vagrant server to update the development build.
  124. #
  125. # To install:
  126. # $ vagrant plugin install vagrant-hostsupdater
  127. config.vm.hostname = "mastodon.local"
  128. if defined?(VagrantPlugins::HostsUpdater)
  129. config.vm.network :private_network, ip: "192.168.42.42", nictype: "virtio"
  130. config.hostsupdater.remove_on_suspend = false
  131. end
  132. if config.vm.networks.any? { |type, options| type == :private_network }
  133. config.vm.synced_folder ".", "/vagrant", type: "nfs", mount_options: ['rw', 'actimeo=1']
  134. else
  135. config.vm.synced_folder ".", "/vagrant"
  136. end
  137. # Otherwise, you can access the site at http://localhost:3000 and http://localhost:4000 , http://localhost:8080
  138. config.vm.network :forwarded_port, guest: 3000, host: 3000
  139. config.vm.network :forwarded_port, guest: 4000, host: 4000
  140. config.vm.network :forwarded_port, guest: 8080, host: 8080
  141. config.vm.network :forwarded_port, guest: 9200, host: 9200
  142. config.vm.network :forwarded_port, guest: 9300, host: 9300
  143. config.vm.network :forwarded_port, guest: 9243, host: 9243
  144. config.vm.network :forwarded_port, guest: 5601, host: 5601
  145. # Full provisioning script, only runs on first 'vagrant up' or with 'vagrant provision'
  146. config.vm.provision :shell, inline: $provisionA, privileged: false, reset: true
  147. # Run with elevated privileges for Elasticsearch installation
  148. config.vm.provision :shell, inline: $provisionElasticsearch, privileged: true
  149. config.vm.provision :shell, inline: $provisionB, privileged: false
  150. config.vm.post_up_message = <<MESSAGE
  151. To start server
  152. $ vagrant ssh -c "cd /vagrant && foreman start"
  153. MESSAGE
  154. end