Module: Ironfan

Defined in:
lib/ironfan/broker.rb,
lib/ironfan.rb,
lib/ironfan/dsl.rb,
lib/ironfan/builder.rb,
lib/ironfan/dsl/ec2.rb,
lib/ironfan/dsl/rds.rb,
lib/ironfan/headers.rb,
lib/ironfan/version.rb,
lib/ironfan/dsl/role.rb,
lib/ironfan/provider.rb,
lib/ironfan/dsl/cloud.rb,
lib/ironfan/dsl/facet.rb,
lib/ironfan/dsl/realm.rb,
lib/ironfan/deprecated.rb,
lib/ironfan/dsl/server.rb,
lib/ironfan/dsl/static.rb,
lib/ironfan/dsl/volume.rb,
lib/ironfan/dsl/cluster.rb,
lib/ironfan/dsl/compute.rb,
lib/ironfan/dsl/vsphere.rb,
lib/ironfan/plugin/base.rb,
lib/ironfan/broker/drive.rb,
lib/ironfan/provider/ec2.rb,
lib/ironfan/provider/rds.rb,
lib/ironfan/dsl/component.rb,
lib/ironfan/dsl/openstack.rb,
lib/ironfan/provider/chef.rb,
lib/ironfan/dsl/virtualbox.rb,
lib/ironfan/broker/computer.rb,
lib/ironfan/provider/static.rb,
lib/ironfan/provider/vsphere.rb,
lib/chef/knife/ironfan_script.rb,
lib/ironfan/dsl/security_group.rb,
lib/ironfan/provider/chef/node.rb,
lib/ironfan/provider/chef/role.rb,
lib/ironfan/provider/openstack.rb,
lib/ironfan/provider/virtualbox.rb,
lib/ironfan/provider/chef/client.rb,
lib/ironfan/provider/ec2/keypair.rb,
lib/ironfan/provider/ec2/machine.rb,
lib/ironfan/provider/rds/machine.rb,
lib/ironfan/cookbook_requirements.rb,
lib/chef/knife/ironfan_knife_common.rb,
lib/ironfan/provider/ec2/ebs_volume.rb,
lib/ironfan/provider/ec2/elastic_ip.rb,
lib/ironfan/provider/static/machine.rb,
lib/ironfan/provider/vsphere/keypair.rb,
lib/ironfan/provider/vsphere/machine.rb,
lib/ironfan/provider/openstack/keypair.rb,
lib/ironfan/provider/openstack/machine.rb,
lib/ironfan/provider/ec2/security_group.rb,
lib/ironfan/provider/rds/security_group.rb,
lib/ironfan/provider/virtualbox/machine.rb,
lib/ironfan/provider/ec2/placement_group.rb,
lib/ironfan/provider/openstack/elastic_ip.rb,
lib/ironfan/provider/openstack/security_group.rb,
lib/ironfan/provider/ec2/elastic_load_balancer.rb,
lib/ironfan/provider/ec2/iam_server_certificate.rb

Overview

Author

Philip (flip) Kromer (<[email protected]>)

Copyright

Copyright © 2011 Infochimps, Inc

License

Apache License, Version 2.0

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Modules: CookbookRequirements, KnifeCommon, Pluggable, Plugin Classes: Broker, Builder, Dsl, IaasProvider, Provider, Script

Constant Summary collapse

VERSION =
'6.1.9'

Class Method Summary collapse

Class Method Details

.brokerObject



6
7
8
# File 'lib/ironfan/broker.rb', line 6

def self.broker
  @@broker ||= Ironfan::Broker.new
end

.chef_configObject



21
# File 'lib/ironfan.rb', line 21

def chef_config()     @chef_config       ; end

.chef_config=(cc) ⇒ Object



20
# File 'lib/ironfan.rb', line 20

def chef_config=(cc)  @chef_config = cc  ; end

.cluster(name, attrs = {}, &blk) ⇒ Object

Dsl constructors



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ironfan.rb', line 29

