Class: SwiftClient

Inherits:
Object
  • Object
show all
Defined in:
lib/swift_client.rb

Overview

Author

Murali Raju (<[email protected]>)

Copyright

Copyright © 2011 Murali Raju.

License

Apache License, Version 2.0

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

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.

Instance Method Summary collapse

Constructor Details

#initialize(type, auth_json) ⇒ SwiftClient

Returns a new instance of SwiftClient.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/swift_client.rb', line 20

def initialize(type, auth_json)
	auth_config = File.read(Dir.home + "/.swift-manager/authentication/#{auth_json}.json")
	@auth_params = JSON.parse(auth_config)

	case type
	when 's3'
		provider = @auth_params["provider"]
		aws_secret_access_key = @auth_params["keys"]["secret_access_key"]
		aws_access_key_id = @auth_params["keys"]["access_key_id"]
		begin
			puts 'Connecting to OpenStack Swift: S3 middleware...'.color(:green)
			puts ''
			@connection = Fog::Storage.new(	:provider => provider, 
											:aws_secret_access_key => aws_secret_access_key, 
											:aws_access_key_id => aws_access_key_id	)
		
		rescue Exception => e
			raise 'Unable to connect to OpenStack Swift: S3 middleware. Check authentication information or if the service is running'
		end

	when 'swift'
		provider = @auth_params["provider"]			
		swift_api_key = @auth_params["keys"]["secret_access_key"]
		swift_username = @auth_params["keys"]["access_key_id"]
		swift_auth_ip = @auth_params["url_ip"]

		begin
		Excon.defaults[:ssl_verify_peer] = false #SSL verify error on Mac OS X	
		puts 'Connecting to OpenStack Swift...'.color(:green)
		puts ''
		@connection = Fog::Storage.new({	:provider => provider,
											:rackspace_username => swift_username,
											:rackspace_api_key => swift_api_key,
											:rackspace_auth_url => "#{swift_auth_ip}:8080/auth/v1.0"})

		@cdn_connection = Fog::CDN.new({	:provider => provider,
											:rackspace_username => swift_username,
											:rackspace_api_key => swift_api_key,
											:rackspace_auth_url => "#{swift_auth_ip}:8080/auth/v1.0"}) 			

		rescue Exception => e
			raise 'Unable to connect to OpenStack Swift. Check authentication information or if the service is running.'
		end						
	end
end

Instance Method Details

#create_container_no_cdn(prefix) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/swift_client.rb', line 71

def create_container_no_cdn(prefix)
	@connection.directories.create(
				:key    => "#{prefix}-#{Time.now.to_i}", # globally unique name
				:public => false,
				:cdn => ""
	)
	sleep 1	
	list_containers
	puts ''
end

#create_containers_no_cdn(prefix, number_of) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/swift_client.rb', line 82

def create_containers_no_cdn(prefix, number_of)
	start_time = Time.now.to_f
	@count = 1
	number_of.times do
		create_container_no_cdn(prefix)
		end_time = Time.now.to_f
		time_elapsed = end_time - start_time
		puts "Time elapsed to create #{@count} containers: #{time_elapsed} seconds".bright
		@count += 1			
	end
end

#create_objects(path_to_local_files, container) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/swift_client.rb', line 125

def create_objects(path_to_local_files, container)
	directory = @connection.directories.get(container)
	files = Dir[path_to_local_files]
	for file in files do
		name = File.basename(file)
		begin
			unless directory.files.head(name)
				directory.files.create(:key => name, :body => open(file))
				puts "Uploading file #{name} to #{container}".bright
			else
				puts "File #{name} already exists. Please delete file and try again".bright
			end
		rescue Exception => e
			raise "Container #{container} does not exist. Please check and try again"
		end

	end
	puts ''
	puts 'Creating objects complete'.color(:green)
	puts ''
end

#delete_container(prefix) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/swift_client.rb', line 94

def delete_container(prefix)
	begin
		puts "Deleting container #{prefix}...".color(:green)
		@connection.directories.get(prefix).destroy
   		puts ''
	rescue Exception => e
		#Fog related bugs...tbd
		#raise 'Delete files first or check if the container lists using \'swift-manager list storage -t swift -f <auth> -i container\''
	end
end

#delete_containersObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/swift_client.rb', line 105

def delete_containers
	containers_json = @connection.directories.to_json
	containers = JSON.parse(containers_json)

	container_names = []
	containers.length.times do |i|
		container_names << containers[i]['key']
	end
	start_time = Time.now.to_f
	container_names.each do |container|
		delete_files(container)
		delete_container(container)
		end_time = Time.now.to_f
		@time_elapsed = end_time - start_time
	end
	puts ''
	puts "Time elapsed to delete #{containers.length} containers: #{@time_elapsed} seconds".bright
	puts ''
end

#delete_objects(container) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/swift_client.rb', line 151

def delete_objects(container)

    directory = @connection.directories.get(container)
    files = directory.files
    files.each do |f|
		f.destroy
    end
	puts ''
	puts "*Deleting all files in container #{container}".bright
end

#generate_json_outputObject



162
163
164
165
166
167
168
169
# File 'lib/swift_client.rb', line 162

def generate_json_output
	container_json_path = Dir.home + "/.swift-manager/shell/container.json"
	files_json_path 	= Dir.home + "/.swift-manager/shell/files.json"

	File.open(container_json_path, "w") do |f|        
	    f << @connection.directories.to_json
	end
end

#list_containersObject



67
68
69
# File 'lib/swift_client.rb', line 67

def list_containers
 	@connection.directories.table
end

#list_objects(container_name) ⇒ Object



147
148
149
# File 'lib/swift_client.rb', line 147

def list_objects(container_name)
	@connection.directories.get(container_name).files.table([:key, :last_modified, :content_length])			
end