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

POC #4

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
VVV
/.vagrant
/log
/www
/git
/src/coretech
/src/wp-themes
/src/wp-core/wordpress
/src/wp-core/wp-tests
/config/.ssh
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vvv"]
path = vvv
url = [email protected]:Varying-Vagrant-Vagrants/VVV.git
133 changes: 133 additions & 0 deletions Customfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
pmc_vvv=File.realdirpath( '..' );

require File.join( pmc_vvv,'lib','functions.rb' )

# custom mapping for pmc specific improvement
config.vm.synced_folder File.join(pmc_vvv, 'config/.ssh'), '/srv/.ssh', owner: 'vagrant', group: 'root', mount_options: ['dmode=775', 'fmode=600'], automount: true, SharedFoldersEnableSymlinksCreate: true
config.vm.synced_folder File.join(pmc_vvv, 'src'), '/srv/src', owner: 'vagrant', group: 'www-data', mount_options: ['dmode=775', 'fmode=774'], automount: true, SharedFoldersEnableSymlinksCreate: true
config.vm.synced_folder pmc_vvv, '/srv/pmc-vvv', owner: 'vagrant', group: 'www-data', mount_options: ['dmode=775', 'fmode=774'], automount: true, SharedFoldersEnableSymlinksCreate: true

# we want to override some default from VVV to settings we know work properly from pmc-qs7
config.vm.provider :virtualbox do |v|

# rename the VM since we are customized the setting to PMC specificiation
v.name = "pmc-vvv_" + (Digest::SHA256.hexdigest pmc_vvv)[0..10]

# Allow the VM to utilize up to 1/2 of the host cpu cores
v.cpus = detect_max_cpus 2, 0.5

# Allow the VM to utilize up to 3/4 of the host system memory
v.memory = detect_max_mem 4096, 0.75

# Only allow the VM to use up-to 50% of the host system cpu
# Windows OS, the server may hang if cpu allow to reach 100%
v.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]

# Instruct Vagrant to utilize all the cpu cores we've provided above
# Without this, only 1 core will be used
v.customize ["modifyvm", :id, "--ioapic", "on"]

# Plug in the virtual network adapter as if it were a real cat-5 cable
# plugged into a real network card. This corrects an error presented with
# Ubuntu 16 where boot hangs on: "A start job is running for Raise network interfaces"
# This error occurs because Ubuntu tries to raise all the network interfaces,
# but the 'cable' isn't connected, so it waits until the timeout.
# Note, this is the same as selecting 'Cable Connect' for the network adapter
# in the VirtualBox app settings for this box.
# https://github.com/hashicorp/vagrant/issues/8056#issuecomment-267600935
v.customize ["modifyvm", :id, "--cableconnected1", "on"]

# Turn off un-use devices
v.customize ["modifyvm", :id, "--usb", "off"]
v.customize ["modifyvm", :id, "--usbehci", "off"]
v.customize ["modifyvm", :id, "--usbxhci", "off"]
v.customize ["modifyvm", :id, "--accelerate3d", "off"]
v.customize ["modifyvm", :id, "--accelerate2dvideo", "off"]
v.customize ["modifyvm", :id, "--audio", "none"]
v.customize ["modifyvm", :id, "--vrde", "off"]

end

# In order to add files to /etc folder, we need to access the privileged script to do the copy
# First we need to add the source to a temporarly folder
config.vm.provision "pmc-file",
run: 'always',
type: "file",
source: File.join(pmc_vvv, 'config/etc/.'),
destination: "/tmp/etc/"
# We then execute a shell script inside VM with admin privilege to copy the files
config.vm.provision "pmc-script",
run: 'always',
after: "pmc-file",
type: "shell",
privileged: true,
inline: <<-SHELL
rsync -r /tmp/etc/ /etc/
rm -rf /tmp/pmc-etc
mkdir -p /vagrant
mkdir -p /vagrant/failed_provisioners
cp /srv/.ssh/config /root/.ssh/config
cp /srv/.ssh/config /home/vagrant/.ssh/config
cp /srv/config/config.yml /vagrant/config.yml
chmod 600 /root/.ssh/config /home/vagrant/.ssh/config
chown root:root /root/.ssh/config
chown vagrant:vagrant /home/vagrant/.ssh/config
SHELL

# Implementing PMC specific provision for each site that can't be done by common provision template
$vvv_config['sites'].each do |site, args|
next if args['skip_provisioning']

# In order to properly develop and test provision script directly on local,
# we need to override and map the custom-site-template folder
if Dir.exist?(File.join(pmc_vvv, 'git/custom-site-template'))

