swift-manager 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. data/.gitignore +2 -0
  2. data/Gemfile +19 -0
  3. data/LICENSE +201 -0
  4. data/README.md +56 -0
  5. data/README.rdoc +8 -0
  6. data/Rakefile +38 -0
  7. data/bin/swift-manager +337 -0
  8. data/docs/examples.txt +407 -0
  9. data/html/CloudShell.html +308 -0
  10. data/html/CloudShell/Context.html +403 -0
  11. data/html/Configurator.html +270 -0
  12. data/html/Formatter.html +272 -0
  13. data/html/Object.html +165 -0
  14. data/html/Parser.html +291 -0
  15. data/html/README_rdoc.html +102 -0
  16. data/html/SwiftClient.html +656 -0
  17. data/html/SwiftManager.html +181 -0
  18. data/html/bin/swift-manager.html +103 -0
  19. data/html/created.rid +9 -0
  20. data/html/images/brick.png +0 -0
  21. data/html/images/brick_link.png +0 -0
  22. data/html/images/bug.png +0 -0
  23. data/html/images/bullet_black.png +0 -0
  24. data/html/images/bullet_toggle_minus.png +0 -0
  25. data/html/images/bullet_toggle_plus.png +0 -0
  26. data/html/images/date.png +0 -0
  27. data/html/images/find.png +0 -0
  28. data/html/images/loadingAnimation.gif +0 -0
  29. data/html/images/macFFBgHack.png +0 -0
  30. data/html/images/package.png +0 -0
  31. data/html/images/page_green.png +0 -0
  32. data/html/images/page_white_text.png +0 -0
  33. data/html/images/page_white_width.png +0 -0
  34. data/html/images/plugin.png +0 -0
  35. data/html/images/ruby.png +0 -0
  36. data/html/images/tag_green.png +0 -0
  37. data/html/images/wrench.png +0 -0
  38. data/html/images/wrench_orange.png +0 -0
  39. data/html/images/zoom.png +0 -0
  40. data/html/index.html +119 -0
  41. data/html/js/darkfish.js +116 -0
  42. data/html/js/jquery.js +32 -0
  43. data/html/js/quicksearch.js +114 -0
  44. data/html/js/thickbox-compressed.js +10 -0
  45. data/html/lib/cloud_shell_rb.html +75 -0
  46. data/html/lib/configurator_rb.html +75 -0
  47. data/html/lib/formatter_rb.html +75 -0
  48. data/html/lib/parser_rb.html +75 -0
  49. data/html/lib/swift-manager_version_rb.html +75 -0
  50. data/html/lib/swift_client_rb.html +75 -0
  51. data/html/rdoc.css +763 -0
  52. data/lib/cloud_shell.rb +127 -0
  53. data/lib/configurator.rb +69 -0
  54. data/lib/formatter.rb +42 -0
  55. data/lib/parser.rb +60 -0
  56. data/lib/swift-manager_version.rb +21 -0
  57. data/lib/swift_client.rb +172 -0
  58. data/swift-manager.gemspec +48 -0
  59. data/swift-manager.rdoc +2 -0
  60. data/test/tc_nothing.rb +14 -0
  61. metadata +174 -0
