ansible_make_role 0.7.1 → 0.7.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29ff2c2883c8a8b4d51a218cd24759b9c30800cf3026da0baf6810ff7b277e46
4
- data.tar.gz: af5a5ff7ba52f8738d90ae5933c98e31d315596eced5ad283611444867bc2708
3
+ metadata.gz: b9b8638421a96b17f3ff792b156c82ad46514a22762f772357213a1944cd09ff
4
+ data.tar.gz: 830f6c5592ef11571f0ad3092ce39cf9d6c4b79bb5bb3747e68f65ae696d2963
5
5
  SHA512:
6
- metadata.gz: ef8c02faa05b9151e13d431a9d85ab96460a19cb2b898bc46f022a860cf57a7d82e83b7dc7974118cbb9364d7d5ecad09ffb2a142abdc6c998373435f72e0bac
7
- data.tar.gz: 167cf5397c44fb3d961fb6a63ea0242956dcd7542d92af86f1d6c97449a8a0454c4fe62fc3a087a88bec529f2fedf02d2eec8bc6cd950d028aafcf7b9911fc2d
6
+ metadata.gz: 4f71300995da1f30d7e8fff7d05b25855b1b073de011c8e7d9a78e9714d65f7718eaa95447ff250a03b10ad6bb97525a71b1dbdfd37f91426152b2265e8fafc9
7
+ data.tar.gz: e917a67e699c2190ed5921d615b3e28bb85990da813dd19fe16ed59c29f4788f83ffd2dcc437cc053e420b14885f41f27d9c688d171638002254b7bbd23c3a2c
data/README.md CHANGED
@@ -1,19 +1,21 @@
1
1
  # ansible-make-role
2
2
 
3
- Pre-compiler that turns `make.yml` files into Ansible roles
3
+ Pre-compiler that turns a single-file role definition into Ansible role files.
4
+ The single-file `role.yml` file combines the Ansible `main.yml` files from the
5
+ `defaults`, `vars`, `tasks`, and `meta` subdirectories with a YAML top-level
6
+ section for each file (`defaults:`, `vars:`, `tasks:` etc.)
4
7
 
5
8
  ## Usage
6
9
 
