r10k 1.4.2 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (244) hide show
  1. data/.travis.yml +2 -0
  2. data/CHANGELOG.mkd +192 -0
  3. data/CONTRIBUTING.mkd +3 -3
  4. data/Gemfile +4 -0
  5. data/README.mkd +13 -6
  6. data/doc/dynamic-environments/configuration.mkd +77 -8
  7. data/doc/dynamic-environments/git-environments.mkd +12 -2
  8. data/doc/dynamic-environments/quickstart.mkd +6 -0
  9. data/doc/faq.mkd +6 -6
  10. data/doc/git/cloning-and-mirroring.mkd +60 -0
  11. data/doc/git/providers.mkd +68 -0
  12. data/integration/Gemfile +3 -0
  13. data/integration/README.mkd +29 -0
  14. data/integration/configs/README.mkd +7 -0
  15. data/integration/configs/pe/centos-6-64mda +25 -0
  16. data/integration/configs/pe/centos-7-64mda +25 -0
  17. data/integration/configs/pe/debian-6-64mda +25 -0
  18. data/integration/configs/pe/debian-7-64mda +25 -0
  19. data/integration/configs/pe/redhat-6-64mda +25 -0
  20. data/integration/configs/pe/redhat-7-64mda +25 -0
  21. data/integration/configs/pe/sles-11-64mda +25 -0
  22. data/integration/configs/pe/ubuntu-1004-64mda +25 -0
  23. data/integration/configs/pe/ubuntu-1204-64mda +25 -0
  24. data/integration/configs/pe/ubuntu-1404-64mda +25 -0
  25. data/integration/files/README.mkd +4 -0
  26. data/integration/files/hiera.yaml +8 -0
  27. data/integration/files/modules/helloworld/manifests/init.pp +3 -0
  28. data/integration/files/modules/hieratest/manifests/init.pp +3 -0
  29. data/integration/files/modules/unicode/files/pretend_unicode +1 -0
  30. data/integration/files/modules/unicode/manifests/init.pp +6 -0
  31. data/integration/files/pre-suite/prod_env.config +3 -0
  32. data/integration/files/r10k_conf.yaml.erb +9 -0
  33. data/integration/lib/README.mkd +4 -0
  34. data/integration/lib/git_utils.rb +205 -0
  35. data/integration/lib/master_manipulator.rb +205 -0
  36. data/integration/lib/r10k_utils.rb +166 -0
  37. data/integration/manifests/README.mkd +4 -0
  38. data/integration/pre-suite/00_pe_install.rb +4 -0
  39. data/integration/pre-suite/01_git_config.rb +59 -0
  40. data/integration/pre-suite/02_pe_r10k.rb +60 -0
  41. data/integration/pre-suite/03_test_utils.rb +17 -0
  42. data/integration/pre-suite/README.mkd +5 -0
  43. data/integration/test_run_scripts/README.mkd +5 -0
  44. data/integration/test_run_scripts/all_tests-rugged-pe-centos6.sh +20 -0
  45. data/integration/test_run_scripts/all_tests-rugged-pe-rhel7.sh +20 -0
  46. data/integration/test_run_scripts/all_tests-rugged-pe-sles11.sh +20 -0
  47. data/integration/test_run_scripts/all_tests-rugged-pe-ubuntu1204.sh +20 -0
  48. data/integration/test_run_scripts/all_tests-rugged-pe-ubuntu1404.sh +20 -0
  49. data/integration/test_run_scripts/all_tests-shellgit-pe-centos6.sh +20 -0
  50. data/integration/test_run_scripts/all_tests-shellgit-pe-rhel7.sh +20 -0
  51. data/integration/test_run_scripts/all_tests-shellgit-pe-sles11.sh +20 -0
  52. data/integration/test_run_scripts/all_tests-shellgit-pe-ubuntu1204.sh +20 -0
  53. data/integration/test_run_scripts/all_tests-shellgit-pe-ubuntu1404.sh +20 -0
  54. data/integration/test_run_scripts/basic_functionality/all_tests-pe-centos6.sh +20 -0
  55. data/integration/test_run_scripts/command_line/all_tests-pe-centos6.sh +20 -0
  56. data/integration/test_run_scripts/git_source/all_tests-pe-centos6.sh +20 -0
  57. data/integration/test_run_scripts/user_scenario/basic_workflow/all_tests-pe-centos6.sh +20 -0
  58. data/integration/test_run_scripts/user_scenario/complex_workflow/all_tests-pe-centos6.sh +20 -0
  59. data/integration/tests/README.mkd +4 -0
  60. data/integration/tests/basic_functionality/negative/neg_deploy_with_invalid_r10k_yaml.rb +46 -0
  61. data/integration/tests/basic_functionality/negative/neg_deploy_with_missing_r10k_yaml.rb +27 -0
  62. data/integration/tests/command_line/deploy_env_without_mod_update.rb +75 -0
  63. data/integration/tests/command_line/negative/neg_deploy_env_with_module_update.rb +76 -0
  64. data/integration/tests/command_line/negative/neg_invalid_cli_dep_flag.rb +22 -0
  65. data/integration/tests/command_line/negative/neg_invalid_cli_env_flag.rb +22 -0
  66. data/integration/tests/command_line/negative/neg_invalid_cmd_line_arg.rb +24 -0
  67. data/integration/tests/git_source/git_source_git.rb +123 -0
  68. data/integration/tests/git_source/git_source_ssh.rb +84 -0
  69. data/integration/tests/git_source/git_source_submodule.rb +69 -0
  70. data/integration/tests/git_source/negative/neg_git_broken_remote.rb +37 -0
  71. data/integration/tests/git_source/negative/neg_git_unauthorized_https.rb +45 -0
  72. data/integration/tests/git_source/negative/neg_git_unauthorized_ssh.rb +72 -0
  73. data/integration/tests/git_source/negative/neg_git_unicode_branch.rb +34 -0
  74. data/integration/tests/user_scenario/basic_workflow/multi_env_1000_branches.rb +66 -0
  75. data/integration/tests/user_scenario/basic_workflow/multi_env_custom_forge_git_module.rb +110 -0
  76. data/integration/tests/user_scenario/basic_workflow/multi_env_custom_forge_git_module_static.rb +113 -0
  77. data/integration/tests/user_scenario/basic_workflow/multi_env_hiera.rb +93 -0
  78. data/integration/tests/user_scenario/basic_workflow/multi_env_multi_source.rb +132 -0
  79. data/integration/tests/user_scenario/basic_workflow/multi_source_custom_forge_git_module.rb +160 -0
  80. data/integration/tests/user_scenario/basic_workflow/negative/neg_bad_basedir.rb +45 -0
  81. data/integration/tests/user_scenario/basic_workflow/negative/neg_bad_forge_module.rb +43 -0
  82. data/integration/tests/user_scenario/basic_workflow/negative/neg_bad_git_module.rb +40 -0
  83. data/integration/tests/user_scenario/basic_workflow/negative/neg_bad_git_module_ref.rb +42 -0
  84. data/integration/tests/user_scenario/basic_workflow/negative/neg_bad_git_remote.rb +44 -0
  85. data/integration/tests/user_scenario/basic_workflow/negative/neg_branch_name_collision.rb +63 -0
  86. data/integration/tests/user_scenario/basic_workflow/negative/neg_disk_full.rb +73 -0
  87. data/integration/tests/user_scenario/basic_workflow/negative/neg_duplicate_module_names.rb +45 -0
  88. data/integration/tests/user_scenario/basic_workflow/negative/neg_inaccessible_forge.rb +51 -0
  89. data/integration/tests/user_scenario/basic_workflow/negative/neg_invalid_env_name.rb +33 -0
  90. data/integration/tests/user_scenario/basic_workflow/negative/neg_invalid_puppet_file.rb +35 -0
  91. data/integration/tests/user_scenario/basic_workflow/negative/neg_module_specified_at_deleted_release.rb +44 -0
  92. data/integration/tests/user_scenario/basic_workflow/negative/neg_read_only.rb +57 -0
  93. data/integration/tests/user_scenario/basic_workflow/negative/neg_specify_deleted_forge_module.rb +44 -0
  94. data/integration/tests/user_scenario/basic_workflow/single_env_10000_files.rb +73 -0
  95. data/integration/tests/user_scenario/basic_workflow/single_env_custom_forge_git_module.rb +103 -0
  96. data/integration/tests/user_scenario/basic_workflow/single_env_custom_forge_module.rb +80 -0
  97. data/integration/tests/user_scenario/basic_workflow/single_env_custom_module.rb +48 -0
  98. data/integration/tests/user_scenario/basic_workflow/single_env_large_files.rb +73 -0
  99. data/integration/tests/user_scenario/basic_workflow/single_env_module_already_installed.rb +81 -0
  100. data/integration/tests/user_scenario/basic_workflow/single_env_module_last_release_deleted.rb +67 -0
  101. data/integration/tests/user_scenario/basic_workflow/single_env_non-existent_base_dir.rb +93 -0
  102. data/integration/tests/user_scenario/basic_workflow/single_env_purge_unmanaged_modules.rb +92 -0
  103. data/integration/tests/user_scenario/basic_workflow/single_env_switch_forge_git_module.rb +116 -0
  104. data/integration/tests/user_scenario/basic_workflow/single_env_unicode_paths.rb +59 -0
  105. data/integration/tests/user_scenario/basic_workflow/single_env_upgrade_forge_mod_revert_change.rb +165 -0
  106. data/integration/tests/user_scenario/complex_workflow/multi_env_add_change_remove.rb +162 -0
  107. data/integration/tests/user_scenario/complex_workflow/multi_env_remove_re-add.rb +110 -0
  108. data/lib/r10k/action/cri_runner.rb +10 -13
  109. data/lib/r10k/cli.rb +3 -8
  110. data/lib/r10k/cli/version.rb +10 -1
  111. data/lib/r10k/deployment/config.rb +26 -6
  112. data/lib/r10k/deployment/config/loader.rb +38 -29
  113. data/lib/r10k/deployment/source.rb +2 -4
  114. data/lib/r10k/environment/git.rb +10 -19
  115. data/lib/r10k/environment/name.rb +86 -0
  116. data/lib/r10k/errors.rb +8 -5
  117. data/lib/r10k/feature.rb +35 -0
  118. data/lib/r10k/feature/collection.rb +23 -0
  119. data/lib/r10k/features.rb +18 -0
  120. data/lib/r10k/git.rb +106 -12
  121. data/lib/r10k/git/alternates.rb +2 -2
  122. data/lib/r10k/git/cache.rb +43 -26
  123. data/lib/r10k/git/errors.rb +12 -6
  124. data/lib/r10k/git/head.rb +3 -0
  125. data/lib/r10k/git/ref.rb +3 -0
  126. data/lib/r10k/git/remote_head.rb +6 -0
  127. data/lib/r10k/git/repository.rb +6 -0
  128. data/lib/r10k/git/rugged.rb +17 -0
  129. data/lib/r10k/git/rugged/bare_repository.rb +86 -0
  130. data/lib/r10k/git/rugged/base_repository.rb +90 -0
  131. data/lib/r10k/git/rugged/cache.rb +11 -0
  132. data/lib/r10k/git/rugged/thin_repository.rb +72 -0
  133. data/lib/r10k/git/rugged/working_repository.rb +99 -0
  134. data/lib/r10k/git/shellgit.rb +9 -0
  135. data/lib/r10k/git/shellgit/bare_repository.rb +29 -0
  136. data/lib/r10k/git/shellgit/base_repository.rb +102 -0
  137. data/lib/r10k/git/shellgit/cache.rb +11 -0
  138. data/lib/r10k/git/shellgit/thin_repository.rb +56 -0
  139. data/lib/r10k/git/shellgit/working_repository.rb +73 -0
  140. data/lib/r10k/git/stateful_repository.rb +80 -0
  141. data/lib/r10k/git/tag.rb +3 -0
  142. data/lib/r10k/git/working_dir.rb +15 -2
  143. data/lib/r10k/logging.rb +73 -36
  144. data/lib/r10k/logging/terminaloutputter.rb +36 -0
  145. data/lib/r10k/module/forge.rb +10 -9
  146. data/lib/r10k/module/git.rb +14 -44
  147. data/lib/r10k/module/metadata_file.rb +31 -0
  148. data/lib/r10k/module/svn.rb +0 -1
  149. data/lib/r10k/module_repository/forge.rb +20 -7
  150. data/lib/r10k/puppetfile.rb +1 -1
  151. data/lib/r10k/settings/container.rb +6 -0
  152. data/lib/r10k/settings/mixin.rb +9 -0
  153. data/lib/r10k/source.rb +2 -3
  154. data/lib/r10k/source/base.rb +5 -5
  155. data/lib/r10k/source/git.rb +11 -86
  156. data/lib/r10k/source/svn.rb +8 -52
  157. data/lib/r10k/util/basedir.rb +4 -4
  158. data/lib/r10k/util/commands.rb +31 -0
  159. data/lib/r10k/util/exec_env.rb +36 -0
  160. data/lib/r10k/util/platform.rb +4 -0
  161. data/lib/r10k/util/purgeable.rb +2 -2
  162. data/lib/r10k/util/subprocess.rb +3 -7
  163. data/lib/r10k/util/subprocess/runner.rb +3 -28
  164. data/lib/r10k/util/subprocess/runner/posix.rb +103 -0
  165. data/lib/r10k/util/subprocess/runner/pump.rb +59 -0
  166. data/lib/r10k/util/subprocess/{windows/runner.rb → runner/windows.rb} +2 -11
  167. data/lib/r10k/util/symbolize_keys.rb +31 -0
  168. data/lib/r10k/version.rb +1 -1
  169. data/lib/shared/puppet/module_tool/metadata.rb +197 -0
  170. data/r10k.gemspec +7 -5
  171. data/spec/fixtures/integration/git/puppet-boolean-bare.tar +0 -0
  172. data/spec/fixtures/module/forge/bad_module/metadata.json +1 -0
  173. data/spec/fixtures/unit/util/subprocess/posix/runner/no-execute.sh +3 -0
  174. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/and_the_expected_version_is_latest/can_fetch_all_versions_of_a_given_module.yml +171 -26
  175. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/and_the_expected_version_is_latest/can_fetch_the_latest_version_of_a_given_module.yml +171 -26
  176. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/and_the_expected_version_is_latest/ignores_deleted_releases.yml +190 -0
  177. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/it_handles_errors_from_forgeapi_puppetlabs_com/raises_an_error_for_a_non-existant_module.yml +34 -0
  178. data/spec/fixtures/vcr/cassettes/R10K_Module_Forge/and_the_expected_version_is_latest/sets_the_expected_version_based_on_the_latest_forge_version.yml +87 -26
  179. data/spec/integration/git/rugged/bare_repository_spec.rb +13 -0
  180. data/spec/integration/git/rugged/thin_repository_spec.rb +14 -0
  181. data/spec/integration/git/rugged/working_repository_spec.rb +13 -0
  182. data/spec/integration/git/shellgit/bare_repository_spec.rb +13 -0
  183. data/spec/integration/git/shellgit/thin_repository_spec.rb +14 -0
  184. data/spec/integration/git/shellgit/working_repository_spec.rb +13 -0
  185. data/spec/integration/git/stateful_repository_spec.rb +104 -0
  186. data/spec/matchers/match_realpath.rb +18 -0
  187. data/spec/shared-contexts/git-fixtures.rb +55 -0
  188. data/spec/shared-examples/git-repository.rb +38 -0
  189. data/spec/shared-examples/git/bare_repository.rb +70 -0
  190. data/spec/shared-examples/git/thin_repository.rb +26 -0
  191. data/spec/shared-examples/git/working_repository.rb +115 -0
  192. data/spec/shared-examples/subprocess-runner.rb +83 -0
  193. data/spec/spec_helper.rb +13 -9
  194. data/spec/unit/action/cri_runner_spec.rb +0 -6
  195. data/spec/unit/action/puppetfile/{cri_action_spec.rb → cri_runner_spec.rb} +0 -0
  196. data/spec/unit/deployment/config/loader_spec.rb +48 -0
  197. data/spec/unit/deployment/config_spec.rb +49 -0
  198. data/spec/unit/environment/git_spec.rb +5 -39
  199. data/spec/unit/environment/name_spec.rb +135 -0
  200. data/spec/unit/feature_spec.rb +50 -0
  201. data/spec/unit/git/alternates_spec.rb +1 -1
  202. data/spec/unit/git/cache_spec.rb +32 -19
  203. data/spec/unit/git/commit_spec.rb +1 -0
  204. data/spec/unit/git/head_spec.rb +1 -6
  205. data/spec/unit/git/ref_spec.rb +0 -23
  206. data/spec/unit/git/rugged/cache_spec.rb +26 -0
  207. data/spec/unit/git/shellgit/cache_spec.rb +27 -0
  208. data/spec/unit/git/stateful_repository_spec.rb +43 -0
  209. data/spec/unit/git/tag_spec.rb +1 -0
  210. data/spec/unit/git/working_dir_spec.rb +1 -0
  211. data/spec/unit/git_spec.rb +74 -0
  212. data/spec/unit/logging/terminaloutputter_spec.rb +53 -0
  213. data/spec/unit/logging_spec.rb +68 -0
  214. data/spec/unit/module/forge_spec.rb +7 -38
  215. data/spec/unit/module/git_spec.rb +7 -38
  216. data/spec/unit/module/metadata_file_spec.rb +69 -0
  217. data/spec/unit/module/svn_spec.rb +0 -4
  218. data/spec/unit/module_repository/forge_spec.rb +22 -1
  219. data/spec/unit/puppet/module_tool/metadata_spec.rb +301 -0
  220. data/spec/unit/settings/container_spec.rb +17 -0
  221. data/spec/unit/source/git_spec.rb +0 -123
  222. data/spec/unit/source/svn_spec.rb +66 -0
  223. data/spec/unit/util/commands_spec.rb +61 -0
  224. data/spec/unit/util/exec_env_spec.rb +56 -0
  225. data/spec/unit/util/subprocess/runner/posix_spec.rb +7 -0
  226. data/spec/unit/util/subprocess/runner/pump_spec.rb +79 -0
  227. data/spec/unit/util/subprocess_spec.rb +3 -2
  228. data/spec/unit/util/symbolize_keys_spec.rb +51 -0
  229. metadata +226 -45
  230. data/lib/r10k/execution.rb +0 -47
  231. data/lib/r10k/module/metadata.rb +0 -47
  232. data/lib/r10k/util/core_ext/hash_ext.rb +0 -19
  233. data/lib/r10k/util/subprocess/io.rb +0 -7
  234. data/lib/r10k/util/subprocess/posix.rb +0 -4
  235. data/lib/r10k/util/subprocess/posix/io.rb +0 -7
  236. data/lib/r10k/util/subprocess/posix/runner.rb +0 -140
  237. data/lib/r10k/util/subprocess/windows.rb +0 -4
  238. data/lib/r10k/util/subprocess/windows/io.rb +0 -6
  239. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/looking_up_versions.yml +0 -42
  240. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/looking_up_versions/can_fetch_all_versions_of_a_given_module.yml +0 -42
  241. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/looking_up_versions/can_fetch_the_latest_version_of_a_given_module.yml +0 -42
  242. data/spec/unit/deployment/source_spec.rb +0 -24
  243. data/spec/unit/module/metadata_spec.rb +0 -68
  244. data/spec/unit/util/core_ext/hash_ext_spec.rb +0 -63
