Class: Circus::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/circus/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, name = nil) ⇒ Application

Returns a new instance of Application.



8
9
10
11
12
# File 'lib/circus/application.rb', line 8

def initialize(dir, name = nil)
  @dir = dir
  @name = name || File.basename(dir)
  @acts = nil
end

Instance Attribute Details

#actsObject (readonly)

Returns the value of attribute acts.



6
7
8
# File 'lib/circus/application.rb', line 6

def acts
  @acts
end

Instance Method Details

#acts_fileObject



138
139
140
# File 'lib/circus/application.rb', line 138

def acts_file
  File.join(@dir, 'acts.yaml')
end

#assemble!(output_dir, logger, dev = false, only_acts = nil) ⇒ Object

Instructs the application to assemble it’s components for deployment and generate act output files.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/circus/application.rb', line 89

def assemble!(output_dir, logger, dev = false, only_acts = nil)
  load! unless @acts

  if dev
    acts.each do |act|
      logger.info "Assembling development act #{act.name} at #{act.dir}"
      act.package_for_dev(logger, private_run_root)
    end
    return true
  end
  
  assembly_acts = acts.select {|a| only_acts.nil? or only_acts.include? a.name }
  
  FileUtils.rm_rf(output_dir)
  FileUtils.rm_rf(private_overlay_root)
  FileUtils.mkdir_p(output_dir)
  assembly_acts.each do |act|
    act.detect!
    if act.should_package?
      logger.info "Assembling act #{act.name} at #{act.dir} using profile #{act.profile.name}"
    end
  end
  logger.info "---------------------"
  assembly_acts.each do |act|
    if act.should_package?
      return false unless act.assemble(logger, output_dir, private_overlay_root)
    end
  end
  
  true
end

#go!(logger) ⇒ Object

Instructs the application to startup in place and activate all of its associated acts.



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
# File 'lib/circus/application.rb', line 31

def go!(logger)
  # Ensure that we have svscan available. If we don't, die nice and early
  `which svscan`
  if $? != 0
    logger.error 'The svscan utility (usually provided by daemontools) is not available. ' +
      ' This utility must be installed before the circus go command can function.'
    # TODO: Detect OS and suggest installation mechanism?
      
    return
  end
  
  load! unless @acts

  run_acts = acts.select { |a| a.profile.supported_for_development? }
  run_acts.each do |act|
    logger.info "Starting act #{act.name} at #{act.dir} using profile #{act.profile.name}"
  end
  logger.info "---------------------"
  FileUtils.rm_rf(private_run_root)
  run_acts.each do |act|
    return unless act.package_for_dev(logger, private_run_root)
  end
  
  # If we've loaded bundler ourselves, then we need to remove its environment variables; otherwise,
  # it will screw up child applications!
  ENV['BUNDLE_GEMFILE'] = nil
  ENV['BUNDLE_BIN_PATH'] = nil
  system("svscan #{private_run_root}")
end

#load!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/circus/application.rb', line 14

def load!
  @acts = if File.exists?(acts_file)
    acts_cfg = YAML.load(File.read(acts_file))
    acts_cfg.map do |name, props|
        # If no props are specified in the YAML, this could be nil. Default it.
      props ||= {}
      
      act_dir = File.join(@dir, props['dir'] || name)
      Act.new(name, act_dir, props)
    end
  else
    [Act.new(@name, @dir)]
  end
end

#pause(logger, act_name) ⇒ Object

Instructs the application to stop the given act that is running under development via go.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/circus/application.rb', line 62

def pause(logger, act_name)
  load! unless @acts
  
  target_act = acts.find { |a| a.name == act_name }
  unless target_act
    logger.error "Act #{act_name} could not be found"
    return
  end
  
  target_act.pause(logger, private_run_root)
end

#private_overlay_rootObject



134
135
136
# File 'lib/circus/application.rb', line 134

def private_overlay_root
  File.join(@dir, '.circus', 'overlays')
end

#private_run_rootObject



130
131
132
# File 'lib/circus/application.rb', line 130

def private_run_root
  File.join(@dir, '.circus', 'run-dev')
end

#resume(logger, act_name) ⇒ Object

Instructs the application to resume the given act that is running under development via go.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/circus/application.rb', line 75

def resume(logger, act_name)
  load! unless @acts
  
  target_act = acts.find { |a| a.name == act_name }
  unless target_act
    logger.error "Act #{act_name} could not be found"
    return
  end
  
  target_act.resume(logger, private_run_root)
end

#upload(output_dir, act_store, only_acts = nil) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/circus/application.rb', line 121

def upload(output_dir, act_store, only_acts = nil)
  load! unless @acts
  upload_acts = acts.select {|a| only_acts.nil? or only_acts.include? a.name }
  
  upload_acts.each do |act|
    act.upload(output_dir, act_store)
  end
end