Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic filename generation #3222

Open
wants to merge 4 commits 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
5 changes: 1 addition & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ inherit_from: .rubocop_todo.yml
# Do not attempt to police vendored code
AllCops:
NewCops: enable
TargetRubyVersion: 3.1
TargetRubyVersion: 3.0
Exclude:
- 'vendor/**/*'

Expand Down Expand Up @@ -52,9 +52,6 @@ Style/FormatStringToken:
Style/HashEachMethods:
Enabled: true

Style/HashSyntax:
EnforcedShorthandSyntax: either

Style/HashTransformKeys:
Enabled: true

Expand Down
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-07-05 13:13:46 UTC using RuboCop version 1.64.1.
# on 2024-02-27 14:27:59 UTC using RuboCop version 1.60.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 28
# Offense count: 27
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes, Max.
Metrics/AbcSize:
Enabled: false
Expand Down Expand Up @@ -72,7 +72,7 @@ Style/OpenStructUse:
- 'lib/oxidized/node.rb'
- 'spec/hook/githubrepo_spec.rb'

# Offense count: 48
# Offense count: 47
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, AllowInnerSlashes.
# SupportedStyles: slashes, percent_r, mixed
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Added
- model for Siklu Multihaul TG radios (@bdg-robert)
- fortios: variable `fullconfig` to get the configuration with default values. Fixes: #3159 (@robertcheramy)
- Add capability to customize the filename in output file
- model for VMWare NSX DFW (@elmobp)
- model for F5OS (@teunvink)

Expand Down
33 changes: 33 additions & 0 deletions docs/Outputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@ output:
file:
directory: /var/lib/oxidized/configs
```
It is possibile to customize the filename using the variables provided by the source, adding the fileformat variable.

```yaml
output:
file:
directory: /var/lib/oxidized/configs
filename: "custom_{{user}}_{{vars_custom1}}_{{time}}"
```
The variables passed from the source in the vars_map will be mapped in 'vars_<name>'.
By default, the '{{time}}' variable will be added with the '%m%d%Y%H%M%S' format.
You can change the format using the timeformat configuration field.

Complete example of configuration
```
output:
file:
directory: /var/lib/oxidized/configs
filename: "custom_{{user}}_{{vars_custom1}}_{{vars_fileformat}}_{{time}}"
timeformat: "%Y%m%d%H%M%S"

source:
default: csv
csv:
file: /etc/oxidized/devices.csv
delimiter: !ruby/regexp /:/
map:
name: 0
model: 1
user: 2
vars_map:
custom1: 3

```

## Output: Git

Expand Down
5 changes: 4 additions & 1 deletion lib/oxidized/hook/exec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'json'

class Exec < Oxidized::Hook
include Process

Expand Down Expand Up @@ -70,7 +72,8 @@ def make_env(ctx)
"OX_REPO_COMMITREF" => ctx.commitref.to_s,
"OX_REPO_NAME" => ctx.node.repo.to_s,
"OX_ERR_TYPE" => ctx.node.err_type.to_s,
"OX_ERR_REASON" => ctx.node.err_reason.to_s
"OX_ERR_REASON" => ctx.node.err_reason.to_s,
"OX_NODE_VARS" => JSON.dump(ctx.node.vars)
)
end
if ctx.job
Expand Down
61 changes: 56 additions & 5 deletions lib/oxidized/output/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ class OxidizedFile < Output
def initialize
super
@cfg = Oxidized.config.output.file
Oxidized.logger.debug "Config #{@cfg.filename}"
end

def setup
unless @cfg.filename.nil?
if @cfg.filename.include?("/")
raise NoConfig, 'filename must not have "/"'
end
end
return unless @cfg.empty?

Oxidized.asetus.user.output.file.directory = File.join(Config::ROOT, 'configs')
Expand All @@ -18,27 +24,29 @@ def setup
end

def store(node, outputs, opt = {})
filename = get_filename(node, opt)

file = File.expand_path @cfg.directory
file = File.join File.dirname(file), opt[:group] if opt[:group]
FileUtils.mkdir_p file
file = File.join file, node
file = File.join file, filename
File.write(file, outputs.to_cfg)
@commitref = file
end

def fetch(node, group)
cfg_dir = File.expand_path @cfg.directory
node_name = node.name

node_name = get_filename node.name, email: node.email, user: node.user, group: node.group, vars: node.vars , mode: "fetch"
if group # group is explicitly defined by user
cfg_dir = File.join File.dirname(cfg_dir), group
File.read File.join(cfg_dir, node_name)
path = Dir.glob(File.join(cfg_dir, node_name)).max_by {|f| File.mtime(f)}
elsif File.exist? File.join(cfg_dir, node_name) # node configuration file is stored on base directory
File.read File.join(cfg_dir, node_name)
path = Dir.glob(File.join(cfg_dir, node_name)).max_by {|f| File.mtime(f)}
else
path = Dir.glob(File.join(File.dirname(cfg_dir), '**', node_name)).first # fetch node in all groups
File.read path
end
File.read path
rescue Errno::ENOENT
nil
end
Expand All @@ -51,5 +59,48 @@ def version(_node, _group)
def get_version(_node, _group, _oid)
'not supported'
end

def interpolate_format_string(format_string, vars)
vars.each do |key, value|
format_string = format_string.gsub("{{#{key}}}", value.to_s)
end
format_string
end

def flatten_variables(hash, parent_key = '', separator = '_')
flat_hash = {}
hash.each do |key, value|
new_key = parent_key.empty? ? key.to_s : "#{parent_key}#{separator}#{key}"
if value.is_a?(Hash)
flat_hash.merge!(flatten_variables(value, new_key, separator))
else
flat_hash[new_key] = value
end
end
flat_hash
end

def get_filename(node, opt, mode = 'store')
filename = node
Oxidized.logger.debug "OPT Received: #{node} #{opt}"
unless @cfg.filename.nil?
Oxidized.logger.debug "Filename from config #{@cfg.filename}"
flatten_ops = flatten_variables(opt)
if mode == 'store'
if @cfg.timeformat.nil || @cfg.timeformat.empty
time = Time.now.strftime("%m%d%Y%H%M%S")
else
time = Time.now.strftime(@cfg.timeformat)
end
flatten_ops["time"] = time
elsif mode == 'fetch'
flatten_ops["time"] = "*"
end
Oxidized.logger.debug "Flatten #{flatten_ops}"
filename = interpolate_format_string(@cfg.filename, flatten_ops)
end
Oxidized.logger.debug "Filename #{filename}"
filename
end
end
end
2 changes: 1 addition & 1 deletion lib/oxidized/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def process_success(node, job)
msg += " with message '#{node.msg}'" if node.msg
output = node.output.new
if output.store node.name, job.config,
msg: msg, email: node.email, user: node.user, group: node.group
msg: msg, email: node.email, user: node.user, group: node.group, vars: node.vars
node.modified
Oxidized.logger.info "Configuration updated for #{node.group}/#{node.name}"
Oxidized.hooks.handle :post_store, node: node,
Expand Down
Loading