command_timer 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
19
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in command_timer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # CommandTimer
2
+
3
+ Execute commands on schedule.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'command_timer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install command_timer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bin/cmdtimer ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
4
+
5
+ require "optparse"
6
+ require "command_timer"
7
+
8
+ options = {}
9
+ opts = OptionParser.new do |opts|
10
+ opts.on("--version", "Display current version") do
11
+ puts "command_timer " + CommandTimer::VERSION
12
+ exit 0
13
+ end
14
+
15
+ opts.on("-c [COUNT_DOWN]", "Count down second") do |count_down|
16
+ options['count_down'] = count_down.to_i
17
+ end
18
+
19
+ opts.on("-s [NTP_SERVER]", "NTP Server") do |ntp_server|
20
+ options['ntp_server'] = ntp_server
21
+ end
22
+ end
23
+ opts.parse!
24
+
25
+ CommandTimer::Runner.new(ARGV[0], options).start
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/command_timer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["cctiger36"]
6
+ gem.email = ["cctiger36@gmail.com"]
7
+ gem.description = %q{Simple tool to execute commands on schedule}
8
+ gem.summary = %q{Execute commands on schedule}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "command_timer"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = CommandTimer::VERSION
17
+ end
@@ -0,0 +1,19 @@
1
+ require "command_timer/version"
2
+ require "command_timer/command"
3
+ require "command_timer/runner"
4
+ require "command_timer/parser"
5
+
6
+ module CommandTimer
7
+ class << self
8
+ def ntp_time(server)
9
+ server ||= 'ntp.nict.jp'
10
+ parse_time(%x[ntpdate -q #{server}].scan(/[0-9]{2}:[0-9]{2}:[0-9]{2}/).first)
11
+ end
12
+
13
+ def parse_time(str)
14
+ hms = str.split(':').map(&:to_i)
15
+ now = Time.now
16
+ Time.new(now.year, now.month, now.day, hms[0], hms[1], hms[2])
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module CommandTimer
2
+ class Command
3
+ attr_accessor :description
4
+ attr_accessor :content
5
+ attr_writer :burn_time
6
+
7
+ def burn_time
8
+ CommandTimer.parse_time(@burn_time)
9
+ end
10
+
11
+ def exec
12
+ echo_command
13
+ system @content
14
+ end
15
+
16
+ def echo_command
17
+ @content.split(';').each do |line|
18
+ puts ">> #{line.strip.gsub('\\', '')}"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ require "yaml"
2
+
3
+ module CommandTimer
4
+ class Parser
5
+ def self.parse(yml_path)
6
+ yml_path += ".yml" unless yml_path.end_with?('.yml')
7
+ yml = YAML.load_file(yml_path)
8
+ commands = []
9
+ yml.each do |cell|
10
+ data = cell[1]
11
+ command = Command.new
12
+ data.each do |k, v|
13
+ if k == 'content'
14
+ command.content = ''
15
+ v.each_line do |line|
16
+ command.content += line.strip
17
+ command.content += ';' unless command.content.end_with?(';')
18
+ end
19
+ else
20
+ command.send("#{k}=", v)
21
+ end
22
+ end
23
+ commands << command
24
+ end
25
+ commands
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,60 @@
1
+ module CommandTimer
2
+ class Runner
3
+ def initialize(yml, options = {})
4
+ @commands = Parser.parse(yml)
5
+ echo_commands
6
+ @count_down = options['count_down'] || 15
7
+ @ntp_server = options['ntp_server']
8
+ end
9
+
10
+ def start
11
+ @commands.each_with_index do |command|
12
+ count_down_and_burn_command(command)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def count_down_and_burn_command(command)
19
+ puts "Get ready to execute next command at #{command.burn_time}."
20
+ current_time = CommandTimer.ntp_time(@ntp_server)
21
+ left_sec = command.burn_time.to_i - current_time.to_i
22
+ if left_sec <= 0
23
+ puts "Time over."
24
+ return
25
+ end
26
+ loop do
27
+ if left_sec >= @count_down
28
+ current_time = CommandTimer.ntp_time(@ntp_server)
29
+ left_sec = command.burn_time.to_i - current_time.to_i
30
+ puts "Time resynced."
31
+ else
32
+ current_time += 1
33
+ left_sec -= 1
34
+ end
35
+ puts "[#{current_time}] Command will be executed in #{left_sec} second."
36
+ if left_sec < 1
37
+ break
38
+ else
39
+ if left_sec > @count_down
40
+ sleep(left_sec - @count_down)
41
+ else
42
+ sleep(1)
43
+ end
44
+ end
45
+ end
46
+ command.exec
47
+ end
48
+
49
+ def echo_commands
50
+ @commands.each_with_index do |command, index|
51
+ puts "--------------------"
52
+ puts "Command #{index + 1}"
53
+ puts command.description if command.description
54
+ puts "Will be executed at #{command.burn_time}"
55
+ command.echo_command
56
+ end
57
+ puts "--------------------"
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module CommandTimer
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,17 @@
1
+ command1:
2
+ description: メンテナンス開始
3
+ burn_time: "11:33:00"
4
+ content: |
5
+ cd ~/Documents/workspace/bm_gree/tower7
6
+ ls
7
+ command2:
8
+ description: デプロイ
9
+ burn_time: "14:10:00"
10
+ content: |
11
+ cap deploy ROLES=deploy
12
+ cap deploy\:admin ROLES=admin
13
+ command3:
14
+ description: メンテナンス終了
15
+ burn_time: "15:00:00"
16
+ content: |
17
+ cap maintenance\:nginx_end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: command_timer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - cctiger36
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-11 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple tool to execute commands on schedule
15
+ email:
16
+ - cctiger36@gmail.com
17
+ executables:
18
+ - cmdtimer
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - bin/cmdtimer
28
+ - command_timer.gemspec
29
+ - lib/command_timer.rb
30
+ - lib/command_timer/command.rb
31
+ - lib/command_timer/parser.rb
32
+ - lib/command_timer/runner.rb
33
+ - lib/command_timer/version.rb
34
+ - sample_commands.yml
35
+ homepage: ''
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.24
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Execute commands on schedule
59
+ test_files: []