Skip to content

Instantly share code, notes, and snippets.

@obfusk
Last active August 29, 2015 13:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obfusk/9188866 to your computer and use it in GitHub Desktop.
Save obfusk/9188866 to your computer and use it in GitHub Desktop.
eftcmdr + nap
<% # {{{1
require 'json'
app, apps_file, is_admin, *tasks_wl = args
q = -> { puts 'press return ...'; $stdin.readline }
tasks_ = Hash[
shc!('RAILS_ENV=production bundle exec rake -T') \
.stdout.lines.grep(/^rake /) \
.map { |l| l.chomp.split(/\s+/, 3)[1..2] } \
.map { |(t,d)| [t,d.sub(/^# /,'')] } .sort \
.select { |(t,d)| t =~ /\A[a-zA-Z_:]+\z/ } # TODO
]
tasks = is_admin == 'true' ? tasks_ :
tasks_.select { |k,v| tasks_wl.include? k }
%> # }}}1
&_menu
menu: menu_rakes
title: rake tasks
text: <%= "Choose a rake task to run for app #{app}".to_json %>
choices:
<% tasks.each_pair do |t,d| %>
- tag: <%= t.to_json %>
text: <%= d.to_json %>
then:
eval: eval_rake_task
code: |
osh!('RAILS_ENV=production bundle exec rake "$1"',
ctx[:menu_rakes][:tag]); q[]
then: *_menu
<% end %>
admin:
- foo
control:
- bar
- qux
- quuux
view: all
rake:
qux: &qux
- bar
quux: *qux
<% # {{{1
require 'json'
q = -> { puts 'press return ...'; $stdin.readline }
rake_yml = File.expand_path '../_rake.yml', file
rake_apps = File.expand_path '../_rake.apps', file
nap_home = ENV['NAP_APPS_DIR'] || Dir.home
appdir_for = -> app { "#{nap_home}/#{app}/app" }
all_apps = %x[ naps list ].lines.map(&:chomp)
apps_file = File.expand_path \
"../#{File.basename(file, '.yml')}.apps", file
apps_ = File.exists?(apps_file) ?
YAML.load(File.read(apps_file)) : {}
rapps = File.exists?(rake_apps) ?
YAML.load(File.read(rake_apps)) : {}
perms = { 'admin' => [], 'control' => [], 'rake' => {} }.merge \
Hash[apps_.map do |k,v|
[k, v == 'all' ? all_apps : v ]
end]
apps = perms.reject { |k,v| k == 'rake' } .values.flatten.uniq.sort
logs = Hash[apps.map do |x|
[x, Hash[%x[ nap log #{x} assoc ].lines
.map { |l| l.chomp.split /\s+/, 2 } .sort]]
end]
rake_wl = -> app {
( perms['rake'].fetch('~all', []) |
perms['rake'].fetch(app , []) |
rapps .fetch('~all', []) |
rapps .fetch(app , []) )
}
branch = -> dir {
shc!('git symbolic-ref -q HEAD', chdir: dir).stdout \
.chomp.sub %r{^refs/heads/}, ''
}
%> # }}}1
&_menu
menu: menu_apps
title: apps
text: Choose an app
choices:
<% apps.each_with_index do |x,i| %>
- tag: <%= x.to_json %>
text: <%= x.to_json %>
then:
eval: eval_menu_apps
code: |
app = ctx[:menu_apps][:tag]
appdir = appdir_for[app]
then: &_menu_commands_<%= i %>
_stat_<%= i %>: &_stat_<%= i %> # {{{1
tag: stat
text: app status
then:
eval: eval_nap_status
code: |
osh! 'nap status "$1"', app; q[]
then: *_menu_commands_<%= i %>
# }}}1
_info_<%= i %>: &_info_<%= i %> # {{{1
tag: info
text: app info
then:
eval: eval_nap_info
code: |
osh! 'nap info "$1"', app; q[]
then: *_menu_commands_<%= i %>
# }}}1
_logs_<%= i %>: &_logs_<%= i %> # {{{1
tag: log
text: view log files
then: &_menu_logs_<%= i %>
menu: menu_logs
title: log files
text: <%= ("Choose a log file to view for app #{x}" +
" (^C quits viewing)").to_json %>
choices:
<% logs[x].each_pair do |n,f| %>
- tag: <%= n.to_json %>
text: <%= f.to_json %>
then:
ask: ask_lines
text: How many lines?
default: 100
validate: \A\d+\z
then:
eval: eval_view_log
code: |
trap('INT') { ohai 'bye' }
l = logs[app][ctx[:menu_logs][:tag]]
r = osh 'tail -f -n "$1" "$2"', ctx[:ask_lines], l
s = r.status.termsig
s && Signal.list.key(s) == 'INT' or raise 'tail failed'
then: *_menu_logs_<%= i %>
cancel: *_menu_logs_<%= i %>
<% end %>
cancel: *_menu_commands_<%= i %>
# }}}1
_hist_<%= i %>: &_hist_<%= i %> # {{{1
tag: hist
text: git history
then:
ask: ask_commits
text: How many commits?
default: 100
validate: \A\d+\z
then:
eval: eval_nap_hist
code: |
osh! 'PAGER= nap log "$1" hist "$2" -v',
app, ctx[:ask_commits]; q[]
then: *_menu_commands_<%= i %>
cancel: *_menu_commands_<%= i %>
# }}}1
_start_<%= i %>: &_start_<%= i %> # {{{1
tag: start
text: start app
then:
eval: eval_nap_start
code: |
osh! 'nap start "$1"', app; q[]
then: *_menu_commands_<%= i %>
# }}}1
_stop_<%= i %>: &_stop_<%= i %> # {{{1
tag: stop
text: stop app
then:
eval: eval_nap_stop
code: |
osh! 'nap stop "$1"', app; q[]
then: *_menu_commands_<%= i %>
# }}}1
_update_<%= i %>: &_update_<%= i %> # {{{1
tag: update
text: update app
then:
eval: eval_nap_update
code: |
osh! 'nap update "$1"', app; q[]
then: *_menu_commands_<%= i %>
# }}}1
_whenever_<%= i %>: &_whenever_<%= i %> # {{{1
tag: whenever
text: update crontab for whenever
then:
eval: eval_whenever
code: |
osh! 'RAILS_ENV=production bundle exec whenever ' +
'--update-crontab "$1"', app, chdir: appdir; q[]
then: *_menu_commands_<%= i %>
# }}}1
_rake_<%= i %>: &_rake_<%= i %> # {{{1
tag: rake
text: run rake tasks
then:
eval: eval_eftcmdr_rake
code: |
osh! 'eftcmdr "$@"', rake_yml, app, apps_file,
perms['admin'].include?(app).to_s, *rake_wl[app],
chdir: appdir
then: *_menu_commands_<%= i %>
# }}}1
_console_<%= i %>: &_console_<%= i %> # {{{1
tag: console
text: rails console
then:
eval: eval_rails_console
code: |
osh! 'RAILS_ENV=production bundle exec rails c', chdir: appdir
then: *_menu_commands_<%= i %>
# }}}1
_psql_<%= i %>: &_psql_<%= i %> # {{{1
tag: psql
text: postgresql console
then:
eval: eval_psql
code: |
osh! 'psql -h localhost -U "$1" "$1"', app, chdir: appdir
then: *_menu_commands_<%= i %>
# }}}1
_reset_<%= i %>: &_reset_<%= i %> # {{{1
tag: reset
text: git hard reset
then:
show_text: show_text_git_status
scroll: true
code: |
oshc!('pwd; git fetch --all; git status',
merge: true, exit: true, chdir: appdir).stdout
then:
ask_yesno: ask_yesno_git_reset
text: Are you sure?
then:
eval: eval_git_reset
code: |
b = branch[appdir]
osh! 'git reset --hard origin/"$1"', b, chdir: appdir
q[]
then: *_menu_commands_<%= i %>
else:
eval: eval_git_reset_cancelled
code: |
ohai 'git reset cancelled'; q[]
then: *_menu_commands_<%= i %>
# }}}1
menu: menu_commands
title: commands
text: <%= "Choose a command to run for app #{x}".to_json %>
choices:
- *_stat_<%= i %>
- *_info_<%= i %>
- *_logs_<%= i %>
- *_hist_<%= i %>
<% if perms['admin'].include?(x) ||
perms['control'].include?(x) %>
- *_start_<%= i %>
- *_stop_<%= i %>
- *_update_<%= i %>
- *_whenever_<%= i %>
<% end %>
# ... TODO ...
<% if perms['admin'].include?(x) ||
(perms['control'].include?(x) && !rake_wl[x].empty?) %>
- *_rake_<%= i %>
<% end %>
<% if perms['admin'].include?(x) %>
- *_console_<%= i %>
- *_psql_<%= i %>
- *_reset_<%= i %>
<% end %>
cancel: *_menu
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment