Commit 0b78fc33 by ifool

Refactor setup script

parent b62c7083
## 操作说明
进入配置文件项目(`cd vcooline_ikcrm_config`),执行如下命令:
进入配置文件项目(`cd vcooline_ikcrm_config; git checkout ding`),执行如下命令:
```bash
ruby -e "`curl http://gitlab.ikcrm.com/ikcrm_common/servers_config/raw/master/setup_script.rb`" crm prod
ruby -e "`curl http://gitlab.ikcrm.com/ikcrm_common/servers_config/raw/master/setup.rb`" prod
```
### 说明
1. 上述脚本中的参数 `crm` 需匹配该项目的1级子目录名称
1. 项目配置文件(vcooline_ikcrm_config)的分支名称(`ding`分支)需匹配该项目的2级子目录名称
1. 上述脚本中的参数 `prod` 需匹配该项目的3级子目录名称
1. 项目配置文件名称(`vcooline_ikcrm_config`)去掉 `_config` 后需匹配该项目的1级子目录名称。
1. 项目配置文件(`vcooline_ikcrm_config`)的分支名称(`ding`分支)需匹配该项目的2级子目录名称
1. 上述脚本中的参数 `prod` 为环境名称,需匹配该项目的3级子目录名称
#### 以CRM钉钉版生产环境为例:
1. 配置文件项目名称为 `vcooline_ikcrm_config`,去掉 `_config` 后对应1级子目录为`vcooline_ikcrm`
1. 分支为 `ding`,对应2级子目录为 `ding`
1. 环境名称为 `prod`,对应3级子目录为 `prod`
1. `server.json` url对应的全路径为: http://gitlab.ikcrm.com/ikcrm_common/servers_config/raw/master/`vcooline_ikcrm`/`ding`/`prod`/server.json
......
require 'erb'
require 'json'
class ScriptGenerator
attr_accessor :product_name, :git_branch, :config_root
def initialize
self.config_root = File.expand_path('..', __FILE__)
self.product_name = File.basename(File.expand_path('..', config_root).gsub("_config", ""))
self.git_branch = `cd #{config_root}; git rev-parse --abbrev-ref HEAD`.chop
end
def download_server_json
env_name = File.basename config_root
server_conf_url = "http://gitlab.ikcrm.com/ikcrm_common/servers_config/raw/master/#{product_name}/#{git_branch}/#{env_name}/server.json"
puts "Use server config url: #{server_conf_url}"
system "curl #{server_conf_url} > #{config_root}/server.json"
end
def deploy_binding
config_root = File.expand_path('..', __FILE__)
server_conf = JSON(File.read("#{__dir__}/server.json"))
binding
end
def generate_push_config
template = ERB.new File.read("#{__dir__}/push_config.sh.erb"), nil, '>'
script_content = template.result(deploy_binding)
puts "\n\n\n******************* push_config content *******************\n\n\n"
puts script_content
File.open("#{__dir__}/push_config.sh", "w") { |f| f.write(script_content) }
end
def generate_pull_config
template = ERB.new File.read("#{__dir__}/pull_config.sh.erb"), nil, '>'
script_content = template.result(deploy_binding)
puts "\n\n\n******************* pull_config content *******************\n\n\n"
puts script_content
File.open("#{__dir__}/pull_config.sh", "w") { |f| f.write(script_content) }
end
end
require 'erb'
require 'json'
def download_server_json
product_name = ARGV[0]
config_root = File.expand_path('..', __FILE__)
git_branch = `cd #{config_root}; git rev-parse --abbrev-ref HEAD`.chop
dir_basename = File.basename config_root
server_conf_url = "http://gitlab.ikcrm.com/ikcrm_common/servers_config/raw/master/#{product_name}/#{git_branch}/#{dir_basename}/server.json"
puts "Use server config url: #{server_conf_url}"
system "curl #{server_conf_url} > #{config_root}/server.json"
end
#!/usr/bin/env ruby
def deploy_binding
config_root = File.expand_path('..', __FILE__)
server_conf = JSON(File.read("#{__dir__}/server.json"))
binding
env_name = ARGV[0] # dev, test, staging, prod
config_root = File.expand_path("../#{env_name}", __FILE__)
if env_name.nil?
raise "Dir name is required!"
elsif !Dir.exists?(config_root)
raise "Dir: #{config_root} not exists!"
end
def generate_push_config
template = ERB.new File.read("#{__dir__}/push_config.sh.erb"), nil, '>'
script_content = template.result(deploy_binding)
puts "\n\n\n******************* push_config content *******************\n\n\n"
puts script_content
File.open("#{__dir__}/push_config.sh", "w") { |f| f.write(script_content) }
end
puts "======================= config_root is: #{config_root} ======================="
def generate_pull_config
template = ERB.new File.read("#{__dir__}/pull_config.sh.erb"), nil, '>'
script_content = template.result(deploy_binding)
puts "\n\n\n******************* pull_config content *******************\n\n\n"
puts script_content
File.open("#{__dir__}/pull_config.sh", "w") { |f| f.write(script_content) }
end
git_config_url = "http://gitlab.ikcrm.com/ikcrm_common/servers_config/raw/master"
setup_url = "#{git_config_url}/script_generator.rb"
puts "Use script_generator url: #{setup_url}"
system "curl #{setup_url} > #{config_root}/script_generator.rb"
push_script_url = "#{git_config_url}/push_config.sh.erb"
puts "Use push script url: #{push_script_url}"
system "curl #{push_script_url} > #{config_root}/push_config.sh.erb"
pull_script_url = "#{git_config_url}/pull_config.sh.erb"
puts "Use pull script url: #{pull_script_url}"
system "curl #{pull_script_url} > #{config_root}/pull_config.sh.erb"
require "#{config_root}/script_generator.rb"
generator = ScriptGenerator.new
generator.download_server_json
generator.generate_push_config
generator.generate_pull_config
download_server_json
generate_push_config
generate_pull_config
# Usage example:
# `ruby setup_script.rb crm prod` OR ./setup_script.rb crm prod
#!/usr/bin/env ruby
product_name = ARGV[0] # crm, jxc, bms
dir_name = ARGV[1] # dev, test, staging, prod
config_root = File.expand_path("../#{dir_name}", __FILE__)
if dir_name.nil?
raise "Dir name is required!"
elsif !Dir.exists?(config_root)
raise "Dir: #{config_root} not exists!"
end
puts "======================= config_root is: #{config_root} ======================="
git_config_url = "http://gitlab.ikcrm.com/ikcrm_common/servers_config/raw/master"
setup_url = "#{git_config_url}/setup.rb"
puts "Use setup url: #{setup_url}"
system "curl #{setup_url} > #{config_root}/setup.rb"
push_script_url = "#{git_config_url}/push_config.sh.erb"
puts "Use push script url: #{push_script_url}"
system "curl #{push_script_url} > #{config_root}/push_config.sh.erb"
pull_script_url = "#{git_config_url}/pull_config.sh.erb"
puts "Use pull script url: #{pull_script_url}"
system "curl #{pull_script_url} > #{config_root}/pull_config.sh.erb"
require "#{config_root}/setup.rb"
# Usage example:
# `ruby setup_script.rb crm prod` OR ./setup_script.rb crm prod
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment