tlopo-selenium-docker 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/lib/tlopo/selenium_docker/version.rb +7 -0
- data/lib/tlopo/selenium_docker.rb +200 -0
- data/lib/tlopo-selenium-docker.rb +3 -0
- metadata +51 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e44606ee96d7d1dd5c3be75a0d6c15783d45a76e6b8ff6c5d348cba082f49039
|
4
|
+
data.tar.gz: efd32c53c7cc27ed02e59a49adbe20311f1fc9b6633beab61db0511ce166ba36
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8279b6e826b5bbac0561c07eec38c31f9c6214c435c02fb4c26b4afde289c12f459e602b424ec141d6bba4f5d5cf27c7fdc52f047840a89bcbbcd87c546ac616
|
7
|
+
data.tar.gz: cd2b86ebbb72a22fbcc9389d82470fc60bb57ff9760d730eb6f30add1875d5589617a0c3944193da00b1ee3ce25d130b4bc6bda3ff5ebe8fa4c801e6eabe76d3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 tlopo
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Tlopo::Selenium::Docker
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tlopo/selenium/docker`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Development
|
24
|
+
|
25
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
26
|
+
|
27
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tlopo-selenium-docker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/tlopo-selenium-docker/blob/main/CODE_OF_CONDUCT.md).
|
32
|
+
|
33
|
+
## License
|
34
|
+
|
35
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
36
|
+
|
37
|
+
## Code of Conduct
|
38
|
+
|
39
|
+
Everyone interacting in the Tlopo::Selenium::Docker project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/tlopo-selenium-docker/blob/main/CODE_OF_CONDUCT.md).
|
@@ -0,0 +1,200 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "selenium_docker/version"
|
4
|
+
require "zlib"
|
5
|
+
require "fileutils"
|
6
|
+
require "timeout"
|
7
|
+
require "socket"
|
8
|
+
require "docker"
|
9
|
+
require "tlopo/futex"
|
10
|
+
require "logger"
|
11
|
+
require "selenium-webdriver"
|
12
|
+
|
13
|
+
module Tlopo
|
14
|
+
class SeleniumDocker
|
15
|
+
LOGGER ||= Logger.new $stderr
|
16
|
+
|
17
|
+
def initialize(opts = {})
|
18
|
+
@selenium_image = opts[:selenium_image] || "selenium/standalone-chrome:4.8.3"
|
19
|
+
@video_image = opts[:video_image] || "selenium/video:ffmpeg-4.3.1-20220726"
|
20
|
+
@chrome_data_dir = opts[:chrome_data_dir] || nil
|
21
|
+
@video_path = opts[:video_path] || nil
|
22
|
+
@name = "selenium-#{gen_short_id}"
|
23
|
+
@ws = "/tmp/#{@name}"
|
24
|
+
FileUtils.mkdir_p @ws
|
25
|
+
FileUtils.mkdir_p @chrome_data_dir if @chrome_data_dir
|
26
|
+
end
|
27
|
+
|
28
|
+
def run
|
29
|
+
start
|
30
|
+
yield driver
|
31
|
+
ensure
|
32
|
+
driver.quit
|
33
|
+
stop
|
34
|
+
end
|
35
|
+
|
36
|
+
def gen_short_id
|
37
|
+
Zlib.crc32(Time.now.strftime("%s.%N")).to_s(36)
|
38
|
+
end
|
39
|
+
|
40
|
+
def get_port_lock(port)
|
41
|
+
filename = "/tmp/.selenium-docker-#{port}"
|
42
|
+
f = Tlopo::Futex.new(filename)
|
43
|
+
f.lock
|
44
|
+
f
|
45
|
+
end
|
46
|
+
|
47
|
+
def start
|
48
|
+
pull_images
|
49
|
+
lock_chrome_data_dir if @chrome_data_dir
|
50
|
+
@port = find_free_port 8100, 8150
|
51
|
+
@port_lock = get_port_lock @port
|
52
|
+
@vnc_port = find_free_port 5900, 5950
|
53
|
+
@vnc_port_lock = get_port_lock @vnc_port
|
54
|
+
create_network
|
55
|
+
start_selenium
|
56
|
+
start_video if @video_path
|
57
|
+
end
|
58
|
+
|
59
|
+
def stop
|
60
|
+
stop_video if @video_path
|
61
|
+
stop_selenium
|
62
|
+
remove_network
|
63
|
+
@port_lock.release
|
64
|
+
@vnc_port_lock.release
|
65
|
+
@chrome_data_dir_lock&.release
|
66
|
+
copy_video
|
67
|
+
FileUtils.rm_rf @ws
|
68
|
+
end
|
69
|
+
|
70
|
+
def copy_video
|
71
|
+
return unless @video_path
|
72
|
+
|
73
|
+
path = @video_path =~ /\.mp4$/ ? @video_path : "#{@video_path}.mp4"
|
74
|
+
FileUtils.cp "#{@ws}/video/video.mp4", path
|
75
|
+
LOGGER.debug "Recorded video saved to '#{path}''"
|
76
|
+
end
|
77
|
+
|
78
|
+
def find_free_port(from, to)
|
79
|
+
(from..to).each do |port|
|
80
|
+
next if Tlopo::Futex.new("/tmp/.selenium-docker-#{port}").locked?
|
81
|
+
|
82
|
+
Timeout.timeout(1) { TCPSocket.new("127.0.0.1", port).close }
|
83
|
+
rescue Errno::ECONNREFUSED
|
84
|
+
return port
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def pull_images
|
89
|
+
[@selenium_image, @video_image].each do |image|
|
90
|
+
unless Docker::Image.exist? image
|
91
|
+
LOGGER.debug "Pulling image #{image}"
|
92
|
+
Docker::Image.create("fromImage" => image)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def create_network
|
98
|
+
network_name = @name.to_s
|
99
|
+
return if Docker::Network.all.map(&:info).any? { |n| n["Name"] == network_name }
|
100
|
+
|
101
|
+
LOGGER.debug "Creating network #{network_name}"
|
102
|
+
Docker::Network.create network_name
|
103
|
+
end
|
104
|
+
|
105
|
+
def remove_network
|
106
|
+
network_name = @name.to_s
|
107
|
+
return unless Docker::Network.all.map(&:info).any? { |n| n["Name"] == network_name }
|
108
|
+
|
109
|
+
LOGGER.debug "Removing network #{network_name}"
|
110
|
+
Docker::Network.get(network_name).remove
|
111
|
+
end
|
112
|
+
|
113
|
+
def get_container(container_name)
|
114
|
+
Docker::Container.get(container_name)
|
115
|
+
rescue Docker::Error::NotFoundError
|
116
|
+
nil
|
117
|
+
end
|
118
|
+
|
119
|
+
def stop_container(container_name, timeout = 30)
|
120
|
+
c = get_container(container_name)
|
121
|
+
return if c.nil?
|
122
|
+
|
123
|
+
LOGGER.debug "Stopping container '#{container_name}'"
|
124
|
+
c.stop t: timeout
|
125
|
+
end
|
126
|
+
|
127
|
+
def lock_chrome_data_dir
|
128
|
+
@chrome_data_dir_lock = Tlopo::Futex.new("#{@chrome_data_dir}/.chrome_data_dir.lock")
|
129
|
+
@chrome_data_dir_lock.lock
|
130
|
+
end
|
131
|
+
|
132
|
+
def start_selenium
|
133
|
+
ws = @chrome_data_dir || "/#{@ws}/chrome-data-dir"
|
134
|
+
LOGGER.debug "Starting selenium, container name: #{@name}, vnc port: #{@vnc_port}"
|
135
|
+
Docker::Container.create(
|
136
|
+
Image: @selenium_image,
|
137
|
+
name: @name.to_s,
|
138
|
+
HostConfig: {
|
139
|
+
NetworkMode: @name.to_s,
|
140
|
+
AutoRemove: true,
|
141
|
+
PortBindings: {
|
142
|
+
"4444/tcp": [{ HostPort: @port.to_s }],
|
143
|
+
"5900/tcp": [{ HostPort: @vnc_port.to_s }]
|
144
|
+
},
|
145
|
+
"Binds" => ["#{ws}:/tmp/chrome-data-dir"],
|
146
|
+
"ShmSize" => 2 * 1024 * 1024 * 1024 # 2 GB in bytes
|
147
|
+
},
|
148
|
+
Env: ["VIDEO=true"]
|
149
|
+
).start
|
150
|
+
end
|
151
|
+
|
152
|
+
def stop_selenium
|
153
|
+
stop_container @name.to_s
|
154
|
+
end
|
155
|
+
|
156
|
+
def start_video
|
157
|
+
LOGGER.debug "Starting video, container name: #{@name}-video"
|
158
|
+
Docker::Container.create(
|
159
|
+
Image: @video_image,
|
160
|
+
name: "#{@name}-video",
|
161
|
+
HostConfig: {
|
162
|
+
NetworkMode: @name.to_s,
|
163
|
+
AutoRemove: true,
|
164
|
+
"Binds" => ["#{@ws}/video:/videos"]
|
165
|
+
},
|
166
|
+
Env: ["DISPLAY_CONTAINER_NAME=#{@name}"]
|
167
|
+
).start
|
168
|
+
end
|
169
|
+
|
170
|
+
def stop_video
|
171
|
+
stop_container "#{@name}-video"
|
172
|
+
end
|
173
|
+
|
174
|
+
def driver
|
175
|
+
return @driver unless @driver.nil?
|
176
|
+
|
177
|
+
Timeout.timeout(60) do
|
178
|
+
loop do
|
179
|
+
TCPSocket.new("localhost", @port).close
|
180
|
+
url = "http://localhost:#{@port}/wd/hub"
|
181
|
+
|
182
|
+
options = Selenium::WebDriver::Chrome::Options.new
|
183
|
+
options.add_argument("--no-first-run")
|
184
|
+
options.add_argument("--user-data-dir=/tmp/chrome-data-dir")
|
185
|
+
|
186
|
+
@driver = Selenium::WebDriver.for :remote, url: url, capabilities: options
|
187
|
+
LOGGER.debug "driver created for #{@name}"
|
188
|
+
break
|
189
|
+
rescue Errno::ECONNREFUSED
|
190
|
+
sleep 0.5
|
191
|
+
rescue EOFError
|
192
|
+
sleep 0.5
|
193
|
+
rescue Selenium::WebDriver::Error::UnknownError
|
194
|
+
sleep 0.5
|
195
|
+
end
|
196
|
+
end
|
197
|
+
@driver
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tlopo-selenium-docker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tlopo
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-10-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Manges Selenium Docker container lifecycle and provides a handle to Selenium
|
14
|
+
driver
|
15
|
+
email:
|
16
|
+
- tlopo@github.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- LICENSE.txt
|
22
|
+
- README.md
|
23
|
+
- lib/tlopo-selenium-docker.rb
|
24
|
+
- lib/tlopo/selenium_docker.rb
|
25
|
+
- lib/tlopo/selenium_docker/version.rb
|
26
|
+
homepage: https://github.com/tlopo-ruby/tlopo-selenium-docker
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata:
|
30
|
+
homepage_uri: https://github.com/tlopo-ruby/tlopo-selenium-docker
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 2.6.0
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubygems_version: 3.5.3
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Manges Selenium Docker container lifecycle and provides a handle to Selenium
|
50
|
+
driver
|
51
|
+
test_files: []
|