From 3bec4feb49b00d22fc52f986a2687b662bea5769 Mon Sep 17 00:00:00 2001 From: Kevin Meaney Date: Sat, 12 Mar 2016 18:05:14 +0000 Subject: [PATCH 1/9] Added delete bot command. Minor bug fixes. --- bin/xcskarel | 24 +++++++++++++++++++----- lib/xcskarel/application.rb | 6 ++++++ lib/xcskarel/server.rb | 19 ++++++++++++++++++- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/bin/xcskarel b/bin/xcskarel index faeab94..57a6581 100755 --- a/bin/xcskarel +++ b/bin/xcskarel @@ -107,8 +107,8 @@ class XCSKarelApplication command :bots do |c| c.syntax = 'xcskarel bots [options]' c.description = 'Fetches all Bots found on the specified server' - c.example 'get all bot names & identifiers', 'xcskarel bots --hostname 127.0.0.1' - c.example 'get all bot metadata', 'xcskarel bots --no_filter --hostname 127.0.0.1' + c.example 'get all bot names & identifiers', 'xcskarel bots --host 127.0.0.1' + c.example 'get all bot metadata', 'xcskarel bots --no_filter --host 127.0.0.1' add_xcs_options(c) c.action do |args, options| server = create_server_from_options(options) @@ -117,10 +117,24 @@ class XCSKarelApplication end end + command :'delete bot' do |c| + c.syntax = 'xcskarel delete bot [options]' + c.description = 'Deletes the bot specified by bot name or bot id' + c.example 'delete the bot on host with name or id', 'xcskarel delete bot --host 127.0.0.1 --bot "My Bot"' + add_xcs_options(c) + add_bot_options(c) + c.action do |args, options| + raise "No Bot id was specified, please see `xcskarel delete bot --help`" unless options.bot + server = create_server_from_options(options) + delete_result = XCSKarel::Application.delete_bot(server, options.bot) + puts delete_result + end + end + command :integrations do |c| c.syntax = 'xcskarel integrations [options]' c.description = 'Fetches all Integrations for a specified Bot identifier' - c.example 'get all integration identifiers, numbers & results', 'xcskarel integrations --bot BOT_ID --hostname 127.0.0.1' + c.example 'get all integration identifiers, numbers & results', 'xcskarel integrations --bot BOT_ID --host 127.0.0.1' add_xcs_options(c) add_bot_options(c) c.action do |args, options| @@ -178,7 +192,7 @@ class XCSKarelApplication command :health do |c| c.syntax = 'xcskarel health [options]' c.description = 'Fetches health information of the specified server' - c.example 'get health', 'xcskarel health --hostname 127.0.0.1' + c.example 'get health', 'xcskarel health --host 127.0.0.1' add_xcs_options(c) c.action do |args, options| server = create_server_from_options(options) @@ -190,7 +204,7 @@ class XCSKarelApplication command :status do |c| c.syntax = 'xcskarel status [options]' c.description = 'Fetches status information of the specified server' - c.example 'get status', 'xcskarel status --hostname 127.0.0.1' + c.example 'get status', 'xcskarel status --host 127.0.0.1' add_xcs_options(c) c.action do |args, options| server = create_server_from_options(options) diff --git a/lib/xcskarel/application.rb b/lib/xcskarel/application.rb index f55aad1..27fa242 100644 --- a/lib/xcskarel/application.rb +++ b/lib/xcskarel/application.rb @@ -100,6 +100,12 @@ def self.integrations(server, bot_id_or_name) server.get_integrations(bot['_id']) end + def self.delete_bot(server, bot_id_or_name) + bot = server.find_bot_by_id_or_name(bot_id_or_name) + XCSKarel.log.debug "Found Bot #{bot['name']} with id #{bot['_id']}".yellow + server.delete_bot(bot) + end + def self.integrate(server, bot_id_or_name) # find bot by id or name diff --git a/lib/xcskarel/server.rb b/lib/xcskarel/server.rb index 0afb40e..de4b95e 100644 --- a/lib/xcskarel/server.rb +++ b/lib/xcskarel/server.rb @@ -90,11 +90,22 @@ def integrate(bot) integration = JSON.parse(integration) XCSKarel.log.info "Successfully started integration #{integration['number']} on Bot \"#{bot['name']}\"".green else - raise "Failed to integrate Bot #{bot_id}".red + raise "Failed to integrate Bot #{bot['name']}".red end return integration end + def delete_bot(bot) + response = delete_endpoint("/bots/#{bot['_id']}", nil) + delete = response.body + if response.status == 204 + XCSKarel.log.info "Successfully deleted Bot \"#{bot['name']}\"".green + else + raise "Failed to delete Bot #{bot['name']}".red + end + return delete + end + def find_bot_by_id_or_name(id_or_name, raise_if_none_found=true) bots = self.get_bots found_bots = bots.select { |bot| [bot['name'], bot['_id']].index(id_or_name) != nil } @@ -125,6 +136,10 @@ def post_endpoint(endpoint, body) call_endpoint("post", endpoint, body) end + def delete_endpoint(endpoint, body) + call_endpoint("delete", endpoint, body) + end + private def call_endpoint(method, endpoint, body) @@ -135,6 +150,8 @@ def call_endpoint(method, endpoint, body) response = Excon.get(url, headers: headers) when "post" response = Excon.post(url, headers: headers, body: body) + when "delete" + response = Excon.delete(url, headers: headers, body: body) else raise "Unrecognized method #{method}" end From 1c630f64f8d90ee4de1b7227da46fd8c23030055 Mon Sep 17 00:00:00 2001 From: Kevin Meaney Date: Sun, 13 Mar 2016 15:04:09 +0000 Subject: [PATCH 2/9] Removing body as it will never be used. --- lib/xcskarel/server.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/xcskarel/server.rb b/lib/xcskarel/server.rb index de4b95e..94494ed 100644 --- a/lib/xcskarel/server.rb +++ b/lib/xcskarel/server.rb @@ -96,7 +96,7 @@ def integrate(bot) end def delete_bot(bot) - response = delete_endpoint("/bots/#{bot['_id']}", nil) + response = delete_endpoint("/bots/#{bot['_id']}") delete = response.body if response.status == 204 XCSKarel.log.info "Successfully deleted Bot \"#{bot['name']}\"".green @@ -136,8 +136,8 @@ def post_endpoint(endpoint, body) call_endpoint("post", endpoint, body) end - def delete_endpoint(endpoint, body) - call_endpoint("delete", endpoint, body) + def delete_endpoint(endpoint) + call_endpoint("delete", endpoint, nil) end private From 581c5b84a31635299814c8cfcc9ac6254b961f65 Mon Sep 17 00:00:00 2001 From: Kevin Meaney Date: Sun, 13 Mar 2016 22:14:41 +0000 Subject: [PATCH 3/9] Updated rspec to 3.4.0 in the xcskarel.gemspec file. --- xcskarel.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xcskarel.gemspec b/xcskarel.gemspec index 3332521..a192ae0 100644 --- a/xcskarel.gemspec +++ b/xcskarel.gemspec @@ -33,7 +33,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'pry-byebug', '3.2.0' # better debugging s.add_development_dependency 'bundler' s.add_development_dependency 'rake' - s.add_development_dependency 'rspec', '~> 3.1.0' + s.add_development_dependency 'rspec', '~> 3.4.0' s.add_development_dependency 'fastlane' s.add_development_dependency 'github_changelog_generator' From 17b500db104354c0a38f623c2ba311d22f4f1b21 Mon Sep 17 00:00:00 2001 From: Kevin Meaney Date: Sun, 13 Mar 2016 22:23:44 +0000 Subject: [PATCH 4/9] Updated gem file lock. --- Gemfile.lock | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f1b3add..4d5b6f1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -208,18 +208,19 @@ GEM http-cookie (>= 1.0.2, < 2.0) mime-types (>= 1.16, < 3.0) netrc (~> 0.7) - rspec (3.1.0) - rspec-core (~> 3.1.0) - rspec-expectations (~> 3.1.0) - rspec-mocks (~> 3.1.0) - rspec-core (3.1.7) - rspec-support (~> 3.1.0) - rspec-expectations (3.1.2) + rspec (3.4.0) + rspec-core (~> 3.4.0) + rspec-expectations (~> 3.4.0) + rspec-mocks (~> 3.4.0) + rspec-core (3.4.4) + rspec-support (~> 3.4.0) + rspec-expectations (3.4.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.1.0) - rspec-mocks (3.1.3) - rspec-support (~> 3.1.0) - rspec-support (3.1.2) + rspec-support (~> 3.4.0) + rspec-mocks (3.4.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.4.0) + rspec-support (3.4.1) rubyzip (1.1.7) security (0.1.3) sigh (0.10.6) @@ -270,8 +271,8 @@ DEPENDENCIES pry (= 0.10.1) pry-byebug (= 3.2.0) rake - rspec (~> 3.1.0) + rspec (~> 3.4.0) xcskarel! BUNDLED WITH - 1.10.6 + 1.11.2 From a77e6a49d1f5921d16ff26158f3d5be4410e08f1 Mon Sep 17 00:00:00 2001 From: Kevin Meaney Date: Mon, 14 Mar 2016 10:41:17 +0000 Subject: [PATCH 5/9] Updated version number. --- lib/xcskarel/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/xcskarel/version.rb b/lib/xcskarel/version.rb index f274eb6..e27c0e0 100644 --- a/lib/xcskarel/version.rb +++ b/lib/xcskarel/version.rb @@ -1,3 +1,3 @@ module XCSKarel - VERSION = '0.16.1' + VERSION = '0.16.2' end From e334111bb6c5a437f1aa5bc9a120e0bdf6f8c550 Mon Sep 17 00:00:00 2001 From: Kevin Meaney Date: Mon, 14 Mar 2016 18:36:15 +0000 Subject: [PATCH 6/9] Updating gem lock file --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 4d5b6f1..be9fee7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - xcskarel (0.16.1) + xcskarel (0.16.2) colored (= 1.2) commander (= 4.3.5) excon (= 0.45.4) From abbb2f0d29b7ac5ae1712fc16cfc00890935a2fa Mon Sep 17 00:00:00 2001 From: Kevin Meaney Date: Sun, 20 Mar 2016 12:16:20 +0000 Subject: [PATCH 7/9] Updated gem file --- Gemfile.lock | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 8723f72..1e9eb62 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -214,18 +214,19 @@ GEM byebug (~> 5.0) pry (~> 0.10) rack (1.6.4) + rack (>= 1.0) rake (11.1.1) representable (2.3.0) uber (~> 0.0.7) retriable (2.1.0) rouge (1.10.1) rspec (3.1.0) - rspec-core (~> 3.1.0) - rspec-expectations (~> 3.1.0) - rspec-mocks (~> 3.1.0) - rspec-core (3.1.7) - rspec-support (~> 3.1.0) - rspec-expectations (3.1.2) + rspec-core (~> 3.4.0) + rspec-expectations (~> 3.4.0) + rspec-mocks (~> 3.4.0) + rspec-core (3.4.4) + rspec-support (~> 3.4.0) + rspec-expectations (3.4.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.4.0) rspec-mocks (3.4.1) From 660d6f758f9075ae4060afe66ce0072d8970fc47 Mon Sep 17 00:00:00 2001 From: Kevin Meaney Date: Sun, 20 Mar 2016 12:58:45 +0000 Subject: [PATCH 8/9] Lets see if undoing my wrong dependency fix is now the fix. --- Gemfile.lock | 3 +-- xcskarel.gemspec | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 1e9eb62..4ab84ff 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -214,7 +214,6 @@ GEM byebug (~> 5.0) pry (~> 0.10) rack (1.6.4) - rack (>= 1.0) rake (11.1.1) representable (2.3.0) uber (~> 0.0.7) @@ -303,7 +302,7 @@ DEPENDENCIES pry (= 0.10.1) pry-byebug (= 3.2.0) rake - rspec (~> 3.4.0) + rspec (~> 3.1.0) xcskarel! BUNDLED WITH diff --git a/xcskarel.gemspec b/xcskarel.gemspec index a192ae0..3332521 100644 --- a/xcskarel.gemspec +++ b/xcskarel.gemspec @@ -33,7 +33,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'pry-byebug', '3.2.0' # better debugging s.add_development_dependency 'bundler' s.add_development_dependency 'rake' - s.add_development_dependency 'rspec', '~> 3.4.0' + s.add_development_dependency 'rspec', '~> 3.1.0' s.add_development_dependency 'fastlane' s.add_development_dependency 'github_changelog_generator' From 27b06d6f005eac2c31c91476cd4a9721feeda8ae Mon Sep 17 00:00:00 2001 From: Kevin Meaney Date: Sun, 20 Mar 2016 18:48:16 +0000 Subject: [PATCH 9/9] Duplicate bot kind of works, but code still needs cleaning up and fixing. --- bin/xcskarel | 32 ++++++++++++++++++++++++++++++++ lib/xcskarel/application.rb | 6 ++++++ lib/xcskarel/server.rb | 22 +++++++++++++++++++++- 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/bin/xcskarel b/bin/xcskarel index 57a6581..c486b45 100755 --- a/bin/xcskarel +++ b/bin/xcskarel @@ -131,6 +131,38 @@ class XCSKarelApplication end end + command :'duplicate bot' do |c| + c.syntax = 'xcskarel duplicate bot [options]' + c.description = 'Duplicates the bot by bot name or bot id with possible modifications' + c.example 'duplicate the bot on host with name', 'xcskarel duplicate bot --host 127.0.0.1 --bot "Useful bot" --config path/to/config.json' + c.option '--newname "New bot name"', 'The name for the newly duplicated bot' + c.option '--config FILE', 'A JSON file with configuration information for the duplicated bot' + add_xcs_options(c) + add_bot_options(c) + c.action do |args, options| + raise "No Bot id was specified, please see `xcskarel duplicate bot --help`" unless options.bot + server = create_server_from_options(options) + body = nil + body = { name: options.newname } unless options.newname.nil? + filePath = nil + filePath = File.expand_path(options.config) unless options.config.nil? + fileContents = nil + unless filePath.nil? + fileContents = File.read(filePath) if File.exists?(filePath) + end + unless fileContents.nil? || fileContents.empty? + if body.nil? + body = { configuration: JSON.parse(fileContents) } + else + body['configuration'] = JSON.parse(fileContents) + end + end + puts body + duplicate_result = XCSKarel::Application.duplicate_bot(server, options.bot, body.to_json) + puts XCSKarel::Application.format(duplicate_result, options, ['name', '_id']) + end + end + command :integrations do |c| c.syntax = 'xcskarel integrations [options]' c.description = 'Fetches all Integrations for a specified Bot identifier' diff --git a/lib/xcskarel/application.rb b/lib/xcskarel/application.rb index 27fa242..b146bdd 100644 --- a/lib/xcskarel/application.rb +++ b/lib/xcskarel/application.rb @@ -106,6 +106,12 @@ def self.delete_bot(server, bot_id_or_name) server.delete_bot(bot) end + def self.duplicate_bot(server, bot_id_or_name, body) + bot = server.find_bot_by_id_or_name(bot_id_or_name) + XCSKarel.log.debug "Found Bot #{bot['name']} with id #{bot['_id']}".yellow + server.duplicate_bot(bot, body) + end + def self.integrate(server, bot_id_or_name) # find bot by id or name diff --git a/lib/xcskarel/server.rb b/lib/xcskarel/server.rb index 94494ed..05ae807 100644 --- a/lib/xcskarel/server.rb +++ b/lib/xcskarel/server.rb @@ -106,6 +106,18 @@ def delete_bot(bot) return delete end + def duplicate_bot(bot, body) + # puts body + response = post_endpoint("/bots/#{bot['_id']}/duplicate", body) + duplicate = response.body + if response.status == 201 + XCSKarel.log.info "Successfully duplicated Bot \"#{bot['name']}\"".green + else + raise "Failed to duplicate Bot #{bot['name']}".red + end + return JSON.parse(duplicate) + end + def find_bot_by_id_or_name(id_or_name, raise_if_none_found=true) bots = self.get_bots found_bots = bots.select { |bot| [bot['name'], bot['_id']].index(id_or_name) != nil } @@ -140,6 +152,10 @@ def delete_endpoint(endpoint) call_endpoint("delete", endpoint, nil) end + def patch_endpoint(endpoint, body) + call_endpoint("patch", endpoint, body) + end + private def call_endpoint(method, endpoint, body) @@ -149,9 +165,13 @@ def call_endpoint(method, endpoint, body) when "get" response = Excon.get(url, headers: headers) when "post" - response = Excon.post(url, headers: headers, body: body) + local_headers = headers + local_headers['Content-Type'] = 'application/json' unless body.nil? + response = Excon.post(url, headers: local_headers, body: body) when "delete" response = Excon.delete(url, headers: headers, body: body) + when "patch" + response = Excon.patch(url, headers: headers, body: body) else raise "Unrecognized method #{method}" end