Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Added generic call script custom DNS Plugin Gem #6119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/dns/custom/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
doc
.yardoc
1 change: 1 addition & 0 deletions plugins/dns/custom/COPYRIGHT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Copyright 2015 Red Hat, Inc. and/or its affiliates.
3 changes: 3 additions & 0 deletions plugins/dns/custom/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "https://rubygems.org"

gemspec
11 changes: 11 additions & 0 deletions plugins/dns/custom/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
35 changes: 35 additions & 0 deletions plugins/dns/custom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Configuration

This plugin provides a DNS integration for OpenShift Enterprise version 2.2.
The plugin essentially allows calling of a local script that can be used to integrate with a remote system as required. In the example the script calls local nsupdate commands.

The configuration file for the plugin is ```/etc/openshift/plugins.d/openshift-origin-dns-custom.conf```

Three variables in the configuration file define the location of the update server:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mentions three variables in the configuration file. There looks to only be one and it doesn't define the location of the update server.

# The DNS server
DNS_CUSTOM_SCRIPT_NAME="/usr/local/bin/ose-dns-custom"

# Build - generates the gem file
gem build openshift-origin-dns-custom.gemspec

# Manually Install:
gem install -V --local --install-dir /opt/rh/ruby193/root/usr/share/gems --force ./openshift-origin-dns-custom-1.0.0.gem

restorecon -Rv /opt

cp /opt/rh/ruby193/root/usr/share/gems/gems/openshift-origin-dns-custom-1.0.0/conf/openshift-origin-dns-custom.conf.example /etc/openshift/plugins.d/
cp /opt/rh/ruby193/root/usr/share/gems/gems/openshift-origin-dns-custom-1.0.0/conf/ose-dns-custom /usr/local/bin/
mv /etc/openshift/plugins.d/openshift-origin-dns-nsupdate.conf /etc/openshift/plugins.d/openshift-origin-dns-nsupdate.conf.save
cp /etc/openshift/plugins.d/openshift-origin-dns-custom.conf.example /etc/openshift/plugins.d/openshift-origin-dns-custom.conf

cp /var/named/mydomain.key /etc/openshift/
chown apache:root /etc/openshift/mydomain.key

edit /usr/local/bin/ose-dns-custom as required, for local DNS server update the domainname and keyfile name, for custom DNS change add and delete code as required
Note: if using on second broker host, remote to the DNS server, need to remove the -l from nsupdate and insert 'server <IP address>' entry.

Restart openshift-* services, broker first then console afterwards.

# Note: oo-accept-broker NOTICE
The oo-accept-broker script has a descrete list of dynamic DNS plugins in a case statement so there is a warning NOTICE raised when the broker is checked, this is just a warning from the check script and not an issue with the OpenShift::CustomDNSPlugin class not being known.

21 changes: 21 additions & 0 deletions plugins/dns/custom/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#require "bundler/gem_tasks"
require 'rake'
require 'rake/testtask'
require 'rspec/core/rake_task'
require 'rdoc/task'

task :default => [:rdoc]

desc "Run RSpec unit tests"
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = "./spec/*/*_spec.rb" # don't need this, it's default.
# make sure ruby can find the superclass and dependencies
t.ruby_opts = "-I spec/lib -I ../../../common/lib -I ../../../controller/lib -I lib"
end

desc "Generate RDoc output"
Rake::RDocTask.new do |rd|
rd.main = "README.rdoc"
rd.rdoc_dir = "doc"
rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Settings related OpenShift Enterprise custom DNS plugin

# The custom script to call to perform the DNS updates
DNS_CUSTOM_SCRIPT_NAME="/usr/local/bin/ose-dns-custom"

100 changes: 100 additions & 0 deletions plugins/dns/custom/conf/ose-dns-custom
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash

# Usage
function usage {
echo "Usage: $0 <args>"
echo
echo " --action {add|delete}"
echo " --host hostname - FQ Hostname"
echo " --cname - CNAME to add to host"
echo
exit 1
}