def cluster(name, attrs = {}, &blk)
  existing = clusters[name.to_sym]
  return existing if attrs.empty? && !block_given?
  if existing
    existing.receive!(attrs, &blk)
    existing.resolve!
  else
    cl = Ironfan::Dsl::Cluster.define(attrs.merge(name: name.to_sym), &blk)
    cl.resolve!
  end
end

.clustersObject

Attributes



9
10
11
# File 'lib/ironfan.rb', line 9

def clusters
  Ironfan::Dsl::Cluster.definitions
end

.clusters_dirObject

Dsl loaders



56
57
58
59
60
61
62
# File 'lib/ironfan.rb', line 56

def clusters_dir
  return Array(chef_config[:cluster_path]) if chef_config[:cluster_path]
  raise "Holy smokes, you have no cookbook_path or cluster_path set up. Follow chef's directions for creating a knife.rb." if chef_config[:cookbook_path].blank?
  cl_path = chef_config[:cookbook_path].map{|dir| File.expand_path('../clusters', dir) }.uniq
  ui.warn "No cluster path set. Taking a wild guess that #{cl_path.inspect} is \nreasonable based on your cookbook_path -- but please set cluster_path in your knife.rb"
  chef_config[:cluster_path] = cl_path
end

.deprecated(call, replacement = nil) ⇒ Object

Raises:

  • (StandardError)


2
3
4
5
6
# File 'lib/ironfan/deprecated.rb', line 2

def self.deprecated call, replacement=nil
  correction = ", " if replacement
  ui.error "The '#{call}' statement is deprecated#{correction} (in #{caller(2).first.inspect}). It looks like you are using an outdated DSL definition: please see https://github.com/infochimps-labs/ironfan/wiki/Upgrading-to-v4 for all the necessary upgrade steps."
  raise StandardError, "Deprecated call to #{call} - #{replacement}", caller
end

.die(*messages) ⇒ Object

Common utility methods



101
102
103
104
105
# File 'lib/ironfan.rb', line 101

def die(*messages)
  exit_code = messages.last.is_a?(Integer) ? messages.pop : -1
  messages.each{ |msg| ui.warn msg }
  exit exit_code
end

.dry_run?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/ironfan.rb', line 176

def dry_run?
  knife_config[:dry_run]
end

.dsl_files(location) ⇒ Object



89
90
91
# File 'lib/ironfan.rb', line 89

def dsl_files location
  Dir.glob File.join(location, '**/*.rb')    
end

.knife_configObject



24
# File 'lib/ironfan.rb', line 24

def knife_config()    @knife_config      ; end

.knife_config=(kc) ⇒ Object



23
# File 'lib/ironfan.rb', line 23

def knife_config=(kc) @knife_config = kc ; end

.load_cluster(name) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
# File 'lib/ironfan.rb', line 68

def load_cluster name
  raise ArgumentError.new('Please supply a cluster name') if name.blank?
  load_dsl_definition(name, clusters_dir)
  clusters[name.to_sym] or die("Couldn't find a cluster definition for <#{name}> in #{clusters_dir}")
end

.load_dsl_definition(name, location) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/ironfan.rb', line 80

def load_dsl_definition(name, location)
  Chef::Log.info "Looking for <#{name}> dsl definition in #{location}"
  if named_file = dsl_files(location).detect{ |dsl_file| File.basename(dsl_file, '.rb') == name }
    load_dsl_file named_file
  else
    dsl_files(location).each{ |f| load_dsl_file f }
  end
end

.load_dsl_file(filename) ⇒ Object



93
94
95
96
# File 'lib/ironfan.rb', line 93

def load_dsl_file filename
  Chef::Log.info "Loading dsl file #{filename}"
  require filename
end

.load_realm(name) ⇒ Object

Raises:

  • (ArgumentError)


74
75
76
77
78
# File 'lib/ironfan.rb', line 74

