wrata_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 557d84f0f762d728293064900d541e64bbac2f72cee30cbbc7453255f7f1dcc3
4
+ data.tar.gz: ab8a8885d4fe45746712d1614c9fc0b35676934eee88b9583fbe5550fbf48ddf
5
+ SHA512:
6
+ metadata.gz: 73a54b8332386382265bc660375cdeccb022a56d7cbc1b0898aa3447e11b792ad7e811ccb0b16cf4644a2eb690af56648b032e63b70d4e36a6c7b39b1f963dd0
7
+ data.tar.gz: 254b4c171a0d637a1c1ffc1d5013d98127212f4c6ff6fe727afaf94fd79687d692928682338330c573516e5d264ee54c08ef376296e2f16cd48d505ceffbc33a
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'wrata_api/wrata_api_request'
4
+ require_relative 'wrata_api/server_list'
5
+ require_relative 'wrata_api/server_methods'
6
+ require_relative 'wrata_api/file_tree'
7
+ require_relative 'wrata_api/queue_methos'
8
+ require 'json'
9
+ require 'logger'
10
+ require 'uri'
11
+ require 'yaml'
12
+ # Mail module of gem
13
+ module WrataApi
14
+ # Class for working with wrata api
15
+ class WrataApi
16
+ include WrataApiRequest
17
+ include ServerMethods
18
+ include FileListMethods
19
+ include QueueMethods
20
+
21
+ def initialize(config = nil)
22
+ config ||= YAML.load_file("#{ENV['HOME']}/.gem-wrata/config.yaml")
23
+ @uri = config['uri']
24
+ @cookie = config['cookie']
25
+ @wrata_session = config['wrata_session']
26
+ @csrf_token = config['csrf_token']
27
+ @logger = Logger.new(STDOUT)
28
+ @waiting_timeout = 5 * 60
29
+ @between_request_timeout = 10
30
+ end
31
+
32
+ # @return [True, False] check if service is running
33
+ def available?
34
+ uri = URI("#{@uri}/signin")
35
+ begin
36
+ source = Net::HTTP.get(uri)
37
+ rescue StandardError
38
+ source = ''
39
+ end
40
+ available = source.include?('Runner')
41
+ @logger.info("wrata_available?(#{@uri}): #{available}")
42
+ available
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WrataApi
4
+ # Methods to work with file tree
5
+ module FileListMethods
6
+ def file_list(project, ref = 'master')
7
+ uri = URI.parse("#{@uri}/runner/file_tree")
8
+ body = {
9
+ 'flatten' => true,
10
+ 'project' => project,
11
+ 'refs' => ref
12
+ }
13
+ perform_get(uri, body)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WrataApi
4
+ # This module holds the WrataApi version information.
5
+ module Name
6
+ # @return [String] current gem version
7
+ STRING = 'wrata_api'
8
+ end
9
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WrataApi
4
+ # Methods for working with queue
5
+ module QueueMethods
6
+ # Clear test in queue
7
+ # @return [Nothing]
8
+ def clear_tests_list
9
+ uri = URI.parse("#{@uri}/queue/clear_tests")
10
+ perform_post(uri)
11
+ end
12
+
13
+ # Get list of tests in queue
14
+ # @return [Array<Hash>] test list data
15
+ def tests_in_queue
16
+ uri = URI.parse("#{@uri}/runner/updated_data")
17
+ response = perform_get(uri)
18
+ response['queue_data']['tests']
19
+ end
20
+
21
+ # Add test to queue
22
+ # @param tests_to_add [String] path to add test
23
+ # @param options [Hash] string of some options
24
+ # @return [Nothing]
25
+ def add_to_queue(tests_to_add, options = {})
26
+ uri = URI.parse("#{@uri}/queue/add_test")
27
+
28
+ options = queue_item_default_values(options)
29
+ body = {
30
+ 'test_path' => tests_to_add,
31
+ 'branch' => options[:branch],
32
+ 'location' => options[:location],
33
+ 'spec_browser' => options[:browser]
34
+ }
35
+ perform_post(uri, body)
36
+ end
37
+
38
+ # Add default value to queue item
39
+ # @param options [Hash] values for add to queue
40
+ # @return [Hash] after adding default
41
+ def queue_item_default_values(options)
42
+ options[:branch] ||= 'develop'
43
+ options[:location] ||= 'info us'
44
+ options[:browser] ||= 'default'
45
+ options
46
+ end
47
+
48
+ # Add several tests to queue
49
+ # @param test_list [Array, String] tests to add
50
+ # @param options [Hash] option to each test
51
+ # @return [Nothing]
52
+ def add_tests_to_queue(test_list, options = {})
53
+ test_list.each do |current_test|
54
+ add_to_queue(current_test, options)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WrataApi
4
+ # Class for working with several servers at once
5
+ class ServerList
6
+ # @return [Array, Hash] list of servers
7
+ attr_accessor :servers
8
+
9
+ def initialize(api)
10
+ @api = api
11
+ @servers = []
12
+ @concurrent_operation_at_once = 5
13
+ end
14
+
15
+ # @return [Integer] count of servers
16
+ def length
17
+ @servers.length
18
+ end
19
+
20
+ # Turn on all servers in list
21
+ def concurrent_action
22
+ splitted_servers = @servers.each_slice(@concurrent_operation_at_once).to_a
23
+ splitted_servers.each do |each_slice|
24
+ threads = []
25
+ each_slice.each do |current_server|
26
+ threads << Thread.new(current_server) do
27
+ yield(current_server)
28
+ end
29
+ end
30
+ threads.each(&:join)
31
+ end
32
+ end
33
+
34
+ # Power on all servers
35
+ # @return [Nothing]
36
+ def power_on(size = nil)
37
+ concurrent_action do |server|
38
+ @api.power_on_server(server['name'], size)
39
+ end
40
+ end
41
+
42
+ # @return [True, False] is all servers powered on?
43
+ def powered_on?
44
+ @servers.each do |current|
45
+ return false unless @api.powering_status(current['name']) == :on
46
+ end
47
+ true
48
+ end
49
+
50
+ # Power on all servers
51
+ # @return [Nothing]
52
+ def power_off
53
+ concurrent_action do |server|
54
+ @api.power_off_server(server['name'])
55
+ end
56
+ end
57
+
58
+ # @return [True, False] is all servers powered on?
59
+ def powered_off?
60
+ @servers.each do |current|
61
+ return false unless @api.powering_status(current['name']) == :off
62
+ end
63
+ true
64
+ end
65
+
66
+ # Book each server
67
+ # @return [Nothing]
68
+ def book
69
+ @servers.each do |current|
70
+ @api.book_server(current['name'])
71
+ end
72
+ end
73
+
74
+ # Unbook each server
75
+ # @return [Nothing]
76
+ def unbook
77
+ @servers.each do |current|
78
+ @api.unbook_server(current['name'])
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'server_methods/server_methods_exceptions'
4
+ module WrataApi
5
+ # Methods for getting server status
6
+ module ServerMethods
7
+ # @return [Array, String] list of all servers
8
+ def servers
9
+ uri = URI.parse("#{@uri}/servers.json")
10
+ perform_get(uri)
11
+ end
12
+
13
+ # Got as much free servers as `count`
14
+ # @param count [Integer] how much servers to got
15
+ # @return [Array, Hash]
16
+ def free_servers(count)
17
+ free = ServerList.new(self)
18
+ raise NotEnoughServerCount if count > servers.length
19
+
20
+ servers.each do |single_server|
21
+ free.servers << single_server if single_server['book_client_id'].nil?
22
+ return free if free.length == count
23
+ end
24
+ end
25
+
26
+ # @return [Hash] Return servers data
27
+ def server_data(server_name)
28
+ uri = URI.parse("#{@uri}/runner/updated_data")
29
+ response = perform_get(uri, 'servers' => [{ name: server_name }].to_json)
30
+ response['servers_data'].first
31
+ end
32
+
33
+ # @return [Symbol] if server is powered on or off
34
+ def powering_status(server_name)
35
+ all_data = server_data(server_name)
36
+ status = all_data['_status']
37
+ if status == 'created'
38
+ :on
39
+ elsif status == 'destroyed'
40
+ :off
41
+ else
42
+ :other
43
+ end
44
+ end
45
+
46
+ # @return [True, False] check if server execute some test
47
+ def executing_test?(server_name)
48
+ all_data = server_data(server_name)
49
+ all_data.key?('test')
50
+ end
51
+
52
+ # @param server_name [Server] server to unbook
53
+ # @return [Nothing]
54
+ def unbook_server(server_name)
55
+ uri = URI.parse("#{@uri}/queue/unbook_server")
56
+ perform_post(uri, 'server' => server_name)
57
+ end
58
+
59
+ # @param server_name [Server] server to book
60
+ # @return [Nothing]
61
+ def book_server(server_name)
62
+ uri = URI.parse("#{@uri}/queue/book_server")
63
+ perform_post(uri, 'server' => server_name)
64
+ end
65
+
66
+ # @param server_name [Server] server to power on
67
+ # @return [Nothing]
68
+ def power_on_server(server_name, size = nil)
69
+ return if powering_status(server_name) == :on
70
+
71
+ uri = URI.parse("#{@uri}/servers/cloud_server_create")
72
+ perform_post(uri, 'server' => server_name, 'size' => size)
73
+ wait_for_server_have_status(server_name, :on)
74
+ end
75
+
76
+ # @param server_name [Server] server to power on
77
+ # @return [Nothing]
78
+ def power_off_server(server_name)
79
+ return if powering_status(server_name) == :off
80
+
81
+ unbook_server(server_name)
82
+ uri = URI.parse("#{@uri}/servers/cloud_server_destroy")
83
+ perform_post(uri, 'server' => server_name)
84
+ wait_for_server_have_status(server_name, :off)
85
+ end
86
+
87
+ # Waiting until server have statue
88
+ # @param server [String] name of server
89
+ # @param status [Symbol] state to wait
90
+ # @return [Nothing]
91
+ def wait_for_server_have_status(server, status)
92
+ current_wait_time = 0
93
+ loop do
94
+ return if powering_status(server) == status
95
+
96
+ @logger.info("Wait for state: #{status}: \
97
+ #{current_wait_time} of: #{@waiting_timeout}")
98
+ sleep @between_request_timeout
99
+ current_wait_time += @between_request_timeout
100
+ raise(server_status_timeout_error(server, status)) if current_wait_time > @waiting_timeout
101
+ end
102
+ end
103
+
104
+ # @param server [String] name of server
105
+ # @param status [String] status to wait failed
106
+ # @return [String] error for server timeout
107
+ def server_status_timeout_error(server, status)
108
+ "Couldn't wait until #{server} have status "\
109
+ "#{status} in specified timeout"
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WrataApi
4
+ # Class raised if server count is too big
5
+ class NotEnoughServerCount < StandardError
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WrataApi
4
+ # This module holds the WrataApi version information.
5
+ module Version
6
+ # @return [String] current gem version
7
+ STRING = '0.1.0'
8
+ end
9
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ module WrataApi
5
+ # Module for describing base request actions
6
+ module WrataApiRequest
7
+ # @return [Hash] default header for requests
8
+ def default_header
9
+ { 'Cookie' => "remember_token=#{@cookie}; \
10
+ _runner_session=#{@wrata_session}",
11
+ 'X-CSRF-Token' => @csrf_token }
12
+ end
13
+
14
+ # Make get request
15
+ # @param uri [URI] uri wiht server
16
+ # @param body [Hash] body of request
17
+ # @return [JSON] result of request
18
+ def perform_get(uri, body = {})
19
+ http = Net::HTTP.new(uri.host, uri.port)
20
+ request = Net::HTTP::Get.new(uri.request_uri, default_header)
21
+ request.set_form_data(body)
22
+ body = http.request(request).body
23
+ @logger.info("Get request: #{uri} answered: #{body}")
24
+ return {} if body.empty?
25
+
26
+ JSON.parse(body)
27
+ end
28
+
29
+ # Make post request
30
+ # @param uri [URI] uri wiht server
31
+ # @param body [Hash] body of request
32
+ # @return [JSON] result of request
33
+ def perform_post(uri, body = {})
34
+ http = Net::HTTP.new(uri.host, uri.port)
35
+ request = Net::HTTP::Post.new(uri.request_uri, default_header)
36
+ request.set_form_data(body)
37
+ http.read_timeout = 500
38
+ body = http.request(request).body
39
+ @logger.info("Post request: #{uri} answered: #{body}")
40
+ return {} if body.empty?
41
+
42
+ JSON.parse(body)
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wrata_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ONLYOFFICE
8
+ - Pavel Lobashov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2020-05-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '13.0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '13.0'
28
+ description: Wrapper around wrata API
29
+ email:
30
+ - shockwavenn@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/wrata_api.rb
36
+ - lib/wrata_api/file_tree.rb
37
+ - lib/wrata_api/name.rb
38
+ - lib/wrata_api/queue_methos.rb
39
+ - lib/wrata_api/server_list.rb
40
+ - lib/wrata_api/server_methods.rb
41
+ - lib/wrata_api/server_methods/server_methods_exceptions.rb
42
+ - lib/wrata_api/version.rb
43
+ - lib/wrata_api/wrata_api_request.rb
44
+ homepage: https://github.com/ONLYOFFICE/wrata_api
45
+ licenses:
46
+ - AGPL-3.0
47
+ metadata:
48
+ bug_tracker_uri: https://github.com/ONLYOFFICE/wrata_api/issues
49
+ changelog_uri: https://github.com/ONLYOFFICE/wrata_api/blob/master/CHANGELOG.md
50
+ documentation_uri: https://www.rubydoc.info/gems/wrata_api
51
+ homepage_uri: https://github.com/ONLYOFFICE/wrata_api
52
+ source_code_uri: https://github.com/ONLYOFFICE/wrata_api
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '2.4'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.1.2
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Wrata API Gem
72
+ test_files: []