diff --git a/.env.example b/.env.example index 32debfdb0c..0788a6ad97 100644 --- a/.env.example +++ b/.env.example @@ -175,3 +175,11 @@ DELAYED_JOB_MAX_RUNTIME=2 # Amount of seconds for delayed_job to sleep before checking for new jobs DELAYED_JOB_SLEEP_DELAY=10 + +########################################################## +# Capistrano deployment (read the documentation FIXME) # +########################################################## + +#CAPISTRANO_DEPLOY_SERVER= +#CAPISTRANO_DEPLOY_USER= +#CAPISTRANO_DEPLOY_REPO_URL= diff --git a/Capfile b/Capfile index f3634e86a0..83b98d42ee 100644 --- a/Capfile +++ b/Capfile @@ -1,2 +1,8 @@ -load 'deploy' -load 'config/deploy' +# Load DSL and set up stages +require 'capistrano/setup' +# Include default deployment tasks +require 'capistrano/deploy' + +require 'capistrano/bundler' +require 'capistrano/rails/assets' +require 'capistrano/rails/migrations' diff --git a/Gemfile b/Gemfile index 74cee4884f..c305fc6e0e 100644 --- a/Gemfile +++ b/Gemfile @@ -103,6 +103,10 @@ group :development do gem 'guard-rspec' gem 'letter_opener_web' + gem 'capistrano', '~> 3.4.0' + gem 'capistrano-rails', '~> 1.1' + gem 'capistrano-bundler', '~> 1.1.4' + group :test do gem 'coveralls', require: false gem 'delorean' diff --git a/Gemfile.lock b/Gemfile.lock index 9ecfe29e29..1a569e7736 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -84,6 +84,16 @@ GEM rails (>= 3.1) buftok (0.2.0) builder (3.2.2) + capistrano (3.4.0) + i18n + rake (>= 10.0.0) + sshkit (~> 1.3) + capistrano-bundler (1.1.4) + capistrano (~> 3.1) + sshkit (~> 1.2) + capistrano-rails (1.1.3) + capistrano (~> 3.1) + capistrano-bundler (~> 1.1) celluloid (0.16.0) timers (~> 4.0.0) chronic (0.10.2) @@ -95,6 +105,7 @@ GEM coffee-script-source execjs coffee-script-source (1.9.1) + colorize (0.7.7) cookiejar (0.3.2) coveralls (0.7.1) multi_json (~> 1.3) @@ -267,6 +278,9 @@ GEM mysql2 (0.3.16) naught (1.0.0) net-ftp-list (3.2.8) + net-scp (1.2.1) + net-ssh (>= 2.6.5) + net-ssh (2.9.2) netrc (0.10.3) nokogiri (1.6.6.2) mini_portile (~> 0.6.0) @@ -425,6 +439,10 @@ GEM actionpack (>= 3.0) activesupport (>= 3.0) sprockets (>= 2.8, < 4.0) + sshkit (1.7.1) + colorize (>= 0.7.0) + net-scp (>= 1.1.2) + net-ssh (>= 2.8.0) string-scrub (0.0.5) systemu (2.6.4) term-ansicolor (1.3.0) @@ -500,6 +518,9 @@ DEPENDENCIES binding_of_caller bootstrap-kaminari-views (~> 0.0.3) bundler (>= 1.5.0) + capistrano (~> 3.4.0) + capistrano-bundler (~> 1.1.4) + capistrano-rails (~> 1.1) coffee-rails (~> 4.1.0) coveralls daemons (~> 1.1.9) diff --git a/config/deploy.rb b/config/deploy.rb new file mode 100644 index 0000000000..125f3635d2 --- /dev/null +++ b/config/deploy.rb @@ -0,0 +1,54 @@ +require 'dotenv' +Dotenv.load + +# config valid only for current version of Capistrano +lock '3.4.0' + +set :application, 'huginn' +set :repo_url, ENV['CAPISTRANO_DEPLOY_REPO_URL'] || 'https://github.com/cantino/huginn.git' + +# Default branch is :master +set :branch, ENV['BRANCH'] || 'master' + +set :deploy_to, '/home/huginn' + +# Set to :debug for verbose ouput +set :log_level, :info + +# Default value for :linked_files is [] +set :linked_files, fetch(:linked_files, []).push('.env', 'Procfile', 'config/unicorn.rb') + +# Default value for linked_dirs is [] +set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle') + +# Default value for keep_releases is 5 +# set :keep_releases, 5 + +set :bundle_jobs, 4 + +set :conditionally_migrate, true # Defaults to false. If true, it's skip migration if files in db/migrate not modified + +task :deploy => [:production] + +namespace :deploy do + after 'check:make_linked_dirs', :migrate_to_cap do + on roles(:all) do + # Try to migrate from the manual installation to capistrano directory structure + next if test('[ -L ~/huginn ]') + fetch(:linked_files).each do |f| + if !test("[ -f ~/shared/#{f} ] ") && test("[ -f ~/huginn/#{f} ]") + execute("cp ~/huginn/#{f} ~/shared/#{f}") + end + end + execute('mv ~/huginn ~/huginn.manual') + execute('ln -s ~/current ~/huginn') + end + end + after :publishing, :restart do + on roles(:all) do + within release_path do + execute :rake, 'production:restart' + end + end + end +end diff --git a/config/deploy/production.rb b/config/deploy/production.rb new file mode 100644 index 0000000000..f0196d2e64 --- /dev/null +++ b/config/deploy/production.rb @@ -0,0 +1 @@ +server ENV['CAPISTRANO_DEPLOY_SERVER'], user: ENV['CAPISTRANO_DEPLOY_USER'] || 'huginn', roles: %w{app db web} \ No newline at end of file diff --git a/doc/README.md b/doc/README.md index ea05ba3f77..b956117328 100644 --- a/doc/README.md +++ b/doc/README.md @@ -12,6 +12,7 @@ Manual installation which will guide through the steps to install Huginn on any - [Install](manual/README.md) Requirements, directory structures and installation from source. - [Update](manual/update.md) Update your installation. +- Deploy updates via [Capistrano](manual/capistrano.md). ### Heroku diff --git a/doc/manual/README.md b/doc/manual/README.md index d8ddbae2f0..6293e77f70 100644 --- a/doc/manual/README.md +++ b/doc/manual/README.md @@ -2,4 +2,5 @@ - [Requirements](requirements.md) Software and hardware requirements to run the Huginn installation - [Install](installation.md) Installation guide for Ubundu/Debian -- [Update](update.md) Update an existing Huginn installation \ No newline at end of file +- [Update](update.md) Update an existing Huginn installation +- Deploy updates via [Capistrano](capistrano.md) \ No newline at end of file diff --git a/doc/manual/capistrano.md b/doc/manual/capistrano.md new file mode 100644 index 0000000000..8701340dd6 --- /dev/null +++ b/doc/manual/capistrano.md @@ -0,0 +1,47 @@ +# Deploy updates via Capistrano + +After you followed the [manual installation guide](installation.md) it is simple to push updates to your huginn instance using capistrano. + +### 1. Ensure you have SSH access to your server via the huginn user + +Either set a password for the huginn user or add your public SSH key: + + # Set password + sudo passwd huginn + + # Or add a SSH key + sudo -u huginn -H mkdir -p /home/huginn/.ssh + sudo -u huginn -H editor /home/huginn/.ssh/authorized_keys + sudo -u huginn -H chmod -R 700 /home/huginn/.ssh + +### 2. Configure Capistrano on your local machine + +Add Capistrano configuration to you local `.env`: + + CAPISTRANO_DEPLOY_SERVER= + CAPISTRANO_DEPLOY_USER=huginn + CAPISTRANO_DEPLOY_REPO_URL=https://github.com/cantino/huginn.git + + +### 3. Run Capistrano + +You can now run Capistrano and update your server: + + cap production deploy + +If you want to deploy a different branch, pass it as environment variable: + + cap production deploy BRANCH=awesome-feature + +### Changes to remote .env and Procfile + +If you want to change the `.env`, `Procfile` or `config/unicorn.rb` of your installation you still need to do it on your server, do not forget to export the init scripts after your are done: + + cd /home/huginn/huginn + # Whichever you want to change + sudo -u huginn -H editor Procfile + sudo -u huginn -H editor .env + sudo -u huginn -H editor config/unicorn.rb + # Export init scripts and restart huginn + sudo rake production:export +