Module: AnsibleMakeRole
- Defined in:
- lib/ansible_make_role.rb,
lib/ansible_make_role/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- ROLE_FILE_NAME =
"role.yml"- DEFAULT_FORCE_FLAG =
false- DEFAULT_GIT_FLAG =
false- VERSION =
"0.7.6"
Class Method Summary collapse
- .clean(role_dir) ⇒ Object
-
.compile_role(source, target) ⇒ Object
source is a single-file role and target is the role directory.
- .force ⇒ Object
- .force=(value) ⇒ Object
- .git ⇒ Object
- .git=(value) ⇒ Object
- .make(role_dir) ⇒ Object
-
.unindent(lines) ⇒ Object
Unindent lines by the indentation of the first non-comment and non-blank line.
-
.wrap_system_call_error(&block) ⇒ Object
Turn a SystemCallError into a AnsibleMakeRole::Error exception and remove Ruby reference from message (eg. “@ rb_sysopen”).
Class Method Details
.clean(role_dir) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ansible_make_role.rb', line 40 def self.clean(role_dir) changed = false wrap_system_call_error { File.exists?("#{role_dir}/#{ROLE_FILE_NAME}") or raise Error "Not a role directory: #{role_dir}" for file in Dir["#{role_dir}/*/main.yml"] FileUtils.rm(file) dir = File.dirname(file) FileUtils.rmdir(dir) if File.empty?(dir) changed = true end git_file = "#{role_dir}/.gitignore" if git && File.exist?(git_file) FileUtils.rm(git_file) changed = true end } return changed end |
.compile_role(source, target) ⇒ Object
source is a single-file role and target is the role directory. Returns list generated files
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ansible_make_role.rb', line 71 def self.compile_role(source, target) = [] sections = { "defaults" => [], "vars" => [], "tasks" => [], "handlers" => [] } current_section = File.readlines(source).each { |line| line.chomp! next if line =~ /^---\s*$/ if line =~ /^(\w+)\s*:/ section = $1 if sections.key?(section) # Built-in section? current_section = sections[section] else # Everything else goes to the meta file incl. section header current_section = current_section << line end else current_section << line end } generated_files = [] (sections.to_a + [["meta", ]]).each { |section, lines| next if lines.empty? && section != "meta" next if lines.all? { |l| l =~ /^\s*$/ } dir = "#{target}/#{section}" file = "#{dir}/main.yml" generated_files << file FileUtils.mkdir_p(dir) File.open(file, "w") { |f| f.puts "---" if section != "meta" unindent(lines).each { |l| f.puts l } } } generated_files end |
.force ⇒ Object
14 |
# File 'lib/ansible_make_role.rb', line 14 def self.force() @force end |
.force=(value) ⇒ Object
15 |
# File 'lib/ansible_make_role.rb', line 15 def self.force=(value) @force = value end |
.git ⇒ Object
18 |
# File 'lib/ansible_make_role.rb', line 18 def self.git() @git end |
.git=(value) ⇒ Object
19 |
# File 'lib/ansible_make_role.rb', line 19 def self.git=(value) @git = value end |
.make(role_dir) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ansible_make_role.rb', line 21 def self.make(role_dir) wrap_system_call_error { role_file = "#{role_dir}/#{ROLE_FILE_NAME}" = "#{role_dir}/meta/main.yml" if force || !File.exist?() || File.mtime(role_file) > File.mtime() files = compile_role(role_file, role_dir) if git git_file = "#{role_dir}/.gitignore" FileUtils.rm_f(git_file) if !files.empty? lines = files.map { |f| "./" + f.split("/")[-2..-1].join("/") + "\n" }.join IO.write(git_file, lines) end end true end } end |
.unindent(lines) ⇒ Object
Unindent lines by the indentation of the first non-comment and non-blank line
116 117 118 119 120 121 122 |
# File 'lib/ansible_make_role.rb', line 116 def self.unindent(lines) line = lines.find { |l| l !~ /^\s*#/ && l !~ /^\s*$/ } return lines if line.nil? line =~ /^(\s*)/ prefix = $1.dup lines.map { |l| l.sub(/^#{prefix}/, "") } end |
.wrap_system_call_error(&block) ⇒ Object
Turn a SystemCallError into a AnsibleMakeRole::Error exception and remove Ruby reference from message (eg. “@ rb_sysopen”)
62 63 64 65 66 67 68 |
# File 'lib/ansible_make_role.rb', line 62 def self.wrap_system_call_error(&block) begin yield rescue SystemCallError => ex raise Error.new(ex..sub(/ @ \w+/, "")) end end |