cloudcli 0.0.1

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 (3) hide show
  1. data/bin/cloudcli +31 -0
  2. data/lib/deltacloud-cli.rb +184 -0
  3. metadata +83 -0
data/bin/cloudcli ADDED
@@ -0,0 +1,31 @@
1
+ #!/opt/local/bin/ruby -rrubygems
2
+
3
+ require "readline"
4
+ require 'optparse'
5
+ require 'deltacloud'
6
+
7
+ options = {}
8
+ OptionParser.new do |opt|
9
+ opt.banner = "Usage: cloudcli [options]"
10
+ opt.on("-a", "--api=[URL]", 'Deltacloud API URL') { |v| options[:url] = v }
11
+ opt.on("-p", "--provider=[PROVIDER]", 'Deltacloud API provider URL') { |v| options[:provider] = v }
12
+ opt.on("-i", "--driver=[DRIVER]", 'Deltacloud API driver (ec2, gogrid, rackspace,...)') { |v| options[:driver] = v }
13
+ opt.on("-u", "--user=[USER]", 'Username') { |v| options[:user] = v }
14
+ opt.on("-p", "--password=[PASSWORD]", 'Password') { |v| options[:password] = v }
15
+ opt.parse!(ARGV)
16
+ end
17
+
18
+ def error(msg)
19
+ puts "ERROR: #{msg}"
20
+ exit(1)
21
+ end
22
+
23
+ error("Deltacloud API URL must be set") unless options[:url]
24
+ error("Deltacloud API username must be set") unless options[:user]
25
+ error("Deltacloud API password must be set") unless options[:password]
26
+
27
+ ENV['API_HOST'], ENV['API_USER'], ENV['API_PASSWORD'] = options[:url], options[:user], options[:password]
28
+ ENV['API_PROVIDER'] ||= options[:provider]
29
+ ENV['API_DRIVER'] ||= options[:driver]
30
+
31
+ require 'lib/deltacloud-cli'
@@ -0,0 +1,184 @@
1
+ begin
2
+ require 'ap'
3
+ rescue LoadError
4
+ require 'pp'
5
+ def ap(txt); pp txt; end
6
+ end
7
+
8
+ module DeltaCloud
9
+
10
+ module Operation
11
+ def self.init
12
+ @current_config = {
13
+ :username => ENV['API_USER'],
14
+ :password => ENV['API_PASSWORD'],
15
+ :provider => ENV['API_PROVIDER'],
16
+ :api => ENV['API_HOST']
17
+ }
18
+ begin
19
+ @client = DeltaCloud::new(@current_config[:username], @current_config[:password], @current_config[:api])
20
+ @current_config[:driver] = ENV['API_DRIVER'] || @client.driver_name
21
+ rescue Errno::ECONNREFUSED
22
+ puts "ERROR: Unable to connect: #{ENV['API_HOST']}"
23
+ exit(1)
24
+ end
25
+ end
26
+
27
+ def self.current_config(key=nil, value=nil)
28
+ @current_config[:"#{key}"]=value unless key.nil?
29
+ @current_config
30
+ end
31
+
32
+ def self.resources
33
+ Operation::client.entry_points
34
+ end
35
+
36
+ def self.client; @client ; end
37
+ def self.client=(client); @client=client; end
38
+
39
+ def self.execute(operation, resource, options='')
40
+ client = Operation::client
41
+ operation = operation.to_sym
42
+ resource.gsub!('-', '_')
43
+ case operation
44
+ when :get, :list then DeltaCloud::Operation::GET::new(operation, resource, options)
45
+ when :create, :get, :start, :stop, :reboot then DeltaCloud::Operation::POST::new(operation, resource, options)
46
+ when :destroy then DeltaCloud::Operation::DELETE::new(resource, options)
47
+ when :switch then DeltaCloud::Operation::switch(resource.to_sym, options)
48
+ end
49
+ end
50
+
51
+ def self.switch(resource, value)
52
+ Operation::current_config(resource, value)
53
+ client = Operation::client.with_config(Operation::current_config)
54
+ Operation::current_config
55
+ end
56
+
57
+ def self.help
58
+ help = "\n* Basic commands:\n\n"
59
+ help += " help\t\t- Print this help\n"
60
+ help += " exit\t\t- Exit to shell\n"
61
+ help += " config\t- Print current configuration\n"
62
+ help += " resources\t- Print collections available for current configuration\n\n"
63
+ help += "* Changing settings:\n"
64
+ help += " Every change will take effect immediately.\n\n"
65
+ help += " switch-driver DRIVER\t\t- Switch to other Deltacloud driver. (See also: list-drivers)\n"
66
+ help += " switch-provider PROVIDER\t- Switch provider for current Deltacloud driver.\n"
67
+ help += " switch-username USERNAME\t- Set Deltacloud username for current driver (Eg. API key).\n"
68
+ help += " switch-password PASSWORD\t- Set Deltacloud user password for current driver (Eg. API Secret key).\n\n"
69
+ help += "* Listing and getting resources:\n\n"
70
+ help += " list-COLLECTION \t\t- Will list all resources for given collection (Eg. list-images, See: resources)\n"
71
+ help += " get-RESOURCE ID \t\t- Get single resource identified by given ID (Eg. get-image img1)\n\n"
72
+ help += "* Creating and destroying new resources:\n\n"
73
+ help += " create-RESOURCE OPTIONS\t- Will create a new resource based on options (Eg. create-instance image_id:img1 hwp_id:m1-small)\n"
74
+ help += " destroy-RESOURCE ID\t\t- Will execute start operation on given resource (Eg. start-instance inst1)\n\n"
75
+ help += "* Managing resources:\n\n"
76
+ help += " start-RESOURCE ID\t\t- Will execute start operation on given resource (Eg. start-instance inst1)\n"
77
+ help += " stop-RESOURCE ID\t\t- Will execute stop operation on given resource (Eg. stop-instance inst1)\n"
78
+ help += " reboot-RESOURCE ID\t\t- Will execute reboot operation on given resource (Eg. reboot-instance inst1)\n"
79
+ help += "\n\n"
80
+ end
81
+
82
+ class POST
83
+
84
+ def initialize(operation, resource, options)
85
+ @operation, @resource, @options = operation, resource, options
86
+ instance_options = parse_opts(options)
87
+ puts instance_options.inspect
88
+ #begin
89
+ ap resource
90
+ @collection=case @operation
91
+ when :create then DeltaCloud::Operation::client.send(:"create_#{resource}", instance_options)
92
+ when :start, :stop, :reboot then DeltaCloud::Operation::client.send(:"#{resource}", options.strip).send(:"#{@operation}!")
93
+ end
94
+ #rescue NoMethodError
95
+ # ap "ERROR: Unknown resource #{resource}. Type 'resources' to list all resources"
96
+ #end
97
+ return self
98
+ end
99
+
100
+ def parse_opts(options)
101
+ opts = {}
102
+ options.split(' ').collect do |opt|
103
+ opts[:"#{opt.split(':').first}"] = opt.split(':').last
104
+ end
105
+ ap opts
106
+ opts
107
+ end
108
+
109
+ def to_s
110
+ if @operation != :create
111
+ GET::new(:get, @resource, @options).to_s
112
+ else
113
+ @collection.inspect
114
+ end
115
+ end
116
+
117
+ end
118
+
119
+ class GET
120
+
121
+ attr_reader :operation
122
+
123
+ def initialize(operation, resource, options)
124
+ @operation = operation
125
+ begin
126
+ if operation == :list
127
+ @collection = DeltaCloud::Operation::client.send(:"#{resource}")
128
+ end
129
+ if operation == :get
130
+ @collection = [DeltaCloud::Operation::client.send(:"#{resource}", options.strip)].compact
131
+ end
132
+ rescue NoMethodError
133
+ ap "ERROR: Unknown resource #{resource}. Type 'resources' to list all resources"
134
+ end
135
+ return self
136
+ end
137
+
138
+ def to_s
139
+ return '0 items returned' if not @collection
140
+ return '0 items returned' if @collection.empty?
141
+ output = @collection.collect do |item|
142
+ base = {
143
+ :id => item.id,
144
+ :name => item.name,
145
+ }
146
+ base.merge!(:state => item.state) if item.respond_to?(:state)
147
+ base.merge!(:description => item.description) if item.respond_to?(:description)
148
+ base
149
+ end
150
+ return output if @operation == :list
151
+ return output.first if @operation == :get
152
+ end
153
+
154
+ end
155
+
156
+ end
157
+
158
+ end
159
+
160
+ DeltaCloud::Operation::init
161
+ operation = ''
162
+
163
+ puts <<BANNER
164
+ Connected to Deltacloud API console (#{DeltaCloud::Operation::current_config[:api]})
165
+
166
+ BANNER
167
+
168
+
169
+ while operation!='exit'
170
+ buf = Readline.readline("#{DeltaCloud::Operation::current_config[:driver]}~ > ", true)
171
+ operation, resource = buf.split('-', 2)
172
+ case operation
173
+ when 'config' then (ap(DeltaCloud::Operation::current_config);next)
174
+ when 'resources' then (ap(DeltaCloud::Operation::resources);next)
175
+ when 'help' then (puts(DeltaCloud::Operation::help);next)
176
+ when 'exit' then next
177
+ end
178
+ if not operation or not resource
179
+ puts "ERROR: Unknown command. To get list of commands type 'help'"
180
+ next
181
+ end
182
+ resource, options = resource.split(' ', 2)
183
+ ap DeltaCloud::Operation::execute(operation, resource, options).to_s
184
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudcli
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Michal Fojtik
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-16 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: deltacloud-client
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 0
32
+ - 3
33
+ - 0
34
+ version: 0.3.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: A simple CLI tool for accessing Deltacloud API
38
+ email: mfojtik@redhat.com
39
+ executables:
40
+ - cloudcli
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - bin/cloudcli
47
+ - lib/deltacloud-cli.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/mifo/cloudcli
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.6.2
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: A simple CLI to interact with Deltacloud API server
82
+ test_files: []
83
+