Class: Appserver::ServerDir

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

Constant Summary collapse

CONFIG_FILE_NAME =
'appserver.conf.rb'
SETTINGS_DEFAULTS =
{
  :monit_conf => 'monitrc',
  :monit_reload => '/usr/sbin/monit reload',
  :nginx_conf => 'nginx.conf',
  :nginx_reload => '/usr/sbin/nginx -s reload',
  :nginx_reopen => '/usr/sbin/nginx -s reopen',
  :logrotate_conf => 'logrotate.conf',
}
SETTINGS_EXPAND =
[ :monit_conf, :nginx_conf, :logrotate_conf ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ ServerDir

Returns a new instance of ServerDir.



46
47
48
49
50
51
52
53
# File 'lib/appserver/server_dir.rb', line 46

def initialize (path, options = {})
  self.path = File.expand_path(path)
  # Load and apply configuration settings
  app_keys = App::SETTINGS_DEFAULTS.keys
  global_keys = SETTINGS_DEFAULTS.keys + App::SETTINGS_DEFAULTS.keys
  @config = Configurator.new(File.exist?(config_file) ? config_file : nil, global_keys, app_keys)
  @config.apply!(self)
end

Instance Attribute Details

#logrotate_confObject

Returns the value of attribute logrotate_conf

Returns:

  • (Object)

    the current value of logrotate_conf



7
8
9
# File 'lib/appserver/server_dir.rb', line 7

def logrotate_conf
  @logrotate_conf
end

#monit_confObject

Returns the value of attribute monit_conf

Returns:

  • (Object)

    the current value of monit_conf



7
8
9
# File 'lib/appserver/server_dir.rb', line 7

def monit_conf
  @monit_conf
end

#monit_reloadObject

Returns the value of attribute monit_reload

Returns:

  • (Object)

    the current value of monit_reload



7
8
9
# File 'lib/appserver/server_dir.rb', line 7

def monit_reload
  @monit_reload
end

#nginx_confObject

Returns the value of attribute nginx_conf

Returns:

  • (Object)

    the current value of nginx_conf



7
8
9
# File 'lib/appserver/server_dir.rb', line 7

def nginx_conf
  @nginx_conf
end

#nginx_reloadObject

Returns the value of attribute nginx_reload

Returns:

  • (Object)

    the current value of nginx_reload



7
8
9
# File 'lib/appserver/server_dir.rb', line 7

def nginx_reload
  @nginx_reload
end

#nginx_reopenObject

Returns the value of attribute nginx_reopen

Returns:

  • (Object)

    the current value of nginx_reopen



7
8
9
# File 'lib/appserver/server_dir.rb', line 7

def nginx_reopen
  @nginx_reopen
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



7
8
9
# File 'lib/appserver/server_dir.rb', line 7

def path
  @path
end

Class Method Details

.config_file_templateObject



22
23
24
# File 'lib/appserver/server_dir.rb', line 22

def self.config_file_template
  File.expand_path("../#{CONFIG_FILE_NAME}", __FILE__)
end

.discover(path = '.', options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/appserver/server_dir.rb', line 26

def self.discover (path = '.', options = {})
  if File.exist?(File.join(path, CONFIG_FILE_NAME))
    new(path, options)
  elsif path != '/'
    discover(File.expand_path('..', path), options)
  else
    raise NotInitializedError
  end
end

.init(path, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/appserver/server_dir.rb', line 36

def self.init (path, options = {})
  raise DirectoryAlreadyExistError if File.exist?(path) && !options[:force]
  FileUtils.mkdir_p path
  Dir.chdir(path) do
    FileUtils.cp config_file_template, CONFIG_FILE_NAME
    FileUtils.mkdir_p ['apps', 'tmp', 'log']
  end
  new(path, options)
end

Instance Method Details

#app(name) ⇒ Object



76
77
78
79
# File 'lib/appserver/server_dir.rb', line 76

def app (name)
  @apps ||= {}
  @apps[name] ||= App.new(self, name, @config)
end

#appsObject



81
82
83
84
85
86
87
# File 'lib/appserver/server_dir.rb', line 81

def apps
  Dir.glob(File.join(apps_path, '*')).
    select { |f| File.directory?(f) }.
    map { |f| File.basename(f) }.
    reject { |f| f =~ /\.(tmp|old|new)$/ }.
    map { |name| app(name) }
end

#apps_pathObject



64
65
66
# File 'lib/appserver/server_dir.rb', line 64

def apps_path
  File.join(path, 'apps')
end

#appserver_cmd(*args) ⇒ Object



59
60
61
62
# File 'lib/appserver/server_dir.rb', line 59

def appserver_cmd (*args)
  cmd = File.expand_path('../../../bin/appserver', __FILE__)
  "#{cmd} -d #{path} #{args.join(' ')}"
end

#config_fileObject



55
56
57
# File 'lib/appserver/server_dir.rb', line 55

def config_file
  File.join(path, CONFIG_FILE_NAME)
end

#log_pathObject



72
73
74
# File 'lib/appserver/server_dir.rb', line 72

def log_path
  File.join(path, 'log')
end

#repository(path) ⇒ Object



89
90
91
92
# File 'lib/appserver/server_dir.rb', line 89

def repository (path)
  @repositories ||= {}
  @repositories[File.expand_path(path, self.path)] ||= Repository.new(self, path, @config)
end

#tmp_pathObject



68
69
70
# File 'lib/appserver/server_dir.rb', line 68

def tmp_path
  File.join(path, 'tmp')
end

#write_configsObject



94
95
96
97
98
# File 'lib/appserver/server_dir.rb', line 94

def write_configs
  Monit.write_config(self)
  Nginx.write_config(self)
  Logrotate.write_config(self)
end