From 0e17b71ee96a28184513b1d9286a3cd9152314a8 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Tue, 16 Sep 2014 17:03:27 +0400 Subject: [PATCH] It works --- .gitignore | 14 +++++++++ Gemfile | 4 +++ LICENSE.txt | 22 +++++++++++++++ README.md | 39 ++++++++++++++++++++++++++ Rakefile | 2 ++ lib/mina/reboot.rb | 2 ++ lib/mina/reboot/save_startup_script.rb | 28 ++++++++++++++++++ lib/mina/reboot/version.rb | 3 ++ mina_reboot.gemspec | 23 +++++++++++++++ 9 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 lib/mina/reboot.rb create mode 100644 lib/mina/reboot/save_startup_script.rb create mode 100644 lib/mina/reboot/version.rb create mode 100644 mina_reboot.gemspec diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3fdc2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +/.bundle/ +/.yardoc +/Gemfile.lock +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ +*.bundle +*.so +*.o +*.a +mkmf.log diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..f5b3524 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in mina_reboot.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..f3e873d --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2014 Gregory Eremin + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..6a7062f --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# Mina Reboot + +Generates cron `@reboot` task every time you deploy with [Mina](http://nadarei.co/mina/), so your app will start automatically in case of server reboot. + +## Installation + +Add this line to your application's Gemfile: + +```ruby +gem 'mina_reboot' +``` + +## Usage + +Add these lines to your application's deploy config: +```ruby +require 'mina/reboot' + +set :on_reboot, -> { + # Tasks needed to start application + invoke 'unicorn:start' + invoke 'sidekiq:start' +} + +task :deploy => :environment do + # ... + # Deploy commands + # ... + invoke 'reboot:save_startup_script' +end +``` + +## Contributing + +1. [Fork it](https://github.com/localhots/mina_reboot/fork) +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create a new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..809eb56 --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +require "bundler/gem_tasks" + diff --git a/lib/mina/reboot.rb b/lib/mina/reboot.rb new file mode 100644 index 0000000..cf65570 --- /dev/null +++ b/lib/mina/reboot.rb @@ -0,0 +1,2 @@ +require "mina/reboot/version" +require "mina/reboot/save_startup_script" diff --git a/lib/mina/reboot/save_startup_script.rb b/lib/mina/reboot/save_startup_script.rb new file mode 100644 index 0000000..ee7ed7a --- /dev/null +++ b/lib/mina/reboot/save_startup_script.rb @@ -0,0 +1,28 @@ +require 'rake' + +namespace :reboot do + task :save_startup_script do + unless settings.on_reboot? + queue %| echo "-----> No reboot commands given" | + next + end + + script_path = "#{deploy_to}/#{shared_path}/startup.sh" + log_path = "#{deploy_to}/#{shared_path}/startup.log" + + script = "#!/bin/bash\n" + crontab_task = "@reboot #{script_path} > #{log_path} #minareboot" + + isolate do + fetch(:on_reboot).call + script << commands.join(" && \\\n") + end + + queue %| echo "-----> Updating startup script" | + queue! %| echo '#{script}' > #{script_path} | + queue! %| chmod +x #{script_path} | + + queue %| echo "-----> Updating @reboot cron task" | + queue! %| [[ $(crontab -l) != *minareboot* ]] && cat <(crontab -l) <(echo "#{crontab_task}") \| crontab - \|\| true | + end +end diff --git a/lib/mina/reboot/version.rb b/lib/mina/reboot/version.rb new file mode 100644 index 0000000..7352768 --- /dev/null +++ b/lib/mina/reboot/version.rb @@ -0,0 +1,3 @@ +module MinaReboot + VERSION = "0.1.0" +end diff --git a/mina_reboot.gemspec b/mina_reboot.gemspec new file mode 100644 index 0000000..b55c7dc --- /dev/null +++ b/mina_reboot.gemspec @@ -0,0 +1,23 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'mina/reboot/version' + +Gem::Specification.new do |spec| + spec.name = "mina_reboot" + spec.version = MinaReboot::VERSION + spec.authors = ["Gregory Eremin"] + spec.email = ["g@erem.in"] + spec.summary = %q{Generates cron @reboot script from Minafile} + spec.description = %q{Generates cron @reboot script from Minafile every time you deploy, so your app will start automatically in case of server reboot} + spec.homepage = "https://github.com/localhots/mina_reboot" + spec.license = "MIT" + + spec.files = `git ls-files -z`.split("\x0") + spec.executables = [] + spec.test_files = [] + spec.require_paths = ["lib"] + + spec.add_development_dependency "bundler", "~> 1.7" + spec.add_development_dependency "rake", "~> 10.0" +end