chatopsify 0.0.2
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 +7 -0
- data/.github/workflows/run_rubocop.yml +27 -0
- data/.github/workflows/run_tests.yml +27 -0
- data/.gitignore +8 -0
- data/.rubocop.yml +19 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +50 -0
- data/Rakefile +16 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/chatopsify.gemspec +40 -0
- data/lib/capistrano/chatopsify.rb +5 -0
- data/lib/capistrano/tasks/chatopsify.cap +68 -0
- data/lib/chatopsify/co.rb +173 -0
- data/lib/chatopsify/co_service.rb +10 -0
- data/lib/chatopsify/version.rb +5 -0
- data/lib/chatopsify.rb +5 -0
- data/lib/core_ext/custom_lib.rb +15 -0
- data/test/chatworkify_test.rb +11 -0
- data/test/test_helper.rb +6 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e3d8ad710fc9874981352d9405220466e6550206c49d4fb5ea72184b363a749c
|
4
|
+
data.tar.gz: 3b07f2b87948298459f4771c2e7370de956726119666f8a8e6a16b89166098d8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 979cf1b4aba2b5a465ff9d42ddbb4772c8d4bc9df9078a5976b4a7f7dca9d41aa4434ae3147ed07ac27a04da61af19e03ff61c94000cc907e5ed84777b4285fc
|
7
|
+
data.tar.gz: 02d46232bdab80013960c1a78166c7d09fae227a6f544c673e3e9964b0137a9e73b9fc6dedfaeb7e523e50c4cbb08578085357dc8499243d04cc9a2a0352c0c1
|
@@ -0,0 +1,27 @@
|
|
1
|
+
name: Rubocop Lint
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
|
10
|
+
steps:
|
11
|
+
- uses: actions/checkout@v2
|
12
|
+
- name: Set up Ruby 2.6 is
|
13
|
+
uses: actions/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: 2.6.x
|
16
|
+
- uses: actions/cache@v1
|
17
|
+
with:
|
18
|
+
path: vendor/bundle
|
19
|
+
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
|
20
|
+
restore-keys: |
|
21
|
+
${{ runner.os }}-gems-
|
22
|
+
- name: Run Rubocop linter
|
23
|
+
run: |
|
24
|
+
gem install bundler
|
25
|
+
bundle config path vendor/bundle
|
26
|
+
bundle install --jobs 4 --retry 3
|
27
|
+
bundle exec rubocop
|
@@ -0,0 +1,27 @@
|
|
1
|
+
name: Tests 🧪
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
|
10
|
+
steps:
|
11
|
+
- uses: actions/checkout@v2
|
12
|
+
- name: Set up Ruby 2.6 is
|
13
|
+
uses: actions/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: 2.6.x
|
16
|
+
- uses: actions/cache@v1
|
17
|
+
with:
|
18
|
+
path: vendor/bundle
|
19
|
+
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
|
20
|
+
restore-keys: |
|
21
|
+
${{ runner.os }}-gems-
|
22
|
+
- name: Run test suite
|
23
|
+
run: |
|
24
|
+
gem install bundler
|
25
|
+
bundle config path vendor/bundle
|
26
|
+
bundle install --jobs 4 --retry 3
|
27
|
+
bundle exec rake test
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Style/StringLiterals:
|
2
|
+
Enabled: true
|
3
|
+
EnforcedStyle: single_quotes
|
4
|
+
|
5
|
+
Style/StringLiteralsInInterpolation:
|
6
|
+
Enabled: true
|
7
|
+
EnforcedStyle: single_quotes
|
8
|
+
|
9
|
+
Layout/LineLength:
|
10
|
+
Max: 120
|
11
|
+
|
12
|
+
AllCops:
|
13
|
+
NewCops: enable
|
14
|
+
TargetRubyVersion: 2.6
|
15
|
+
Exclude:
|
16
|
+
- 'vendor/**/*'
|
17
|
+
- 'bin/*'
|
18
|
+
- 'Gemfile.lock'
|
19
|
+
- 'Rakefile'
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at phunt@runsystem.net. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 phunt
|
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,50 @@
|
|
1
|
+
# Chatopsify
|
2
|
+
|
3
|
+

