Permalink
Browse files

US2816 Refactor RHC tail command

  • Loading branch information...
1 parent c190c0e commit 2eb7ff9e8e6a70199765eb859f685ed43b823e58 Chris Alfonso committed with John (J5) Palmieri Sep 18, 2012
View
6 doc/USAGE.txt
@@ -87,13 +87,13 @@ You can also use rhc-snapshot to restore an application from an existing snapsho
rhc-snapshot -a eat -r ./eat.tar.gz
-rhc-tail-files
+rhc tail
===========
-rhc-tail-files is used tail the logs or any files within an application. The example below uses the
+rhc tail is used to tail the logs or any files within an application. The example below uses the
default log location of '<application_name>/logs/*'.
- rhc-tail-files -a eat
+ rhc tail -a eat
Upon successful execution, the application logs will be tailed in your terminal. Use ctrl+c to cancel.
View
27 lib/rhc/commands/tail.rb
@@ -0,0 +1,27 @@
+require 'rhc/commands/base'
+require 'rhc/config'
+require 'rhc-common'
+module RHC::Commands
+ class Tail < Base
+ summary "Tail the logs of an application"
+ syntax "<application>"
+ argument :app, "Application name (alphanumeric - max 32 chars) (required)", []
+ option ["-n", "--namespace namespace"], "Namespace of your application", :context => :namespace_context, :required => true
+ option ["-o", "--opts options"], "Options to pass to the server-side (linux based) tail command (applicable to tail command only) (-f is implicit. See the linux tail man page full list of options.) (Ex: --opts '-n 100')"
+ option ["-f", "--files files"], "File glob relative to app (default <application_name>/logs/*) (optional)"
+ alias_action :"app tail", :root_command => true, :deprecated => true
+ def run(app)
+ begin
+ rest_domain = rest_client.find_domain(options.namespace)
+ rest_app = rest_domain.find_application(app)
+ rest_app.tail(options)
+ rescue Interrupt
+ results { say "Terminating..." }
+ rescue SocketError => e
+ results { say "#{e.message}" }
+ exit 1
+ end
+ 0
+ end
+ end
+end
View
24 lib/rhc/rest/application.rb
@@ -109,6 +109,30 @@ def find_cartridges(name, options={})
def host
@host ||= URI(app_url).host
end
+
+ #Application log file tailing
+ def tail(options)
+ debug "Tail in progress for #{name}"
+
+ file_glob = options.files ? options.files : "#{name}/logs/*"
+ remote_cmd = "tail#{options.opts ? ' --opts ' + Base64::encode64(options.opts).chomp : ''} #{file_glob}"
+ ssh_cmd = "ssh -t #{uuid}@#{host} '#{remote_cmd}'"
+ begin
+ #Use ssh -t to tail the logs
+ debug ssh_cmd
+ ssh_ruby(host, uuid, remote_cmd)
+ rescue SocketError => e
+ msg <<MESSAGE
+Could not connect: #{e.message}
+You can try to run this manually if you have ssh installed:
+#{ssh_cmd}
+
+MESSAGE
+ debug "DEBUG: #{e.debug}\n"
+ raise e
+ end
+ end
+ alias :delete :destroy
end
end
end
View
16 lib/rhc/rest/client.rb
@@ -99,6 +99,22 @@ def find_key(name)
user.find_key(name) or raise RHC::KeyNotFoundException.new("Key #{name} does not exist")
end
+ def sshkeys
+ logger.debug "Finding all keys for #{user.login}" if @mydebug
+ user.keys
+ end
+
+ def add_key(name, key, content)
+ logger.debug "Adding key #{key} for #{user.login}" if @mydebug
+ user.add_key name, key, content
+ end
+
+ def delete_key(name)
+ logger.debug "Deleting key '#{name}'" if @mydebug
+ key = find_key(name)
+ key.destroy
+ end
+
def logout
#TODO logout
debug "Logout/Close client"
View
3 man/rhc-ctl-domain.1
@@ -84,6 +84,5 @@ Mike McGrath <mmcgrath@redhat.com>, Jim Jagielski <jimjag@redhat.com>
.BR rhc-create-app (1),
.BR rhc-ctl-app (1),
.BR rhc-snapshot(1),
-.BR rhc-tail-files(1),
.BR rhc-port-forward(1),
-.BR express.conf (5)
+.BR express.conf (5)
View
3 man/rhc-domain-info.1
@@ -58,5 +58,4 @@ Mike McGrath <mmcgrath@redhat.com>, Jim Jagielski <jimjag@redhat.com>
.BR rhc-snapshot(1),
.BR rhc-create-domain(1),
.BR rhc-ctl-domain (1),
-.BR rhc-tail-files(1),
-.BR express.conf (5)
+.BR express.conf (5)
View
85 man/rhc-tail.1
@@ -0,0 +1,85 @@
+.\" Process this file with
+.\" groff -man -Tascii rhc-tail.1
+.\"
+.TH "RHC_TAIL" "1" "MAY 2011" "Linux" "User Manuals"
+.SH "NAME"
+rhc\-tail \- Tail files from remote running applications in real\-time
+.SH "SYNOPSIS"
+.B rhc tail [\-p
+.I password
+.B ]
+.B [\-f
+.I pathname
+.B ] [\-d] [\-h]
+.B [\-l
+.I rhlogin
+.B ] [\-o
+.I options
+.B ]
+.B [\-\-config
+.I config_path
+.B ]
+.B [\-\-timeout
+.I #
+.B ] \-a
+.I application_name
+.SH "DESCRIPTION"
+.B rhc tail
+connects to a remote running application and tail's files in
+real\-time. This is very useful for watching logs.
+.SH "OPTIONS"
+.IP "\-l|\-\-rhlogin login"
+Red Hat login (RHN or OpenShift login with OpenShift access)
+.IP "\-p|\-\-password password"
+RHLogin password
+.IP "\-a|\-\-app application_name"
+Name of remote running application
+.IP "\-f|\-\-files"
+Relative file path glob. Default: <application_name>/logs/*
+.IP "\-o|\-\-opts options"
+Options to pass to the server\-side (linux based) tail command (\-f is implicit. See the linux tail man page full list of options.) (Ex: \-\-opts '\-n 100')
+Note: Options not supported by the rhcloud server will result in an error.
+.IP \-d|\-\-debug
+Enable debugging / verbose output
+.IP \-h|\-\-help
+Display help menu
+.IP \-\-config
+Alternate full pathname for the config file
+.IP \-\-timeout
+Timeout value, in seconds, for the connection (default is 10)
+.SH "FILES"
+.I <ruby_gem_dir>/gems/rhc\-<version>/conf/express.conf
+.RS
+The RubyGem system wide configuration file. See
+.BR express.conf (5)
+for further details.
+.RE
+.I /etc/openshift/express.conf
+.RS
+The system wide configuration file. See
+.BR express.conf (5)
+for further details.
+.RE
+.I ~/.openshift/express.conf
+.RS
+Per user configuration file. See
+.BR express.conf (5)
+for further details.
+.RE
+.SH "BUGS"
+Please contact the Red Hat OpenShift team.
+Forums: https://openshift.redhat.com/community/forums/openshift
+IRC: #openshift on irc.freenode.net
+.SH "AUTHOR"
+Mike McGrath <mmcgrath@redhat.com>, Jim Jagielski <jimjag@redhat.com>
+.SH "SEE ALSO"
+.BR rhc\-chk (1),
+.BR rhc\-create\-app(1),
+.BR rhc\-ctl\-app(1),
+.BR rhc\-create\-domain(1),
+.BR rhc\-ctl\-domain (1),
+.BR rhc\-domain\-info (1),
+.BR rhc\-snapshot(1),
+.BR rhc\-port\-forward(1),
+.BR express.conf(5),
+.BR tail(1)
View
3 man/rhc.1
@@ -24,6 +24,8 @@ Forward remote ports to the workstation
Run the setup wizard to configure your account
.IP server
Display information about the status of the service
+.IP tail
+Tail files from remote running applications in real\-time
.IP threaddump
Trigger a thread dump for JBossAS, JBossEAP, and Ruby
.IP \-h|\-\-help|help
@@ -62,5 +64,6 @@ Mike McGrath <mmcgrath@redhat.com>, Jim Jagielski <jimjag@redhat.com>
.BR rhc-domain (1),
.BR rhc-sshkey (1),
.BR rhc-port-forward (1),
+.BR rhc-tail (1),
.BR rhc-threaddump (1),
.BR express.conf (5)
View
61 spec/rhc/commands/tail_spec.rb
@@ -0,0 +1,61 @@
+require 'spec_helper'
+require 'rest_spec_helper'
+require 'rhc/commands/tail'
+require 'rhc/config'
+describe RHC::Commands::Tail do
+ let(:client_links) { mock_response_links(mock_client_links) }
+ let(:domain_0_links) { mock_response_links(mock_domain_links('mock_domain_0')) }
+ let(:domain_1_links) { mock_response_links(mock_domain_links('mock_domain_1')) }
+ let(:app_0_links) { mock_response_links(mock_app_links('mock_domain_0', 'mock_app_0')) }
+ before(:each) do
+ RHC::Config.set_defaults
+ @rc = MockRestClient.new
+ @rc.add_domain("mock-domain-0").add_application("mock-app-0", "ruby-1.8.7")
+ stub_api_request(:any, client_links['LIST_DOMAINS']['relative']).with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate'}).
+ to_return({ :body => {
+ :type => 'domains',
+ :data =>
+ [{ :id => 'mock-domain-0',
+ :links => mock_response_links(mock_domain_links('mock_domain_0')),
+ },
+ { :id => 'mock-domain-1',
+ :links => mock_response_links(mock_domain_links('mock_domain_1')),
+ }]
+ }.to_json,
+ :status => 200
+ })
+ stub_api_request(:any, domain_0_links['LIST_APPLICATIONS']['relative']).with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate'}).
+ to_return({ :body => {
+ :type => 'applications',
+ :data =>
+ [{ :domain_id => 'mock-domain-0',
+ :name => 'mock-app-0',
+ :creation_time => Time.new.to_s,
+ :uuid => 1234,
+ :aliases => ['alias_1', 'alias_2'],
+ :server_identity => 'mock_server_identity',
+ :links => mock_response_links(mock_app_links('mock_domain_0','mock_app_0')),
+ }]
+ }.to_json,
+ :status => 200
+ })
+ end
+
+ describe 'help' do
+ let(:arguments) { ['tail', '--help'] }
+
+ context 'help is run' do
+ it "should display help" do
+ expect { run }.should exit_with_code(0)
+ end
+ it('should output usage') { run_output.should match("Usage: rhc tail") }
+ end
+ end
+
+ describe 'tail no args' do
+ let(:arguments) { ['tail', '--noprompt', '--config', 'test.conf', '-l', 'test@test.foo', '-p', 'password'] }
+ context 'args not supplied' do
+ it { expect { run }.should exit_with_code(1) }
+ end
+ end
+end

0 comments on commit 2eb7ff9

Please sign in to comment.