-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute-firefox
executable file
·244 lines (192 loc) · 5.73 KB
/
execute-firefox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env ruby
require "fileutils"
require "optparse"
require "socket"
def host_addr
Socket.getifaddrs.
find {|f| f.name == "docker0" and f.addr.ip? and f.addr.ipv6? == false }.
addr.ip_address
end
DOWNLOADS_PREFIX = File.expand_path("~/.local/share/firefox-downloads")
dns_servers = "1.1.1.1"
share_x11 = false
share_downloads = true
shared_downloads_path = nil
shm_size = "2g"
profile_path = nil
profile_name = nil
profile_create = false
profile_template = "_default"
start_private = nil
volumes = []
env_vars = []
image_name = "firefox"
OptionParser.new do |opts|
opts.on "-x", "--[no-]share-x11", "Share X11 instance" do |v|
share_x11 = v
end
opts.on "-v", "--volume [VOLUME]", "Add more volumes to the container" do |v|
volumes << v
end
opts.on "--[no-]private", "Start in private mode" do |v|
start_private = v
end
opts.on "-e", "--env [VOLUME]", "Add more enviroment variables to the container" do |v|
env_vars << v
end
opts.on "-d", "--disable-share-downloads", "Disable shared downloads path" do |v|
share_downloads = !v
end
opts.on "-i", "--image [IMAGE]", "Docker image" do |v|
image_name = v
end
opts.on "-pPROFILE", "--profile=PATH", "Profile name" do |name|
profile_name = name
end
opts.on "-PPROFILE", "--new-profile=PATH", "Create a new profile" do |name|
profile_name = name
profile_create = true
end
opts.on "-tTEMPLATE", "--template=TEMPLATE", "Template to create new profile" do |template|
profile_template = template == "none" ? nil : template
end
opts.on "-DPATH", "--downloads=PATH", "Path shared by downloads" do |v|
shared_downloads_path = v
end
opts.on "-s", "--shm-size", "Set the size of /dev/shm of the container" do |v|
shm_size = v
end
opts.on "-DDNS", "--dns=DNS", "DNS servers" do |d|
dns_servers = d
end
end.parse!
def profile_path(name)
File.expand_path("~/.local/share/firefox-profiles/#{name}")
end
if profile_name
if profile_name !~ /\A(\w|-)+\Z/
STDERR.puts "Invalid profile name: #{profile_name}"
exit 1
end
profile_path = profile_path(profile_name)
shared_downloads_path = File.join(DOWNLOADS_PREFIX, profile_name)
else
profile_name = "tmp-" + Time.now.strftime("%Y%m%d%H%M%S")
profile_path = "/tmp/.firefox-profiles/#{profile_name}"
profile_create = true
start_private = true if start_private.nil?
end
container_name = "firefox-#{profile_name}"
puts "Container Name: \033[1m#{container_name}\033[m"
cmd = [ "docker", "run", "--name", container_name, "--pull=never" ]
cmd << "--shm-size" << shm_size
cmd << "--dns" << dns_servers
if dpi_scale = ENV["GDK_DPI_SCALE"]
cmd << "-e" << "GDK_DPI_SCALE=#{dpi_scale}"
end
if share_x11
if xdg_rt = ENV["XDG_RUNTIME_DIR"]
cmd << "-e" << "XDG_RUNTIME_DIR=#{xdg_rt}"
cmd << "-v" << "#{xdg_rt}:#{xdg_rt}"
end
if wl_display = ENV["WAYLAND_DISPLAY"]
cmd << "--rm"
cmd << "-e" << "MOZ_ENABLE_WAYLAND=1"
cmd << "-e" << "WAYLAND_DISPLAY=#{wl_display}"
else
cmd.concat %w(--rm -v /tmp/.X11-unix/:/tmp/.X11-unix -e)
cmd << "DISPLAY=#{ENV["DISPLAY"]}"
end
cmd << "--device=/dev/dri"
else
cmd << "-d" << "-e" << "DISPLAY=:0"
end
if not File.directory?(profile_path)
if profile_create
FileUtils.mkdir_p(profile_path)
if profile_template
%w(.mozilla .config).each do |dirname|
template_path = File.join(profile_path(profile_template), dirname)
if File.directory?(template_path)
STDERR.puts "[INFO] cp -r #{template_path} #{profile_path}"
FileUtils.cp_r template_path, profile_path, preserve: true
# Rename the Firefox profile to have a unique DBus name
profiles_ini = File.join(profile_path, ".mozilla/firefox/profiles.ini")
File.write(
profiles_ini,
File.read(profiles_ini).gsub(/^Name=.*?$/, "Name=#{profile_name}"))
else
STDERR.puts "[WARN] Missing template item #{template_path}"
end
end
end
else
STDERR.puts "[ERROR] Profile '#{profile_name}' does not exist. Create it with -P"
exit 1
end
end
cmd << "-v" << "#{profile_path}:/browser"
STDERR.puts "[INFO] Profile in #{profile_path}"
if share_downloads
shared_downloads_path ||= File.join(DOWNLOADS_PREFIX, "ANY")
if not File.directory?(shared_downloads_path)
FileUtils.mkdir_p shared_downloads_path
end
cmd << "-v" << "#{shared_downloads_path}:/browser/Downloads"
STDERR.puts "[INFO] Shared downloads in #{shared_downloads_path}"
end
env_vars.each do |env|
cmd << "-e" << env
end
volumes.each do |vol|
cmd << "-v" << vol
end
cmd << image_name
if share_x11
cmd.concat %w(firefox --new-instance)
cmd << "--Private" if start_private
else
cmd << "/usr/local/bin/browsersystem"
end
cmd.concat ARGV
puts "[INFO] #{cmd.join(" ")}"
output_log = "/tmp/.firefox-logs/#{container_name}"
FileUtils.mkdir_p(File.dirname(output_log))
if fork
STDERR.puts "Log to #{output_log}"
exit
end
STDIN.reopen("/dev/null", "r")
STDOUT.reopen(output_log, "a")
STDERR.reopen(output_log, "a")
if share_x11
exec(*cmd)
else
cid = IO.popen(cmd).read.strip
ip_address = IO.popen(["docker", "inspect", "-f", "{{.NetworkSettings.IPAddress}}", cid]).read.strip
puts "[INFO] Container #{cid} @ #{ip_address}"
vnc_pid = fork do
n = 0
while n < 1000
begin
TCPSocket.new(ip_address, 5900).close
break
rescue Errno::ECONNREFUSED
STDERR.print "\rWait VNC (#{n})\033[K"
STDERR.flush
sleep 0.1
n += 1
retry
end
end
exec "xtigervncviewer",
"-AcceptClipboard=0",
"-SetPrimary=0",
"-SendClipboard=0",
"-SendPrimary=0",
ip_address
end
system "docker", "wait", cid
system "docker", "rm", cid
Process.waitpid(vnc_pid)
end