# override and disable remote repo since we want use our local template
$vvv_config['sites'][site]['repo'] = false

# map the custom provision script directly into the folder so we can develop the scripts without copying between the folders
config.vm.synced_folder File.join(pmc_vvv, 'git/custom-site-template/provision'), '/srv/www/' + site + '/provision', owner: 'vagrant', group: 'www-data', mount_options: ['dmode=775', 'fmode=774'], automount: true, SharedFoldersEnableSymlinksCreate: true
end

# VIP GO/Classic
if args['custom'] && args['custom']['wp_vip'] && File.exist?(File.join(pmc_vvv, 'provision/provision-bind-mount.sh'))

# We could let's vagrant auto map the shared folder,
# however the VVV site provision script doesn't support remote git repo checkout into non-empty folder

# provision after dependencies doesn't work :(
# let's work some magic to bypass vvv's site provision
$vvv_config['sites'][site]['skip_provisioning'] = true
config.vm.provision "site-#{site}",
type: 'shell',
keep_color: true,
path: File.join('provision', 'provision-site.sh'),
args: [
site,
args['repo'].to_s,
args['branch'],
args['vm_dir'],
args['skip_provisioning'].to_s,
args['nginx_upstream']
],
env: { "VVV_LOG" => "site-#{site}" }

config.vm.provision "bind-mount-#{site}",
after: "site-#{site}",
run: 'always',
type: 'shell',
keep_color: true,
path: File.join(pmc_vvv, 'provision', 'provision-bind-mount.sh'),
args: [
site,
args['vm_dir'],
args['custom']['wp_vip'],
args['custom']['share_code'].to_s
],
env: { "VVV_LOG" => "bind-mount-#{site}" }

end

end


33 changes: 33 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

# -*- mode: ruby -*-
# vi: set ft=ruby ts=2 sw=2 et:
Vagrant.require_version '>= 2.2.4'

# Define constant for vvv folder
VVV='vvv'

require "getoptlong"
require 'fileutils'
require 'open3'
require "readline"
require 'yaml'
require File.join( __dir__,'lib','functions.rb' )

# Auto install all required plugins
install_required_plugins()

FileUtils.mkdir_p("./src/coretech/pmc-plugins") unless Dir.exist?("./src/coretech/pmc-plugins")
FileUtils.mkdir_p("./src/coretech/pmc-core-v2") unless Dir.exist?("./src/coretech/pmc-core-v2")
FileUtils.cp( File.join( 'provision', 'provision-post.sh' ), File.join( 'vvv', 'provision', 'provision-post.sh' ) );

# Copy our config.yml to vvv folder, currently there is no way for us to use a different config file from different location

# development testing for now
FileUtils.cp( 'config-tests.yml', File.join( 'vvv', 'config', 'config.yml' ) );
FileUtils.cp( 'Customfile', File.join( 'vvv', 'Customfile' ) );

ENV['VVV_SKIP_LOGO'] = 'true'
Dir.chdir 'vvv'
exec "vagrant #{ARGV.join(' ')}"
Kernel.exit!(0)
81 changes: 81 additions & 0 deletions config-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
pmc:
share:
skip_provisioning: true
php_version: 7.3
wp_version: 5.4
coretech_dir: /srv/src/coretech
wp_core_dir: /srv/src/wp-core
wp_themes_dir: /srv/src/wp-themes

sites:

wpcom:
skip_provisioning: true
repo: https://github.com/hauvong/custom-site-template.git
hosts:
- wpcom.test
- bgr.wpcom.test
- pmc-corporate-2018.wpcom.test
- pmc-deadline-2019.wpcom.test
- pmc-footwearnews-2018.wpcom.test
- pmc-hollywoodlife-2017.wpcom.test
- pmc-spy.wpcom.test
- pmc-summits.wpcom.test
- pmc-summits-rr1.wpcom.test
- pmc-summits-variety.wpcom.test
- pmc-summits-wwd.wpcom.test
- pmc-summits.wpcom.test
- pmc-tvline.wpcom.test
- pmc-variety-2017.wpcom.test
- pmc-wwd-2016.wpcom.test
- pmc-variety-2020.wpcom.test
custom:
wp_type: single
wp_vip: classic
wp_tests: true
wp_version: 5.4
wp_themes:
vip/plugins:
repo: https://bitbucket.org/penskemediacorp/wordpress-vip-plugins.git