@@ -1,7 +1,6 @@
1
1
  require 'r10k/svn'
2
2
  require 'r10k/environment'
3
- require 'r10k/util/purgeable'
4
- require 'r10k/util/core_ext/hash_ext'
3
+ require 'r10k/environment/name'
5
4
  require 'r10k/util/setopts'
6
5
 
7
6
  # This class implements a source for SVN environments.
@@ -79,10 +78,9 @@ class R10K::Source::SVN < R10K::Source::Base
79
78
  # @return [Array<R10K::Environment::SVN>] An array of environments created
80
79
  # from this source.
81
80
  def generate_environments
82
-
83
- branch_names.map do |branch|
81
+ names_and_paths.map do |(branch, path)|
84
82
  options = {
85
- :remote => branch.remote,
83
+ :remote => path,
86
84
  :username => @username,
87
85
  :password => @password
88
86
  }
@@ -90,20 +88,8 @@ class R10K::Source::SVN < R10K::Source::Base
90
88
  end
91
89
  end
92
90
 
93
- include R10K::Util::Purgeable
94
-
95
- def managed_directory
96
- @basedir
97
- end
98
-
99
- def current_contents
100
- Dir.glob(File.join(@basedir, '*')).map do |fname|
101
- File.basename fname
102
- end
103
- end
104
-
105
91
  # List all environments that should exist in the basedir for this source
