app42 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +119 -0
  6. data/Rakefile +5 -0
  7. data/TODO.txt +36 -0
  8. data/app42.gemspec +41 -0
  9. data/bin/app42 +15 -0
  10. data/lib/app42.rb +23 -0
  11. data/lib/app42/base/constants.rb +33 -0
  12. data/lib/app42/base/help.rb +717 -0
  13. data/lib/app42/base/http_helper.rb +46 -0
  14. data/lib/app42/base/message.rb +35 -0
  15. data/lib/app42/base/shell.rb +39 -0
  16. data/lib/app42/base/util.rb +304 -0
  17. data/lib/app42/client/app42_rest_client.rb +451 -0
  18. data/lib/app42/client/rest_util.rb +66 -0
  19. data/lib/app42/command/app.rb +194 -0
  20. data/lib/app42/command/authorize.rb +29 -0
  21. data/lib/app42/command/base.rb +660 -0
  22. data/lib/app42/command/client.rb +232 -0
  23. data/lib/app42/command/config.rb +185 -0
  24. data/lib/app42/command/info.rb +101 -0
  25. data/lib/app42/command/service.rb +196 -0
  26. data/lib/app42/command/user.rb +140 -0
  27. data/lib/app42/command/user_key.rb +68 -0
  28. data/lib/app42/version.rb +13 -0
  29. data/spec/app42/base/constants_spec.rb +11 -0
  30. data/spec/app42/command/app_spec.rb +103 -0
  31. data/spec/app42/command/base_spec.rb +7 -0
  32. data/spec/app42/command/config_spec.rb +20 -0
  33. data/spec/app42/command/info_spec.rb +27 -0
  34. data/spec/app42/command/service_spec.rb +98 -0
  35. data/spec/app42/command/user_spec.rb +7 -0
  36. data/spec/app42/command/user_token_spec.rb +40 -0
  37. data/spec/app42/version_spec.rb +7 -0
  38. data/spec/app42_spec.rb +2 -0
  39. data/spec/data/info.yml +16 -0
  40. data/spec/data/services.yml +25 -0
  41. data/spec/data/state.yml +16 -0
  42. data/spec/data/user_services.yml +18 -0
  43. data/spec/spec_helper.rb +18 -0
  44. metadata +257 -0
