Skip to content

Commit

Permalink
#356 Migrating old shared directory structure to new
Browse files Browse the repository at this point in the history
  • Loading branch information
REBELinBLUE committed Oct 1, 2017
1 parent 9f59727 commit 8e36ed2
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 7 deletions.
27 changes: 23 additions & 4 deletions app/Jobs/DeployProject/ScriptBuilder.php
Expand Up @@ -192,11 +192,12 @@ private function getScriptForStep(array $tokens = [])
case Command::DO_INSTALL:
$release_path = $tokens['release_path'];
$shared_path = $tokens['shared_path'];
$project_path = $tokens['project_path'];

// Write configuration file to release dir, symlink shared files and run composer
return $this->process->setScript('deploy.steps.InstallComposerDependencies', $tokens)
->prependScript($this->configurationFileCommands($release_path))
->appendScript($this->shareFileCommands($release_path, $shared_path));
->appendScript($this->shareFileCommands($release_path, $shared_path, $project_path));
case Command::DO_ACTIVATE:
return $this->process->setScript('deploy.steps.ActivateNewRelease', $tokens);
case Command::DO_PURGE:
Expand Down Expand Up @@ -240,19 +241,30 @@ private function configurationFileCommands($release_dir)
*
* @param string $release_dir
* @param string $shared_dir
* @param string $project_dir
*
* @return string
*/
private function shareFileCommands($release_dir, $shared_dir)
private function shareFileCommands($release_dir, $shared_dir, $project_dir)
{
/** @var Collection $files */
$files = $this->deployment->project->sharedFiles;
if (!$files->count()) {
return '';
}

$script = '';
$files->each(function (SharedFile $shared) use (&$script, $release_dir, $shared_dir) {
$migration = '.deployer-migrated';
$backup_dir = $shared_dir . '.backup';

$script = $this->parser->parseFile('deploy.MigrateShared', [
'deployment' => $this->deployment->id,
'shared_dir' => $shared_dir,
'project_dir' => $project_dir,
'backup_dir' => $backup_dir,
'migration' => $migration,
]);

$files->each(function (SharedFile $shared) use (&$script, $release_dir, $shared_dir, $backup_dir) {
$pathinfo = pathinfo($shared->file);
$template = 'File';

Expand All @@ -267,12 +279,19 @@ private function shareFileCommands($release_dir, $shared_dir)
'deployment' => $this->deployment->id,
'shared_dir' => $shared_dir,
'release_dir' => $release_dir,
'backup_dir' => $backup_dir,
'path' => $file,
'filename' => $pathinfo['basename'],
'parent_dir' => ltrim($pathinfo['dirname'], '/'),
]);
});

$script .= $this->parser->parseFile('deploy.MigrateSharedTimestamp', [
'migration' => $migration,
'shared_dir' => $shared_dir,
'release' => $this->deployment->release_id,
]);

return PHP_EOL . $script;
}
}
12 changes: 12 additions & 0 deletions resources/scripts/deploy/MigrateShared.sh
@@ -0,0 +1,12 @@
### Migrate shared directories/files - {{ deployment }}
SHARED_NEEDS_MIGRATING=false
if [ ! -f {{ shared_dir }}/{{ migration }} ]; then
echo ""
echo "Shared directory needs migrating - Backup created"

if [ ! -d {{ backup_dir }} ]; then
mv -f {{ shared_dir }} {{ backup_dir }}
fi

SHARED_NEEDS_MIGRATING=true
fi
3 changes: 3 additions & 0 deletions resources/scripts/deploy/MigrateSharedTimestamp.sh
@@ -0,0 +1,3 @@
if [ ! -f {{ shared_dir }}/{{ migration }} ]; then
echo '{{ release }}' > {{ shared_dir }}/{{ migraton }}
fi
10 changes: 9 additions & 1 deletion resources/scripts/deploy/ShareDirectory.sh
Expand Up @@ -7,7 +7,15 @@ if [ ! -d {{ shared_dir }}/{{ path }} ]; then
fi
fi

rm -rf {{ release_dir }}/{{ path }}
if [ -d {{ release_dir }}/{{ path }} ]; then
rm -rf {{ release_dir }}/{{ path }}
fi

