profig 0.01 → 0.02

Sign up to get free protection for your applications and to get access to all the features.
data/lib/profig/config.rb CHANGED
@@ -3,30 +3,40 @@ require 'yaml'
3
3
  module Profig
4
4
 
5
5
 
6
- class Resource
6
+ # An individual piece of configuration
7
+ class Item
7
8
  attr_reader :name
8
9
  attr_reader :opts
10
+
11
+ def initialize(name, opts)
12
+ @name = name
13
+ @opts = opts
14
+ end
9
15
  end
10
16
 
17
+ # A group of configuration items that are of the same type and should
18
+ # be handled at the same time
19
+ class Section
20
+ attr_reader :type
21
+ attr_reader :items
11
22
 
12
- # Take an array of configs, and merge them into a single list
13
- def self.merge_config(confs)
14
- merge = {}
15
- confs.each do |c|
16
- c.each do |name, items|
17
- if merge.has_key? name:
18
- merge[name].concat(items)
19
- else
20
- merge[name] = items
21
- end
23
+ def initialize(type)
24
+ @type = type
25
+ @items = []
26
+ end
27
+
28
+ def <<(item)
29
+ if not item.instance_of? Item
30
+ raise 'Unknown item type'
22
31
  end
32
+ @items << item
23
33
  end
24
- return merge
25
34
  end
26
35
 
27
36
 
28
37
  def self.load_config_yaml(directory)
29
- files = Dir.glob("#{directory}/*")
38
+ # do a stable ordering by alphabetical
39
+ files = Dir.glob("#{directory}/*").sort
30
40
  confs = []
31
41
  files.each do |f|
32
42
  if File.file?(f)
@@ -37,9 +47,50 @@ def self.load_config_yaml(directory)
37
47
  end
38
48
 
39
49
 
50
+ def self.yaml_items(yitems)
51
+ yitems.each do |item|
52
+ if item.instance_of? String
53
+ name = item
54
+ opts = nil
55
+ else
56
+ name = item.keys[0]
57
+ opts = item[name]
58
+ end
59
+ yield name, opts
60
+ end
61
+ end
62
+
63
+ def self.yaml_sections(yaml)
64
+ yaml.each do |doc|
65
+ if not doc.has_key? 'profig'
66
+ raise 'Invalid configuration format'
67
+ end
68
+
69
+ doc['profig'].each do |yaml_section|
70
+ type = yaml_section.keys[0]
71
+ items = yaml_section[type]
72
+ yield type, items
73
+ end
74
+ end
75
+ end
76
+
77
+ def self.yaml_to_sections(yaml)
78
+ section_acc = []
79
+ yaml_sections(yaml) do |type, yitems|
80
+ section = Section.new(type)
81
+ yaml_items(yitems) do |name, opts|
82
+ section << Item.new(name, opts).freeze
83
+ end
84
+ section.freeze
85
+ section_acc << section
86
+ end
87
+ return section_acc
88
+ end
89
+
90
+
40
91
  def self.load_config(directory)
41
92
  confs = load_config_yaml(directory)
42
- return merge_config(confs)
93
+ return yaml_to_sections(confs)
43
94
  end
44
95
 
45
96
 
data/lib/profig/linux.rb CHANGED
@@ -4,20 +4,32 @@ require 'fileutils'
4
4
  module Profig
5
5
 
6
6
 
7
+ def self.handle_linux_file(name, opts)
8
+ src = opts['source']
9
+ owner, group = split_owner(opts['owner'])
10
+ mode = opts['mode']
11
+
12
+ FileUtils.copy(src, name)
13
+ FileUtils.chown(owner, group, name)
14
+ FileUtils.chmod(mode, name) if mode
15
+ end
16
+
17
+
7
18
  def self.handle_linux_dir(name, opts)
19
+ owner, group = split_owner(opts['owner'])
20
+ mode = opts['mode']
21
+
8
22
  FileUtils.mkdir_p(name)