@@ -0,0 +1,127 @@
1
+ #
2
+ # Author:: Murali Raju (<murali.raju@appliv.com>)
3
+ # Copyright:: Copyright (c) 2011 Murali Raju.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+
20
+ class CloudShell
21
+
22
+ #A basic read-only shell-like interface for browsing S3 or Swift JSON objects via fog.to_json using readline
23
+
24
+ def run(json_file)
25
+ root = JSON.parse(json_file)
26
+ command = nil
27
+
28
+ current_context = Context.new(root,nil)
29
+
30
+ Readline.completion_proc = proc { |input|
31
+ current_context.completions(input)
32
+ }
33
+
34
+ while command != 'exit'
35
+ command = Readline.readline("swift> ",true)
36
+ break if command.nil?
37
+ current_context = execute_command(command.strip,current_context)
38
+ end
39
+ end
40
+
41
+
42
+ def execute_command(command,current_context)
43
+ case command
44
+ when /^ls$/
45
+ puts current_context.to_s
46
+ when /^cd (.*$)/
47
+ new_context = current_context.cd($1)
48
+ if new_context.nil?
49
+ puts "No such key #{$1}"
50
+ else
51
+ current_context = new_context
52
+ end
53
+ when /^cat (.*)$/
54
+ item = current_context.cat($1)
55
+ if item.nil?
56
+ puts "No such item #{$1}"
57
+ else
58
+ puts item.inspect
59
+ end
60
+ when /^help$/
61
+ puts "cat <item> - print the contents of <item> in the current context"
62
+ puts "cd <item> - change context to the context of <item>"
63
+ puts "cd .. - change up one level"
64
+ puts "ls - list available items in the current context"
65
+ puts "exit - exit shell"
66
+ end
67
+ current_context
68
+ end
69
+
70
+ class Context
71
+ attr_reader :here
72
+ attr_reader :parent_context
73
+
74
+ def initialize(here,parent_context)
75
+ @here = here
76
+ @parent_context = parent_context
77
+ end
78
+
79
+ def completions(input)
80
+ self.to_s.split(/\s+/).grep(/^#{input}/)
81
+ end
82
+
83
+ def cat(path)
84
+ item_at(path)
85
+ end
86
+
87
+ def cd(path)
88
+ if path == '..'
89
+ self.parent_context
90
+ else
91
+ item = item_at(path)
92
+ if item.nil?
93
+ nil
94
+ else
95
+ Context.new(item,self)
96
+ end
97
+ end
98
+ end
99
+
100
+ def to_s
101
+ if self.here.kind_of? Array
102
+ indices = []
103
+ self.here.each_index { |i| indices << i }
104
+ indices.join(' ')
105
+ elsif self.here.kind_of? Hash
106
+ self.here.keys.join(' ')
107
+ else
108
+ self.here.to_s
109
+ end
110
+ end
111
+
112
+ private
113
+
114
+ def item_at(path)
115
+ if path == '..'
116
+ self.parent_context.here
117
+ elsif self.here.kind_of? Array
118
+ self.here[path.to_i]
119
+ elsif self.here.kind_of? Hash
120
+ self.here[path]
121
+ else
122
+ nil
123
+ end
124
+ end
125
+ end
126
+
127
+ end
@@ -0,0 +1,69 @@
1
+ # Author:: Murali Raju (<murali.raju@appliv.com>)
2
+ # Copyright:: Copyright (c) 2011 Murali Raju.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ class Configurator
19
+
20
+
21
+ def create_auth_config(auth_hash)
22
+ my_provider = auth_hash[:provider]
23
+ my_service = auth_hash[:service]
24
+ my_secret_access_key = auth_hash[:secret_access_key]
25
+ my_access_key_id = auth_hash[:access_key_id]
26
+ my_tag = auth_hash[:tag]
27
+ my_auth_url_ip = auth_hash[:url_ip]
28
+
29
+ auth_params = { :provider => my_provider, :service => my_service, :tag => my_tag, :url_ip => my_auth_url_ip,
30
+ :keys => { :secret_access_key => my_secret_access_key,
31
+ :access_key_id => my_access_key_id } }
32
+
33
+ if File.exists?( File.expand_path "~/.swift-manager/authentication/" )
34
+
35
+
36
+ @auth_config_path = Dir.home + "/.swift-manager/authentication/auth_config_#{my_tag}.json"
37
+ File.open(@auth_config_path, "w") do |f|
38
+ f << JSON.pretty_generate(auth_params)
39
+ end
40
+
41
+ formatter = Formatter.new
42
+ formatter.print_progress_bar_short
43
+
44
+ puts ''
45
+ puts "Adding authentication info to #{@auth_config_path}".color(:green)
46
+ puts ''
47
+ else
48
+
49
+ puts 'Looks like this is the first time you are running swift-manager. Let\'s setup the env...'.color(:green)
50
+ puts ''
51
+ formatter = Formatter.new
52
+ formatter.print_progress_bar_short
53
+
54
+ Dir.mkdir(File.expand_path "~/.swift-manager")
55
+ Dir.mkdir(File.expand_path "~/.swift-manager/authentication")
56
+ Dir.mkdir(File.expand_path "~/.swift-manager/log")
57
+ Dir.mkdir(File.expand_path "~/.swift-manager/shell")
58
+
59
+ @auth_config_path = Dir.home + "/.swift-manager/authentication/auth_config_#{my_tag}.json"
60
+
61
+ File.open(@auth_config_path, "w") do |f|
62
+ f << JSON.pretty_generate(auth_params)
63
+ end
64
+ puts ''
65
+ puts "Adding authentication info to #{@auth_config_path}".color(:green)
66
+ puts ''
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,42 @@
1
+
2
+ # Author:: Murali Raju (<murali.raju@appliv.com>)
3
+ # Copyright:: Copyright (c) 2011 Murali Raju.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+
20
+
21
+ class Formatter
22
+
23
+ def print_progress_bar_long
24
+ puts ''
25
+ bar = ProgressBar.new
26
+ 100.times do
27
+ sleep 0.075
28
+ bar.increment!
29
+ end
30
+ puts ''
31
+ end
32
+
33
+ def print_progress_bar_short
34
+ puts ''
35
+ bar = ProgressBar.new
36
+ 100.times do
37
+ sleep 0.0055
38
+ bar.increment!
39
+ end
40
+ puts ''
41
+ end
42
+ end
@@ -0,0 +1,60 @@
1
+ # Author:: Murali Raju (<murali.raju@appliv.com>)
2
+ # Copyright:: Copyright (c) 2011 Murali Raju.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ class Parser
19
+
20
+ #Parse swift-manager configs (JSON)
21
+
22
+ def list_auth_configs
23
+ auth_config_path = Dir.home + "/.swift-manager/authentication/*.json"
24
+ auth_configs = Dir.glob(auth_config_path)
25
+
26
+ #Display table output using the terminal-table gem
27
+ table = Terminal::Table.new :headings => ['Auth Config']
28
+ unless auth_configs.empty?
29
+ auth_configs.each do |row|
30
+ @row = []
31
+ @row << "#{File.basename(row, '.*').chomp(File.extname(row))}".bright
32
+ table << @row
33
+ end
34
+
35
+ end
36
+ puts table.to_s
37
+ puts ''
38
+
39
+ end
40
+
41
+ def show_auth_config(auth_json)
42
+ auth_config = File.read(Dir.home + "/.swift-manager/authentication/#{auth_json}.json")
43
+ auth_params = JSON.parse(auth_config)
44
+
45
+ #Display table output using the terminal-table gem
46
+ table = Terminal::Table.new :headings => [ 'Tag', 'Provider', 'Service', 'Secret Access Key', 'Access Key ID', 'Auth URL IP']
47
+ auth_params.each do |row|
48
+ @row = []
49
+ @row << auth_params["tag"]
50
+ @row << auth_params["provider"]
51
+ @row << auth_params["service"]
52
+ @row << auth_params["keys"]["secret_access_key"]
53
+ @row << auth_params["keys"]["access_key_id"]
54
+ @row << auth_params["url_ip"]
55
+ end
56
+ table << @row
57
+ puts table.to_s
58
+ puts ''
59
+ end
60
+ end
@@ -0,0 +1,21 @@
1
+ #
2
+ # Author:: Murali Raju (<murali.raju@appliv.com>)
3
+ # Copyright:: Copyright (c) 2011 Murali Raju.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ module SwiftManager
20
+ VERSION = '0.0.4'
21
+ end
@@ -0,0 +1,172 @@
1
+ # Author:: Murali Raju (<murali.raju@appliv.com>)
2
+ # Copyright:: Copyright (c) 2011 Murali Raju.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ class SwiftClient
19
+
20
+ def initialize(type, auth_json)
21
+ auth_config = File.read(Dir.home + "/.swift-manager/authentication/#{auth_json}.json")
22
+ @auth_params = JSON.parse(auth_config)
23
+
24
+ case type
25
+ when 's3'
26
+ provider = @auth_params["provider"]
27
+ aws_secret_access_key = @auth_params["keys"]["secret_access_key"]
28
+ aws_access_key_id = @auth_params["keys"]["access_key_id"]
29
+ begin
30
+ puts 'Connecting to OpenStack Swift: S3 middleware...'.color(:green)
31
+ puts ''
32
+ @connection = Fog::Storage.new( :provider => provider,
33
+ :aws_secret_access_key => aws_secret_access_key,
34
+ :aws_access_key_id => aws_access_key_id )
35
+
36
+ rescue Exception => e
37
+ raise 'Unable to connect to OpenStack Swift: S3 middleware. Check authentication information or if the service is running'
38
+ end
39
+
40
+ when 'swift'
41
+ provider = @auth_params["provider"]
42
+ swift_api_key = @auth_params["keys"]["secret_access_key"]
43
+ swift_username = @auth_params["keys"]["access_key_id"]
44
+ swift_auth_ip = @auth_params["url_ip"]
45
+
46
+ begin
47
+ Excon.defaults[:ssl_verify_peer] = false #SSL verify error on Mac OS X
48
+ puts 'Connecting to OpenStack Swift...'.color(:green)
49
+ puts ''
50
+ @connection = Fog::Storage.new({ :provider => provider,
51
+ :rackspace_username => swift_username,
52
+ :rackspace_api_key => swift_api_key,
53
+ :rackspace_auth_url => "#{swift_auth_ip}:8080/auth/v1.0"})
54
+
55
+ @cdn_connection = Fog::CDN.new({ :provider => provider,
56
+ :rackspace_username => swift_username,
57
+ :rackspace_api_key => swift_api_key,
58
+ :rackspace_auth_url => "#{swift_auth_ip}:8080/auth/v1.0"})
59
+
60
+ rescue Exception => e
61
+ raise 'Unable to connect to OpenStack Swift. Check authentication information or if the service is running.'
62
+ end
63
+ end
64
+ end
65
+
66
+
67
+ def list_containers
68
+ @connection.directories.table
69
+ end
70
+
71
+ def create_container_no_cdn(prefix)
72
+ @connection.directories.create(
73
+ :key => "#{prefix}-#{Time.now.to_i}", # globally unique name
74
+ :public => false,
75
+ :cdn => ""
76
+ )
77
+ sleep 1
78
+ list_containers
79
+ puts ''
80
+ end
81
+
82
+ def create_containers_no_cdn(prefix, number_of)
83
+ start_time = Time.now.to_f
84
+ @count = 1
85
+ number_of.times do
86
+ create_container_no_cdn(prefix)
87
+ end_time = Time.now.to_f
88
+ time_elapsed = end_time - start_time
89
+ puts "Time elapsed to create #{@count} containers: #{time_elapsed} seconds".bright
90
+ @count += 1
91
+ end
92
+ end
93
+
94
+ def delete_container(prefix)
95
+ begin
96
+ puts "Deleting container #{prefix}...".color(:green)
97
+ @connection.directories.get(prefix).destroy
98
+ puts ''
99
+ rescue Exception => e
100
+ #Fog related bugs...tbd
101
+ #raise 'Delete files first or check if the container lists using \'swift-manager list storage -t swift -f <auth> -i container\''
102
+ end
103
+ end
104
+
105
+ def delete_containers
106
+ containers_json = @connection.directories.to_json
107
+ containers = JSON.parse(containers_json)
108
+
109
+ container_names = []
110
+ containers.length.times do |i|
111
+ container_names << containers[i]['key']
112
+ end
113
+ start_time = Time.now.to_f
114
+ container_names.each do |container|
115
+ delete_files(container)
116
+ delete_container(container)
117
+ end_time = Time.now.to_f
118
+ @time_elapsed = end_time - start_time
119
+ end
120
+ puts ''
121
+ puts "Time elapsed to delete #{containers.length} containers: #{@time_elapsed} seconds".bright
122
+ puts ''
123
+ end
124
+
125
+ def create_objects(path_to_local_files, container)
126
+ directory = @connection.directories.get(container)
127
+ files = Dir[path_to_local_files]
128
+ for file in files do
129
+ name = File.basename(file)
130
+ begin
131
+ unless directory.files.head(name)
132
+ directory.files.create(:key => name, :body => open(file))
133
+ puts "Uploading file #{name} to #{container}".bright
134
+ else
135
+ puts "File #{name} already exists. Please delete file and try again".bright
136
+ end
137
+ rescue Exception => e
138
+ raise "Container #{container} does not exist. Please check and try again"
139
+ end
140
+
141
+ end
142
+ puts ''
143
+ puts 'Creating objects complete'.color(:green)
144
+ puts ''
145
+ end
146
+
147
+ def list_objects(container_name)
148
+ @connection.directories.get(container_name).files.table([:key, :last_modified, :content_length])
149
+ end
150
+
151
+ def delete_objects(container)
152
+
153
+ directory = @connection.directories.get(container)
154
+ files = directory.files
155
+ files.each do |f|
156
+ f.destroy
157
+ end
158
+ puts ''
159
+ puts "*Deleting all files in container #{container}".bright
160
+ end
161
+
162
+ def generate_json_output
163
+ container_json_path = Dir.home + "/.swift-manager/shell/container.json"
164
+ files_json_path = Dir.home + "/.swift-manager/shell/files.json"
165
+
166
+ File.open(container_json_path, "w") do |f|
167
+ f << @connection.directories.to_json
168
+ end
169
+ end
170
+
171
+
172
+ end