Skip to content

Support write environment file such like .env, .local.env #35

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

Merged
merged 7 commits into from
May 24, 2015
Merged
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
156 changes: 103 additions & 53 deletions app/Commands/DeployProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ private function getScript(DeployStep $step, Server $server)
$remote_key_file = $root_dir . '/id_rsa';
$remote_wrapper_file = $root_dir . '/wrapper.sh';

// FIXME: This does not belong here as this function should only being returning the commands not running them!
// FIXME: This does not belong here as this function should
// only being returning the commands
// not running them!
$this->prepareServer($server);

$commands = [
Expand Down Expand Up @@ -340,60 +342,31 @@ private function getScript(DeployStep $step, Server $server)
$latest_release_dir
)
];
} elseif ($step->stage === Stage::DO_ACTIVATE) { // Activate latest release
$commands = [
sprintf('cd %s', $root_dir)
];

foreach ($project->shareFiles as $filecfg) {
if ($filecfg->file) {
$pathinfo = pathinfo($filecfg->file);
$isDir = false;

if (substr($filecfg->file, 0, 1) == '/') {
$filecfg->file = substr($filecfg->file, 1);
}

if (substr($filecfg->file, -1) == '/') {
$isDir = true;
$filecfg->file = substr($filecfg->file, 0, -1);
}

if (isset($pathinfo['extension'])) {
$filename = $pathinfo['filename'] . '.' . $pathinfo['extension'];
} else {
$filename = $pathinfo['filename'];
}

$sourceFile = $release_shared_dir . '/' . $filename;
$targetFile = $latest_release_dir . '/' . $filecfg->file;

if ($isDir) {
$commands[] = sprintf(
'[ -d %s ] && cp -pRn %s %s && rm -rf %s',
$targetFile,
$targetFile,
$sourceFile,
$targetFile
);
$commands[] = sprintf('[ ! -d %s ] && mkdir %s', $sourceFile, $sourceFile);
} else {
$commands[] = sprintf(
'[ -f %s ] && cp -pRn %s %s && rm -rf %s',
$targetFile,
$targetFile,
$sourceFile,
$targetFile
);
$commands[] = sprintf('[ ! -f %s ] && touch %s', $sourceFile, $sourceFile);
}

$commands[] = sprintf('ln -s %s %s', $sourceFile, $targetFile);
// the shared file must be created in the install step
$shareFileCommands = $this->shareFileCommands(
$project,
$latest_release_dir,
$release_shared_dir
);

$commands = array_merge($commands, $shareFileCommands);

// write project file to release dir before install

$projectFiles = $project->projectFiles;
foreach ($projectFiles as $file) {
if ($file->path) {
$filepath = $latest_release_dir . '/' . $file->path;
$this->sendFileFromString($server, $filepath, $file->content);
}
}

$commands[] = sprintf('[ -h %s/latest ] && rm %s/latest', $root_dir, $root_dir);
$commands[] = sprintf('ln -s %s %s/latest', $latest_release_dir, $root_dir);
} elseif ($step->stage === Stage::DO_ACTIVATE) { // Activate latest release
$commands = [
sprintf('cd %s', $root_dir),
sprintf('[ -h %s/latest ] && rm %s/latest', $root_dir, $root_dir),
sprintf('ln -s %s %s/latest', $latest_release_dir, $root_dir)
];
} elseif ($step->stage === Stage::DO_PURGE) { // Purge old releases
$commands = [
sprintf('cd %s', $releases_dir),
Expand Down Expand Up @@ -545,4 +518,81 @@ private function prepareServer(Server $server)

unlink($wrapper);
}
}

/**
* send a string to server
* @param Server $server target server
* @param string $filename remote filename
* @param string $content the file content
* @return void
*/
private function sendFileFromString(Server $server, $filepath, $content)
{
$wrapper = tempnam(storage_path() . '/app/', 'wrapper');
file_put_contents($wrapper, $content);

// Upload the wrapper file
$this->sendFile($wrapper, $filepath, $server);

unlink($wrapper);
}

/**
* create the command for share files
* @param Project $project the related project
* @param string $release_dir current release dir
* @param string $shared_dir the shared dir
* @return array
*/
private function shareFileCommands(Project $project, $release_dir, $shared_dir)
{
$commands = array();
foreach ($project->shareFiles as $filecfg) {
if ($filecfg->file) {
$pathinfo = pathinfo($filecfg->file);
$isDir = false;

if (substr($filecfg->file, 0, 1) == '/') {
$filecfg->file = substr($filecfg->file, 1);
}

if (substr($filecfg->file, -1) == '/') {
$isDir = true;
$filecfg->file = substr($filecfg->file, 0, -1);
}

if (isset($pathinfo['extension'])) {
$filename = $pathinfo['filename'] . '.' . $pathinfo['extension'];
} else {
$filename = $pathinfo['filename'];
}

$sourceFile = $shared_dir . '/' . $filename;
$targetFile = $release_dir . '/' . $filecfg->file;

if ($isDir) {
$commands[] = sprintf(
'[ -d %s ] && cp -pRn %s %s && rm -rf %s',
$targetFile,
$targetFile,
$sourceFile,
$targetFile
);
$commands[] = sprintf('[ ! -d %s ] && mkdir %s', $sourceFile, $sourceFile);
} else {
$commands[] = sprintf(
'[ -f %s ] && cp -pRn %s %s && rm -rf %s',
$targetFile,
$targetFile,
$sourceFile,
$targetFile
);
$commands[] = sprintf('[ ! -f %s ] && touch %s', $sourceFile, $sourceFile);
}

$commands[] = sprintf('ln -s %s %s', $sourceFile, $targetFile);
}
}
return $commands;
}
}
50 changes: 37 additions & 13 deletions app/Commands/TestServerConnection.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php namespace App\Commands;

use Config;
use SSH;
use App\Server;
use App\Commands\Command;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldBeQueued;
use Symfony\Component\Process\Process;

/**
* Tests if a server can successfully be SSHed into
Expand Down Expand Up @@ -42,19 +41,17 @@ public function handle()
$key = tempnam(storage_path() . '/app/', 'sshkey');
file_put_contents($key, $this->server->project->private_key);

Config::set('remote.connections.runtime.host', $this->server->ip_address . ':' . $this->server->port);
Config::set('remote.connections.runtime.username', $this->server->user);
Config::set('remote.connections.runtime.password', '');
Config::set('remote.connections.runtime.key', $key);
Config::set('remote.connections.runtime.keyphrase', '');
Config::set('remote.connections.runtime.root', $this->server->path);

try {
SSH::into('runtime')->run([
'ls'
]);
$command = $this->sshCommand($this->server, $key, 'ls', $this->server->user);
$process = new Process($command);
$process->setTimeout(null);
$process->run();

$this->server->status = Server::SUCCESSFUL;
if (!$process->isSuccessful()) {
$this->server->status = Server::FAILED;
} else {
$this->server->status = Server::SUCCESSFUL;
}
} catch (\Exception $error) {
$this->server->status = Server::FAILED;
}
Expand All @@ -63,4 +60,31 @@ public function handle()

unlink($key);
}

/**
* Generates the SSH command for running the script on a server
*
* @param Server $server
* @param string $script The script to run
* @param string $user
* @return string
*/
private function sshCommand(Server $server, $private_key, $script, $user = null)
{
if (is_null($user)) {
$user = $server->user;
}

$script = 'set -e' . PHP_EOL . $script;
return 'ssh -o CheckHostIP=no \
-o IdentitiesOnly=yes \
-o StrictHostKeyChecking=no \
-o PasswordAuthentication=no \
-o IdentityFile=' . $private_key . ' \
-p ' . $server->port . ' \
' . $user . '@' . $server->ip_address . ' \'bash -s\' << EOF
'.$script.'
EOF';

}
}
1 change: 1 addition & 0 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function show(Project $project, DeploymentRepositoryInterface $deployment
'notifications' => $project->notifications,
'heartbeats' => $project->heartbeats,
'sharedFiles' => $project->shareFiles,
'projectFiles' => $project->projectFiles,
'optional' => $optional
]);
}
Expand Down
60 changes: 60 additions & 0 deletions app/Http/Controllers/ProjectFileController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php namespace App\Http\Controllers;

