mozzn 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc728053eb2673b230d6cca92c0ef60015bfb8fd
4
- data.tar.gz: 8d4bdfff003e054e23d6a40bc4efcc36a3e50b0c
3
+ metadata.gz: b32a146475f55c41943db377119d126dbca9fd00
4
+ data.tar.gz: 55019427b77f4474b78092e62919a15ae23ebfce
5
5
  SHA512:
6
- metadata.gz: 0a063774968c2bc3aab49346a5bf572f8c0c2933b9d44f55e7a1b4664ad6fc85ffec6e6212c4addebdf731baf06c0ee7580808d4f40839045a4dbfa4719dea9b
7
- data.tar.gz: b2a614e7ba900028bffc37f35354e1002896153fa43d58020e86457d4a5c9a78a0a0aa017f080c7fb1f115b6fd73f7c499430949c47cee6be5d5ed11f23849d4
6
+ metadata.gz: 3b02f8b487ed7fc49018937888d143f384ac6b2bdc2d48de869c54d078681920784101989c8f6cffa20e9be194bd0566c0f2c6edc4bd76b16e10fd25a66c8d04
7
+ data.tar.gz: 928091596f1ae6d18583b198208b6801fa5517ecb37def5be973addd8b278723935e1375c46128ba24f02359a3b50dc2c4f19b1fa276d33fc5d55fb14386b324
data/bin/mozzn CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ trap(:INT) { puts 'Exiting...'; exit 1 }
3
+
2
4
  require 'mozzn'
3
5
 
4
6
  # Start our command line interface
data/lib/mozzn/api.rb CHANGED
@@ -8,7 +8,11 @@ module Mozzn
8
8
  attr_accessor :token
9
9
 
10
10
  def initialize token = nil
11
- @connection = Faraday.new('http://mozzn.com/api/v1/')
11
+ if ENV['GEM_ENV'] == 'test'
12
+ @connection = Faraday.new('http://localhost:3000/api/v1/')
13
+ else
14
+ @connection = Faraday.new('http://mozzn.com/api/v1/')
15
+ end
12
16
  @token = token
13
17
  end
14
18
 
data/lib/mozzn/cli.rb CHANGED
@@ -5,157 +5,17 @@ require 'rubygems'
5
5
  require 'git'
6
6
  require 'mozzn/version'
7
7
  require 'terminal-table'
8
+ require "mozzn/commands/key"
9
+ require "mozzn/commands/app"
10
+ require "mozzn/commands/auth"
8
11
 
9
12
  module Mozzn
10
13
  class Cli < Thor
11
14
  include Thor::Actions
12
15
 
13
- trap(:INT) { exit 1 }
14
-
15
16
  default_task :help
16
17
 
