require 'whitelist_update'
class ContainerRequest < ArvadosModel
+ include ArvadosModelUpdates
include HasUuid
include KindAndEtag
include CommonApiTemplate
include WhitelistUpdate
belongs_to :container, foreign_key: :container_uuid, primary_key: :uuid
+ belongs_to :requesting_container, {
+ class_name: 'Container',
+ foreign_key: :requesting_container_uuid,
+ primary_key: :uuid,
+ }
serialize :properties, Hash
serialize :environment, Hash
before_validation :fill_field_defaults, :if => :new_record?
before_validation :validate_runtime_constraints
- before_validation :validate_scheduling_parameters
before_validation :set_container
+ before_validation :set_default_preemptable_scheduling_parameter
validates :command, :container_image, :output_path, :cwd, :presence => true
validates :output_ttl, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :priority, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 1000 }
+ validate :validate_scheduling_parameters
validate :validate_state_change
validate :check_update_whitelist
validate :secret_mounts_key_conflict
before_save :scrub_secret_mounts
- after_save :update_priority
- after_save :finalize_if_needed
before_create :set_requesting_container_uuid
before_destroy :set_priority_zero
+ after_save :update_priority
+ after_save :finalize_if_needed
api_accessible :user, extend: :common do |t|
t.add :command
AttrsPermittedAlways = [:owner_uuid, :state, :name, :description]
AttrsPermittedBeforeCommit = [:command, :container_count_max,
:container_image, :cwd, :environment, :filters, :mounts,
- :output_path, :priority, :properties, :requesting_container_uuid,
+ :output_path, :priority, :properties,
:runtime_constraints, :state, :container_uuid, :use_existing,
:scheduling_parameters, :secret_mounts, :output_name, :output_ttl]
if state == Committed && Container.find_by_uuid(container_uuid).final?
reload
act_as_system_user do
- finalize!
+ leave_modified_by_user_alone do
+ finalize!
+ end
end
end
end
end
end
+ def set_default_preemptable_scheduling_parameter
+ if self.state == Committed
+ # If preemptable instances (eg: AWS Spot Instances) are allowed,
+ # ask them on child containers by default.
+ if Rails.configuration.preemptable_instances and
+ !self.requesting_container_uuid.nil? and
+ self.scheduling_parameters['preemptable'].nil?
+ self.scheduling_parameters['preemptable'] = true
+ end
+ end
+ end
+
def validate_runtime_constraints
case self.state
when Committed
scheduling_parameters['partitions'].size)
errors.add :scheduling_parameters, "partitions must be an array of strings"
end
+ if !Rails.configuration.preemptable_instances and scheduling_parameters['preemptable']
+ errors.add :scheduling_parameters, "preemptable instances are not allowed"
+ end
end
end
end
def update_priority
- if self.state_changed? or
- self.priority_changed? or
- self.container_uuid_changed?
- act_as_system_user do
- Container.
- where('uuid in (?)',
- [self.container_uuid_was, self.container_uuid].compact).
- map(&:update_priority!)
- end
+ return unless state_changed? || priority_changed? || container_uuid_changed?
+ act_as_system_user do
+ Container.
+ where('uuid in (?)', [self.container_uuid_was, self.container_uuid].compact).
+ map(&:update_priority!)
end
end
end
def set_requesting_container_uuid
- return !new_record? if self.requesting_container_uuid # already set
-
- token_uuid = current_api_client_authorization.andand.uuid
- container = Container.where('auth_uuid=?', token_uuid).order('created_at desc').first
- if container
- self.requesting_container_uuid = container.uuid
- self.priority = container.priority
+ return if !current_api_client_authorization
+ if (c = Container.where('auth_uuid=?', current_api_client_authorization.uuid).select([:uuid, :priority]).first)
+ self.requesting_container_uuid = c.uuid
+ self.priority = c.priority>0 ? 1 : 0
end
- true
end
end