Last active
December 2, 2016 14:48
-
-
Save csabahenk/06e295f69b81f6a74c005aab237f3db6 to your computer and use it in GitHub Desktop.
gluster volfile parser / extractor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'yaml' | |
class GlusterConfError < Exception | |
end | |
def assert x, *bool | |
bool.each { |b| raise GlusterConfError, "#{$.}: #{x}" unless bool } | |
end | |
volh = {} | |
vol = {} | |
$<.each { |l| | |
ls = l.split | |
case ls[0] | |
when "volume" | |
assert l, ls.size == 2, vol.empty? | |
vol["name"] = ls[1] | |
when "type" | |
assert l, ls.size == 2, vol["name"], !vol["type"] | |
vol["type"] = ls[1] | |
when "option" | |
assert l, ls.size == 3, vol["name"] | |
(vol["option"] ||= {})[ls[1]] = case ls[2] | |
when /\A\d+\Z/ | |
Integer ls[2] | |
when /\Aon|yes|true|enable\Z/i | |
true | |
when /\Aoff|no|false|disable\Z/i | |
false | |
else | |
ls[2] | |
end | |
when "subvolumes" | |
assert l, ls.size > 1, vol["name"], !vol["subvolumes"] | |
vol["subvolumes"] = ls[1..-1] | |
when "end-volume" | |
assert l, ls.size == 1, vol["name"], vol["type"] | |
volh[vol["name"]] = vol | |
vol = {} | |
when nil | |
else | |
assert l, false | |
end | |
} | |
subvolumes = [] | |
volh.each_value { |vol| subvolumes.concat(vol["subvolumes"] || []) } | |
topvols = volh.keys - subvolumes | |
assert "ambiguous toplevel volume: #{topvols.join(", ")}", topvols.size == 1 | |
topvol = volh[topvols[0]] | |
volh.each_value { |vol| (vol["subvolumes"] || []).map! { |w| volh[w] } } | |
puts topvol.to_yaml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment