Skip to content

Commit

Permalink
リソースをS3に同期するコマンドを実装 #42
Browse files Browse the repository at this point in the history
  • Loading branch information
app2641 committed Sep 20, 2014
1 parent 93f0583 commit 4ca59bf
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
3 changes: 3 additions & 0 deletions data/config/CrawlerCloudConfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

runcmd:
- [git, clone, https://github.com/app2641/AdultMidnight.git, /home/ec2-user/AdultMidnight]
- [cd, /home/ec2-user/AdultMidnight]
- [composer.phar, self-update]
- [composer.phar, install]
- [/home/ec2-user/AdultMidnight/bin/midnight, Crawl]
- [/home/ec2-user/AdultMidnight/bin/midnight, TerminateCrawler]

7 changes: 7 additions & 0 deletions data/config/sync_exclude.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store

demo.html
index.html
contents
resources/sass

129 changes: 129 additions & 0 deletions src/Midnight/Commands/S3Sync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

use Emerald\Command\AbstractCommand;
use Emerald\Command\CommandInterface;

use Midnight\Aws\S3;

class S3Sync extends AbstractCommand implements CommandInterface
{

/**
* @var array
*/
private $params;


/**
* @var array
*/
private $exclude_files = array();


/**
* @var S3
*/
private $s3;


/**
* コマンドの実行
*
* @param array $params パラメータ配列
* @return void
**/
public function execute (array $params)
{
try {
// アップロード対象のディレクトリを取得する
$this->params = $params;
$target_directory = $this->_getTargetDirectory();

// 除外ファイルの初期化
$this->_initExcludeFiles();

// 同期処理
$this->s3 = new S3();
$this->_sync($target_directory);

} catch (\Exception $e) {
$this->errorLog($e->getMessage());
}
}


/**
* 同期対象のディレクトリパスを取得する
*
* @return string
*/
private function _getTargetDirectory ()
{
if (isset($this->params[1])) {
$target = ROOT.'/'.$this->params[1];
$target = preg_replace('/\/$/', '', $target);

if (! preg_match('/public_html\/*/', $target)) {
throw new \Exception('同期はpublic_htmlディレクトリ以下が対象です');
}

} else {
$target = ROOT.'/public_html';
}

if (! is_dir($target)) {
throw new \Exception('ディレクトリが存在しません');
}

return $target.'/';
}


/**
* data/config/sync_exclude.txtから除外ファイルを配列化させる
*
* @return void
*/
private function _initExcludeFiles ()
{
$files = file_get_contents(ROOT.'/data/config/sync_exclude.txt');
$files = explode(PHP_EOL, $files);

$this->exclude_files = $files;
}


/**
* S3に対象ディレクトリを同期する
*
* @param string $target_directory
* @return void
*/
private function _sync ($target_directory)
{
foreach (glob($target_directory.'*') as $path) {
$file = str_replace(ROOT.'/public_html/', '', $path);
if (in_array($file, $this->exclude_files)) {
continue;
}

if (is_dir($path)) {
$path .= '/';
$this->_sync($path);
} else {
$this->s3->upload($path, $file);
}
}
}


/**
* ヘルプメッセージの表示
*
* @return String
**/
public static function help ()
{
return '指定ディレクトリをS3に同期する。未指定の場合はpublic_html以下を対象とする。';
}
}
2 changes: 1 addition & 1 deletion src/Midnight/Commands/UpdateScheduledAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private function _updateScheduledAction ()
$options = array(
'AutoScalingGroupName' => 'ModernAdultMidnightCrawlerGroup',
'ScheduledActionName' => 'ModernAdultMidnightCrawlerSchedule',
'Recurrence' => '55 * * * *',
'Recurrence' => '55 10-14 * * *',
'MinSize' => 1,
'MaxSize' => 1
);
Expand Down

0 comments on commit 4ca59bf

Please sign in to comment.