~ubuntu-branches/ubuntu/quantal/puppet/quantal

« back to all changes in this revision

Viewing changes to lib/puppet/module_tool/applications/uninstaller.rb

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-07-14 01:56:30 UTC
  • mfrom: (1.1.29) (3.1.43 sid)
  • Revision ID: package-import@ubuntu.com-20120714015630-ntj41rkvkq4zph4y
Tags: 2.7.18-1ubuntu1
* Resynchronise with Debian. (LP: #1023931) Remaining changes:
  - debian/puppetmaster-passenger.postinst: Make sure we error if puppet
    config print doesn't work
  - debian/puppetmaster-passenger.postinst: Ensure upgrades from
    <= 2.7.11-1 fixup passenger apache configuration.
* Dropped upstreamed patches:
  - debian/patches/CVE-2012-1906_CVE-2012-1986_to_CVE-2012-1989.patch
  - debian/patches/puppet-12844
  - debian/patches/2.7.17-Puppet-July-2012-CVE-fixes.patch
* Drop Build-Depends on ruby-rspec (in universe):
  - debian/control: remove ruby-rspec from Build-Depends
  - debian/patches/no-rspec.patch: make Rakefile work anyway if rspec
    isn't installed so we can use it in debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
module Puppet::Module::Tool
 
1
module Puppet::ModuleTool
2
2
  module Applications
3
3
    class Uninstaller < Application
 
4
      include Puppet::ModuleTool::Errors
4
5
 
5
 
      def initialize(name, options = {})
6
 
        @name = name
7
 
        @target_directories = options[:target_directories]
8
 
        @removed_dirs = []
 
6
      def initialize(name, options)
 
7
        @name        = name
 
8
        @options     = options
 
9
        @errors      = Hash.new {|h, k| h[k] = {}}
 
10
        @unfiltered  = []
 
11
        @installed   = []
 
12
        @suggestions = []
 
13
        @environment = Puppet::Node::Environment.new(options[:environment])
9
14
      end
10
15
 
11
16
      def run
12
 
        uninstall
13
 
        Puppet.notice "#{@name} is not installed" if @removed_dirs.empty?
14
 
        @removed_dirs
 
17
        results = {
 
18
          :module_name       => @name,
 
19
          :requested_version => @version,
 
20
        }
 
21
 
 
22
        begin
 
23
          find_installed_module
 
24
          validate_module
 
25
          FileUtils.rm_rf(@installed.first.path, :secure => true)
 
26
 
 
27
          results[:affected_modules] = @installed
 
28
          results[:result] = :success
 
29
        rescue ModuleToolError => err
 
30
          results[:error] = {
 
31
            :oneline   => err.message,
 
32
            :multiline => err.multiline,
 
33
          }
 
34
        rescue => e
 
35
          results[:error] = {
 
36
            :oneline => e.message,
 
37
            :multiline => e.respond_to?(:multiline) ? e.multiline : [e.to_s, e.backtrace].join("\n")
 
38
          }
 
39
        ensure
 
40
          results[:result] ||= :failure
 
41
        end
 
42
 
 
43
        results
15
44
      end
16
45
 
17
46
      private
18
47
 
19
 
      def uninstall
20
 
        # TODO: #11803 Check for broken dependencies before uninstalling modules.
21
 
        #
22
 
        # Search each path in the target directories for the specified module
23
 
        # and delete the directory.
24
 
        @target_directories.each do |target|
25
 
          if File.directory? target
26
 
            module_path = File.join(target, @name)
27
 
            @removed_dirs << FileUtils.rm_rf(module_path).first if File.directory?(module_path)
28
 
          end
 
48
      def find_installed_module
 
49
        @environment.modules_by_path.values.flatten.each do |mod|
 
50
          mod_name = (mod.forge_name || mod.name).gsub('/', '-')
 
51
          if mod_name == @name
 
52
            @unfiltered << {
 
53
              :name    => mod_name,
 
54
              :version => mod.version,
 
55
              :path    => mod.modulepath,
 
56
            }
 
57
            if @options[:version] && mod.version
 
58
              next unless SemVer[@options[:version]].include?(SemVer.new(mod.version))
 
59
            end
 
60
            @installed << mod
 
61
          elsif mod_name =~ /#{@name}/
 
62
            @suggestions << mod_name
 
63
          end
 
64
        end
 
65
 
 
66
        if @installed.length > 1
 
67
          raise MultipleInstalledError,
 
68
            :action            => :uninstall,
 
69
            :module_name       => @name,
 
70
            :installed_modules => @installed.sort_by { |mod| @environment.modulepath.index(mod.modulepath) }
 
71
        elsif @installed.empty?
 
72
          if @unfiltered.empty?
 
73
            raise NotInstalledError,
 
74
              :action      => :uninstall,
 
75
              :suggestions => @suggestions,
 
76
              :module_name => @name
 
77
          else
 
78
            raise NoVersionMatchesError,
 
79
              :installed_modules => @unfiltered.sort_by { |mod| @environment.modulepath.index(mod[:path]) },
 
80
              :version_range     => @options[:version],
 
81
              :module_name       => @name
 
82
          end
 
83
        end
 
84
      end
 
85
 
 
86
      def validate_module
 
87
        mod = @installed.first
 
88
 
 
89
        if !@options[:force] && mod.has_metadata? && mod.has_local_changes?
 
90
          raise LocalChangesError,
 
91
            :action            => :uninstall,
 
92
            :module_name       => (mod.forge_name || mod.name).gsub('/', '-'),
 
93
            :requested_version => @options[:version],
 
94
            :installed_version => mod.version
 
95
        end
 
96
 
 
97
        if !@options[:force] && !mod.required_by.empty?
 
98
          raise ModuleIsRequiredError,
 
99
            :module_name       => (mod.forge_name || mod.name).gsub('/', '-'),
 
100
            :required_by       => mod.required_by,
 
101
            :requested_version => @options[:version],
 
102
            :installed_version => mod.version
29
103
        end
30
104
      end
31
105
    end