7
- ```shell
8
- ansible-make-role -f|--force -v|--verbose --version DIR...
10
+ ```
11
+ ansible-make-role -c|--clean -f|--force -g|--git -r|--roles=DIR -v|--verbose --version DIR...
9
12
  ```
10
13
 
11
- `ansible-make-role` expects a `make.yml` file in each of the given directories
12
- and generate a Ansible role from each of them. The generated files will reside
13
- in subdirectories of the directory of the `make.yml` file but it can be
14
- overridden by the --target-dir option. `ansible-make-role` only generates
15
- out-of-date files unless the --force option is used. Use the `--verbose` option
16
- to get information about the process
14
+ `ansible-make-role` expects a `role.yml` file in each of the given directories
15
+ and generates an Ansible role in it. Existing `main.yml` files will be
16
+ overwritten or removed but other files won't be touched. `ansible-make-role`
17
+ search subdirectories of `./roles` if no dirctories was given on the command
18
+ line
17
19
 
18
20
  ## Description
19
21
 
@@ -53,9 +55,19 @@ handlers: # Goes to handlers/main.yml"
53
55
 
54
56
  ## Options
55
57
 
58
+ `-c, --clean`
59
+ Clean the project by removing autogenerated files
56
60
 
57
61
  `-f, --force`
58
- Re-generate all files even if not needed
62
+ Re-generate all files even if not needed. Useful if you forgot a
63
+ `--git` option
64
+
65
+ `-g, --git`
66
+ Also handle per-role .gitignore files. Note that existing .gitignore
67
+ files are overwritten
68
+
69
+ `-r, --roles=DIR`
70
+ Use DIR instead of default `./roles`
59
71
 
60
72
  `-v, --verbose`
61
73
  Report progress
data/TODO ADDED
@@ -0,0 +1,19 @@
1
+
2
+ o Add a "warning: auto-generated" header to all generated files
3
+ o Checksum of files?
4
+ o Warn before overwrite if checksum doesn't match
5
+ o Write comments "connected" to a section in the section file:
6
+
7
+ # Goes to meta/main.yml
8
+
9
+ # Goes to defaults/main.yml
10
+ #
11
+ defaults:
12
+ # Goes to defaults/main.yml
13
+
14
+
15
+ + Only copy templates/files if needed. What to do with directories?
16
+ o Remove orphaned files
17
+
18
+ - (Sym-)link files, templates, and directories?
19
+ - Replace ansible-make-role with an option: "-m - Make a single directory with a 'make.yml' file"
@@ -11,11 +11,10 @@ Gem::Specification.new do |spec|
11
11
 
12
12
  spec.summary = %q{Make Ansible role from make.yml file}
13
13
  spec.description = %q{
14
- Instead of having an Ansible role per-directory
15
- main.yml file, ansible-make-role processes a make.yml
16
- file in the role directory and generates the main.yml
17
- files from that. In make.yml each main.yml definition
18
- is contained in a per-file section: 'defaults',
14
+ ansible-make-role process a single-file role
15
+ definition file and generate an Ansible role from it.
16
+ The role is defined in the role.yml file and contain
17
+ a section for each Ansible main.yml file: 'defaults',
19
18
  'vars', 'tasks', and 'handlers'. Definitions outside
20
19
  of those sections (notably 'dependencies') are going
21
20
  to the meta/main.yml file
@@ -40,8 +39,9 @@ Gem::Specification.new do |spec|
40
39
  spec.require_paths = ["lib"]
41
40
 
42
41
  spec.add_development_dependency "bundler", "~> 1.17"
43
- spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "rake", ">= 12.3.3"
44
43
  spec.add_development_dependency "rspec", "~> 3.0"
45
44
 
46
45
  spec.add_dependency "shellopts", "0.9.3"
46
+ spec.add_dependency "indented_io"
47
47
  end
@@ -3,21 +3,72 @@
3
3
  require 'ansible_make_role.rb'
4
4
  require "shellopts"
5
5
 
6
- USAGE = "f,force v,verbose version -- DIRECTORY..."
6
+ USAGE = "c,clean f,force g,git +r,roles=DIR v,verbose version -- DIRECTORY..."
7
7
 
8
+ DEFAULT_ROLES_DIR = "./roles"
9
+
10
+ clean = false
8
11
  force = false
12
+ git = false
13
+ roles_dirs = [] # A roles (plural) directory containing role (singular) directories
9
14
  verbose = false
10
15
 
11
16
  args = ShellOpts.process(USAGE, ARGV) { |opt, arg|
12
17
  case opt
18
+ when '-c', '--clean'; clean = true
13
19
  when '-f', '--force'; force = true
20
+ when '-g', '--git'; git = true
21
+ when '-r', '--roles'
22
+ File.directory?(arg) or ShellOpts::error "Not a directory: #{arg}"
23
+ roles_dirs << arg
14
24
  when '-v', '--verbose'; verbose = true
15
25
  when '--version'; puts "ansible-make-role #{AnsibleMakeRole::VERSION}"; exit
16
26
  end
17
27
  }
18
- args.size > 0 or args << "."
19
28
 
20
- for source in args
21
- File.directory?(source) or ShellOpts.error "Not a directory: #{source.inspect}"
22
- AnsibleMakeRole.make(source, verbose: verbose, force: force)
29
+ AnsibleMakeRole.force = force
30
+ AnsibleMakeRole.git = git
31
+ if verbose
32
+ def verb(*msgs) print(msgs.join, "\n") end
33
+ else
34
+ def verb(*_) end
35
+ end
36
+
37
+ if args.empty? && roles_dirs.empty?
38
+ if File.directory?(DEFAULT_ROLES_DIR)
39
+ roles_dirs << DEFAULT_ROLES_DIR
40
+ else
41
+ ShellOpts::error "No role_dirs given"
42
+ end
43
+ end
44
+
45
+ role_dirs = args
46
+ for roles_dir in roles_dirs
47
+ role_dirs += Dir["#{roles_dir}/**/role.yml"].map { |f| File.dirname(f) }
48
+ end
49
+
50
+ if role_dirs.empty?
51
+ $stderr.puts "#{PROGRAM}: No role_dirs found"
52
+ exit 0
53
+ end
54
+
55
+ begin
56
+ for role_dir in role_dirs
57
+ if clean
58
+ if AnsibleMakeRole.clean(role_dir)
59
+ verb("Cleaned #{role_dir}")
60
+ else
61
+ verb("Skipped #{role_dir} - already clean")
62
+ end
63
+ else
64
+ if AnsibleMakeRole.make(role_dir)
65
+ verb("Updated #{role_dir}")
66
+ else
67
+ verb("Skipped #{role_dir} - up to date")
68
+ end
69
+ end
70
+ end
71
+ rescue AnsibleMakeRole::Error => ex
72
+ ShellOpts::fail(ex.message)
23
73
  end
74
+
@@ -1,29 +1,71 @@
1
1
  require "ansible_make_role/version"
2
2
 
3
- require "shellopts"
4
-
5
3
  require "fileutils"
6
4
 
7
5
  module AnsibleMakeRole
8
- def self.make(source_dir, verbose: false, force: false)
9
- source = "#{source_dir}/make.yml"
10
- target_dir = source_dir
11
- # TODO: Doesn't belong at this level - catch exceptions in exe instead
12
- File.file?(source) or ShellOpts::error "Can't read file #{source.inspect}"
13
- File.directory?(target_dir) or ShellOpts::error "Can't find directory #{target_dir}"
14
-
15
- meta_yml = "#{target_dir}/meta/main.yml"
16
- if force || !File.exist?(meta_yml) || File.mtime(source) > File.mtime(meta_yml)
17
- compile_role(source, target_dir, verbose: verbose)
18
- else
19
- puts "#{target_dir} is up to date" if verbose
20
- end
6
+ ROLE_FILE_NAME = "role.yml"
7
+
8
+ DEFAULT_FORCE_FLAG = false
9
+ DEFAULT_GIT_FLAG = false
10
+
11
+ class Error < StandardError; end
12
+
13
+ @force = DEFAULT_FORCE_FLAG
14
+ def self.force() @force end
15
+ def self.force=(value) @force = value end
16
+
17
+ @git = DEFAULT_GIT_FLAG
18
+ def self.git() @git end
19
+ def self.git=(value) @git = value end
20
+
21
+ def self.make(role_dir)
22
+ changed = false
23
+ wrap_system_call_error {
24
+ role_file = "#{role_dir}/#{ROLE_FILE_NAME}"
25
+ meta_file = "#{role_dir}/meta/main.yml"
26
+ git_file = "#{role_dir}/.gitignore"
27
+ if force || !File.exist?(meta_file) || File.mtime(role_file) > File.mtime(meta_file)
28
+ if !compile_role(role_file, role_dir).empty?
29
+ IO.write(git_file, "*/main.yml\n") if git
30
+ changed = true
31
+ end
32
+ end
33
+ }
34
+ return changed
35
+ end
36
+
37
+ def self.clean(role_dir)
38
+ changed = false
39
+ wrap_system_call_error {
40
+ File.exists?("#{role_dir}/#{ROLE_FILE_NAME}") or raise Error.new("Not a role directory: #{role_dir}")
41
+ for file in Dir["#{role_dir}/*/main.yml"]
42
+ FileUtils.rm(file)
43
+ dir = File.dirname(file)
44
+ FileUtils.rmdir(dir) if File.empty?(dir)
45
+ changed = true
46
+ end
47
+ git_file = "#{role_dir}/.gitignore"
48
+ if git && File.exist?(git_file)
49
+ FileUtils.rm(git_file)
50
+ changed = true
51
+ end
52
+ }
53
+ return changed
21
54
  end
22
55
 
23
56
  private
24
- # source is a file, target is a directory. Target can be nil and defaults to
25
- # dirname of source
26
- def self.compile_role(source, target, verbose: false)
57
+ # Turn a SystemCallError into a AnsibleMakeRole::Error exception and remove
58
+ # Ruby reference from message (eg. "@ rb_sysopen")
59
+ def self.wrap_system_call_error(&block)
60
+ begin
61
+ yield
62
+ rescue SystemCallError => ex
63
+ raise Error.new(ex.message.sub(/ @ \w+/, ""))
64
+ end
65
+ end
66
+
67
+ # source is a single-file role and target is the role directory. Returns list generated files
68
+ def self.compile_role(source, target)
27
69
  meta = []
28
70
  sections = {
29
71
  "defaults" => [],
@@ -33,7 +75,6 @@ private
33
75
  }
34
76
  current_section = meta
35
77
 
36
- puts "Parsing #{source}" if verbose
37
78
  File.readlines(source).each { |line|
38
79
  line.chomp!
39
80
  next if line =~ /^---\s*$/
@@ -50,18 +91,21 @@ private
50
91
  end
51
92
  }
52
93
 
53
- (sections.to_a + [["meta", meta]]).each { |section, lines|
54
- next if lines.empty? && section != "meta"
94
+ if meta.all? { |l| l =~ /^\s*$/ }
95
+ meta = ["dependencies: []"]
96
+ end
97
+
98
+ (sections.to_a + [["meta", meta]]).map { |section, lines|
99
+ next if lines.all? { |l| l =~ /^\s*$/ }
55
100
  dir = "#{target}/#{section}"
56
101
  file = "#{dir}/main.yml"
57
-
58
- puts "Create #{file}" if verbose
59
102
  FileUtils.mkdir_p(dir)
60
- File.open("#{dir}/main.yml", "w") { |f|
103
+ File.open(file, "w") { |f|
61
104
  f.puts "---" if section != "meta"
62
105
  unindent(lines).each { |l| f.puts l }
63
106
  }
64
- }
107
+ file
108
+ }.compact
65
109
  end
66
110
 
67
111
  # Unindent lines by the indentation of the first non-comment and non-blank
@@ -74,3 +118,6 @@ private
74
118
  lines.map { |l| l.sub(/^#{prefix}/, "") }
75
119
  end
76
120
  end
121
+
122
+
123
+
@@ -1,3 +1,3 @@
1
1
  module AnsibleMakeRole
2
- VERSION = "0.7.1"
2
+ VERSION = "0.7.9"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansible_make_role
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-15 00:00:00.000000000 Z
11
+ date: 2020-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: 12.3.3
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: 12.3.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,14 +66,27 @@ dependencies:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.9.3
69
- description: "\n Instead of having an Ansible role per-directory\n
70
- \ main.yml file, ansible-make-role processes a make.yml\n
71
- \ file in the role directory and generates the main.yml\n
72
- \ files from that. In make.yml each main.yml definition\n
73
- \ is contained in a per-file section: 'defaults',\n 'vars',
74
- 'tasks', and 'handlers'. Definitions outside\n of those
75
- sections (notably 'dependencies') are going\n to the meta/main.yml
76
- file\n "
69
+ - !ruby/object:Gem::Dependency
70
+ name: indented_io
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: "\n ansible-make-role process a single-file
84
+ role\n definition file and generate an Ansible role from
85
+ it.\n The role is defined in the role.yml file and contain\n
86
+ \ a section for each Ansible main.yml file: 'defaults',\n
87
+ \ 'vars', 'tasks', and 'handlers'. Definitions outside\n
88
+ \ of those sections (notably 'dependencies') are going\n
89
+ \ to the meta/main.yml file\n "
77
90
  email:
78
91
  - claus.l.rasmussen@gmail.com
79
92
  executables:
@@ -88,6 +101,7 @@ files:
88
101
  - Gemfile
89
102
  - README.md
90
103
  - Rakefile
104
+ - TODO
91
105
  - ansible_make_role.gemspec
92
106
  - bin/console
93
107
  - bin/setup
@@ -113,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
127
  - !ruby/object:Gem::Version
114
128
  version: '0'
115
129
  requirements: []
116
- rubygems_version: 3.0.1
130
+ rubygems_version: 3.0.8
117
131
  signing_key:
118
132
  specification_version: 4
119
133
  summary: Make Ansible role from make.yml file