106
- # @note This implements a required method for the Purgeable mixin
92
+ # @note This is required by {R10K::Util::Basedir}
107
93
  # @return [Array<String>]
108
94
  def desired_contents
109
95
  @environments.map {|env| env.dirname }
@@ -113,46 +99,16 @@ class R10K::Source::SVN < R10K::Source::Base
113
99
 
114
100
  private
115
101
 
116
- def branch_names
102
+ def names_and_paths
117
103
  branches = []
118
104
 
119
- branch_opts = {
120
- :prefix => @prefix,
121
- :sourcename => @name,
122
- }
105
+ opts = {:prefix => @prefix, :correct => false, :validate => false, :source => @name}
123
106
 
124
- branches << BranchName.new('production', "#{@remote}/trunk", branch_opts)
107
+ branches << [R10K::Environment::Name.new('production', opts), "#{@remote}/trunk"]
125
108
  @svn_remote.branches.each do |branch|
126
- branches << BranchName.new(branch, "#{@remote}/branches/#{branch}", branch_opts)
109
+ branches << [R10K::Environment::Name.new(branch, opts), "#{@remote}/branches/#{branch}"]
127
110
  end
128
111
 
129
112
  branches
130
113
  end
131
-
132
- # @api private
133
- # @todo respect environment name corrections
134
- class BranchName
135
-
136
- attr_reader :name
137
- attr_reader :remote
138
-
139
- def initialize(name, remote, opts)
140
- @name = name
141
- @remote = remote
142
- @opts = opts
143
-
144
- @prefix = opts[:prefix]
145
- @sourcename = opts[:sourcename]
146
- end
147
-
148
- def dirname
149
- dir = @name.dup
150
-
151
- if @prefix
152
- dir = "#{@sourcename}_#{dir}"
153
- end
154
-
155
- dir
156
- end
157
- end
158
114
  end
