rrunit 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,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rrunit.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/bin/rrunit ADDED
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
4
+ require 'rrunit'
5
+ require 'optparse'
6
+ require 'json'
7
+
8
+ options = {
9
+ }
10
+
11
+ OptionParser.new do |opts|
12
+ opts.banner = "Usage: rrunit -c <service-command>"
13
+
14
+ opts.on('-c', "--command <command>", "The service command") do |command|
15
+ options[:command] = command
16
+ end
17
+
18
+ opts.on('-l', "--logdir <logdir>", "The service logdir") do |logdir|
19
+ options[:log_dir] = logdir
20
+ end
21
+
22
+ opts.on('-o', "--outputdir <outputdir>", "The directory to output the service") do |output|
23
+ options[:output] = output
24
+ end
25
+
26
+ opts.on('-e', "--env <json>", "The environment variables") do |env|
27
+ options[:environment] = JSON.parse(env)
28
+ end
29
+
30
+ opts.on('-x', "--xenv <json>", "The exported environment variables") do |env|
31
+ options[:exported_environment] = JSON.parse(env)
32
+ end
33
+
34
+ opts.on('-a', "--args <quoted-string>", "The arguments to the service command") do |args|
35
+ options[:args] = args.split(' ')
36
+ end
37
+
38
+ end.parse!
39
+
40
+ unless options.key?(:command)
41
+ puts "You must at least specify a command"
42
+ exit(1)
43
+ end
44
+
45
+ output = options.delete(:output)
46
+ output = '/tmp' unless output
47
+ output = "#{output}/#{options[:name]}"
48
+
49
+ r = Rrunit::Service.new(options.delete(:command))
50
+ options.each do |key,value|
51
+ r.send(key, value)
52
+ end
53
+
54
+ r.write(output)
@@ -0,0 +1,94 @@
1
+ module Rrunit
2
+
3
+ class Service
4
+
5
+ attr_reader :name
6
+ attr_reader :command
7
+
8
+ def initialize(command)
9
+ @command = command
10
+ @name = File.basename(command)
11
+ @environment = Hash.new
12
+ @exported_environment = Hash.new
13
+ @args = Array.new
14
+ end
15
+
16
+ def args(*args)
17
+ @args = args unless args.empty?
18
+ args.empty? ? @args : self
19
+ end
20
+
21
+ def log_dir(log_dir=nil)
22
+ @log_dir = log_dir unless log_dir.nil?
23
+ log_dir.nil? ? @log_dir : self
24
+ end
25
+
26
+ def environment(env=nil)
27
+ @environment=env if env.is_a?(Hash)
28
+ env.nil? ? @environment : self
29
+ end
30
+
31
+ def exported_environment(env=nil)
32
+ @exported_environment=env if env.is_a?(Hash)
33
+ env.nil? ? @exported_environment : self
34
+ end
35
+
36
+ def env_var(value)
37
+ case
38
+ when value.is_a?(String)
39
+ "\"#{value}\""
40
+ else
41
+ value.to_s
42
+ end
43
+ end
44
+
45
+ def service
46
+ sv = Array.new
47
+ cmd = "exec #{command}"
48
+ cmd << " #{@args.join(' ')}" unless @args.empty?
49
+ sv << "#!/bin/bash"
50
+ @exported_environment.each do |key, value|
51
+ sv << "export #{key.to_s.upcase}=#{env_var(value)}"
52
+ end
53
+ @environment.each do |key, value|
54
+ sv << "#{key.to_s.upcase}=#{env_var(value)}"
55
+ end
56
+ sv << cmd
57
+ sv.join("\n")
58
+ end
59
+
60
+ def log_service
61
+ if @log_dir
62
+ sv = Array.new
63
+ cmd = "exec svlogd -tt #{@log_dir}"
64
+ sv << "#!/bin/bash"
65
+ sv << cmd
66
+ sv.join("\n")
67
+ end
68
+ end
69
+
70
+ def write(path)
71
+ @output_directory = path
72
+ FileUtils.mkdir_p @output_directory
73
+ File.open("#{@output_directory}/run", 'w') do |f|
74
+ f << service
75
+ f << "\n"
76
+ end
77
+ FileUtils.chmod 0755, "#{@output_directory}/run"
78
+ if @log_dir
79
+ FileUtils.mkdir_p "#{@output_directory}/log"
80
+ File.open("#{@output_directory}/log/run", 'w') do |f|
81
+ f << log_service
82
+ f << "\n"
83
+ end
84
+ FileUtils.chmod 0755, "#{@output_directory}/log/run"
85
+ end
86
+ end
87
+
88
+ def rm
89
+ FileUtils.rm_r @output_directory
90
+ end
91
+
92
+ end
93
+
94
+ end
@@ -0,0 +1,3 @@
1
+ module Rrunit
2
+ VERSION = "0.0.3"
3
+ end
data/lib/rrunit.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'fileutils'
2
+ require "rrunit/version"
3
+ require "rrunit/service"
4
+ #module Rrunit
5
+ # # Your code goes here...
6
+ #end
7
+ #
data/rrunit.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rrunit/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rrunit"
7
+ s.version = Rrunit::VERSION
8
+ s.authors = ["John Axel Eriksson"]
9
+ s.email = ["john@insane.se"]
10
+ s.homepage = "https://github.com/johnae/rrunit"
11
+ s.summary = %q{Creates and writes runit service files}
12
+ s.description = %q{Creates and writes runit service files}
13
+
14
+ s.rubyforge_project = "rrunit"
15
+
16
+ s.add_development_dependency "bundler", ">= 1.0.10"
17
+ s.add_development_dependency('rspec', '>= 2.6.0')
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,10 @@
1
+ rrunit_path = File.expand_path('./lib', File.dirname(__FILE__))
2
+ $:.unshift(rrunit_path) if File.directory?(rrunit_path) && !$:.include?(rrunit_path)
3
+
4
+ require 'rrunit'
5
+
6
+ RSpec.configure do |config|
7
+ config.mock_with :rspec
8
+ config.color_enabled = true
9
+ config.formatter = 'd'
10
+ end
@@ -0,0 +1,145 @@
1
+ require 'spec_helper'
2
+
3
+ module Rrunit
4
+ describe Service do
5
+
6
+ context "default settings" do
7
+
8
+ let(:runit_service) { Service.new('/usr/bin/service') }
9
+
10
+ it "has a name" do
11
+ runit_service.name.should == 'service'
12
+ end
13
+
14
+ it "has a command" do
15
+ runit_service.command.should == '/usr/bin/service'
16
+ end
17
+
18
+ it "has a basic runit run string" do
19
+ runit_service.service.should == ["#!/bin/bash","exec #{runit_service.command}"].join("\n")
20
+ end
21
+
22
+ end
23
+
24
+ context "with arguments" do
25
+
26
+ let(:runit_service) { Service.new('service').args('-arg1', 1, '-arg2', 2) }
27
+
28
+ it "has arguments for command" do
29
+ runit_service.args.should == ['-arg1', 1, '-arg2', 2]
30
+ end
31
+
32
+ it "has a runit run string with arguments" do
33
+ runit_service.service.should == ["#!/bin/bash","exec #{runit_service.command} -arg1 1 -arg2 2"].join("\n")
34
+ end
35
+
36
+ end
37
+
38
+ context "with environment" do
39
+
40
+ let(:runit_service) {
41
+ Service.new('service')
42
+ .environment(variable1: "127.0.0.1", variable2: "www.google.com", variable3: 100)
43
+ .exported_environment(evariable1: "exported")
44
+ }
45
+
46
+ it "has a runit run string with environment variables set" do
47
+ runit_service.service.should == ["#!/bin/bash", "export EVARIABLE1=\"exported\"", "VARIABLE1=\"127.0.0.1\"", "VARIABLE2=\"www.google.com\"", "VARIABLE3=100", "exec #{runit_service.command}"].join("\n")
48
+ end
49
+
50
+ end
51
+
52
+ context "with log_dir" do
53
+
54
+ let(:runit_service) {
55
+ Service.new('service')
56
+ .args('-arg1', 1, '-arg2', 2)
57
+ .log_dir('/tmp/somedir')
58
+ }
59
+
60
+ it "has a log_dir" do
61
+ runit_service.log_dir.should == '/tmp/somedir'
62
+ end
63
+
64
+ it "has a log_service" do
65
+ runit_service.log_service.should == ["#!/bin/bash","exec svlogd -tt #{runit_service.log_dir}"].join("\n")
66
+ end
67
+
68
+ end
69
+
70
+ context "when writing the files" do
71
+
72
+ let(:runit_output_directory) { "/tmp/runit_generator_spec_output_directory" }
73
+
74
+ before do
75
+ FileUtils.rm_rf(runit_output_directory)
76
+ runit_service.write(runit_output_directory)
77
+ end
78
+
79
+ after(:all) do
80
+ FileUtils.rm_rf(runit_output_directory)
81
+ end
82
+
83
+ context "without log dir" do
84
+
85
+ let(:runit_service) {
86
+ Service.new('service')
87
+ .args('-arg1', 1, '-arg2', 2)
88
+ .exported_environment(var1: 100, var2: "HELLO")
89
+ }
90
+
91
+ it "writes the runit configuration files to specified location" do
92
+ File.directory?(runit_output_directory).should be true
93
+ File.exists?("#{runit_output_directory}/run").should be true
94
+ end
95
+
96
+ it "the runit run file should be executable" do
97
+ File.stat("#{runit_output_directory}/run").executable?.should be true
98
+ end
99
+
100
+ it "the program run file should contain the service" do
101
+ File.read("#{runit_output_directory}/run").should == runit_service.service+"\n"
102
+ end
103
+
104
+ end
105
+
106
+ context "with log dir" do
107
+
108
+ let(:runit_service) {
109
+ Service.new('service')
110
+ .args('-arg1', 1, '-arg2', 2)
111
+ .exported_environment(var1: 100, var2: "HELLO")
112
+ .log_dir('/tmp/somedir')
113
+ }
114
+
115
+ it "writes the runit configuration files to specified location" do
116
+ File.directory?(runit_output_directory).should be true
117
+ File.exists?("#{runit_output_directory}/run").should be true
118
+ end
119
+
120
+ it "the runit run file should be executable" do
121
+ File.stat("#{runit_output_directory}/run").executable?.should be true
122
+ end
123
+
124
+ it "the runit run file should contain the service" do
125
+ File.read("#{runit_output_directory}/run").should == runit_service.service+"\n"
126
+ end
127
+
128
+ it "writes the runit log service to specified location" do
129
+ File.directory?("#{runit_output_directory}/log").should be true
130
+ File.exists?("#{runit_output_directory}/log/run").should be true
131
+ end
132
+
133
+ it "the runit log service run file should be executable" do
134
+ File.stat("#{runit_output_directory}/log/run").executable?.should be true
135
+ end
136
+
137
+ it "the runit log service run file should contain the log service" do
138
+ File.read("#{runit_output_directory}/log/run").should == runit_service.log_service+"\n"
139
+ end
140
+
141
+ end
142
+ end
143
+
144
+ end
145
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rrunit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Axel Eriksson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-23 00:00:00.000000000 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: &2164580600 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.10
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *2164580600
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &2164580100 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.6.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *2164580100
37
+ description: Creates and writes runit service files
38
+ email:
39
+ - john@insane.se
40
+ executables:
41
+ - rrunit
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - Rakefile
48
+ - bin/rrunit
49
+ - lib/rrunit.rb
50
+ - lib/rrunit/service.rb
51
+ - lib/rrunit/version.rb
52
+ - rrunit.gemspec
53
+ - spec/spec_helper.rb
54
+ - spec/unit/runit_service_spec.rb
55
+ has_rdoc: true
56
+ homepage: https://github.com/johnae/rrunit
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project: rrunit
76
+ rubygems_version: 1.6.2
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Creates and writes runit service files
80
+ test_files:
81
+ - spec/spec_helper.rb
82
+ - spec/unit/runit_service_spec.rb