|
4
|
+

|
5
|
+

|
6
|
+

|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'chatopsify', require: false
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle install
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install chatopsify
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Require the gem in your `Capfile`:
|
27
|
+
|
28
|
+
require 'capistrano/chatopsify'
|
29
|
+
|
30
|
+
## Configuration
|
31
|
+
|
32
|
+
### Puts `CHATOPS_API_KEY` into your ENV
|
33
|
+
|
34
|
+
- Export to your `bash`: `export CHATOPS_API_KEY=xxx`
|
35
|
+
|
36
|
+
- Add to `.env` if you're using `dotenv` gem
|
37
|
+
|
38
|
+
|
39
|
+
### Puts `CHATOPS_CHANNEL_ID` into your ENV or custom in `config/deploy`
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# config/deploy.rb
|
43
|
+
...
|
44
|
+
set :chatops_channel_id, 'xxx'
|
45
|
+
...
|
46
|
+
```
|
47
|
+
|
48
|
+
### Copyright
|
49
|
+
|
50
|
+
©rs-phunt
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "chatopsify"
|
6
|
+
|
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__)
|
data/bin/setup
ADDED
data/chatopsify.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require_relative 'lib/chatopsify/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'chatopsify'
|
10
|
+
spec.version = Chatopsify::VERSION
|
11
|
+
spec.authors = ['phunt']
|
12
|
+
spec.email = ['phunt@runsystem.net']
|
13
|
+
|
14
|
+
spec.summary = 'Publish deployment notifications to ChatOps via the API'
|
15
|
+
spec.description = 'Publish deployment notifications to ChatOps via the API'
|
16
|
+
spec.homepage = 'https://github.com/papakvy/chatopsify'
|
17
|
+
spec.license = 'MIT'
|
18
|
+
spec.required_ruby_version = '>= 2.6'
|
19
|
+
|
20
|
+
# spec.metadata['allowed_push_host'] = 'TODO: Set to your gem server 'https://example.com''
|
21
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
22
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
23
|
+
spec.metadata['source_code_uri'] = 'https://github.com/papakvy/chatopsify'
|
24
|
+
spec.metadata['changelog_uri'] = 'https://github.com/papakvy/chatopsify/blob/master/CHANGELOG.md'
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0")
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ['lib']
|
30
|
+
|
31
|
+
# Uncomment to register a new dependency of your gem
|
32
|
+
# spec.add_dependency 'chatops', '~> 0.1'
|
33
|
+
|
34
|
+
spec.add_dependency 'capistrano', '~> 3.2', '>= 3.2.0'
|
35
|
+
spec.add_development_dependency 'bundler', '~> 2'
|
36
|
+
spec.add_development_dependency 'rake', '~> 1'
|
37
|
+
|
38
|
+
# For more information and examples about making a new gem, checkout our
|
39
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
40
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
namespace :chatops do
|
2
|
+
desc "Notify ChatOps Started"
|
3
|
+
task :notify_started do
|
4
|
+
# return
|
5
|
+
set :chatops_time_started, Time.now
|
6
|
+
run_locally do
|
7
|
+
if fetch(:chatops_notify_events).include? :started
|
8
|
+
Chatopsify::Co.call.process(Chatopsify::CoLib.msg_fmt(:starting))
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
# before 'deploy:starting', 'chatops:notify_started'
|
13
|
+
before 'rbenv:validate', 'chatops:notify_started'
|
14
|
+
|
15
|
+
desc "Notify ChatOps Finished"
|
16
|
+
task :notify_finished do
|
17
|
+
set :chatops_time_finished, Time.now
|
18
|
+
run_locally do
|
19
|
+
if fetch(:chatops_notify_events).include? :finished
|
20
|
+
info 'Notifying ChatOps of deploy finished'
|
21
|
+
Chatopsify::Co.call.process(Chatopsify::CoLib.msg_fmt(:success))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
# after 'deploy:finished', 'chatops:notify_finished'
|
26
|
+
before 'deploy:failed', 'chatops:notify_finished'
|
27
|
+
|
28
|
+
desc "Notify ChatOps Finished"
|
29
|
+
task :notify_failed do
|
30
|
+
set :chatops_time_finished, Time.now
|
31
|
+
run_locally do
|
32
|
+
if fetch(:chatops_notify_events).include? :failed
|
33
|
+
info 'Notifying ChatOps of deploy failed'
|
34
|
+
Chatopsify::Co.call.process(Chatopsify::CoLib.msg_fmt(:failed))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
after 'deploy:failed', 'chatops:notify_failed'
|
39
|
+
end
|
40
|
+
|
41
|
+
namespace :load do
|
42
|
+
task :defaults do
|
43
|
+
set :chatops_co_uri, nil
|
44
|
+
set :chatops_api_key, nil
|
45
|
+
set :chatops_channel_id, nil
|
46
|
+
|
47
|
+
set :local_user, ->{ ENV["USER"] || ENV["LOGNAME"] || ENV["USERNAME"] || `hostname`.strip + '•' + `whoami`.strip || "default_user" }
|
48
|
+
set :ip_address, ->{
|
49
|
+
servers = release_roles(:all)
|
50
|
+
servers = servers.select{ |e| ENV['HOSTS']&.include?(e.hostname) } if ENV['HOSTS']
|
51
|
+
servers.map{ |e| ["#{e.hostname} (#{e.properties.fetch(:my_property)&.upcase!})"]}.join(', ')
|
52
|
+
}
|
53
|
+
set :chatops_deploy_starting_text, ->{
|
54
|
+
"### :zap: ⎡#{fetch(:application)}⎦ is deploying by ⎡*#{fetch(:local_user)}*⎦: Starting •••"
|
55
|
+
}
|
56
|
+
set :chatops_deploy_succeed_text, ->{
|
57
|
+
time_elapsed = Integer(fetch(:chatops_time_finished, 0).to_i - fetch(:chatops_time_started, 0).to_i)
|
58
|
+
"### :tada: ⎡#{fetch(:application)}⎦ was deployed by ⎡#{fetch(:local_user)}⎦: " \
|
59
|
+
"*successful* in **#{time_elapsed}** seconds."
|
60
|
+
}
|
61
|
+
set :chatops_deploy_failed_text, ->{
|
62
|
+
time_elapsed = Integer(fetch(:chatops_time_finished, 0).to_i - fetch(:chatops_time_started, 0).to_i)
|
63
|
+
"### :sos: ⎡#{fetch(:application)}⎦ was deployed by ⎡#{fetch(:local_user)}⎦: " \
|
64
|
+
"*failed* in **#{time_elapsed}** seconds."
|
65
|
+
}
|
66
|
+
set :chatops_notify_events, [:started, :finished, :failed]
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'net/http'
|
5
|
+
require 'tzinfo'
|
6
|
+
|
7
|
+
module Chatopsify
|
8
|
+
# ChatOps service
|
9
|
+
class Co
|
10
|
+
def initialize(api_key = nil)
|
11
|
+
@api_key = api_key || load_api_key
|
12
|
+
@channel_id = load_channel_id
|
13
|
+
@uri = load_uri
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.call(*args, &block)
|
17
|
+
new(*args, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def process(body = nil)
|
21
|
+
body ||= Chatopsify::CoLib.msg_fmt
|
22
|
+
|
23
|
+
send_request(body)
|
24
|
+
rescue StandardError => e
|
25
|
+
puts e.message
|
26
|
+
end
|
27
|
+
|
28
|
+
def call_get(id = nil)
|
29
|
+
send_get_request(id)
|
30
|
+
rescue StandardError => e
|
31
|
+
puts e.message
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def load_uri
|
37
|
+
ENV.fetch('CHATOPS_URI', nil) || # Load from env
|
38
|
+
(fetch(:chatops_uri) if defined?(Capistrano)) # Load from Capistrano setting
|
39
|
+
end
|
40
|
+
|
41
|
+
def load_api_key
|
42
|
+
ENV.fetch('CHATOPS_API_KEY', nil) || # Load from env
|
43
|
+
(fetch(:chatops_api_key) if defined?(Capistrano)) # Load from Capistrano setting
|
44
|
+
end
|
45
|
+
|
46
|
+
def load_channel_id
|
47
|
+
ENV.fetch('CHATOPS_CHANNEL_ID', nil) || # Load from env
|
48
|
+
(fetch(:chatops_channel_id) if defined?(Capistrano)) # Load from Capistrano setting
|
49
|
+
end
|
50
|
+
|
51
|
+
def o_api_key
|
52
|
+
Chatopsify::CoSecurity.call(@api_key).decrypt_string
|
53
|
+
end
|
54
|
+
|
55
|
+
def send_request(msg)
|
56
|
+
# puts "msg: #{msg}"
|
57
|
+
uri = URI(@uri)
|
58
|
+
|
59
|
+
req = Net::HTTP::Post.new(uri)
|
60
|
+
req['authorization'] = "Bearer #{o_api_key}"
|
61
|
+
req.content_type = 'application/json'
|
62
|
+
req.body = { channel_id: @channel_id, message: msg }.to_json
|
63
|
+
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
64
|
+
http.request(req)
|
65
|
+
end
|
66
|
+
|
67
|
+
puts "Response: #{res.code} #{res.body}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def send_get_request(id = nil)
|
71
|
+
uri = URI(@uri)
|
72
|
+
uri.path += "/#{id}" if id
|
73
|
+
|
74
|
+
# req = Net::HTTP::Get.new(co_uri)
|
75
|
+
req = Net::HTTP::Delete.new(uri)
|
76
|
+
req['authorization'] = "Bearer #{o_api_key}"
|
77
|
+
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
78
|
+
puts "req: #{req}"
|
79
|
+
http.request(req)
|
80
|
+
end
|
81
|
+
|
82
|
+
puts "Response: #{res.code} #{res.body}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# ChatOps service libs
|
87
|
+
class CoLib
|
88
|
+
class << self
|
89
|
+
def text(status)
|
90
|
+
case status
|
91
|
+
when :starting
|
92
|
+
fetch(:chatops_deploy_starting_text)
|
93
|
+
when :success
|
94
|
+
fetch(:chatops_deploy_succeed_text)
|
95
|
+
when :failed
|
96
|
+
fetch(:chatops_deploy_failed_text)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def msg_fmt(status = nil)
|
101
|
+
"""#{text(status)}
|
102
|
+
| TITLE | CONTENTS |
|
103
|
+
|----------:|:-------------|
|
104
|
+
| Stage | #{fetch(:stage).upcase!} |
|
105
|
+
| Server | #{fetch(:ip_address) }|
|
106
|
+
| Branch | #{fetch(:branch)} |
|
107
|
+
| Revision | #{fetch(:current_revision) || '<empty>'} |
|
108
|
+
| Timestamp | #{TZInfo::Timezone.get('Asia/Ho_Chi_Minh')&.now || Time.now} |
|
109
|
+
"""
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class CoSecurity
|
115
|
+
require 'openssl'
|
116
|
+
require 'securerandom'
|
117
|
+
|
118
|
+
def self.call(*args, &block)
|
119
|
+
new(*args, &block)
|
120
|
+
end
|
121
|
+
|
122
|
+
def initialize(str)
|
123
|
+
@str = str
|
124
|
+
end
|
125
|
+
|
126
|
+
def encrypt_string
|
127
|
+
begin
|
128
|
+
cipher = OpenSSL::Cipher::AES256.new(:CBC)
|
129
|
+
cipher.encrypt
|
130
|
+
salt = SecureRandom.random_bytes(16)
|
131
|
+
key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1(generate_pwd, salt, 2000, cipher.key_len + cipher.iv_len)
|
132
|
+
key = key_iv[0, cipher.key_len]
|
133
|
+
iv = key_iv[cipher.key_len, cipher.iv_len]
|
134
|
+
|
135
|
+
cipher.key = key
|
136
|
+
cipher.iv = iv
|
137
|
+
|
138
|
+
encrypted = cipher.update(@str) + cipher.final
|
139
|
+
(salt + encrypted).unpack1('H*')
|
140
|
+
rescue => e
|
141
|
+
false
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def decrypt_string
|
146
|
+
begin
|
147
|
+
encrypted = [@str].pack('H*')
|
148
|
+
cipher = OpenSSL::Cipher::AES256.new(:CBC)
|
149
|
+
cipher.decrypt
|
150
|
+
|
151
|
+
salt = encrypted[0, 16]
|
152
|
+
encrypted_data = encrypted[16..-1]
|
153
|
+
|
154
|
+
key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1(generate_pwd, salt, 2000, cipher.key_len + cipher.iv_len)
|
155
|
+
key = key_iv[0, cipher.key_len]
|
156
|
+
iv = key_iv[cipher.key_len, cipher.iv_len]
|
157
|
+
|
158
|
+
cipher.key = key
|
159
|
+
cipher.iv = iv
|
160
|
+
|
161
|
+
cipher.update(encrypted_data) + cipher.final
|
162
|
+
rescue => e
|
163
|
+
false
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
private
|
168
|
+
|
169
|
+
def generate_pwd
|
170
|
+
self.class.to_s.split('::').last.upcase.reverse
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
data/lib/chatopsify.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Custom methods for String class
|
4
|
+
class String
|
5
|
+
def truncate(max)
|
6
|
+
length > max ? "#{self[0...max]}..." : self
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
# Custom methods for Symbol class
|
11
|
+
class Symbol
|
12
|
+
def upcase!
|
13
|
+
to_s.upcase
|
14
|
+
end
|
15
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chatopsify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- phunt
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-09-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: capistrano
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.2.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.2.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '2'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '2'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1'
|
61
|
+
description: Publish deployment notifications to ChatOps via the API
|
62
|
+
email:
|
63
|
+
- phunt@runsystem.net
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- ".github/workflows/run_rubocop.yml"
|
69
|
+
- ".github/workflows/run_tests.yml"
|
70
|
+
- ".gitignore"
|
71
|
+
- ".rubocop.yml"
|
72
|
+
- CHANGELOG.md
|
73
|
+
- CODE_OF_CONDUCT.md
|
74
|
+
- Gemfile
|
75
|
+
- LICENSE.txt
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- bin/console
|
79
|
+
- bin/setup
|
80
|
+
- chatopsify.gemspec
|
81
|
+
- lib/capistrano/chatopsify.rb
|
82
|
+
- lib/capistrano/tasks/chatopsify.cap
|
83
|
+
- lib/chatopsify.rb
|
84
|
+
- lib/chatopsify/co.rb
|
85
|
+
- lib/chatopsify/co_service.rb
|
86
|
+
- lib/chatopsify/version.rb
|
87
|
+
- lib/core_ext/custom_lib.rb
|
88
|
+
- test/chatworkify_test.rb
|
89
|
+
- test/test_helper.rb
|
90
|
+
homepage: https://github.com/papakvy/chatopsify
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata:
|
94
|
+
rubygems_mfa_required: 'true'
|
95
|
+
homepage_uri: https://github.com/papakvy/chatopsify
|
96
|
+
source_code_uri: https://github.com/papakvy/chatopsify
|
97
|
+
changelog_uri: https://github.com/papakvy/chatopsify/blob/master/CHANGELOG.md
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '2.6'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubygems_version: 3.5.10
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Publish deployment notifications to ChatOps via the API
|
117
|
+
test_files: []
|