@@ -28,10 +28,10 @@ module R10K
28
28
  end
29
29
 
30
30
  # @param path [String] The path to the directory to manage
31
- # @param sources [Array<R10K::Util::Purgeable>] A list of purgeable objects
31
+ # @param sources [Array<#desired_contents>] A list of objects that may create filesystem entries
32
32
  def initialize(path, sources)
33
33
  if sources.is_a? R10K::Deployment
34
- raise ArgumentError, "Expected Array<Purgeable>, got R10K::Deployment"
34
+ raise ArgumentError, "Expected Array<#desired_contents>, got R10K::Deployment"
35
35
  end
36
36
  @path = path
37
37
  @sources = sources
@@ -53,10 +53,10 @@ module R10K
53
53
 
54
54
  def purge!
55
55
  @sources.each do |source|
56
- logger.debug1 "Source #{source.name} in #{@path} claimed contents #{source.desired_contents.inspect}"
56
+ logger.debug1 "Source #{source.name} in #{@path} manages contents #{source.desired_contents.inspect}"
57
57
  end
58
58
  if !stale_contents.empty?
59
- logger.debug "No sources in #{@path} claimed contents #{stale_contents.inspect}"
59
+ logger.debug "The path #{@path} has unmanaged contents #{stale_contents.inspect}"
60
60
  end
