Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Splitting VVV code in Vagrantfile into classes #2641

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ jobs:
uses: StephaneBour/actions-php-lint@7.3
with:
dir: '.'
- name: Checkout Repository
uses: actions/checkout@v3
- name: Install Ruby version specified in `.ruby-version`
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- name: Rubocop
run: gem install rubocop && rubocop -c .rubocop.yml .vvv/
25 changes: 25 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
AllCops:
NewCops: enable
Exclude:
- 'Vagrantfile'

Layout/LineLength:
Max: 80

Metrics/ClassLength:
Max: 128
CountComments: false

Metrics/MethodLength:
Max: 20
CountComments: false

Metrics/AbcSize:
Enabled: false

Style/Documentation:
Exclude:
- '.vvv/lib/config.rb'
- '.vvv/lib/info.rb'
- '.vvv/lib/migrate.rb'
- '.vvv/lib/splash_screens.rb'
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.7.6
23 changes: 23 additions & 0 deletions .vvv/assets/bear_warning_sudo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
┌-──────────────────────────────────────────────────────────────────────────────┐
│ │
│ ⚠ DANGER DO NOT USE SUDO ⚠ │
│ │
│ ! ▄▀▀▀▄▄▄▄▄▄▄▀▀▀▄ ! You should never use sudo or root with vagrant. │
│ !█▒▒░░░░░░░░░▒▒█ It causes lots of problems :( │
│ █░░█░▄▄░░█░░█ ! │
│ █░░█░░█░▄▄█ ! We're really sorry but you may need to do painful │
│ ! ▀▄░█░░██░░█ cleanup commands to fix this. │
│ │
│ If vagrant does not work for you without sudo, open a GitHub issue instead │
│ In the future, this warning will halt provisioning to prevent new users │
│ making this mistake. │
│ │
│ ⚠ DANGER SUDO DETECTED! │
│ │
│ In the future the VVV team will be making it harder to use VVV with sudo. │
│ We will require a config option so that users can do data recovery, and │
│ disable sites and the dashboard. │
│ │
│ DO NOT USE SUDO, use ctrl+c/cmd+c and cancel this command ASAP!!! │
│ │
└───────────────────────────────────────────────────────────────────────────────┘
29 changes: 29 additions & 0 deletions .vvv/lib/bootstrap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module VVV
# The Bootstrap class
#
# Used for determining which and if logos and other messages should be
# displayed before the `Vagrant.configure` block in the `Vagrantfile`.
class Bootstrap
# Determine if the VVV logo and platform splash should be displayed
def self.show_logo?
return false if ENV['VVV_SKIP_LOGO']

return true if %w[up resume status provision reload].include? ARGV[0]

false
end

# Determine if the sudo warning should be displayed
def self.show_sudo_bear?
return true if !Vagrant::Util::Platform.windows? && Process.uid.zero?

false
end

def self.box_overridden?(config)
config['vm_config'].key?('box')
end
end
end
160 changes: 160 additions & 0 deletions .vvv/lib/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# frozen_string_literal: true

module VVV
class Config
GITHUB_USER_URL = 'https://github.com/Varying-Vagrant-Vagrants/'
UTILITIES_REPO_URL = "#{GITHUB_USER_URL}vvv-utilities.git"
DASHBOARD_REPO_URL = "#{GITHUB_USER_URL}dashboard.git"
PRIVATE_NETWORK_IP = '192.168.56.4'

CONFIG_FILE = File.join(VVV::Info.vagrant_dir, 'config/config.yml').freeze

require 'yaml'

def initialize
@config = YAML.load_file(CONFIG_FILE)
lint
end

def values
@config
end

def lint
# If 'hosts' isn't an array, redefine it as such
@config['hosts'] = ['vvv.test'] unless @config['hosts'].is_a? Array

@config['sites'].each do |site, args|
# If the site's value is a string, treat it as the repo value
if @config['sites'][site].is_a? String
@config['sites'][site] = { repo: args }
end

# If the site's args aren't defined as a Hash already,
# redefine it as such.
@config['sites'][site] = {} unless @config['sites'][site].is_a? Hash

merge_site_defaults(site)
process_hosts_for_site(site)
end
process_dashboard
process_utilities
process_utility_sources
process_extensions
process_extension_sources
Comment on lines +41 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are the wrong way around

Suggested change
process_utilities
process_utility_sources
process_extensions
process_extension_sources
process_extension_sources
process_utility_sources
process_extensions
process_utilities

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though it is just linting so this isn't as important as it looked when I read it

process_vm_config
process_general
process_vagrant_plugins
set_vagrant_default_provider_env_variable
end

private

def set_vagrant_default_provider_env_variable
return unless @config['vm_config']['provider']

ENV['VAGRANT_DEFAULT_PROVIDER'] = @config['vm_config']['provider']
end

def process_vagrant_plugins
@config['vagrant-plugins'] = {} unless @config['vagrant-plugins']
end

def process_utilities
@config['utilities'] = {} unless @config['utilities'].is_a? Hash
end

def process_utility_sources
return if @config['utility-sources'].is_a? Hash

@config['utility-sources'] = {}
end

def process_extensions
@config['extensions'] = {} unless @config['extensions'].is_a? Hash
end

def process_extension_sources
if @config['extension-sources'].is_a? Hash
@config['extension-sources'].each do |name, args|
next unless args.is_a? String

@config['extension-sources'][name] = { 'repo' => args,
'branch' => 'master' }
end
unless @config['extension-sources'].key?('core')
@config['extension-sources']['core'] = { 'repo' => UTILITIES_REPO_URL,
'branch' => 'master' }
end
else
@config['extension-sources'] = {}
end
end

def process_vm_config
@config['vm_config'] = {} unless @config['vm_config'].is_a? Hash
merge_vm_config_defaults
end

def merge_vm_config_defaults
defaults = { 'memory' => 2048, 'cores' => 1, 'provider' => 'virtualbox',
'private_network_ip' => PRIVATE_NETWORK_IP }
defaults['provider'] = 'parallels' if Etc.uname[:version].include? 'ARM64'
@config['vm_config'] = defaults.merge(@config['vm_config'])
end

def process_general
@config['general'] = {} unless @config['general'].is_a? Hash
end

def process_dashboard
@config['dashboard'] = {} unless @config['dashboard'].is_a? Hash
merge_dashboard_defaults
end

def merge_dashboard_defaults
defaults = { 'repo' => DASHBOARD_REPO_URL, 'branch' => 'master' }
@config['dashboard'] = defaults.merge(@config['dashboard'])
end

def process_hosts_for_site(site)
unless @config['sites'][site]['skip_provisioning']
# Find vvv-hosts files and add their lines to the @config hash
site_host_paths = Dir.glob(
"#{@config['sites'][site]['local_dir']}/*/vvv-hosts"
)
site_host_paths.each do |path|
lines = File.readlines(path).map(&:chomp).grep(/\A[^#]/)
lines.each do |l|
@config['sites'][site]['hosts'] << l
end
end

# Add the site's hosts to the 'global' hosts array
@config['hosts'] += if @config['sites'][site]['hosts'].is_a? Array
@config['sites'][site]['hosts']
else
["#{site}.test"]
end
@config['sites'][site].delete('hosts')
end
@config['hosts'] = @config['hosts'].uniq
end

def merge_site_defaults(site)
# Merge the defaults with the currently defined site args
@config['sites'][site] = site_defaults(site).merge(@config['sites'][site])
end

def site_defaults(site)
{ 'repo' => false,
'vm_dir' => "/srv/www/#{site}",
'local_dir' => File.join(VVV::Info.vagrant_dir, 'www', site),
'branch' => 'master',
'skip_provisioning' => false,
'allow_customfile' => false,
'nginx_upstream' => 'php',
'hosts' => [] }
end
end
end
Loading