9
- if opts.has_key? 'owner'
10
- owner, group = split_owner(opts['owner'])
11
- FileUtils.chown(owner, group, name)
12
- end
13
- if opts.has_key? 'mode'
14
- mode = opts['mode']
15
- FileUtils.chmod(mode, name)
16
- end
23
+ FileUtils.chown(owner, group, name)
24
+ FileUtils.chmod(mode, name) if mode
17
25
  end
18
26
 
19
27
 
20
28
  def self.split_owner(owner_str)
29
+ if owner_str.nil?
30
+ return nil, nil
31
+ end
32
+
21
33
  split_owner = owner_str.split(':')
22
34
  owner, group = nil, nil
23
35
  if split_owner.length == 1
data/lib/profig/main.rb CHANGED
@@ -4,21 +4,15 @@ require 'profig/ubuntu'
4
4
  module Profig
5
5
 
6
6
 
7
- def self.run_handler(handler, items)
7
+ # Run all items one at a time
8
+ def self.run_item_handler(handler, items)
8
9
  items.each do |i|
9
- if i.instance_of? String
10
- name = i
11
- opts = nil
12
- else
13
- name = i.keys[0]
14
- opts = i[name]
15
- end
16
- handler.call(name, opts)
10
+ handler.call(i.name, i.opts)
17
11
  end
18
12
  end
19
13
 
20
14
 
21
- def self.run(os, config)
15
+ def self.run(os, sections)
22
16
  users_groups = `id -G`.split()
23
17
  if not users_groups.include? "0"
24
18
  # this should be made an exception at some point
@@ -26,16 +20,27 @@ def self.run(os, config)
26
20
  exit
27
21
  end
28
22
 
29
- config.each do |name, items|
30
- handler_name = "handle_#{os}_#{name}"
31
-
32
- if not Profig.respond_to? handler_name
33
- puts "No #{name} handler for #{os}"
23
+ sections.each do |section|
24
+ type = section.type
25
+ section_handler_name = "handle_#{type}_section_for_#{os}"
26
+ #item_handler_name = "handle_#{type}_item_for_#{os}"
27
+ item_handler_name = "handle_#{os}_#{type}"
28
+
29
+ # first check if there's a section handler for this type
30
+ # a section handler takes all items at once
31
+ if Profig.respond_to? section_handler_name
32
+ handler = method(section_handler_name)
33
+ handler.call(section.items)
34
+ # second, check if tere's a handler for individual items
35
+ # this type of handler will process items one at a time
36
+ elsif Profig.respond_to? item_handler_name
37
+ handler = method(item_handler_name)
38
+ run_item_handler(handler, section.items)
39
+ # if no handlers, complain about it and move on
40
+ else
41
+ puts "No #{type} handlers for #{os}"
34
42
  next
35
43
  end
36
-
37
- handler = method(handler_name)
38
- run_handler(handler, items)
39
44
  end
40
45
  end
41
46
 
data/lib/profig/ubuntu.rb CHANGED
@@ -33,5 +33,9 @@ def self.handle_ubuntu_dir(name, opts)
33
33
  Profig.handle_linux_dir(name, opts)
34
34
  end
35
35
 
36
+ def self.handle_ubuntu_file(name, opts)
37
+ Profig.handle_linux_file(name, opts)
38
+ end
39
+
36
40
 
37
41
  end # module
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: profig
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.01"
8
+ - 2
9
+ version: "0.02"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matthew Graham
@@ -18,7 +18,7 @@ date: 2010-09-23 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
21
- description: Push-modeled system configuration manager with limited feature set
21
+ description: A YAML-based system configuration manager with no central server
22
22
  email:
23
23
  - mdg149@gmail.com
24
24
  executables:
@@ -67,6 +67,6 @@ rubyforge_project:
67
67
  rubygems_version: 1.3.7
68
68
  signing_key:
69
69
  specification_version: 3
70
- summary: Limited system configuration manager
70
+ summary: A YAML-based system configuration manager
71
71
  test_files: []
72
72