Rake task to write config file if it doesn't already exist.
RPM spec updated to run in %post
---
aeolus-conductor.spec.in | 5 ++++
src/.gitignore | 1 +
src/config/initializers/aeolus-image.rb | 33 +++++++++++++++++++++---------
src/lib/tasks/dc_tasks.rake | 25 +++++++++++++++++++++++
4 files changed, 54 insertions(+), 10 deletions(-)
diff --git a/aeolus-conductor.spec.in b/aeolus-conductor.spec.in
index b68cd29..586542d 100644
--- a/aeolus-conductor.spec.in
+++ b/aeolus-conductor.spec.in
@@ -37,6 +37,7 @@ Requires: rubygem(aeolus-image)
Requires: rubygem(pg)
Requires: rubygem(ruby-net-ldap)
Requires: rubygem(oauth)
+Requires: rubygem(rake)
Requires: postgresql
Requires: postgresql-server
@@ -233,11 +234,15 @@ touch %{buildroot}%{_localstatedir}/log/%{name}/dbomatic.log
# into /etc/aeolus-conductor. Note that we unceremoniously use -f here;
# if the user had broken the symlinks and put data in here, it would have been
# completely ignored *anyway*
+# Generate OAuth configuration:
+pushd %{app_root}; rake dc:oauth_keys --trace; popd
+
%{__ln_s} -f %{app_root}/config/environments/development.rb %{_sysconfdir}/%{name}
%{__ln_s} -f %{app_root}/config/environments/production.rb %{_sysconfdir}/%{name}
%{__ln_s} -f %{app_root}/config/environments/test.rb %{_sysconfdir}/%{name}
%{__ln_s} -f %{app_root}/config/database.yml %{_sysconfdir}/%{name}
%{__ln_s} -f %{app_root}/config/settings.yml %{_sysconfdir}/%{name}
+%{__ln_s} -f %{app_root}/config/oauth.json %{_sysconfdir}/%{name}
%postun
# kind of a weird construct. There are two cases where postun gets called;
diff --git a/src/.gitignore b/src/.gitignore
index af64fae..283be2f 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -2,3 +2,4 @@
db/*.sqlite3
log/*.log
tmp/**/*
+config/oauth.json
diff --git a/src/config/initializers/aeolus-image.rb
b/src/config/initializers/aeolus-image.rb
index b472766..02e129a 100644
--- a/src/config/initializers/aeolus-image.rb
+++ b/src/config/initializers/aeolus-image.rb
@@ -16,28 +16,43 @@
# MA 02110-1301, USA. A copy of the GNU General Public License is
# also available at
http://www.gnu.org/copyleft/gpl.html.
+# Read config files
begin
config = YAML.load_file("#{::Rails.root.to_s}/config/settings.yml")
rescue Errno::ENOENT
raise "Failed to load #{::Rails.root.to_s}/config/settings.yml for
aeolus-image-rubygem"
end
-# Aeolus::Image::Warehouse takes our whole YAML config file:
begin
- Aeolus::Image::Warehouse::WarehouseModel.config = config
-rescue NoMethodError
- raise "aeolus-image-rubygem is too old to support Warehouse configuration"
+ file = File.open("#{::Rails.root.to_s}/config/oauth.json", 'r') or
raise "Could not open file"
+ contents = file.read
+ oauth_config = JSON.parse(contents)
+rescue
+ puts "Failed to load #{::Rails.root.to_s}/config/oauth.json, continuing without
OAuth support. Run rake dc:oauth_keys and restart Conductor to enable."
end
-# Aeolus::Image::Factory has a more dignified configuration setting:
+# Aeolus::Image::Warehouse setup
+begin
+ Aeolus::Image::Warehouse::WarehouseModel.config = {
+ :iwhd => {
+ :url => config[:iwhd][:url],
+ :oauth => {
+ :consumer_key => (oauth_config['iwhd']['consumer_key'] rescue
nil),
+ :consumer_secret => (oauth_config['iwhd']['consumer_secret']
rescue nil)
+ }
+ }
+ }
+end
+
+# Aeolus::Image::Factory setup
begin
# Image Factory URL is required:
factory_site = config[:imagefactory][:url] or
raise "#{::Rails.root.to_s}/config/settings.yml failed to define Image Factory
URL"
# Consumer key and secret are not required; we fall back into OAuth-less mode:
- consumer_key = config[:imagefactory][:oauth][:consumer_key] rescue nil
- consumer_secret = config[:imagefactory][:oauth][:consumer_secret] rescue nil
+ consumer_key = oauth_config['factory']['consumer_key'] rescue nil
+ consumer_secret = oauth_config['factory']['consumer_secret'] rescue
nil
factory_config = {}
if consumer_key && consumer_secret
factory_config = {
@@ -46,12 +61,10 @@ begin
:consumer_secret => consumer_secret
}
else
- Rails.logger.warn "#{::Rails.root.to_s}/config/settings.yml did not define OAuth
configuration for Image Factory; continuing without OAuth support"
+ Rails.logger.warn "#{::Rails.root.to_s}/config/oauth.json did not define OAuth
configuration for Image Factory; continuing without OAuth support"
factory_config = {
:site => factory_site
}
end
Aeolus::Image::Factory::Base.config = factory_config
-rescue NoMethodError
- raise "aeolus-image-rubygem is too old to support Image Factory
configuration"
end
diff --git a/src/lib/tasks/dc_tasks.rake b/src/lib/tasks/dc_tasks.rake
index 0d74a75..28653cd 100644
--- a/src/lib/tasks/dc_tasks.rake
+++ b/src/lib/tasks/dc_tasks.rake
@@ -77,6 +77,27 @@ namespace :dc do
Rake::Task[:'dc:create_admin_user'].invoke
end
+ desc 'Generate keys for OAuth and create config/oauth.json'
+ task :oauth_keys do
+ oauth_config_file = "#{::Rails.root}/config/oauth.json"
+ if File.exist?(oauth_config_file)
+ puts "config/oauth.json already exists; not overwriting"
+ else
+ oauth_keys = {
+ :iwhd => {
+ :consumer_key => random_key,
+ :consumer_secret => random_key
+ },
+ :factory => {
+ :consumer_key => random_key,
+ :consumer_secret => random_key
+ }
+ }
+ key_file = File.open(oauth_config_file, 'w+')
+ key_file.write(oauth_keys.to_json)
+ end
+ end
+
def get_account(provider_name, account_name)
unless provider = Provider.find_by_name(provider_name)
raise "There is no provider with '#{provider_name}' name"
@@ -86,4 +107,8 @@ namespace :dc do
end
account
end
+
+ def random_key(bytes=24)
+ SecureRandom.base64(bytes)
+ end
end
--
1.7.6.4