summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-12-06 23:04:47 +0200
committerPaul Buetow <paul@buetow.org>2024-12-06 23:04:47 +0200
commit7686fe830946ae36957501f1656bb429c694b09e (patch)
treea6ac996e4a7cbbdc054c2a62c7ae374b61ed49af
parentbfdcad7efca071374de0be3124dcc92deeab681e (diff)
add log module
-rw-r--r--Rakefile5
-rw-r--r--lib/autorequire/file.rb20
-rw-r--r--lib/autorequire/log.rb19
-rw-r--r--lib/rcm.rb11
4 files changed, 51 insertions, 4 deletions
diff --git a/Rakefile b/Rakefile
index 5122919..4e7f68b 100644
--- a/Rakefile
+++ b/Rakefile
@@ -7,8 +7,9 @@ task :wireguard do
dump_config
only_when { hostname is :earth }
- file '/etc/wg/wg0.conf' do
- content 'the content'
+ file '/tmp/test/wg/wg0.conf' do
+ create_parent
+ content 'the content is here'
end
end
end
diff --git a/lib/autorequire/file.rb b/lib/autorequire/file.rb
index 2a25a07..3233015 100644
--- a/lib/autorequire/file.rb
+++ b/lib/autorequire/file.rb
@@ -1,8 +1,13 @@
+require 'fileutils'
+require_relative 'log'
+
module RCM
# Managing files
class File
attr_reader :path
+ include Log
+
def initialize(path)
@path = path
end
@@ -11,12 +16,25 @@ module RCM
content.nil? ? @content : @content = content
end
+ def create_parent
+ @create_parent = true
+ end
+
def to_s
@path
end
def do!
- puts "Evaluating #{self.class}:#{self}"
+ dirname = ::File.dirname(@path)
+ if !::File.directory?(dirname) && @create_parent
+ info "Creating parent directory #{parent}"
+ FileUtils.mkdir_p(dirname)
+ end
+
+ info "Creating file #{@path}"
+ tmp_path = "#{@path}.tmp"
+ ::File.write(tmp_path, @content)
+ ::File.rename(tmp_path, @path)
end
end
diff --git a/lib/autorequire/log.rb b/lib/autorequire/log.rb
new file mode 100644
index 0000000..0fe9ea9
--- /dev/null
+++ b/lib/autorequire/log.rb
@@ -0,0 +1,19 @@
+require 'logger'
+
+module RCM
+ module Log
+ @@logger = Logger.new(STDOUT)
+
+ def info(message)
+ @@logger.info("#{self.class}(#{self}): #{message}")
+ end
+
+ def warn(message)
+ @@logger.warn("#{self.class}(#{self}): #{message}")
+ end
+
+ def debug(message)
+ @@logger.debug("#{self.class}(#{self}): #{message}")
+ end
+ end
+end
diff --git a/lib/rcm.rb b/lib/rcm.rb
index 0c3d028..d9880eb 100644
--- a/lib/rcm.rb
+++ b/lib/rcm.rb
@@ -2,15 +2,23 @@ Dir["#{Dir.pwd}/lib/autorequire/*.rb"].each { |m| require m }
# Ruby Configiration Management system
module RCM
-
# Here all starts
class RCM
+ @@rcm_counter = 0
+
include Config
include Options
+ include Log
def initialize
@objs = []
@conds_met = true
+ @@rcm_counter += 1
+ @number = @@rcm_counter
+ end
+
+ def to_s
+ "RCM #{@number}"
end
def do!
@@ -25,6 +33,7 @@ end
def make_it_so(&block)
rcm = RCM::RCM.new
+ rcm.info('Making it so...')
rcm.instance_eval(&block)
rcm.do!
end