61
61
  super
62
62
  end
@@ -0,0 +1,31 @@
1
+ module R10K
2
+ module Util
3
+ module Commands
4
+ module_function
5
+
6
+ # Find the full path of a shell command.
7
+ #
8
+ # On POSIX platforms, the PATHEXT environment variable will be unset, so
9
+ # the first command named 'cmd' will be returned.
10
+ #
11
+ # On Windows platforms, the PATHEXT environment variable will contain a
12
+ # semicolon delimited list of executable file extensions, so the first
13
+ # command with a matching path extension will be returned.
14
+ #
15
+ # @param cmd [String] The name of the command to search for
16
+ # @return [String, nil] The path to the file if found, nil otherwise
17
+ def which(cmd)
18
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
19
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir|
20
+ exts.each do |ext|
21
+ path = File.join(dir, "#{cmd}#{ext}")
22
+ if File.executable?(path) && File.file?(path)
23
+ return path
24
+ end
25
+ end
26
+ end
27
+ nil
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ module R10K
2
+ module Util
3
+
4
+ # Utility methods for dealing with environment variables
5
+ module ExecEnv
6
+ module_function
7
+
8
+ # Swap out all environment settings
9
+ #
10
+ # @param env [Hash] The new environment to use
11
+ # @return [void]
12
+ def reset(env)
13
+ env.each_pair do |key, value|
14
+ ENV[key] = value
15
+ end
16
+
17
+ (ENV.keys - env.keys).each do |key|
18
+ ENV.delete(key)
19
+ end
20
+ end
21
+
22
+ # Add the specified settings to the env for the supplied block
23
+ #
24
+ # @param env [Hash] The values to add to the environment
25
+ # @param block [Proc] The code to call with the modified environnment
26
+ # @return [void]
27
+ def withenv(env, &block)
28
+ original = ENV.to_hash
29
+ reset(original.merge(env))
30
+ block.call
31
+ ensure
32
+ reset(original)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -14,6 +14,10 @@ module R10K
14
14
  def self.windows?
