bushido 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in bushido.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/bin/bushido ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+ require './lib/bushido'
3
+
4
+ options = {}
5
+
6
+ commands = [:create, :destroy, :reload]
7
+ help_docs = {:create => "bushido create [NAME] - creates a new app"}
8
+
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: bushido <command>"
11
+
12
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
13
+ options[:verbose] = v
14
+ end
15
+
16
+ opts.on("-h", "--help [command]", commands, "Help (this screen)") do |h|
17
+ if h.nil?
18
+ puts opts
19
+ puts "Supported commands: create, names"
20
+ exit
21
+ end
22
+
23
+ puts help_docs[h]
24
+ exit
25
+ end
26
+
27
+ opts.on_tail("-V", "--version", "Show version") do
28
+ puts Bushido::VERSION.join('.')
29
+ exit
30
+ end
31
+ end.parse!
32
+
33
+ command = ARGV.first
34
+
35
+ if command
36
+ case command.downcase.to_sym
37
+ when :reauth then Bushido::User.reauth
38
+ when :claim then Bushido::App.claim(ARGV[1])
39
+ when :list then Bushido::App.list()
40
+ when :create then Bushido::App.create(ARGV[1])
41
+ when :show then Bushido::App.show(ARGV[1])
42
+ when :start then Bushido::App.start(ARGV[1])
43
+ when :stop then Bushido::App.stop(ARGV[1])
44
+ when :restart then Bushido::App.restart(ARGV[1])
45
+ when :update then Bushido::App.update(ARGV[1])
46
+ end
47
+ else
48
+ puts "usage: bushido <command>\n\nSee bushido -h for more detailed instructions"
49
+ puts "Supported commands: create, names"
50
+ end
data/bushido.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "bushido/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "bushido"
7
+ s.version = Bushido::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Sean Grove"]
10
+ s.email = ["s@bushi.do"]
11
+ s.homepage = ""
12
+ s.summary = %q{Command-lin interface for bushi.do}
13
+ s.description = %q{A command line tool to do everything with bushido, from signing up and deploying apps, to claiming, restarting, and updating existing apps}
14
+
15
+ s.rubyforge_project = "bushido"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,84 @@
1
+ module Bushido
2
+ class App
3
+ class << self
4
+ def create(url)
5
+ puts "Creating account for #{Bushido::User.email}..."
6
+
7
+ post({:app => {:url => url}})
8
+ end
9
+
10
+ def list
11
+ response = Bushido::Command.get_command("#{Temple}/apps")
12
+
13
+ response.each do |app|
14
+ puts app["app"]['subdomain']
15
+ end
16
+ end
17
+
18
+ def get(name, params={})
19
+ url = "#{Temple}/apps/#{name}"
20
+ Bushido::Command.get_command(url, params)
21
+ end
22
+
23
+ def put(app, command)
24
+ url = "#{Temple}/apps/#{app}.json"
25
+ params = {:command => command}
26
+
27
+ show_response Bushido::Command.put_command(url, params)
28
+ end
29
+
30
+ def post(params)
31
+ url = "#{Temple}/apps"
32
+ show_response Bushido::Command.post_command(url, params)
33
+ end
34
+
35
+ def show_response(response)
36
+ show_messages response
37
+ show_errors response
38
+ end
39
+
40
+ def show_messages(response)
41
+ if response["messages"]
42
+ puts "Messages:"
43
+ response["messages"].each_with_index do |error, counter|
44
+ puts "\t#{counter + 1}. #{error}"
45
+ end
46
+ end
47
+ end
48
+
49
+ def show_errors(response)
50
+ if response["errors"]
51
+ puts "Errors:"
52
+ response["errors"].each_with_index do |error, counter|
53
+ puts "\t#{counter + 1}. #{error}"
54
+ end
55
+ end
56
+ end
57
+
58
+ def show(name)
59
+ result = get name
60
+ puts result.inspect
61
+ end
62
+
63
+ def start(name)
64
+ put name, :start
65
+ end
66
+
67
+ def stop(name)
68
+ put name, :stop
69
+ end
70
+
71
+ def restart(name)
72
+ put name, :restart
73
+ end
74
+
75
+ def claim(name)
76
+ put name, :claim
77
+ end
78
+
79
+ def update(name)
80
+ put name, :update
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,28 @@
1
+ module Bushido
2
+ class Command
3
+ class << self
4
+ 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?
6
+
7
+ raw = RestClient.get(url, {:params => params, :accept => :json})
8
+ response = JSON.parse raw
9
+ end
10
+
11
+ 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?
13
+
14
+ raw = RestClient.post(url, params.to_json, :content_type => :json, :accept => :json)
15
+ response = JSON.parse raw
16
+ end
17
+
18
+ def put_command(url, params)
19
+ Bushido::Utils.while_authorized do
20
+ params.merge!({:auth_token => Bushido::User.authentication_token}) if params[:auth_token].nil? unless Bushido::User.authentication_token.nil?
21
+
22
+ raw = RestClient.put(url, params.to_json, :content_type => :json)
23
+ response = JSON.parse raw
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,120 @@
1
+ module Bushido
2
+ class User
3
+ class << self
4
+ def account
5
+ load_config || {}
6
+ end
7
+
8
+ def load_config
9
+ begin
10
+ return JSON.parse(File.open(File.expand_path("~/.bushido/config.json"), 'r') { |l| l.read })
11
+ rescue Errno::ENOENT
12
+ end
13
+ end
14
+
15
+ def load_account
16
+ begin
17
+ @account = JSON.parse(File.open(File.expand_path("~/.bushido/config.json"), 'r') { |l| l.read })
18
+ rescue Errno::ENOENT
19
+ @account = retrieve_account
20
+ end
21
+ end
22
+
23
+ def reauth
24
+ retrieve_account
25
+ end
26
+
27
+ def update_account(account)
28
+ print "Storing account information..."
29
+ File.open(File.expand_path("~/.bushido/config.json"), 'w') { |f| f.write(account.to_json) }
30
+ puts " Done!"
31
+ end
32
+
33
+ def retrieve_account
34
+ credentials = self.prompt_for_credentials
35
+
36
+ raw = RestClient.get "#{Bushido::Temple}/users/verify.json", {:params => {:email => credentials[:email], :password => credentials[:password]}, :accept => :json}
37
+
38
+ begin
39
+ result = JSON.parse(raw)
40
+ rescue
41
+ puts "Our servers didn't respond properly while trying to retrieve the account, this seems to be an issue on our end. Please email us at support@bushi.do to clear this up."
42
+ exit 1
43
+ end
44
+
45
+ if result["authentication_token"] and result["error"].nil?
46
+ update_account({:email => credentials[:email], :authentication_token => result["authentication_token"]})
47
+ return {:email => credentials[:email], :authentication_token => result["authentication_token"]}
48
+ else
49
+ puts result["error"]
50
+ if result["error_type"] == "verification_failure"
51
+ puts ""
52
+ 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]"
53
+ if $stdin.gets.chomp == "y"
54
+ self.create_account(credentials)
55
+ else
56
+ exit 1
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ def create_account(credentials)
63
+ # GET instead of POST because of AuthenticityToken issues. Deal with it later.
64
+ begin
65
+ raw = RestClient.get "#{Bushido::Temple}/users/create.json", {:params => {:email => credentials[:email], :password => credentials[:password], :accept => :json}}
66
+ rescue RestClient::UnprocessableEntity
67
+ puts "We ran into an error registering, either our site is down or that name is registered."
68
+ exit 1
69
+ end
70
+
71
+ begin
72
+ result = JSON.parse(raw)
73
+ #puts result.inspect
74
+ #puts "----------------------------------------------------------------------"
75
+ rescue JSON::ParserError
76
+ puts "Our servers didn't respond properly while trying to create an account, this seems to be an issue on our end. Please email us at support@bushi.do to clear this up."
77
+ exit 1
78
+ end
79
+
80
+ if result["errors"]
81
+ puts "There were some errors registering: "
82
+
83
+ result["errors"].each_pair { |field, error| puts " #{field.capitalize} #{error}" }
84
+ exit 1
85
+ end
86
+
87
+ update_account result
88
+ return credentials
89
+ end
90
+
91
+ def authentication_token
92
+ account["authentication_token"]
93
+ end
94
+
95
+ def email
96
+ account["email"]
97
+ end
98
+
99
+ def prompt_for_credentials(confirm=false)
100
+ result = {
101
+ :email => self.prompt_for_email,
102
+ :password => self.prompt_for_password
103
+ }
104
+
105
+ result.merge!({:password_confirmation => self.prompt_for_password(" Confirm: ")}) if confirm
106
+
107
+ return result
108
+ end
109
+
110
+ def prompt_for_email
111
+ print " Email: "
112
+ email = $stdin.gets.chomp
113
+ end
114
+
115
+ def prompt_for_password(prompt=" Password:")
116
+ ask(prompt) { |q| q.echo = false }
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,14 @@
1
+ module Bushido
2
+ class Utils
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
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Bushido
2
+ VERSION = "0.0.1"
3
+ end
data/lib/bushido.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Bushido
2
+ require 'rubygems'
3
+ require 'optparse'
4
+ require 'rest-client'
5
+ require 'json'
6
+ require 'highline/import'
7
+
8
+ require "./lib/bushido/user"
9
+ require "./lib/bushido/utils"
10
+ require "./lib/bushido/command"
11
+ require "./lib/bushido/app"
12
+
13
+
14
+ Temple = ENV["HOST"] || "http://nok.tea.sh/"
15
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bushido
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Sean Grove
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-26 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: A command line tool to do everything with bushido, from signing up and deploying apps, to claiming, restarting, and updating existing apps
22
+ email:
23
+ - s@bushi.do
24
+ executables:
25
+ - bushido
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - Gemfile
32
+ - Rakefile
33
+ - bin/bushido
34
+ - bushido.gemspec
35
+ - lib/bushido.rb
36
+ - lib/bushido/app.rb
37
+ - lib/bushido/command.rb
38
+ - lib/bushido/user.rb
39
+ - lib/bushido/utils.rb
40
+ - lib/bushido/version.rb
41
+ has_rdoc: true
42
+ homepage: ""
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project: bushido
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Command-lin interface for bushi.do
73
+ test_files: []
74
+