Class: Blacksmith::Forge

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_blacksmith/forge.rb

Constant Summary collapse

PUPPETLABS_FORGE =
"https://forge.puppetlabs.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, password = nil, url = nil) ⇒ Forge

Returns a new instance of Forge.



12
13
14
15
16
17
18
# File 'lib/puppet_blacksmith/forge.rb', line 12

def initialize(username = nil, password = nil, url = nil)
  self.username = username
  self.password = password
  self.url = url
  RestClient.proxy = ENV['http_proxy']
  load_credentials_from_file if username.nil?
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/puppet_blacksmith/forge.rb', line 9

def password
  @password
end

#urlObject



20
21
22
# File 'lib/puppet_blacksmith/forge.rb', line 20

def url
  @url || PUPPETLABS_FORGE
end

#usernameObject

Returns the value of attribute username.



9
10
11
# File 'lib/puppet_blacksmith/forge.rb', line 9

def username
  @username
end

Instance Method Details

#push!(name, package = nil) ⇒ Object

Raises:

  • (Errno::ENOENT)


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
# File 'lib/puppet_blacksmith/forge.rb', line 24

def push!(name, package = nil)
  unless package
    regex = /^#{username}-#{name}-.*\.tar\.gz$/
    pkg = File.expand_path("pkg")
    f = Dir.new(pkg).select{|f| f.match(regex)}.last
    raise Errno::ENOENT, "File not found in #{pkg} with regex #{regex}" if f.nil?
    package = File.join(pkg, f)
  end
  raise Errno::ENOENT, "File does not exist: #{package}" unless File.exists?(package)

  # login to the puppet forge
  response = RestClient.post(
    "#{url}/login",
    {'username' => username, 'password' => password}){
      |response, request, result, &block|
      if [301, 302, 307].include? response.code
        response # no need to follow redirects
      else
        response.return!(request, result, &block)
      end
  }
  raise Blacksmith::Error, "Failed to login to Puppet Forge: cookies not set correctly" unless response.cookies['auth']

  page = Nokogiri::HTML(response.body)
  errors = page.css('.failure')
  raise Blacksmith::Error, "Error uploading module #{package} to Puppet Forge #{username}/#{name}:#{errors.text}" unless errors.empty?

  # upload the file
  response = RestClient.post("#{url}/#{username}/#{name}/upload",
    {:tarball => File.new(package, 'rb')},
    {:cookies => response.cookies}){
      |response, request, result, &block|
      if [301, 302, 307].include? response.code
        response # no need to follow redirects
      else
        response.return!(request, result, &block)
      end
  }
  page = Nokogiri::HTML(response.body)
  errors = page.css('.errors')
  raise Blacksmith::Error, "Error uploading module #{package} to Puppet Forge #{username}/#{name}:#{errors.text}" unless errors.empty?
end