# options followed by one colon indicate they have a required argument
if ! options=$(getopt -o ahc: -l action:,host:,cname: -- "$@")
then
usage
fi

set -- $options

while [ $# -gt 0 ]
do
case $1 in
--action) action=`eval echo $2` ; shift ;;
--host) osehost=`eval echo $2` ; shift ;;
--cname) cname=`eval echo $2` ; shift ;;
(--) shift; break;;
(-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
(*) break;;
esac
shift
done

echo
echo [ose-dns-custom] INFO: Input Param: action = $action
echo [ose-dns-custom] INFO: Input Param: host = $osehost
echo [ose-dns-custom] INFO: Input Param: cname = $cname


if [ -z $action ] ; then
echo
echo " action is a required option with arguments: {add|delete}, exiting."
echo
usage
fi

if [ -z $osehost ] && [ "$action" = "add" ] ; then
echo
echo " host is a required option when action is add, exiting."
echo
usage
fi

if [ -z $cname ] ; then
echo
echo " cname is a required option, exiting."
echo
usage
fi


# Main

# Params for nsupdate
hostname=127.0.0.1
priv_key=/etc/openshift/example.com.key
ttl=60
zone=example.com
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these parameters be read in from a configuration file? Few actual openshift environments have named installed on the same host as a broker, so this plugin won't work for most.

They should either be read in from a configuration file or passed into the script as parameters. Normally, dns plugins establish connection information in the plugin class's initialize method. I can see why that wasn't done here, since it would be cumbersome to pass the connection information every time the script is called. If the connection information isn't passed as parameters, they should at least be read in from a configuration file.

One issue with defining the connection parameters in this script is that this file is not marked as a configuration file in the rpm spec. If a newer version of the rpm is installed, it will overwrite the modified file with the one in the newer rpm.


ret_stat=0

case $action in

add) echo [ose-dns-custom] INFO: adding CNAME $cname to HOST $osehost: update add $cname $ttl CNAME $osehost
nsupdate_out=$(echo "zone $zone
server $hostname
update add ${cname} $ttl CNAME $osehost
show
send" | nsupdate -k $priv_key -v 2>&1)
ret_stat=$?
logger "$nsupdate_out"
;;

delete) echo [ose-dns-custom] INFO: deleting CNAME $cname: update delete $cname
nsupdate_out=$(echo "zone $zone
server $hostname
update delete $cname
show
send" | nsupdate -k $priv_key -v 2>&1)
ret_stat=$?
logger "$nsupdate_out"
;;

(*) echo [ose-dns-custom] ERROR: Invalid option value for action, exiting. ; usage ;;

esac

echo [ose-dns-custom] INFO: return status=$ret_stat
exit $ret_stat
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'openshift-origin-common'

Broker::Application.configure do
conf_file = File.join(OpenShift::Config::PLUGINS_DIR, File.basename(__FILE__, '.rb') + '.conf')
if Rails.env.development?
dev_conf_file = File.join(OpenShift::Config::PLUGINS_DIR, File.basename(__FILE__, '.rb') + '-dev.conf')
if File.exist? dev_conf_file
conf_file = dev_conf_file
else
Rails.logger.info "Development configuration for #{File.basename(__FILE__, '.rb')} not found. Using production configuration."
end
end
conf = OpenShift::Config.new(conf_file)

config.dns = {
:dns_custom_script => conf.get("DNS_CUSTOM_SCRIPT_NAME", "/usr/local/bin/ose-dns-custom"),
}
end
7 changes: 7 additions & 0 deletions plugins/dns/custom/lib/custom_dns_engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'openshift-origin-controller'
require 'rails'

module OpenShift
class CustomDnsEngine < Rails::Engine
end
end
10 changes: 10 additions & 0 deletions plugins/dns/custom/lib/openshift-origin-dns-custom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "openshift-origin-common"

module OpenShift
module CustomDnsModule
require 'custom_dns_engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
end
end

require "openshift/custom_dns_plugin.rb"
OpenShift::DnsService.provider=OpenShift::CustomDNSPlugin
147 changes: 147 additions & 0 deletions plugins/dns/custom/lib/openshift/custom_dns_plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#
# Make Openshift
#
require 'rubygems'

module OpenShift

class CustomDNSPlugin < OpenShift::DnsService

@provider = OpenShift::CustomDNSPlugin

attr_reader :dns_custom_script


# Establish the parameters for a connection to the DNS update service
#
# @param access_info [Hash] communication configuration settings
#
def initialize(access_info = nil)

if access_info != nil
@domain_suffix = access_info[:domain_suffix]

elsif defined? Rails
access_info = Rails.application.config.dns
@domain_suffix = Rails.application.config.openshift[:domain_suffix]

else
raise DNSException.new("Custom DNS plugin did not initialize")
end

@dnsscript = access_info[:dns_custom_script]

end


## public

# Publish an application - create DNS record
#
# @param [String] app_name The name of the application to publish
# @param [String] namespace The namespace which contains the application
# @param [String] public_hostname The name of the location where the application resides
# @return [Object] The response from the service provider
#
def register_application(app_name, namespace, public_hostname)

# create an A record for the application in the domain
fqdn = "#{app_name}-#{namespace}.#{@domain_suffix}"
cmd = add_cmd(fqdn, public_hostname)

modify_dns(cmd, "adding", fqdn)
end


# Unpublish an application - remove DNS record
#
# @param [String] app_name The name of the application to publish
# @param [String] namespace The namespace which contains the application
# @return [Object] The response from the service provider
#
def deregister_application(app_name, namespace)

# delete the CNAME record for the application in the domain
fqdn = "#{app_name}-#{namespace}.#{@domain_suffix}"
cmd = del_cmd(fqdn)

modify_dns(cmd, "removing", fqdn)
end


# Change the published location of an application - Modify DNS record
#
# @param [String] app_name The name of the application to publish
# @param [String] namespace The namespace which contains the application
# @param [String] public_hostname The name of the location where the application resides
# @return [Object] The response from the service provider
#
def modify_application(app_name, namespace, public_hostname)

deregister_application(app_name, namespace)
register_application(app_name, namespace, public_hostname)
end


# send any queued requests to the update server
# @return [nil]
def publish
end

# close any persistent connection to the update server
# @return [nil]
def close
end


private

# Generate a DNS add command string
#
# @param fqdn [String] DNS record name to add
# @param value [String] DNS record value
# @return [String] An nsupdate command sequence
#
def add_cmd(fqdn, value)

# compose the DNS add command
cmd = "#{@dnsscript} --action add --cname #{fqdn} --host #{value} 2>&1"

end


# Generate a DNS delete command string
#
# @param fqdn [String] DNS record name to delete
# @return [String] An nsupdate command sequence
#
def del_cmd(fqdn)

# compose the DNS add command
cmd = "#{@dnsscript} --action delete --cname #{fqdn} 2>&1"

end


# Run an nsupdate command, returning a detailed error on failure
#
# @param cmd [String] Command sequence to add the DNS CNAME entry
# @param action [String] Action to be reported in log message ("adding" or "removing")
# @param fqdn FQDN of the application
#
def modify_dns(cmd, action, fqdn)

Rails.logger.info "[modify-dns]: #{action} DNS application record #{fqdn}: cmd=#{cmd}"

output = `#{cmd}`
exit_code = $?.exitstatus

if exit_code != 0
Rails.logger.error "[modify-dns]: Error #{action} DNS application record #{fqdn}: #{output}"
raise DNSException.new("[modify-dns]: Error #{action} DNS application record #{fqdn} rc=#{exit_code}")
end
end


end
end
Binary file not shown.
Loading