appli 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/appli/command.rb CHANGED
@@ -5,6 +5,10 @@ module Appli
5
5
  (class << self;self end).send :define_method, :command, &block
6
6
  end
7
7
 
8
+ def debug?
9
+ false
10
+ end
11
+
8
12
  def call(options, *args)
9
13
  @options = options
10
14
  arity = method(:command).arity
@@ -14,14 +18,10 @@ module Appli
14
18
  puts "An error occured while excuting your command...\n#{e.message}"
15
19
  end
16
20
 
17
- def use_hirb
18
- begin
19
- require 'hirb'
20
- extend Hirb::Console
21
- rescue LoadError
22
- puts "Hirb is not installed. Install hirb using '[sudo] gem install hirb' to get cool ASCII tables"
23
- Process.exit(1)
24
- end
21
+ private
22
+
23
+ def host
24
+ application['host']
25
25
  end
26
26
 
27
27
  def application
@@ -36,28 +36,15 @@ module Appli
36
36
  @options || Hash.new
37
37
  end
38
38
 
39
- def execute_commands(array)
40
- for command in array
41
- puts "\e[44;33m" + command + "\e[0m"
42
- exit_code = 0
43
- IO.popen(command) do |f|
44
- output = f.read
45
- exit_code = Process.waitpid2(f.pid)[1]
46
- end
47
- if exit_code != 0
48
- $stderr.puts "An error occured running: #{command}"
49
- Process.exit(1)
50
- end
51
- end
52
- end
53
-
54
39
  def git_config_variable(name)
55
- if name.is_a?(Symbol)
56
- r = `git config appli.#{name.to_s}`.chomp
40
+ @config_variables = Hash.new if @config_variables.nil?
41
+ if @config_variables[name]
42
+ @config_variables[name]
57
43
  else
58
- r = `git config #{name.to_s}`.chomp
44
+ r = `git config appli.#{name.to_s}`.chomp
45
+ puts "Getting configuration variable: appli.#{name.to_s} (was: '#{r}')" if debug?
46
+ @config_variables[name] = (r.empty? ? nil : r)
59
47
  end
60
- r.empty? ? nil : r
61
48
  end
62
49
 
63
50
  def api_request(url, username, password, data = nil)
@@ -78,10 +65,14 @@ module Appli
78
65
  res.use_ssl = true
79
66
  res.verify_mode = OpenSSL::SSL::VERIFY_NONE
80
67
  end
68
+ puts "Requesting '#{url}' on as #{data ? 'POST' : 'GET'}" if debug?
81
69
  res = res.request(req, data)
82
70
  case res
83
71
  when Net::HTTPSuccess
84
72
  return res.body
73
+ when Net::HTTPNotFound
74
+ puts "Resource not found"
75
+ Process.exit(1)
85
76
  when Net::HTTPServiceUnavailable
86
77
  puts "The API is currently unavailable. Please check your codebase account has been enabled for API access."
87
78
  Process.exit(1)
@@ -113,14 +104,21 @@ module Appli
113
104
  def domain
114
105
  git_config_variable(:domain) || 'applihq.com'
115
106
  end