def load_realm name
  raise ArgumentError.new('Please supply a realm name') if name.blank?
  load_dsl_definition(name, realms_dir)
  realms[name.to_sym] or die("Couldn't find a realm definition for <#{name}> in #{realms_dir}")
end

.noop(source, method, *params) ⇒ Object



180
181
182
# File 'lib/ironfan.rb', line 180

def noop(source, method, *params)
  # Chef::Log.debug("#{method} is a no-op for #{source} -- skipping (#{params.join(',')})")
end

.parallel(targets, &actions) ⇒ Object

Raises:

  • (ArgumentError)


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ironfan.rb', line 107

def parallel(targets, &actions)
  raise ArgumentError.new('Must provide a block to run in parallel') unless block_given?
  results = []
  [targets].flatten.each_with_index.map do |target, idx|
    sleep(0.25) # avoid hammering with simultaneous requests
    Thread.new(target) do |target|
      results[idx] = safely(target.inspect) do
        yield target
      end
    end
  end.each(&:join) # wait for all the blocks to return
  results
end

.realm(name, attrs = {}, &blk) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ironfan.rb', line 41

def realm(name, attrs = {}, &blk)
  existing = realms[name.to_sym]
  return existing if attrs.empty? && !block_given?
  if existing
    existing.receive!(attrs, &blk)
    existing.resolve!
  else
    rlm = Ironfan::Dsl::Realm.define(attrs.merge(name: name.to_sym), &blk)
    rlm.resolve!
  end
end

.realmsObject



13
14
15
# File 'lib/ironfan.rb', line 13

def realms
  Ironfan::Dsl::Realm.definitions
end

.realms_dirObject



64
65
66
# File 'lib/ironfan.rb', line 64

def realms_dir
  clusters_dir.map{ |dir| File.expand_path('../realms', dir) }.uniq
end

.safely(operation = '', &action) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ironfan.rb', line 121

def safely(operation = '', &action)
  begin
    yield
  rescue StandardError => err
    ui.warn "Error running #{operation}:"
    ui.warn err
    Chef::Log.error err
    Chef::Log.error err.backtrace.join("\n")
    return err
  end
end

.step(name, desc, *style) ⇒ Object



151
152
153
# File 'lib/ironfan.rb', line 151

def step(name, desc, *style)
  ui.info("  #{"%-15s" % (name.to_s+":")}\t#{ui.color(desc.to_s, *style)}")
end

.substep(name, desc, color = :gray) ⇒ Object



155
156
157
# File 'lib/ironfan.rb', line 155

def substep(name, desc, color = :gray)
  step(name, "  - #{desc}", color) if (verbosity >= 1 or color != :gray)
end

.tell_you_thrice(options = {}, &action) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ironfan.rb', line 133

def tell_you_thrice(options = {}, &action)
  error_class = options[:error_class] || StandardError
  retries     = options[:retries]     || 3
  multiplier  = options[:multiplier]  || 3  

  attempt = 0
  begin
    attempt += 1
    yield
  rescue error_class => err
    raise if attempt > retries
    pause_for = multiplier * attempt
    Chef::Log.debug "Caught error (was #{err.inspect}). Sleeping #{pause_for} seconds."
    sleep pause_for
    retry
  end
end

.todo(*args) ⇒ Object



163
164
165
# File 'lib/ironfan.rb', line 163

def todo(*args)
  Chef::Log.debug(*args) if knife_config[:show_todo]
end

.uiObject



18
# File 'lib/ironfan.rb', line 18

def ui()              @ui                ; end

.ui=(ui) ⇒ Object



17
# File 'lib/ironfan.rb', line 17

def ui=(ui)           @ui = ui           ; end

.unless_dry_run(&action) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/ironfan.rb', line 167

def unless_dry_run(&action)
  if dry_run?
    ui.info("      ... but not really")
    return nil
  else
    yield
  end
end

.verbosityObject



159
160
161
# File 'lib/ironfan.rb', line 159

def verbosity
  knife_config[:verbosity].to_i
end