2012-07-13 12:18:12 +01:00
|
|
|
class KeyPair < ActiveRecord::Base
|
|
|
|
belongs_to :repository
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
attr_accessor :secret
|
|
|
|
attr_accessible :public, :secret, :repository_id
|
|
|
|
|
2012-07-30 20:07:49 +01:00
|
|
|
validates :repository_id, :public, :user_id, :presence => true
|
|
|
|
validates :secret, :presence => true, :on => :create
|
2012-07-13 12:18:12 +01:00
|
|
|
|
2012-07-30 22:24:31 +01:00
|
|
|
validates :repository_id, :uniqueness => {:message => I18n.t("activerecord.errors.key_pair.repo_key_exists")}
|
2012-07-30 20:07:49 +01:00
|
|
|
|
|
|
|
before_create :key_create_call
|
|
|
|
before_destroy :rm_key_call
|
2012-07-13 15:16:56 +01:00
|
|
|
|
2012-07-30 20:07:49 +01:00
|
|
|
protected
|
|
|
|
|
|
|
|
def key_create_call
|
|
|
|
result, self.key_id = BuildServer.import_gpg_key_pair(public, secret)
|
2012-07-30 22:24:31 +01:00
|
|
|
raise "Failed to create key_pairs for repository #{repository_id} with code #{result}." if result == 4
|
2012-07-31 13:50:53 +01:00
|
|
|
if result != 0 || self.key_id.nil?
|
|
|
|
errors.add(:public, I18n.t("activerecord.errors.key_pair.rpc_error_#{result}"))
|
2012-07-30 20:07:49 +01:00
|
|
|
return false
|
|
|
|
end
|
2012-07-30 22:24:31 +01:00
|
|
|
result = BuildServer.set_repository_key(repository.platform.name, repository.name, key_id)
|
|
|
|
raise "Failed to sign repository #{repository.name} in platform #{repository.platform.name}
|
|
|
|
using key_id #{key_id} with code #{result}." unless result.zero?
|
2012-07-13 12:18:12 +01:00
|
|
|
end
|
|
|
|
|
2012-07-30 20:07:49 +01:00
|
|
|
def rm_key_call
|
2012-07-30 22:24:31 +01:00
|
|
|
result = BuildServer.rm_repository_key(repository.platform.name, repository.name)
|
|
|
|
raise "Failed to desroy repository key #{repository.name} in platform
|
|
|
|
#{repository.platform.name} with code #{result}." unless result.zero?
|
2012-07-30 20:07:49 +01:00
|
|
|
end
|
2012-07-13 12:18:12 +01:00
|
|
|
end
|