itrigga-generator 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem "hashie"
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.6.4"
11
+ gem "rcov", ">= 0"
12
+ gem "rspec", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ hashie (1.1.0)
7
+ jeweler (1.6.4)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rake (0.8.7)
12
+ rcov (0.9.9)
13
+ rspec (2.5.0)
14
+ rspec-core (~> 2.5.0)
15
+ rspec-expectations (~> 2.5.0)
16
+ rspec-mocks (~> 2.5.0)
17
+ rspec-core (2.5.1)
18
+ rspec-expectations (2.5.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.5.0)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.0.0)
27
+ hashie
28
+ jeweler (~> 1.6.4)
29
+ rcov
30
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Anson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = itrigga-generator
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to itrigga-generator
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Anson. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ $LOAD_PATH.unshift "lib"
7
+ require 'itrigga-generator'
8
+
9
+ begin
10
+ Bundler.setup(:default, :development)
11
+ rescue Bundler::BundlerError => e
12
+ $stderr.puts e.message
13
+ $stderr.puts "Run `bundle install` to install missing gems"
14
+ exit e.status_code
15
+ end
16
+ require 'rake'
17
+
18
+ load 'tasks/generator.rake'
19
+
20
+ require 'jeweler'
21
+ Jeweler::Tasks.new do |gem|
22
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
23
+ gem.name = "itrigga-generator"
24
+ gem.homepage = "http://gems.itrigga.com/itrigga-generator"
25
+ gem.license = "MIT"
26
+ gem.summary = %Q{Generates config files from templates}
27
+ gem.description = %Q{Generates config files from templates using information from a config yaml file}
28
+ gem.email = "ansonkelly@itrigga.com"
29
+ gem.authors = ["Anson Kelly"]
30
+ # dependencies defined in Gemfile
31
+ gem.require_paths << "lib"
32
+ end
33
+ Jeweler::RubygemsDotOrgTasks.new
34
+
35
+ require 'rake/testtask'
36
+ Rake::TestTask.new(:test) do |test|
37
+ test.libs << 'lib' << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ end
41
+
42
+ require 'rcov/rcovtask'
43
+ Rcov::RcovTask.new do |test|
44
+ test.libs << 'test'
45
+ test.pattern = 'test/**/test_*.rb'
46
+ test.verbose = true
47
+ test.rcov_opts << '--exclude "gems/*"'
48
+ end
49
+
50
+ task :default => :test
51
+
52
+ require 'rake/rdoctask'
53
+ Rake::RDocTask.new do |rdoc|
54
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
55
+
56
+ rdoc.rdoc_dir = 'rdoc'
57
+ rdoc.title = "itrigga-generator #{version}"
58
+ rdoc.rdoc_files.include('README*')
59
+ rdoc.rdoc_files.include('lib/**/*.rb')
60
+ end
61
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1 @@
1
+ Dir[File.join(File.dirname(File.expand_path(__FILE__)),"itrigga-generator","*.rb")].each {|f| require f }
@@ -0,0 +1,23 @@
1
+ module ITrigga
2
+ module Generator
3
+
4
+ def self.std_alert(max_memory = 1000)
5
+ "if totalmem is greater than #{max_memory} Mb for 5 cycles then alert
6
+ if cpu is greater than 50% for 4 cycles then alert
7
+ if cpu is greater than 80% for 6 cycles then restart
8
+ if loadavg(5min) greater than 10 for 8 cycles then restart"
9
+ end
10
+
11
+ def self.disks
12
+ @devices ||= `df | grep ^/dev/`
13
+ disks = {}
14
+ @devices.split("\n").each{ |l|
15
+ cols = l.split("\s")
16
+ # note: on mac, we can get spaces in filenames. Meh...
17
+ disks[cols.first] = cols[5..(cols.size-1)].join(" ")
18
+ }
19
+ disks
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,126 @@
1
+ require 'erb'
2
+ require 'hashie'
3
+
4
+ module ITrigga
5
+ module Generator
6
+ class Generator
7
+
8
+ attr_accessor :opts, :config
9
+
10
+ #
11
+ # Opts: (* is required)
12
+ # :template_dir* - where to look for the .erb templates
13
+ # :output_dir* - where to put the generated files
14
+ # :config_file* - the yml config file that provides the hash of values passed to each template. Also the list of templates to render
15
+ # :pattern - the file pattern name to be used for the template (eg which templates to render)
16
+ #
17
+ # All other params are made available to the templates
18
+ #
19
+ # All template files must end in .config
20
+ #
21
+ def initialize(opts = {})
22
+
23
+ raise "Config file does not exist / not accessible" unless (opts[:config_file] && File.exist?(opts[:config_file]) && File.readable?(opts[:config_file])) || opts[:config_file] == false
24
+ raise "Template dir does not exist / not accessible" unless (opts[:template_dir] && File.exist?(opts[:template_dir]) && File.readable?(opts[:template_dir]))
25
+ raise "Output dir does not exist / not accessible" unless (opts[:output_dir] && File.exist?(opts[:output_dir]) && File.writable?(opts[:output_dir]))
26
+
27
+
28
+ # load and parse the config file
29
+ if opts[:config_file] == false # we dont want to use a config file
30
+ @config = Hashie::Mash.new
31
+ else
32
+ begin
33
+ config = YAML::load(File.open(opts[:config_file],"r"))
34
+
35
+ if config.kind_of?(Hash)
36
+ @config = Hashie::Mash.new(config)
37
+ else
38
+ raise "Config file #{opts[:config_file]} did not serialize into a hash"
39
+ end
40
+ rescue Exception => e
41
+ raise "Could not parse config file. Is is YAML format?. Exception was: #{e}"
42
+ return false
43
+ end
44
+ end
45
+
46
+ # if the pattern is not given set a default
47
+ opts[:pattern] ||= ".*"
48
+
49
+ @opts = Hashie::Mash.new(opts)
50
+ include_constants
51
+
52
+ end
53
+
54
+ def generate
55
+
56
+ # TODO : we merge in the entire config file mainly for the ssh monitoring scripts (need server dns & ports etc). Once config files are refactored this should change also
57
+ if config.processes_to_start
58
+ config.processes_to_start.each do |process|
59
+ template_file_path = template_file(process)
60
+ generate_template( template_file_path, config.merge(config.processes[process] || {}).merge(opts))
61
+ end
62
+ else
63
+ # we dont have a config file to tell use which templates to render, so we render all templates that match the extension
64
+ files_in_template_dir.each {|f| generate_template( f, opts) }
65
+ end
66
+
67
+ end
68
+
69
+ #
70
+ # Returns the filenames of all the files in the template_dir that match the given regex
71
+ #
72
+ def files_in_template_dir(regex = opts.pattern)
73
+ Dir[File.join(File.expand_path(opts.template_dir),"*.config")].reject{|f| File.basename(f).to_s.match(/.inactive$/) }.select{|f| File.basename(f).to_s.match(Regexp.new(regex.to_s)) }
74
+ end
75
+
76
+ #
77
+ # Returns the absolute path to the template file
78
+ #
79
+ def template_file(filename)
80
+ files_in_template_dir.find{|f| File.basename(f).match(/^#{filename}/)}
81
+ end
82
+
83
+ #
84
+ # Returns the absolute path to the output file (which is the template file name without the .config)
85
+ #
86
+ def output_file(filename)
87
+ File.expand_path( File.join(opts.output_dir,filename.gsub(".config","")) )
88
+ end
89
+
90
+ #
91
+ # Includes the output from all the methods defined in the constants.rb file into the hash to be available to the templates
92
+ #
93
+ def include_constants
94
+ ITrigga::Generator.public_methods.select{|m| ITrigga::Generator.method(m).owner.to_s.match(/ITrigga::Generator/) }.each do |m|
95
+ opts[m] = ITrigga::Generator.send(m)
96
+ end
97
+ end
98
+
99
+ #
100
+ # Generates the output file from the template file
101
+ #
102
+ def generate_template(template_file_path, template_variables = {})
103
+
104
+ begin
105
+
106
+ # load template
107
+ template = ERB.new( File.read(template_file_path) )
108
+
109
+ # render erb
110
+ rendered_template = template.result(template_variables.send("binding"))
111
+
112
+ # write output file
113
+ File.open( output_file(File.basename(template_file_path)), "w" ){|f| f.write(rendered_template) }
114
+
115
+ rescue Exception => e
116
+ puts "Exception when rendering template '#{template_file_path}'"
117
+ puts "Exception: #{e}"
118
+ puts "template variables: #{template_variables.inspect}"
119
+ puts e.backtrace
120
+ throw e
121
+ end
122
+ end
123
+
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,22 @@
1
+ Dir[File.join(File.dirname(File.expand_path(__FILE__)),"..","itrigga-generator","*.rb")].each {|f| require f }
2
+
3
+ namespace :itrigga do
4
+
5
+ desc "Generates files"
6
+ task :generator do
7
+
8
+ raise "Must enter an config file" unless ENV['CONFIG_FILE']
9
+
10
+ # Example rake calls
11
+ # rake itrigga:generator PATTERN=monit TEMPLATE_DIR=/tmp/test/templates OUTPUT_DIR=/tmp/test/output CONFIG_FILE=/tmp/test/test.yml BASE_DIR=/var/www/itrigga/shared
12
+ #
13
+ # rake itrigga:generator PATTERN=logrotate TEMPLATE_DIR=/tmp/test/templates OUTPUT_DIR=/tmp/test/output CONFIG_FILE=false BASE_DIR=/var/www/itrigga/shared
14
+
15
+ # convert all ENV params into a hash with symbolised keys
16
+ args = ENV.inject({}){|h,e| h[e[0].to_s.downcase.to_sym] = (e[1].to_s == "false" ? false : e[1]);h; }
17
+
18
+ generator = ITrigga::Generator::Generator.new args
19
+ generator.generate
20
+
21
+ end
22
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'itrigga-generator'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestItriggaGenerator < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: itrigga-generator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - Anson Kelly
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-04 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ name: hashie
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ requirement: *id001
34
+ type: :runtime
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ name: bundler
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 23
44
+ segments:
45
+ - 1
46
+ - 0
47
+ - 0
48
+ version: 1.0.0
49
+ requirement: *id002
50
+ type: :development
51
+ - !ruby/object:Gem::Dependency
52
+ prerelease: false
53
+ name: jeweler
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 7
60
+ segments:
61
+ - 1
62
+ - 6
63
+ - 4
64
+ version: 1.6.4
65
+ requirement: *id003
66
+ type: :development
67
+ - !ruby/object:Gem::Dependency
68
+ prerelease: false
69
+ name: rcov
70
+ version_requirements: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirement: *id004
80
+ type: :development
81
+ - !ruby/object:Gem::Dependency
82
+ prerelease: false
83
+ name: rspec
84
+ version_requirements: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirement: *id005
94
+ type: :development
95
+ description: Generates config files from templates using information from a config yaml file
96
+ email: ansonkelly@itrigga.com
97
+ executables: []
98
+
99
+ extensions: []
100
+
101
+ extra_rdoc_files:
102
+ - LICENSE.txt
103
+ - README.rdoc
104
+ files:
105
+ - .document
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE.txt
109
+ - README.rdoc
110
+ - Rakefile
111
+ - VERSION
112
+ - lib/itrigga-generator.rb
113
+ - lib/itrigga-generator/constants.rb
114
+ - lib/itrigga-generator/generator.rb
115
+ - lib/tasks/generator.rake
116
+ - test/helper.rb
117
+ - test/test_itrigga-generator.rb
118
+ has_rdoc: true
119
+ homepage: http://gems.itrigga.com/itrigga-generator
120
+ licenses:
121
+ - MIT
122
+ post_install_message:
123
+ rdoc_options: []
124
+
125
+ require_paths:
126
+ - lib
127
+ - lib
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ hash: 3
135
+ segments:
136
+ - 0
137
+ version: "0"
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ hash: 3
144
+ segments:
145
+ - 0
146
+ version: "0"
147
+ requirements: []
148
+
149
+ rubyforge_project:
150
+ rubygems_version: 1.3.7
151
+ signing_key:
152
+ specification_version: 3
153
+ summary: Generates config files from templates
154
+ test_files: []
155
+