Class: OwncloudCookbook::Config

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

Overview

Class to read and write ownCloud configuration file.

Examples:

config_file = '/var/www/owncloud/config/config.php'
c = OwncloudCookbook::Config.new(config_file)
c.merge(node['owncloud']['config'])
c.write

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Config

OwncloudCookbook::Config constructor.

Reads the current configuration values.

Examples:

config_file = '/var/www/owncloud/config/config.php'
OwncloudCookbook::Config.new(config_file)

49
50
51
52
53
54
# File 'libraries/config.rb', line 49

def initialize(file)
  @file = file
  @options = {}
  @original_options = {}
  read
end

Instance Method Details

#[](index) ⇒ Mixed

Gets an option value.

Examples:

config_file = '/var/www/owncloud/config/config.php'
c = OwncloudCookbook::Config.new(config_file)
c['dbtype'] #=> 'mysql'

107
108
109
# File 'libraries/config.rb', line 107

def [](index)
  @options[index]
end

#merge(new_options) ⇒ Hash

Merges new configuration options with the current values.

Saves the merged configuration values internally in an accumulator variable but not on disk.

Examples:

config_file = '/var/www/owncloud/config/config.php'
c = OwncloudCookbook::Config.new(config_file)
c.merge(node['owncloud']['config'])
  #=> {"dbtype"=>"mysql", "dbname"=>"owncloud", ...}

124
125
126
127
128
129
130
131
# File 'libraries/config.rb', line 124

def merge(new_options)
  return unless new_options.respond_to?(:to_hash)
  new_options = new_options.to_hash
  # remove not set values
  new_options.reject! { |_k, v| v.nil? }
  # merge options overriding collisions with new values
  @options.merge!(new_options) { |k, v1, v2| merge_value(k, v1, v2) }
end

#readHash

Reads the current configuration values from disk.

Saves the read configuration values internally in an accumulator variable.

Examples:

config_file = '/var/www/owncloud/config/config.php'
c = OwncloudCookbook::Config.new(config_file)
c.read #=> {"dbtype"=>"mysql", "dbname"=>"owncloud", ...}

144
145
146
147
148
149
150
151
152
153
154
155
# File 'libraries/config.rb', line 144

def read
  return unless ::File.exist?(@file)
  f = IO.popen('php', 'r+')
  f.write "<?php require('#{@file}'); echo json_encode($CONFIG);\n"
  f.close_write
  data = f.read
  f.close
  @options = JSON.parse(data)
  @original_options = @options.clone
rescue StandardError => e
  raise "Error reading ownCloud configuration: #{e.message}"
end

#writeObject

Writes the accumulated configuration values to disk.


160
161
162
163
164
165
166
167
168
169
170
171
# File 'libraries/config.rb', line 160

def write
  return if @options == @original_options
  f = IO.popen('php', 'r+')
  f.write "<?php var_export(json_decode('#{options_php_json}', true));\n"
  f.close_write
  data = f.read
  f.close
  File.open(@file, 'w') { |of| of.write "<?php\n$CONFIG = #{data};\n" }
  Chef::Log.info('OwnCloud config writen')
rescue StandardError => e
  raise "Error writing ownCloud configuration: #{e.message}"
end