Skip to content

Instantly share code, notes, and snippets.

@mtthlm
Last active December 20, 2015 03:59
Show Gist options
  • Save mtthlm/6067929 to your computer and use it in GitHub Desktop.
Save mtthlm/6067929 to your computer and use it in GitHub Desktop.
`$ php deploy <env>` Deploy project to SFTP server
<?php
echo PHP_EOL;
if ( array_key_exists( 1 , $argv ) == false ) {
echo 'Usage: ' . $argv[ 0 ] . ' <env>' . PHP_EOL . PHP_EOL;
exit(1);
}
$env = $argv[ 1 ];
$filename = $env . '.env';
$file = __DIR__ . '/' . $filename;
if ( file_exists( $file ) == false ) {
environment:
echo 'Environment file does not exist.';
if ( is_writable( __DIR__ ) == false ) {
echo ' I would assist you in creating one, but this directory is not writable.' . PHP_EOL;
exit(1);
}
echo ' Set one up now? ';
$answer = strtolower( trim( fgets( STDIN ) ) );
if ( in_array( $answer , array( 'y' , 'yes' ) ) ) {
echo PHP_EOL;
echo ' Server: ';
$server = trim( fgets( STDIN ) );
echo 'Username: ';
$username = trim( fgets( STDIN ) );
echo 'Password: ';
system( 'stty -echo' );
$password = trim( fgets( STDIN ) );
system( 'stty echo' );
echo PHP_EOL;
echo ' Path: ';
$path = trim( fgets( STDIN ) );
echo PHP_EOL;
echo 'Creating environment file...';
file_put_contents( $file , 'SERVER="' . $server . '"' . PHP_EOL . 'USERNAME="' . $username . '"' . PHP_EOL . 'PASSWORD="' . $password . '"' . PHP_EOL . 'PATH="' . $path . '"' . PHP_EOL );
echo ' Done.' . PHP_EOL;
echo PHP_EOL;
} else if ( in_array( $answer , array( 'n' , 'no' ) ) ) {
echo PHP_EOL;
exit;
} else {
goto environment;
}
} else {
$contents = file_get_contents( $file );
foreach ( explode( PHP_EOL , trim( $contents ) ) as $line ) {
$line = explode( '=' , $line );
${ strtolower( $line[ 0 ] ) } = trim( $line[ 1 ] , '"' );
}
}
$mountpoint = __DIR__ . '/mnt';
echo 'Creating mountpoint...';
exec( 'mkdir -p ' . $mountpoint );
echo ' Done.' . PHP_EOL;
echo PHP_EOL;
echo 'Mounting server...';
exec( 'echo ' . $password . ' | sshfs -o workaround=rename -o defer_permissions -o password_stdin ' . $username . '@' . $server . ':' . $path . ' ' . $mountpoint );
sleep( 2 );
echo ' Done.' . PHP_EOL;
echo PHP_EOL;
echo 'Firing up rsync...' . PHP_EOL;
echo PHP_EOL;
system( 'rsync --recursive --delete --compress --verbose --exclude=' . $argv[ 0 ] . ' --exclude=*.env --exclude=/mnt ' . __DIR__ . '/ ' . $mountpoint );
sleep( 2 );
echo PHP_EOL;
echo 'Done.' . PHP_EOL;
echo PHP_EOL;
echo 'Cleaning up...';
exec( 'umount ' . $mountpoint );
sleep( 2 );
exec( 'rm -r ' . $mountpoint );
echo ' Done.' . PHP_EOL;
sleep( 2 );
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment