情况是这样的, 公司内部测试服务器经常需要更新代码供测试人员使用网站, 每次都是我们后端开发人员部署的, 这样就加大了工作量,效率低下,话说我们部署也是使用capistrano 的, 只需一条命令就可以顺利部署, 但是还是不如非开发人员部署来的方便,于是就有了 rake_ui
rake_ui gem 是我发布的,但是是在修改别人代码的基础上发布的,下面介绍使用方法:
首先看下效果图:
1, 环境
Node.js Socket.io Rails 3.x
2,Gemfile
gem 'rake_ui', '0.6.0'
3, 在你的 routes.rb 中添加路由
Rails.application.routes.draw do mount RakeUi::Engine => "/rake_ui" end
4, 配置 config/rake_ui.yml
host: '192.168.10.107' log: '/data/projects/entos/log/rake.log'
host是你的ip地址,Nodejs 要用, log 是你项目下log目录下rake.log 会被自动创建
5, 配置 config/tasks.yml
- 'rake about' - 'rake routes'
把你需要执行的rake任务写在这个 yaml 中
6, 启动 nodejs server
rake start_node_server
ok, 你可以访问 /rake_ui 看到你的 web gui 界面了, 把你的部署方案写在rake任务中, 然后在这个界面可以点击部署.
该gem有可能被更新,看到最新的说明请移到步这里:
环境:ruby 1.9.2 + rake 0.8.7 + rails 3.0.3
今天在写 rake 文件 , 提示我没找到 model Class,问题在于我没加 => :environment
, 那为什么加上 => :environment
就可以了 , 研究了下rake:
task :test => :environment do end
其中后面的 :environment
也是 一个 task , 也就是说在执行 test 这个 task 之前 会执行 :environment 这个 task,有点像rails controller 中的 before_filter ,那么 :environment 这个task 定义在什么地方,rails project中也没有啊 。。。,定义在 rails 源码中了 ,你在 你的project下执行 rake environment 不会报错的
wxianfeng@ubuntu:/usr/local/system/projects/entos/ent_os$ rake environment --trace (in /usr/local/system/projects/entos/ent_os) ** Invoke environment (first_time) ** Execute environment
不过 也看不到 输出什么信息,因为只是加载了 运行环境而已 ,那么从哪里执行的,定义在 project 下 Rakefile 中了
# Add your own tasks in files placed in lib/tasks ending in .rake, # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. require File.expand_path('../config/application', __FILE__) require 'rake' EntOs::Application.load_tasks
继续跟踪 load_tasks:
最后 找到了 task environment
def initialize_tasks require "rails/tasks" task :environment do $rails_rake_task = true require_environment! end end
全部文件 here
所以加载了 environment 也就可以 找到 model Class 了
总结:
1,rake 命名格式
rakefile
, Rakefile
, rakefile.rb
, Rakefile.rb
都可以 ,常用 Rakefile
另外 rails 中使用的话,还可以 使用 .rake
后缀的文件
2,设置命名空间
namespace :test do end
3,设置默认task
task :default => ["test"]
可以指定多个 task,执行 rake 后 ,默认调用的是 default task
4,指定task执行顺序
task :test => [:hello,:world]
5,查看rake 任务
rake -T rake --tasks rake -T db: # 查看db打头的task
6,跟踪task
rake test --trace
更多 options
wxianfeng@ubuntu:/usr/local/system/projects/entos/ent_os$ rake -h rake [-f rakefile] {options} targets... Options are ... -C, --classic-namespace Put Task and FileTask in the top level namespace -D, --describe [PATTERN] Describe the tasks (matching optional PATTERN), then exit. -n, --dry-run Do a dry run without executing actions. -e, --execute CODE Execute some Ruby code and exit. -p, --execute-print CODE Execute some Ruby code, print the result, then exit. -E, --execute-continue CODE Execute some Ruby code, then continue with normal task processing. -I, --libdir LIBDIR Include LIBDIR in the search path for required modules. -P, --prereqs Display the tasks and dependencies, then exit. -q, --quiet Do not log messages to standard output. -f, --rakefile [FILE] Use FILE as the rakefile. -R, --rakelibdir RAKELIBDIR, Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib') --rakelib -r, --require MODULE Require MODULE before executing rakefile. --rules Trace the rules resolution. -N, --no-search, --nosearch Do not search parent directories for the Rakefile. -s, --silent Like --quiet, but also suppresses the 'in directory' announcement. -g, --system Using system wide (global) rakefiles (usually '~/.rake/*.rake'). -G, --no-system, --nosystem Use standard project Rakefile search paths, ignore system wide rakefiles. -T, --tasks [PATTERN] Display the tasks (matching optional PATTERN) with descriptions, then exit. -t, --trace Turn on invoke/execute tracing, enable full backtrace. -v, --verbose Log message to standard output. -V, --version Display the program version. -h, -H, --help Display this help message.
DEMO:
wxianfeng@ubuntu:~/test/rake$ cat Rakefile desc "Default: test" task :default => ["r:test"] namespace :r do desc "test" task :test do puts "test" end desc "test1" task :test1 => :test do puts "test1" end desc "test3" task :test3 => :test4 do puts "test3" end end
SEE:
http://hi.baidu.com/%D0%C7203/blog/item/ebda2dd09f1d698ea1ec9c7a.html http://guides.rubyonrails.org/command_line.html http://ericzouthoughts.wordpress.com/2009/06/20/execute-shell-command-in-ruby-rake-task/