Class: Spiceweasel::Environments

Inherits:
Object
  • Object
show all
Includes:
CommandHelper
Defined in:
lib/spiceweasel/environments.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandHelper

#create_command, #delete_command

Constructor Details

#initialize(environments = [], cookbooks = {}) ⇒ Environments

Returns a new instance of Environments.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/spiceweasel/environments.rb', line 28

def initialize(environments = [], cookbooks = {})
  @create = Array.new
  @delete = Array.new
  @environment_list = Array.new
  if environments
    Spiceweasel::Log.debug("environments: #{environments}")
    flatenvs = environments.collect {|x| x.keys}.flatten
    envfiles = []
    flatenvs.each do |env|
      Spiceweasel::Log.debug("environment: #{env}")
      if File.directory?("environments")
        #expand wildcards and push into environments
        if env =~ /\*/ #wildcard support
          wildenvs = Dir.glob("environments/#{env}")
          #remove anything not ending in .json or .rb
          wildenvs.delete_if {|x| !x.end_with?(".rb", ".json")}
          Spiceweasel::Log.debug("found environments '#{wildenvs}' for wildcard: #{env}")
          flatenvs.concat(wildenvs.collect {|x| x[x.rindex('/')+1..x.rindex('.')-1]})
          next
        end
        validate(env, cookbooks) unless Spiceweasel::Config[:novalidation]
      elsif !Spiceweasel::Config[:novalidation]
        STDERR.puts "'environments' directory not found, unable to validate or load environments"
        exit(-1)
      end
      if File.exists?("environments/#{env}.json")
        envfiles.push("#{env}.json")
      else #assume no .json means they want .rb and catchall for misssing dir
        envfiles.push("#{env}.rb")
      end
      delete_command("knife environment#{Spiceweasel::Config[:knife_options]} delete #{env} -y")
      @environment_list.push(env)
    end
    create_command("knife environment#{Spiceweasel::Config[:knife_options]} from file #{envfiles.uniq.sort.join(' ')}")
  end
end

Instance Attribute Details

#createObject (readonly)

Returns the value of attribute create.



26
27
28
# File 'lib/spiceweasel/environments.rb', line 26

def create
  @create
end

#deleteObject (readonly)

Returns the value of attribute delete.



26
27
28
# File 'lib/spiceweasel/environments.rb', line 26

def delete
  @delete
end

#environment_listObject (readonly)

Returns the value of attribute environment_list.



26
27
28
# File 'lib/spiceweasel/environments.rb', line 26

def environment_list
  @environment_list
end

Instance Method Details

#member?(environment) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/spiceweasel/environments.rb', line 100

def member?(environment)
  environment_list.include?(environment)
end

#validate(environment, cookbooks) ⇒ Object

validate the content of the environment file



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/spiceweasel/environments.rb', line 66

def validate(environment, cookbooks)
  file = %W(environments/#{environment}.rb environments/#{environment}.json).detect{|f| File.exists?(f)}
  if environment =~ /\// #pull out directories
    environment = environment.split('/').last
  end
  if file
    case file
    when /\.json$/
      env = Chef::JSONCompat.from_json(IO.read(file))
    when /\.rb$/
      if (Chef::VERSION.split('.')[0].to_i < 11)
        env = Chef::Environment.new(false)
      else
        env = Chef::Environment.new
      end
      env.from_file(file)
    end
    if(env.name != environment)
      STDERR.puts "ERROR: Environment '#{environment}' listed in the manifest does not match the name '#{env.name}' within the #{file} file."
      exit(-1)
    end
    env.cookbook_versions.keys.each do |dep|
      Spiceweasel::Log.debug("environment: '#{environment}' cookbook: '#{dep}'")
      unless cookbooks.member?(dep)
        STDERR.puts "ERROR: Cookbook dependency '#{dep}' from environment '#{environment}' is missing from the list of cookbooks in the manifest."
        exit(-1)
      end
    end
  else #environment is not here
    STDERR.puts "ERROR: Invalid Environment '#{environment}' listed in the manifest but not found in the environments directory."
    exit(-1)
  end
end