17
- desc 'login', 'Login with your mozzn credentials, an interactive shell will ask you about your email and password'
18
- # mozzn login
19
- method_option :email, :aliases => "-u", :desc => "Mozzn email"
20
- method_option :password, :aliases => "-p", :desc => "Mozzn password"
21
- def login
22
- mozzn = Mozzn::Api.new
23
- if options[:email].nil? && options[:password].nil?
24
- hl = HighLine.new
25
- email = hl.ask 'Mozzn email: '
26
- password = hl.ask('Mozzn password (we will not store this): ') { |q| q.echo = "*" }
27
- elsif options[:email].nil? || options[:password].nil?
28
- raise Thor::Error, "Email and password must be provided!"
29
- else
30
- email = options[:email]
31
- password = options[:password]
32
- end
33
- params = {
34
- user: {
35
- email: email,
36
- password: password
37
- }
38
- }
39
- response = mozzn.post(:sessions, params)
40
- auth_token = response['data']['auth_token']
41
- if auth_token == nil
42
- raise Thor::Error, response['info']
43
- else
44
- Mozzn::Config.new.add('token', auth_token)
45
- say response['info'], :green
46
- git_check
47
- ssh_key_check
48
- end
49
- rescue Mozzn::Disconnected
50
- say 'Unable to connect to Mozzn. Please check your internet connection.', :red
51
- rescue Mozzn::UnexpectedOutput
52
- say 'UnexpectedOutput', :red
53
- end
54
-
55
- desc 'add_key', 'Add your SSH Public Key directily or add its path'
56
- method_option :public_key, :aliases => "-k", :desc => "RSA/DSA public key"
57
- method_option :key_path, :aliases => "-p", :desc => "Path to RSA/DSA public key"
58
- # mozzn add_key
59
- def add_key
60
- mozzn = Mozzn::Api.new(Mozzn::Config.new.read['token'])
61
- if options[:key_path].present?
62
- key_path = File.expand_path(options[:key_path])
63
- elsif options[:public_key].present?
64
- public_key = options[:public_key]
65
- else
66
- raise Thor::Error, "Neither a key path or an SSH key were provided. You must use -p or -k options."
67
- end
68
-
69
- if public_key.nil?
70
- if File.exist?(key_path)
71
- File.open(key_path, "rb") do |f|
72
- public_key = f.read
73
- end
74
- else
75
- raise Thor::Error, "Unable to read #{key_path}. File does not exist or not accessible."
76
- end
77
- end
78
-
79
- path = 'keys'
80
- params = {
81
- key: {
82
- public: public_key
83
- }
84
- }
85
- response = mozzn.post(path, params)
86
- say response['info'], :green
87
- rescue Mozzn::Disconnected
88
- say 'Unable to connect to Mozzn check the internet connection!', :red
89
- rescue Mozzn::UnexpectedOutput
90
- say 'UnexpectedOutput', :red
91
- end
92
-
93
- desc 'create_app APPNAME', 'create a new application'
94
- # mozzn create_app
95
- def create_app name = nil
96
- mozzn = Mozzn::Api.new(Mozzn::Config.new.read['token'])
97
- if name == nil
98
- raise Thor::Error, "You must enter application name."
99
- end
100
- path = 'applications'
101
- params = {
102
- application: {
103
- name: name
104
- }
105
- }
106
- response = mozzn.post(path, params)
107
- say response['info'], :green
108
- git = Git.init
109
- unless File.exist?('.git')
110
- git.add(all: true)
111
- begin
112
- git.commit('First commit')
113
- rescue Git::GitExecuteError => e
114
- # Do nothing
115
- end
116
- end
117
- begin
118
- git.add_remote("mozzn", "git@git.mozzn.com:#{name}.git")
119
- rescue Git::GitExecuteError => e
120
- say 'You already have this remote.', :red
121
- end
122
- rescue Mozzn::Disconnected
123
- say 'Unable to connect to Mozzn check the internet connection!', :red
124
- rescue Mozzn::UnexpectedOutput
125
- say 'UnexpectedOutput'
126
- end
127
-
128
- desc 'remove_app APPNAME', 'Remove spcicfic Application.'
129
-
130
- def remove_app name = nil
131
- mozzn = Mozzn::Api.new(Mozzn::Config.new.read['token'])
132
- if !name.present?
133
- raise Thor::Error, "You must enter Application Name!"
134
- end
135
- params = {
136
- name: name
137
- }
138
- search_path = "applications/search"
139
- begin
140
- response = mozzn.get(search_path, params)
141
- if response.has_key?('info')
142
- raise Thor::Error, "#{response['info']}"
143
- else
144
- id = response['app_id']
145
- resources_path = "applications/#{id}/remove"
146
- response = mozzn.get(resources_path,nil)
147
- say response['info'], :green
148
- end
149
- rescue JSON::ParserError => e
150
- raise Thor::Error,"You do not have an application with the name #{params[:name]}. Please check the application name."
151
- end
152
- rescue Mozzn::Disconnected
153
- say 'Unable to connect to Mozzn. Check your internet connection!', :red
154
- rescue Mozzn::UnexpectedOutput
155
- say 'UnexpectedOutput', :red
156
- end
157
-
158
- desc 'update', 'To show if there is an update for the CLI'
18
+ desc 'update', 'Check for updates'
159
19
  def update
160
20
  # TODO after gem release https://rubygems.org/api/v1/versions/mozzn.json
