Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from brandocorp/issue-1
Browse files Browse the repository at this point in the history
add address DSL
  • Loading branch information
brandocorp committed Jan 7, 2016
2 parents 462a2b8 + b30ed3e commit caeeac2
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/tmp/
.kitchen/
.kitchen.local.yml
.cocina.kitchen.yml
9 changes: 0 additions & 9 deletions example/.kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,18 @@ platforms:

suites:
- name: db
driver:
network:
- ["private_network", ip: "10.11.11.10"]
run_list:
- counter_redis::default
attributes:

- name: app
driver:
network:
- ["private_network", ip: "10.11.11.11"]
run_list:
- counter_app::default
attributes:
counter_app:
redis_host: "10.11.11.10"

- name: web
driver:
network:
- ["private_network", ip: "10.11.11.12"]
run_list:
- counter_nginx::default
attributes:
Expand Down
7 changes: 7 additions & 0 deletions example/Cocinafile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ log_level :info
instance 'web-ubuntu-1404' do
depends 'app-ubuntu-1404'
actions :converge, :converge, :verify
address :static, "10.11.11.12"
cleanup true
end

instance 'app-ubuntu-1404' do
depends 'db-ubuntu-1404'
actions :converge, :converge, :verify
address :static, "10.11.11.11"
cleanup true
end

instance 'db-ubuntu-1404' do
address :static, "10.11.11.10"
actions :create
end
46 changes: 42 additions & 4 deletions lib/cocina/config.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'cocina/instance'
require 'kitchen/loader/cocina'

module Cocina
class Config
Expand All @@ -15,22 +16,25 @@ def initialize(file)
load_cocinafile
load_kitchen_config
build_dependencies
# unlink_cocina_config
end

def load_cocinafile
self.instance_eval(IO.read(cocinafile), cocinafile, 1)
end

def load_kitchen_config
@loader = Kitchen::Loader::YAML.new(
write_cocina_yaml
@loader = Kitchen::Loader::Cocina.new(
project_config: project_kitchen_yaml,
local_config: ENV["KITCHEN_LOCAL_YAML"],
local_config: local_kitchen_yaml,
cocina_config: cocina_yaml_file.path,
global_config: ENV["KITCHEN_GLOBAL_YAML"]
)
@config = Kitchen::Config.new(
loader: @loader
loader: @loader,
log_level: log_level
)
@config.log_level = log_level
@config.log_overwrite =
Kitchen.env_log_overwrite unless Kitchen.env_log_overwrite.nil?
end
Expand All @@ -48,6 +52,40 @@ def project_kitchen_yaml
@project_kitchen_yaml ||= ENV["KITCHEN_YAML"]
end

def local_kitchen_yaml
ENV["KITCHEN_LOCAL_YAML"]
end

def cocina_yaml_file
@cocina_yaml_file ||= File.open('.cocina.kitchen.yml', 'w+')
end

def unlink_cocina_config
File.unlink(cocina_yaml_file.path)
end

def write_cocina_yaml
cocina_yaml_file.tap do |yaml|
yaml << "---\n"
yaml << "suites:\n"
instances.each do |machine|
yaml << " - name: #{machine.suite}\n"
yaml << " driver:\n"
yaml << " network:\n"
machine.addresses.each do |(key, val)|
case key
when :static
yaml << " - ['private_network', ip: '#{val}']\n"
when :dhcp
yaml << " - ['private_network', type: 'dhcp']\n"
end
end
end
end
cocina_yaml_file.close
true
end

def kitchen_instance(target)
@config.instances.get(target)
end
Expand Down
12 changes: 11 additions & 1 deletion lib/cocina/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ module Cocina
class Instance
extend Forwardable

attr_reader :name, :dependencies, :actions
attr_reader :name, :dependencies, :actions, :addresses
attr_accessor :runner

def_delegators :@runner, :destroy, :create, :converge, :verify

def initialize(name)
@name = name
@dependencies = []
@addresses = []
@actions = default_actions
@cleanup = false
end
Expand Down Expand Up @@ -41,6 +42,11 @@ def dependencies?
dependencies.empty? ? false : true
end

# Define a network address for the instance
def address(type, ip=nil)
@addresses << [type, ip]
end

# Set or return the list of actions
#
def actions(*list)
Expand All @@ -63,6 +69,10 @@ def run_actions
end
end

def suite
name.split('-').first
end

private

def default_actions
Expand Down
61 changes: 61 additions & 0 deletions lib/kitchen/loader/cocina.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'kitchen/loader/yaml'
require 'kitchen/util'

module Kitchen
module Loader
class Cocina < YAML
def initialize(options = {})
super
@cocina_config_file =
File.expand_path(options[:cocina_config] || default_cocina_config_file)
@process_cocina = options.fetch(:process_cocina, true)
end

private

# @return [String] the absolute path to the Cocina config YAML file
# @api private
attr_reader :cocina_config_file

# Performed a prioritized recursive merge of several source Hashes and
# returns a new merged Hash. There are 4 sources of configuration data:
#
# 1. cocina config
# 2. local config
# 3. project config
# 4. global config
#
# The merge order is cocina -> local -> project -> global, meaning that
# elements at the top of the above list will be merged last, and have greater
# precedence than elements at the bottom of the list.
#
# @return [Hash] a new merged Hash
# @api private
def combined_hash
{}.tap do |conf|
conf.rmerge!(normalize(global_yaml)) if @process_global
conf.rmerge!(normalize(yaml))
conf.rmerge!(normalize(local_yaml)) if @process_local
conf.rmerge!(normalize(cocina_yaml)) if @process_cocina
end
end

# Loads and returns the Cocina config YAML as a Hash.
#
# @return [Hash] the config hash
# @api private
def cocina_yaml
parse_yaml_string(yaml_string(cocina_config_file), cocina_config_file)
end

# Determines the default absolute path to the Cocina config YAML file,
# based on current working directory.
#
# @return [String] an absolute path to a Kitchen config YAML file
# @api private
def default_cocina_config_file
File.join(Dir.pwd, '.cocina.kitchen.yml')
end
end
end
end
2 changes: 0 additions & 2 deletions spec/cocina/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
.with('bar')
.and_return(bar)
allow(kitchen_config).to receive(:instances).and_return(kitchen_instances)
allow(kitchen_config).to receive(:log_level=).and_return(true)
allow(IO).to receive(:read).with('Cocinafile').and_return(content)
allow(Kitchen::Config).to receive(:new).and_return(kitchen_config)
end
Expand Down Expand Up @@ -120,7 +119,6 @@
.with('bar')
.and_return(bar)
allow(kitchen_config).to receive(:instances).and_return(kitchen_instances)
allow(kitchen_config).to receive(:log_level=).and_return(true)
allow(IO).to receive(:read).with('Cocinafile').and_return(content)
allow(Kitchen::Config).to receive(:new).and_return(kitchen_config)
end
Expand Down
1 change: 0 additions & 1 deletion spec/cocina/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
.with('bar')
.and_return(bar)
allow(kitchen_config).to receive(:instances).and_return(kitchen_instances)
allow(kitchen_config).to receive(:log_level=).and_return(true)
allow(Kitchen::Config).to receive(:new).and_return(kitchen_config)
end
end
Expand Down

0 comments on commit caeeac2

Please sign in to comment.