@@ -0,0 +1,232 @@
1
+
2
+ require 'paint'
3
+ require 'optparse'
4
+
5
+ # A Ruby class to call the App42 REST API.
6
+
7
+ module App42
8
+ module Command
9
+ class Client
10
+
11
+ # read access
12
+ attr_reader :kclass
13
+ attr_reader :command
14
+ attr_reader :args
15
+ attr_reader :options
16
+
17
+ # @return [Object]
18
+ def initialize(arg = [])
19
+ @args = args
20
+ @options = {}
21
+ @should_exit = true
22
+ @exception_msg = nil
23
+ end
24
+
25
+ # pre load all commands file
26
+ def load_command_file
27
+ Dir[File.join(File.dirname(__FILE__), "*.rb")].each { |file| require file }
28
+ end
29
+
30
+ # @param args
31
+ # call for require files
32
+ # Initiate client object
33
+ def self.app42(*args )
34
+ begin
35
+ action = args.shift.strip rescue 'help'
36
+ App42::Command::Client.new.load_command_file
37
+ App42::Command::Client.new.start(action, args)
38
+ rescue Interrupt => e
39
+ @exception_msg = e.message
40
+ rescue OptionParser::MissingArgument => e
41
+ @exception_msg = e.message
42
+ rescue OptionParser::InvalidOption => e
43
+ @exception_msg = e.message
44
+ rescue OptionParser::AmbiguousOption => e
45
+ @exception_msg = e.message
46
+ rescue => error
47
+ @exception_msg = e.message
48
+ ensure
49
+ puts Paint["#{@exception_msg}", :red]
50
+ @should_exit = true if @should_exit.nil?
51
+ exit @should_exit
52
+ end
53
+
54
+ end
55
+
56
+ # Collect all the available options for all commands
57
+ # Some duplicates exists to capture all scenarios
58
+ def parse_options(command, args)
59
+ opts_parser = OptionParser.new do |opts|
60
+ opts.banner = "\nAvailable options:\n\n"
61
+
62
+ opts.on('--api_key API_KEY') { |api| @options[:api] = api }
63
+ opts.on('--secret_key SECRET_KEY') { |secret| @options[:secret] = secret }
64
+ opts.on('-a API_KEY') { |api| @options[:api] = api }
65
+ opts.on('-s SECRET_KEY') { |secret| @options[:secret] = secret }
66
+
67
+ opts.on('--app NAME') { |name| @options[:name] = name }
68
+ opts.on('--name NAME') { |name| @options[:name] = name }
69
+
70
+ opts.on('--instance INSTANCE') { |instance| @options[:instance] = instance }
71
+ opts.on('-i INSTANCE') { |instance| @options[:instance] = instance }
72
+
73
+ opts.on('--service SERVICE') { |service| @options[:service] = service }
74
+ opts.on('-s SERVICE') { |service| @options[:service] = service }
75
+
76
+ opts.on('-h', '--help') { puts "#{App42::Base::Help.usage(command)}\n"; exit! }
77
+ end
78
+ opts_parser.parse!(args)
79
+ self
80
+ end
81
+
82
+ # @param [Object] command
83
+ # @param [Object] args
84
+ def start(command, args = [])
85
+ if is_available?(command)
86
+ parse_options(command, args)
87
+ execute command
88
+ cmd = App42::Command.const_get(@kclass.to_s.capitalize)
89
+ begin
90
+ cmd.new(@options).send(@command)
91
+ rescue Interrupt
92
+ puts Paint[" Command cancelled.", :red]
93
+ exit!
94
+ rescue Exception => e
95
+ puts e
96
+ end
97
+ elsif command == 'help'
98
+ send(command)
99
+ else
100
+ puts Paint["app42: Unknown command [#{command}]", :red]
101
+ App42::Base::Help.how_to
102
+ end
103
+ end
104
+
105
+
106
+ # @return true OR false
107
+ def is_available? command
108
+ App42::Base::APP42_COMMAND.include?(command)
109
+ end
110
+
111
+ # choose action respective of class
112
+ def execute command
113
+ case command
114
+ when 'version'
115
+ set_cmd(:config, :version)
116
+
117
+ when 'list'
118
+ set_cmd(:config, :list)
119
+
120
+ when 'apps'
121
+ set_cmd(:app, :apps)
122
+
123
+ when 'deploy'
124
+ set_cmd(:app, :deploy)
125
+
126
+ when 'setupInfra'
127
+ set_cmd(:app, :setup_infra)
128
+
129
+ when 'update'
130
+ set_cmd(:app, :deploy)
131
+
132
+ when 'keys'
133
+ set_cmd(:user, :keys)
134
+
135
+ when 'clearKeys'
136
+ set_cmd(:user, :clear)
137
+
138
+ when 'addKeys'
139
+ set_cmd(:user, :add)
140
+
141
+ when 'scale'
142
+ set_cmd(:app, :scale)
143
+
144
+ when 'descale'
145
+ set_cmd(:app, :descale)
146
+
147
+ when 'appState'
148
+ set_cmd(:info, :state)
149
+
150
+ when 'appInfo'
151
+ set_cmd(:info, :info)
152
+
153
+ when 'logs'
154
+ set_cmd(:info, :logs)
155
+
156
+ when 'start'
157
+ set_cmd(:app, :start)
158
+
159
+ when 'stop'
160
+ set_cmd(:app, :stop)
161
+
162
+ when 'restart'
163
+ set_cmd(:app, :restart)
164
+
165
+ when 'deleteInfra'
166
+ set_cmd(:app, :delete)
167
+
168
+ when 'releases'
169
+ set_cmd(:info, :releases)
170
+
171
+ when 'iaasProviders'
172
+ set_cmd(:config, :iaas_providers)
173
+
174
+ when 'frameworks'
175
+ set_cmd(:config, :frameworks)
176
+
177
+ when 'runtimes'
178
+ set_cmd(:config, :runtimes)
179
+
180
+ when 'vmconfigs'
181
+ set_cmd(:config, :memory)
182
+
183
+ when 'app42-update'
184
+ set_cmd(:config, :update)
185
+
186
+ when 'supportedServices'
187
+ set_cmd(:service, :app42pass_services)
188
+
189
+ when 'services'
190
+ set_cmd(:service, :services)
191
+
192
+ when 'serviceInfo'
193
+ set_cmd(:service, :info)
194
+
195
+ when 'createService'
196
+ set_cmd(:service, :create)
197
+
198
+ when 'deleteService'
199
+ set_cmd(:service, :delete)
200
+
201
+ when 'resetServicePassword'
202
+ set_cmd(:service, :reset_pass)
203
+
204
+ when 'bindIP'
205
+ set_cmd(:service, :service_bind)
206
+
207
+ when 'unbindIP'
208
+ set_cmd(:service, :service_unbind)
209
+
210
+ when 'bindInfo'
211
+ set_cmd(:service, :service_bindInfo)
212
+
213
+ else
214
+ puts Paint["app42: Unknown command [#{action}]", :red]
215
+ App42::Base::Help.how_to
216
+ end
217
+ end
218
+
219
+ # Set class and respective methods
220
+ def set_cmd(kclass, command)
221
+ @kclass = kclass
222
+ @command = command
223
+ end
224
+
225
+ # invoke help options
226
+ def help
227
+ App42::Base::Help.commands
228
+ end
229
+
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,185 @@
1
+ module App42::Command
2
+ class Config < Base
3
+
4
+ # This version number is used as the RubyGem release version.
5
+ def version
6
+ puts message "App42 current version: #{App42::Version::VERSION::VERSION}", false, 'green'
7
+ end
8
+
9
+ # This will list all app42 available commands.
10
+ def list
11
+ message "All app42 available commands", true, 'blue'
12
+ App42::Base::APP42_COMMAND.select { |action| puts Paint["app42 #{action}", :green]}
13
+ end
14
+
15
+ #
16
+ # return list of virtual machine type
17
+ #
18
+ def get_vm_type
19
+ build_get_request params, 'info', 'subscription'
20
+ end
21
+
22
+ #
23
+ # return list of iaas_providers
24
+ #
25
+ def get_iaas_provider
26
+ build_get_request params, 'info', 'iaasproviders'
27
+ end
28
+
29
+ #
30
+ # return list of runtimes
31
+ #
32
+ def get_runtimes
33
+ build_get_request params, 'info', 'runtimes'
34
+ end
35
+
36
+ #
37
+ # return list of templates
38
+ #
39
+ def get_frameworks iaas, vm_type, rt
40
+ query_params = params
41
+ query_params.store('vmType', vm_type)
42
+ query_params.store('iaas', iaas)
43
+ query_params.store('runtime', rt)
44
+ build_get_request query_params, 'info', 'frameworks'
45
+ end
46
+
47
+ #
48
+ # return list of web server
49
+ #
50
+ def get_webservers iaas, vm_type, rt, framework
51
+ query_params = params
52
+ query_params.store('vmType', vm_type)
53
+ query_params.store('iaas', iaas)
54
+ query_params.store('runtime', rt)
55
+ query_params.store('framework', framework)
56
+ build_get_request query_params, 'info', 'webserver'
57
+ end
58
+
59
+ #
60
+ # return list of operating system for app
61
+ #
62
+ def get_operating_sys_for_app iaas, vm_type, rt, framework, webserver
63
+ query_params = params
64
+ query_params.store('vmType', vm_type)
65
+ query_params.store('iaas', iaas)
66
+ query_params.store('runtime', rt)
67
+ query_params.store('framework', framework)
68
+ query_params.store('webServer', webserver)
69
+ build_get_request query_params, 'info/app', 'os'
70
+ end
71
+
72
+ #
73
+ # return list of operating system for service
74
+ #
75
+ def get_operating_sys_for_service iaas, vm_type, service
76
+ query_params = params
77
+ query_params.store('vmType', vm_type)
78
+ query_params.store('iaas', iaas)
79
+ query_params.store('service', service)
80
+ build_get_request query_params, 'info/service', 'os'
81
+ end
82
+
83
+ #
84
+ # return list of vm_configuration(memory)
85
+ #
86
+ def get_vmconfig vm_type, iaas
87
+ query_params = params
88
+ query_params.store('vmType', vm_type)
89
+ query_params.store('iaas', iaas)
90
+ build_get_request query_params, 'info', 'memory'
91
+ end
92
+
93
+ #
94
+ # return list of iaas_provider
95
+ #
96
+ def iaas_providers
97
+ rows, rows_header_final, rows_header = [], [], nil
98
+ get_iaas_provider['iaas'].each do |iaas|
99
+ iaas.delete('id')
100
+ rows_header = iaas.keys
101
+ rows << iaas.values
102
+ end
103
+
104
+ rows_header.map { |e| rows_header_final << camel_case_to_whitespace(e) }
105
+
106
+ table = Terminal::Table.new :title => Paint["=== Available IaaS Providers ===", :green], :headings => rows_header_final, :rows => rows
107
+ puts table
108
+ end
109
+
110
+ #
111
+ # list app42paas supported frameworks
112
+ #
113
+ def frameworks
114
+ runtime = get_runtime
115
+ vm_type = get_vm_types
116
+ iaas = get_iaas_providers
117
+ framework = get_frameworks iaas, vm_type, runtime
118
+ rows = []
119
+ framework['frameworks'].each_pair do |key, value|
120
+ rows << Array(value.capitalize)
121
+ end unless framework['frameworks'].nil?
122
+
123
+ table = Terminal::Table.new :title => Paint["=== Available Frameworks ===", :green], :rows => rows
124
+ puts table
125
+ print_new_line
126
+ end
127
+
128
+ #
129
+ # list app42paas supported vm_type
130
+ #
131
+ def vm_type
132
+ vm_type = get_config 'config', __method__
133
+ puts Paint["=== Available virtual machine type ===", :green]
134
+ vm_type['vm_type'].each do |vt|
135
+ puts vt.capitalize
136
+ end
137
+ end
138
+
139
+ #
140
+ # list app42paas supported runtimes
141
+ #
142
+ def runtimes
143
+ rows, rows_header_final, rows_header = [], [], nil
144
+
145
+ get_runtimes['runtimes'].each do |rt|
146
+ rt.delete('id')
147
+ rows_header = rt.keys
148
+ rows << rt.values
149
+ end
150
+
151
+ rows_header.map { |e| rows_header_final << camel_case_to_whitespace(e) }
152
+
153
+ table = Terminal::Table.new :title => Paint["=== Available Runtimes ===", :green], :headings => rows_header_final, :rows => rows
154
+ puts table
155
+ end
156
+
157
+ #
158
+ # list app42paas supported memory
159
+ #
160
+ def memory
161
+ vm_type = get_vm_types
162
+ iaas = get_iaas_providers
163
+ vmconfig = get_vmconfig vm_type, iaas
164
+ rows = []
165
+ vmconfig['vmconfig'].each_pair do |key, value|
166
+ rows << Array(value.capitalize)
167
+ end
168
+
169
+ table = Terminal::Table.new :title => Paint["=== Available vmconfigs ===", :green], :rows => rows
170
+ puts table
171
+ print_new_line
172
+ end
173
+
174
+ #
175
+ # update the app42paas client
176
+ #
177
+ def update
178
+ puts 'Uninstalling current app42 client'
179
+ system('gem uninstall app42')
180
+ puts "Installing latest app42 client"
181
+ system ("gem install app42-0.5.0.gem")
182
+ end
183
+
184
+ end
185
+ end
@@ -0,0 +1,101 @@
1
+
2
+ require 'app42/command/user_key'
3
+
4
+ module App42
5
+ module Command
6
+ class Info < Base
7
+
8
+
9
+ #
10
+ # show detailed app information
11
+ #
12
+ #Examples:
13
+ #
14
+ # $ app42 info --app APP
15
+ # === Demo info
16
+ # Memory 512
17
+ # Framework rails
18
+ # Runtime ruby20
19
+ # Domain demo.app42paas.com
20
+ # User jon
21
+ # Status running
22
+ #
23
+ def info
24
+ @options[:name] = get_app_name if @options[:name].nil?
25
+
26
+ rows, rows_header_final, rows_header = [], [], nil
27
+ app_info = app_information 'app', @options[:name]
28
+ if app_info && app_info['appInfo']
29
+ rows_header = app_info['appInfo'].keys
30
+ rows << app_info['appInfo'].values
31
+ rows_header.map { |e| rows_header_final << camel_case_to_whitespace(e) }
32
+
33
+ table = Terminal::Table.new :title => Paint["=== #{@options[:name]} Info ===", :green], :headings => rows_header_final, :rows => rows
34
+ puts table
35
+ end
36
+ end
37
+
38
+ # show app status information
39
+ #
40
+ #Examples:
41
+ #
42
+ # $ app42 status --app APP
43
+ #=== Demo status
44
+ # running
45
+ #
46
+ def state
47
+ @options[:name] = get_app_name if @options[:name].nil?
48
+ rows = []
49
+ rows_header = nil
50
+ app_status = app_information 'app', @options[:name]
51
+ rows_header = Array('State')
52
+ rows << Array(app_status['appInfo']['appStatus'])
53
+
54
+ table = Terminal::Table.new :title => Paint["=== #{@options[:name]} State ===", :green], :rows => rows
55
+ puts table
56
+ end
57
+
58
+ # Returns the log URLs for the application
59
+ def logs
60
+ @options[:name] = get_app_name if @options[:name].nil?
61
+ app_logs = app_information 'app/logs', @options[:name] if is_app_exist? @options[:name]
62
+ rows, rows_header = [], nil
63
+ rows_header = 'Log URL(s)'
64
+ app_logs['logUrls'].each do |log|
65
+ rows << Array(log)
66
+ end
67
+ message "#{Message::LOG_MESSAGE}", true, 'green'
68
+ table = Terminal::Table.new :title => rows_header, :rows => rows
69
+ puts table
70
+ end
71
+
72
+ #
73
+ # list releases
74
+ #
75
+ #Examples:
76
+ #
77
+ # $ app42 releases --app APP
78
+ # === Demo releases
79
+ # Version v1
80
+ #
81
+ # Email test@gmail.com
82
+ #
83
+ # Date 20/02/2010
84
+ #
85
+ #TODO , NEED TO SYNC WITH SERVICE
86
+ def releases
87
+ @options[:name] = get_app_name if @options[:name].nil?
88
+ app_releases = app_information 'releases', @options[:name]
89
+ puts Paint["=== #{@options[:name]} releases ===", :green]
90
+ app_releases['releaseInfo'].each do |release|
91
+ release.each_pair do |key, value|
92
+ print Paint["#{key.capitalize}", :bright]
93
+ print Paint[" #{value}\n", :bright]
94
+ end
95
+ end if app_releases['releaseInfo']
96
+ print_new_line
97
+ end
98
+
99
+ end
100
+ end
101
+ end