Class: HDeploy::Client
- Inherits:
-
Object
- Object
- HDeploy::Client
- Defined in:
- lib/hdeploy/client.rb
Instance Method Summary collapse
- #check_deploy ⇒ Object
-
#find_executable(name) ⇒ Object
FIXME should be in some other path.
-
#initialize ⇒ Client
constructor
A new instance of Client.
-
#keepalive ⇒ Object
————————————————————————-.
- #put_state ⇒ Object
- #run_hook(hook, params) ⇒ Object
- #symlink(params) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/hdeploy/client.rb', line 10 def initialize @conf = HDeploy::Conf.instance #FIXME search for the configuration at the right place @conf.add_defaults({ 'client' => { 'keepalive_delay' => 60, 'check_deploy_delay' => 60, 'max_run_duration' => 3600, 'hostname' => `/bin/hostname`.chomp, } }) # Check for needed configuration parameters # API api_params = %w[http_user http_password endpoint] raise "#{@conf.file}: you need 'api' section for hdeploy client (#{api_params.join(', ')})" unless @conf['api'] api_params.each do |p| raise "#{@conf.file}: you need param for hdeploy client: api/#{p}" unless @conf['api'][p] end # Deploy raise "#{@conf.file}: you need 'deploy' section for hdeploy client" unless @conf['deploy'] @conf['deploy'].keys.each do |k| raise "#{@conf.file}: deploy key must be in the format app:env - found #{k}" unless k =~ /^[a-z0-9\-\_]+:[a-z0-9\-\_]+$/ end default_user = Process.uid == 0 ? 'www-data' : Process.uid default_group = Process.gid == 0 ? 'www-data' : Process.gid @conf['deploy'].each do |k,c| raise "#{@conf.file}: deploy section '#{k}': missing symlink param" unless c['symlink'] c['symlink'] = File.(c['symlink']) # FIXME: throw exception if user/group are root and/or don't exist { 'relpath' => File.('../releases', c['symlink']), 'tgzpath' => File.('../tarballs', c['symlink']), 'user' => default_user, 'group' => default_group, }.each do |k2,v| c[k2] ||= v end # It's not a mistake to check for uid in the gid section: only root can change gid. raise "You must run client as uid root if you want a different user for deploy #{k}" if Process.uid != 0 and c['user'] != Process.uid raise "You must run client as gid root if you want a different group for deploy #{k}" if Process.uid != 0 and c['group'] != Process.gid end end |
Instance Method Details
#check_deploy ⇒ Object
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/hdeploy/client.rb', line 127 def check_deploy put_state c = Curl::Easy.new() c.http_auth_types = :basic c.username = @conf['api']['http_user'] c.password = @conf['api']['http_password'] # Now this is the big stuff @conf['deploy'].each do |section,conf| app,env = section.split(':') #it's already checked for syntax higher in the code # Here we get the info. # FIXME: double check that config is ok relpath,tgzpath,symlink,user,group = conf.values_at('relpath','tgzpath','symlink','user','group') # Now the release info from the server c.url = @conf['api']['endpoint'] + '/distribute/' + app + '/' + env c.perform # prepare directories FileUtils.mkdir_p(relpath) FileUtils.mkdir_p(tgzpath) artifacts = JSON.parse(c.body_str) puts "found #{artifacts.keys.length} artifacts for #{app} / #{env}" dir_to_keep = [] tgz_to_keep = [] artifacts.each do |artifact,artdata| puts "checking artifact #{artifact}" destdir = File.join relpath,artifact tgzfile = File.join tgzpath,(artifact+'.tar.gz') readyfile = File.join destdir,'READY' if !(File.exists?readyfile) # we have to release. let's cleanup. FileUtils.rm_rf(destdir) if File.exists?(destdir) count = 0 while count < 5 and !(File.exists?tgzfile and Digest::MD5.file(tgzfile) == artdata['checksum']) count += 1 File.unlink tgzfile if File.exists?tgzfile # FIXME: add altsource and BREAK # FIXME: don't run download as root!! ##### if f = find_executable('aria2c') system("#{f} -x 5 -d #{tgzpath} -o #{artifact}.tar.gz #{artdata['source']}") elsif f = find_executable('wget') system("#{f} -o #{tgzfile} #{artdata['source']}") elsif f = find_executable('curl') system("#{f} -o #{tgzfile} #{artdata['source']}") else raise "no aria2c, wget or curl available. please install one of them." end end raise "unable to download artifact" unless File.exists?tgzfile raise "incorrect checksum for #{tgzfile}" unless Digest::MD5.file(tgzfile) == artdata['checksum'] FileUtils.mkdir_p destdir FileUtils.chown user, group, destdir Dir.chdir destdir chpst = '' if Process.uid == 0 chpst = find_executable('chpst') or raise "unable to find chpst binary" chpst += " -u #{user}:#{group} " end tar = find_executable('tar') system("#{chpst}#{tar} xzf #{tgzfile}") or raise "unable to extract #{tgzfile} as #{user}:#{group}" File.chmod 0755, destdir # Post distribute hook run_hook('post_distribute', {'app' => app, 'env' => env, 'artifact' => artifact}) FileUtils.touch(File.join(destdir,'READY')) #FIXME: root? end # we only get here if previous step worked. tgz_to_keep << File.(tgzfile) dir_to_keep << File.(destdir) end # check for symlink symlink({'app' => app,'env' => env, 'force' => false}) # cleanup if Dir.exists? conf['symlink'] dir_to_keep << File.(File.join(File.join(conf['symlink'],'..'),File.readlink(conf['symlink']))) end (Dir.glob(File.join conf['relpath'], '*') - dir_to_keep).each do |d| puts "cleanup dir #{d}" FileUtils.rm_rf d end (Dir.glob(File.join conf['tgzpath'],'*') - tgz_to_keep).each do |f| puts "cleanup file #{f}" File.unlink f end end put_state end |
#find_executable(name) ⇒ Object
FIXME should be in some other path
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/hdeploy/client.rb', line 106 def find_executable(name) #FIXME should be in some other path %w[ /opt/hdeploy/embedded/bin /opt/hdeploy/bin /usr/local/bin /usr/bin ].each do |p| e = File.join p,name next unless File.exists? e st = File.stat(e) next unless st.uid == 0 next unless st.gid == 0 if sprintf("%o", st.mode) == '100755' return e else warn "file #{file} does not have permissions 100755" end end return nil end |
#keepalive ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/hdeploy/client.rb', line 59 def keepalive hostname = @conf['client']['hostname'] c = Curl::Easy.new(@conf['api']['endpoint'] + '/srv/keepalive/' + hostname) c.http_auth_types = :basic c.username = @conf['api']['http_user'] c.password = @conf['api']['http_password'] c.put((@conf['client']['keepalive_delay'].to_i * 2).to_s) end |
#put_state ⇒ Object
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 |
# File 'lib/hdeploy/client.rb', line 68 def put_state hostname = @conf['client']['hostname'] c = Curl::Easy.new(@conf['api']['endpoint'] + '/distribute_state/' + hostname) c.http_auth_types = :basic c.username = @conf['api']['http_user'] c.password = @conf['api']['http_password'] r = [] # Will look at directories and figure out current state @conf['deploy'].each do |section,conf| app,env = section.split(':') relpath,tgzpath,symlink = conf.values_at('relpath','tgzpath','symlink') # could be done with ternary operator but I find it more readable like that. current = "unknown" if File.symlink? symlink and Dir.exists? symlink current = File.basename(File.readlink(symlink)) end # For artifacts, what we want is a directory, that contains the file "READY" artifacts = Dir.glob(File.join(relpath, '*', 'READY')).map{|x| File.basename(File.(File.join(x,'..'))) } r << { app: app, env: env, current: current, artifacts: artifacts.sort, } end puts JSON.pretty_generate(r) if ENV.has_key?'DEBUG' c.put(JSON.generate(r)) end |
#run_hook(hook, params) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/hdeploy/client.rb', line 237 def run_hook(hook,params) # This is a generic function to run the hooks defined in hdeploy.ini. # Standard hooks are app,env,artifact = params.values_at('app','env','artifact') oldpwd = Dir.pwd raise "no such app/env #{app} / #{env}" unless @conf['deploy'].has_key? "#{app}:#{env}" relpath,user,group = @conf['deploy']["#{app}:#{env}"].values_at('relpath','user','group') destdir = File.join relpath,artifact # It's OK if the file doesn't exist = File.join destdir, 'hdeploy.ini' return unless File.exists? # It's also OK if that hook doesn't exist hdc = IniFile.load()['hooks'] return unless hdc.has_key? hook hfile = hdc[hook] # But if it is defined, we're gonna scream if it's defined incorrectly. raise "no such file #{hfile} for hook #{hook}" unless File.exists? (File.join destdir,hfile) raise "non-executable file #{hfile} for hook #{hook}" unless File.executable? (File.join destdir,hfile) # OK let's run the hook Dir.chdir destdir chpst = '' if Process.uid == 0 chpst = find_executable('chpst') or raise "unable to find chpst binary" chpst += " -u #{user}:#{group} " end system("#{chpst}#{hfile} '#{JSON.generate(params)}'") if $?.success? puts "Successfully run #{hook} hook / #{hfile}" Dir.chdir oldpwd else Dir.chdir oldpwd raise "Error while running file #{hfile} hook #{hook} : #{$?} - (DEBUG: (pwd: #{destdir}): #{chpst}#{hfile} '#{JSON.generate(params)}'" end end |
#symlink(params) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/hdeploy/client.rb', line 283 def symlink(params) app,env = params.values_at('app','env') force = true if params.has_key? 'force' force = params['force'] end raise "no such app/env #{app} / #{env}" unless @conf['deploy'].has_key? "#{app}:#{env}" conf = @conf['deploy']["#{app}:#{env}"] link,relpath = conf.values_at('symlink','relpath') if force or !(File.exists?link) FileUtils.rm_rf(link) unless File.symlink?link c = Curl::Easy.new(@conf['api']['endpoint'] + '/target/' + app + '/' + env) c.http_auth_types = :basic c.username = @conf['api']['http_user'] c.password = @conf['api']['http_password'] c.perform target = c.body_str target_relative_path = Pathname.new(File.join relpath,target).relative_path_from(Pathname.new(File.join(link,'..'))) if File.symlink?(link) and (File.readlink(link) == target_relative_path) puts "symlink for app #{app} is already OK (#{target_relative_path})" else # atomic symlink override puts "setting symlink for app #{app} to #{target_relative_path}" File.symlink(target_relative_path,link + '.tmp') #FIXME: should this belong to root? File.rename(link + '.tmp', link) put_state end run_hook('post_symlink', {'app' => app, 'env' => env, 'artifact' => target}) else puts "not changing symlink for app #{app}" end end |