Class: Appserver::Repository

Inherits:
Struct
  • Object
show all
Defined in:
lib/appserver/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_dir, path, config) ⇒ Repository

Returns a new instance of Repository.



10
11
12
13
# File 'lib/appserver/repository.rb', line 10

def initialize (server_dir, path, config)
  self.server_dir, self.path = server_dir, path.chomp('/')
  raise InvalidRepositoryError unless valid?
end

Instance Attribute Details

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



8
9
10
# File 'lib/appserver/repository.rb', line 8

def path
  @path
end

#server_dirObject

Returns the value of attribute server_dir

Returns:

  • (Object)

    the current value of server_dir



8
9
10
# File 'lib/appserver/repository.rb', line 8

def server_dir
  @server_dir
end

Instance Method Details

#appObject



19
20
21
22
# File 'lib/appserver/repository.rb', line 19

def app
  # The app for this repository (app of same name)
  @app ||= server_dir.app(name)
end

#deploy(ref = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/appserver/repository.rb', line 51

def deploy (ref = nil)
  # Choose a temporary build directory on the same filesystem so that it
  # can be easily renamed/moved to be the real application directory later
  build_path, old_path = "#{app.path}.new", "#{app.path}.old"
  begin
    # Check out the current code
    ref ||= app.branch
    checkout(build_path, ref)
    # Install gem bundle if a Gemfile exists
    install_bundle(build_path)

    # TODO: more deploy setup (write database config, ...)

    # Replace the current application directory with the newly built one
    FileUtils.rm_rf old_path
    FileUtils.mv app.path, old_path if File.exist?(app.path)
    FileUtils.mv build_path, app.path
  ensure
    # If anything broke and the build directory still exists, remove it
    FileUtils.rm_rf build_path
    # If anything broke and the app directory doesn't exist anymore, put the old directory in place
    FileUtils.mv old_path, app.path if !File.exist?(app.path) && File.exist?(old_path)
  end
end

#install_hookObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/appserver/repository.rb', line 34

def install_hook
  deploy_cmd = server_dir.appserver_cmd('deploy')
  if !File.exist?(update_hook) || !File.executable?(update_hook)
    puts "Installing git update hook to repository #{path}..."
    Utils.safe_replace_file(update_hook) do |f|
      f.puts '#!/bin/sh'
      f.puts "#{deploy_cmd} #{path} $1 $3"
      f.chown File.stat(path).uid, File.stat(path).gid
      f.chmod 0755
    end
  elsif !File.readlines(update_hook).any? { |line| line =~ /^#{Regexp.escape(deploy_cmd)}/ }
    puts "Couldn't install update hook. Foreign hook script already present in repository #{path}!"
  else
    #puts "Hook already installed in repository #{path}"
  end
end

#nameObject



15
16
17
# File 'lib/appserver/repository.rb', line 15

def name
  File.basename(path, '.git')
end

#update_hookObject



30
31
32
# File 'lib/appserver/repository.rb', line 30

def update_hook
  File.join(path, 'hooks', 'update')
end

#valid?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/appserver/repository.rb', line 24

def valid?
  name && name != '' &&
  File.directory?(File.join(path, 'hooks')) &&
  File.directory?(File.join(path, 'refs'))
end