use App\ProjectFile;
use App\Http\Controllers\Controller;

use App\Http\Requests\StoreProjectFileRequest;

/**
* Manage the project global file like some environment files
*/
class ProjectFileController extends Controller
Copy link
Owner

Choose a reason for hiding this comment

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

Class ProjectFileController does not have a Docblock comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

{

/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(StoreProjectFileRequest $request)
{
return ProjectFile::create($request->only(
'name',
'path',
'content',
'project_id'
));
}

/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update(ProjectFile $file, StoreProjectFileRequest $request)
{
$file->update($request->only(
'name',
'path',
'content'
));

return $file;
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy(ProjectFile $file)
{
$file->delete();

return [
'success' => true
];
}
}
24 changes: 24 additions & 0 deletions app/Http/Requests/StoreProjectFileRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

/**
* Request for validating servers
*/
class StoreProjectFileRequest extends Request
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:255',
'path' => 'required',
'content' => 'required',
'project_id' => 'required|integer|exists:projects,id'
];
}
}
1 change: 1 addition & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
]);

Route::resource('shared-files', 'SharedFilesController');
Route::resource('project-file', 'ProjectFileController');

Route::group(['prefix' => 'admin'], function () {

Expand Down
9 changes: 9 additions & 0 deletions app/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ public function shareFiles()
return $this->hasMany('App\SharedFile');
}

/**
* Has many relationship to project file
* @return ProjectFile
*/
public function projectFiles()
{
return $this->hasMany('App\ProjectFile');
}

/**
* Generates a hash for use in the webhook URL
*
Expand Down
Loading