Class: Ztil::Command

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/ztil/command.rb

Instance Method Summary collapse

Instance Method Details

#backup(name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ztil/command.rb', line 35

def backup(name)
  storages = options[:storages] || 'local'

  run "backup_zh generate:model \
      --trigger #{name} \
      --databases=#{options[:databases]} \
      --compressor=gzip \
      --storages='#{storages}'"

  file = File.join( File.expand_path('~/Backup'), 'models', "#{name}.rb" )

  # 数据库名称
  database_name = options[:name] || name
  gsub_file file, /my_database_name/, database_name

  # 注释掉一些没用的
  options[:databases].split(',').map(&:strip).each do |database|
    case database
    when 'mysql'
      comment_lines file, 'skip_tables'
      comment_lines file, 'only_tables'
    when 'mongodb'
      comment_lines file, 'only_collections'
    end
  end

  # 邮箱设置
  if options[:email] && File.read(file) !~ /[email protected]/
    insert_into_file file, after: "compress_with Gzip\n" do
      <<-MAIL
  ##
  # Mail [Notifier]
  notify_by Mail do |mail|
mail.on_success           = true
mail.on_warning           = false
mail.on_failure           = true
mail.from                 = '[email protected]'
mail.address              = 'smtp.exmail.qq.com'
mail.port                 = 465
mail.domain               = 'aukudu.com'
mail.user_name            = '[email protected]'
mail.password             = 'Zsb170523402'
mail.authentication       = 'plain'
mail.encryption           = :ssl
mail.to                   = '#{options[:email]}'
  end
      MAIL
    end
  end

  run "vi #{file}"

  # 周期执行
  if options[:schedule]
    schedule_path = File.join( File.expand_path('~/Backup'), 'schedule')
    schedule_config_path = File.join(schedule_path, 'config')
    FileUtils.mkdir_p(schedule_config_path) unless File.directory?(schedule_config_path)
    run "wheneverize #{schedule_path}"
    time, at = options[:schedule].split('&').map(&:strip)
    append_to_file( File.join(schedule_config_path, 'schedule.rb') ) do
      <<-SCH
  every #{time}, :at => '#{at}' do
command "#{options[:run_prefix]} backup_zh perform -t #{name}"
  end
      SCH
    end
    run "whenever -f #{File.join(schedule_config_path, 'schedule.rb')} --update-crontab"
  else
    run "backup_zh perform -t #{name}"
  end

end

#cap_me(path = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ztil/command.rb', line 118

def cap_me(path = nil)
  path ||= '.'
  gemfile = File.expand_path("#{path}/Gemfile")

  raise "Can't find Gemfile #{gemfile}" unless File.exists?(gemfile)

  append_to_file gemfile do
    <<-GEMS

### Add by cap me
gem 'unicorn'
group :development do
  gem 'capistrano', '>= 3.1.0'
  gem 'capistrano-rails', '>= 1.1.0'
  gem 'capistrano-bundler'
  gem 'capistrano-rbenv', '>= 2.0'
end

    GEMS
  end

  run "cd #{path} && bundle install && bundle exec cap install"

  tmp_dir = Dir.mktmpdir
  run "git clone https://github.com/zires/capistrano-3-rails-template.git #{tmp_dir}"
  run "cp -R #{tmp_dir}/config #{path}/"
  run "cp -R #{tmp_dir}/lib #{path}/"
  append_to_file File.join(path, 'Capfile') do
    <<-CAP
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
Dir.glob('lib/capistrano/**/*.rb').each { |r| import r }
    CAP
  end

  uncomment_lines File.join(path, 'Capfile'), /require.+rbenv/
  uncomment_lines File.join(path, 'Capfile'), /require.+bundler/
  uncomment_lines File.join(path, 'Capfile'), /require.+rails/

  run "cd #{path} && bundle exec cap production deploy --dry-run"

end

#coordcoder(location) ⇒ Object



18
19
20
21
# File 'lib/ztil/command.rb', line 18

def coordcoder(location)
  require 'ztil/baidu'
  say Baidu.coordcoder(location, options)
end

#geocoder(address) ⇒ Object



10
11
12
13
# File 'lib/ztil/command.rb', line 10

def geocoder(address)
  require 'ztil/baidu'
  say Baidu.geocoder(address, options)
end

#static_meObject



111
112
113
# File 'lib/ztil/command.rb', line 111

def static_me
  run "ruby -run -ehttpd . -p#{options[:port]}"
end