ln -s {{ shared_dir }}/{{ path }} {{ release_dir }}/{{ path }}

if [ $SHARED_NEEDS_MIGRATING ]; then
if [ -e {{ backup_dir }}/{{ filename }} ]; then
#rm -rf {{ shared_dir }}/{{ path }}
cp -RvfT {{ backup_dir }}/{{ filename }} {{ shared_dir }}/{{ path }}
fi
fi
9 changes: 8 additions & 1 deletion resources/scripts/deploy/ShareFile.sh
@@ -1,4 +1,4 @@
### Created shared files - {{ deployment }}
### Create shared files - {{ deployment }}
mkdir -p {{ shared_dir }}/{{ parent_dir }}

if [ -f {{ release_dir }}/{{ path }} ]; then
Expand All @@ -14,3 +14,10 @@ if [ ! -f {{ shared_dir }}/{{ path }} ]; then
fi

ln -s {{ shared_dir }}/{{ path }} {{ release_dir }}/{{ path }}

if [ $SHARED_NEEDS_MIGRATING ]; then
if [ -e {{ backup_dir }}/{{ filename }} ]; then
rm -rf {{ shared_dir }}/{{ path }}
cp -RvfT {{ backup_dir }}/{{ filename }} {{ shared_dir }}/{{ path }}
fi
fi
19 changes: 18 additions & 1 deletion tests/Unit/Jobs/DeployProject/ScriptBuilderTest.php
Expand Up @@ -412,13 +412,22 @@ public function testIncludesSharedFiles()
$this->project->shouldReceive('getAttribute')->with('configFiles')->andReturn(new Collection());
$this->project->shouldReceive('getAttribute')->with('sharedFiles')->andReturn($shared);

$this->parser->shouldReceive('parseFile')->with('deploy.MigrateShared', [
'deployment' => 12312,
'shared_dir' => '/var/www/shared',
'project_dir' => '/var/www',
'backup_dir' => '/var/www/shared.backup',
'migration' => '.deployer-migrated',
])->andReturn($script);

// Test directory
$this->parser->shouldReceive('parseFile')->with('deploy.ShareDirectory', [
'deployment' => 12312,
'shared_dir' => '/var/www/shared',
'release_dir' => '/var/www/releases/20170110155645',
'path' => 'a/path/to/directory',
'filename' => 'directory',
'backup_dir' => '/var/www/shared.backup',
'parent_dir' => 'a/path/to',
])->andReturn($script);

Expand All @@ -429,6 +438,7 @@ public function testIncludesSharedFiles()
'release_dir' => '/var/www/releases/20170110155645',
'path' => 'config/file/config.yml',
'filename' => 'config.yml',
'backup_dir' => '/var/www/shared.backup',
'parent_dir' => 'config/file',
])->andReturn($script);

Expand All @@ -438,15 +448,22 @@ public function testIncludesSharedFiles()
'shared_dir' => '/var/www/shared',
'release_dir' => '/var/www/releases/20170110155645',
'path' => 'config/file/README',
'backup_dir' => '/var/www/shared.backup',
'filename' => 'README',
'parent_dir' => 'config/file',
])->andReturn($script);

$this->parser->shouldReceive('parseFile')->with('deploy.MigrateSharedTimestamp', [
'release' => 20170110155645,
'shared_dir' => '/var/www/shared',
'migration' => '.deployer-migrated',
])->andReturn($script);

$this->process->shouldReceive('setScript')
->with('deploy.steps.InstallComposerDependencies', m::type('array')) // FIXME: Should check tokens
->andReturnSelf();
$this->process->shouldReceive('prependScript')->with('')->andReturnSelf();
$this->process->shouldReceive('appendScript')->with(PHP_EOL . $script . $script . $script)->andReturnSelf();
$this->process->shouldReceive('appendScript')->with(PHP_EOL . $script . $script . $script . $script . $script)->andReturnSelf();

$this->deployment->shouldReceive('getAttribute')->with('user')->andReturnNull();
$this->deployment->shouldReceive('getAttribute')->with('is_webhook')->andReturn(false);
Expand Down

0 comments on commit 8e36ed2

Please sign in to comment.