15
15
  RbConfig::CONFIG['host_os'] =~ /mswin|win32|dos|mingw|cygwin/i
16
16
  end
17
+
18
+ def self.posix?
19
+ !windows?
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -46,11 +46,11 @@ module R10K
46
46
  # Forcibly remove all unmanaged content in `self.managed_directory`
47
47
  def purge!
48
48
  if stale_contents.empty?
49
- logger.debug1 "No stale contents in #{managed_directory}, nothing to purge"
49
+ logger.debug1 "No unmanaged contents in #{managed_directory}, nothing to purge"
50
50
  else
51
51
  stale_contents.each do |fname|
52
52
  fpath = File.join(self.managed_directory, fname)
53
- logger.debug "Removing stale path #{fpath}"
53
+ logger.info "Removing unmanaged path #{fpath}"
54
54
  FileUtils.rm_rf(fpath, :secure => true)
55
55
  end
56
56
  end
@@ -11,19 +11,15 @@ module R10K
11
11
  class Subprocess
12
12
 
13
13
  require 'r10k/util/subprocess/runner'
14
- require 'r10k/util/subprocess/io'
15
14
  require 'r10k/util/subprocess/result'
16
15
  require 'r10k/util/subprocess/subprocess_error'
17
16
 
18
- require 'r10k/util/subprocess/posix'
19
- require 'r10k/util/subprocess/windows'
20
-
21
17
  # @return [Class < R10K::Util::Subprocess::Runner]
