From 0ce2c7dcee2aa78e5315f16052912c5c0fa4ab3c Mon Sep 17 00:00:00 2001 From: Alexandre Jose Dutra Gomes Date: Wed, 11 Nov 2015 15:04:38 -0200 Subject: [PATCH 01/29] Initial Commit --- Dockerfile | 1 + Gemfile | 17 ++++++++++++++++ Gemfile.lock | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ lita_config.rb | 39 +++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 Dockerfile create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 lita_config.rb diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b559e8e --- /dev/null +++ b/Dockerfile @@ -0,0 +1 @@ +FROM litaio/lita diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..88e740f --- /dev/null +++ b/Gemfile @@ -0,0 +1,17 @@ +source "https://rubygems.org" + +gem "lita" + +# Uncomment to use the HipChat adapter +# gem "lita-hipchat" + +# Uncomment to use the IRC adapter +# gem "lita-irc" + +# Add handlers to give Lita new functionality. +# For example: +# gem "lita-google-images" +# gem "lita-karma" +# + +gem "lita-slack" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..a0bbd3e --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,53 @@ +GEM + remote: https://rubygems.org/ + specs: + eventmachine (1.0.8) + faraday (0.9.2) + multipart-post (>= 1.2, < 3) + faye-websocket (0.10.0) + eventmachine (>= 0.12.0) + websocket-driver (>= 0.5.1) + http_router (0.11.2) + rack (>= 1.0.0) + url_mount (~> 0.2.1) + i18n (0.7.0) + ice_nine (0.11.1) + lita (4.6.1) + bundler (>= 1.3) + faraday (>= 0.8.7) + http_router (>= 0.11.2) + i18n (>= 0.6.9) + ice_nine (>= 0.11.0) + multi_json (>= 1.7.7) + puma (>= 2.7.1) + rack (>= 1.5.2) + rb-readline (>= 0.5.1) + redis-namespace (>= 1.3.0) + thor (>= 0.18.1) + lita-slack (1.7.0) + eventmachine + faraday + faye-websocket (>= 0.8.0) + lita (>= 4.6.0) + multi_json + multi_json (1.11.2) + multipart-post (2.0.0) + puma (2.14.0) + rack (1.6.4) + rb-readline (0.5.3) + redis (3.2.1) + redis-namespace (1.5.2) + redis (~> 3.0, >= 3.0.4) + thor (0.19.1) + url_mount (0.2.1) + rack + websocket-driver (0.6.2) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.2) + +PLATFORMS + ruby + +DEPENDENCIES + lita + lita-slack diff --git a/lita_config.rb b/lita_config.rb new file mode 100644 index 0000000..b738b6e --- /dev/null +++ b/lita_config.rb @@ -0,0 +1,39 @@ +Lita.configure do |config| + config.robot.name = "Lita" + config.robot.log_level = :debug + config.robot.adapter = :slack + config.adapters.slack.token = "" + config.redis[:host] = "redis" + + # The name your robot will use. + #config.robot.name = "Lita" + + # The locale code for the language to use. + # config.robot.locale = :en + + # The severity of messages to log. Options are: + # :debug, :info, :warn, :error, :fatal + # Messages at the selected level and above will be logged. + #config.robot.log_level = :info + + # An array of user IDs that are considered administrators. These users + # the ability to add and remove other users from authorization groups. + # What is considered a user ID will change depending on which adapter you use. + # config.robot.admins = ["1", "2"] + + # The adapter you want to connect with. Make sure you've added the + # appropriate gem to the Gemfile. + #config.robot.adapter = :shell + + ## Example: Set options for the chosen adapter. + # config.adapter.username = "myname" + # config.adapter.password = "secret" + + ## Example: Set options for the Redis connection. + # config.redis.host = "127.0.0.1" + # config.redis.port = 1234 + + ## Example: Set configuration for any loaded handlers. See the handler's + ## documentation for options. + # config.handlers.some_handler.some_config_key = "value" +end From 528d175eee20d2614db28cd4f686dcbdb6923cc3 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Wed, 2 Dec 2015 18:50:22 +0000 Subject: [PATCH 02/29] Add new configuration for capistrano handler --- Dockerfile | 14 ++++++++++++++ Gemfile | 1 + Gemfile.lock | 5 +++++ lita_config.rb | 7 +++++++ 4 files changed, 27 insertions(+) diff --git a/Dockerfile b/Dockerfile index b559e8e..dcef546 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1 +1,15 @@ FROM litaio/lita + +RUN apt-get -qq update + +RUN apt-get -y install locales + +ENV LANG pt_BR.UTF-8 + +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app +RUN bundle install + +VOLUME ["/usr/src/app"] + +CMD ["lita"] diff --git a/Gemfile b/Gemfile index 88e740f..ff77174 100644 --- a/Gemfile +++ b/Gemfile @@ -15,3 +15,4 @@ gem "lita" # gem "lita-slack" +gem "net-ssh" diff --git a/Gemfile.lock b/Gemfile.lock index a0bbd3e..a21875e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -32,6 +32,7 @@ GEM multi_json multi_json (1.11.2) multipart-post (2.0.0) + net-ssh (3.0.1) puma (2.14.0) rack (1.6.4) rb-readline (0.5.3) @@ -51,3 +52,7 @@ PLATFORMS DEPENDENCIES lita lita-slack + net-ssh + +BUNDLED WITH + 1.10.6 diff --git a/lita_config.rb b/lita_config.rb index b738b6e..b2f55e9 100644 --- a/lita_config.rb +++ b/lita_config.rb @@ -1,9 +1,16 @@ +require_relative 'lita-capistrano/lib/lita/handlers/capistrano.rb' + Lita.configure do |config| config.robot.name = "Lita" config.robot.log_level = :debug config.robot.adapter = :slack config.adapters.slack.token = "" config.redis[:host] = "redis" + config.robot.admins = [] + + config.handlers.capistrano.server = "" + config.handlers.capistrano.server_user = "" + config.handlers.capistrano.server_password = "" # The name your robot will use. #config.robot.name = "Lita" From 1e784e8e216257be1d17c606821fb4a581943aa2 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Wed, 2 Dec 2015 20:18:31 +0000 Subject: [PATCH 03/29] Add new handler lita-capistrano --- lita-capistrano/.gitignore | 17 +++ lita-capistrano/Gemfile | 3 + lita-capistrano/README.md | 19 +++ lita-capistrano/Rakefile | 6 + lita-capistrano/lib/lita-capistrano.rb | 12 ++ .../lib/lita/handlers/capistrano.rb | 121 ++++++++++++++++++ lita-capistrano/lita-capistrano.gemspec | 25 ++++ lita-capistrano/locales/en.yml | 4 + .../spec/lita/handlers/capistrano_spec.rb | 5 + lita-capistrano/spec/spec_helper.rb | 6 + lita-capistrano/templates/.gitkeep | 0 11 files changed, 218 insertions(+) create mode 100644 lita-capistrano/.gitignore create mode 100644 lita-capistrano/Gemfile create mode 100644 lita-capistrano/README.md create mode 100644 lita-capistrano/Rakefile create mode 100644 lita-capistrano/lib/lita-capistrano.rb create mode 100644 lita-capistrano/lib/lita/handlers/capistrano.rb create mode 100644 lita-capistrano/lita-capistrano.gemspec create mode 100644 lita-capistrano/locales/en.yml create mode 100644 lita-capistrano/spec/lita/handlers/capistrano_spec.rb create mode 100644 lita-capistrano/spec/spec_helper.rb create mode 100644 lita-capistrano/templates/.gitkeep diff --git a/lita-capistrano/.gitignore b/lita-capistrano/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/lita-capistrano/.gitignore @@ -0,0 +1,17 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp diff --git a/lita-capistrano/Gemfile b/lita-capistrano/Gemfile new file mode 100644 index 0000000..b4e2a20 --- /dev/null +++ b/lita-capistrano/Gemfile @@ -0,0 +1,3 @@ +source "https://rubygems.org" + +gemspec diff --git a/lita-capistrano/README.md b/lita-capistrano/README.md new file mode 100644 index 0000000..0504a20 --- /dev/null +++ b/lita-capistrano/README.md @@ -0,0 +1,19 @@ +# lita-capistrano + +TODO: Add a description of the plugin. + +## Installation + +Add lita-capistrano to your Lita instance's Gemfile: + +``` ruby +gem "lita-capistrano" +``` + +## Configuration + +TODO: Describe any configuration attributes the plugin exposes. + +## Usage + +TODO: Describe the plugin's features and how to use them. diff --git a/lita-capistrano/Rakefile b/lita-capistrano/Rakefile new file mode 100644 index 0000000..c92b11e --- /dev/null +++ b/lita-capistrano/Rakefile @@ -0,0 +1,6 @@ +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:spec) + +task default: :spec diff --git a/lita-capistrano/lib/lita-capistrano.rb b/lita-capistrano/lib/lita-capistrano.rb new file mode 100644 index 0000000..658cfa5 --- /dev/null +++ b/lita-capistrano/lib/lita-capistrano.rb @@ -0,0 +1,12 @@ +require "lita" + +Lita.load_locales Dir[File.expand_path( + File.join("..", "..", "locales", "*.yml"), __FILE__ +)] + +require "lita/handlers/capistrano" + +Lita::Handlers::Capistrano.template_root File.expand_path( + File.join("..", "..", "templates"), + __FILE__ +) diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb new file mode 100644 index 0000000..c1c7f53 --- /dev/null +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -0,0 +1,121 @@ +module Lita + module Handlers + class Capistrano < Handler + require 'net/ssh' + + config :server, type: String, required: true + config :server_user, type: String, required: true + config :server_password, type: String, required: true + config :test_server, type: String + config :test_server_user, type: String + config :test_server_password, type: String + + route( + /^capistrano\s+(.+)\s+list$/, + :cap_list, command: true, + restrict_to: [:admins], + help: { "capistrano APP list " => "List available commands for determined application"} + ) + + route( + /^deploy\s+(.+)\s+(.+)\s+(.+)/, + :deploy_test, command: true, + restrict_to: [:admins, :deploy_test], + help: { "deploy AREA ENV TAG " => "Executa deploy nos ambientes internos"} + ) + + # route( + # /^capistrano\s+(.+)/, + # :cap, command: true, + # restrict_to: [:admins], + # help: { "capistrano ENV METHOD " => "Execute a capistrano task in a defined environment"} + # ) + + DEPLOY_PROPERTIES = { + commerce: { + teste: { + dir: "/home/deploy/deploy_teste/deploy_commerce", + envs: [ + "teste", + "teste2", + "teste3" + ] + }, + staging: { + dir: "/home/deploy/staging/commerce", + envs: [ + "staging_commerce", + "staging2_commerce", + "staging_exclusive", + "staging2_exclusive" + ] + } + } + } + + def cap(response) + env = response.matches[0][0] + method = response.matches[0][1] + + Net::SSH.start(config.server, config.server_user, :password => config.server_password) do |ssh| + @output = ssh.exec!("cap #{env} #{method}") + end + + response.reply(@output) + end + + def cap_list(response) + app = response.matches[0][0] + output = ssh_exec("cd /home/deploy/deploy_#{app}; cap -vT") + response.reply(output) + end + + def deploy_test(response) + area = response.matches[0][0] + env = response.matches[0][1] + tag = response.matches[0][2] + + unless area_exists?(area) + return response.reply("A área informada é inválida.") + end + unless env_exists?(area, env) + return response.reply("O ambiente informado é inválido.") + end + + dir = DEPLOY_PROPERTIES[:commerce][area.to_sym][:dir] + + response.reply("Deploy da tag #{tag} iniciado no ambiente #{env}.") + output = deploy(dir, env, tag) + if output.lines.last.include? "deploy:restart" + return response.reply("Deploy da tag #{tag} no ambiente #{env} realizado com sucesso!") + elsif output.lines.last.include? "status code 32768" + return response.reply("A tag #{tag} informada não existe. Deploy não realizado.") + else + return response.reply("Ocorreu um erro na execução do deploy da tag #{tag} no ambiente #{env}.") + end + end + + def area_exists?(area) + DEPLOY_PROPERTIES[:commerce].include?(area.to_sym) + end + + def env_exists?(area, env) + DEPLOY_PROPERTIES[:commerce][area.to_sym][:envs].include?(env) + end + + def deploy(dir, env, tag) + output = ssh_exec("cd #{dir}; cap #{env} deploy tag=#{tag}") + end + + def ssh_exec(cmd) + #TODO tornar acesso ao servidor dinamico entro prod e test + Net::SSH.start(config.server, config.server_user, :password => config.server_password) do |ssh| + @output = ssh.exec!(cmd) + end + @output + end + + Lita.register_handler(self) + end + end +end diff --git a/lita-capistrano/lita-capistrano.gemspec b/lita-capistrano/lita-capistrano.gemspec new file mode 100644 index 0000000..02639a1 --- /dev/null +++ b/lita-capistrano/lita-capistrano.gemspec @@ -0,0 +1,25 @@ +Gem::Specification.new do |spec| + spec.name = "lita-capistrano" + spec.version = "0.1.0" + spec.authors = ["Alexandre Gomes"] + spec.email = ["alejdg@outlook.com.br"] + spec.description = "TODO: Add a description" + spec.summary = "TODO: Add a summary" + spec.homepage = "TODO: Add a homepage" + spec.license = "TODO: Add a license" + spec.metadata = { "lita_plugin_type" => "handler" } + + spec.files = `git ls-files`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_runtime_dependency "lita", ">= 4.6" + spec.add_runtime_dependency 'net-ssh' + + spec.add_development_dependency "bundler", "~> 1.3" + spec.add_development_dependency "pry-byebug" + spec.add_development_dependency "rake" + spec.add_development_dependency "rack-test" + spec.add_development_dependency "rspec", ">= 3.0.0" +end diff --git a/lita-capistrano/locales/en.yml b/lita-capistrano/locales/en.yml new file mode 100644 index 0000000..3f617c1 --- /dev/null +++ b/lita-capistrano/locales/en.yml @@ -0,0 +1,4 @@ +en: + lita: + handlers: + capistrano: diff --git a/lita-capistrano/spec/lita/handlers/capistrano_spec.rb b/lita-capistrano/spec/lita/handlers/capistrano_spec.rb new file mode 100644 index 0000000..ac5532f --- /dev/null +++ b/lita-capistrano/spec/lita/handlers/capistrano_spec.rb @@ -0,0 +1,5 @@ +require "spec_helper" + +describe Lita::Handlers::Capistrano, lita_handler: true do + it { is_expected.to route ("cap commerce list").to(:cap_list)} +end diff --git a/lita-capistrano/spec/spec_helper.rb b/lita-capistrano/spec/spec_helper.rb new file mode 100644 index 0000000..94e23bf --- /dev/null +++ b/lita-capistrano/spec/spec_helper.rb @@ -0,0 +1,6 @@ +require "lita-capistrano" +require "lita/rspec" + +# A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin +# was generated with Lita 4, the compatibility mode should be left disabled. +Lita.version_3_compatibility_mode = false diff --git a/lita-capistrano/templates/.gitkeep b/lita-capistrano/templates/.gitkeep new file mode 100644 index 0000000..e69de29 From 1d355366aa911a0e25fc928fc139aca4180917c0 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Thu, 3 Dec 2015 19:05:17 +0000 Subject: [PATCH 04/29] Change the deploy_tree config to the lita_config.rb --- .../lib/lita/handlers/capistrano.rb | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb index c1c7f53..7cd9e39 100644 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -6,9 +6,7 @@ class Capistrano < Handler config :server, type: String, required: true config :server_user, type: String, required: true config :server_password, type: String, required: true - config :test_server, type: String - config :test_server_user, type: String - config :test_server_password, type: String + config :deploy_tree, type: Hash, required: true route( /^capistrano\s+(.+)\s+list$/, @@ -24,12 +22,6 @@ class Capistrano < Handler help: { "deploy AREA ENV TAG " => "Executa deploy nos ambientes internos"} ) - # route( - # /^capistrano\s+(.+)/, - # :cap, command: true, - # restrict_to: [:admins], - # help: { "capistrano ENV METHOD " => "Execute a capistrano task in a defined environment"} - # ) DEPLOY_PROPERTIES = { commerce: { @@ -82,11 +74,12 @@ def deploy_test(response) return response.reply("O ambiente informado é inválido.") end - dir = DEPLOY_PROPERTIES[:commerce][area.to_sym][:dir] + dir = config.deploy_tree[:commerce][area.to_sym][:dir] response.reply("Deploy da tag #{tag} iniciado no ambiente #{env}.") output = deploy(dir, env, tag) - if output.lines.last.include? "deploy:restart" + # The deploy:restart could be in two positions depending on the + if (output.lines.last.include? "deploy:restart") || (output.lines.last(5)[0].include? "deploy:restart") return response.reply("Deploy da tag #{tag} no ambiente #{env} realizado com sucesso!") elsif output.lines.last.include? "status code 32768" return response.reply("A tag #{tag} informada não existe. Deploy não realizado.") @@ -96,11 +89,11 @@ def deploy_test(response) end def area_exists?(area) - DEPLOY_PROPERTIES[:commerce].include?(area.to_sym) + config.deploy_tree[:commerce].include?(area.to_sym) end def env_exists?(area, env) - DEPLOY_PROPERTIES[:commerce][area.to_sym][:envs].include?(env) + config.deploy_tree[:commerce][area.to_sym][:envs].include?(env) end def deploy(dir, env, tag) From 6a66d31d41268fa2e2fc5f059d4346114d992d86 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Mon, 14 Dec 2015 14:07:49 +0000 Subject: [PATCH 05/29] Correct locale problem on the Docker container --- Dockerfile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index dcef546..9de7192 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,13 @@ RUN apt-get -qq update RUN apt-get -y install locales -ENV LANG pt_BR.UTF-8 +COPY locale /etc/default/locale +RUN locale-gen pt_BR.UTF-8 &&\ + DEBIAN_FRONTEND=noninteractive dpkg-reconfigure locales RUN mkdir -p /usr/src/app +COPY Gemfile /usr/src/app +COPY Gemfile.lock /usr/src/app WORKDIR /usr/src/app RUN bundle install From e1d9360537d576dba8dfef6a7570da7bbf016cfb Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Mon, 14 Dec 2015 14:22:33 +0000 Subject: [PATCH 06/29] Remove deploy-tree config from handler file --- .../lib/lita/handlers/capistrano.rb | 25 +------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb index 7cd9e39..22ebe35 100644 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -22,29 +22,6 @@ class Capistrano < Handler help: { "deploy AREA ENV TAG " => "Executa deploy nos ambientes internos"} ) - - DEPLOY_PROPERTIES = { - commerce: { - teste: { - dir: "/home/deploy/deploy_teste/deploy_commerce", - envs: [ - "teste", - "teste2", - "teste3" - ] - }, - staging: { - dir: "/home/deploy/staging/commerce", - envs: [ - "staging_commerce", - "staging2_commerce", - "staging_exclusive", - "staging2_exclusive" - ] - } - } - } - def cap(response) env = response.matches[0][0] method = response.matches[0][1] @@ -78,7 +55,7 @@ def deploy_test(response) response.reply("Deploy da tag #{tag} iniciado no ambiente #{env}.") output = deploy(dir, env, tag) - # The deploy:restart could be in two positions depending on the + # The deploy:restart could be in two positions depending on the if (output.lines.last.include? "deploy:restart") || (output.lines.last(5)[0].include? "deploy:restart") return response.reply("Deploy da tag #{tag} no ambiente #{env} realizado com sucesso!") elsif output.lines.last.include? "status code 32768" From 9097e06ff0f33eb362477e7d5947531b88f53edd Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Mon, 14 Dec 2015 14:27:30 +0000 Subject: [PATCH 07/29] Add gitinore and lita_config.rb.example files. Removes lita_config.rb from versioning --- .gitignore | 2 ++ lita_config.rb | 46 ------------------------------------------ lita_config.rb.example | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 46 deletions(-) create mode 100644 .gitignore delete mode 100644 lita_config.rb create mode 100644 lita_config.rb.example diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0806344 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Ignore config +lita_config.rb diff --git a/lita_config.rb b/lita_config.rb deleted file mode 100644 index b2f55e9..0000000 --- a/lita_config.rb +++ /dev/null @@ -1,46 +0,0 @@ -require_relative 'lita-capistrano/lib/lita/handlers/capistrano.rb' - -Lita.configure do |config| - config.robot.name = "Lita" - config.robot.log_level = :debug - config.robot.adapter = :slack - config.adapters.slack.token = "" - config.redis[:host] = "redis" - config.robot.admins = [] - - config.handlers.capistrano.server = "" - config.handlers.capistrano.server_user = "" - config.handlers.capistrano.server_password = "" - - # The name your robot will use. - #config.robot.name = "Lita" - - # The locale code for the language to use. - # config.robot.locale = :en - - # The severity of messages to log. Options are: - # :debug, :info, :warn, :error, :fatal - # Messages at the selected level and above will be logged. - #config.robot.log_level = :info - - # An array of user IDs that are considered administrators. These users - # the ability to add and remove other users from authorization groups. - # What is considered a user ID will change depending on which adapter you use. - # config.robot.admins = ["1", "2"] - - # The adapter you want to connect with. Make sure you've added the - # appropriate gem to the Gemfile. - #config.robot.adapter = :shell - - ## Example: Set options for the chosen adapter. - # config.adapter.username = "myname" - # config.adapter.password = "secret" - - ## Example: Set options for the Redis connection. - # config.redis.host = "127.0.0.1" - # config.redis.port = 1234 - - ## Example: Set configuration for any loaded handlers. See the handler's - ## documentation for options. - # config.handlers.some_handler.some_config_key = "value" -end diff --git a/lita_config.rb.example b/lita_config.rb.example new file mode 100644 index 0000000..9cb5f31 --- /dev/null +++ b/lita_config.rb.example @@ -0,0 +1,42 @@ +require_relative 'lita-capistrano/lib/lita/handlers/capistrano.rb' + +Lita.configure do |config| + config.robot.name = "Sir Bottington" + config.robot.log_level = :debug + config.robot.adapter = :example_chat_service + config.adapters.example_chat_service.username = "bottington" + config.adapters.example_chat_service.password = "secret" + config.redis[:host] = "redis.example.com" + config.robot.admins = [] + + config.handlers.capistrano.server = "capistrano-deploy.com" + config.handlers.capistrano.server_user = "lita" + config.handlers.capistrano.server_password = "secret" + + config.handlers.capistrano.deploy_tree = { + first_app: { + qa: { + dir: "/capistrano/first_app", + envs: [ + "test", + "test2" + ] + }, + staging: { + dir: "/capistrano/fist_app/staging", + envs: [ + "staging1", + "staging2" + ] + }, + production: { + dir: "/capistrano/production/first_app", + envs: [ + "production-dc1", + "production-dc2" + ] + } + } + } + +end From 9eb998bd1a8ec975194a508836455b9330bd84a1 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Mon, 14 Dec 2015 14:28:07 +0000 Subject: [PATCH 08/29] Add docker-composer file --- docker-compose.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e61c1c3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +web: + container_name: lita-dev + build: . + volumes: + - //c/Users/agomes/workspace/Ruby/lita-chatops:/usr/src/app + links: + - redis + - mongodb + tty: true + stdin_open: true +redis: + image: redis +mongodb: + image: mongo From 6fbdd3463b8624bc26c135532a8821c2e7e22b61 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Tue, 15 Dec 2015 13:38:12 +0000 Subject: [PATCH 09/29] Add new deploy-tracker-handler --- Gemfile | 1 + Gemfile.lock | 4 ++++ lita-deploy-tracker/.gitignore | 17 +++++++++++++ lita-deploy-tracker/Gemfile | 3 +++ lita-deploy-tracker/README.md | 19 +++++++++++++++ lita-deploy-tracker/Rakefile | 6 +++++ .../lib/lita-deploy-tracker.rb | 12 ++++++++++ .../lib/lita/handlers/deploy_tracker.rb | 18 ++++++++++++++ .../lita-deploy-tracker.gemspec | 24 +++++++++++++++++++ lita-deploy-tracker/locales/en.yml | 4 ++++ .../spec/lita/handlers/deploy_tracker_spec.rb | 4 ++++ lita-deploy-tracker/spec/spec_helper.rb | 6 +++++ lita-deploy-tracker/templates/.gitkeep | 0 lita_config.rb.example | 6 +++++ 14 files changed, 124 insertions(+) create mode 100644 lita-deploy-tracker/.gitignore create mode 100644 lita-deploy-tracker/Gemfile create mode 100644 lita-deploy-tracker/README.md create mode 100644 lita-deploy-tracker/Rakefile create mode 100644 lita-deploy-tracker/lib/lita-deploy-tracker.rb create mode 100644 lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb create mode 100644 lita-deploy-tracker/lita-deploy-tracker.gemspec create mode 100644 lita-deploy-tracker/locales/en.yml create mode 100644 lita-deploy-tracker/spec/lita/handlers/deploy_tracker_spec.rb create mode 100644 lita-deploy-tracker/spec/spec_helper.rb create mode 100644 lita-deploy-tracker/templates/.gitkeep diff --git a/Gemfile b/Gemfile index ff77174..f1dba46 100644 --- a/Gemfile +++ b/Gemfile @@ -16,3 +16,4 @@ gem "lita" gem "lita-slack" gem "net-ssh" +gem 'mongo', '~> 2.1' diff --git a/Gemfile.lock b/Gemfile.lock index a21875e..654d82b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,7 @@ GEM remote: https://rubygems.org/ specs: + bson (4.0.0) eventmachine (1.0.8) faraday (0.9.2) multipart-post (>= 1.2, < 3) @@ -30,6 +31,8 @@ GEM faye-websocket (>= 0.8.0) lita (>= 4.6.0) multi_json + mongo (2.2.0) + bson (~> 4.0) multi_json (1.11.2) multipart-post (2.0.0) net-ssh (3.0.1) @@ -52,6 +55,7 @@ PLATFORMS DEPENDENCIES lita lita-slack + mongo (~> 2.1) net-ssh BUNDLED WITH diff --git a/lita-deploy-tracker/.gitignore b/lita-deploy-tracker/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/lita-deploy-tracker/.gitignore @@ -0,0 +1,17 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp diff --git a/lita-deploy-tracker/Gemfile b/lita-deploy-tracker/Gemfile new file mode 100644 index 0000000..b4e2a20 --- /dev/null +++ b/lita-deploy-tracker/Gemfile @@ -0,0 +1,3 @@ +source "https://rubygems.org" + +gemspec diff --git a/lita-deploy-tracker/README.md b/lita-deploy-tracker/README.md new file mode 100644 index 0000000..b3147a9 --- /dev/null +++ b/lita-deploy-tracker/README.md @@ -0,0 +1,19 @@ +# lita-deploy-tracker + +TODO: Add a description of the plugin. + +## Installation + +Add lita-deploy-tracker to your Lita instance's Gemfile: + +``` ruby +gem "lita-deploy-tracker" +``` + +## Configuration + +TODO: Describe any configuration attributes the plugin exposes. + +## Usage + +TODO: Describe the plugin's features and how to use them. diff --git a/lita-deploy-tracker/Rakefile b/lita-deploy-tracker/Rakefile new file mode 100644 index 0000000..c92b11e --- /dev/null +++ b/lita-deploy-tracker/Rakefile @@ -0,0 +1,6 @@ +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:spec) + +task default: :spec diff --git a/lita-deploy-tracker/lib/lita-deploy-tracker.rb b/lita-deploy-tracker/lib/lita-deploy-tracker.rb new file mode 100644 index 0000000..2687ffc --- /dev/null +++ b/lita-deploy-tracker/lib/lita-deploy-tracker.rb @@ -0,0 +1,12 @@ +require "lita" + +Lita.load_locales Dir[File.expand_path( + File.join("..", "..", "locales", "*.yml"), __FILE__ +)] + +require "lita/handlers/deploy_tracker" + +Lita::Handlers::DeployTracker.template_root File.expand_path( + File.join("..", "..", "templates"), + __FILE__ +) diff --git a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb new file mode 100644 index 0000000..2895d6b --- /dev/null +++ b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb @@ -0,0 +1,18 @@ +module Lita + module Handlers + class DeployTracker < Handler + + require 'mongo' + + config :host, type: String, required: true + config :user, type: String, required: true + config :password, type: String, required: true + + def mongo_client + client = Mongo::Client.new("mongodb://#{config.user}:#{config.password}@#{config.host}:27017/lita-deploy-tracker") + end + + Lita.register_handler(self) + end + end +end diff --git a/lita-deploy-tracker/lita-deploy-tracker.gemspec b/lita-deploy-tracker/lita-deploy-tracker.gemspec new file mode 100644 index 0000000..fbd062a --- /dev/null +++ b/lita-deploy-tracker/lita-deploy-tracker.gemspec @@ -0,0 +1,24 @@ +Gem::Specification.new do |spec| + spec.name = "lita-deploy-tracker" + spec.version = "0.1.0" + spec.authors = ["TODO: Write your name"] + spec.email = ["TODO: Write your email address"] + spec.description = "TODO: Add a description" + spec.summary = "TODO: Add a summary" + spec.homepage = "TODO: Add a homepage" + spec.license = "TODO: Add a license" + spec.metadata = { "lita_plugin_type" => "handler" } + + spec.files = `git ls-files`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_runtime_dependency "lita", ">= 4.6" + + spec.add_development_dependency "bundler", "~> 1.3" + spec.add_development_dependency "pry-byebug" + spec.add_development_dependency "rake" + spec.add_development_dependency "rack-test" + spec.add_development_dependency "rspec", ">= 3.0.0" +end diff --git a/lita-deploy-tracker/locales/en.yml b/lita-deploy-tracker/locales/en.yml new file mode 100644 index 0000000..3b0dc1e --- /dev/null +++ b/lita-deploy-tracker/locales/en.yml @@ -0,0 +1,4 @@ +en: + lita: + handlers: + deploy_tracker: diff --git a/lita-deploy-tracker/spec/lita/handlers/deploy_tracker_spec.rb b/lita-deploy-tracker/spec/lita/handlers/deploy_tracker_spec.rb new file mode 100644 index 0000000..e70e3b6 --- /dev/null +++ b/lita-deploy-tracker/spec/lita/handlers/deploy_tracker_spec.rb @@ -0,0 +1,4 @@ +require "spec_helper" + +describe Lita::Handlers::DeployTracker, lita_handler: true do +end diff --git a/lita-deploy-tracker/spec/spec_helper.rb b/lita-deploy-tracker/spec/spec_helper.rb new file mode 100644 index 0000000..163cab1 --- /dev/null +++ b/lita-deploy-tracker/spec/spec_helper.rb @@ -0,0 +1,6 @@ +require "lita-deploy-tracker" +require "lita/rspec" + +# A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin +# was generated with Lita 4, the compatibility mode should be left disabled. +Lita.version_3_compatibility_mode = false diff --git a/lita-deploy-tracker/templates/.gitkeep b/lita-deploy-tracker/templates/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/lita_config.rb.example b/lita_config.rb.example index 9cb5f31..d924033 100644 --- a/lita_config.rb.example +++ b/lita_config.rb.example @@ -9,6 +9,12 @@ Lita.configure do |config| config.redis[:host] = "redis.example.com" config.robot.admins = [] + # lita-deploy-tracker config + config.handlers.deploy_tracker.host = "mongodb" + config.handlers.deploy_tracker.user = "lita" + config.handlers.deploy_tracker.password = "secret" + + # lita-capistrano config config.handlers.capistrano.server = "capistrano-deploy.com" config.handlers.capistrano.server_user = "lita" config.handlers.capistrano.server_password = "secret" From fe7269f7f0569c23e7e12cdd7a5476269305a9da Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Wed, 16 Dec 2015 14:39:02 +0000 Subject: [PATCH 10/29] New method register deploy --- .../lib/lita/handlers/capistrano.rb | 4 +++ .../lib/lita/handlers/deploy_tracker.rb | 25 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb index 22ebe35..d36f855 100644 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -53,8 +53,12 @@ def deploy_test(response) dir = config.deploy_tree[:commerce][area.to_sym][:dir] + # Deploy start response.reply("Deploy da tag #{tag} iniciado no ambiente #{env}.") + start_time = Time.now + robot.trigger(:deploy_started, app: 'commerce', env: env, tag: tag, start_time: start_time) output = deploy(dir, env, tag) + # The deploy:restart could be in two positions depending on the if (output.lines.last.include? "deploy:restart") || (output.lines.last(5)[0].include? "deploy:restart") return response.reply("Deploy da tag #{tag} no ambiente #{env} realizado com sucesso!") diff --git a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb index 2895d6b..0a09d02 100644 --- a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb +++ b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb @@ -1,15 +1,36 @@ module Lita module Handlers class DeployTracker < Handler - require 'mongo' config :host, type: String, required: true config :user, type: String, required: true config :password, type: String, required: true + on :deploy_started, :register_deploy + + def register_deploy(payload) + app = payload[:app] + area = payload[:area] + env = payload[:env] + tag = payload[:tag] + + db = mongo_client + + result = db[:deploys].insert_one ({ + app: payload[:app], + area: payload[:area], + env: payload[:env], + tag: payload[:tag], + start_time: payload[:start_time], + status: 'in progress' + }) + + end + def mongo_client - client = Mongo::Client.new("mongodb://#{config.user}:#{config.password}@#{config.host}:27017/lita-deploy-tracker") + # client = Mongo::Client.new("mongodb://#{config.user}:#{config.password}@#{config.host}:27017/lita-deploy-tracker") + client = Mongo::Client.new("mongodb://#{config.host}:27017/lita-deploy-tracker") end Lita.register_handler(self) From ff9dcb1606383dfbe7c765d967862fb9db604d2c Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Wed, 16 Dec 2015 14:40:04 +0000 Subject: [PATCH 11/29] New method update deploy --- .../lib/lita/handlers/capistrano.rb | 34 +++++++++++++++++-- .../lib/lita/handlers/deploy_tracker.rb | 32 +++++++++++++---- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb index d36f855..8a5cebe 100644 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -56,15 +56,45 @@ def deploy_test(response) # Deploy start response.reply("Deploy da tag #{tag} iniciado no ambiente #{env}.") start_time = Time.now - robot.trigger(:deploy_started, app: 'commerce', env: env, tag: tag, start_time: start_time) + robot.trigger(:deploy_started, + app: 'commerce', + env: env, + tag: tag, + start_time: start_time) + output = deploy(dir, env, tag) + # After deploy stopped + finish_time =Time.now # The deploy:restart could be in two positions depending on the - if (output.lines.last.include? "deploy:restart") || (output.lines.last(5)[0].include? "deploy:restart") + # capistrano config + if (output.lines.last.include? "deploy:restart") || + (output.lines.last(5)[0].include? "deploy:restart") + robot.trigger(:deploy_finished, + app: 'commerce', + env: env, + tag: tag, + start_time: start_time, + finish_time: finish_time, + status: 'success') return response.reply("Deploy da tag #{tag} no ambiente #{env} realizado com sucesso!") elsif output.lines.last.include? "status code 32768" + robot.trigger(:deploy_finished, + app: 'commerce', + env: env, + tag: tag, + start_time: start_time, + finish_time: finish_time, + status: 'invalid tag') return response.reply("A tag #{tag} informada não existe. Deploy não realizado.") else + robot.trigger(:deploy_finished, + app: 'commerce', + env: env, + tag: tag, + start_time: start_time, + finish_time: finish_time, + status: 'error') return response.reply("Ocorreu um erro na execução do deploy da tag #{tag} no ambiente #{env}.") end end diff --git a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb index 0a09d02..2727f23 100644 --- a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb +++ b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb @@ -8,16 +8,12 @@ class DeployTracker < Handler config :password, type: String, required: true on :deploy_started, :register_deploy + on :deploy_finished, :update_deploy def register_deploy(payload) - app = payload[:app] - area = payload[:area] - env = payload[:env] - tag = payload[:tag] - db = mongo_client - result = db[:deploys].insert_one ({ + db[:deploys].insert_one ({ app: payload[:app], area: payload[:area], env: payload[:env], @@ -25,6 +21,30 @@ def register_deploy(payload) start_time: payload[:start_time], status: 'in progress' }) + end + + def update_deploy(payload) + db = mongo_client + db[:deploys].update_one({ + app: payload[:app], + area: payload[:area], + env: payload[:env], + tag: payload[:tag], + start_time: payload[:start_time], + status: 'in progress' + }, + { + app: payload[:app], + area: payload[:area], + env: payload[:env], + tag: payload[:tag], + start_time: payload[:start_time], + finish_time: payload[:finish_time], + status: payload[:status] + }) + db[:deploys].find().each do |doc| + p doc + end end From 7f17531dfe5db2b8a1acaacdc1fc24b0a74dfb13 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Wed, 16 Dec 2015 18:50:13 +0000 Subject: [PATCH 12/29] Initial tests with event route callbacks --- .../lib/lita/handlers/capistrano.rb | 28 ++++++++++++++++--- .../lib/lita/handlers/deploy_tracker.rb | 22 +++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb index 8a5cebe..c2d39f5 100644 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -8,6 +8,8 @@ class Capistrano < Handler config :server_password, type: String, required: true config :deploy_tree, type: Hash, required: true + on :stop_deploy, :interrupt_deploy + route( /^capistrano\s+(.+)\s+list$/, :cap_list, command: true, @@ -40,6 +42,7 @@ def cap_list(response) end def deploy_test(response) + app = 'commerce' area = response.matches[0][0] env = response.matches[0][1] tag = response.matches[0][2] @@ -53,11 +56,15 @@ def deploy_test(response) dir = config.deploy_tree[:commerce][area.to_sym][:dir] + # Pre deploy check + deploy_in_progress?(app, area, env, response) + # Deploy start response.reply("Deploy da tag #{tag} iniciado no ambiente #{env}.") start_time = Time.now robot.trigger(:deploy_started, - app: 'commerce', + app: app, + area: area, env: env, tag: tag, start_time: start_time) @@ -71,7 +78,8 @@ def deploy_test(response) if (output.lines.last.include? "deploy:restart") || (output.lines.last(5)[0].include? "deploy:restart") robot.trigger(:deploy_finished, - app: 'commerce', + app: app, + area: area, env: env, tag: tag, start_time: start_time, @@ -80,7 +88,7 @@ def deploy_test(response) return response.reply("Deploy da tag #{tag} no ambiente #{env} realizado com sucesso!") elsif output.lines.last.include? "status code 32768" robot.trigger(:deploy_finished, - app: 'commerce', + app: app, env: env, tag: tag, start_time: start_time, @@ -89,7 +97,7 @@ def deploy_test(response) return response.reply("A tag #{tag} informada não existe. Deploy não realizado.") else robot.trigger(:deploy_finished, - app: 'commerce', + app: app, env: env, tag: tag, start_time: start_time, @@ -99,6 +107,7 @@ def deploy_test(response) end end + def area_exists?(area) config.deploy_tree[:commerce].include?(area.to_sym) end @@ -107,6 +116,17 @@ def env_exists?(area, env) config.deploy_tree[:commerce][area.to_sym][:envs].include?(env) end + # If a deploy is in progress the deploy_tracker handler will return a + # reponse to chat and will interrupt further using the interrupt_deploy + # method + def deploy_in_progress?(app, area, env, response) + robot.trigger(:deploy_in_progress?, app: app, area: area, env: env, response: response) + end + + def interrupt_deploy(payload) + return payload[:response].reply(payload[:msg]) + end + def deploy(dir, env, tag) output = ssh_exec("cd #{dir}; cap #{env} deploy tag=#{tag}") end diff --git a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb index 2727f23..852033b 100644 --- a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb +++ b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb @@ -9,6 +9,7 @@ class DeployTracker < Handler on :deploy_started, :register_deploy on :deploy_finished, :update_deploy + on :deploy_in_progress?, :check_deploy_in_progress def register_deploy(payload) db = mongo_client @@ -48,6 +49,27 @@ def update_deploy(payload) end + def check_deploy_in_progress(payload) + db = mongo_client + + result = db[:deploys].find({app: payload[:app], + area: payload[:area], + env: payload[:env], + status: 'in progress'}).limit(1).count + p "Result é igual a ======> #{result}" + + if result > 0 + robot.trigger(:stop_deploy, + msg: 'Já existe um deploy dessa aplicação sendo'\ + 'executado nessa area. Aguarde ele ser finalizado', + response: payload[:response]) + # robot.trigger(:deploy_in_progress_response, result: true) + # else + # robot.trigger(:deploy_in_progress_response, result: false) + end + + end + def mongo_client # client = Mongo::Client.new("mongodb://#{config.user}:#{config.password}@#{config.host}:27017/lita-deploy-tracker") client = Mongo::Client.new("mongodb://#{config.host}:27017/lita-deploy-tracker") From 6a3dbe013aa1c0b2f737a382a0a06a127210349f Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Wed, 16 Dec 2015 22:20:02 +0000 Subject: [PATCH 13/29] Add new check beefore deploy starts and handle the async between handlers --- .../lib/lita/handlers/capistrano.rb | 67 ++++++++++++------- .../lib/lita/handlers/deploy_tracker.rb | 16 ++--- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb index c2d39f5..907fdd6 100644 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -8,7 +8,8 @@ class Capistrano < Handler config :server_password, type: String, required: true config :deploy_tree, type: Hash, required: true - on :stop_deploy, :interrupt_deploy + on :deploy_checked, :deploy_exec + on :deploy_aborted, :deploy_abort route( /^capistrano\s+(.+)\s+list$/, @@ -54,10 +55,38 @@ def deploy_test(response) return response.reply("O ambiente informado é inválido.") end - dir = config.deploy_tree[:commerce][area.to_sym][:dir] # Pre deploy check - deploy_in_progress?(app, area, env, response) + deploy_in_progress?(app, area, env, tag, response) + end + + + def area_exists?(area) + config.deploy_tree[:commerce].include?(area.to_sym) + end + + def env_exists?(area, env) + config.deploy_tree[:commerce][area.to_sym][:envs].include?(env) + end + + # If a deploy is in progress the deploy_tracker handler will return a + # reponse to chat and will interrupt further using the interrupt_deploy + # method + def deploy_in_progress?(app, area, env, tag, response) + robot.trigger(:deploy_in_progress?, app: app, area: area, env: env, tag: tag, response: response) + end + + def deploy_abort(payload) + return payload[:response].reply(payload[:msg]) + end + + def deploy_exec(payload) + app = payload[:app] + area = payload[:area] + env = payload[:env] + tag = payload[:tag] + response = payload[:response] + dir = config.deploy_tree[app.to_sym][area.to_sym][:dir] # Deploy start response.reply("Deploy da tag #{tag} iniciado no ambiente #{env}.") @@ -75,8 +104,7 @@ def deploy_test(response) # The deploy:restart could be in two positions depending on the # capistrano config - if (output.lines.last.include? "deploy:restart") || - (output.lines.last(5)[0].include? "deploy:restart") + if (output.lines.last.include? "deploy:restart") || (output.lines.last(5)[0].include? "deploy:restart") robot.trigger(:deploy_finished, app: app, area: area, @@ -85,10 +113,16 @@ def deploy_test(response) start_time: start_time, finish_time: finish_time, status: 'success') - return response.reply("Deploy da tag #{tag} no ambiente #{env} realizado com sucesso!") + return response.reply("Deploy da tag #{tag} no ambiente #{env} realizado com sucesso!") elsif output.lines.last.include? "status code 32768" + p app + p area + p env + p tag + robot.trigger(:deploy_finished, app: app, + area: area, env: env, tag: tag, start_time: start_time, @@ -98,6 +132,7 @@ def deploy_test(response) else robot.trigger(:deploy_finished, app: app, + area: area, env: env, tag: tag, start_time: start_time, @@ -107,26 +142,6 @@ def deploy_test(response) end end - - def area_exists?(area) - config.deploy_tree[:commerce].include?(area.to_sym) - end - - def env_exists?(area, env) - config.deploy_tree[:commerce][area.to_sym][:envs].include?(env) - end - - # If a deploy is in progress the deploy_tracker handler will return a - # reponse to chat and will interrupt further using the interrupt_deploy - # method - def deploy_in_progress?(app, area, env, response) - robot.trigger(:deploy_in_progress?, app: app, area: area, env: env, response: response) - end - - def interrupt_deploy(payload) - return payload[:response].reply(payload[:msg]) - end - def deploy(dir, env, tag) output = ssh_exec("cd #{dir}; cap #{env} deploy tag=#{tag}") end diff --git a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb index 852033b..a5c1158 100644 --- a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb +++ b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb @@ -31,8 +31,7 @@ def update_deploy(payload) area: payload[:area], env: payload[:env], tag: payload[:tag], - start_time: payload[:start_time], - status: 'in progress' + start_time: payload[:start_time] }, { app: payload[:app], @@ -56,16 +55,13 @@ def check_deploy_in_progress(payload) area: payload[:area], env: payload[:env], status: 'in progress'}).limit(1).count - p "Result é igual a ======> #{result}" if result > 0 - robot.trigger(:stop_deploy, - msg: 'Já existe um deploy dessa aplicação sendo'\ - 'executado nessa area. Aguarde ele ser finalizado', - response: payload[:response]) - # robot.trigger(:deploy_in_progress_response, result: true) - # else - # robot.trigger(:deploy_in_progress_response, result: false) + payload[:msg] = 'Já existe um deploy dessa aplicação sendo'\ + 'executado nessa area. Aguarde ele ser finalizado' + robot.trigger(:deploy_aborted, payload) + else + robot.trigger(:deploy_checked, payload) end end From 2756df794febfed1060be93c0d1efbe84b354781 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Wed, 16 Dec 2015 22:28:08 +0000 Subject: [PATCH 14/29] Change the deploy_test method name to deploy_request --- lita-capistrano/lib/lita/handlers/capistrano.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb index 907fdd6..b4bebe2 100644 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -20,7 +20,7 @@ class Capistrano < Handler route( /^deploy\s+(.+)\s+(.+)\s+(.+)/, - :deploy_test, command: true, + :deploy_request, command: true, restrict_to: [:admins, :deploy_test], help: { "deploy AREA ENV TAG " => "Executa deploy nos ambientes internos"} ) @@ -42,7 +42,7 @@ def cap_list(response) response.reply(output) end - def deploy_test(response) + def deploy_request(response) app = 'commerce' area = response.matches[0][0] env = response.matches[0][1] @@ -55,7 +55,6 @@ def deploy_test(response) return response.reply("O ambiente informado é inválido.") end - # Pre deploy check deploy_in_progress?(app, area, env, tag, response) end @@ -98,6 +97,7 @@ def deploy_exec(payload) tag: tag, start_time: start_time) + # Deploy execution output = deploy(dir, env, tag) # After deploy stopped finish_time =Time.now @@ -115,11 +115,6 @@ def deploy_exec(payload) status: 'success') return response.reply("Deploy da tag #{tag} no ambiente #{env} realizado com sucesso!") elsif output.lines.last.include? "status code 32768" - p app - p area - p env - p tag - robot.trigger(:deploy_finished, app: app, area: area, From f548f3b767e461bf5bec94cf2f0f62f23e9df88e Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Thu, 17 Dec 2015 20:07:49 +0000 Subject: [PATCH 15/29] New deploy list command --- .../lib/lita/handlers/capistrano.rb | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb index b4bebe2..3e2774e 100644 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -2,6 +2,9 @@ module Lita module Handlers class Capistrano < Handler require 'net/ssh' + require 'json' + require 'yaml' + require 'pp' config :server, type: String, required: true config :server_user, type: String, required: true @@ -12,41 +15,41 @@ class Capistrano < Handler on :deploy_aborted, :deploy_abort route( - /^capistrano\s+(.+)\s+list$/, - :cap_list, command: true, - restrict_to: [:admins], - help: { "capistrano APP list " => "List available commands for determined application"} + /^deploy\s+list/, + :deploy_list, command: false, + restrict_to: [:admins, :deploy_test], + help: { "deploy list [APP] " => "List available apps for deploy"} ) route( - /^deploy\s+(.+)\s+(.+)\s+(.+)/, + /^deploy\s+(.+)\s+(.+)\s+(.+)\s+(.+)/, :deploy_request, command: true, restrict_to: [:admins, :deploy_test], - help: { "deploy AREA ENV TAG " => "Executa deploy nos ambientes internos"} + help: { "deploy APP AREA ENV TAG " => "Executa deploy"} ) - def cap(response) - env = response.matches[0][0] - method = response.matches[0][1] - - Net::SSH.start(config.server, config.server_user, :password => config.server_password) do |ssh| - @output = ssh.exec!("cap #{env} #{method}") - end - - response.reply(@output) + def deploy_list_apps(response) + response.reply_privately('Available apps:') + response.reply_privately(config.deploy_tree.keys) end - def cap_list(response) - app = response.matches[0][0] - output = ssh_exec("cd /home/deploy/deploy_#{app}; cap -vT") - response.reply(output) + def deploy_list(response) + requested_app = response.args[1] + if requested_app.nil? + apps = config.deploy_tree.keys.join("\n") + response.reply_privately("Available apps:\n#{apps}") + else + # app_tree = JSON.pretty_generate(config.deploy_tree[requested_app.to_sym]) + app_tree = get_app_tree(config.deploy_tree[requested_app.to_sym]) + response.reply_privately("Available tree for #{requested_app}: \n #{app_tree}") + end end def deploy_request(response) - app = 'commerce' - area = response.matches[0][0] - env = response.matches[0][1] - tag = response.matches[0][2] + app = response.matches[0][0] + area = response.matches[0][1] + env = response.matches[0][2] + tag = response.matches[0][3] unless area_exists?(area) return response.reply("A área informada é inválida.") @@ -68,6 +71,14 @@ def env_exists?(area, env) config.deploy_tree[:commerce][area.to_sym][:envs].include?(env) end + def get_app_tree(config_tree) + app_tree = {} + config_tree.each do |key, value| + app_tree.store(key.to_s, value[:envs].map { |e| ">#{e}\n" }.join) + end + app_tree.flatten.map { |e| "#{e}\n" }.join + end + # If a deploy is in progress the deploy_tracker handler will return a # reponse to chat and will interrupt further using the interrupt_deploy # method From 662305ad6382b721b3d9479892bd2e596e4eef17 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Mon, 21 Dec 2015 19:12:29 +0000 Subject: [PATCH 16/29] Add responsible user to deploy workflow --- lita-capistrano/lib/lita/handlers/capistrano.rb | 16 ++++++++-------- .../lib/lita/handlers/deploy_tracker.rb | 3 +++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb index 3e2774e..b7ccb35 100644 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -2,9 +2,6 @@ module Lita module Handlers class Capistrano < Handler require 'net/ssh' - require 'json' - require 'yaml' - require 'pp' config :server, type: String, required: true config :server_user, type: String, required: true @@ -39,7 +36,6 @@ def deploy_list(response) apps = config.deploy_tree.keys.join("\n") response.reply_privately("Available apps:\n#{apps}") else - # app_tree = JSON.pretty_generate(config.deploy_tree[requested_app.to_sym]) app_tree = get_app_tree(config.deploy_tree[requested_app.to_sym]) response.reply_privately("Available tree for #{requested_app}: \n #{app_tree}") end @@ -99,13 +95,14 @@ def deploy_exec(payload) dir = config.deploy_tree[app.to_sym][area.to_sym][:dir] # Deploy start - response.reply("Deploy da tag #{tag} iniciado no ambiente #{env}.") + response.reply("#{response.user.mention_name}: Deploy da tag #{tag} iniciado no ambiente #{env}.") start_time = Time.now robot.trigger(:deploy_started, app: app, area: area, env: env, tag: tag, + responsible: response.user.mention_name start_time: start_time) # Deploy execution @@ -121,30 +118,33 @@ def deploy_exec(payload) area: area, env: env, tag: tag, + responsible: response.user.mention_name start_time: start_time, finish_time: finish_time, status: 'success') - return response.reply("Deploy da tag #{tag} no ambiente #{env} realizado com sucesso!") + return response.reply("#{response.user.mention_name}: Deploy da tag #{tag} no ambiente #{env} realizado com sucesso!") elsif output.lines.last.include? "status code 32768" robot.trigger(:deploy_finished, app: app, area: area, env: env, tag: tag, + responsible: response.user.mention_name start_time: start_time, finish_time: finish_time, status: 'invalid tag') - return response.reply("A tag #{tag} informada não existe. Deploy não realizado.") + return response.reply("#{response.user.mention_name}: A tag #{tag} informada não existe. Deploy não realizado.") else robot.trigger(:deploy_finished, app: app, area: area, env: env, tag: tag, + responsible: response.user.mention_name start_time: start_time, finish_time: finish_time, status: 'error') - return response.reply("Ocorreu um erro na execução do deploy da tag #{tag} no ambiente #{env}.") + return response.reply("#{response.user.mention_name}: Ocorreu um erro na execução do deploy da tag #{tag} no ambiente #{env}.") end end diff --git a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb index a5c1158..4e8368a 100644 --- a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb +++ b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb @@ -19,6 +19,7 @@ def register_deploy(payload) area: payload[:area], env: payload[:env], tag: payload[:tag], + responsible: payload[:responsible], start_time: payload[:start_time], status: 'in progress' }) @@ -31,6 +32,7 @@ def update_deploy(payload) area: payload[:area], env: payload[:env], tag: payload[:tag], + responsible: payload[:responsible], start_time: payload[:start_time] }, { @@ -38,6 +40,7 @@ def update_deploy(payload) area: payload[:area], env: payload[:env], tag: payload[:tag], + responsible: payload[:responsible], start_time: payload[:start_time], finish_time: payload[:finish_time], status: payload[:status] From 350ea296cb89e907ba43f478562f62ac6ed96441 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Mon, 21 Dec 2015 19:15:49 +0000 Subject: [PATCH 17/29] Bug fix --- lita-capistrano/lib/lita/handlers/capistrano.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb index b7ccb35..e380444 100644 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -102,7 +102,7 @@ def deploy_exec(payload) area: area, env: env, tag: tag, - responsible: response.user.mention_name + responsible: response.user.mention_name, start_time: start_time) # Deploy execution @@ -118,7 +118,7 @@ def deploy_exec(payload) area: area, env: env, tag: tag, - responsible: response.user.mention_name + responsible: response.user.mention_name, start_time: start_time, finish_time: finish_time, status: 'success') @@ -129,7 +129,7 @@ def deploy_exec(payload) area: area, env: env, tag: tag, - responsible: response.user.mention_name + responsible: response.user.mention_name, start_time: start_time, finish_time: finish_time, status: 'invalid tag') @@ -140,7 +140,7 @@ def deploy_exec(payload) area: area, env: env, tag: tag, - responsible: response.user.mention_name + responsible: response.user.mention_name, start_time: start_time, finish_time: finish_time, status: 'error') From a4a3e51e0292ee710f48ce4cb46bab028490d1a6 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Wed, 23 Dec 2015 17:55:17 +0000 Subject: [PATCH 18/29] Generated gem files --- .gitignore | 1 + Gemfile | 1 + Gemfile.lock | 19 ++++++++++++------ .../lib/lita/handlers/capistrano.rb | 20 ++++++++++++++++--- lita-capistrano/lita-capistrano.gemspec | 13 ++++++------ .../lita-deploy-tracker.gemspec | 10 ++++------ 6 files changed, 42 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 0806344..b3cf1ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # Ignore config lita_config.rb +locale diff --git a/Gemfile b/Gemfile index f1dba46..d1ec460 100644 --- a/Gemfile +++ b/Gemfile @@ -15,5 +15,6 @@ gem "lita" # gem "lita-slack" +gem "lita-capistrano" gem "net-ssh" gem 'mongo', '~> 2.1' diff --git a/Gemfile.lock b/Gemfile.lock index 654d82b..aba9aaa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,7 +5,7 @@ GEM eventmachine (1.0.8) faraday (0.9.2) multipart-post (>= 1.2, < 3) - faye-websocket (0.10.0) + faye-websocket (0.10.2) eventmachine (>= 0.12.0) websocket-driver (>= 0.5.1) http_router (0.11.2) @@ -25,27 +25,33 @@ GEM rb-readline (>= 0.5.1) redis-namespace (>= 1.3.0) thor (>= 0.18.1) - lita-slack (1.7.0) + lita-capistrano (0.1.1) + lita (~> 4.6) + lita-deploy-tracker + net-ssh + lita-deploy-tracker (0.1.0) + lita (>= 4.6) + lita-slack (1.7.2) eventmachine faraday faye-websocket (>= 0.8.0) lita (>= 4.6.0) multi_json - mongo (2.2.0) + mongo (2.2.1) bson (~> 4.0) multi_json (1.11.2) multipart-post (2.0.0) net-ssh (3.0.1) - puma (2.14.0) + puma (2.15.3) rack (1.6.4) rb-readline (0.5.3) - redis (3.2.1) + redis (3.2.2) redis-namespace (1.5.2) redis (~> 3.0, >= 3.0.4) thor (0.19.1) url_mount (0.2.1) rack - websocket-driver (0.6.2) + websocket-driver (0.6.3) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.2) @@ -54,6 +60,7 @@ PLATFORMS DEPENDENCIES lita + lita-capistrano lita-slack mongo (~> 2.1) net-ssh diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb index e380444..6824dcc 100644 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -1,7 +1,8 @@ +require 'net/ssh' + module Lita module Handlers class Capistrano < Handler - require 'net/ssh' config :server, type: String, required: true config :server_user, type: String, required: true @@ -14,17 +15,30 @@ class Capistrano < Handler route( /^deploy\s+list/, :deploy_list, command: false, - restrict_to: [:admins, :deploy_test], + restrict_to: [:admins, :deploy], help: { "deploy list [APP] " => "List available apps for deploy"} ) route( /^deploy\s+(.+)\s+(.+)\s+(.+)\s+(.+)/, :deploy_request, command: true, - restrict_to: [:admins, :deploy_test], + restrict_to: [:admins, :deploy], help: { "deploy APP AREA ENV TAG " => "Executa deploy"} ) + # Not in use + def teste + p "teste" + config.deploy_tree.each do |key, value| + route( + /^deploy\s+#{key.to_s}\s+(.+)\s+(.+)\s+(.+)/, + :deploy_request, command: true, + restrict_to: [:admins, value[:deploy_group]], + help: { "deploy #{key.to_s} AREA ENV TAG " => "Executa deploy"} + ) + end + end + def deploy_list_apps(response) response.reply_privately('Available apps:') response.reply_privately(config.deploy_tree.keys) diff --git a/lita-capistrano/lita-capistrano.gemspec b/lita-capistrano/lita-capistrano.gemspec index 02639a1..43e4f53 100644 --- a/lita-capistrano/lita-capistrano.gemspec +++ b/lita-capistrano/lita-capistrano.gemspec @@ -1,12 +1,10 @@ Gem::Specification.new do |spec| spec.name = "lita-capistrano" - spec.version = "0.1.0" + spec.version = "0.1.1" spec.authors = ["Alexandre Gomes"] spec.email = ["alejdg@outlook.com.br"] - spec.description = "TODO: Add a description" - spec.summary = "TODO: Add a summary" - spec.homepage = "TODO: Add a homepage" - spec.license = "TODO: Add a license" + spec.summary = "A Lita handler to integrate with Capistrano.rb" + spec.homepage = "https://github.com/alejdg/lita-capistrano.git" spec.metadata = { "lita_plugin_type" => "handler" } spec.files = `git ls-files`.split($/) @@ -14,12 +12,13 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] - spec.add_runtime_dependency "lita", ">= 4.6" + spec.add_runtime_dependency "lita", " ~> 4.6" + spec.add_runtime_dependency 'lita-deploy-tracker' spec.add_runtime_dependency 'net-ssh' spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "pry-byebug" spec.add_development_dependency "rake" spec.add_development_dependency "rack-test" - spec.add_development_dependency "rspec", ">= 3.0.0" + spec.add_development_dependency "rspec", "~> 3.0" end diff --git a/lita-deploy-tracker/lita-deploy-tracker.gemspec b/lita-deploy-tracker/lita-deploy-tracker.gemspec index fbd062a..c000968 100644 --- a/lita-deploy-tracker/lita-deploy-tracker.gemspec +++ b/lita-deploy-tracker/lita-deploy-tracker.gemspec @@ -1,12 +1,10 @@ Gem::Specification.new do |spec| spec.name = "lita-deploy-tracker" spec.version = "0.1.0" - spec.authors = ["TODO: Write your name"] - spec.email = ["TODO: Write your email address"] - spec.description = "TODO: Add a description" - spec.summary = "TODO: Add a summary" - spec.homepage = "TODO: Add a homepage" - spec.license = "TODO: Add a license" + spec.authors = ["Alexandre Gomes"] + spec.email = ["alejdg@outlook.com.br"] + spec.summary = "A Lita handler to track deploys" + spec.homepage = "https://github.com/alejdg/lita-deploy-tracker.git" spec.metadata = { "lita_plugin_type" => "handler" } spec.files = `git ls-files`.split($/) From 088e9033014cbd7481ddf9c9b65c91d693f5eb1d Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Thu, 14 Jan 2016 18:44:51 +0000 Subject: [PATCH 19/29] New Features --- Gemfile | 3 +- Gemfile.lock | 7 -- .../lib/lita/handlers/capistrano.rb | 97 +++++++++++++------ lita-capistrano/lita-capistrano.gemspec | 3 +- .../lib/lita/handlers/deploy_tracker.rb | 1 - .../lita-deploy-tracker.gemspec | 4 +- 6 files changed, 72 insertions(+), 43 deletions(-) diff --git a/Gemfile b/Gemfile index d1ec460..89ef504 100644 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,7 @@ gem "lita" # gem "lita-slack" -gem "lita-capistrano" +# gem "lita-capistrano" +# gem "lita-deploy-tracker" gem "net-ssh" gem 'mongo', '~> 2.1' diff --git a/Gemfile.lock b/Gemfile.lock index aba9aaa..3b3244a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -25,12 +25,6 @@ GEM rb-readline (>= 0.5.1) redis-namespace (>= 1.3.0) thor (>= 0.18.1) - lita-capistrano (0.1.1) - lita (~> 4.6) - lita-deploy-tracker - net-ssh - lita-deploy-tracker (0.1.0) - lita (>= 4.6) lita-slack (1.7.2) eventmachine faraday @@ -60,7 +54,6 @@ PLATFORMS DEPENDENCIES lita - lita-capistrano lita-slack mongo (~> 2.1) net-ssh diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb index 6824dcc..40ee297 100644 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ b/lita-capistrano/lib/lita/handlers/capistrano.rb @@ -9,39 +9,14 @@ class Capistrano < Handler config :server_password, type: String, required: true config :deploy_tree, type: Hash, required: true + on :loaded, :define_routes + on :deploy_checked, :deploy_exec on :deploy_aborted, :deploy_abort - route( - /^deploy\s+list/, - :deploy_list, command: false, - restrict_to: [:admins, :deploy], - help: { "deploy list [APP] " => "List available apps for deploy"} - ) - - route( - /^deploy\s+(.+)\s+(.+)\s+(.+)\s+(.+)/, - :deploy_request, command: true, - restrict_to: [:admins, :deploy], - help: { "deploy APP AREA ENV TAG " => "Executa deploy"} - ) - - # Not in use - def teste - p "teste" - config.deploy_tree.each do |key, value| - route( - /^deploy\s+#{key.to_s}\s+(.+)\s+(.+)\s+(.+)/, - :deploy_request, command: true, - restrict_to: [:admins, value[:deploy_group]], - help: { "deploy #{key.to_s} AREA ENV TAG " => "Executa deploy"} - ) - end - end - - def deploy_list_apps(response) - response.reply_privately('Available apps:') - response.reply_privately(config.deploy_tree.keys) + def define_routes(payload) + define_static_routes + define_dinamic_routes(config.deploy_tree) end def deploy_list(response) @@ -55,6 +30,17 @@ def deploy_list(response) end end + def deploy_auth_list(response) + requested_app = response.args[2] + if requested_app.nil? + apps_auth_tree = get_apps_auth_groups(config.deploy_tree) + response.reply_privately("Auth groups for apps:\n#{apps_auth_tree}") + else + app_tree = get_app_auth_group(config.deploy_tree[requested_app.to_sym]) + response.reply_privately("Auth group needed to deploy #{requested_app}: \n #{app_tree}") + end + end + def deploy_request(response) app = response.matches[0][0] area = response.matches[0][1] @@ -89,6 +75,22 @@ def get_app_tree(config_tree) app_tree.flatten.map { |e| "#{e}\n" }.join end + def get_apps_auth_groups(config_tree) + app_tree = {} + config_tree.each do |key, value| + app_tree.store(key.to_s, value.map { |e| ">#{e[0]}: #{e[1][:auth_group]}\n" }.join) + end + app_tree.flatten.map { |e| "#{e}\n" }.join + end + + def get_app_auth_group(config_tree) + app_tree = [] + config_tree.each do |key, value| + app_tree << "#{key.to_s}: #{value[:auth_group]}" + end + app_tree.flatten.map { |e| "#{e}\n" }.join + end + # If a deploy is in progress the deploy_tracker handler will return a # reponse to chat and will interrupt further using the interrupt_deploy # method @@ -162,6 +164,37 @@ def deploy_exec(payload) end end + private + + def define_static_routes + self.class.route( + %r{^deploy\s+list}, + :deploy_list, + command: false, + help: { "deploy list [APP] " => "List available apps for deploy"} + ) + self.class.route( + %r{^deploy\s+auth\s+list}, + :deploy_auth_list, + command: false, + help: { "deploy auth list [APP] " => "List available apps for deploy"} + ) + end + + def define_dinamic_routes(deploy_tree) + deploy_tree.each do |app, areas| + areas.each do |area, value| + self.class.route( + %r{^deploy\s+(#{app})\s+(#{area})\s+(.+)\s+(.+)}, + :deploy_request, + command: true, + restrict_to: [:admins, value[:auth_group]], + help: { "deploy #{app} #{area} ENV TAG " => "Executa deploy da app #{app} na area #{area}"} + ) + end + end + end + def deploy(dir, env, tag) output = ssh_exec("cd #{dir}; cap #{env} deploy tag=#{tag}") end @@ -173,8 +206,8 @@ def ssh_exec(cmd) end @output end - - Lita.register_handler(self) end + + Lita.register_handler(Capistrano) end end diff --git a/lita-capistrano/lita-capistrano.gemspec b/lita-capistrano/lita-capistrano.gemspec index 43e4f53..7247b0f 100644 --- a/lita-capistrano/lita-capistrano.gemspec +++ b/lita-capistrano/lita-capistrano.gemspec @@ -1,9 +1,10 @@ Gem::Specification.new do |spec| spec.name = "lita-capistrano" - spec.version = "0.1.1" + spec.version = "0.1.2" spec.authors = ["Alexandre Gomes"] spec.email = ["alejdg@outlook.com.br"] spec.summary = "A Lita handler to integrate with Capistrano.rb" + spec.description = "A Lita handler to integrate with Capistrano.rb" spec.homepage = "https://github.com/alejdg/lita-capistrano.git" spec.metadata = { "lita_plugin_type" => "handler" } diff --git a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb index 4e8368a..899862e 100644 --- a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb +++ b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb @@ -46,7 +46,6 @@ def update_deploy(payload) status: payload[:status] }) db[:deploys].find().each do |doc| - p doc end end diff --git a/lita-deploy-tracker/lita-deploy-tracker.gemspec b/lita-deploy-tracker/lita-deploy-tracker.gemspec index c000968..98f8c60 100644 --- a/lita-deploy-tracker/lita-deploy-tracker.gemspec +++ b/lita-deploy-tracker/lita-deploy-tracker.gemspec @@ -1,9 +1,10 @@ Gem::Specification.new do |spec| spec.name = "lita-deploy-tracker" - spec.version = "0.1.0" + spec.version = "0.1.1" spec.authors = ["Alexandre Gomes"] spec.email = ["alejdg@outlook.com.br"] spec.summary = "A Lita handler to track deploys" + spec.description = "A Lita handler to track deploys" spec.homepage = "https://github.com/alejdg/lita-deploy-tracker.git" spec.metadata = { "lita_plugin_type" => "handler" } @@ -13,6 +14,7 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.add_runtime_dependency "lita", ">= 4.6" + spec.add_runtime_dependency "mongo", "~> 2.1" spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "pry-byebug" From 40e7c571fd205546576b03f900a7cf2c5eb70ea8 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Thu, 14 Jan 2016 17:03:03 -0200 Subject: [PATCH 20/29] Add new file local --- locale | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 locale diff --git a/locale b/locale new file mode 100644 index 0000000..2de0cfb --- /dev/null +++ b/locale @@ -0,0 +1,3 @@ +LANGUAGE="pt_BR.UTF-8" +LANG="pt_BR.UTF-8" +LC_ALL="pt_BR.UTF-8" \ No newline at end of file From c2e9f349724826fcca7a041223efdee03d7bd3ef Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Thu, 14 Jan 2016 17:06:31 -0200 Subject: [PATCH 21/29] Remove locale from .gitignore --- .gitignore | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index b3cf1ec..addfb71 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ -# Ignore config -lita_config.rb -locale +# Ignore config +lita_config.rb From 2edc993436e89ea21f247231a9501fd753e6d0d3 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Fri, 22 Jan 2016 14:31:40 -0200 Subject: [PATCH 22/29] Update Dockerfile --- Dockerfile | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9de7192..a948f04 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,16 +4,8 @@ RUN apt-get -qq update RUN apt-get -y install locales -COPY locale /etc/default/locale -RUN locale-gen pt_BR.UTF-8 &&\ - DEBIAN_FRONTEND=noninteractive dpkg-reconfigure locales - RUN mkdir -p /usr/src/app -COPY Gemfile /usr/src/app -COPY Gemfile.lock /usr/src/app -WORKDIR /usr/src/app -RUN bundle install - VOLUME ["/usr/src/app"] +WORKDIR /usr/src/app -CMD ["lita"] +CMD ["bundle exec lita"] \ No newline at end of file From 37e0c9cd354c8598153743365c7ef7167d20c7ac Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Date: Wed, 4 May 2016 15:59:08 -0300 Subject: [PATCH 23/29] Update Dockerfile --- Dockerfile | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index a948f04..66b0e39 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,8 +4,18 @@ RUN apt-get -qq update RUN apt-get -y install locales +COPY locale /etc/default/locale +RUN locale-gen pt_BR.UTF-8 &&\ + DEBIAN_FRONTEND=noninteractive dpkg-reconfigure locales + RUN mkdir -p /usr/src/app -VOLUME ["/usr/src/app"] +COPY . /usr/src/app +# COPY Gemfile /usr/src/app +# COPY Gemfile.lock /usr/src/app WORKDIR /usr/src/app +RUN bundle install + +# Copy config +COPY lita_config.rb /usr/src/app -CMD ["bundle exec lita"] \ No newline at end of file +CMD ["lita"] \ No newline at end of file From 478e5eb43850045eb4be35877526ff91b05beb1c Mon Sep 17 00:00:00 2001 From: Alexandre Jose Dutra Gomes Date: Thu, 18 Aug 2016 15:23:42 -0300 Subject: [PATCH 24/29] Remove local deploy tracker --- lita-deploy-tracker/.gitignore | 17 ---- lita-deploy-tracker/Gemfile | 3 - lita-deploy-tracker/README.md | 19 ----- lita-deploy-tracker/Rakefile | 6 -- .../lib/lita-deploy-tracker.rb | 12 --- .../lib/lita/handlers/deploy_tracker.rb | 79 ------------------- .../lita-deploy-tracker.gemspec | 24 ------ lita-deploy-tracker/locales/en.yml | 4 - .../spec/lita/handlers/deploy_tracker_spec.rb | 4 - lita-deploy-tracker/spec/spec_helper.rb | 6 -- lita-deploy-tracker/templates/.gitkeep | 0 11 files changed, 174 deletions(-) delete mode 100644 lita-deploy-tracker/.gitignore delete mode 100644 lita-deploy-tracker/Gemfile delete mode 100644 lita-deploy-tracker/README.md delete mode 100644 lita-deploy-tracker/Rakefile delete mode 100644 lita-deploy-tracker/lib/lita-deploy-tracker.rb delete mode 100644 lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb delete mode 100644 lita-deploy-tracker/lita-deploy-tracker.gemspec delete mode 100644 lita-deploy-tracker/locales/en.yml delete mode 100644 lita-deploy-tracker/spec/lita/handlers/deploy_tracker_spec.rb delete mode 100644 lita-deploy-tracker/spec/spec_helper.rb delete mode 100644 lita-deploy-tracker/templates/.gitkeep diff --git a/lita-deploy-tracker/.gitignore b/lita-deploy-tracker/.gitignore deleted file mode 100644 index d87d4be..0000000 --- a/lita-deploy-tracker/.gitignore +++ /dev/null @@ -1,17 +0,0 @@ -*.gem -*.rbc -.bundle -.config -.yardoc -Gemfile.lock -InstalledFiles -_yardoc -coverage -doc/ -lib/bundler/man -pkg -rdoc -spec/reports -test/tmp -test/version_tmp -tmp diff --git a/lita-deploy-tracker/Gemfile b/lita-deploy-tracker/Gemfile deleted file mode 100644 index b4e2a20..0000000 --- a/lita-deploy-tracker/Gemfile +++ /dev/null @@ -1,3 +0,0 @@ -source "https://rubygems.org" - -gemspec diff --git a/lita-deploy-tracker/README.md b/lita-deploy-tracker/README.md deleted file mode 100644 index b3147a9..0000000 --- a/lita-deploy-tracker/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# lita-deploy-tracker - -TODO: Add a description of the plugin. - -## Installation - -Add lita-deploy-tracker to your Lita instance's Gemfile: - -``` ruby -gem "lita-deploy-tracker" -``` - -## Configuration - -TODO: Describe any configuration attributes the plugin exposes. - -## Usage - -TODO: Describe the plugin's features and how to use them. diff --git a/lita-deploy-tracker/Rakefile b/lita-deploy-tracker/Rakefile deleted file mode 100644 index c92b11e..0000000 --- a/lita-deploy-tracker/Rakefile +++ /dev/null @@ -1,6 +0,0 @@ -require "bundler/gem_tasks" -require "rspec/core/rake_task" - -RSpec::Core::RakeTask.new(:spec) - -task default: :spec diff --git a/lita-deploy-tracker/lib/lita-deploy-tracker.rb b/lita-deploy-tracker/lib/lita-deploy-tracker.rb deleted file mode 100644 index 2687ffc..0000000 --- a/lita-deploy-tracker/lib/lita-deploy-tracker.rb +++ /dev/null @@ -1,12 +0,0 @@ -require "lita" - -Lita.load_locales Dir[File.expand_path( - File.join("..", "..", "locales", "*.yml"), __FILE__ -)] - -require "lita/handlers/deploy_tracker" - -Lita::Handlers::DeployTracker.template_root File.expand_path( - File.join("..", "..", "templates"), - __FILE__ -) diff --git a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb b/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb deleted file mode 100644 index 899862e..0000000 --- a/lita-deploy-tracker/lib/lita/handlers/deploy_tracker.rb +++ /dev/null @@ -1,79 +0,0 @@ -module Lita - module Handlers - class DeployTracker < Handler - require 'mongo' - - config :host, type: String, required: true - config :user, type: String, required: true - config :password, type: String, required: true - - on :deploy_started, :register_deploy - on :deploy_finished, :update_deploy - on :deploy_in_progress?, :check_deploy_in_progress - - def register_deploy(payload) - db = mongo_client - - db[:deploys].insert_one ({ - app: payload[:app], - area: payload[:area], - env: payload[:env], - tag: payload[:tag], - responsible: payload[:responsible], - start_time: payload[:start_time], - status: 'in progress' - }) - end - - def update_deploy(payload) - db = mongo_client - db[:deploys].update_one({ - app: payload[:app], - area: payload[:area], - env: payload[:env], - tag: payload[:tag], - responsible: payload[:responsible], - start_time: payload[:start_time] - }, - { - app: payload[:app], - area: payload[:area], - env: payload[:env], - tag: payload[:tag], - responsible: payload[:responsible], - start_time: payload[:start_time], - finish_time: payload[:finish_time], - status: payload[:status] - }) - db[:deploys].find().each do |doc| - end - - end - - def check_deploy_in_progress(payload) - db = mongo_client - - result = db[:deploys].find({app: payload[:app], - area: payload[:area], - env: payload[:env], - status: 'in progress'}).limit(1).count - - if result > 0 - payload[:msg] = 'Já existe um deploy dessa aplicação sendo'\ - 'executado nessa area. Aguarde ele ser finalizado' - robot.trigger(:deploy_aborted, payload) - else - robot.trigger(:deploy_checked, payload) - end - - end - - def mongo_client - # client = Mongo::Client.new("mongodb://#{config.user}:#{config.password}@#{config.host}:27017/lita-deploy-tracker") - client = Mongo::Client.new("mongodb://#{config.host}:27017/lita-deploy-tracker") - end - - Lita.register_handler(self) - end - end -end diff --git a/lita-deploy-tracker/lita-deploy-tracker.gemspec b/lita-deploy-tracker/lita-deploy-tracker.gemspec deleted file mode 100644 index 98f8c60..0000000 --- a/lita-deploy-tracker/lita-deploy-tracker.gemspec +++ /dev/null @@ -1,24 +0,0 @@ -Gem::Specification.new do |spec| - spec.name = "lita-deploy-tracker" - spec.version = "0.1.1" - spec.authors = ["Alexandre Gomes"] - spec.email = ["alejdg@outlook.com.br"] - spec.summary = "A Lita handler to track deploys" - spec.description = "A Lita handler to track deploys" - spec.homepage = "https://github.com/alejdg/lita-deploy-tracker.git" - spec.metadata = { "lita_plugin_type" => "handler" } - - spec.files = `git ls-files`.split($/) - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ["lib"] - - spec.add_runtime_dependency "lita", ">= 4.6" - spec.add_runtime_dependency "mongo", "~> 2.1" - - spec.add_development_dependency "bundler", "~> 1.3" - spec.add_development_dependency "pry-byebug" - spec.add_development_dependency "rake" - spec.add_development_dependency "rack-test" - spec.add_development_dependency "rspec", ">= 3.0.0" -end diff --git a/lita-deploy-tracker/locales/en.yml b/lita-deploy-tracker/locales/en.yml deleted file mode 100644 index 3b0dc1e..0000000 --- a/lita-deploy-tracker/locales/en.yml +++ /dev/null @@ -1,4 +0,0 @@ -en: - lita: - handlers: - deploy_tracker: diff --git a/lita-deploy-tracker/spec/lita/handlers/deploy_tracker_spec.rb b/lita-deploy-tracker/spec/lita/handlers/deploy_tracker_spec.rb deleted file mode 100644 index e70e3b6..0000000 --- a/lita-deploy-tracker/spec/lita/handlers/deploy_tracker_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -require "spec_helper" - -describe Lita::Handlers::DeployTracker, lita_handler: true do -end diff --git a/lita-deploy-tracker/spec/spec_helper.rb b/lita-deploy-tracker/spec/spec_helper.rb deleted file mode 100644 index 163cab1..0000000 --- a/lita-deploy-tracker/spec/spec_helper.rb +++ /dev/null @@ -1,6 +0,0 @@ -require "lita-deploy-tracker" -require "lita/rspec" - -# A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin -# was generated with Lita 4, the compatibility mode should be left disabled. -Lita.version_3_compatibility_mode = false diff --git a/lita-deploy-tracker/templates/.gitkeep b/lita-deploy-tracker/templates/.gitkeep deleted file mode 100644 index e69de29..0000000 From d9fc3cb090c998281c31d0cf55d4c35fd33c2b14 Mon Sep 17 00:00:00 2001 From: Alexandre Jose Dutra Gomes Date: Thu, 18 Aug 2016 15:24:20 -0300 Subject: [PATCH 25/29] Squashed 'lita-deploy-tracker/' content from commit 3984557 git-subtree-dir: lita-deploy-tracker git-subtree-split: 398455729937b1959cacb362c62c27db0c01942f --- .gitignore | 17 ++ Gemfile | 3 + README.md | 19 +++ Rakefile | 6 + lib/lita-deploy-tracker.rb | 12 ++ lib/lita/handlers/deploy_tracker.rb | 197 ++++++++++++++++++++++ lita-deploy-tracker.gemspec | 24 +++ locales/en.yml | 4 + spec/lita/handlers/deploy_tracker_spec.rb | 4 + spec/spec_helper.rb | 6 + templates/.gitkeep | 0 11 files changed, 292 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 README.md create mode 100644 Rakefile create mode 100644 lib/lita-deploy-tracker.rb create mode 100644 lib/lita/handlers/deploy_tracker.rb create mode 100644 lita-deploy-tracker.gemspec create mode 100644 locales/en.yml create mode 100644 spec/lita/handlers/deploy_tracker_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 templates/.gitkeep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..b4e2a20 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source "https://rubygems.org" + +gemspec diff --git a/README.md b/README.md new file mode 100644 index 0000000..b3147a9 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# lita-deploy-tracker + +TODO: Add a description of the plugin. + +## Installation + +Add lita-deploy-tracker to your Lita instance's Gemfile: + +``` ruby +gem "lita-deploy-tracker" +``` + +## Configuration + +TODO: Describe any configuration attributes the plugin exposes. + +## Usage + +TODO: Describe the plugin's features and how to use them. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..c92b11e --- /dev/null +++ b/Rakefile @@ -0,0 +1,6 @@ +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:spec) + +task default: :spec diff --git a/lib/lita-deploy-tracker.rb b/lib/lita-deploy-tracker.rb new file mode 100644 index 0000000..2687ffc --- /dev/null +++ b/lib/lita-deploy-tracker.rb @@ -0,0 +1,12 @@ +require "lita" + +Lita.load_locales Dir[File.expand_path( + File.join("..", "..", "locales", "*.yml"), __FILE__ +)] + +require "lita/handlers/deploy_tracker" + +Lita::Handlers::DeployTracker.template_root File.expand_path( + File.join("..", "..", "templates"), + __FILE__ +) diff --git a/lib/lita/handlers/deploy_tracker.rb b/lib/lita/handlers/deploy_tracker.rb new file mode 100644 index 0000000..e70e100 --- /dev/null +++ b/lib/lita/handlers/deploy_tracker.rb @@ -0,0 +1,197 @@ +module Lita + module Handlers + class DeployTracker < Handler + require 'mongo' + + config :host, type: String, required: true + config :user, type: String, required: true + config :password, type: String, required: true + + on :loaded, :define_routes + + on :deploy_started, :register_deploy + on :deploy_finished, :update_deploy + on :deploy_in_progress?, :check_deploy_in_progress + + def define_routes(payload) + define_static_routes + end + + def register_deploy(payload) + db = mongo_client + + db[:deploys].insert_one ({ + app: payload[:app], + area: payload[:area], + env: payload[:env], + tag: payload[:tag], + responsible: payload[:responsible], + start_time: payload[:start_time], + status: 'in progress' + }) + end + + def update_deploy(payload) + db = mongo_client + db[:deploys].update_one({ + app: payload[:app], + area: payload[:area], + env: payload[:env], + tag: payload[:tag], + responsible: payload[:responsible], + start_time: payload[:start_time] + }, + { + app: payload[:app], + area: payload[:area], + env: payload[:env], + tag: payload[:tag], + responsible: payload[:responsible], + start_time: payload[:start_time], + finish_time: payload[:finish_time], + status: payload[:status] + }) + db[:deploys].find().each do |doc| + end + + end + + def check_deploy_in_progress(payload) + db = mongo_client + # db[:deploys].drop + + result = db[:deploys].find({app: payload[:app], + area: payload[:area], + env: payload[:env], + status: 'in progress'}).limit(1).count + + if result > 0 + payload[:msg] = 'Já existe um deploy dessa aplicação sendo '\ + 'executado nessa area. Aguarde ele ser finalizado' + robot.trigger(:deploy_aborted, payload) + else + robot.trigger(:deploy_checked, payload) + end + + end + + def get_deploys_in_progress(response) + target = response.message.source.room_object + db = mongo_client + deploys = [] + result = db[:deploys].find(status: 'in progress') + if result.count == 0 + return response.reply("No deploy in progress") + end + + result.each do |doc| + deploys << Adapters::Slack::Attachment.new( + "", + title: "Deploy #{doc[:app]} in progress", + fields: [ + { + title: "App", + value: doc[:app], + short: true + }, + { + title: "Área", + value: doc[:area], + short: true + }, + { + title: "Ambiente", + value: doc[:env], + short: true + }, + { + title: "tag", + value: doc[:tag], + short: true + }, + { + title: "responsible", + value: doc[:responsible], + short: true + } + ] + ) + end + return robot.chat_service.send_attachments(target, deploys) + end + + def get_last_deploys(response) + target = response.message.source.room_object + app = response.args[3] + area = response.args[4] + db = mongo_client + + result = db[:deploys].find({app: app, area: area}).limit(5) + if result.count == 0 + return response.reply("No previous deployments of the app #{app} #{area}") + end + + deploys = [] + result.each do |doc| + deploys << Adapters::Slack::Attachment.new( + "", + title: "Deploy #{doc[:app]}", + fields: [ + { + title: "App", + value: doc[:app], + short: true + }, + { + title: "Área", + value: doc[:area], + short: true + }, + { + title: "Ambiente", + value: doc[:env], + short: true + }, + { + title: "tag", + value: doc[:tag], + short: true + }, + { + title: "responsible", + value: doc[:responsible], + short: true + } + ] + ) + end + return robot.chat_service.send_attachments(target, deploys) + end + + + private + + def define_static_routes + self.class.route( + %r{^show\s+deploy\s+in\s+progress}, + :get_deploys_in_progress, + command: false, + help: { "show deploy in progress" => "Show all deploys being executed"} + ) + self.class.route( + %r{^show\s+last\s+deploys\s+from\s+(.+)\s+(.+)}, + :get_last_deploys, + command: false, + help: { "show last deploys from APP AREA" => "Show the last 5 deploys from the application area"} + ) + end + + def mongo_client + # client = Mongo::Client.new("mongodb://#{config.user}:#{config.password}@#{config.host}:27017/lita-deploy-tracker") + client = Mongo::Client.new("mongodb://#{config.host}:27017/lita-deploy-tracker") + end + + Lita.register_handler(self) + end + end +end diff --git a/lita-deploy-tracker.gemspec b/lita-deploy-tracker.gemspec new file mode 100644 index 0000000..1fdb06c --- /dev/null +++ b/lita-deploy-tracker.gemspec @@ -0,0 +1,24 @@ +Gem::Specification.new do |spec| + spec.name = "lita-deploy-tracker" + spec.version = "0.1.2" + spec.authors = ["Alexandre Gomes"] + spec.email = ["alejdg@outlook.com.br"] + spec.summary = "A Lita handler to track deploys" + spec.description = "A Lita handler to track deploys" + spec.homepage = "https://github.com/alejdg/lita-deploy-tracker.git" + spec.metadata = { "lita_plugin_type" => "handler" } + + spec.files = `git ls-files`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_runtime_dependency "lita", ">= 4.6" + spec.add_runtime_dependency "mongo", "~> 2.1" + + spec.add_development_dependency "bundler", "~> 1.3" + spec.add_development_dependency "pry-byebug" + spec.add_development_dependency "rake" + spec.add_development_dependency "rack-test" + spec.add_development_dependency "rspec", ">= 3.0.0" +end diff --git a/locales/en.yml b/locales/en.yml new file mode 100644 index 0000000..3b0dc1e --- /dev/null +++ b/locales/en.yml @@ -0,0 +1,4 @@ +en: + lita: + handlers: + deploy_tracker: diff --git a/spec/lita/handlers/deploy_tracker_spec.rb b/spec/lita/handlers/deploy_tracker_spec.rb new file mode 100644 index 0000000..e70e3b6 --- /dev/null +++ b/spec/lita/handlers/deploy_tracker_spec.rb @@ -0,0 +1,4 @@ +require "spec_helper" + +describe Lita::Handlers::DeployTracker, lita_handler: true do +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..163cab1 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,6 @@ +require "lita-deploy-tracker" +require "lita/rspec" + +# A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin +# was generated with Lita 4, the compatibility mode should be left disabled. +Lita.version_3_compatibility_mode = false diff --git a/templates/.gitkeep b/templates/.gitkeep new file mode 100644 index 0000000..e69de29 From b106c57de4ad5648f8c51a6fb78365235a042de0 Mon Sep 17 00:00:00 2001 From: Alexandre Jose Dutra Gomes Date: Thu, 18 Aug 2016 15:39:28 -0300 Subject: [PATCH 26/29] Remove local lita-capistrano --- lita-capistrano/.gitignore | 17 -- lita-capistrano/Gemfile | 3 - lita-capistrano/README.md | 19 -- lita-capistrano/Rakefile | 6 - lita-capistrano/lib/lita-capistrano.rb | 12 - .../lib/lita/handlers/capistrano.rb | 213 ------------------ lita-capistrano/lita-capistrano.gemspec | 25 -- lita-capistrano/locales/en.yml | 4 - .../spec/lita/handlers/capistrano_spec.rb | 5 - lita-capistrano/spec/spec_helper.rb | 6 - lita-capistrano/templates/.gitkeep | 0 11 files changed, 310 deletions(-) delete mode 100644 lita-capistrano/.gitignore delete mode 100644 lita-capistrano/Gemfile delete mode 100644 lita-capistrano/README.md delete mode 100644 lita-capistrano/Rakefile delete mode 100644 lita-capistrano/lib/lita-capistrano.rb delete mode 100644 lita-capistrano/lib/lita/handlers/capistrano.rb delete mode 100644 lita-capistrano/lita-capistrano.gemspec delete mode 100644 lita-capistrano/locales/en.yml delete mode 100644 lita-capistrano/spec/lita/handlers/capistrano_spec.rb delete mode 100644 lita-capistrano/spec/spec_helper.rb delete mode 100644 lita-capistrano/templates/.gitkeep diff --git a/lita-capistrano/.gitignore b/lita-capistrano/.gitignore deleted file mode 100644 index d87d4be..0000000 --- a/lita-capistrano/.gitignore +++ /dev/null @@ -1,17 +0,0 @@ -*.gem -*.rbc -.bundle -.config -.yardoc -Gemfile.lock -InstalledFiles -_yardoc -coverage -doc/ -lib/bundler/man -pkg -rdoc -spec/reports -test/tmp -test/version_tmp -tmp diff --git a/lita-capistrano/Gemfile b/lita-capistrano/Gemfile deleted file mode 100644 index b4e2a20..0000000 --- a/lita-capistrano/Gemfile +++ /dev/null @@ -1,3 +0,0 @@ -source "https://rubygems.org" - -gemspec diff --git a/lita-capistrano/README.md b/lita-capistrano/README.md deleted file mode 100644 index 0504a20..0000000 --- a/lita-capistrano/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# lita-capistrano - -TODO: Add a description of the plugin. - -## Installation - -Add lita-capistrano to your Lita instance's Gemfile: - -``` ruby -gem "lita-capistrano" -``` - -## Configuration - -TODO: Describe any configuration attributes the plugin exposes. - -## Usage - -TODO: Describe the plugin's features and how to use them. diff --git a/lita-capistrano/Rakefile b/lita-capistrano/Rakefile deleted file mode 100644 index c92b11e..0000000 --- a/lita-capistrano/Rakefile +++ /dev/null @@ -1,6 +0,0 @@ -require "bundler/gem_tasks" -require "rspec/core/rake_task" - -RSpec::Core::RakeTask.new(:spec) - -task default: :spec diff --git a/lita-capistrano/lib/lita-capistrano.rb b/lita-capistrano/lib/lita-capistrano.rb deleted file mode 100644 index 658cfa5..0000000 --- a/lita-capistrano/lib/lita-capistrano.rb +++ /dev/null @@ -1,12 +0,0 @@ -require "lita" - -Lita.load_locales Dir[File.expand_path( - File.join("..", "..", "locales", "*.yml"), __FILE__ -)] - -require "lita/handlers/capistrano" - -Lita::Handlers::Capistrano.template_root File.expand_path( - File.join("..", "..", "templates"), - __FILE__ -) diff --git a/lita-capistrano/lib/lita/handlers/capistrano.rb b/lita-capistrano/lib/lita/handlers/capistrano.rb deleted file mode 100644 index 40ee297..0000000 --- a/lita-capistrano/lib/lita/handlers/capistrano.rb +++ /dev/null @@ -1,213 +0,0 @@ -require 'net/ssh' - -module Lita - module Handlers - class Capistrano < Handler - - config :server, type: String, required: true - config :server_user, type: String, required: true - config :server_password, type: String, required: true - config :deploy_tree, type: Hash, required: true - - on :loaded, :define_routes - - on :deploy_checked, :deploy_exec - on :deploy_aborted, :deploy_abort - - def define_routes(payload) - define_static_routes - define_dinamic_routes(config.deploy_tree) - end - - def deploy_list(response) - requested_app = response.args[1] - if requested_app.nil? - apps = config.deploy_tree.keys.join("\n") - response.reply_privately("Available apps:\n#{apps}") - else - app_tree = get_app_tree(config.deploy_tree[requested_app.to_sym]) - response.reply_privately("Available tree for #{requested_app}: \n #{app_tree}") - end - end - - def deploy_auth_list(response) - requested_app = response.args[2] - if requested_app.nil? - apps_auth_tree = get_apps_auth_groups(config.deploy_tree) - response.reply_privately("Auth groups for apps:\n#{apps_auth_tree}") - else - app_tree = get_app_auth_group(config.deploy_tree[requested_app.to_sym]) - response.reply_privately("Auth group needed to deploy #{requested_app}: \n #{app_tree}") - end - end - - def deploy_request(response) - app = response.matches[0][0] - area = response.matches[0][1] - env = response.matches[0][2] - tag = response.matches[0][3] - - unless area_exists?(area) - return response.reply("A área informada é inválida.") - end - unless env_exists?(area, env) - return response.reply("O ambiente informado é inválido.") - end - - # Pre deploy check - deploy_in_progress?(app, area, env, tag, response) - end - - - def area_exists?(area) - config.deploy_tree[:commerce].include?(area.to_sym) - end - - def env_exists?(area, env) - config.deploy_tree[:commerce][area.to_sym][:envs].include?(env) - end - - def get_app_tree(config_tree) - app_tree = {} - config_tree.each do |key, value| - app_tree.store(key.to_s, value[:envs].map { |e| ">#{e}\n" }.join) - end - app_tree.flatten.map { |e| "#{e}\n" }.join - end - - def get_apps_auth_groups(config_tree) - app_tree = {} - config_tree.each do |key, value| - app_tree.store(key.to_s, value.map { |e| ">#{e[0]}: #{e[1][:auth_group]}\n" }.join) - end - app_tree.flatten.map { |e| "#{e}\n" }.join - end - - def get_app_auth_group(config_tree) - app_tree = [] - config_tree.each do |key, value| - app_tree << "#{key.to_s}: #{value[:auth_group]}" - end - app_tree.flatten.map { |e| "#{e}\n" }.join - end - - # If a deploy is in progress the deploy_tracker handler will return a - # reponse to chat and will interrupt further using the interrupt_deploy - # method - def deploy_in_progress?(app, area, env, tag, response) - robot.trigger(:deploy_in_progress?, app: app, area: area, env: env, tag: tag, response: response) - end - - def deploy_abort(payload) - return payload[:response].reply(payload[:msg]) - end - - def deploy_exec(payload) - app = payload[:app] - area = payload[:area] - env = payload[:env] - tag = payload[:tag] - response = payload[:response] - dir = config.deploy_tree[app.to_sym][area.to_sym][:dir] - - # Deploy start - response.reply("#{response.user.mention_name}: Deploy da tag #{tag} iniciado no ambiente #{env}.") - start_time = Time.now - robot.trigger(:deploy_started, - app: app, - area: area, - env: env, - tag: tag, - responsible: response.user.mention_name, - start_time: start_time) - - # Deploy execution - output = deploy(dir, env, tag) - # After deploy stopped - finish_time =Time.now - - # The deploy:restart could be in two positions depending on the - # capistrano config - if (output.lines.last.include? "deploy:restart") || (output.lines.last(5)[0].include? "deploy:restart") - robot.trigger(:deploy_finished, - app: app, - area: area, - env: env, - tag: tag, - responsible: response.user.mention_name, - start_time: start_time, - finish_time: finish_time, - status: 'success') - return response.reply("#{response.user.mention_name}: Deploy da tag #{tag} no ambiente #{env} realizado com sucesso!") - elsif output.lines.last.include? "status code 32768" - robot.trigger(:deploy_finished, - app: app, - area: area, - env: env, - tag: tag, - responsible: response.user.mention_name, - start_time: start_time, - finish_time: finish_time, - status: 'invalid tag') - return response.reply("#{response.user.mention_name}: A tag #{tag} informada não existe. Deploy não realizado.") - else - robot.trigger(:deploy_finished, - app: app, - area: area, - env: env, - tag: tag, - responsible: response.user.mention_name, - start_time: start_time, - finish_time: finish_time, - status: 'error') - return response.reply("#{response.user.mention_name}: Ocorreu um erro na execução do deploy da tag #{tag} no ambiente #{env}.") - end - end - - private - - def define_static_routes - self.class.route( - %r{^deploy\s+list}, - :deploy_list, - command: false, - help: { "deploy list [APP] " => "List available apps for deploy"} - ) - self.class.route( - %r{^deploy\s+auth\s+list}, - :deploy_auth_list, - command: false, - help: { "deploy auth list [APP] " => "List available apps for deploy"} - ) - end - - def define_dinamic_routes(deploy_tree) - deploy_tree.each do |app, areas| - areas.each do |area, value| - self.class.route( - %r{^deploy\s+(#{app})\s+(#{area})\s+(.+)\s+(.+)}, - :deploy_request, - command: true, - restrict_to: [:admins, value[:auth_group]], - help: { "deploy #{app} #{area} ENV TAG " => "Executa deploy da app #{app} na area #{area}"} - ) - end - end - end - - def deploy(dir, env, tag) - output = ssh_exec("cd #{dir}; cap #{env} deploy tag=#{tag}") - end - - def ssh_exec(cmd) - #TODO tornar acesso ao servidor dinamico entro prod e test - Net::SSH.start(config.server, config.server_user, :password => config.server_password) do |ssh| - @output = ssh.exec!(cmd) - end - @output - end - end - - Lita.register_handler(Capistrano) - end -end diff --git a/lita-capistrano/lita-capistrano.gemspec b/lita-capistrano/lita-capistrano.gemspec deleted file mode 100644 index 7247b0f..0000000 --- a/lita-capistrano/lita-capistrano.gemspec +++ /dev/null @@ -1,25 +0,0 @@ -Gem::Specification.new do |spec| - spec.name = "lita-capistrano" - spec.version = "0.1.2" - spec.authors = ["Alexandre Gomes"] - spec.email = ["alejdg@outlook.com.br"] - spec.summary = "A Lita handler to integrate with Capistrano.rb" - spec.description = "A Lita handler to integrate with Capistrano.rb" - spec.homepage = "https://github.com/alejdg/lita-capistrano.git" - spec.metadata = { "lita_plugin_type" => "handler" } - - spec.files = `git ls-files`.split($/) - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ["lib"] - - spec.add_runtime_dependency "lita", " ~> 4.6" - spec.add_runtime_dependency 'lita-deploy-tracker' - spec.add_runtime_dependency 'net-ssh' - - spec.add_development_dependency "bundler", "~> 1.3" - spec.add_development_dependency "pry-byebug" - spec.add_development_dependency "rake" - spec.add_development_dependency "rack-test" - spec.add_development_dependency "rspec", "~> 3.0" -end diff --git a/lita-capistrano/locales/en.yml b/lita-capistrano/locales/en.yml deleted file mode 100644 index 3f617c1..0000000 --- a/lita-capistrano/locales/en.yml +++ /dev/null @@ -1,4 +0,0 @@ -en: - lita: - handlers: - capistrano: diff --git a/lita-capistrano/spec/lita/handlers/capistrano_spec.rb b/lita-capistrano/spec/lita/handlers/capistrano_spec.rb deleted file mode 100644 index ac5532f..0000000 --- a/lita-capistrano/spec/lita/handlers/capistrano_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require "spec_helper" - -describe Lita::Handlers::Capistrano, lita_handler: true do - it { is_expected.to route ("cap commerce list").to(:cap_list)} -end diff --git a/lita-capistrano/spec/spec_helper.rb b/lita-capistrano/spec/spec_helper.rb deleted file mode 100644 index 94e23bf..0000000 --- a/lita-capistrano/spec/spec_helper.rb +++ /dev/null @@ -1,6 +0,0 @@ -require "lita-capistrano" -require "lita/rspec" - -# A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin -# was generated with Lita 4, the compatibility mode should be left disabled. -Lita.version_3_compatibility_mode = false diff --git a/lita-capistrano/templates/.gitkeep b/lita-capistrano/templates/.gitkeep deleted file mode 100644 index e69de29..0000000 From 66580580cab0457849cb74f0ce60fb06a001c366 Mon Sep 17 00:00:00 2001 From: Alexandre Jose Dutra Gomes Date: Fri, 19 Aug 2016 09:26:22 -0300 Subject: [PATCH 27/29] Fix static application on area and env checks --- lib/lita/handlers/capistrano.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/lita/handlers/capistrano.rb b/lib/lita/handlers/capistrano.rb index 7eca5cf..11f80dc 100644 --- a/lib/lita/handlers/capistrano.rb +++ b/lib/lita/handlers/capistrano.rb @@ -55,10 +55,10 @@ def deploy_request(response) return response.reply("Deploy da app #{app} #{area} permitido somente no canal ##{allowed_channel}") end - unless area_exists?(area) + unless area_exists?(app, area) return response.reply("A área informada é inválida.") end - unless env_exists?(area, env) + unless env_exists?(app, area, env) return response.reply("O ambiente informado é inválido.") end @@ -67,12 +67,12 @@ def deploy_request(response) deploy_in_progress?(app, area, env, tag, response) end - def area_exists?(area) - config.deploy_tree[:commerce].include?(area.to_sym) + def area_exists?(app, area) + config.deploy_tree[app.to_sym].include?(area.to_sym) end - def env_exists?(area, env) - config.deploy_tree[:commerce][area.to_sym][:envs].include?(env) + def env_exists?(app, area, env) + config.deploy_tree[app.to_sym][area.to_sym][:envs].include?(env) end def get_app_tree(config_tree) @@ -317,6 +317,10 @@ def allowed_room?(room_id, allowed_channel) return true if room.metadata["name"] == allowed_channel end + def remind() + config + end + end Lita.register_handler(Capistrano) From adf99d4aedd197f3247fb1a8ceceb24ee128181b Mon Sep 17 00:00:00 2001 From: Alexandre Jose Dutra Gomes Date: Fri, 26 Aug 2016 11:42:56 -0300 Subject: [PATCH 28/29] Create reminders on deploy_finished --- Gemfile | 2 ++ lib/lita/handlers/capistrano.rb | 39 +++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index b4e2a20..47ef624 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ source "https://rubygems.org" +gem "net-ssh" +gem 'slack-api' gemspec diff --git a/lib/lita/handlers/capistrano.rb b/lib/lita/handlers/capistrano.rb index 11f80dc..8dcc1fa 100644 --- a/lib/lita/handlers/capistrano.rb +++ b/lib/lita/handlers/capistrano.rb @@ -1,4 +1,5 @@ require 'net/ssh' +require 'slack' module Lita module Handlers @@ -8,11 +9,13 @@ class Capistrano < Handler config :server_user, type: String, required: true config :server_password, type: String, required: true config :deploy_tree, type: Hash, required: true + config :slack_api_token, type: String, required: false on :loaded, :define_routes on :deploy_checked, :deploy_exec on :deploy_aborted, :deploy_abort + on :deploy_finished, :remind_next_deploy def define_routes(payload) define_static_routes @@ -218,6 +221,23 @@ def deploy_exec(payload) end end + def remind_next_deploy(payload) + deploy_status = payload[:status] + if slack_api_configured? && deploy_status == "success" + app = payload[:app] + area = payload[:area] + env = payload[:env] + tag = payload[:tag] + responsible = payload[:responsible] + reminders = get_reminders(app, area, env) + if reminders + reminders.each do |env, time| + set_deploy_reminder(responsible, app, area, env, tag, time) + end + end + end + end + private def define_static_routes @@ -317,8 +337,23 @@ def allowed_room?(room_id, allowed_channel) return true if room.metadata["name"] == allowed_channel end - def remind() - config + def get_reminders(app, area, env) + config.deploy_tree[app.to_sym][area.to_sym][:reminders][env.to_sym] + rescue + false + end + + def set_deploy_reminder(target_user, app, area, env, tag, time) + slack = Slack::API.new(token: config.slack_api_token) + target = Lita::User.find_by_mention_name(target_user).id + todo_action = "Fazer deploy da app #{app} #{env} tag:#{tag}" + slack.reminders_add(text: todo_action, time: time, user: target) + end + + def slack_api_configured? + config.slack_api_token + rescue + config.slack_api_token ||= false end end From ec1ce74d224c3ee1cc88301156be164465cb349e Mon Sep 17 00:00:00 2001 From: Alexandre Jose Dutra Gomes Date: Fri, 26 Aug 2016 12:05:13 -0300 Subject: [PATCH 29/29] Improved README with reminders instructions --- README.md | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1df8568..9534b62 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,19 @@ [![Gem Version](https://badge.fury.io/rb/lita-capistrano.png)](http://badge.fury.io/rb/lita-capistrano) -**lita-capistrano** is a handler for [Lita](https://www.lita.io/) that allows you to use make deploys through your robot. +**lita-capistrano** is a handler for [Lita](https://www.lita.io/) that allows you to make deploys through your robot. + +## Features + +* Deploy and rollback. + +* Permission groups and allowed rooms for deploy based on apps. + +* Set reminders to deploy to environments after a sucessful deploy. ## Requirements -In order to **lita-capistrano** to identify a good deploy from a failed one, you capistrano script should always end with a message. +In order to **lita-capistrano** to identify a good deploy from a failed one, your capistrano script should always end with a message. ## Installation @@ -28,6 +36,10 @@ gem "lita-capistrano" * `deploy_tree` (String) – A json configuration of how deploys work. +### Optional attributes + +* `slack_api_token` (String) – The slack api token. Only necessary if using Slack reminders + ### Example ``` ruby @@ -35,6 +47,8 @@ Lita.configure do |config| config.handlers.capistrano.server = "capistrano-deploy.com" config.handlers.capistrano.server_user = "lita" config.handlers.capistrano.server_password = "secret" + config.handlers.capistrano.slack_api_token = "super-long-token-for-slack-api" # not required, if not using Slack reminders + end config.handlers.capistrano.deploy_tree = { @@ -66,7 +80,12 @@ config.handlers.capistrano.deploy_tree = { envs: [ "dc1", "dc2" - ] + ], + reminders: { + dc1: { + dc2: "2 hours" # Set a reminder to deploy to dc2 after 2 hours from the dc1 deploy success. + } + } } } ```