Class: Dokuen::Application
- Inherits:
-
Object
- Object
- Dokuen::Application
- Defined in:
- lib/dokuen/application.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #clean ⇒ Object
- #create ⇒ Object
- #delete_env(var) ⇒ Object
- #deploy(revision) ⇒ Object
- #env ⇒ Object
- #get_env(var) ⇒ Object
-
#initialize(name, config) ⇒ Application
constructor
A new instance of Application.
- #install_nginx_config ⇒ Object
- #restart ⇒ Object
- #run_command(args) ⇒ Object
- #scale ⇒ Object
- #set_env(var, val) ⇒ Object
- #shutdown(release = nil) ⇒ Object
Constructor Details
#initialize(name, config) ⇒ Application
Returns a new instance of Application.
9 10 11 12 |
# File 'lib/dokuen/application.rb', line 9 def initialize(name, config) @name = name @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/dokuen/application.rb', line 7 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/dokuen/application.rb', line 7 def name @name end |
Instance Method Details
#clean ⇒ Object
224 225 226 227 228 229 230 |
# File 'lib/dokuen/application.rb', line 224 def clean with_current_release do Dir.glob(".dokuen/*.pid") do |f| File.delete(f) end end end |
#create ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dokuen/application.rb', line 56 def create Dir.chdir(File.join(config.dokuen_dir, 'apps')) do if File.exists?(name) raise "Application #{name} exists!" end FileUtils.mkdir_p(name) with_app_dir do dirs = [ 'releases', 'env', 'logs', 'build' ] FileUtils.mkdir_p(dirs) end end end |
#delete_env(var) ⇒ Object
34 35 36 37 38 |
# File 'lib/dokuen/application.rb', line 34 def delete_env(var) with_app_dir do File.delete(File.join("env", var)) rescue nil end end |
#deploy(revision) ⇒ Object
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 107 108 109 110 111 112 113 114 |
# File 'lib/dokuen/application.rb', line 75 def deploy(revision) git_dir = Dir.getwd with_app_dir do now = Time.now().utc().strftime("%Y%m%dT%H%M%S") release_dir = "releases/#{now}" clone_dir = clone(git_dir, revision) buildpack = get_env('BUILDPACK_URL') if buildpack buildpack = "-b #{buildpack}" else buildpack = '' end sys("mason build #{clone_dir} #{buildpack} -o #{release_dir} -c build") Dir.mkdir("#{release_dir}/.dokuen") hook = get_env('DOKUEN_AFTER_BUILD') if hook sys("foreman run #{hook}") end if File.symlink?("previous") File.unlink("previous") File.symlink(File.readlink("current"), "previous") end if File.symlink?("current") File.unlink("current") end File.symlink(File.(release_dir), "current") end scale if File.symlink?("previous") shutdown(File.readlink("previous")) end end |
#env ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dokuen/application.rb', line 40 def env vars = read_env_dir("#{config.dokuen_dir}/env") with_app_dir do vars.merge!(read_env_dir('env')) with_current_release do File.open(".env") do |f| f.lines.each do |line| key, val = line.split('=', 2) vars[key] = val.chomp end end end end vars end |
#get_env(var) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/dokuen/application.rb', line 14 def get_env(var) with_app_dir do env_path = File.join("env", var) if File.exists?(env_path) return File.read(env_path) else return nil end end end |
#install_nginx_config ⇒ Object
204 205 206 207 208 209 210 211 212 213 |
# File 'lib/dokuen/application.rb', line 204 def install_nginx_config puts "Installing nginx config" sleep 2 conf = Dokuen.template('nginx', binding) File.open(File.join(config.dokuen_dir, "nginx", "#{name}.#{config.base_domain_name}.conf"), "w+") do |f| f.write(conf) end sys("sudo #{config.bin_path}/dokuen_restart_nginx") end |
#restart ⇒ Object
194 195 196 197 198 199 200 201 202 |
# File 'lib/dokuen/application.rb', line 194 def restart with_current_release do running_processes.each do |proc, pidfile| pid = YAML.load(File.read(pidfile))['pid'] puts "Sending USR2 to pid #{pid}" Process.kill("USR2", pid) end end end |
#run_command(args) ⇒ Object
215 216 217 218 219 220 221 222 |
# File 'lib/dokuen/application.rb', line 215 def run_command(args) with_current_release do env.each do |k,v| ENV[k] = v end sys("#{config.bin_path}/foreman run #{args.join(" ")}") end end |
#scale ⇒ Object
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/dokuen/application.rb', line 116 def scale puts "Scaling..." with_current_release do processes = running_processes running_count_by_name = {} processes.each do |proc, pidfile| proc_name = proc.split('.')[0] running_count_by_name[proc_name] ||= 0 running_count_by_name[proc_name] += 1 end desired_count_by_name = {} scale_spec = get_env('DOKUEN_SCALE') if scale_spec scale_spec.split(',').each do |spec| proc_name, count = spec.split('=') desired_count_by_name[proc_name] = count.to_i end end to_start = [] to_stop = [] desired_count_by_name.each do |proc_name, count| running = running_count_by_name[proc_name] || 0 if running < count (count - running).times do |i| index = running + i + 1 to_start << [proc_name, index] end elsif running > count (running - count).times do |i| index = count + i + 1 to_stop << [proc_name, index] end end end running_count_by_name.each do |proc_name, count| if not desired_count_by_name.has_key?(proc_name) count.times do |i| to_stop << [proc_name, i] end end end to_start.each do |proc_name, index| port = reserve_port fork do Dokuen::Wrapper.new(self, proc_name, index, File.join(config.dokuen_dir, 'ports', port.to_s)).run! end end to_stop.each do |proc_name, index| pid_file = processes["#{proc_name}.#{index}"] pid = YAML.load(File.read(pid_file))['pid'] Process.kill("TERM", pid) end install_nginx_config end end |
#set_env(var, val) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/dokuen/application.rb', line 25 def set_env(var, val) with_app_dir do env_path = File.join("env", var) File.open(env_path, "w+") do |f| f.write(val) end end end |
#shutdown(release = nil) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/dokuen/application.rb', line 179 def shutdown(release=nil) if release.nil? with_app_dir do release = File.readlink("current") end end with_release_dir(release) do running_processes.each do |proc, pidfile| pid = YAML.load(File.read(pidfile))['pid'] Process.kill("TERM", pid) end end end |