https://www.aeolusproject.org/redmine/issues/1866
As part of this, noticed we were not installing our sample files, consolidated
them and moved them into an examples folder.
---
services/image_factory/aeolus-image/Rakefile | 2 +-
.../image_factory/aeolus-image/examples/aeolus-cli | 9 +++
.../{spec/sample_data => examples}/custom_repo.tdl | 0
.../sample_data => examples}/image_description.xml | 0
.../image_factory/aeolus-image/lib/base_command.rb | 60 ++++++++++++++-----
.../aeolus-image/sample_data/aeolus-cli | 8 ---
.../aeolus-image/spec/base_command_spec.rb | 34 +++++++++--
.../aeolus-image/spec/build_command_spec.rb | 2 +-
.../aeolus-image/spec/push_command_spec.rb | 2 +-
.../aeolus-image/spec/sample_data/aeolus-cli | 7 --
.../image_factory/aeolus-image/spec/spec_helper.rb | 2 +-
11 files changed, 85 insertions(+), 41 deletions(-)
create mode 100644 services/image_factory/aeolus-image/examples/aeolus-cli
rename services/image_factory/aeolus-image/{spec/sample_data =>
examples}/custom_repo.tdl (100%)
rename services/image_factory/aeolus-image/{spec/sample_data =>
examples}/image_description.xml (100%)
delete mode 100644 services/image_factory/aeolus-image/sample_data/aeolus-cli
delete mode 100644 services/image_factory/aeolus-image/spec/sample_data/aeolus-cli
diff --git a/services/image_factory/aeolus-image/Rakefile
b/services/image_factory/aeolus-image/Rakefile
index 29941f1..092ccd1 100644
--- a/services/image_factory/aeolus-image/Rakefile
+++ b/services/image_factory/aeolus-image/Rakefile
@@ -21,7 +21,7 @@ spec = Gem::Specification.new do |s|
s.license = 'GPL-2'
s.homepage = 'http://aeolusproject.org'
s.executables << 'aeolus-image'
- s.files = %w(Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
+ s.files = %w(Rakefile) + Dir.glob("{bin,lib,spec,examples}/**/*")
s.require_path = "lib"
s.bindir = "bin"
s.add_dependency('nokogiri', '>=0.4.0')
diff --git a/services/image_factory/aeolus-image/examples/aeolus-cli
b/services/image_factory/aeolus-image/examples/aeolus-cli
new file mode 100644
index 0000000..22c4a17
--- /dev/null
+++ b/services/image_factory/aeolus-image/examples/aeolus-cli
@@ -0,0 +1,9 @@
+
+# Below is the correct format for this file, please update any older version you may
have
+:iwhd:
+ :url:
http://localhost:9090
+
+:conductor:
+ :url:
http://localhost:3000/conductor
+ :username: admin
+ :password: password
\ No newline at end of file
diff --git a/services/image_factory/aeolus-image/spec/sample_data/custom_repo.tdl
b/services/image_factory/aeolus-image/examples/custom_repo.tdl
similarity index 100%
rename from services/image_factory/aeolus-image/spec/sample_data/custom_repo.tdl
rename to services/image_factory/aeolus-image/examples/custom_repo.tdl
diff --git a/services/image_factory/aeolus-image/spec/sample_data/image_description.xml
b/services/image_factory/aeolus-image/examples/image_description.xml
similarity index 100%
rename from services/image_factory/aeolus-image/spec/sample_data/image_description.xml
rename to services/image_factory/aeolus-image/examples/image_description.xml
diff --git a/services/image_factory/aeolus-image/lib/base_command.rb
b/services/image_factory/aeolus-image/lib/base_command.rb
index 3346e1a..7e5f72e 100644
--- a/services/image_factory/aeolus-image/lib/base_command.rb
+++ b/services/image_factory/aeolus-image/lib/base_command.rb
@@ -11,6 +11,7 @@ module Aeolus
def initialize(opts={}, logger=nil)
logger(logger)
@options = opts
+ @config_location = "~/.aeolus-cli"
@config = load_config
end
@@ -30,17 +31,17 @@ module Aeolus
end
def iwhd
- create_resource('iwhd')
+ create_resource(:iwhd)
end
def conductor
- create_resource('conductor')
+ create_resource(:conductor)
end
def read_file(path)
begin
full_path = File.expand_path(path)
- if File.exist?(full_path) && !File.directory?(full_path)
+ if is_file?(path)
File.read(full_path)
else
return nil
@@ -50,6 +51,16 @@ module Aeolus
end
end
+ # TODO: Consider ripping all this file-related stuff into a module or
+ # class for better encapsulation and testability
+ def is_file?(path)
+ full_path = File.expand_path(path)
+ if File.exist?(full_path) && !File.directory?(full_path)
+ return true
+ end
+ false
+ end
+
def quit(code)
exit(code)
end
@@ -57,42 +68,59 @@ module Aeolus
private
def load_config
begin
- @config_location = ENV['HOME'] + "/.aeolus-cli"
- YAML::load(File.open(@config_location))
+ file_str = read_file(@config_location)
+ if is_file?(@config_location) && !file_str.include?(":url")
+ lines = File.readlines(File.expand_path((a)config_location)).map do |line|
+ "#" + line
+ end
+ File.open(File.expand_path(@config_location), 'w') do |file|
+ file.puts lines
+ end
+ write_file
+ end
+ write_file unless is_file?(@config_location)
+ YAML::load(File.open(File.expand_path(@config_location)))
rescue Errno::ENOENT
#TODO: Create a custom exception to wrap CLI Exceptions
- raise "Unable to locate configuration file: \"" +
@config_location + "\""
+ raise "Unable to locate or write configuration file: \"" +
@config_location + "\""
+ end
+ end
+
+ def write_file
+ example = File.read(File.expand_path(File.dirname(__FILE__) +
"/../examples/aeolus-cli"))
+ File.open(File.expand_path(@config_location), 'a+') do |f|
+ f.write(example)
end
end
def create_resource(resource_name)
# Check to see if config has a resource with this name
if !(a)config.has_key?(resource_name)
- raise "Unable to determine resource: " + resource_name + " from
configuration file. Please check: " + @config_location
+ raise "Unable to determine resource: " + resource_name.to_s + "
from configuration file. Please check: " + @config_location
return
end
#Use command line arguments for username/password
- if @options.has_key?('username')
- if @options.has_key?('password')
- RestClient::Resource.new(@config[resource_name]['url'], :user =>
@options['username'], :password => @options['password'])
+ if @options.has_key?(:username)
+ if @options.has_key?(:password)
+ RestClient::Resource.new(@config[resource_name][:url], :user =>
@options[:username], :password => @options[:password])
else
#TODO: Create a custom exception to wrap CLI Exceptions
- raise "Password not found for user: " +
@options['username']
+ raise "Password not found for user: " + @options[:username]
end
#Use config for username/password
- elsif @config[resource_name].has_key?('username')
- if @config[resource_name].has_key?('password')
- RestClient::Resource.new(@config[resource_name]['url'], :user =>
@config[resource_name]['username'], :password =>
@config[resource_name]['password'])
+ elsif @config[resource_name].has_key?(:username)
+ if @config[resource_name].has_key?(:password)
+ RestClient::Resource.new(@config[resource_name][:url], :user =>
@config[resource_name][:username], :password => @config[resource_name][:password])
else
#TODO: Create a custom exception to wrap CLI Exceptions
- raise "Password not found for user: " +
@config[resource_name]['username']
+ raise "Password not found for user: " +
@config[resource_name][:username]
end
# Do not use authentication
else
- RestClient::Resource.new(@config[resource_name]['url'])
+ RestClient::Resource.new(@config[resource_name][:url])
end
end
end
diff --git a/services/image_factory/aeolus-image/sample_data/aeolus-cli
b/services/image_factory/aeolus-image/sample_data/aeolus-cli
deleted file mode 100644
index f66c3db..0000000
--- a/services/image_factory/aeolus-image/sample_data/aeolus-cli
+++ /dev/null
@@ -1,8 +0,0 @@
-# Place this file in: ~/.aeolus-cli
-iwhd:
- url:
http://localhost:9090
-
-conductor:
- url:
http://localhost:3000/conductor
- username: admin
- password: password
diff --git a/services/image_factory/aeolus-image/spec/base_command_spec.rb
b/services/image_factory/aeolus-image/spec/base_command_spec.rb
index f56edd5..d8459db 100644
--- a/services/image_factory/aeolus-image/spec/base_command_spec.rb
+++ b/services/image_factory/aeolus-image/spec/base_command_spec.rb
@@ -13,28 +13,50 @@ module Aeolus
conductor.user.should == "admin"
conductor.password.should == "password"
- basec = BaseCommand.new({'username' => "testusername",
'password'=> "testpassword"})
+ basec = BaseCommand.new({:username => "testusername", :password=>
"testpassword"})
conductor = basec.send :conductor
conductor.user.should == "testusername"
conductor.password.should == "testpassword"
end
describe "#read_file" do
- after(:each) do
- #(a)b.console.shutdown
- end
it "should return nil when it cannot find file" do
b = BaseCommand.new
- b.send(:read_file, "foo.fake").should == nil
+ b.send(:read_file, "foo.fake").should be_nil
end
it "should read file content into string variable" do
b = BaseCommand.new
- template_str = b.send(:read_file, 'spec/sample_data/custom_repo.tdl')
+ template_str = b.send(:read_file, "#{File.dirname(__FILE__)}" +
"/../examples/custom_repo.tdl")
template_str.should include("<template>")
end
end
+
+ describe "#is_file?" do
+ it "should return false if no file found" do
+ b = BaseCommand.new
+ b.send(:is_file?, "foo.fake").should be_false
+ end
+ it "should return true if file found" do
+ b = BaseCommand.new
+ valid_file = "#{File.dirname(__FILE__)}" +
"/../examples/aeolus-cli"
+ b.instance_eval {@config_location = valid_file}
+ b.send(:is_file?, valid_file).should be_true
+ end
+ end
+
+ describe "#write_file" do
+ it "should write a new file" do
+ b = BaseCommand.new
+ new_file = "/tmp/foo.fake"
+ b.instance_eval {@config_location = new_file}
+ b.send(:write_file)
+ conf = YAML::load(File.open(File.expand_path(new_file)))
+ conf.has_key?(:iwhd).should be_true
+ File.delete(new_file)
+ end
+ end
end
end
end
\ No newline at end of file
diff --git a/services/image_factory/aeolus-image/spec/build_command_spec.rb
b/services/image_factory/aeolus-image/spec/build_command_spec.rb
index f6b97b6..90e6929 100644
--- a/services/image_factory/aeolus-image/spec/build_command_spec.rb
+++ b/services/image_factory/aeolus-image/spec/build_command_spec.rb
@@ -5,7 +5,7 @@ module Aeolus
describe BuildCommand do
before(:each) do
@options[:target] = ['mock','ec2']
- @options[:template] = 'spec/sample_data/custom_repo.tdl'
+ @options[:template] = "#{File.dirname(__FILE__)}" +
"/../examples/custom_repo.tdl"
end
describe "#run" do
diff --git a/services/image_factory/aeolus-image/spec/push_command_spec.rb
b/services/image_factory/aeolus-image/spec/push_command_spec.rb
index 75bd1c5..83cb48b 100644
--- a/services/image_factory/aeolus-image/spec/push_command_spec.rb
+++ b/services/image_factory/aeolus-image/spec/push_command_spec.rb
@@ -14,7 +14,7 @@ module Aeolus
before(:each) do
options = {}
options[:target] = ['mock','ec2']
- options[:template] = 'spec/sample_data/custom_repo.tdl'
+ options[:template] = "#{File.dirname(__FILE__)}" +
"/../examples/custom_repo.tdl"
b = BuildCommand.new(options)
sleep(5)
tmpl_str = b.send(:read_file, options[:template])
diff --git a/services/image_factory/aeolus-image/spec/sample_data/aeolus-cli
b/services/image_factory/aeolus-image/spec/sample_data/aeolus-cli
deleted file mode 100644
index 574a233..0000000
--- a/services/image_factory/aeolus-image/spec/sample_data/aeolus-cli
+++ /dev/null
@@ -1,7 +0,0 @@
-iwhd:
- url:
http://localhost:9090
-
-conductor:
- url:
http://localhost:3000/conductor
- username: admin
- password: password
\ No newline at end of file
diff --git a/services/image_factory/aeolus-image/spec/spec_helper.rb
b/services/image_factory/aeolus-image/spec/spec_helper.rb
index fb58b43..013fbd3 100644
--- a/services/image_factory/aeolus-image/spec/spec_helper.rb
+++ b/services/image_factory/aeolus-image/spec/spec_helper.rb
@@ -36,7 +36,7 @@ Spec::Runner.configure do |config|
config.before(:all) do
Aeolus::Image::BaseCommand.class_eval do
def load_config
- YAML::load(File.open(File.join(File.dirname(__FILE__),
"sample_data/aeolus-cli")))
+ YAML::load(File.open(File.join(File.dirname(__FILE__),
"/../examples/aeolus-cli")))
end
end
end
--
1.7.4.4