22
18
  def self.runner
23
19
  if R10K::Util::Platform.windows?
24
- R10K::Util::Subprocess::Windows::Runner
20
+ R10K::Util::Subprocess::Runner::Windows
25
21
  else
26
- R10K::Util::Subprocess::POSIX::Runner
22
+ R10K::Util::Subprocess::Runner::POSIX
27
23
  end
28
24
  end
29
25
 
@@ -75,7 +71,7 @@ module R10K
75
71
  result = subprocess.run
76
72
  logger.debug2("Finished process:\n#{result.format}")
77
73
 
78
- if @raise_on_fail and subprocess.crashed?
74
+ if @raise_on_fail && result.failed?
79
75
  raise SubprocessError.new("Command exited with non-zero exit code", :result => result)
80
76
  end
81
77
 
@@ -3,19 +3,14 @@
3
3
  # @api private
4
4
  class R10K::Util::Subprocess::Runner
5
5
 
6
+ require 'r10k/util/subprocess/runner/windows'
7
+ require 'r10k/util/subprocess/runner/posix'
8
+
6
9
  # @!attribute [rw] cwd
7
10
  # @return [String] The directory to be used as the cwd when executing
8
11
  # the command.
9
12
  attr_accessor :cwd
10
13
 
11
- attr_reader :io
12
-
13
- attr_reader :pid
14
-
15
- # @!attribute [r] status
16
- # @return [Process::Status]
17
- attr_reader :status
18
-
19
14
  # @!attribute [r] result
20
15
  # @return [R10K::Util::Subprocess::Result]
21
16
  attr_reader :result
@@ -27,24 +22,4 @@ class R10K::Util::Subprocess::Runner
27
22
  def run
28
23
  raise NotImplementedError
29
24
  end
30
-
31
- # Start the process asynchronously and return. Not all runners will implement this.
32
- def start
33
- raise NotImplementedError
34
- end
35
-
36
- # Wait for the process to exit. Not all runners will implement this.
37
- def wait
38
- raise NotImplementedError
39
- end
40
-
41
- # Did the given process exit with a non-zero exit code?
42
- def crashed?
43
- raise NotImplementedError
44
- end
45
-
46
- # @return [Integer] The exit status of the given process.
47
- def exit_code
48
- raise NotImplementedError
49
- end
50
25
  end
