Class: Puppet::ModuleTool::Applications::Installer Private

Inherits:
Application show all
Includes:
Forge::Errors, Errors, Shared, Network::Uri
Defined in:
lib/puppet/module_tool/applications/installer.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary

Constants inherited from Application

Application::DOCPATTERN

Constants included from Util

Util::ALNUM, Util::ALPHA, Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::ESCAPED, Util::HEX, Util::HttpProxy, Util::PUPPET_STACK_INSERTION_FRAME, Util::RESERVED, Util::RFC_3986_URI_REGEX, Util::UNRESERVED, Util::UNSAFE

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Instance Attribute Summary

Attributes inherited from Application

#command_line, #options

Instance Method Summary collapse

Methods included from Shared

#add_module_name_constraints_to_graph, #annotated_version, #download_tarballs, #forced?, #get_local_constraints, #get_remote_constraints, #implicit_version, #resolve_constraints

Methods included from Network::Uri

#mask_credentials

Methods inherited from Application

[], #app_defaults, available_application_names, banner, clear!, clear?, clear_everything_for_tests, #configure_indirector_routes, controlled_run, #deprecate, #deprecated?, environment_mode, exit, find, get_environment_mode, #handle_logdest_arg, #handlearg, #help, #initialize_app_defaults, interrupted?, #log_runtime_environment, #main, #name, option, option_parser_commands, #parse_options, #preinit, restart!, restart_requested?, #run_command, run_mode, #set_log_level, #setup, #setup_logs, stop!, stop_requested?, #summary, try_load_class

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::SymbolicFileMode

#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Constructor Details

#initialize(name, install_dir, options = {}) ⇒ Installer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Installer.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/puppet/module_tool/applications/installer.rb', line 23

def initialize(name, install_dir, options = {})
  super(options)

  @action              = :install
  @environment         = options[:environment_instance]
  @ignore_dependencies = forced? || options[:ignore_dependencies]
  @name                = name
  @install_dir         = install_dir

  Puppet::Forge::Cache.clean

  @local_tarball = Puppet::FileSystem.exist?(name)

  if @local_tarball
    release = local_tarball_source.release
    @name = release.name
    options[:version] = release.version.to_s
    SemanticPuppet::Dependency.add_source(local_tarball_source)

    # If we're operating on a local tarball and ignoring dependencies, we
    # don't need to search any additional sources.  This will cut down on
    # unnecessary network traffic.
    unless @ignore_dependencies
      SemanticPuppet::Dependency.add_source(installed_modules_source)
      SemanticPuppet::Dependency.add_source(module_repository)
    end

  else
    SemanticPuppet::Dependency.add_source(installed_modules_source) unless forced?
    SemanticPuppet::Dependency.add_source(module_repository)
  end
end

Instance Method Details

#runObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/puppet/module_tool/applications/installer.rb', line 56

