Module: Profig
- Defined in:
- lib/profig/main.rb,
lib/profig/linux.rb,
lib/profig/config.rb,
lib/profig/install.rb
Defined Under Namespace
Class Method Summary collapse
-
.handle_cabal(pkg_name, opts) ⇒ Object
Package Installers.
- .handle_deb_set(pkgs) ⇒ Object
- .handle_dir(name, opts) ⇒ Object
- .handle_egg_set(eggs) ⇒ Object
- .handle_file(name, opts) ⇒ Object
- .handle_gem_set(gems) ⇒ Object
- .handle_group(group_name, opts) ⇒ Object
- .handle_iptables_set(iptables) ⇒ Object
-
.handle_rake(path, opts) ⇒ Object
Source Installers.
- .handle_rpm_set(rpms) ⇒ Object
- .handle_user(user_name, opts) ⇒ Object
- .handle_yum_set(rpms) ⇒ Object
- .load_config(path) ⇒ Object
- .load_config_directory(directory) ⇒ Object
- .run(sections) ⇒ Object
-
.run_item_handler(handler, items) ⇒ Object
Run all items one at a time.
- .split_owner(owner_str) ⇒ Object
- .yaml_items(yitems) ⇒ Object
- .yaml_sections(yaml) ⇒ Object
- .yaml_to_sections(yaml) ⇒ Object
Class Method Details
.handle_cabal(pkg_name, opts) ⇒ Object
Package Installers
7 8 9 10 |
# File 'lib/profig/install.rb', line 7 def self.handle_cabal(pkg_name, opts) cmd = "cabal install #{package_name}" system cmd end |
.handle_deb_set(pkgs) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/profig/install.rb', line 12 def self.handle_deb_set(pkgs) cmd = "apt-get install -y" pkgs.each do |pkg| cmd += ' '+ pkg.name end system cmd end |
.handle_dir(name, opts) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/profig/linux.rb', line 45 def self.handle_dir(name, opts) owner, group = split_owner(opts['owner']) mode = opts['mode'] FileUtils.mkdir_p(name) FileUtils.chown(owner, group, name) FileUtils.chmod(mode, name) if mode end |
.handle_egg_set(eggs) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/profig/install.rb', line 20 def self.handle_egg_set(eggs) cmd = "pip-python install" eggs.each do |egg| cmd += ' '+ egg.name end system cmd end |
.handle_file(name, opts) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/profig/linux.rb', line 31 def self.handle_file(name, opts) src = opts['source'] owner, group = split_owner(opts['owner']) mode = opts['mode'] raise 'nil filename' if not name raise "nil file source for #{name}" if not src FileUtils.copy(src, name) FileUtils.chown(owner, group, name) FileUtils.chmod(mode, name) if mode end |
.handle_gem_set(gems) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/profig/install.rb', line 28 def self.handle_gem_set(gems) cmd = "gem install" gems.each do |gem| cmd += ' '+ gem.name end system cmd end |
.handle_group(group_name, opts) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/profig/linux.rb', line 21 def self.handle_group(group_name, opts) if not group_name.instance_of? String raise 'Unknown group definition' end cmd = "groupadd --force --system #{group_name}" system cmd end |
.handle_iptables_set(iptables) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/profig/linux.rb', line 74 def self.handle_iptables_set(iptables) system 'iptables -F' iptables.each do |cfg| system "iptables #{cfg}" end system "/sbin/service iptables save" end |
.handle_rake(path, opts) ⇒ Object
Source Installers
51 52 53 54 55 56 57 58 |
# File 'lib/profig/install.rb', line 51 def self.handle_rake(path, opts) pwd = Dir.pwd Dir.chdir path target = opts['target'] cmd = "rake #{target}" system cmd Dir.chdir pwd end |
.handle_rpm_set(rpms) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/profig/install.rb', line 36 def self.handle_rpm_set(rpms) cmd = "yum install -y" rpms.each do |rpm| cmd += ' '+ rpm.name end system cmd end |
.handle_user(user_name, opts) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/profig/linux.rb', line 7 def self.handle_user(user_name, opts) if not user_name.instance_of? String raise 'Unknown user type' elsif not /[a-zA-Z][a-zA-Z0-9_]+/.match user_name raise 'Invalid user format' end cmd = "useradd --no-user-group --system --no-create-home #{user_name}" system cmd if not [0,9].include?($?.exitstatus) then raise "Error creating user (#{$?.exitstatus}): #{user_name}" end end |
.handle_yum_set(rpms) ⇒ Object
44 45 46 |
# File 'lib/profig/install.rb', line 44 def self.handle_yum_set(rpms) self.handle_rpm_set(rpms) end |
.load_config(path) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/profig/config.rb', line 95 def self.load_config(path) if File.file? path then confs = [YAML.load_file(path)] elsif File.directory? path then confs = load_config_directory(path) end return yaml_to_sections(confs) end |
.load_config_directory(directory) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/profig/config.rb', line 42 def self.load_config_directory(directory) # do a stable ordering by alphabetical files = Dir.glob("#{directory}/*").sort confs = [] files.each do |f| if File.file?(f) confs << YAML.load_file(f) end end return confs end |
.run(sections) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/profig/main.rb', line 15 def self.run(sections) users_groups = `id -G`.split() if not users_groups.include? "0" # this should be made an exception at some point puts "You must be root to run profig." exit end sections.each do |section| type = section.type section_handler_name = "handle_#{type}_set" item_handler_name = "handle_#{type}" # first check if there's a section handler for this type # a section handler takes all items at once if Profig.respond_to? section_handler_name handler = method(section_handler_name) handler.call(section.items) # second, check if tere's a handler for individual items # this type of handler will process items one at a time elsif Profig.respond_to? item_handler_name handler = method(item_handler_name) run_item_handler(handler, section.items) # if no handlers, complain about it and move on else puts "No #{type} handlers for #{os}" next end end end |
.run_item_handler(handler, items) ⇒ Object
Run all items one at a time
8 9 10 11 12 |
# File 'lib/profig/main.rb', line 8 def self.run_item_handler(handler, items) items.each do |i| handler.call(i.name, i.opts) end end |
.split_owner(owner_str) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/profig/linux.rb', line 55 def self.split_owner(owner_str) if owner_str.nil? return nil, nil end split_owner = owner_str.split(':') owner, group = nil, nil if split_owner.length == 1 owner = split_owner[0] elsif split_owner[0].length == 0 group = split_owner[1] else owner = split_owner[0] group = split_owner[1] end return owner, group end |
.yaml_items(yitems) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/profig/config.rb', line 55 def self.yaml_items(yitems) yitems.each do |item| if item.instance_of? String name = item opts = nil else name = item.keys[0] opts = item[name] end yield name, opts end end |
.yaml_sections(yaml) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/profig/config.rb', line 68 def self.yaml_sections(yaml) yaml.each do |doc| if not doc.has_key? 'profig' raise 'Invalid configuration format' end doc['profig'].each do |yaml_section| type = yaml_section.keys[0] items = yaml_section[type] yield type, items end end end |
.yaml_to_sections(yaml) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/profig/config.rb', line 82 def self.yaml_to_sections(yaml) section_acc = [] yaml_sections(yaml) do |type, yitems| section = Section.new(type) yaml_items(yitems) do |name, opts| section << Item.new(name, opts).freeze end section.freeze section_acc << section end return section_acc end |