Module: Puppet::ModuleTool::Shared

Includes:
Errors
Included in:
Applications::Installer, Applications::Upgrader
Defined in:
lib/vendor/puppet/module_tool/shared_behaviors.rb

Instance Method Summary collapse

Instance Method Details

#annotated_version(mod, versions) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/vendor/puppet/module_tool/shared_behaviors.rb', line 59

def annotated_version(mod, versions)
  if versions.empty?
    return implicit_version(mod)
  else
    return "#{implicit_version(mod)}: #{versions.last}"
  end
end

#download_tarballs(graph, default_path) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/vendor/puppet/module_tool/shared_behaviors.rb', line 143

def download_tarballs(graph, default_path)
  graph.map do |release|
    begin
      if release[:tarball]
        cache_path = Pathname(release[:tarball])
      else
        cache_path = Puppet::Forge.repository.retrieve(release[:file])
      end
    rescue OpenURI::HTTPError => e
      raise RuntimeError, "Could not download module: #{e.message}"
    end

    [
      { (release[:path] ||= default_path) => cache_path},
      *download_tarballs(release[:dependencies], default_path)
    ]
  end.flatten
end

#get_local_constraintsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vendor/puppet/module_tool/shared_behaviors.rb', line 5

def get_local_constraints
  @local      = Hash.new { |h,k| h[k] = { } }
  @conditions = Hash.new { |h,k| h[k] = [] }
  @installed  = Hash.new { |h,k| h[k] = [] }

  @environment.modules_by_path.values.flatten.each do |mod|
    mod_name = (mod.forge_name || mod.name).gsub('/', '-')
    @installed[mod_name] << mod
    d = @local["#{mod_name}@#{mod.version}"]
    (mod.dependencies || []).each do |hash|
      name, conditions = hash['name'], hash['version_requirement']
      name = name.gsub('/', '-')
      d[name] = conditions
      @conditions[name] << {
        :module => mod_name,
        :version => mod.version,
        :dependency => conditions
      }
    end
  end
end

#get_remote_constraintsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vendor/puppet/module_tool/shared_behaviors.rb', line 27

def get_remote_constraints
  @remote   = Hash.new { |h,k| h[k] = { } }
  @urls     = {}
  @versions = Hash.new { |h,k| h[k] = [] }

  Puppet.notice "Downloading from #{Puppet::Forge.repository.uri} ..."
  author, modname = Puppet::ModuleTool.username_and_modname_from(@module_name)
  info = Puppet::Forge.remote_dependency_info(author, modname, @options[:version])
  info.each do |pair|
    mod_name, releases = pair
    mod_name = mod_name.gsub('/', '-')
    releases.each do |rel|
      semver = SemVer.new(rel['version'] || '0.0.0') rescue SemVer.MIN
      @versions[mod_name] << { :vstring => rel['version'], :semver => semver }
      @versions[mod_name].sort! { |a, b| a[:semver] <=> b[:semver] }
      @urls["#{mod_name}@#{rel['version']}"] = rel['file']
      d = @remote["#{mod_name}@#{rel['version']}"]
      (rel['dependencies'] || []).each do |name, conditions|
        d[name.gsub('/', '-')] = conditions
      end
    end
  end
end

#implicit_version(mod) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/vendor/puppet/module_tool/shared_behaviors.rb', line 51

def implicit_version(mod)
  return :latest if @conditions[mod].empty?
  if @conditions[mod].all? { |c| c[:queued] || c[:module] == :you }
    return :latest
  end
  return :best
end

#resolve_constraints(dependencies, source = [{:name => :you}], seen = {}, action = @action) ⇒ Object



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
# File 'lib/vendor/puppet/module_tool/shared_behaviors.rb', line 67

def resolve_constraints(dependencies, source = [{:name => :you}], seen = {}, action = @action)
  dependencies = dependencies.map do |mod, range|
    source.last[:dependency] = range

    @conditions[mod] << {
      :module     => source.last[:name],
      :version    => source.last[:version],
      :dependency => range,
      :queued     => true
    }

    if @force
      range = SemVer[@version] rescue SemVer['>= 0.0.0']
    else
      range = (@conditions[mod]).map do |r|
        SemVer[r[:dependency]] rescue SemVer['>= 0.0.0']
      end.inject(&:&)
    end

    if @action == :install && seen.include?(mod)
      next if range === seen[mod][:semver]

      req_module   = @module_name
      req_versions = @versions["#{@module_name}"].map { |v| v[:semver] }
      raise InvalidDependencyCycleError,
        :module_name       => mod,
        :source            => (source + [{ :name => mod, :version => source.last[:dependency] }]),
        :requested_module  => req_module,
        :requested_version => @version || annotated_version(req_module, req_versions),
        :conditions        => @conditions
    end

    if !(@force || @installed[mod].empty? || source.last[:name] == :you)
      next if range === SemVer.new(@installed[mod].first.version)
      action = :upgrade
    elsif @installed[mod].empty?
      action = :install
    end

    if action == :upgrade
      @conditions.each { |_, conds| conds.delete_if { |c| c[:module] == mod } }
    end

    valid_versions = @versions["#{mod}"].select { |h| range === h[:semver] }

    unless version = valid_versions.last
      req_module   = @module_name
      req_versions = @versions["#{@module_name}"].map { |v| v[:semver] }
      raise NoVersionsSatisfyError,
        :requested_name    => req_module,
        :requested_version => @version || annotated_version(req_module, req_versions),
        :installed_version => @installed[@module_name].empty? ? nil : @installed[@module_name].first.version,
        :dependency_name   => mod,
        :conditions        => @conditions[mod],
        :action            => @action
    end

    seen[mod] = version

    {
      :module           => mod,
      :version          => version,
      :action           => action,
      :previous_version => @installed[mod].empty? ? nil : @installed[mod].first.version,
      :file             => @urls["#{mod}@#{version[:vstring]}"],
      :path             => action == :install ? @options[:target_dir] : (@installed[mod].empty? ? @options[:target_dir] : @installed[mod].first.modulepath),
      :dependencies     => []
    }
  end.compact
  dependencies.each do |mod|
    deps = @remote["#{mod[:module]}@#{mod[:version][:vstring]}"].sort_by(&:first)
    mod[:dependencies] = resolve_constraints(deps, source + [{ :name => mod[:module], :version => mod[:version][:vstring] }], seen, :install)
  end unless @ignore_dependencies
  return dependencies
end