vipgo:
skip_provisioning: false
repo: https://github.com/hauvong/custom-site-template.git
hosts:
- vipgo.test
- rollingstone.vipgo.test
- artnews.vipgo.test
- goldderby.vipgo.test
- sheknows.vipgo.test
- soaps.vipgo.test
custom:
wp_type: single
wp_vip: vipgo
wp_tests: true
wp_version: 5.4
wp_plugins:
repo: [email protected]:penskemediacorp/pmc-vip-go-plugins.git

utilities:
core:
- memcached-admin
# - mongodb
- opcache-status
- php73
- phpmyadmin
# - tideways
- tls-ca
- webgrind

vm_config:
cores: 4
memory: 4096

general:
# db_backup: true
# db_restore: true
db_share_type: false

vagrant-plugins:
disksize: 10GB
12 changes: 12 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
---
pmc:
share:
skip_provisioning: true
php_version: 7.3
wp_version: 5.4
coretech_dir: /srv/src/coretech
wp_core_dir: /srv/src/wp-core
wp_themes_dir: /srv/src/wp-themes

sites:
wpcom:
skip_provisioning: false
Expand All @@ -22,6 +31,8 @@ sites:
- pmc-variety-2020.wpcom.test
custom:
wp_type: subdomain
wp_tests: true


# VIPGO sites:
# @NOTE: name === theme-name && host
Expand Down Expand Up @@ -370,6 +381,7 @@ utilities:
- mongodb
- opcache-status
- php74
- php73
- phpmyadmin
- tideways
- tls-ca
Expand Down
4 changes: 4 additions & 0 deletions config/.ssh/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
IdentityFile /srv/.ssh/bitbucket-id_rsa
IdentityFile /srv/.ssh/github-id_rsa
5 changes: 5 additions & 0 deletions config/etc/environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/srv/config/homebin
XDEBUG_REMOTE_HOST=10.0.2.2
PMC_PHPUNIT_BOOTSTRAP=/srv/src/wp-core/wp-config/bootstrap-phpunit.php
WP_TESTS_DIR=/srv/src/wp-core/wp-tests/phpunit/
WP_CONFIG_BOOTSTRAP=true
77 changes: 77 additions & 0 deletions lib/functions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
$logger = Vagrant::UI::Colored.new

#require File.join( __dir__,'functions-git.rb' )

def install_required_plugins()

# vagrant-persistent-storage
# vagrant-git
# vagrant-docker-compose

required_plugins = %w( rb-readline vagrant-ghost vagrant-vbguest vagrant-hostsupdater vagrant-disksize )
missing_plugins = []
required_plugins.each do |plugin|
missing_plugins.push(plugin) unless Vagrant.has_plugin? plugin
end
if ! missing_plugins.empty?
install_these = missing_plugins.join(' ')
$logger.warn "Required following plugins: #{install_these}."
if system "vagrant plugin install #{install_these}"
exec "vagrant #{ARGV.join(' ')}"
Kernel.exit!(0)
else
$logger.warn "Error install plugins, please install these plugins then restart vagrant:"
$logger.warn " #{install_these}"
Kernel.exit!(0)
end
end

end

# @see https://github.com/psudug/nittany-vagrant/blob/master/Vagrantfile
def detect_max_cpus( default = 1, multiplier = 1 )
host_os = RbConfig::CONFIG['host_os']
cpus = 0

# Give VM 1/4 system memory & access to all cpu cores on the host
if host_os =~ /darwin/
cpus = `sysctl -n hw.ncpu`.to_i
elsif host_os =~ /linux/
cpus = `nproc`.to_i
elsif host_os =~ /mingw32/
cpus = `wmic cpu get NumberOfLogicalProcessors | grep '^[0-9]'`.to_i
end

if cpus <= 0
return default
end

return (cpus * multiplier).round
end

# @see https://github.com/psudug/nittany-vagrant/blob/master/Vagrantfile
def detect_max_mem( default = 1024, multiplier = 1 )
host_os = RbConfig::CONFIG['host_os']
mem = 0

# Give VM 1/4 system memory & access to all cpu cores on the host
if host_os =~ /darwin/
# sysctl returns Bytes and we need to convert to MB
mem = `sysctl -n hw.memsize`.to_i / 1024 / 1024
elsif host_os =~ /linux/
# meminfo shows KB and we need to convert to MB
mem = `grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//'`.to_i / 1024
elsif host_os =~ /mingw32/
mem = `wmic os get TotalVisibleMemorySize | grep '^[0-9]'`.to_i / 1024
if mem < 1024
mem = 1024
end
end

if mem <= 0
return default
end

return (mem * multiplier).round
end

Loading