Skip to content

Commit

Permalink
Merge pull request #207 from lemberg/feature/206-override-sync-direct…
Browse files Browse the repository at this point in the history
…ories

Allow overriding sync directories source/destination
  • Loading branch information
T2L committed Aug 6, 2020
2 parents 1671113 + 23ed593 commit 88946c5
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Draft Environment (unreleased)

Updates:

- [GH-206](https://github.com/lemberg/draft-environment/issues/206) - Allow overriding source/destination directory. Configuration setting `vagrant.base_directory` has been replaced with `vagrant.destination_directory`. Added new configuration setting `vagrant.source_directory` (defaults to `.`).

## Draft Environment 3.0.1, 2020-06-05

Fixes:
Expand Down
6 changes: 3 additions & 3 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
# Allow creation of symlinks in VirtualBox shared folders (works with both
# VirtualBox shared folders and NFS).
v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/" + configuration.get("vagrant.base_directory"), "1"]
v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/" + configuration.get("vagrant.destination_directory"), "1"]
# Enable multiple cores in Vagrant/VirtualBox.
v.customize ['modifyvm', :id, '--ioapic', 'on']
# Disable Audio.
Expand Down Expand Up @@ -198,7 +198,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# See https://docs.vagrantup.com/v2/synced-folders/nfs.html

# Configure synched folders.
config.vm.synced_folder ".", configuration.get("vagrant.base_directory"), configuration.get("vagrant.synced_folder_options")
config.vm.synced_folder configuration.get("vagrant.source_directory"), configuration.get("vagrant.destination_directory"), configuration.get("vagrant.synced_folder_options")

# Provisioning
#
Expand All @@ -219,7 +219,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.provision "ansible_local" do |ansible|
ansible.become = true
ansible.playbook = "playbook.yml"
ansible.provisioning_path = File.join(configuration.get("vagrant.base_directory"), vm_pathname.relative_path_from(project_pathname), "/provisioning")
ansible.provisioning_path = File.join("/vagrant", vm_pathname.relative_path_from(project_pathname), "/provisioning")
ansible.extra_vars = configuration.getConfiguration()
ansible.galaxy_role_file = "requirements.yml"
ansible.galaxy_roles_path = "/etc/ansible/roles"
Expand Down
8 changes: 5 additions & 3 deletions default.vm-settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ vagrant:
hostname: ''
# Host aliases. Will be added to hosts file alongside <hostname>.test.
host_aliases: []
# Base directory. No trailing slash!
base_directory: /var/www/draft
# Source directory. No trailing slash!
source_directory: '.'
# Destination directory. No trailing slash!
destination_directory: /var/www/draft
# Synced folder options. Refer to https://www.vagrantup.com/docs/synced-folders/
# for possible options and their values. Please note that each synced fodler
# type could have extra options, look at NFS or RSync for examples.
Expand Down Expand Up @@ -101,7 +103,7 @@ draft_features:

# Role: draft (internal) @ self

ssh_default_directory: '{{ vagrant.base_directory }}'
ssh_default_directory: '{{ vagrant.destination_directory }}'

# Role: oefenweb.swapfile @ v2.0.26

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Base directory and web server document root
# Sync directories and web server document root

Draft environment allows setting of base project directory (in guest OS) and web server document root.
Draft environment allows setting of the source (in the host OS) and the destination (in the guest OS) directories, and web server document root.

## How to

Base project directory can be set by overriding `vagrant.base_directory` variable (defaults to `/var/www/draft`). See [default.vm-settings.yml](/default.vm-settings.yml#L28).
Source directory can be set by overriding `vagrant.source_directory` variable (defaults to `.`). See [default.vm-settings.yml](/default.vm-settings.yml#L28).

Web server document root can be specified by overriding `apache2_document_root` variable (defaults to `docroot`). See [default.vm-settings.yml](/default.vm-settings.yml#L125).
Destination directory can be set by overriding `vagrant.destination_directory` variable (defaults to `/var/www/draft`). See [default.vm-settings.yml](/default.vm-settings.yml#L30).

SSH default directory can be set by overriding `ssh_default_directory` variable (defaults to value of `vagrant.base_directory` variable, which is `/var/www/draft` by default :) ). See [default.vm-settings.yml](/default.vm-settings.yml#L104).
Web server document root can be specified by overriding `apache2_document_root` variable (defaults to `docroot`). See [default.vm-settings.yml](/default.vm-settings.yml#L127).

SSH default directory can be set by overriding `ssh_default_directory` variable (defaults to value of `vagrant.destination_directory` variable, which is `/var/www/draft` by default :) ). See [default.vm-settings.yml](/default.vm-settings.yml#L104).

## Synced folder options

Expand Down
2 changes: 1 addition & 1 deletion provisioning/roles/apache2/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
state: directory
mode: 0755
with_items:
- '{{ vagrant.base_directory }}/{{ apache2_document_root }}'
- '{{ vagrant.destination_directory }}/{{ apache2_document_root }}'

# Configure web server.
- name: Set correct DirectoryIndex
Expand Down
6 changes: 3 additions & 3 deletions provisioning/roles/apache2/templates/draft.conf.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# {{ ansible_managed }}

<Directory {{ vagrant.base_directory }}/{{ apache2_document_root }}>
<Directory {{ vagrant.destination_directory }}/{{ apache2_document_root }}>
AllowOverride All
Options -Indexes +FollowSymLinks
Require all granted
Expand All @@ -12,7 +12,7 @@
ServerAlias {{ vagrant.host_aliases|join(" ") }}
{% endif %}

DocumentRoot {{ vagrant.base_directory }}/{{ apache2_document_root }}
DocumentRoot {{ vagrant.destination_directory }}/{{ apache2_document_root }}

CustomLog "/var/log/draft/apache_access.log" common
ErrorLog /var/log/draft/apache_error.log
Expand All @@ -26,7 +26,7 @@
ServerAlias {{ vagrant.host_aliases|join(" ") }}
{% endif %}

DocumentRoot {{ vagrant.base_directory }}/{{ apache2_document_root }}
DocumentRoot {{ vagrant.destination_directory }}/{{ apache2_document_root }}

CustomLog "/var/log/draft/apache_access.log" common
ErrorLog /var/log/draft/apache_error.log
Expand Down
2 changes: 1 addition & 1 deletion provisioning/roles/draft/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---

ssh_default_directory: "{{ vagrant.base_directory }}"
ssh_default_directory: "{{ vagrant.destination_directory }}"
6 changes: 3 additions & 3 deletions provisioning/roles/draft/templates/vendor_path.sh.j2
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# {{ ansible_managed }}

# Allow executable app dependencies from Composer to be run from the path.
if [ -n {{ vagrant.base_directory }} -a -f {{ vagrant.base_directory }}/composer.json ] ; then
bin=$(composer config bin-dir --working-dir={{ vagrant.base_directory }} --no-interaction 2>/dev/null)
if [ -n {{ vagrant.destination_directory }} -a -f {{ vagrant.destination_directory }}/composer.json ] ; then
bin=$(composer config bin-dir --working-dir={{ vagrant.destination_directory }} --no-interaction 2>/dev/null)
if [ -n "$bin" ] ; then
export PATH="{{ vagrant.base_directory }}/${bin}:${PATH}"
export PATH="{{ vagrant.destination_directory }}/${bin}:${PATH}"
fi
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Lemberg\Draft\Environment\Config\Update\Step;

use Lemberg\Draft\Environment\Config\Update\UpdateStepInterface;

/**
* Replace base directory with destination directory.
*/
final class ReplaceBaseDirectoryWithDestinationDirectory extends AbstractUpdateStep implements UpdateStepInterface {

/**
* {@inheritdoc}
*/
public function getWeight(): int {
return 4;
}

/**
* {@inheritdoc}
*/
public function update(array &$config): void {
$config['vagrant']['source_directory'] = '.';
$config['vagrant']['destination_directory'] = $config['vagrant']['base_directory'] ?? '/var/www/draft';
unset($config['vagrant']['base_directory']);

// Replace the default SSH directory setting as well.
if (array_key_exists('ssh_default_directory', $config)) {
$config['ssh_default_directory'] = str_replace('base_directory', 'destination_directory', $config['ssh_default_directory']);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final protected function assertComposerLockContainsPackageExtra(): void {
$key = array_search(App::PACKAGE_NAME, array_column($decoded_composer_lock['packages-dev'], 'name'), TRUE);

self::assertTrue($decoded_composer_lock['packages-dev'][$key]['extra']['draft-environment']['already-installed']);
self::assertSame(3, $decoded_composer_lock['packages-dev'][$key]['extra']['draft-environment']['last-update-weight']);
self::assertSame(4, $decoded_composer_lock['packages-dev'][$key]['extra']['draft-environment']['last-update-weight']);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Config/Manager/UpdateManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function testGetAndSetLastAppliedUpdateWeight(): void {
* Tests the last available update weight getter.
*/
public function testGetLastAvailableUpdateWeight(): void {
self::assertSame(3, $this->configUpdateManager->getLastAvailableUpdateWeight());
self::assertSame(4, $this->configUpdateManager->getLastAvailableUpdateWeight());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace Lemberg\Tests\Unit\Draft\Environment\Config\Update\Step;

use Composer\Composer;
use Composer\Config as ComposerConfig;
use Composer\IO\IOInterface;
use Lemberg\Draft\Environment\Config\Config;
use Lemberg\Draft\Environment\Config\Manager\UpdateManager;
use Lemberg\Draft\Environment\Config\Update\Step\ReplaceBaseDirectoryWithDestinationDirectory;
use Lemberg\Draft\Environment\Utility\Filesystem;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

/**
* Tests replacing base directory with destination directory update step.
*
* @covers \Lemberg\Draft\Environment\Config\Update\Step\AbstractUpdateStep
* @covers \Lemberg\Draft\Environment\Config\Update\Step\ReplaceBaseDirectoryWithDestinationDirectory
*/
final class ReplaceBaseDirectoryWithDestinationDirectoryTest extends TestCase {

/**
* @var \Composer\Composer
*/
private $composer;

/**
* @var \Composer\IO\IOInterface
*/
private $io;

/**
* @var string
*/
private $root;

/**
* @var \Lemberg\Draft\Environment\Config\Manager\UpdateManagerInterface
*/
private $configUpdateManager;

/**
* {@inheritdoc}
*/
protected function setUp(): void {
$this->composer = new Composer();
$this->composer->setConfig(new ComposerConfig());
$this->io = $this->createMock(IOInterface::class);

// Mock source and target configuration directories.
$this->root = vfsStream::setup()->url();
$fs = new Filesystem();
$fs->mkdir(["$this->root/source", "$this->root/target"]);

$configObject = new Config("$this->root/source", "$this->root/target");
$this->configUpdateManager = new UpdateManager($this->composer, $this->io, $configObject);
}

/**
* Tests step weight getter.
*/
final public function testGetWeight(): void {
$step = new ReplaceBaseDirectoryWithDestinationDirectory($this->composer, $this->io, $this->configUpdateManager);
self::assertSame(4, $step->getWeight());
}

/**
* Tests update step execution.
*
* @param array<string,mixed> $config
* @param array<string,mixed> $expectedConfig
*
* @dataProvider updateDataProvider
*/
final public function testUpdate(array $config, array $expectedConfig): void {
$step = new ReplaceBaseDirectoryWithDestinationDirectory($this->composer, $this->io, $this->configUpdateManager);

$step->update($config);
self::assertSame($config, $expectedConfig);
}

/**
* Data provider for the ::testUpdate().
*
* @return array<int,array<int,string|array<string,mixed>>>
*/
final public function updateDataProvider(): array {
return [
[
[
'vagrant' => [
'base_directory' => '/var/www/draft',
],
'ssh_default_directory' => '{{ vagrant.base_directory }}',
],
[
'vagrant' => [
'source_directory' => '.',
'destination_directory' => '/var/www/draft',
],
'ssh_default_directory' => '{{ vagrant.destination_directory }}',
],
],
];
}

}

0 comments on commit 88946c5

Please sign in to comment.