Class: Puppet::ModuleTool::Applications::Upgrader

Inherits:
Application show all
Includes:
Errors, Shared
Defined in:
lib/vendor/puppet/module_tool/applications/upgrader.rb

Constant Summary

Constants inherited from Application

Application::DOCPATTERN

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Instance Attribute Summary

Attributes inherited from Application

#command_line, #options

Instance Method Summary collapse

Methods included from Shared

#annotated_version, #download_tarballs, #get_local_constraints, #get_remote_constraints, #implicit_version, #resolve_constraints

Methods inherited from Application

[], banner, clear!, clear?, #configure_indirector_routes, controlled_run, exit, find, #handlearg, #help, interrupted?, #main, #name, option, option_parser_commands, #parse_options, #preinit, restart!, restart_requested?, #run_command, run_mode, #set_run_mode, #setup, #setup_logs, should_not_parse_config, should_parse_config, #should_parse_config?, should_parse_config?, stop!, stop_requested?

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

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

Constructor Details

#initialize(name, options) ⇒ Upgrader

Returns a new instance of Upgrader.



7
8
9
10
11
12
13
14
15
# File 'lib/vendor/puppet/module_tool/applications/upgrader.rb', line 7

def initialize(name, options)
  @action              = :upgrade
  @environment         = Puppet::Node::Environment.new(Puppet.settings[:environment])
  @module_name         = name
  @options             = options
  @force               = options[:force]
  @ignore_dependencies = options[:force] || options[:ignore_dependencies]
  @version             = options[:version]
end

Instance Method Details

#runObject



17
18
19
20
21
22
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
55
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
# File 'lib/vendor/puppet/module_tool/applications/upgrader.rb', line 17

def run
  begin
    results = { :module_name => @module_name }

    get_local_constraints

    if @installed[@module_name].length > 1
      raise MultipleInstalledError,
        :action            => :upgrade,
        :module_name       => @module_name,
        :installed_modules => @installed[@module_name].sort_by { |mod| @environment.modulepath.index(mod.modulepath) }
    elsif @installed[@module_name].empty?
      raise NotInstalledError,
        :action      => :upgrade,
        :module_name => @module_name
    end

    @module = @installed[@module_name].last
    results[:installed_version] = @module.version ? @module.version.sub(/^(?=\d)/, 'v') : nil
    results[:requested_version] = @version || (@conditions[@module_name].empty? ? :latest : :best)
    dir = @module.modulepath

    Puppet.notice "Found '#{@module_name}' (#{colorize(:cyan, results[:installed_version] || '???')}) in #{dir} ..."
    if !@options[:force] && @module.has_metadata? && @module.has_local_changes?
      raise LocalChangesError,
        :action            => :upgrade,
        :module_name       => @module_name,
        :requested_version => @version || (@conditions[@module_name].empty? ? :latest : :best),
        :installed_version => @module.version
    end

    begin
      get_remote_constraints
    rescue => e
      raise UnknownModuleError, results.merge(:repository => Puppet::Forge.repository.uri)
    else
      raise UnknownVersionError, results.merge(:repository => Puppet::Forge.repository.uri) if @remote.empty?
    end

    if !@options[:force] && @versions["#{@module_name}"].last[:vstring].sub(/^(?=\d)/, 'v') == (@module.version || '0.0.0').sub(/^(?=\d)/, 'v')
      raise VersionAlreadyInstalledError,
        :module_name       => @module_name,
        :requested_version => @version || ((@conditions[@module_name].empty? ? 'latest' : 'best') + ": #{@versions["#{@module_name}"].last[:vstring].sub(/^(?=\d)/, 'v')}"),
        :installed_version => @installed[@module_name].last.version,
        :conditions        => @conditions[@module_name] + [{ :module => :you, :version => @version }]
    end

    @graph = resolve_constraints({ @module_name => @version })

    # This clean call means we never "cache" the module we're installing, but this
    # is desired since module authors can easily rerelease modules different content but the same
    # version number, meaning someone with the old content cached will be very confused as to why
    # they can't get new content.
    # Long term we should just get rid of this caching behavior and cleanup downloaded modules after they install
    # but for now this is a quick fix to disable caching
    Puppet::Forge::Cache.clean
    tarballs = download_tarballs(@graph, @graph.last[:path])

    unless @graph.empty?
      Puppet.notice 'Upgrading -- do not interrupt ...'
      tarballs.each do |hash|
        hash.each do |dir, path|
          Unpacker.new(path, @options.merge(:target_dir => dir)).run
        end
      end
    end

    results[:result] = :success
    results[:base_dir] = @graph.first[:path]
    results[:affected_modules] = @graph
  rescue VersionAlreadyInstalledError => e
    results[:result] = :noop
    results[:error] = {
      :oneline   => e.message,
      :multiline => e.multiline
    }
  rescue => e
    results[:error] = {
      :oneline => e.message,
      :multiline => e.respond_to?(:multiline) ? e.multiline : [e.to_s, e.backtrace].join("\n")
    }
  ensure
    results[:result] ||= :failure
  end

  return results
end