@@ -0,0 +1,103 @@
1
+ require 'r10k/util/subprocess/runner'
2
+ require 'r10k/util/subprocess/runner/pump'
3
+ require 'fcntl'
4
+
5
+ # Implement a POSIX command runner by using fork/exec.
6
+ #
7
+ # This implementation is optimized to run commands in the background, and has
8
+ # a few noteworthy implementation details.
9
+ #
10
+ # First off, when the child process is forked, it calls setsid() to detach from
11
+ # the controlling TTY. This has two main ramifications: sending signals will
12
+ # never be send to the forked process, and the forked process does not have
13
+ # access to stdin.
14
+ #
15
+ # @api private
16
+ class R10K::Util::Subprocess::Runner::POSIX < R10K::Util::Subprocess::Runner
17
+
18
+ def initialize(argv)
19
+ @argv = argv
20
+ mkpipes
21
+ end
22
+
23
+ def run
24
+ # Create a pipe so that the parent can verify that the child process
25
+ # successfully executed. The pipe will be closed on a successful exec(),
26
+ # and will contain an error message on failure.
27
+ exec_r, exec_w = pipe
28
+
29
+ @stdout_pump = R10K::Util::Subprocess::Runner::Pump.new(@stdout_r)
30
+ @stderr_pump = R10K::Util::Subprocess::Runner::Pump.new(@stderr_r)
31
+ @stdout_pump.start
32
+ @stderr_pump.start
33
+
34
+ pid = fork do
35
+ exec_r.close
36
+ execute_child(exec_w)
37
+ end
38
+
39
+ exec_w.close
40
+ execute_parent(exec_r, pid)
41
+
42
+ @result
43
+ end
44
+
45
+ private
46
+
47
+ def execute_child(exec_w)
48
+ if @cwd
49
+ Dir.chdir @cwd
50
+ end
51
+
52
+ # Create a new session for the forked child. This prevents children from
53
+ # ever being the foreground process on a TTY, which is almost always what
54
+ # we want in r10k.
55
+ Process.setsid
56
+
57
+ # Reopen file descriptors
58
+ STDOUT.reopen(@stdout_w)
59
+ STDERR.reopen(@stderr_w)
60
+
61
+ executable = @argv.shift
62
+ exec([executable, executable], *@argv)
63
+ rescue SystemCallError => e
64
+ exec_w.write("#{e.class}: #{e.message}")
65
+ exit(254)
66
+ end
67
+
68
+ def execute_parent(exec_r, pid)
69
+ @stdout_w.close
70
+ @stderr_w.close
71
+
72
+ stdout = ''
73
+ stderr = ''
74
+
75
+ if !exec_r.eof?
76
+ stderr = exec_r.read || "exec() failed"
77
+ _, @status = Process.waitpid2(pid)
78
+ else
79
+ _, @status = Process.waitpid2(pid)
80
+ @stdout_pump.wait
81
+ @stderr_pump.wait
82
+ stdout = @stdout_pump.string
83
+ stderr = @stderr_pump.string
84
+ end
85
+ exec_r.close
86
+
87
+ @stdout_r.close
88
+ @stderr_r.close
89
+
90
+ @result = R10K::Util::Subprocess::Result.new(@argv, stdout, stderr, @status.exitstatus)
91
+ end
92
+
93
+ def mkpipes
94
+ @stdout_r, @stdout_w = pipe
95
+ @stderr_r, @stderr_w = pipe
96
+ end
97
+
98
+ def pipe
99
+ ::IO.pipe.tap do |pair|
100
+ pair.each { |p| p.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,59 @@
1
+ require 'r10k/util/subprocess/runner'
2
+
3
+ # Perform nonblocking reads on a streaming IO instance.
4
+ #
5
+ # @api private
6
+ class R10K::Util::Subprocess::Runner::Pump
7
+
8
+ # !@attribute [r] string
9
+ # @return [String] The output collected from the IO device
10
+ attr_reader :string
11
+
12
+ # @!attribute [r] min_delay
13
+ # @return [Float] The minimum time to wait while polling the IO device
14
+ attr_accessor :min_delay
15
+
16
+ # @!attribute [r] max_delay
17
+ # @return [Float] The maximum time to wait while polling the IO device
18
+ attr_accessor :max_delay
19
+
20
+ def initialize(io)
21
+ @io = io
22
+ @thread = nil
23
+ @string = ''
24
+ @run = true
25
+ @min_delay = 0.05
26
+ @max_delay = 1.0
27
+ end
28
+
29
+ def start
30
+ @thread = Thread.new { pump }
31
+ end
32
+
33
+ def halt!
34
+ @run = false
35
+ @thread.join
36
+ end
37
+
38
+ # Block until the pumping thread reaches EOF on the IO object.
39
+ def wait
40
+ @thread.join
41
+ end
42
+
43
+ private
44
+
45
+ def pump
46
+ backoff = @min_delay
47
+ while @run
48
+ begin
49
+ @string << @io.read_nonblock(4096)
50
+ backoff /= 2 if backoff > @min_delay
51
+ rescue Errno::EWOULDBLOCK, Errno::EAGAIN
52
+ backoff *= 2 if backoff < @max_delay
53
+ IO.select([@io], [], [], backoff)
54
+ rescue EOFError
55
+ @run = false
56
+ end
57
+ end
58
+ end
59
+ end