diff options
| -rw-r--r-- | Rakefile | 13 | ||||
| -rw-r--r-- | rcm/conditions.rb | 37 | ||||
| -rw-r--r-- | rcm/file.rb | 33 | ||||
| -rw-r--r-- | rcm/rcm.rb | 27 |
4 files changed, 110 insertions, 0 deletions
diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..1a7ea43 --- /dev/null +++ b/Rakefile @@ -0,0 +1,13 @@ +require_relative 'rcm/rcm' + +task :default do |t| + rcm do + conditions do + hostname is :earth + end + + file '/etc/wg/wg0.conf' do + content 'the content' + end + end +end diff --git a/rcm/conditions.rb b/rcm/conditions.rb new file mode 100644 index 0000000..f90c74c --- /dev/null +++ b/rcm/conditions.rb @@ -0,0 +1,37 @@ +module RCM + # Conditions (e.g. run on host foo) + class Conditions + require 'socket' + + def initialize + @conds = {} + end + + def is(arg) + arg + end + + def method_missing(method_name, *args, &block) + @conds[method_name] = args.first + end + + def respond_to_missing? + true + end + + def met? + return false if @conds.key?(:hostname) && Socket.gethostname != @conds[:hostname].to_s + + true + end + end + + # Add conditions "keyword" to the DSL + class RCM + def conditions(&block) + conds = Conditions.new + conds.instance_eval(&block) + @conds_met = conds.met? + end + end +end diff --git a/rcm/file.rb b/rcm/file.rb new file mode 100644 index 0000000..2a25a07 --- /dev/null +++ b/rcm/file.rb @@ -0,0 +1,33 @@ +module RCM + # Managing files + class File + attr_reader :path + + def initialize(path) + @path = path + end + + def content(content = nil) + content.nil? ? @content : @content = content + end + + def to_s + @path + end + + def do! + puts "Evaluating #{self.class}:#{self}" + end + end + + # Add file keyword to the DSL + class RCM + def file(path, &block) + return unless @conds_met + + f = File.new(path) + f.instance_eval(&block) + self << f + end + end +end diff --git a/rcm/rcm.rb b/rcm/rcm.rb new file mode 100644 index 0000000..6c69d7b --- /dev/null +++ b/rcm/rcm.rb @@ -0,0 +1,27 @@ +require_relative 'conditions' +require_relative 'file' + +# Ruby Configiration Management system +module RCM + # Here all starts + class RCM + def initialize + @objs = [] + @conds_met = true + end + + def do! + @objs.each(&:do!) + end + + def <<(obj) + @objs << obj + end + end +end + +def rcm(&block) + rcm = RCM::RCM.new + rcm.instance_eval(&block) + rcm.do! +end |