161
21
  @connection = Faraday.new('https://rubygems.org/api/v1/versions/coulda.json')
@@ -169,164 +29,6 @@ module Mozzn
169
29
  end
170
30
  end
171
31
 
172
- desc 'registration', 'Create an account on mozzn.'
173
- method_option :name, :aliases => "-n", :desc => "Username."
174
- method_option :email, :aliases => "-u", :desc => "User email used to login."
175
- method_option :password, :aliases => "-p", :desc => "User password."
176
- method_option :password_confirmation, :aliases => "-c", :desc => "Password_confirmation."
177
-
178
- def registration
179
- mozzn = Mozzn::Api.new
180
- if options.present?
181
- name = options[:name]
182
- email = options[:email]
183
- password = options[:password]
184
- password_confirmation = options[:password_confirmation]
185
- else
186
- h = HighLine.new
187
- name = h.ask("Username: ")
188
- email = h.ask("Email: ")
189
- password = h.ask("Password: ")
190
- password_confirmation = h.ask("password_confirmation: ")
191
- end
192
- params = {
193
- user: {
194
- name: name,
195
- email: email,
196
- password: password,
197
- password_confirmation: password_confirmation
198
- }
199
- }
200
- response = mozzn.post(:registrations, params)
201
- errors = response['data']['errors']
202
- if errors.present?
203
- errors = JSON.parse(errors)
204
- say "#{response['info']}, the following errors were found:\n * #{errors.map {|e| e.join(' ')}.join("\n * ")}\nPlease try again.", :red
205
- # TODO: re-run registration
206
- else
207
- say response['info'], :green
208
- end
209
- end
210
-
211
- desc 'resources appname', 'To list all instances which a specific application use.'
212
-
213
- def resources appname = nil
214
- mozzn = Mozzn::Api.new(Mozzn::Config.new.read['token'])
215
- if !appname.present?
216
- raise Thor::Error, "You must enter Application Name!"
217
- end
218
- params = {
219
- name: appname
220
- }
221
- search_path = "applications/search"
222
- begin
223
- response = mozzn.get(search_path, params)
224
- if response.has_key?('info')
225
- raise Thor::Error, "#{response['info']}"
226
- else
227
- id = response['app_id']
228
- resources_path = "applications/#{id}/resources"
229
- response = mozzn.get(resources_path,nil)
230
- if response.has_key?('info')
231
- raise Thor::Error, "#{response['info']}"
232
- else
233
- table1 = Terminal::Table.new(headings: ['Process', 'Command']) do |t|
234
- response['resources'].each do |resource|
235
- key = (resource.has_key?('command') ? resource['name'] : nil)
236
- value = (resource.has_key?('command') ? resource['command'] : nil)
237
- if key.present?
238
- t.add_row [key, value]
239
- end
240
- end
241
- end
242
- table2 = Terminal::Table.new(headings: ['Database', 'Role']) do |t|
243
- response['resources'].each do |resource|
244
- key = (resource.has_key?('role') ? resource['name'] : nil)
245
- value = (resource.has_key?('role') ? resource['role'] : nil)
246
- if key.present?
247
- t.add_row [key, value]
248
- end
249
- end
250
- end
251
- end
252
- instances_path = "applications/#{id}/instances"
253
- response = mozzn.get(instances_path,nil)
254
- # say response['instances'].first['data']['ip_address'].inspect, :green
255
- if response.has_key?('info')
256
- raise Thor::Error, "#{response['info']}"
257
- else
258
- table3 = Terminal::Table.new(headings: ['Name', 'IP']) do |t|
259
- response['instances'].each do |instant|
260
- key = instant['data']['name']
261
- value = instant['data']['ip_address']
262
- if key.present?
263
- t.add_row [key, value]
264
- end
265
- end
266
- end
267
- end
268
- say "Processes:"
269
- say "#{table1}"
270
- say "Databases:"
271
- say "#{table2}"
272
- say "Instances:"
273
- say "#{table3}"
274
- end
275
-
276
- rescue JSON::ParserError => e
277
- raise Thor::Error,"You do not have an application with the name #{params[:appname]}. Please check the application name."
278
- end
279
- rescue Mozzn::Disconnected
280
- say 'Unable to connect to Mozzn. Check your internet connection!', :red
281
- rescue Mozzn::UnexpectedOutput
282
- say 'UnexpectedOutput', :red
283
- end
284
-
285
- desc 'console', 'To start a console on your first web server'
286
- method_option :name, :aliases => "-c", :desc => "Command to be run in console."
287
- def console
288
- config_file_path = ".git/config"
289
- if File.exists?(config_file_path)
290
- File.open(config_file_path, "r") do |f|
291
- @data = f.read
292
- end
293
- else
294
- raise Thor::Error,"Unable to find a repository for this directory. You probably not in the application directory or this application does not have git repository yet."
295
- end
296
-
297
- url = @data.scan /url =.*/
298
- app = url.first.split(":")[1]
299
- appname = app.split('.').first
300
- mozzn = Mozzn::Api.new(Mozzn::Config.new.read['token'])
301
- params = {
302
- name: appname
303
- }
304
- search_path = "applications/search"
305
- begin
306
- response = mozzn.get(search_path, params)
307
- if response.has_key?('info')
308
- raise Thor::Error, "#{response['info']}"
309
- else
310
- id = response['app_id']
311
- instances_path = "applications/#{id}/instances"
312
- response = mozzn.get(instances_path,nil)
313
- ip_address = response['instances'].first['data']['ip_address']
314
- if options[:command].present?
315
- system( "ssh app@#{ip_address} #{options[:command]}" )
316
- else
317
- system( "ssh app@#{ip_address}")
318
- end
319
- status = $?.exitstatus
320
- end
321
- rescue JSON::ParserError => e
322
- raise Thor::Error,"You do not have an application with the name #{params[:name]}. Please check the application name."
323
- end
324
- rescue Mozzn::Disconnected
325
- say 'Unable to connect to Mozzn. Check your internet connection!', :red
326
- rescue Mozzn::UnexpectedOutput
327
- say 'UnexpectedOutput', :red
328
- end
329
-
330
32
  desc 'help COMMAND', 'For more infromation about spicific COMMAND'
