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

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

Constant Summary

Constants inherited from Application

Application::DOCPATTERN, Application::SHOULD_PARSE_CONFIG_DEPRECATION_MSG

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE

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

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

Methods inherited from Application

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

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, deterministic_rand, execfail, execpipe, execute, exit_on_fail, logmethods, memory, path_to_uri, pretty_backtrace, proxy, replace_file, safe_posix_fork, symbolizehash, thinmark, uri_to_path, which, withenv, withumask

Methods included from Util::POSIX

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

Methods included from Util::SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Constructor Details

#initialize(name, forge, options) ⇒ Upgrader

Returns a new instance of Upgrader.



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

def initialize(name, forge, 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]
  @forge               = forge
end

Instance Method Details

#runObject



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
104
# File 'lib/puppet/module_tool/applications/upgrader.rb', line 18

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(@forge)
    rescue => e
      raise UnknownModuleError, results.merge(:repository => @forge.uri)
    else
      raise UnknownVersionError, results.merge(:repository => @forge.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], @forge)

    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