rise-mirror_manager 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5a44e98c5346580d88a87c91152a46d913fa7e4dc7e27bdc6c603037d94442f9
4
+ data.tar.gz: 2242749b201b99dcb73e49704d2cefc61b9dd7caa9deb9036402bb90010c2e75
5
+ SHA512:
6
+ metadata.gz: 28267919621c984337ae5072e08242811621c57d1d09eb5e180fbc950cb7bb49c6db5986cc746b589c6d65d4f9d6eac57bc66e3b21d698a2c02b8033106d2ae3
7
+ data.tar.gz: 6b95a2d46a3eb9715d5bc29c198f95d983b9a00e34ad1f8ef8f7f08a5c91265485e1cbe3891eb5d41383e543405ac21ebfc204baf384932feddc79ea760a635c
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /logs
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ Metrics/LineLength:
2
+ Max: 120
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at script-deployer@me.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rise-mirror_manager.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rise-mirror_manager (0.1.0)
5
+ concurrent-ruby
6
+ inifile
7
+ marz-rsync (~> 0.2.0)
8
+ pony
9
+ slack-incoming-webhooks
10
+ sqlite3
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ backports (3.11.4)
16
+ builder (3.2.3)
17
+ coderay (1.1.2)
18
+ concurrent-ruby (1.1.4)
19
+ cucumber (3.1.2)
20
+ builder (>= 2.1.2)
21
+ cucumber-core (~> 3.2.0)
22
+ cucumber-expressions (~> 6.0.1)
23
+ cucumber-wire (~> 0.0.1)
24
+ diff-lcs (~> 1.3)
25
+ gherkin (~> 5.1.0)
26
+ multi_json (>= 1.7.5, < 2.0)
27
+ multi_test (>= 0.1.2)
28
+ cucumber-core (3.2.1)
29
+ backports (>= 3.8.0)
30
+ cucumber-tag_expressions (~> 1.1.0)
31
+ gherkin (~> 5.0)
32
+ cucumber-expressions (6.0.1)
33
+ cucumber-tag_expressions (1.1.1)
34
+ cucumber-wire (0.0.1)
35
+ diff-lcs (1.3)
36
+ gherkin (5.1.0)
37
+ inifile (3.0.0)
38
+ mail (2.7.1)
39
+ mini_mime (>= 0.1.1)
40
+ marz-rsync (0.2.0)
41
+ method_source (0.9.2)
42
+ mini_mime (1.0.1)
43
+ multi_json (1.13.1)
44
+ multi_test (0.1.2)
45
+ pony (1.12)
46
+ mail (>= 2.0)
47
+ pry (0.12.2)
48
+ coderay (~> 1.1.0)
49
+ method_source (~> 0.9.0)
50
+ rake (10.5.0)
51
+ rspec (3.8.0)
52
+ rspec-core (~> 3.8.0)
53
+ rspec-expectations (~> 3.8.0)
54
+ rspec-mocks (~> 3.8.0)
55
+ rspec-core (3.8.0)
56
+ rspec-support (~> 3.8.0)
57
+ rspec-expectations (3.8.2)
58
+ diff-lcs (>= 1.2.0, < 2.0)
59
+ rspec-support (~> 3.8.0)
60
+ rspec-mocks (3.8.0)
61
+ diff-lcs (>= 1.2.0, < 2.0)
62
+ rspec-support (~> 3.8.0)
63
+ rspec-support (3.8.0)
64
+ slack-incoming-webhooks (0.2.0)
65
+ sqlite3 (1.3.13)
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ bundler (~> 1.16)
72
+ cucumber (~> 3.0)
73
+ pry
74
+ rake (~> 10.0)
75
+ rise-mirror_manager!
76
+ rspec (~> 3.0)
77
+
78
+ BUNDLED WITH
79
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 marzdgzmn
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,27 @@
1
+ # Rise::MirrorManager
2
+
3
+ Manager for Mirror Sync jobs.
4
+
5
+ ## Usage
6
+
7
+ rise-mirror_manager/bin/mirror_manager #{interval}
8
+
9
+ ## Development
10
+
11
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
12
+
13
+ ## Requirements
14
+ - ruby >=
15
+ - sqlite3 >= 3.24
16
+
17
+ ## Contributing
18
+
19
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rise-mirror_manager. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
20
+
21
+ ## License
22
+
23
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
24
+
25
+ ## Code of Conduct
26
+
27
+ Everyone interacting in the Rise::MirrorManager project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rise-mirror_manager/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require File.join(__dir__, '../config/initializers/production')
5
+ # require File.join(__dir__, '../config/initializers/test')
6
+ require "rise/mirror_manager"
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require File.join(__dir__, '../config/initializers/production')
5
+ require File.join(__dir__, '../lib/rise/mirror_manager/')
6
+
7
+ if ARGV.length < 1
8
+ raise'Please provide at least one interval'
9
+ exit(1)
10
+ end
11
+
12
+ # TODO: Check if arguments are Integers
13
+
14
+ # begin
15
+ Rise::MirrorManager.run(ARGV)
16
+ # rescue ArgumentError
17
+ # Rise::MirrorManager::LOG.error "Invalid Argument: #{ARGV}"
18
+ # end
19
+
20
+
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ require File.join(__dir__, '../../lib/rise/mirror_manager/configuration')
2
+ require 'sqlite3'
3
+
4
+ # require_relative File.join(__dir__, '../../config/initializers/production')
5
+ # puts 'initializing'
6
+
7
+ Rise::MirrorManager.configure do |config|
8
+ config.log_dir = '/var/log/mirror_manager/'
9
+ config.admin_email = 'riseops@rise.ph'
10
+ config.mirror_data_file = File.join(__dir__, '../../data/mirror_data.ini')
11
+ config.database = File.join(__dir__, '../../db/mirror_prod.db')
12
+ config.slack_webhook = ENV['SLACK_WEBHOOK']
13
+ end
14
+
15
+ mirror_db = Rise::MirrorManager.configuration.database
16
+ unless File.exist?(mirror_db)
17
+ SQLite3::Database.open(mirror_db, results_as_hash: true) do |db|
18
+ db.execute %{
19
+ CREATE TABLE IF NOT EXISTS mirrors(
20
+ mirror_id INTEGER NOT NULL PRIMARY KEY,
21
+ name TEXT NOT NULL UNIQUE
22
+ );}
23
+ db.execute %{
24
+ CREATE TABLE IF NOT EXISTS sync_info(
25
+ sync_info_id INTEGER NOT NULL PRIMARY KEY,
26
+ mirror_id INTEGER UNIQUE NOT NULL,
27
+ sync_status TEXT,
28
+ total_bandwidth TEXT,
29
+ error TEXT,
30
+ FOREIGN KEY (mirror_id) REFERENCES mirror (mirror_id)
31
+ ON DELETE CASCADE ON UPDATE NO ACTION
32
+ );}
33
+ end
34
+ end
@@ -0,0 +1,40 @@
1
+ require 'rise/mirror_manager/configuration'
2
+ require 'sqlite3'
3
+
4
+ puts 'Initializing . . .'
5
+
6
+ Rise::MirrorManager.configure do |config|
7
+ config.log_dir = File.join(__dir__, '../../logs/')
8
+ config.admin_email = 'riseops@rise.ph'
9
+ config.mirror_data_file = File.join(__dir__, '../../spec/data/mirror_data.ini')
10
+ config.database = File.join(__dir__, '../../db/mirror_test.db')
11
+ end
12
+
13
+ # Create /db/mirror_test.db
14
+ mirror_db = Rise::MirrorManager.configuration.database
15
+ File.delete(mirror_db) if File.exist?(mirror_db)
16
+ SQLite3::Database.open(mirror_db, results_as_hash: true) do |db|
17
+ db.execute %{
18
+ CREATE TABLE IF NOT EXISTS mirrors(
19
+ mirror_id INTEGER NOT NULL PRIMARY KEY,
20
+ name TEXT NOT NULL UNIQUE
21
+ );}
22
+ db.execute %{
23
+ CREATE TABLE IF NOT EXISTS sync_info(
24
+ sync_info_id INTEGER NOT NULL PRIMARY KEY,
25
+ mirror_id INTEGER UNIQUE NOT NULL,
26
+ sync_status TEXT,
27
+ total_bandwidth TEXT,
28
+ error TEXT,
29
+ FOREIGN KEY (mirror_id) REFERENCES mirror (mirror_id)
30
+ ON DELETE CASCADE ON UPDATE NO ACTION
31
+ );}
32
+ db.execute %{
33
+ INSERT INTO mirrors(name) VALUES
34
+ ('Ubuntu'),
35
+ ('ArchLinux'),
36
+ ('CentOS'),
37
+ ('Tails'),
38
+ ('RISE')
39
+ }
40
+ end
@@ -0,0 +1,29 @@
1
+ ; This is a test file and should NOT be used in production.
2
+ ;
3
+ ; Default rsync options: -a -q -z -u -H --no-motd --stats --safe-links --numeric-ids --delay-updates --timeout=600 --contimeout=300
4
+ ; Default rsync delete options: --delete --delete-delay
5
+ ;
6
+ ; Required configuration:
7
+ ; NAME - Mirror name
8
+ ; REMOTE_SOURCE - The source host. Not required if CUSTOM_SCRIPT config is present.
9
+ ; LOCAL_DIR - Directory to save files. Not required if CUSTOM_SCRIPT config is present.
10
+ ;
11
+ ; Optional:
12
+ ; RSYNC_OPTIONS - Override default rsync options. Do not put here the exclude, include and filter rsync options.
13
+ ; DELETE - Override default rsync delete options. Do not put here the exclude, include and filter rsync options.
14
+ ; EXCLUDE - Rsync exclude options to be added to default options.
15
+ ; INCLUDE - Rsync include options to be added to default options.
16
+ ; FILTER - Rsync filter options to be added to default options.
17
+ ; SCRIPT - Run custom script.
18
+ ; PRECMD - Command to run before start syncing.
19
+ ; POSTCMD - Command to run after the sync is finish.
20
+ ; DEPTH - Descend at most levels (a non-negative integer) of directories to search files to sync. Default is 1.
21
+ ; mirror sync service configuration is running on single-threaded rsync.
22
+
23
+ [delete-me]
24
+ NAME='Delete Me'
25
+ REMOTE_SOURCE="/tmp/srv/mirror/delete-me-source"
26
+ LOCAL_DIR="/tmp/srv/mirror/delete-me-dest"
27
+ RSYNC_OPTS='-avzuH --no-motd --safe-links --numeric-ids --delay-updates --timeout=600'
28
+ DEPTH=1
29
+ INTERVAL=4
data/db/mirror_prod.db ADDED
Binary file
data/db/mirror_test.db ADDED
Binary file
@@ -0,0 +1,79 @@
1
+ require_relative 'mirror_manager/version'
2
+ require_relative 'mirror_manager/logging'
3
+ require_relative 'mirror_manager/sync'
4
+ require_relative 'mirror_manager/mirror'
5
+ require_relative 'mirror_manager/notification'
6
+ require 'English'
7
+ require 'yaml'
8
+ require 'concurrent'
9
+
10
+ module Rise
11
+ # Module for managing mirror sync jobs
12
+ module MirrorManager
13
+
14
+ def self.run(intervals)
15
+ puts ENV['SLACK_WEBHOOK']
16
+ Manager.execute_jobs(intervals)
17
+ end
18
+
19
+ class Manager
20
+ include Rise::MirrorManager::Notification
21
+
22
+ LOG = Logging.setup_logger
23
+
24
+ def self.execute_jobs(intervals)
25
+ Manager.new(intervals).run_syncs
26
+ end
27
+
28
+ def run_syncs()
29
+ thread_pool = Concurrent::FixedThreadPool.new(10, max_queue:50)
30
+ until mirrors.empty?
31
+ mirror = mirrors.shift
32
+ Concurrent::Promises.future(executor: thread_pool) { Sync.run(mirror) }.rescue { |e| send_error_notification(mirror_name: mirror.name, error: e) }.result
33
+ end
34
+ thread_pool.shutdown
35
+ thread_pool.wait_for_termination
36
+ end
37
+
38
+ private
39
+
40
+ attr_reader :mirrors, :runtime
41
+
42
+ def initialize(intervals)
43
+ @mirrors = retrieve_mirrors(intervals)
44
+ @runtime = Time.now
45
+ end
46
+
47
+ def send_error_notification(mirror_name:, error:)
48
+ send_slack_message(mirror_name: mirror_name, error: error, runtime: runtime)
49
+ end
50
+
51
+ def retrieve_mirrors(intervals)
52
+ mirrors = []
53
+ intervals.each do |i|
54
+ retrieved_mirrors = Mirror.retrieve_mirrors(Integer(i))
55
+ mirrors.push(*retrieved_mirrors)
56
+ end
57
+ if mirrors.empty?
58
+ raise 'No mirrors were retrieved from #{intervals}.'
59
+ end
60
+ mirrors
61
+ end
62
+
63
+ at_exit do
64
+ if $ERROR_INFO
65
+ error = {
66
+ timestamp: Time.now,
67
+ message: $ERROR_INFO.message,
68
+ backtrace: $ERROR_INFO.backtrace,
69
+ gems: Gem.loaded_specs.inject({}) do |m, (n, s)|
70
+ m.merge(n => s.version)
71
+ end
72
+ }
73
+ LOG.error YAML.dump(error)
74
+ send_email YAML.dump(error)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end