Skip to content

Instantly share code, notes, and snippets.

@yushine
Created November 1, 2016 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yushine/dc55f769222af149e3360405b0134436 to your computer and use it in GitHub Desktop.
Save yushine/dc55f769222af149e3360405b0134436 to your computer and use it in GitHub Desktop.
Laravel Envoy Task Runner
@servers(['staging' => 'root@192.168.0.99', 'production' => 'root@192.168.0.100'])
<?php
$app_name = 'l5eka';
$app_path = '/var/www/vhosts/'.$app_name;
$repo = 'https://github.com/vedovelli/l5eka';
$branch = 'aula4';
$github_token = ''; // GitHub OAuth token
$server_name = '192.168.0.99'; // IP ou domínio válido. Usado no nginx
$timezone = 'America/Sao_Paulo';
/*
|--------------------------------------------------------------------------
| EXEMPLOS DE USO:
|--------------------------------------------------------------------------
| envoy run deploy --on=staging
| envoy run deploy --on=staging --swap=2048
| ou
| envoy run deploy --on=production
| envoy run deploy --on=production --swap=2048
|--------------------------------------------------------------------------
| Caso queira executar migrations e seeds, descomente as taks
| 'artisan_migrate' e 'artisan_seed' dentro da macro 'deploy'
*/
/*
|--------------------------------------------------------------------------
| PACOTES NECESSÁRIOS NO SISTEMA OPERACIONAL
|--------------------------------------------------------------------------
| Serão instalados com o comando 'apt-get install -qq -y nome_do_pacote'
| IMPORTANTE: Leva-se em conta que o servidor já possua os serviços mais
| básicos instalados. Exemplo: mysql-server, nginx.
*/
$apt_get_packages = [
'git',
'nodejs',
'npm',
'php5-cli',
'php5-mcrypt',
];
/*
|--------------------------------------------------------------------------
| PACOTES NPM GLOBAIS
|--------------------------------------------------------------------------
| Serão instalados com o comando npm install -g nome_do_pacote
*/
$npm_packages = [
'bower',
'gulp',
];
/*
|--------------------------------------------------------------------------
| PERMISSÕES ESPECIAIS EM ARQUIVOS OU DIRETÓRIOS ESPECÍFICOS
|--------------------------------------------------------------------------
| Deverá ser informado o caminho e a permissão e forma de array. Exemplo:
| $chmods = [ 'public' => '755' ];
*/
$chmods = [
'storage' => '755',
'public' => '755',
];
/*
|--------------------------------------------------------------------------
| REGRAS DE FIREWALL
|--------------------------------------------------------------------------
| As portas informadas neste array serão liberadas no firewall do Ubuntu.
| Exemplo:
| $firewall = ['allow_ports'] => ['22', 80', '443'];
*/
$firewall = [
'allow_ports' => [
'22',
'80',
'443',
]
];
$date = new DateTime('now', new DateTimeZone($timezone));
$release = $app_path .'/releases/'. $date->format('YmdHi');
$environment = isset($env) ? $env : "local";
$swap_size = isset($swap) ? $swap : "1024";
$bin_path = '/usr/bin';
?>
@macro('deploy')
update_apt_sources
mkswap
install_os_packages
install_npm_packages_global
fetch_repo
update-symlink
nginx_setup
composer_config
composer_install
install_npm_packages
bower_install
npm_install
update_permissions
ufw_rules
{{-- artisan_migrate --}}
{{-- artisan_seed --}}
gulp
done
@endmacro
@task('mkswap', ['on' => $on])
@if (isset($swap_size) && is_numeric($swap_size))
echo "Ativando {{$swap_size}}MB de memória swap...";
/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count={{$swap_size}} ;
/sbin/mkswap /var/swap.1 ;
/sbin/swapon /var/swap.1 ;
@endif
@endtask
@task('composer_config', ['on' => $on])
echo "Configurando composer...";
@if (isset($github_token) && strlen($github_token) == 40)
composer config -g github-oauth.github.com {{ $github_token }};
@endif
@endtask
@task('update_apt_sources', ['on' => $on])
echo "Atualizando lista de pacotes..." ;
apt-get update -q ;
@endtask
@task('install_os_packages', ['on' => $on])
echo "Instalando pacotes necessários..." ;
cd /tmp ;
apt-get upgrade -y -q ;
@foreach($apt_get_packages as $pkg)
apt-get install -qq -y {{ $pkg }} ;
echo -e "{{ $pkg }} instalado...\n" ;
@endforeach
([[ -e /usr/bin/nodejs ]] && [[ ! -e /usr/bin/node ]]) && ln -s /usr/bin/nodejs /usr/bin/node ;
apt-get install -f -q ;
php5enmod mcrypt;
service nginx restart ;
service php5-fpm restart ;
[[ -e {{$bin_path}}/composer ]] && composer self-update;
[[ ! -e {{$bin_path}}/composer ]] && curl -sS https://getcomposer.org/installer | php -- --filename=composer --install-dir={{$bin_path}} ;
@endtask
@task('install_npm_packages_global', ['on' => $on])
[[ ! -e {{ $release }} ]] && mkdir -p {{ $release }};
@foreach($npm_packages as $pkg)
echo "Instalando pacotes npm global 'npm install -g {{ $pkg }}' ..." ;
npm install -g {{ $pkg }} ;
@endforeach
@endtask
@task('fetch_repo', ['on' => $on])
mkdir -p {{ $release }};
git clone --depth 1 -b {{ $branch }} "{{ $repo }}" {{ $release }};
@endtask
@task('bower_install', ['on' => $on])
echo "Instalando assets..." ;
cd {{ $release }}/resources/assets ;
bower install --allow-root ;
@endtask
@task('install_npm_packages', ['on' => $on])
cd {{ $release }} ;
@foreach($npm_packages as $pkg)
echo "Instalando pacotes npm global 'npm install {{ $pkg }}' ..." ;
npm install -g {{ $pkg }} ;
@endforeach
@endtask
@task('npm_install', ['on' => $on])
echo "Executando npm install ..." ;
cd {{ $release }} ;
npm install ;
@endtask
@task('gulp', ['on' => $on])
echo "Executando gulp ..." ;
cd {{ $release }} ;
gulp ;
@endtask
@task('composer_install', ['on' => $on])
cd {{ $release }} ;
composer install --no-interaction --no-dev --prefer-dist ;
composer update --no-interaction --no-dev --prefer-dist ;
echo "Dependencias do Composer instaladas e atualizadas..."
@endtask
@task('update-symlink', ['on' => $on])
rm -f {{ $app_path }}/current ;
ln -fs {{ $release }} {{ $app_path }}/current ;
echo "Link simbólico criado em {{ $app_path }}/current apontando para {{ $release }} ..." ;
@endtask
@task('artisan_migrate', ['on' => $on])
cd {{ $release }};
php artisan migrate --env={{ $environment }} --force --no-interaction;
@endtask
@task('artisan_seed', ['on' => $on])
cd {{ $release }};
php artisan db:seed --env={{ $environment }} --force --no-interaction;
@endtask
@task('update_permissions')
cd {{ $release }};
chown -R www-data:www-data {{ $app_path }};
chmod -R 755 {{ $app_path }};
@foreach($chmods as $file => $permission)
chmod -R {{ $permission }} {{ $release }}/{{ $file }} ;
echo "Permissões setadas para {{ $permission }} em {{ $release }}/{{ $file }}" ;
@endforeach
@endtask
@task('nginx_setup', ['on' => $on])
echo "Criando arquivo de configuração nginx /etc/nginx/sites-available/default";
[[ -e /etc/nginx/sites-available/default ]] && cp "/etc/nginx/sites-available/default" "/etc/nginx/sites-available/default_{{$date->format('YmdHis')}}.bak";
cat > /etc/nginx/sites-available/default << "EOF" ;
server {
listen 80;
server_name {{ $server_name }};
root "{{ $app_path }}/current/public/";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/{{ $server_name }}/-error.log error;
error_page 404 /index.php;
sendfile off;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
EOF
[[ ! -e /var/log/nginx/{{ $server_name }} ]] && mkdir -p "/var/log/nginx/{{ $server_name }}";
[[ ! -e /var/log/nginx/{{ $server_name }}/-error.log ]] && touch "/var/log/nginx/{{ $server_name }}/-error.log";
service nginx restart ;
service php5-fpm restart ;
@endtask
@task('ufw_rules', ['on' => $on])
echo "Atualizando regras do Firewall Ufw..." ;
@foreach($firewall['allow_ports'] as $port)
ufw allow {{ $port }};
@endforeach
ufw default deny;
ufw reload;
echo "Certifique-se de ativar o firewall no Servidor com o comando 'ufw enable'..." ;
@endtask
@task('done', ['on' => $on])
echo "Deploy finalizado!";
@endtask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment