Skip to content

Commit

Permalink
Merge cisco_os_shim content into cisco_node_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Glenn Matthews committed Dec 9, 2015
1 parent c1dda7a commit 9715d84
Show file tree
Hide file tree
Showing 36 changed files with 2,128 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pkg/*
vendor/*
coverage
ext/Rakefile
Gemfile.lock
*.gem
*.swp
Expand Down
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
13 changes: 13 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Rubocop configuration
AllCops:
Exclude:
# Auto-generated files
- lib/cisco_node_utils/client/grpc/ems.rb
- lib/cisco_node_utils/client/grpc/ems_services.rb
# Files we don't own
- vendor/**/*

# Code complexity metrics are tracked separately for lib/ vs. tests/
# See lib/.rubocop.yml and tests/.rubocop.yml
Expand Down Expand Up @@ -40,6 +47,9 @@ Style/AlignHash:
EnforcedHashRocketStyle: table
EnforcedColonStyle: table

Style/ClassAndModuleChildren:
Enabled: false

# Template files have wildcard strings in class names
Style/ClassAndModuleCamelCase:
Exclude:
Expand Down Expand Up @@ -75,6 +85,9 @@ Style/SpaceAroundEqualsInParameterDefault:
Style/TrailingComma:
EnforcedStyleForMultiline: comma

Style/TrivialAccessors:
AllowPredicates: true

# Because an SnmpServer cannot be destroyed, we end most of the SnmpServer
# test cases by explicitly returning the configuration to default.
# Rubocop thinks this is a no-op but it isn't.
Expand Down
32 changes: 30 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
# Use Docker infrastructure
sudo: false
# We'd like to use the Docker infrastructure,
# but can't install libgrpc-dev at present in that environment. Sigh.
# sudo: false
sudo: true


language: ruby
cache: bundler
rvm:
- 2.2.2
- 2.1.6
- 2.0.0

before_install:
# sudo apt-get install libgrpc-dev doesn't work in Travis.
# So instead we compile and install from source,
# which takes about 10 minutes. Not too painful.
- sudo apt-get install build-essential autoconf libtool
- git clone https://github.com/grpc/grpc.git
- cd grpc
- git checkout release-0_12
- git submodule update --init
- make
- sudo make install
- cd src/ruby/
- gem build grpc.gemspec
- gem install --local --force grpc-*.gem
- mkdir -p ../../../vendor/cache
- gem install --force --local grpc-*.gem
- cp grpc-*.gem ../../../vendor/cache
- cd ../../..
- rm -rf grpc
# Also install net_http_unix to keep the nxapi submodule happy
- gem install net_http_unix

script:
- bundle exec rake
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ Changelog
### Added

* `Cisco::UnsupportedError` exception class, raised when a command is explicitly marked as unsupported on a particular class of nodes.
* Added client support for gRPC on IOS XR 6.0.0.
* Smart dependency installation - installing this gem will install `grpc` on IOS XR and Linux environments, but not on NX-OS environments.
* XR support for bgp_neighbor

### Changed

* Major refactor and enhancement of `CommandReference` YAML files:
- Added support for `auto_default`, `default_only`, `kind`, and `multiple`
- Added filtering by product ID (`/N7K/`) and by client type (`cli_nexus`)
* `cisco_nxapi` Gem is no longer a dependency as the NXAPI client code has been merged into this Gem.

### Fixed

* Interface:
- Correctly restore IP address when changing VRF membership
- MTU is not supported on loopback interfaces
* NXAPI - Better handling of empty output from cli_show_ascii requests (@hunner)

## [v1.1.0]

Expand Down
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in cisco_node_utils.gemspec
gemspec

# These are optional dependencies in the Gem itself,
# but we need them for Bundler to use.
gem 'net_http_unix'
gem 'grpc'
24 changes: 23 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
require 'bundler/gem_tasks'
require 'rubocop/rake_task'
require 'rake/testtask'
require 'rspec/core/rake_task'

# test task is not part of default task list,
# because it requires a node to test against
task default: %w(rubocop build)
task default: %w(rubocop spec build)

RuboCop::RakeTask.new

# Because each of the below specs requires a clean Ruby environment,
# they need to be run individually instead of as a single RSpec task.
RSpec::Core::RakeTask.new(:spec_no_clients) do |t|
t.pattern = 'spec/no_clients_spec.rb'
end
RSpec::Core::RakeTask.new(:spec_nxapi_only) do |t|
t.pattern = 'spec/nxapi_only_spec.rb'
end
RSpec::Core::RakeTask.new(:spec_grpc_only) do |t|
t.pattern = 'spec/grpc_only_spec.rb'
end
RSpec::Core::RakeTask.new(:spec_all_clients) do |t|
t.pattern = 'spec/all_clients_spec.rb'
end

task spec: [:spec_no_clients,
:spec_nxapi_only,
:spec_grpc_only,
:spec_all_clients,
]

task :build do
system 'gem build cisco_node_utils.gemspec'
end
Expand Down
4 changes: 0 additions & 4 deletions bin/git/hooks/pre-commit/validate-diffs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,3 @@ fi
git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0' | wc -c
check_rc "Rename non-ASCII file name(s) before committing"

# If there are whitespace errors, print the offending file names and fail.
git diff-index --check --cached $against --
check_rc "Fix trailing whitespace before committing"
13 changes: 8 additions & 5 deletions cisco_node_utils.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ require 'cisco_node_utils/version'
Gem::Specification.new do |spec|
spec.name = 'cisco_node_utils'
spec.version = CiscoNodeUtils::VERSION
spec.authors = ['Alex Hunsberger', 'Glenn Matthews',
'Chris Van Heuveln', 'Mike Wiebe', 'Jie Yang']
spec.authors = ['Rob Gries', 'Alex Hunsberger', 'Glenn Matthews',
'Chris Van Heuveln', 'Rich Wellum', 'Mike Wiebe',
'Jie Yang']
spec.email = 'cisco_agent_gem@cisco.com'
spec.summary = 'Utilities for management of Cisco network nodes'
spec.description = <<-EOF
Utilities for management of Cisco network nodes.
Designed to work with Puppet and Chef.
Currently supports NX-OS nodes.
Currently supports NX-OS and IOS XR nodes.
EOF
spec.license = 'Apache-2.0'
spec.homepage = 'https://github.com/cisco/cisco-network-node-utils'
Expand All @@ -27,10 +28,12 @@ Currently supports NX-OS nodes.
spec.required_ruby_version = '>= 2.0.0'
spec.required_rubygems_version = '>= 2.1.0'

spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'bundler', '~> 1.7'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop', '= 0.35.1'
spec.add_development_dependency 'simplecov', '~> 0.9'
spec.add_runtime_dependency 'cisco_os_shim', '~> 1.0.pre.dev'

spec.extensions = ['ext/mkrf_conf.rb']
end
2 changes: 0 additions & 2 deletions docs/README-develop-node-utils-APIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Please note: A virtual Nexus N9000/N3000 may be helpful for development and test
This development guide uses tools that are packaged as gems that need to be installed on your server.

```bash
gem install cisco_os_shim
gem install rake
gem install rubocop
gem install simplecov
Expand All @@ -37,7 +36,6 @@ gem install minitest
**NOTE:** If you are working from a server where you don't have admin/root privilages, use the following commands to install the gems and then update the `PATH` to include `~/.gem/ruby/x.x.x/bin`

```bash
gem install --user-install cisco_os_shim
gem install --user-install rake
gem install --user-install rubocop
gem install --user-install simplecov
Expand Down
63 changes: 63 additions & 0 deletions ext/mkrf_conf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Inspired by:
# https://github.com/ruby-debug/ruby-debug-ide/blob/master/ext/mkrf_conf.rb

# This file needs to be named mkrf_conf.rb
# so that rubygems will recognize it as a ruby extension
# file and not think it is a C extension file

require 'rubygems/specification'
require 'rubygems/dependency'
require 'rubygems/dependency_installer'

