Class: Blacksmith::Forge

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

Constant Summary collapse

PUPPETLABS_FORGE =
"https://forgeapi.puppetlabs.com"
CREDENTIALS_FILE_HOME =
"~/.puppetforge.yml"
CREDENTIALS_FILE_PROJECT =
'.puppetforge.yml'
DEFAULT_CREDENTIALS =
{ 'url' => PUPPETLABS_FORGE }
HEADERS =
{ 'User-Agent' => "Blacksmith/#{Blacksmith::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE}; #{RUBY_PLATFORM})" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Forge.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/puppet_blacksmith/forge.rb', line 16

def initialize(username = nil, password = nil, url = nil)
  self.username = username
  self.password = password
  RestClient.proxy = ENV['http_proxy']
  load_credentials
  load_client_credentials_from_file
  self.url = url unless url.nil?
  if self.url =~ %r{http(s)?://forge.puppetlabs.com}
    puts "Ignoring url entry in .puppetforge.yml: must point to the api server at #{PUPPETLABS_FORGE}, not the Forge webpage"
    self.url = PUPPETLABS_FORGE
  end
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



14
15
16
# File 'lib/puppet_blacksmith/forge.rb', line 14

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



14
15
16
# File 'lib/puppet_blacksmith/forge.rb', line 14

def client_secret
  @client_secret
end

#passwordObject

Returns the value of attribute password.



14
15
16
# File 'lib/puppet_blacksmith/forge.rb', line 14

def password
  @password
end

#urlObject

Returns the value of attribute url.



14
15
16
# File 'lib/puppet_blacksmith/forge.rb', line 14

def url
  @url
end

#usernameObject

Returns the value of attribute username.



14
15
16
# File 'lib/puppet_blacksmith/forge.rb', line 14

def username
  @username
end

Instance Method Details

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

Raises:

  • (Errno::ENOENT)


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

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
  begin
    response = RestClient.post("#{url}/oauth/token", {
      'client_id' => client_id,
      'client_secret' => client_secret,
      'username' => username,
      'password' => password,
      'grant_type' => 'password'
    }, HEADERS)
  rescue RestClient::Exception => e
    raise Blacksmith::Error, "Error login to the forge #{url} as #{username} [#{e.message}]: #{e.response}"
  end
   = JSON.parse(response)
  access_token = ['access_token']

  # upload the file
  begin
    response = RestClient.post("#{url}/v2/releases",
      {:file => File.new(package, 'rb')},
      HEADERS.merge({'Authorization' => "Bearer #{access_token}"}))
  rescue RestClient::Exception => e
    raise Blacksmith::Error, "Error uploading #{package} to the forge #{url} [#{e.message}]: #{e.response}"
  end
end