rosa-build/app/models/key_pair.rb

36 lines
1.4 KiB
Ruby
Raw Normal View History

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
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-30 20:07:49 +01:00
protected
def key_create_call
result, self.key_id = BuildServer.import_gpg_key_pair(public, secret)
raise "Failed to create key_pairs for repository #{repository_id} with code #{result}." if result == 4
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
result = BuildServer.set_repository_key(repository.platform.name, repository.name, self.key_id)
raise "Failed to sign repository #{repository.name} in platform #{repository.platform.name}
using key_id #{self.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
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