116
-
117
- def get_from_ssh(command)
118
- `ssh -p #{application['ssh_port']} app@mercury.uk.applihq.com \"#{command}\"`
119
- end
120
-
107
+
121
108
  def ssh(command, filter = //)
122
- puts get_from_ssh(command).gsub(filter, '')
109
+ puts remote_exec(command).gsub(filter, '')
123
110
  end
124
111
 
112
+ def remote_exec(command)
113
+ command = "ssh -p #{application['ssh_port']} app@#{host['name']} \"#{command}\""
114
+ puts "Executing: #{command}" if debug?
115
+ `#{command}`
116
+ end
117
+
118
+ def error(command, exit_code = 1)
119
+ $stderr.puts command
120
+ Process.exit(exit_code)
121
+ end
122
+
125
123
  end
126
124
  end
data/lib/appli.rb CHANGED
@@ -14,36 +14,36 @@ module Appli
14
14
  class NotConfiguredError < StandardError; end
15
15
  class MustBeInRepositoryError < StandardError; end
16
16
 
17
- VERSION = "4.0.0"
17
+ VERSION = "0.0.3"
18
18
 
19
19
  def run(command, args = [])
20
20
  load_commands
21
21
  command = 'help' if command.nil?
22
- application = nil
23
- commands_array = @global_commands
24
22
 
25
23
  if @global_commands[command]
26
- ## nothing to do...
24
+ run_command(command, :global, args)
27
25
  elsif @application_commands[args.first]
28
- application = command
29
- command = args.shift
30
- commands_array = @application_commands
31
- elsif !command.empty?
32
- puts "usage: appli #{command} {command}\n\n"
33
- command = 'help'
26
+ run_command(args.shift, :app, ([command] + args))
34
27
  else
35
- puts "Command not found. Check 'cb help' for full information."
28
+ puts "usage: appli [command]"
29
+ puts "Command not found. Check 'appli help' for full information."
36
30
  Process.exit(1)
37
31
  end
32
+ end
33
+
34
+ def run_command(command, type, args = [])
35
+ array = (type == :global ? @global_commands : @application_commands)
36
+
37
+ options = parse_options(args)
38
+ options.merge!({:application => args.shift}) if type == :app
38
39
 
39
- options = parse_options(args)
40
- if args.size < commands_array[command][:required_args].to_i
40
+ if args.size < array[command][:required_args].to_i
41
41
  puts "error: #{commands_array[command][:usage]}"
42
42
  puts "See 'cb help #{command}' for usage."
43
43
  Process.exit(1)
44
44
  end
45
- options.merge!({:application => application})
46
- commands_array[command][:block].call(options, *args)
45
+
46
+ array[command][:block].call(options, *args)
47
47
  end
48
48
 
49
49
  def command(command, options = {}, &block)
@@ -74,6 +74,10 @@ module Appli
74
74
  @application_commands
75
75
  end
76
76
 
77
+ def commands
78
+ global_commands.merge(application_commands)
79
+ end
80
+
77
81
  def desc(value)
78
82
  @next_description = Array.new if @next_description.nil?
79
83
  @next_description << value
@@ -88,6 +92,11 @@ module Appli
88
92
  @next_flags[key] = value
89
93
  end
90
94
 
95
+ def alias(a, b)
96
+ @aliases = Hash.new if @aliases.nil?
97
+ @aliases[a] = b
98
+ end
99
+
91
100
  def load_commands
92
101
  Dir[File.join(File.dirname(__FILE__), 'commands', '*.rb')].each do |path|
93
102
  Appli.module_eval File.read(path), path
@@ -139,7 +148,7 @@ Appli.command "help", :global => true do |command|
139
148
  puts "For more information see http://www.applihq.com/gem"
140
149
  puts "See 'appli help [command]' for usage information."
141
150
  else
142
- if c = Appli.global_commands[command]
151
+ if c = Appli.commands[command]
143
152
  puts c[:description]
144
153
  if c[:usage]
145
154
  puts
@@ -164,3 +173,6 @@ Appli.usage "cb version"
164
173
  Appli.command "version", :global => true do
165
174
  puts "Appli Gem Version #{Appli::VERSION}"
166
175
  end
176
+
177
+ Appli.alias '-v', 'version'
178
+ Appli.alias '-s', 'ssh'
@@ -0,0 +1,50 @@
1
+ desc 'Create a MySQL console connection for your first database'
2
+ usage "appli [application] mysql"
3
+ command "db:console" do
4
+ database = get("applications/#{@options[:application]}/databases").first
5
+ if database
6
+ database = database['database']
7
+ puts "Connecting to database '#{database['name']}' on '#{database['host']['name']}'"
8
+ exec("ssh -t -p #{application['ssh_port']} app@#{host['name']} mysql -u #{database['username']} -p#{database['password']} -h #{database['host']['name']} #{database['name']}")
9
+ else
10
+ error("No database was not found for this application.")
11
+ end
12
+ end
13
+
14
+ desc 'Import a named MySQL file'
15
+ usage "appli [application] db:import {database number} {path to import mysql dump}"
16
+ command "db:import" do |number, path|
17
+ error("Import file not found at '#{path}'") unless File.exist?(path)
18
+ database = get("applications/#{@options[:application]}/databases/#{number}")
19
+ if database
20
+ database = database['database']
21
+ puts "Uploading MySQL export to server..."
22
+ remote_exec("rm -f ~/.appli_import.sql")
23
+ system("scp -P #{application['ssh_port']} #{path} app@#{host['name']}:~/.appli_import.sql")
24
+ puts "Importing database into '#{database['name']}' on '#{database['host']['name']}'..."
25
+ remote_exec("mysql -u #{database['username']} -p#{database['password']} -h #{database['host']['name']} #{database['name']} < ~/.appli_import.sql")
26
+ else
27
+ error "No database was found with number '#{number}'"
28
+ end
29
+ end
30
+
31
+ desc 'List all databases on this application'
32
+ usage "appli [application] db"
33
+ command "db" do
34
+ puts '-' * 85
35
+ print 'Name'.ljust(15)
36
+ print 'Username'.ljust(15)
37
+ print 'Password'.ljust(15)
38
+ print 'Host'.ljust(30)
39
+ puts
40
+ puts '-' * 85
41
+
42
+ for database in get("applications/#{@options[:application]}/databases")
43
+ database = database['database']
44
+ print database['name'].ljust(15)
45
+ print database['username'].ljust(15)
46
+ print database['password'].ljust(15)
47
+ print database['host']['name'].ljust(30)
48
+ puts
49
+ end
50
+ end
@@ -1,7 +1,7 @@
1
1
  desc "Display system memory usage"
2
2
  usage "appli mem:system"
3
3
  command "mem:system" do
4
- puts get_from_ssh("free -m").split("\n")[0,2].join("\n")
4
+ puts remote_exec("free -m").split("\n")[0,2].join("\n")
5
5
  end
6
6
 
7
7
  desc 'Display passenger memory statistics'
@@ -13,7 +13,7 @@ end
13
13
  desc 'Display your current disk space usage'
14
14
  usage "appli diskusage"
15
15
  command "diskusage" do
16
- output = get_from_ssh("df -h").split("\n")[1]
16
+ output = remote_exec("df -h").split("\n")[1]
17
17
  output = output.split(/\s+/)
18
18
  fs, size, usage, available, usage_percent, mountpoint = output
19
19
  puts "You are using #{usage} (including system files)."
@@ -32,9 +32,21 @@ command "settings", :global => true do
32
32
  puts "Domain..........: #{git_config_variable(:domain) || 'unknown'}"
33
33
  end
34
34
 
35
-
36
35
  desc 'SSH to the named server'
37
36
  usage "appli [application] ssh"
38
37
  command "ssh" do
39
- exec "ssh -p #{application['ssh_port']} app@mercury.uk.applihq.com"
38
+ exec "ssh -p #{application['ssh_port']} app@#{host['name']}"
39
+ end
40
+
41
+ desc 'Create a MySQL console connection for your first database'
42
+ usage "appli [application] mysql"
43
+ command "mysql" do
44
+ database = get("applications/#{@options[:application]}/databases").first
45
+ if database
46
+ database = database['database']
47
+ puts "Connecting to database '#{database['name']}' on '#{database['host']['name']}'"
48
+ exec("ssh -t -p #{application['ssh_port']} app@#{host['name']} mysql -u #{database['username']} -p#{database['password']} -h #{database['host']['name']} #{database['name']}")
49
+ else
50
+ error("No database was not found for this application.")
51
+ end
40
52
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-04 00:00:00 +00:00
12
+ date: 2010-02-15 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -35,6 +35,7 @@ files:
35
35
  - lib/appli/command.rb
36
36
  - lib/appli.rb
37
37
  - lib/commands/gems.rb
38
+ - lib/commands/mysql.rb
38
39
  - lib/commands/system.rb
39
40
  has_rdoc: true
40
41
  homepage: http://www.atechmedia.com