def run
  name = @name.tr('/', '-')
  version = options[:version] || '>= 0.0.0'

  results = { :action => :install, :module_name => name, :module_version => version }

  begin
    if !@local_tarball && name !~ /-/
      raise InvalidModuleNameError.new(module_name: @name, suggestion: "puppetlabs-#{@name}", action: :install)
    end

    installed_module = installed_modules[name]
    if installed_module
      unless forced?
        if Puppet::Module.parse_range(version).include? installed_module.version
          results[:result] = :noop
          results[:version] = installed_module.version
          return results
        else
          changes = begin
            Checksummer.run(installed_modules[name].mod.path)
          rescue
            []
          end
          raise AlreadyInstalledError,
                :module_name => name,
                :installed_version => installed_modules[name].version,
                :requested_version => options[:version] || :latest,
                :local_changes => changes
        end
      end
    end

    @install_dir.prepare(name, options[:version] || 'latest')
    results[:install_dir] = @install_dir.target

    unless @local_tarball && @ignore_dependencies
      Puppet.notice _("Downloading from %{host} ...") % {
        host: mask_credentials(module_repository.host)
      }
    end

    if @ignore_dependencies
      graph = build_single_module_graph(name, version)
    else
      graph = build_dependency_graph(name, version)
    end

    unless forced?
      add_module_name_constraints_to_graph(graph)
    end

    installed_modules.each do |mod, release|
      mod = mod.tr('/', '-')
      next if mod == name

      version = release.version

      next if forced?

      # Since upgrading already installed modules can be troublesome,
      # we'll place constraints on the graph for each installed module,
      # locking it to upgrades within the same major version.
      installed_range = ">=#{version} #{version.major}.x"
      graph.add_constraint('installed', mod, installed_range) do |node|
        Puppet::Module.parse_range(installed_range).include? node.version
      end

      release.mod.dependencies.each do |dep|
        dep_name = dep['name'].tr('/', '-')

        range = dep['version_requirement']
        graph.add_constraint("#{mod} constraint", dep_name, range) do |node|
          Puppet::Module.parse_range(range).include? node.version
        end
      end
    end

    # Ensure that there is at least one candidate release available
    # for the target package.
    if graph.dependencies[name].empty?
      raise NoCandidateReleasesError, results.merge(:module_name => name, :source => module_repository.host, :requested_version => options[:version] || :latest)
    end

    begin
      Puppet.info _("Resolving dependencies ...")
      releases = SemanticPuppet::Dependency.resolve(graph)
    rescue SemanticPuppet::Dependency::UnsatisfiableGraph => e
      unsatisfied = nil

      if e.respond_to?(:unsatisfied) && e.unsatisfied
        constraints = {}
        # If the module we're installing satisfies all its
        # dependencies, but would break an already installed
        # module that depends on it, show what would break.
        if name == e.unsatisfied
          graph.constraints[name].each do |mod, range, _|
            next unless mod.split.include?('constraint')

            # If the user requested a specific version or range,
            # only show the modules with non-intersecting ranges
            if options[:version]
              requested_range = SemanticPuppet::VersionRange.parse(options[:version])
              constraint_range = SemanticPuppet::VersionRange.parse(range)

              if requested_range.intersection(constraint_range) == SemanticPuppet::VersionRange::EMPTY_RANGE
                constraints[mod.split.first] = range
              end
            else
              constraints[mod.split.first] = range
            end
          end

        # If the module fails to satisfy one of its
        # dependencies, show the unsatisfiable module
        else
          dep_constraints = graph.dependencies[name].max.constraints

          if dep_constraints.key?(e.unsatisfied)
            unsatisfied_range = dep_constraints[e.unsatisfied].first[1]
            constraints[e.unsatisfied] = unsatisfied_range
          end
        end

        installed_module = @environment.module_by_forge_name(e.unsatisfied.tr('-', '/'))
        current_version = installed_module.version if installed_module

        unsatisfied = {
          :name => e.unsatisfied,
          :constraints => constraints,
          :current_version => current_version
        } if constraints.any?
      end

      raise NoVersionsSatisfyError, results.merge(
        :requested_name => name,
        :requested_version => options[:version] || graph.dependencies[name].max.version.to_s,
        :unsatisfied => unsatisfied
      )
    end

    unless forced?
      # Check for module name conflicts.
      releases.each do |rel|
        installed_module = installed_modules_source.by_name[rel.name.split('-').last]
        next unless installed_module
        next if installed_module.has_metadata? && installed_module.forge_name.tr('/', '-') == rel.name

        if rel.name != name
          dependency = {
            :name => rel.name,
            :version => rel.version
          }
        end

        raise InstallConflictError,
              :requested_module => name,
              :requested_version => options[:version] || 'latest',
              :dependency => dependency,
              :directory => installed_module.path,
              :metadata => installed_module.
      end
    end

    Puppet.info _("Preparing to install ...")
    releases.each(&:prepare)

    Puppet.notice _('Installing -- do not interrupt ...')
    releases.each do |release|
      installed = installed_modules[release.name]
      if forced? || installed.nil?
        release.install(Pathname.new(results[:install_dir]))
      else
        release.install(Pathname.new(installed.mod.modulepath))
      end
    end

    results[:result] = :success
    results[:installed_modules] = releases
    results[:graph] = [build_install_graph(releases.first, releases)]
  rescue ModuleToolError, ForgeError => err
    results[:error] = {
      :oneline => err.message,
      :multiline => err.multiline,
    }
  ensure
    results[:result] ||= :failure
  end

  results
end