On Sep 22, 2011, at 9:33 AM, Jan Provaznik wrote:
> On 09/22/2011 02:38 AM, Richard Su wrote:
>> Is task.instance.create_auth_key expected to work for all providers.
>> RHEV deltacloud-core doesn't know how to respond to the request and
>> returns this:
>>
>> /usr/bin/deltacloudd[17507]: 10.3.113.54 - - [21/Sep/2011 19:32:27] "GET
>> /api/keys HTTP/1.1" 403 13014 0.0678
>>
>> I had to comment out a few lines below to get things working.
>>
>
>
> yes - create_auth_key is Instance model method, and inside this method is done
checking for authentication_key feature:
> app/models/instance.rb:245
> return nil unless client&& client.feature?(:instances, :authentication_key)
>
> So if instance key is not supported, it doesn't try to create a key -> maybe
a bug in API? CCing Michal.
The key->create operation is supported only for EC2 backend. Other clouds doesn't
have it.
You should first check if collection 'keys' is present and then try to create a
new key.
I think without key collection is the CREATE call on it ignored (404 or 405 is
returned).
-- Michal
client.feature?(:instances, :authentication_key) is called _before_ any
create_key call.
Jan
>
>
>> On 09/20/2011 02:09 PM, Chris Lalancette wrote:
>>> <snip>
>>> new file mode 100644
>>> index 0000000..84dabd0
>>> --- /dev/null
>>> +++ b/src/app/util/taskomatic.rb
>>> @@ -0,0 +1,171 @@
>>> +#
>>> +# Copyright (C) 2011 Red Hat, Inc.
>>> +# Written by Ian Main<imain(a)redhat.com>
>>> +#
>>> +# This program is free software; you can redistribute it and/or modify
>>> +# it under the terms of the GNU General Public License as published by
>>> +# the Free Software Foundation; version 2 of the License.
>>> +#
>>> +# This program is distributed in the hope that it will be useful,
>>> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
>>> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
>>> +# GNU General Public License for more details.
>>> +#
>>> +# You should have received a copy of the GNU General Public License
>>> +# along with this program; if not, write to the Free Software
>>> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
>>> +# MA 02110-1301, USA. A copy of the GNU General Public License is
>>> +# also available at
http://www.gnu.org/copyleft/gpl.html.
>>> +
>>> +module Taskomatic
>>> +
>>> + def self.create_instance(task)
>>> + begin
>>> + match = matches(task.instance).first
>>> +
>>> + task.state = Task::STATE_PENDING
>>> + task.save!
>>> +
>>> + task.instance.provider_account = match.provider_account
>>> + task.instance.create_auth_key unless task.instance.instance_key
>> commented out previous line, task.instance.create_auth_key...
>>> +
>>> + dcloud_instance = create_dcloud_instance(task.instance, match)
>>> +
>>> + handle_dcloud_error(dcloud_instance)
>>> +
>>> + task.state = Task::STATE_RUNNING
>>> + task.save!
>>> +
>>> + Rails.logger.info "Task instance create completed with key
#{dcloud_instance.id} and state #{dcloud_instance.state}"
>>> + task.instance.external_key = dcloud_instance.id
>>> + task.instance.state = dcloud_to_instance_state(dcloud_instance.state)
>>> + task.instance.save!
>>> + rescue HttpException => ex
>>> + task.failure_code = Task::FAILURE_PROVIDER_CONTACT_FAILED
>>> + handle_create_instance_error(task, ex)
>>> + rescue Exception => ex
>>> + handle_create_instance_error(task, ex)
>>> + ensure
>>> + task.instance.save!
>>> + task.save!
>>> + end
>>> + end
>>> +
>>> + def self.do_action(task, action)
>>> + task.time_started = Time.now
>>> +
>>> + begin
>>> + client = task.instance.provider_account.connect
>>> + dcloud_instance = client.instance(task.instance.external_key)
>>> +
>>> + task.state = Task::STATE_PENDING
>>> + task.save!
>>> +
>>> + dcloud_instance.send(action)
>>> +
>>> + Rails.logger.info("Task instance '#{action}' complete,
now in state #{dcloud_instance.state}.")
>>> +
>>> + task.instance.state = dcloud_to_instance_state(dcloud_instance.state)
>>> + task.instance.save!
>>> + task.state = Task::STATE_FINISHED
>>> + rescue Exception => ex
>>> + task.state = Task::STATE_FAILED
>>> + task.message = ex.message
>>> + ensure
>>> + task.save!
>>> + end
>>> + end
>>> +
>>> + def self.start_instance(task)
>>> + do_action(task, :start!)
>>> + end
>>> +
>>> + def self.stop_instance(task)
>>> + do_action(task, :stop!)
>>> + end
>>> +
>>> + def self.reboot_instance(task)
>>> + do_action(task, :reboot!)
>>> + end
>>> +
>>> + def self.destroy_instance(task)
>>> + task.time_started = Time.now
>>> +
>>> + begin
>>> + client = task.instance.provider_account.connect
>>> + dcloud_instance = client.instance(task.instance.external_key)
>>> +
>>> + task.state = Task::STATE_PENDING
>>> + task.save!
>>> +
>>> + dcloud_instance.destroy!
>>> +
>>> + Rails.logger.info("Task Destroy completed.")
>>> +
>>> + task.state = Task::STATE_FINISHED
>>> + rescue Exception => ex
>>> + task.state = Task::STATE_FAILED
>>> + task.message = ex.message
>>> + ensure
>>> + task.save!
>>> + end
>>> + end
>>> +
>>> + def self.dcloud_to_instance_state(state_str)
>>> + case state_str.upcase
>>> + when 'PENDING'
>>> + return Instance::STATE_PENDING
>>> + when 'RUNNING'
>>> + return Instance::STATE_RUNNING
>>> + when 'STOPPED'
>>> + return Instance::STATE_STOPPED
>>> + when 'TERMINATED'
>>> + return Instance::STATE_STOPPED
>>> + when 'SHUTTING_DOWN'
>>> + return Instance::STATE_SHUTTING_DOWN
>>> + else
>>> + return Instance::STATE_PENDING
>>> + end
>>> + end
>>> +
>>> + private
>>> +
>>> + class HttpException< Exception
>>> + end
>>> +
>>> + def self.handle_dcloud_error(dcloud_instance)
>>> + raise HttpException, "Error creating dcloud instance, returned
internal server error." if dcloud_instance.class == Net::HTTPInternalServerError
>>> + end
>>> +
>>> + def self.handle_create_instance_error(task, ex)
>>> + Rails.logger.error ex.message
>>> + Rails.logger.error ex.backtrace.join("\n")
>>> + task.state = Task::STATE_FAILED
>>> + task.instance.state = Instance::STATE_CREATE_FAILED
>>> + raise ex
>>> + end
>>> +
>>> + def self.create_dcloud_instance(instance, match)
>>> + client = match.provider_account.connect
>>> +
>>> + overrides =
HardwareProfile.generate_override_property_values(instance.hardware_profile, match.hwp)
>>> +
>>> + client.create_instance(:image_id =>
match.provider_image.target_identifier,
>>> + :name => instance.name.tr("/",
"-"),
>>> + :hwp_id => match.hwp.external_key,
>>> + :hwp_memory => overrides[:memory],
>>> + :hwp_cpu => overrides[:cpu],
>>> + :hwp_storage => overrides[:storage],
>>> + :realm_id => (match.realm.external_key
rescue nil),
>>> + :keyname => (instance.instance_key.name))
>> commented out :keyname...
>>> + end
>>> +
>>> + def self.matches(instance)
>>> + matched, errors = instance.matches
>>> + if matched.empty?
>>> + raise "Could not find a matching backend provider, errors:
#{errors.join(', ')}"
>>> + end
>>> + matched
>>> + end
>>> +
>>> +end
>>> diff --git
a/src/db/migrate/20110714114553_remove_condor_job_id_from_instances.rb
b/src/db/migrate/20110714114553_remove_condor_job_id_from_instances.rb
>>> <snip>
>>
>> _______________________________________________
>> aeolus-devel mailing list
>> aeolus-devel(a)lists.fedorahosted.org
>>
https://fedorahosted.org/mailman/listinfo/aeolus-devel
>
------------------------------------------------------
Michal Fojtik, mfojtik(a)redhat.com
Deltacloud API:
http://deltacloud.org