# Load up the rubygems dependency installer to install the dependencies
# we need based on the platform we are running under.
installer = Gem::DependencyInstaller.new
deps = []
begin
# Try to detect Cisco NX-OS and IOS XR environments
os = nil
if File.exist?('/etc/os-release')
cisco_release_file = nil
File.foreach('/etc/os-release') do |line|
next unless line[/^CISCO_RELEASE_INFO=/]
cisco_release_file = line[/^CISCO_RELEASE_INFO=(.*)$/, 1]
break
end
unless cisco_release_file.nil?
File.foreach(cisco_release_file) do |line|
next unless line[/^ID=/]
os = line[/^ID=(.*)$/, 1]
break
end
end
end
puts "Detected client OS as '#{os}'" unless os.nil?

# IOS XR doesn't need net_http_unix
os == 'ios_xr' || deps << Gem::Dependency.new('net_http_unix',
'~> 0.2', '>= 0.2.1')
# NX-OS doesn't need gRPC
os == 'nexus' || deps << Gem::Dependency.new('grpc', '~> 0.11')

deps.each do |dep|
installed = dep.matching_specs
if installed.empty?
puts "Installing #{dep}"
installed = installer.install dep
fail installer.errors[0] unless installer.errors.empty?
fail "Did not install #{dep}" if installed.empty?
else
puts "Found installed gems matching #{dep}:"
installed.each { |i| puts " #{i.name} (#{i.version})" }
end
end
rescue StandardError => e
puts e
puts e.backtrace.join("\n ")
exit(1)
end

# Create a dummy Rakefile to report successful 'compilation'
f = File.open(File.join(File.dirname(__FILE__), 'Rakefile'), 'w')
f.write("task :default\n")
f.close
28 changes: 28 additions & 0 deletions lib/cisco_node_utils/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) 2015 Cisco and/or its affiliates.
#
# 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.

require_relative 'client/core'

# Try to load known extensions
extensions = ['client/nxapi',
'client/grpc',
]
extensions.each do |ext|
begin
require_relative ext
rescue LoadError => e
# ignore missing client-(grpc|nxapi), they're not always required
raise unless e.message =~ /#{Regexp.escape(ext)}/
end
end
23 changes: 23 additions & 0 deletions lib/cisco_node_utils/client/core.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2015 Cisco and/or its affiliates.
#
# 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.

# Namespace for Cisco-specific code
module Cisco
# Namespace for Cisco Client shim
module Client
end
end

# Auto-load all Ruby files in the subdirectory
Dir.glob(__dir__ + '/core/*.rb') { |file| require file }
71 changes: 71 additions & 0 deletions lib/cisco_node_utils/client/core/cisco_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#
# Cisco Logger Library.
#
# January 2015, Jie Yang
#
# Copyright (c) 2015 Cisco and/or its affiliates.
#
# 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.

require 'logger'

# Module for logging in CiscoNxapi and CiscoNodeUtils. Will automatically
# tie into Puppet or Chef logging modules if available.
module CiscoLogger
module_function

# Figure out what provider logging utility we
# should use: Puppet or Chef.
# If not found use the Ruby Logger/STDOUT/INFO.
if defined? (Puppet::Util::Logging)
@@logger = Puppet # rubocop:disable Style/ClassVars
def error(string)
@@logger.err(string)
end

def warn(string)
@@logger.warning(string)
end
else
if defined? (Chef::Log)
@@logger = Chef::Log # rubocop:disable Style/ClassVars
else
@@logger = Logger.new(STDOUT) # rubocop:disable Style/ClassVars
@@logger.level = Logger::INFO

def debug_enable
@@logger.level = Logger::DEBUG
end

def debug_disable
@@logger.level = Logger::INFO
end
end

def error(string)
@@logger.error(string)
end

def warn(string)
@@logger.warn(string)
end
end

def debug(string)
@@logger.debug(string)
end

def info(string)
@@logger.info(string)
end
end # module
Loading

0 comments on commit 9715d84

Please sign in to comment.