Class: OspDeploy::RailsDeployer

Inherits:
Object
  • Object
show all
Defined in:
lib/osp_deploy/rails_deployer.rb

Constant Summary collapse

DEPLOY_CONFIG_FILE =
"config/osp-deploy.yml"
REQUIRED_OPTIONS =
{ :always => [:jobs, :server_uris, :tag_prefix],
                     :puma => [:remote_dest_file_dir],
                     :file => [:remote_dest_file_dir],
                     :war => [:remote_dest_file_dir, :war_file_name],
                     :torquebox => [:appl_name, :torquebox_home, :remote_dest_file_dir],
}
DEFAULT_EXCLUDES =
['coverage', 'nbproject', '.idea', 'tmp', 'log', '.git', '*~']
BUNDLER_DEFAULT_CMD =
"bundle install --deployment --without=test"

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RailsDeployer

Returns a new instance of RailsDeployer.



21
22
23
24
25
26
# File 'lib/osp_deploy/rails_deployer.rb', line 21

def initialize options
  @options = options
  @deployment_config = nil
  @thread_logger = nil
  @valid = nil
end

Instance Method Details

#deployObject

Deploy this project to the remote-servers



112
113
114
115
116
117
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
# File 'lib/osp_deploy/rails_deployer.rb', line 112

def deploy
  validate_config
  gen_version_initializer

  do_before_local = @deployment_config['do_before_local']
  do_cmds 'do_before_local', do_before_local

  if @deployment_config['jobs'].include?(:war)
    execute_cmd "RAILS_ENV=\"#{@rails_env}\" warble"
  end


  threads = nil
  threads = server_iterator(:threads => true) do |server_uri|
    do_before = @deployment_config['do_before']
    do_cmds 'do_before_local', do_before, server_uri, false

    if !([:file, :torquebox, :puma] & @deployment_config['jobs']).empty?
      sync_files_to_remote_server(server_uri)
    end

    torquebox_deploy(server_uri) if @deployment_config['jobs'].include?(:torquebox)
    puma_deploy(server_uri) if @deployment_config['jobs'].include?(:puma)
    war_deploy(server_uri) if @deployment_config['jobs'].include?(:war)

    do_after = @deployment_config['do_after'] || @deployment_config['post_commands']
    do_cmds 'do_after', do_after, server_uri, false
  end
  threads.each{|t| t.join }
  do_after_local = @deployment_config['do_after_local']
  do_cmds 'do_after_local', do_after_local
end

#gen_version_initializerObject



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
# File 'lib/osp_deploy/rails_deployer.rb', line 77

def gen_version_initializer
  validate_config unless @valid
  ini_file = 'version_from_git.rb'
  fname = File.join("config", "initializers", ini_file)

  # Ermittel Datum und ID des Commits vom HEAD
  glog = `git log --pretty=format:"%ci %h" -n 1 HEAD`
  cdate,refid = glog.scan(/(\d{4}-\d\d-\d\d \d\d:\d\d):\d\d [^ ]+ (\w+)/).flatten

  # Ermitteln letzten Tag als Version
  ver_nr = `git tag -l '#{@deployment_config['tag_prefix']}*' | tail -n 1`
  ver_nr.chomp!

  logger.info "Generate initializer '#{fname}' with: " +
         "APP_VERSION(:nr => '#{ver_nr}', :commit_date => '#{cdate}', :git_id => '#{refid}', :branch => #{@deployment_config[:branch]})"

  version_src = <<-EOT
# Version-Initializer
# Autogenerate from osp-git-deploy-tools
# DO NOT EDIT THIS !

app_structur = Struct.new :nr, :commit_date, :git_id, :branch
APP_VERSION = app_structur.new '#{ver_nr}', '#{cdate}', '#{refid}', '#{@deployment_config[:branch]}'
EOT
  File.open(fname, 'w'){|f| f.write(version_src) }
  make_last_changelog
end

#loggerObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/osp_deploy/rails_deployer.rb', line 30

def logger
  @thread_logger ||= begin
    #puts "Create ThreadInfoLogger."
    logger = OspDeploy::ThreadInfoLogger.new(STDOUT)
    logger.level = @options[:verbose] ? Logger::DEBUG : Logger::INFO
    # im HUDSON/JENKINS keine farbige Ausgabe
    logger.disable_ansi_colors=true if ( ENV['JENKINS_URL'] || ENV['HUDSON_URL'] )
    logger
  end
end

#make_last_changelogObject

make git-log for last 20 commits in medium format into file last-changelog.txt



150
151
152
153
# File 'lib/osp_deploy/rails_deployer.rb', line 150

def make_last_changelog
  logger.info "make_last_changelog"
  system("git log --abbrev-commit --pretty=medium  HEAD~20..HEAD > public/last-changelog.txt")
end

#validate_configObject



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
# File 'lib/osp_deploy/rails_deployer.rb', line 42

def validate_config
  dconfig_file_path = DEPLOY_CONFIG_FILE
  cur_branch = parse_git_branch
  logger.info "Read and validate deployment config file '#{dconfig_file_path}' for branch '#{cur_branch}'"
  config = YAML.load_file(dconfig_file_path)
  @deployment_config = config[cur_branch]
  unless @deployment_config
    raise "No config params for branch #{cur_branch} found in #{dconfig_file_path}!"
  end
  required_options = REQUIRED_OPTIONS[:always]
  jobs =  @deployment_config['jobs']
  jobs = jobs.split(',').map(&:strip).map(&:to_sym) if jobs
  @deployment_config['jobs'] = jobs
  jobs && jobs.each do |job|
    required_options |= REQUIRED_OPTIONS[job]
  end
  required_options.compact!
  required_options.uniq!
  validation_errors = required_options.select{|param_name| !@deployment_config.has_key?(param_name.to_s)}
  unless validation_errors.empty?
    logger.debug "Used deployment config: #{@deployment_config.inspect}"
    raise "Error in deployment config! Missing options: #{validation_errors.join(', ')}"
  end
  git_check
  @deployment_config[:branch] = cur_branch
  @deployment_config["server_uris"].freeze

  # some defaults
  @deployment_config['environment'] ||= production
  @rails_env = @deployment_config['environment'] 
  @valid = true
end