From 1a6830d68090c8539a530eaf88dab0f6130ed98e Mon Sep 17 00:00:00 2001 From: Dave Long Date: Tue, 20 Jun 2017 11:05:54 -0400 Subject: [PATCH 1/3] Adds whois command with several improvements * Implements `whois` command * Adds a Rakefile for easy building of project * Adds a README with overview of commands * Includes all shell scripts in the Docker image --- Dockerfile | 2 +- README.md | 33 +++++++++++++++++++++++++ Rakefile | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ config.yaml | 31 ++++++++++++++++++++--- whois.sh | 30 ++++++++++++++++++++++ 5 files changed, 162 insertions(+), 5 deletions(-) create mode 100644 README.md create mode 100644 Rakefile create mode 100755 whois.sh diff --git a/Dockerfile b/Dockerfile index 228883e..50aa746 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,5 +4,5 @@ RUN apk -U add bind-tools bash RUN mkdir /bundle WORKDIR /bundle -COPY dig.sh /bundle/ +COPY *.sh /bundle/ RUN chmod +x /bundle/*.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..1364751 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# net - Simple network utilities (0.3.3) + + + +## Installation + +In chat: + +``` +@cog bundle install jumpcloud +``` + +Via cogctl: + +``` +cogctl bundle install jumpcloud +``` + +For more details about how to install and configure bundles see: + +* [Installing Bundles](https://cog-book.operable.io/#_installing_bundles) +* [Dynamic Command Configuration](https://cog-book.operable.io/#_dynamic_command_configuration) + +## Commands + +The following commands are included with the bundle. For usage info +about each command see the `help` builtin command: `help net:`. + +* `dig` + > DNS lookup utility + +* `whois` + > Whois query utility diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..72a2f7e --- /dev/null +++ b/Rakefile @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'yaml' + +# Make sure we're running from the directory the Rakefile lives in since +# many things (ie: docker build, config reading) use relative paths. +Dir.chdir(File.dirname(__FILE__)) + +BUNDLE_CONFIG = YAML.safe_load(File.read('config.yaml')) +BUNDLE_VERSION = BUNDLE_CONFIG['version'] +BUNDLE_IMAGE = "#{BUNDLE_CONFIG['docker']['image']}:#{BUNDLE_CONFIG['docker']['tag']}" + +task :image do |_t| + sh 'docker', 'build', '-t', BUNDLE_IMAGE, '.' +end + +task push: [:image] do |_t| + sh 'docker', 'push', BUNDLE_IMAGE +end + +task release: [:push] do |_t| + sh 'git', 'tag', BUNDLE_VERSION + sh 'git', 'push', 'origin', BUNDLE_VERSION +end + +task install: [:push] do |_t| + sh 'cogctl', 'bundle', 'install', 'config.yaml', '--enable', '--relay-group', + 'default', '--force' +end + +task :readme do |_t| + name = BUNDLE_CONFIG['name'] + description = BUNDLE_CONFIG['description'] + version = BUNDLE_CONFIG['version'] + long_description = BUNDLE_CONFIG['long_description'] + commands = BUNDLE_CONFIG['commands'].map { |n, c| "* `#{n}`\n > #{c['description']}" }.join("\n\n") + + readme = <<~END + # #{name} - #{description} (#{version}) + + #{long_description} + + ## Installation + + In chat: + + ``` + @cog bundle install jumpcloud + ``` + + Via cogctl: + + ``` + cogctl bundle install jumpcloud + ``` + + For more details about how to install and configure bundles see: + + * [Installing Bundles](https://cog-book.operable.io/#_installing_bundles) + * [Dynamic Command Configuration](https://cog-book.operable.io/#_dynamic_command_configuration) + + ## Commands + + The following commands are included with the bundle. For usage info + about each command see the `help` builtin command: `help #{name}:`. + + #{commands} + END + + File.write('README.md', readme) +end diff --git a/config.yaml b/config.yaml index 94386d3..60d123c 100644 --- a/config.yaml +++ b/config.yaml @@ -4,10 +4,10 @@ name: net description: "Simple network utilities" author: "Operable " homepage: "https://github.com/cogcmd/net" -version: "0.2" +version: "0.3.3" docker: image: "cogcmd/net" - tag: "0.2" + tag: "0.3.3" commands: dig: executable: "/bundle/dig.sh" @@ -36,11 +36,34 @@ commands: description: "Enable the 'trace' query option. Disabled by default" type: bool required: false + whois: + executable: "/bundle/whois.sh" + description: "Whois query utility" + long_description: | + Simple wrapper for the `whois` command line utility. + arguments: "" + rules: + - "when command is net:whois allow" + options: + server: + description: "Whois server to query against." + type: string + required: false + port: + description: "Port on whois server" + type: string + required: false + short: + description: "Returns a terse answer only containing contact information" + type: bool + required: false templates: dig: body: | ~each var=$results~ - ```~each var=$item.body as=l~ + ``` + ~each var=$item.body as=l~ ~$l~ - ~end~``` + ~end~ + ``` ~end~ diff --git a/whois.sh b/whois.sh new file mode 100755 index 0000000..4a6d0f4 --- /dev/null +++ b/whois.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +declare -a arguments + +if [ "$COG_ARGV_0" == "" ]; then + echo "Error: You must supply a domain to query" +fi + +if [ "$COG_OPT_SERVER" != "" ]; then + arguments+=("-h") + arguments+=("${COG_OPT_SERVER}") +fi + +if [ "$COG_OPT_PORT" != "" ]; then + arguments+=("-p") + arguments+=("${COG_OPT_PORT}") +fi + +# Set template to use for rendering output +echo "COG_TEMPLATE: dig" + +# Run the whois command. +result="$(whois "${arguments[@]}" "$COG_ARGV_0")" + +echo $COG_OPT_SHORT +if [ "$COG_OPT_SHORT" != "" ]; then + echo -e "${result}" | grep -E "^(Registrant|Admin|Tech)" +else + echo -e "${result}" +fi From b0b5a1adb3c005680689741643988ed8f4ef343e Mon Sep 17 00:00:00 2001 From: Dave Long Date: Tue, 20 Jun 2017 11:16:57 -0400 Subject: [PATCH 2/3] Fixes bundle name in README --- README.md | 4 ++-- Rakefile | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1364751..b264388 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,13 @@ In chat: ``` -@cog bundle install jumpcloud +@cog bundle install net ``` Via cogctl: ``` -cogctl bundle install jumpcloud +cogctl bundle install net ``` For more details about how to install and configure bundles see: diff --git a/Rakefile b/Rakefile index 72a2f7e..c7cec46 100644 --- a/Rakefile +++ b/Rakefile @@ -45,13 +45,13 @@ task :readme do |_t| In chat: ``` - @cog bundle install jumpcloud + @cog bundle install #{name} ``` Via cogctl: ``` - cogctl bundle install jumpcloud + cogctl bundle install #{name} ``` For more details about how to install and configure bundles see: From 6efa1c711c05820e3a96df66c463d820867a2c68 Mon Sep 17 00:00:00 2001 From: Dave Long Date: Sat, 24 Jun 2017 21:13:46 -0400 Subject: [PATCH 3/3] Fix generalization of Rakefile --- Rakefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index c7cec46..5975177 100644 --- a/Rakefile +++ b/Rakefile @@ -25,7 +25,7 @@ end task install: [:push] do |_t| sh 'cogctl', 'bundle', 'install', 'config.yaml', '--enable', '--relay-group', - 'default', '--force' + 'default', '--force' end task :readme do |_t| @@ -33,6 +33,7 @@ task :readme do |_t| description = BUNDLE_CONFIG['description'] version = BUNDLE_CONFIG['version'] long_description = BUNDLE_CONFIG['long_description'] + env = BUNDLE_CONFIG.fetch('config', {}).fetch('env', []).map { |e| "* `#{e['var']}`\n > #{e['description']}" }.join("\n\n") commands = BUNDLE_CONFIG['commands'].map { |n, c| "* `#{n}`\n > #{c['description']}" }.join("\n\n") readme = <<~END