diff --git a/install.sh b/install.sh index 857fa71b3e..d70daf408e 100755 --- a/install.sh +++ b/install.sh @@ -1094,8 +1094,13 @@ ECO_LIB_VIEW_FILES="src/cloud/ec2/lib/views/describe_images.erb \ ECO_BIN_FILES="src/cloud/ec2/bin/econe-server \ src/cloud/ec2/bin/econe-describe-images \ + src/cloud/ec2/bin/econe-describe-volumes \ src/cloud/ec2/bin/econe-describe-instances \ src/cloud/ec2/bin/econe-register \ + src/cloud/ec2/bin/econe-attach-volume \ + src/cloud/ec2/bin/econe-detach-volume \ + src/cloud/ec2/bin/econe-delete-volume \ + src/cloud/ec2/bin/econe-create-volume \ src/cloud/ec2/bin/econe-run-instances \ src/cloud/ec2/bin/econe-terminate-instances \ src/cloud/ec2/bin/econe-describe-addresses \ @@ -1107,7 +1112,12 @@ ECO_BIN_FILES="src/cloud/ec2/bin/econe-server \ ECO_BIN_CLIENT_FILES="src/cloud/ec2/bin/econe-describe-images \ src/cloud/ec2/bin/econe-describe-instances \ + src/cloud/ec2/bin/econe-describe-volumes \ src/cloud/ec2/bin/econe-register \ + src/cloud/ec2/bin/econe-attach-volume \ + src/cloud/ec2/bin/econe-detach-volume \ + src/cloud/ec2/bin/econe-delete-volume \ + src/cloud/ec2/bin/econe-create-volume \ src/cloud/ec2/bin/econe-run-instances \ src/cloud/ec2/bin/econe-terminate-instances \ src/cloud/ec2/bin/econe-describe-addresses \ diff --git a/src/cloud/ec2/bin/econe-attach-volume b/src/cloud/ec2/bin/econe-attach-volume new file mode 100755 index 0000000000..69db7c1daf --- /dev/null +++ b/src/cloud/ec2/bin/econe-attach-volume @@ -0,0 +1,150 @@ +#!/usr/bin/env ruby +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +ONE_LOCATION=ENV["ONE_LOCATION"] + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" +end + + +$: << RUBY_LIB_LOCATION +$: << RUBY_LIB_LOCATION+"/cloud" + +COMMANDS_HELP=<<-EOT +econe-attach-volume + +Attaches a DATABLOCK to a running instance and exposes it as the specified device. + +Usage: + econe-attach-volume [OPTIONS] VOLUME-ID + +Options: + + --help, -h + Show help + + --access-key , -K + The username of the user + + --secret-key , -S + The password of the user + + --url , -U + Set url as the web service url to use + + --instance , -i + The ID of the instance to attach the volume to + + --device , -d e + exit -1 +end + +volume_id = ARGV.shift + +if !volume_id + puts "#{cmd_name}: missing VOLUME-ID parameter" + exit -1 +end + +auth = "#{access}:#{secret}" if secret && access + +begin + ec2_client = EC2QueryClient::Client.new(auth,url) +rescue Exception => e + puts "#{cmd_name}: #{e.message}" + exit -1 +end + +rc = ec2_client.attach_volume(volume_id, instance, device) + +if CloudClient::is_error?(rc) + puts "#{cmd_name}: #{rc.message}" + exit -1 +end + +fmt = "%-15s %-15s %-10s %s" + +if headers + puts fmt % ["volumeId", "instanceId", "status", "device"] + puts "------------------------------------------------------------------------------" +end + +puts fmt % [rc['volumeId'], rc['instanceId'], rc['status'], rc['device']] + +exit 0 diff --git a/src/cloud/ec2/bin/econe-create-volume b/src/cloud/ec2/bin/econe-create-volume new file mode 100755 index 0000000000..3a18eb86b1 --- /dev/null +++ b/src/cloud/ec2/bin/econe-create-volume @@ -0,0 +1,138 @@ +#!/usr/bin/env ruby +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +ONE_LOCATION=ENV["ONE_LOCATION"] + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" +end + + +$: << RUBY_LIB_LOCATION +$: << RUBY_LIB_LOCATION+"/cloud" + +COMMANDS_HELP=<<-EOT +econe-create-volume + +Create a new DATABLOCK + +Usage: + econe-create-volume [OPTIONS] + +Options: + + --help, -h + Show help + + --access-key , -K + The username of the user + + --secret-key , -S + The password of the user + + --url , -U + Set url as the web service url to use + + --size , -s + The ID of the instance to attach the volume to + + --headers, -H + Display column headers + +EOT + +require 'econe/EC2QueryClient' +require 'CloudClient' +require 'getoptlong' + +include CloudCLI + +opts = GetoptLong.new( + ['--help', '-h',GetoptLong::NO_ARGUMENT], + ['--version', '-v',GetoptLong::NO_ARGUMENT], + ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], + ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], + ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], + ['--size', '-s',GetoptLong::REQUIRED_ARGUMENT], + ['--headers', '-H',GetoptLong::NO_ARGUMENT] + ) + +headers = false +url = nil +access = nil +secret = nil +auth = nil +size = nil + +begin + opts.each do |opt, arg| + case opt + when '--help' + puts COMMANDS_HELP + return + when '--version' + puts CloudCLI.version_text + exit 0 + when '--access-key' + access = arg + when '--secret-key' + secret = arg + when '--url' + url = arg + when '--size' + size = arg + when '--headers' + headers = true + end + end +rescue Exception => e + exit -1 +end + +if !size + puts "#{cmd_name}: missing --size option" + exit -1 +end + +auth = "#{access}:#{secret}" if secret && access + +begin + ec2_client = EC2QueryClient::Client.new(auth,url) +rescue Exception => e + puts "#{cmd_name}: #{e.message}" + exit -1 +end + +rc = ec2_client.create_volume(size) + +if CloudClient::is_error?(rc) + puts "#{cmd_name}: #{rc.message}" + exit -1 +end + +fmt = "%-15s %-10s %s" + +if headers + puts fmt % ["volumeId", "size", "createTime"] + puts "------------------------------------------------------------------------------" +end + +puts fmt % [rc['volumeId'], rc['size'], rc['createTime']] + +exit 0 diff --git a/src/cloud/ec2/bin/econe-delete-volume b/src/cloud/ec2/bin/econe-delete-volume new file mode 100755 index 0000000000..6492e37b53 --- /dev/null +++ b/src/cloud/ec2/bin/econe-delete-volume @@ -0,0 +1,126 @@ +#!/usr/bin/env ruby +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +ONE_LOCATION=ENV["ONE_LOCATION"] + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" +end + + +$: << RUBY_LIB_LOCATION +$: << RUBY_LIB_LOCATION+"/cloud" + +COMMANDS_HELP=<<-EOT +econe-delete-volume + +Deletes a DATABLOCK + +Usage: + econe-delete-volume [OPTIONS] VOLUME-ID + +Options: + + --help, -h + Show help + + --access-key , -K + The username of the user + + --secret-key , -S + The password of the user + + --url , -U + Set url as the web service url to use + + --headers, -H + Display column headers + +VOLUME-ID: The ID of the DATABLOCK + +EOT + +require 'econe/EC2QueryClient' +require 'CloudClient' +require 'getoptlong' + +include CloudCLI + +opts = GetoptLong.new( + ['--help', '-h',GetoptLong::NO_ARGUMENT], + ['--version', '-v',GetoptLong::NO_ARGUMENT], + ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], + ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], + ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], + ['--headers', '-H',GetoptLong::NO_ARGUMENT] + ) + +headers = false +url = nil +access = nil +secret = nil +auth = nil + +begin + opts.each do |opt, arg| + case opt + when '--help' + puts COMMANDS_HELP + return + when '--version' + puts CloudCLI.version_text + exit 0 + when '--access-key' + access = arg + when '--secret-key' + secret = arg + when '--url' + url = arg + when '--headers' + headers = true + end + end +rescue Exception => e + exit -1 +end + +volume_id = ARGV.shift + +if !volume_id + puts "#{cmd_name}: missing VOLUME-ID parameter" + exit -1 +end + +auth = "#{access}:#{secret}" if secret && access + +begin + ec2_client = EC2QueryClient::Client.new(auth,url) +rescue Exception => e + puts "#{cmd_name}: #{e.message}" + exit -1 +end + +rc = ec2_client.delete_volume(volume_id) + +if CloudClient::is_error?(rc) + puts "#{cmd_name}: #{rc.message}" + exit -1 +end + +exit 0 diff --git a/src/cloud/ec2/bin/econe-describe-volumes b/src/cloud/ec2/bin/econe-describe-volumes new file mode 100755 index 0000000000..ea7e9042aa --- /dev/null +++ b/src/cloud/ec2/bin/econe-describe-volumes @@ -0,0 +1,133 @@ +#!/usr/bin/env ruby +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +ONE_LOCATION=ENV["ONE_LOCATION"] + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" +end + + +$: << RUBY_LIB_LOCATION +$: << RUBY_LIB_LOCATION+"/cloud" + +COMMANDS_HELP=<<-EOT +econe-describe-volumes + +Describes your Amazon EBS volumes + +Usage: + econe-describe-volumes [OPTIONS] + +Options: + --help, -h + Show help + + --access-key , -K + The username of the user + + --secret-key , -S + The password of the user + + --url , -U + Set url as the web service url to use + + --headers, -H + Display column headers + +EOT + +require 'econe/EC2QueryClient' +require 'CloudClient' +require 'getoptlong' + +include CloudCLI + + +opts = GetoptLong.new( + ['--help', '-h',GetoptLong::NO_ARGUMENT], + ['--version', '-v',GetoptLong::NO_ARGUMENT], + ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], + ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], + ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], + ['--headers', '-H',GetoptLong::NO_ARGUMENT] + ) + +headers = false +url = nil +access = nil +secret = nil +auth = nil + +begin + opts.each do |opt, arg| + case opt + when '--help' + puts COMMANDS_HELP + return + when '--version' + puts CloudCLI.version_text + exit 0 + when '--access-key' + access = arg + when '--secret-key' + secret = arg + when '--url' + url = arg + when '--headers' + headers = true + end + end +rescue Exception => e + exit -1 +end + +auth = "#{access}:#{secret}" if secret && access + +begin + ec2_client = EC2QueryClient::Client.new(auth,url) +rescue Exception => e + puts "#{cmd_name}: #{e.message}" + exit -1 +end + +rc = ec2_client.describe_volumes + +if CloudClient::is_error?(rc) + puts "#{cmd_name}: #{rc.message}" + exit -1 +end + +volumes = rc['volumeSet']['item'] + +fmt = "%-15s %-10s %s" + +if headers + puts fmt % ["volumeId", "size", "createTime"] + puts "------------------------------------------------------------------------------" +end + +if volumes + volumes.each { |vol| + puts fmt % [vol['volumeId'], vol['size'], vol['createTime']] + } +end + +exit 0 + diff --git a/src/cloud/ec2/bin/econe-detach-volume b/src/cloud/ec2/bin/econe-detach-volume new file mode 100755 index 0000000000..5238b3bb3d --- /dev/null +++ b/src/cloud/ec2/bin/econe-detach-volume @@ -0,0 +1,156 @@ +#!/usr/bin/env ruby +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +ONE_LOCATION=ENV["ONE_LOCATION"] + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" +end + + +$: << RUBY_LIB_LOCATION +$: << RUBY_LIB_LOCATION+"/cloud" + +COMMANDS_HELP=<<-EOT +econe-detach-volume + +Detaches an Amazon EBS volume from an instance. Make sure to unmount any +file systems on the device within your operating system before detaching the volume + +Usage: + econe-detach-volume [OPTIONS] VOLUME-ID + +Options: + + --help, -h + Show help + + --access-key , -K + The username of the user + + --secret-key , -S + The password of the user + + --url , -U + Set url as the web service url to use + + --instance , -i + The ID of the instance to attach the volume to + + --device , -d e + exit -1 +end + +volume_id = ARGV.shift + +if !volume_id + puts "#{cmd_name}: missing VOLUME-ID parameter" + exit -1 +end + +if !instance + puts "#{cmd_name}: missing --instance option" + exit -1 +end + +auth = "#{access}:#{secret}" if secret && access + +begin + ec2_client = EC2QueryClient::Client.new(auth,url) +rescue Exception => e + puts "#{cmd_name}: #{e.message}" + exit -1 +end + +rc = ec2_client.detach_volume(volume_id, instance, device) + +if CloudClient::is_error?(rc) + puts "#{cmd_name}: #{rc.message}" + exit -1 +end + +fmt = "%-15s %-15s %-10s %s" + +if headers + puts fmt % ["volumeId", "instanceId", "status", "device"] + puts "------------------------------------------------------------------------------" +end + +puts fmt % [rc['volumeId'], rc['instanceId'], rc['status'], rc['device']] + +exit 0 diff --git a/src/cloud/ec2/lib/EC2QueryClient.rb b/src/cloud/ec2/lib/EC2QueryClient.rb index 2615bcf849..32170f10fc 100644 --- a/src/cloud/ec2/lib/EC2QueryClient.rb +++ b/src/cloud/ec2/lib/EC2QueryClient.rb @@ -268,7 +268,7 @@ module EC2QueryClient def associate_address(public_ip, instance_id) begin response = @ec2_connection.associate_address( - :public_ip => public_ip, + :public_ip => public_ip, :instance_id => instance_id) rescue Exception => e error = CloudClient::Error.new(e.message) @@ -305,5 +305,93 @@ module EC2QueryClient return response end + + ###################################################################### + # + # + ###################################################################### + def describe_volumes + begin + response = @ec2_connection.describe_volumes + rescue Exception => e + error = CloudClient::Error.new(e.message) + return error + end + + return response + end + + ###################################################################### + # + # + ###################################################################### + def attach_volume(volume, instance, device) + begin + response = @ec2_connection.attach_volume( + :volume_id => volume, + :instance_id => instance, + :device => device + ) + rescue Exception => e + error = CloudClient::Error.new(e.message) + return error + end + + return response + end + + ###################################################################### + # + # + ###################################################################### + def delete_volume(volume) + begin + response = @ec2_connection.delete_volume( + :volume_id => volume + ) + rescue Exception => e + error = CloudClient::Error.new(e.message) + return error + end + + return response + end + + ###################################################################### + # + # + ###################################################################### + def detach_volume(volume, instance, device) + begin + response = @ec2_connection.detach_volume( + :volume_id => volume, + :instance_id => instance, + :device => device + ) + rescue Exception => e + error = CloudClient::Error.new(e.message) + return error + end + + return response + end + + ###################################################################### + # + # + ###################################################################### + def create_volume(size) + begin + response = @ec2_connection.create_volume( + :size => size, + :availability_zone => 'default' + ) + rescue Exception => e + error = CloudClient::Error.new(e.message) + return error + end + + return response + end end end