Class: Nova::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/nova/project.rb

Overview

A Nova project, containing the galaxy and configuration settings for that project.

API:

  • public

Constant Summary collapse

DEFAULT_PATHS =

The default paths to load from.

API:

  • public

[File.absolute_path("../../../galaxy", __FILE__)]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, load_config = true) ⇒ Project

Initializes the project. Loads the configuration file by default.

Parameters:

  • the path to the directory for the project.

  • (defaults to: true)

    whether or not to load the configuration file.

API:

  • public



42
43
44
45
46
47
48
49
50
# File 'lib/nova/project.rb', line 42

def initialize(dir, load_config = true)
  @directory = Dir.new(dir)
  @load_paths = DEFAULT_PATHS.dup
  @options   = {}

  if load_config
    load_config!
  end
end

Instance Attribute Details

#directoryDirectory (readonly)

The directory the project is based in.

Returns:

API:

  • public



24
25
26
# File 'lib/nova/project.rb', line 24

def directory
  @directory
end

#load_pathsArray<String> (readonly)

The load paths for this project.

Returns:

API:

  • public



29
30
31
# File 'lib/nova/project.rb', line 29

def load_paths
  @load_paths
end

#optionsHash (readonly)

The options that were loaded from the config for this project.

Returns:

API:

  • public



34
35
36
# File 'lib/nova/project.rb', line 34

def options
  @options
end

Class Method Details

.valid?(dir) ⇒ Boolean

Whether or not the given directory is deemable as a Nova project.

Parameters:

  • the directory to test.

Returns:

API:

  • public



17
18
19
# File 'lib/nova/project.rb', line 17

def self.valid?(dir)
  Dir.new(dir).each.include?("nova.yml")
end

Instance Method Details

#load_config!Hash

Loads the configuration file.

Returns:

  • the data.

API:

  • public



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nova/project.rb', line 56

def load_config!
  return unless options.empty?

  data = ::YAML.load_file(File.open("#{directory.path}/nova.yml", "r"))
  load_paths.push(*data.fetch("load_paths", []))

  load_paths.map! do |path|
    File.absolute_path(path, directory.path)
  end

  @options = data
end

#require_filesvoid

This method returns an undefined value.

Requires all of the star files that is in the project.

API:

  • public



72
73
74
75
76
77
78
# File 'lib/nova/project.rb', line 72

def require_files
  @load_paths.each do |path|
    Dir["#{path}/**/*"].each do |f|
      require f
    end
  end
end

#run_servers(do_fork = true, which = []) ⇒ void

Note:

If do_fork is false, only the first server in the config file will actually be created.

This method returns an undefined value.

Runs the servers defined in the options.

Parameters:

  • (defaults to: true)

    whether or not to actually fork the process when creating servers.

  • (defaults to: [])

    which servers to run. Defaults to all of them.

API:

  • public



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/nova/project.rb', line 89

def run_servers(do_fork = true, which = [])
  each_server(which) do |server, name|
    puts name

    if File.exists?(server[:files][:pid])
      Nova.logger.warn {
        "PID file #{server[:files][:pid]} already exists. " +
        "Ignoring server definition."
      }
      next
    end

    if do_fork
      process_id = fork
    end

    if process_id
      File.open(server[:files][:pid], "w") { |f| f.write process_id }
      Process.detach(process_id)
    else
      return build_server(server, do_fork)
    end
  end
end

#shoot(which = []) ⇒ void

This method returns an undefined value.

Takes down running servers.

Parameters:

  • (defaults to: [])

    which servers to take down. Defaults to all of them.

API:

  • public



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/nova/project.rb', line 119

def shoot(which = [])
  each_server do |server, name|
    if File.exists?(server[:files][:pid])
      pid = File.open(server[:files][:pid], "r") { |f| f.read }.to_i

      print "Sending INT to #{pid}... "

      Process.kill :INT, pid rescue Errno::ESRCH

      File.delete(server[:files][:pid]) rescue Errno::ENOENT

      puts "OK!"
    end
  end
end