Class: HP::Cloud::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/hpcloud/config.rb

Constant Summary collapse

KNOWN =
[ :default_auth_uri,
  :connect_timeout,
  :read_timeout,
  :write_timeout,
  :preferred_flavor,
  :preferred_image,
  :ssl_verify_peer,
  :ssl_ca_path,
  :ssl_ca_file,
  :default_account,
  :storage_page_length,
  :storage_segment_size,
  :storage_chunk_size,
  :storage_max_size,
  :report_page_length,
  :checker_url,
  :checker_deferment
]
@@home =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ignore = false) ⇒ Config

Returns a new instance of Config.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hpcloud/config.rb', line 48

def initialize(ignore=false)
  if @@home.nil?
    @@home = ENV['HOME']
  end
  @directory = @@home + "/.hpcloud/"
  @file = @directory + "config.yml"
  @file_settings = {}
  @settings = {}
  begin
    read()
  rescue Exception => e
    if ignore
      warn e.to_s
    else
      raise e
    end
  end
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



28
29
30
# File 'lib/hpcloud/config.rb', line 28

def directory
  @directory
end

#fileObject (readonly)

Returns the value of attribute file.



28
29
30
# File 'lib/hpcloud/config.rb', line 28

def file
  @file
end

#settingsObject (readonly)

Returns the value of attribute settings.



28
29
30
# File 'lib/hpcloud/config.rb', line 28

def settings
  @settings
end

Class Method Details

.default_configObject



71
72
73
74
75
# File 'lib/hpcloud/config.rb', line 71

def self.default_config
  return { :default_auth_uri => 'https://region-a.geo-1.identity.hpcloudsvc.com:35357/v2.0/',
           :default_account => 'hp'
         }
end

.default_optionsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/hpcloud/config.rb', line 77

def self.default_options
  return { :connect_timeout => 30,
           :read_timeout => 240,
           :write_timeout => 240,
           :preferred_flavor => 100,
           :ssl_verify_peer => true,
           :ssl_ca_path => nil,
           :ssl_ca_file => nil,
           :default_account => 'hp',
           :checker_url => 'https://region-a.geo-1.objects.hpcloudsvc.com:443/v1/89388614989714/documentation-downloads/unixcli/latest',
           :checker_deferment => 604800,
         }
end

.get_knownObject



91
92
93
94
95
# File 'lib/hpcloud/config.rb', line 91

def self.get_known
  ret = ""
  KNOWN.each{|key| ret += "\n" + key.to_s }
  return ret
end

.home_directory=(dir) ⇒ Object



67
68
69
# File 'lib/hpcloud/config.rb', line 67

def self.home_directory=(dir)
  @@home = dir
end

.split(nvp) ⇒ Object

Raises:

  • (Exception)


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hpcloud/config.rb', line 97

def self.split(nvp)
  begin
    kv = nvp.split('=')
    if kv.length == 2
      return kv[0], kv[1]
    end
    if nvp[-1,1] == '='
      return kv[0], ''
    end
  rescue Exception => e
  end
  raise Exception.new("Invalid name value pair: '#{nvp}'")
end

Instance Method Details

#get(key) ⇒ Object



154
155
156
# File 'lib/hpcloud/config.rb', line 154

def get(key)
  return @settings[key.to_sym]
end

#get_i(key, default_value) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/hpcloud/config.rb', line 158

def get_i(key, default_value)
  begin
    value = @settings[key.to_sym].to_i
    return default_value if value == 0
    return value
  rescue
  end
  return default_value
end

#listObject



111
112
113
# File 'lib/hpcloud/config.rb', line 111

def list
  return @settings.to_yaml.gsub(/---\n/,'').gsub(/^:/,'')
end

#readObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/hpcloud/config.rb', line 115

def read
  cfg = Config.default_config()
  if File.exists?(@file)
    begin
      @file_settings = YAML::load(File.open(@file))
      @settings = @file_settings.clone
      raise Exception.new("File parse error") unless @settings.kind_of?(Hash)
    rescue Exception => e
      @settings = cfg
      raise Exception.new("Error reading configuration file: #{@file}\n" + e.to_s)
    end
  else
    @settings = cfg
  end
  options = Config.default_options()
  @settings[:connect_timeout] ||= options[:connect_timeout]
  @settings[:read_timeout] ||= options[:read_timeout]
  @settings[:write_timeout] ||= options[:write_timeout]
  @settings[:preferred_flavor] ||= options[:preferred_flavor]
  @settings[:connect_timeout] = @settings[:connect_timeout].to_i
  @settings[:read_timeout] = @settings[:read_timeout].to_i
  @settings[:write_timeout] = @settings[:write_timeout].to_i
  if @settings[:ssl_verify_peer].nil?
    @settings[:ssl_verify_peer] = options[:ssl_verify_peer]
  end
  if @settings[:ssl_verify_peer].to_s == "false" || @settings[:ssl_verify_peer].to_s == "no"
    @settings[:ssl_verify_peer] = false
  else
    @settings[:ssl_verify_peer] = true
  end
  @settings[:ssl_ca_path] ||= options[:ssl_ca_path]
  @settings[:ssl_ca_file] ||= options[:ssl_ca_file]
  @settings[:default_account] ||= options[:default_account]
  @settings[:checker_url] ||= options[:checker_url]
  @settings[:checker_deferment] ||= options[:checker_deferment]
  @settings[:checker_deferment] = @settings[:checker_deferment].to_i
  @settings.delete_if { |k,v| v.nil? }
end

#set(key, value) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/hpcloud/config.rb', line 168

def set(key, value)
  key = key.to_sym
  if KNOWN.include?(key) == false
    raise Exception.new("Unknown configuration key value '#{key.to_s}'")
  end
  value = value.to_s
  if value.empty?
    @file_settings.delete(key)
    @settings.delete(key)
  else
    if key.to_s == 'ssl_verify_peer'
      if value.to_s == "false" || value.to_s == "no"
        value = false
      else
        value = true
      end
    end
    @file_settings[key] = value
    @settings[key] = value
  end
  return true
end

#writeObject



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/hpcloud/config.rb', line 191

def write()
  begin
    Dir.mkdir(@directory) unless File.directory?(@directory)
    @file_settings.delete_if { |k,v| v.nil? }
    File.open("#{@file}", 'w') do |file|
      file.write @file_settings.to_yaml
    end
  rescue
    raise Exception.new('Error writing configuration file: ' + @file)
  end
  return true
end