[ansible] added deploy role

This commit is contained in:
2018-03-28 16:38:16 +03:00
parent f6ebec32ca
commit 6d2a1a002b
26 changed files with 129 additions and 139 deletions

View File

@@ -1,2 +1,6 @@
[defaults] [defaults]
hash_behaviour = merge hash_behaviour = merge
host_key_checking = False
[ssh_connection]
ssh_args = -o ForwardAgent=yes

3
ansible/deploy.yml Normal file
View File

@@ -0,0 +1,3 @@
- hosts: all
roles:
- deploy

View File

@@ -0,0 +1,3 @@
project_name: "tankz"
project_smartname: "Tank'z"
project_dir: "/home/holop/{{ project_name }}"

View File

@@ -0,0 +1,19 @@
- import_tasks: repo.yml
- import_tasks: npm.yml
- name: Copy config.json
copy:
src: "{{ release_dir }}/config.shmyga.ru.json"
dest: "{{ release_dir }}/config.json"
remote_src: true
- name: Gulp build
command: "gulp default"
args:
chdir: "{{ release_dir }}"
- name: Create symlink to release
file:
src: "{{ release_dir }}"
dest: "{{ project_dir }}/current"
state: link

View File

@@ -0,0 +1,21 @@
- name: Create directory for node_modules
file:
path: "{{ project_dir }}/npm"
state: directory
- name: Copy package.json
copy:
src: "{{ release_dir }}/package.json"
dest: "{{ project_dir }}/npm/"
remote_src: true
- name: npm install
command: npm install
args:
chdir: "{{ project_dir }}/npm/"
- name: Create symlink to node_modules
file:
src: "{{ project_dir }}/npm/node_modules"
dest: "{{ release_dir }}/node_modules"
state: link

View File

@@ -0,0 +1,17 @@
- name: Update git repository
git:
repo: "{{ repo_url }}"
dest: "{{ project_dir }}/repo"
- name: Creates directory for release
file:
path: "{{ release_dir }}"
state: directory
- name: Copy project files
synchronize:
src: "{{ project_dir }}/repo/"
dest: "{{ release_dir }}"
rsync_opts:
- "--exclude=.git"
delegate_to: "{{ inventory_hostname }}"

View File

@@ -0,0 +1,5 @@
ansible_user: holop
date_str: "{{ ansible_date_time.year }}{{ ansible_date_time.month }}{{ ansible_date_time.day }}{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}{{ ansible_date_time.second }}"
repo_url: "git@bitbucket.org:infernalgames/{{ project_name }}.git"
release_dir: "{{ project_dir }}/releases/{{ date_str }}"

View File

@@ -1,5 +0,0 @@
- name: install tankz systemd unit file
template: src=template/tankz.j2 dest=/etc/systemd/system/tankz.service
- name: start tankz
systemd: state=started name=tankz daemon_reload=yes

View File

@@ -1,16 +0,0 @@
[Unit]
Description=Tank'z game server
[Service]
Type=simple
PIDFile=/var/run/tankz.pid
WorkingDirectory=/home/holop/repo/tankz/current/target
User=www-data
Group=www-data
ExecStart=/usr/bin/neko /home/holop/repo/tankz/current/target/tankz.n
TimeoutSec=300
Restart=always
[Install]
WantedBy=multi-user.target

View File

@@ -1,3 +0,0 @@
ansible_become: yes
ansible_user: barin
ansible_become_pass: 1234!QAZ

View File

@@ -0,0 +1,10 @@
- name: Install the package "Neko"
apt:
name: 'neko'
- name: Install the package "Node"
apt:
name: '{{ item }}'
with_items:
- nodejs
- npm

View File

@@ -0,0 +1,6 @@
- import_tasks: apt.yml
when: ansible_distribution|lower == 'ubuntu'
- import_tasks: npm.yml
- import_tasks: service.yml

View File

@@ -0,0 +1,4 @@
- name: Install "gulp-cli" node.js package globally.
npm:
name: gulp-cli
global: yes

View File

@@ -0,0 +1,10 @@
- name: "install {{ service_name }} systemd unit file"
template:
src: "template/service.j2"
dest: "/etc/systemd/system/{{ service_name }}.service"
- name: "start {{ service_name }} service"
systemd:
state: started
name: "{{ service_name }}"
daemon_reload: yes

View File

@@ -0,0 +1,15 @@
[Unit]
Description={{ service_description }}
[Service]
Type=simple
PIDFile=/var/run/{{ service_name }}.pid
WorkingDirectory={{ service_work_dir }}
User={{ service_user }}
Group={{ service_user }}
ExecStart={{ service_command }}
TimeoutSec=300
Restart=always
[Install]
WantedBy=multi-user.target