331
33
  def help command = nil
332
34
  puts 'Primary help topics, type "mozzn help COMMAND" for more details.'
@@ -334,24 +36,13 @@ module Mozzn
334
36
  super command
335
37
  end
336
38
 
337
- no_commands do
338
- desc 'git_check', 'checks if user has git installed in $PATH or not'
339
- def git_check
340
- line = Cocaine::CommandLine.new("which git")
341
- begin
342
- output = line.run
343
- rescue Cocaine::ExitStatusError => e
344
- raise Thor::Error, 'Unable to find git it is either not installed or not in your $PATH. You may need to install it or add it to $PATH.'
345
- end
346
- end
39
+ desc "key SUBCOMMAND", "Manage SSH keys"
40
+ subcommand "key", Mozzn::Commands::Key
347
41
 
348
- desc 'ssh_key_check', 'checks if user has ssh key in ~/.ssh path'
349
- def ssh_key_check
350
- ssh = ['~/.ssh/id_rsa.pub','~/.ssh/id_dsa.pub']
351
- unless ssh.map { |ssh| File.exist?(File.expand_path(ssh))}
352
- raise Thor::Error, "Unable to find an SSH key in #{File.expand_path('~/.ssh/')}. "
353
- end
354
- end
355
- end
42
+ desc "app SUBCOMMAND", "Manage applications"
43
+ subcommand "app", Mozzn::Commands::App
44
+
45
+ desc "auth SUBCOMMAND", "Manage your accountUser registration and login"
46
+ subcommand "auth", Mozzn::Commands::Auth
356
47
  end
357
48
  end
