Class: RunSSHLib::ConfigFile
- Inherits:
-
Object
- Object
- RunSSHLib::ConfigFile
- Defined in:
- lib/runsshlib/config_file.rb
Overview
Handles configuration file for the application.
The configuration consists of nested hashes which keys either points to another hash or to host definition.
The configuration file should use Marshal to save/load configuration, but should also be able to import/export to/from yaml file.
Constant Summary collapse
- Version =
Config version
1.0
Instance Method Summary collapse
-
#add_host_def(path, name, host_def) ⇒ Object
Add host definition to config file.
-
#delete_path(path) ⇒ Object
This will delete any path if it’s a host definition or an empty group.
-
#export(file) ⇒ Object
Export config as YAML to the supplied file.
-
#get_host(path) ⇒ Object
Returns the host definition in the specified path.
-
#import(file) ⇒ Object
Import config from YAML from the specified file.
-
#initialize(config_file, old_version = false) ⇒ ConfigFile
constructor
Initialize new ConfigFile.
-
#list_groups(path) ⇒ Object
List all available sub groups inside path.
-
#update_config ⇒ Object
Spacial case - perform update to the configuration.
-
#update_host_def(path, host_def) ⇒ Object
Update host definition (host_def) at the specified path.
Constructor Details
#initialize(config_file, old_version = false) ⇒ ConfigFile
Initialize new ConfigFile. Uses supplied config_file or the default ‘~/.runssh’. If file doesn’t exist, it issues a warning and creates a new empty one.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/runsshlib/config_file.rb', line 35 def initialize(config_file, old_version=false) @config_file = config_file if File.exists? config_file File.open(config_file) { |io| @config = Marshal.load(io) } if ! @config['VERSION'] raise OlderConfigVersionError, 'none' unless old_version elsif @config['VERSION'] > Version # This is for the future, to avoid reading more advanced # configuration version in an old runssh version error = "The configuration file is for a newer version of runssh!" raise ConfigError, error end else # warn "Config file not found. It must be the first time you run this app..." @config = Hash.new @config['VERSION'] = Version save end end |
Instance Method Details
#add_host_def(path, name, host_def) ⇒ Object
Add host definition to config file.
- path
-
An array of symbols that represent the path for the host. e.g, [:client, :datacenter1].
- name
-
The name of the host definition as symbol.
- host_def
-
A SshHostDef instance.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/runsshlib/config_file.rb', line 61 def add_host_def(path, name, host_def) # sanity raise ConfigError.new('Invalid host definition') unless host_def.instance_of? SshHostDef k = path.inject(@config) do |hsh, key| if hsh.include? key if hsh[key].instance_of? SshHostDef raise ConfigError.new('Cannot override host definition with path!') end hsh[key] else hsh[key] = {} end end raise ConfigError.new('path already exist!') if k.include? name k[name] = host_def save end |
#delete_path(path) ⇒ Object
This will delete any path if it’s a host definition or an empty group.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/runsshlib/config_file.rb', line 131 def delete_path(path) # we need access to the delete key, not just the value mykey = path.pop value = retrieve_path(path, 'Invalid path!') raise ConfigError.new('Invalid path!') unless value if value[mykey].instance_of? SshHostDef or value[mykey] == {} value.delete(mykey) elsif not value[mykey] raise ConfigError.new('Invalid path!') else raise ConfigError.new('Supplied path is non-empty group!') end save end |
#export(file) ⇒ Object
Export config as YAML to the supplied file.
160 161 162 163 |
# File 'lib/runsshlib/config_file.rb', line 160 def export(file) require 'yaml' File.open(file, 'w') { |out| YAML.dump(@config, out) } end |
#get_host(path) ⇒ Object
Returns the host definition in the specified path.
- path
-
is an array of symbols which translates to nested hash keys.
- Raises
-
ConfigError if not found or if path points to a group.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/runsshlib/config_file.rb', line 107 def get_host(path) host = retrieve_path(path, %Q{host definition (#{path.join(' => ')}) doesn't exist!}) if not host raise ConfigError.new(%Q{host definition (#{path.join(' => ')}) doesn't exist!}) elsif host.instance_of? Hash raise ConfigError.new(%Q("#{path.join(' => ')}" is a group, not host definition!)) end host end |
#import(file) ⇒ Object
Import config from YAML from the specified file.
149 150 151 152 153 154 155 156 157 |
# File 'lib/runsshlib/config_file.rb', line 149 def import(file) require 'yaml' config = YAML.load_file(file) raise ConfigError, "The imported file is from a different version of " + "runssh (config: #{config['VERSION']})! aborting." unless config['VERSION'] == Version @config = config save end |
#list_groups(path) ⇒ Object
List all available sub groups inside path.
120 121 122 123 124 125 126 127 |
# File 'lib/runsshlib/config_file.rb', line 120 def list_groups(path) value = retrieve_path(path, 'Invalid path!') if value.instance_of? Hash value.keys.reject { |i| i == 'VERSION' } else [] end end |
#update_config ⇒ Object
Spacial case - perform update to the configuration. This should later include handling of all
versions of the config!
Returns the name of the backup file or nil if there was no need for backup.
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/runsshlib/config_file.rb', line 170 def update_config return if @config['VERSION'] == Version backup_file = @config_file + '.none' require 'fileutils' new_config = config_none_to_10(@config) FileUtils.move(@config_file, backup_file) @config = new_config @config['VERSION'] = Version save backup_file end |
#update_host_def(path, host_def) ⇒ Object
Update host definition (host_def) at the specified path. Raises ConfigError if doesn’t already exist!
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/runsshlib/config_file.rb', line 84 def update_host_def(path, host_def) # sanity raise ConfigError.new('Invalid host definition!') if not host_def.instance_of? SshHostDef # we need to separate the host name from the path # in order to get the key of the host definition. host = path.pop groups = retrieve_path(path, "Invalid path!") raise ConfigError, 'Invalid path!' unless groups if groups.include? host raise ConfigError.new("Cannot overwrite group with host definition") unless groups[host].instance_of? SshHostDef groups[host] = host_def else raise ConfigError.new("Host definition doesn't exist!") end save end |