View File

@@ -0,0 +1,9 @@
ansible_become: yes
ansible_user: barin
ansible_become_pass: 1234!QAZ
service_name: "{{ project_name }}"
service_description: "{{ project_smartname }} server"
service_work_dir: "{{ project_dir }}/current/target"
service_command: "/usr/bin/neko {{ project_dir }}/current/target/{{ project_name }}.n"
service_user: www-data

View File

@@ -1,3 +1,3 @@
- hosts: all - hosts: all
roles: roles:
- service - setup

View File

@@ -46,4 +46,4 @@ const test = (build) => function test() {
exports['server'] = gulp.series(prepare(Haxe.ID), generate(), build()); exports['server'] = gulp.series(prepare(Haxe.ID), generate(), build());
exports['server:test'] = gulp.series(prepare(Haxe.ID, FlashPlayer.ID), generate(), test(build())); exports['server:test'] = gulp.series(prepare(Haxe.ID), generate(), test(build()));

11
capfile
View File

@@ -1,11 +0,0 @@
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/scm/git'
install_plugin Capistrano::SCM::Git
require 'capistrano/scm/git-with-submodules'
install_plugin Capistrano::SCM::Git::WithSubmodules
require 'capistrano/npm'
require 'capistrano/gulp'
require 'airbrussh/capistrano'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

View File

@@ -1,16 +0,0 @@
set :config_role, :app
set :config_path, -> { release_path }
set :config_type, 'json'
namespace :config do
task :setup do
config_role = fetch(:config_role)
config_path = fetch(:config_path)
config_type = fetch(:config_type)
on roles(config_role) do |server|
# stage = fetch(:stage)
execute "cd #{config_path} && cp config.#{server.hostname}.#{config_type} config.#{config_type}"
end
end
end

View File

@@ -1,31 +0,0 @@
require_relative 'npm'
require_relative 'config'
app = 'tankz'
user = 'holop'
# git
set :application, app
set :repo_url, "git@bitbucket.org:infernalgames/#{app}.git"
set :user, user
set :deploy_to, "/home/#{user}/repo/#{app}"
# airbrussh
set :format, :airbrussh
set :format_options, command_output: false
# config
before 'deploy:updated', 'config:setup'
# npm
set :npm_target_path, -> { "/home/#{fetch(:user)}/npm" }
set :npm_flags, '--silent --no-spin'
set :npm_roles, :web
set :npm_env_variables, {}
# gulp
set :gulp_target_path, -> { release_path }
set :gulp_tasks, 'default'
set :gulp_flags, '--no-color'
set :gulp_roles, :web
before 'deploy:updated', 'gulp'

View File

@@ -1,11 +0,0 @@
role :app, %w{localhost}
role :web, %w{localhost}
role :db, %w{localhost}
#set :user, ENV['USER']
user = fetch(:user)
server 'localhost', ssh_options: { port: 22, user: user, forward_agent: true }
set :tmp_dir, "/home/#{user}/tmp"
set :branch, 'master'

View File

@@ -1,13 +0,0 @@
# host = '94.130.110.9'
host = 'shmyga.ru'
role :app, host
role :web, host
role :db, host
user = fetch(:user)
server host, ssh_options: { port: 22, user: user, forward_agent: true }
set :tmp_dir, "/home/#{user}/tmp"
set :branch, 'master'

View File

@@ -1,24 +0,0 @@
set :npm_path, -> { release_path }
namespace :npm do
task :prepare do
npm_roles = fetch(:npm_roles)
on roles(npm_roles) do
npm_target_path = fetch(:npm_target_path)
npm_path = fetch(:npm_path)
execute "mkdir -p #{npm_target_path}"
execute "cp #{npm_path}/package.json #{npm_target_path}"
end
end
task :link do
npm_roles = fetch(:npm_roles)
on roles(npm_roles) do
npm_target_path = fetch(:npm_target_path)
npm_path = fetch(:npm_path)
execute "ln -s #{npm_target_path}/node_modules #{npm_path}/"
end
end
end
before 'npm:install', 'npm:prepare'
after 'npm:install', 'npm:link'

View File

@@ -1,6 +0,0 @@
source 'https://rubygems.org'
gem 'capistrano', '~> 3.7.0'
gem 'capistrano-git-with-submodules', '~> 2.0'
gem 'capistrano-npm'
gem 'capistrano-gulp'
gem 'airbrussh', :require => false

View File

@@ -33,4 +33,4 @@ merge('./build/editor');
merge('./build/server'); merge('./build/server');
exports.default = gulp.series(exports.clean, exports.client); exports.default = gulp.series(exports.clean, exports.client, exports.server);