profig 0.01

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ require 'profig/config'
6
+ require 'profig/main'
7
+
8
+
9
+ def parse_args()
10
+ options = {}
11
+ OptionParser.new do |opts|
12
+ opts.banner = 'Usage: profig'
13
+
14
+ opts.on('-d', '--directory DIR', 'Select directory') do |d|
15
+ options[:directory] = d
16
+ end
17
+ end.parse!
18
+
19
+ return options, ARGV
20
+ end
21
+
22
+ def main()
23
+ opts, args = parse_args()
24
+ p opts
25
+ p args
26
+
27
+ os = :ubuntu
28
+ dir = opts[:directory]
29
+ dir = '/var/lib/profig/conf.d' if dir.nil?
30
+
31
+ config = Profig.load_config(dir)
32
+ Profig.run(os, config)
33
+ end
34
+
35
+
36
+ main()
@@ -0,0 +1,46 @@
1
+ require 'yaml'
2
+
3
+ module Profig
4
+
5
+
6
+ class Resource
7
+ attr_reader :name
8
+ attr_reader :opts
9
+ end
10
+
11
+
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
22
+ end
23
+ end
24
+ return merge
25
+ end
26
+
27
+
28
+ def self.load_config_yaml(directory)
29
+ files = Dir.glob("#{directory}/*")
30
+ confs = []
31
+ files.each do |f|
32
+ if File.file?(f)
33
+ confs << YAML.load_file(f)
34
+ end
35
+ end
36
+ return confs
37
+ end
38
+
39
+
40
+ def self.load_config(directory)
41
+ confs = load_config_yaml(directory)
42
+ return merge_config(confs)
43
+ end
44
+
45
+
46
+ end
@@ -0,0 +1,9 @@
1
+
2
+ module Profig
3
+
4
+ def self.handle_debian_deb(pkg_name, opts)
5
+ cmd = "apt-get install -y #{pkg_name}"
6
+ system cmd
7
+ end
8
+
9
+ end # module
@@ -0,0 +1,35 @@
1
+
2
+ require 'fileutils'
3
+
4
+ module Profig
5
+
6
+
7
+ def self.handle_linux_dir(name, opts)
8
+ 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
17
+ end
18
+
19
+
20
+ def self.split_owner(owner_str)
21
+ split_owner = owner_str.split(':')
22
+ owner, group = nil, nil
23
+ if split_owner.length == 1
24
+ owner = split_owner[0]
25
+ elsif split_owner[0].length == 0
26
+ group = split_owner[1]
27
+ else
28
+ owner = split_owner[0]
29
+ group = split_owner[1]
30
+ end
31
+ return owner, group
32
+ end
33
+
34
+
35
+ end # module
@@ -0,0 +1,43 @@
1
+ require 'profig/ubuntu'
2
+
3
+
4
+ module Profig
5
+
6
+
7
+ def self.run_handler(handler, items)
8
+ 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)
17
+ end
18
+ end
19
+
20
+
21
+ def self.run(os, config)
22
+ users_groups = `id -G`.split()
23
+ if not users_groups.include? "0"
24
+ # this should be made an exception at some point
25
+ puts "You must be root to run profig."
26
+ exit
27
+ end
28
+
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}"
34
+ next
35
+ end
36
+
37
+ handler = method(handler_name)
38
+ run_handler(handler, items)
39
+ end
40
+ end
41
+
42
+
43
+ end # module
@@ -0,0 +1,37 @@
1
+ require 'profig/linux'
2
+ require 'profig/debian'
3
+
4
+ module Profig
5
+
6
+
7
+ def self.handle_ubuntu_group(group_name, opts)
8
+ if not group_name.instance_of? String
9
+ raise 'Unknown group definition'
10
+ end
11
+
12
+ cmd = "addgroup -q --system #{group_name}"
13
+ system(cmd)
14
+ end
15
+
16
+ def self.handle_ubuntu_user(user_name, opts)
17
+ if not user_name.instance_of? String
18
+ raise 'Unknown user type'
19
+ elsif not /[a-zA-Z][a-zA-Z0-9_]+/.match user_name
20
+ raise 'Invalid user format'
21
+ end
22
+
23
+ cmd = "adduser -q --system --no-create-home"
24
+ cmd += " --disabled-password --disabled-login #{user_name}"
25
+ system cmd
26
+ end
27
+
28
+ def self.handle_ubuntu_deb(pkg_name, opts)
29
+ Profig.handle_debian_deb(pkg_name, opts)
30
+ end
31
+
32
+ def self.handle_ubuntu_dir(name, opts)
33
+ Profig.handle_linux_dir(name, opts)
34
+ end
35
+
36
+
37
+ end # module
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: profig
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.01"
10
+ platform: ruby
11
+ authors:
12
+ - Matthew Graham
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-23 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Push-modeled system configuration manager with limited feature set
22
+ email:
23
+ - mdg149@gmail.com
24
+ executables:
25
+ - profig
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - bin/profig
32
+ - lib/profig/debian.rb
33
+ - lib/profig/ubuntu.rb
34
+ - lib/profig/config.rb
35
+ - lib/profig/main.rb
36
+ - lib/profig/linux.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/mdg/profig
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Limited system configuration manager
71
+ test_files: []
72
+