Class: Greenhouse::Commands::Launch

Inherits:
Object
  • Object
show all
Includes:
Command
Defined in:
lib/greenhouse/commands/launch.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Command

included

Class Method Details

.usageObject



12
13
14
15
16
17
18
19
# File 'lib/greenhouse/commands/launch.rb', line 12

def usage
  puts <<USAGE
usage: #{::Greenhouse::CLI.command_name} #{command_name} <application> [<process> [<process> [...]]]

Applications:
#{project_arguments.to_help}
USAGE
end

Instance Method Details

#quit(signo) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/greenhouse/commands/launch.rb', line 101

def quit(signo)
  @procs.each do |key,pid|
    begin
      Process.kill("KILL", pid)
    rescue # make sure to kill all children
    end
  end
  exit signo
end

#runObject



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
57
58
59
60
61
62
63
64
65
66
67
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
# File 'lib/greenhouse/commands/launch.rb', line 22

def run
  if arguments.empty?
    puts "Please provide the application you'd like to launch."
    puts
    usage
    return
  end

  appname = arguments.slice!(0).key
  app = Projects.applications.select { |app| app.name == appname }.first
  
  if app.nil?
    if Projects.projects.select { |proj| proj.name == appname }.first.nil?
      puts "The application '#{appname}' does not exist."
    else
      puts "The project '#{appname}' is not defined as an application."
    end
    puts
    usage
    return
  end

  if !app.exists?
    puts "The application directory #{app.path} does not exist. Are you sure you've initialized your ecosystem?"
    puts
    usage
    return
  end

  Signal.trap("HUP") { |signo| quit(signo) }
  Signal.trap("INT") { |signo| quit(signo) }
  Signal.trap("KILL") { |signo| quit(signo) }
  Signal.trap("TERM") { |signo| quit(signo) }
  
  begin
    @procs = {}
    Bundler.with_clean_env do
      app.chdir do
        if app.dotenv.exists?
          begin
            # We have to unset these because foreman sets them by default, and Dotenv won't override a set value
            %w(PORT RACK_ENV RAILS_ENV).each { |key| ENV[key] = nil }
            require 'dotenv'
            Dotenv.load!
          rescue
            puts "Error parsing .env file for #{app.title.cyan}.".red
            exit 1
          end
        else
          puts "Warning: No .env file found in #{app.title.cyan}!".yellow
          puts "Your application may not behave as expected without a .env file."
        end
        
        unless app.procfile.exists?
          puts "No Procfile found for #{app.title.cyan}".red
          exit 1
        end

        app.procfile.enabled.each do |key,process|
          next if arguments.length > 0 && !arguments.map(&:key).include?(key)
          @procs[key] = process
        end

        arguments.map(&:key).each { |key| puts "Skipping process not found in Procfile: #{key}" unless @procs.keys.include?(key) }
        @procs.each do |key,process|
          @procs[key] = fork do
            exec process.command
          end
        end
        
        Process.wait
      end
    end
  rescue
    quit 1
  end

end