bushido 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
+ require 'rake/testtask'
1
2
  require 'bundler'
2
3
  Bundler::GemHelper.install_tasks
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs += ["lib", "test"]
7
+ t.test_files = FileList['test/test*.rb']
8
+ t.verbose = true
9
+ end
data/bin/bushido CHANGED
@@ -1,9 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
- require 'bushido'
2
+ begin
3
+ require 'bushido'
4
+ rescue LoadError
5
+ $: << File.expand_path("../../lib", __FILE__)
6
+ require 'bushido'
7
+ end
3
8
 
4
9
  options = {}
5
10
 
6
- commands = [:login, :remove_account, :claim, :list, :create, :show, :start, :stop, :restart, :update, :open, :logs, :add_var, :remove_var, :ssh_key, :api_key].sort
11
+ commands = [:login, :remove_account, :claim, :list, :create, :show, :start, :stop, :restart, :update, :open, :logs, :add_var, :remove_var, :ssh_key, :api_key].sort_by {|s| s.to_s}
7
12
 
8
13
  help_docs = {
9
14
  :login => "bushido login - Authorizes this machine to work under your Bushido account",
@@ -53,9 +58,6 @@ command = ARGV.first
53
58
 
54
59
  if command
55
60
  case command.downcase.to_sym
56
- when :login then Bushido::User.reauth
57
- when :api_key then Bushido::User.show_api_key
58
- when :remove_account then Bushido::User.clear_config
59
61
 
60
62
  when :claim then Bushido::App.claim(ARGV[1])
61
63
  when :list then Bushido::App.list()
data/lib/bushido/app.rb CHANGED
@@ -1,97 +1,102 @@
1
1
  module Bushido
2
2
  class App
3
3
  class << self
4
- def create(url)
5
- Bushido::User.load_account
4
+ def app_url
5
+ "#{Bushido::Platform.host}/apps/#{Bushido::Platform.app}.json"
6
+ end
6
7
 
7
- puts "Creating from #{url} for #{Bushido::User.email}..."
8
8
 
9
- post({:app => {:url => url}})
9
+ def get(params={})
10
+ Bushido::Command.get_command(app_url, params)
10
11
  end
11
12
 
12
- def list
13
- response = Bushido::Command.get_command("#{Temple}/apps")
14
13
 
15
- response.each do |app|
16
- puts app["app"]['subdomain']
17
- end
18
- end
14
+ def put(command, params={})
15
+ params[:command] = command
19
16
 
20
- def open(name)
21
- location = "http://#{name}.#{Bushido::Temple.gsub('http://','')}"
22
- puts "Opening \"#{location}\" ..."
23
- exec "open #{location}"
17
+ Bushido::Command.put_command(app_url, params)
24
18
  end
25
19
 
26
- def get(name, params={})
27
- url = "#{Temple}/apps/#{name}"
28
- Bushido::Command.get_command(url, params)
20
+
21
+ def show
22
+ result = get
29
23
  end
30
24
 
31
- def put(app, command, params={})
32
- url = "#{Temple}/apps/#{app}.json"
33
- params[:command] = command
34
25
 
35
- Bushido::Command.show_response Bushido::Command.put_command(url, params)
26
+ def start
27
+ put :start
36
28
  end
37
29
 
38
- def post(params)
39
- url = "#{Temple}/apps"
40
- Bushido::Command.show_response Bushido::Command.post_command(url, params)
30
+
31
+ def stop
32
+ put :stop
41
33
  end
42
34
 
43
- def show(name)
44
- result = get name
45
- puts result.inspect
35
+
36
+ def restart
37
+ put :restart
46
38
  end
47
39
 
48
- def start(name)
49
- put name, :start
40
+
41
+ def claim
42
+ put :claim
50
43
  end
51
44
 
52
- def stop(name)
53
- put name, :stop
45
+
46
+ def update
47
+ put :update
54
48
  end
55
49
 
56
- def restart(name)
57
- put name, :restart
50
+
51
+ def add_var(key, value)
52
+ put :add_var, {:key => key, :value => value}
58
53
  end
59
54
 
60
- def claim(name)
61
- put name, :claim
55
+
56
+ def remove_var(key)
57
+ put :remove_var, {:key => key}
62
58
  end
63
59
 
64
- def update(name)
65
- put name, :update
60
+
61
+ def domains
62
+ get()["app"]["domains"]
66
63
  end
67
64
 
68
- def add_var(name, key, value)
69
- puts put(name, :add_var, {:key => key, :value => value})
65
+
66
+ def subdomain
67
+ get()["app"]["subdomain"]
70
68
  end
71
69
 
72
- def remove_var(name, key)
73
- puts put(name, :remove_var, {:key => key})
70
+
71
+ def set_subdomain(subdomain)
72
+ put :set_subdomain, {:subdomain => subdomain}
74
73
  end
75
74
 
76
- def add_domain(name, domain)
77
- puts put(name, :add_domain, {:domain => domain})
75
+
76
+ def add_domain(domain)
77
+ put :add_domain, {:domain => domain}
78
78
  end
79
79
 
80
- def remove_domain(name)
81
- puts put(name, :remove_domain)
80
+
81
+ def remove_domain(domain)
82
+ put :remove_domain, {:domain => domain}
82
83
  end
83
84
 
84
- def clear_logs(name)
85
- puts put(name, :clear_logs)
85
+
86
+ def clear_logs
87
+ put :clear_logs
86
88
  end
87
89
 
88
- def logs(name)
89
- puts get(name, {:gift => "logs"})
90
+
91
+ def logs
92
+ get({:gift => "logs"})
90
93
  end
91
94
 
92
- def ssh_key(name)
93
- puts get(name, {:gift => "ssh_key"})["ssh_key"]
95
+
96
+ def ssh_key
97
+ get({:gift => "ssh_key"})["ssh_key"]
94
98
  end
95
99
  end
96
100
  end
97
101
  end
102
+
@@ -1,34 +1,42 @@
1
1
  module Bushido
2
2
  class Command
3
+ @@last_request = nil
4
+ @@request_count = 0
5
+
3
6
  class << self
7
+ def request_count
8
+ @@request_count
9
+ end
10
+
4
11
  def get_command(url, params={})
5
- params.merge!({:auth_token => Bushido::User.authentication_token}) if params[:auth_token].nil? unless Bushido::User.authentication_token.nil?
12
+ @@request_count += 1
13
+ params.merge!({:auth_token => Bushido::Platform.key}) if params[:auth_token].nil? unless Bushido::Platform.key.nil?
6
14
 
7
15
  raw = RestClient.get(url, {:params => params, :accept => :json})
8
- response = JSON.parse raw
16
+ @@last_request = JSON.parse raw
9
17
  end
10
18
 
11
19
  def post_command(url, params)
12
- params.merge!({:auth_token => Bushido::User.authentication_token}) if params[:auth_token].nil? unless Bushido::User.authentication_token.nil?
20
+ @@request_count += 1
21
+ params.merge!({:auth_token => Bushido::Platform.key}) if params[:auth_token].nil? unless Bushido::Platform.key.nil?
13
22
 
14
23
  raw = RestClient.post(url, params.to_json, :content_type => :json, :accept => :json)
15
- response = JSON.parse raw
24
+ @@last_request = JSON.parse raw
16
25
  end
17
26
 
18
27
  def put_command(url, params, meta={})
28
+ @@request_count += 1
19
29
  if meta[:force]
20
- params.merge!({:auth_token => Bushido::User.authentication_token}) if params[:auth_token].nil? unless Bushido::User.authentication_token.nil?
30
+ params.merge!({:auth_token => Bushido::Platform.key}) if params[:auth_token].nil? unless Bushido::Platform.key.nil?
21
31
 
22
32
  raw = RestClient.put(url, params.to_json, :content_type => :json)
23
- response = JSON.parse raw
33
+ @@last_request = JSON.parse raw
24
34
 
25
35
  else
26
- Bushido::Utils.while_authorized do
27
- params.merge!({:auth_token => Bushido::User.authentication_token}) if params[:auth_token].nil? unless Bushido::User.authentication_token.nil?
36
+ params.merge!({:auth_token => Bushido::Platform.key}) if params[:auth_token].nil? unless Bushido::Platform.key.nil?
28
37
 
29
- raw = RestClient.put(url, params.to_json, :content_type => :json)
30
- response = JSON.parse raw
31
- end
38
+ raw = RestClient.put(url, params.to_json, :content_type => :json)
39
+ @@last_request = JSON.parse raw
32
40
  end
33
41
  end
34
42
 
@@ -54,6 +62,14 @@ module Bushido
54
62
  end
55
63
  end
56
64
  end
65
+
66
+ def last_command_successful?
67
+ @@last_request.nil? or @@last_request["errors"].nil?
68
+ end
69
+
70
+ def last_command_errored?
71
+ not last_command_successful?
72
+ end
57
73
  end
58
74
  end
59
75
  end
@@ -0,0 +1,19 @@
1
+ module Bushido
2
+ class Platform
3
+ class << self
4
+ def app
5
+ ENV['BUSHIDO_APP']
6
+ end
7
+
8
+
9
+ def key
10
+ ENV['BUSHIDO_APP_KEY']
11
+ end
12
+
13
+
14
+ def host
15
+ "http://#{ENV['BUSHIDO_HOST'] || 'bushi.do'}"
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/bushido/utils.rb CHANGED
@@ -1,18 +1,6 @@
1
1
  module Bushido
2
2
  class Utils
3
3
  class << self
4
- def while_authorized(&block)
5
- if Bushido::User.authentication_token.nil? or Bushido::User.email.nil?
6
- puts "Please authorized before attempting that command. You can run `bushido reauth` to update your credentials."
7
- exit 1
8
- else
9
- yield
10
- end
11
- end
12
-
13
- def home_directory
14
- ENV['HOME']
15
- end
16
4
  end
17
5
  end
18
6
  end
@@ -1,3 +1,3 @@
1
1
  module Bushido
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
data/lib/bushido.rb CHANGED
@@ -5,11 +5,8 @@ module Bushido
5
5
  require 'json'
6
6
  require 'highline/import'
7
7
 
8
- require "bushido/user"
8
+ require "bushido/platform"
9
9
  require "bushido/utils"
10
10
  require "bushido/command"
11
11
  require "bushido/app"
12
-
13
- host = ENV["HOST"] || "bushi.do"
14
- Temple = "http://#{host}/"
15
12
  end
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+
3
+ class TestExecutable < Test::Unit::TestCase
4
+ EXECUTABLE =
5
+ def test_complains_with_no_args
6
+ output = %x{#{File.expand_path('../../bin/bushido', __FILE__)}}
7
+ assert output =~ /See bushido -h for more detailed instructions/
8
+ end
9
+ end
10
+
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: bushido
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.9
5
+ version: 0.0.10
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sean Grove
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-21 00:00:00 -08:00
13
+ date: 2011-03-23 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -56,6 +56,7 @@ extensions: []
56
56
  extra_rdoc_files: []
57
57
 
58
58
  files:
59
+ - .gitignore
59
60
  - Gemfile
60
61
  - Rakefile
61
62
  - bin/bushido
@@ -63,9 +64,10 @@ files:
63
64
  - lib/bushido.rb
64
65
  - lib/bushido/app.rb
65
66
  - lib/bushido/command.rb
66
- - lib/bushido/user.rb
67
+ - lib/bushido/platform.rb
67
68
  - lib/bushido/utils.rb
68
69
  - lib/bushido/version.rb
70
+ - test/test_executable.rb
69
71
  has_rdoc: true
70
72
  homepage: http://gem.bushi.do
71
73
  licenses: []
@@ -94,5 +96,5 @@ rubygems_version: 1.5.0
94
96
  signing_key:
95
97
  specification_version: 3
96
98
  summary: Command-lin interface for bushi.do
97
- test_files: []
98
-
99
+ test_files:
100
+ - test/test_executable.rb
data/lib/bushido/user.rb DELETED
@@ -1,111 +0,0 @@
1
- module Bushido
2
- class User
3
- class << self
4
- def account
5
- load_config || {}
6
- end
7
-
8
- def clear_config
9
- print "Clearing out all local bushido configuration..."
10
- FileUtils.rm_rf "#{Bushido::Utils.home_directory}/.bushido/"
11
- puts "done"
12
- end
13
-
14
- def load_config
15
- begin
16
- return JSON.parse(File.open(File.expand_path("#{Bushido::Utils.home_directory}/.bushido/config.json"), 'r') { |l| l.read })
17
- rescue Errno::ENOENT
18
- end
19
- end
20
-
21
- def load_account
22
- begin
23
- @account = JSON.parse(File.open(File.expand_path("#{Bushido::Utils.home_directory}/.bushido/config.json"), 'r') { |l| l.read })
24
- rescue Errno::ENOENT
25
- @account = retrieve_account("Couldn't find your Bushido config on this machine.")
26
- end
27
- end
28
-
29
- def reauth
30
- retrieve_account("Re-authenticating your account.")
31
- end
32
-
33
- def update_account(account)
34
- print "Storing account information..."
35
- Dir.mkdir "#{Bushido::Utils.home_directory}/.bushido" unless (File.exist?("#{Bushido::Utils.home_directory}/.bushido") and File.directory?("#{Bushido::Utils.home_directory}/.bushido"))
36
- File.open(File.expand_path("#{Bushido::Utils.home_directory}/.bushido/config.json"), 'w') { |f| f.write(account.to_json) }
37
- puts " Done!"
38
- end
39
-
40
- def retrieve_account(msg)
41
- puts "#{msg} Enter your username and password, and we'll either retrieve if from your existing account on our servers, or create a new account for you."
42
- credentials = self.prompt_for_credentials
43
- puts "Please wait while we look up the account information..."
44
-
45
- result = Bushido::Command.put_command "#{Bushido::Temple}/users/verify", {:email => credentials[:email], :password => credentials[:password]}, {:force => true}
46
-
47
- if result["authentication_token"] and result["error"].nil?
48
- update_account({:email => credentials[:email], :authentication_token => result["authentication_token"]})
49
- return {:email => credentials[:email], :authentication_token => result["authentication_token"]}
50
- else
51
- puts result["error"]
52
- if result["error_type"] == "verification_failure"
53
- puts ""
54
- print "If this is your first time using Bushido, would you like to create an account using the email and password you just entered?\n[y/n] "
55
- if $stdin.gets.chomp == "y"
56
- self.create_account(credentials)
57
- else
58
- exit 1
59
- end
60
- end
61
- end
62
- end
63
-
64
- def create_account(credentials)
65
- # GET instead of POST because of AuthenticityToken issues. Deal with it later.
66
- result = Bushido::Command.post_command "#{Bushido::Temple}/users/create.json", {:email => credentials[:email], :password => credentials[:password]}
67
-
68
- Bushido::Command.show_response result
69
-
70
- if result["errors"].nil?
71
- update_account result
72
- return credentials
73
- else
74
- exit 1
75
- end
76
- end
77
-
78
- def show_api_key
79
- puts "#{account['authentication_token']}"
80
- end
81
-
82
- def authentication_token
83
- account["authentication_token"]
84
- end
85
-
86
- def email
87
- account["email"]
88
- end
89
-
90
- def prompt_for_credentials(confirm=false)
91
- result = {
92
- :email => self.prompt_for_email,
93
- :password => self.prompt_for_password
94
- }
95
-
96
- result.merge!({:password_confirmation => self.prompt_for_password(" Confirm: ")}) if confirm
97
-
98
- return result
99
- end
100
-
101
- def prompt_for_email
102
- print " Email: "
103
- email = $stdin.gets.chomp
104
- end
105
-
106
- def prompt_for_password(prompt=" Password:")
107
- ask(prompt) { |q| q.echo = false }
108
- end
109
- end
110
- end
111
- end