PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/bitbucket-keys/lib/puppet/type/upload_bitbucket_keys.rb

https://bitbucket.org/atlassian/agent-charlie
Ruby | 68 lines | 51 code | 14 blank | 3 comment | 0 complexity | 64af400c7d2afbe4630cf1c152892f27 MD5 | raw file
  1. require 'rest-client'
  2. require 'json'
  3. Puppet::Type.newtype(:upload_bitbucket_keys) do
  4. @doc = "Uploads your private key to Bitbucket"
  5. newproperty(:stupid_hack) do |property|
  6. desc "Stupid hack to get puppet to do what we want"
  7. defaultto :insync
  8. def retrieve
  9. :outofsync
  10. end
  11. def sync
  12. sshkey = File.read("#{self.resource[:home]}/.ssh/id_rsa.pub").chomp
  13. # Grab the *real* username
  14. response = RestClient::Request.execute(
  15. :method => :get,
  16. :url => "https://bitbucket.org/api/1.0/user",
  17. :user => self.resource[:username],
  18. :password => self.resource[:password]
  19. )
  20. from_json = JSON.parse(response)
  21. real_username = from_json['user']['username']
  22. # Send the SSH key
  23. begin
  24. RestClient::Request.execute(
  25. :method => :post,
  26. :url => "https://bitbucket.org/api/1.0/users/#{real_username}/ssh-keys",
  27. :user => real_username,
  28. :password => self.resource[:password],
  29. :payload => {
  30. :label => 'charlie-installed-key',
  31. :key => sshkey
  32. }
  33. )
  34. rescue RestClient::BadRequest
  35. # Ignore this because it signals a duplicate key
  36. rescue => e
  37. self.fail "Could upload SSH keys: #{e.message}"
  38. end
  39. :insync
  40. end
  41. end
  42. newparam(:name) do
  43. desc "The name of the resource"
  44. end
  45. newparam(:home) do
  46. desc "The home directory of the user"
  47. end
  48. newparam(:username) do
  49. desc "Your bitbucket username"
  50. end
  51. newparam(:password) do
  52. desc "Your bitbucket password"
  53. end
  54. end