Class: Falcon::Controller::Virtual

Inherits:
Async::Container::Controller
  • Object
show all
Defined in:
lib/falcon/controller/virtual.rb

Instance Method Summary collapse

Constructor Details

#initialize(command, **options) ⇒ Virtual

Returns a new instance of Virtual.



28
29
30
31
32
33
34
# File 'lib/falcon/controller/virtual.rb', line 28

def initialize(command, **options)
	@command = command
	
	super(**options)
	
	trap(SIGHUP, &self.method(:reload))
end

Instance Method Details

#assume_privileges(path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/falcon/controller/virtual.rb', line 36

def assume_privileges(path)
	stat = File.stat(path)
	
	Process::GID.change_privilege(stat.gid)
	Process::UID.change_privilege(stat.uid)
	
	home = Etc.getpwuid(stat.uid).dir
	
	return {
		'HOME' => home,
	}
end

#falcon_pathObject



59
60
61
# File 'lib/falcon/controller/virtual.rb', line 59

def falcon_path
	File.expand_path("../../../bin/falcon", __dir__)
end

#setup(container) ⇒ Object



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
# File 'lib/falcon/controller/virtual.rb', line 63

def setup(container)
	if proxy = container[:proxy]
		proxy.kill(:HUP)
	end
	
	if redirect = container[:redirect]
		redirect.kill(:HUP)
	end
	
	container.reload do
		@command.resolved_paths do |path|
			path = File.expand_path(path)
			root = File.dirname(path)
			
			spawn(path, container, chdir: root)
		end
		
		container.spawn(name: "Falcon Redirector", restart: true, key: :redirect) do |instance|
			instance.exec(falcon_path, "redirect", "--bind", @command.bind_insecure, "--redirect", @command.bind_secure, *@command.paths, ready: false)
		end
		
		container.spawn(name: "Falcon Proxy", restart: true, key: :proxy) do |instance|
			instance.exec(falcon_path, "proxy", "--bind", @command.bind_secure, *@command.paths, ready: false)
		end
	end
end

#spawn(path, container, **options) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/falcon/controller/virtual.rb', line 49

def spawn(path, container, **options)
	container.spawn(name: "Falcon Application", restart: true, key: path) do |instance|
		env = assume_privileges(path)
		
		instance.exec(env,
			"bundle", "exec", "--keep-file-descriptors",
			path, ready: false, **options)
	end
end