@@ -0,0 +1,222 @@
1
+ module Mozzn
2
+ module Commands
3
+ class App < Thor
4
+
5
+ desc 'create APPNAME', 'create a new application'
6
+
7
+ def create name = nil
8
+ token = Mozzn::Config.new.read['token']
9
+ if token.nil?
10
+ raise Thor::Error,"You need to login in order to continue."
11
+ end
12
+ mozzn = Mozzn::Api.new(Mozzn::Config.new.read['token'])
13
+ if name == nil
14
+ raise Thor::Error, "You must enter application name."
15
+ end
16
+ path = 'applications'
17
+ params = {
18
+ application: {
19
+ name: name
20
+ }
21
+ }
22
+ response = mozzn.post(path, params)
23
+ if response.has_key?('message')
24
+ raise Thor::Error, "#{response['message']}"
25
+ else
26
+ say response['info'], :green
27
+ git = Git.init
28
+ unless File.exist?('.git')
29
+ git.add(all: true)
30
+ begin
31
+ git.commit('First commit')
32
+ rescue Git::GitExecuteError => e
33
+ # Do nothing
34
+ end
35
+ end
36
+ begin
37
+ git.add_remote("mozzn", "git@git.mozzn.com:#{name}.git")
38
+ rescue Git::GitExecuteError => e
39
+ say 'Git remote already configured, skipping...'
40
+ end
41
+ end
42
+ rescue Mozzn::Disconnected
43
+ say 'Unable to connect to Mozzn check the internet connection!', :red
44
+ rescue Mozzn::UnexpectedOutput
45
+ say 'UnexpectedOutput', :red
46
+ end
47
+
48
+ method_option :appname, :aliases => "-n", :desc => "Application name"
49
+ desc 'destroy ', 'Remove spcicfic Application'
50
+ def destroy
51
+ token = Mozzn::Config.new.read['token']
52
+ if token.nil?
53
+ raise Thor::Error,"You need to login in order to continue."
54
+ end
55
+ mozzn = Mozzn::Api.new(Mozzn::Config.new.read['token'])
56
+ if options['appname'].present?
57
+ name = options['appname']
58
+ else
59
+ name = appname
60
+ end
61
+ params = {
62
+ name: name
63
+ }
64
+ search_path = "applications/search"
65
+ begin
66
+ response = mozzn.get(search_path, params)
67
+ if response.has_key?('info')
68
+ raise Thor::Error, "#{response['info']}"
69
+ else
70
+ id = response['app_id']
71
+ resources_path = "applications/#{id}/remove"
72
+ response = mozzn.get(resources_path,nil)
73
+ say response['info'], :green
74
+ end
75
+ rescue JSON::ParserError => e
76
+ raise Thor::Error,"You do not have an application with the name #{params[:name]}. Please check the application name."
77
+ end
78
+ rescue Mozzn::Disconnected
79
+ say 'Unable to connect to Mozzn. Check your internet connection!', :red
80
+ rescue Mozzn::UnexpectedOutput
81
+ say 'UnexpectedOutput', :red
82
+ end
83
+ desc "list", "List all your Applications."
84
+
85
+ def list
86
+ #TODO
87
+ end
88
+
89
+ method_option :appname, :aliases => "-n", :desc => "Application name"
90
+ desc 'resources', 'To list all instances which application use'
91
+ def resources
92
+ token = Mozzn::Config.new.read['token']
93
+ if token.nil?
94
+ raise Thor::Error,"You need to login in order to continue."
95
+ end
96
+ mozzn = Mozzn::Api.new(Mozzn::Config.new.read['token'])
97
+ if options['appname'].present?
98
+ name = options['appname']
99
+ else
100
+ name = appname
101
+ end
102
+ params = {
103
+ name: name
104
+ }
105
+ search_path = "applications/search"
106
+ begin
107
+ response = mozzn.get(search_path, params)
108
+ if response.has_key?('info')
109
+ raise Thor::Error, "#{response['info']}"
110
+ else
111
+ id = response['app_id']
112
+ resources_path = "applications/#{id}/resources"
113
+ response = mozzn.get(resources_path,nil)
114
+ if response.has_key?('info')
115
+ raise Thor::Error, "#{response['info']}"
116
+ else
117
+ table1 = Terminal::Table.new(headings: ['Process', 'Command']) do |t|
118
+ response['resources'].each do |resource|
119
+ key = (resource.has_key?('command') ? resource['name'] : nil)
120
+ value = (resource.has_key?('command') ? resource['command'] : nil)
121
+ if key.present?
122
+ t.add_row [key, value]
123
+ end
124
+ end
125
+ end
126
+ table2 = Terminal::Table.new(headings: ['Database', 'Role']) do |t|
127
+ response['resources'].each do |resource|
128
+ key = (resource.has_key?('role') ? resource['name'] : nil)
129
+ value = (resource.has_key?('role') ? resource['role'] : nil)
130
+ if key.present?
131
+ t.add_row [key, value]
132
+ end
133
+ end
134
+ end
135
+ end
136
+ instances_path = "applications/#{id}/instances"
137
+ response = mozzn.get(instances_path,nil)
138
+ if response.has_key?('info')
139
+ raise Thor::Error, "#{response['info']}"
140
+ else
141
+ table3 = Terminal::Table.new(headings: ['Name', 'IP']) do |t|
142
+ response['instances'].each do |instant|
143
+ key = instant['data']['name']
144
+ value = instant['data']['ip_address']
145
+ if key.present?
146
+ t.add_row [key, value]
147
+ end
148
+ end
149
+ end
150
+ end
151
+ say "Processes:"
152
+ say "#{table1}"
153
+ say "Databases:"
154
+ say "#{table2}"
155
+ say "Instances:"
156
+ say "#{table3}"
157
+ end
158
+
159
+ rescue JSON::ParserError => e
160
+ raise Thor::Error,"You do not have an application with the name #{params[:appname]}. Please check the application name."
161
+ end
162
+ rescue Mozzn::Disconnected
163
+ say 'Unable to connect to Mozzn. Check your internet connection!', :red
164
+ rescue Mozzn::UnexpectedOutput
165
+ say 'UnexpectedOutput', :red
166
+ end
167
+
168
+ desc 'console', 'To start a console on your first web server'
169
+ method_option :name, :aliases => "-c", :desc => "Command to be run in console."
170
+ def console
171
+ token = Mozzn::Config.new.read['token']
172
+ if token.nil?
173
+ raise Thor::Error,"You need to login in order to continue."
174
+ end
175
+ mozzn = Mozzn::Api.new(Mozzn::Config.new.read['token'])
176
+ params = {
177
+ name: appname
178
+ }
179
+ search_path = "applications/search"
180
+ begin
181
+ response = mozzn.get(search_path, params)
182
+ if response.has_key?('info')
183
+ raise Thor::Error, "#{response['info']}"
184
+ else
185
+ id = response['app_id']
186
+ instances_path = "applications/#{id}/instances"
187
+ response = mozzn.get(instances_path,nil)
188
+ ip_address = response['instances'].first['data']['ip_address']
189
+ if options[:command].present?
190
+ system( "ssh app@#{ip_address} #{options[:command]}" )
191
+ else
192
+ system( "ssh app@#{ip_address}")
193
+ end
194
+ status = $?.exitstatus
195
+ end
196
+ rescue JSON::ParserError => e
197
+ raise Thor::Error,"You do not have an application with the name #{params[:name]}. Please check the application name."
198
+ end
199
+ rescue Mozzn::Disconnected
200
+ say 'Unable to connect to Mozzn. Check your internet connection!', :red
201
+ rescue Mozzn::UnexpectedOutput
202
+ say 'UnexpectedOutput', :red
203
+ end
204
+ no_commands do
205
+ desc 'appname', 'Get application name'
206
+ def appname
207
+ config_file_path = ".git/config"
208
+ if File.exists?(config_file_path)
209
+ File.open(config_file_path, "r") do |f|
210
+ @data = f.read
211
+ end
212
+ else
213
+ raise Thor::Error,"Unable to find a repository for this directory. You probably not in the application directory or this application does not have git repository yet."
214
+ end
215
+ url = @data.scan /url =.*/
216
+ app = url.first.split(":")[1]
217
+ appname = app.split('.').first
218
+ end
219
+ end
220
+ end
221
+ end
222
+ end