Class: Spiceweasel::Environments

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

Overview

manages parsing of Environments

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandHelper

#bundler?, #create_command, #delete_command

Constructor Details

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

rubocop:disable CyclomaticComplexity



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/spiceweasel/environments.rb', line 30

def initialize(environments = [], cookbooks = {}) # rubocop:disable CyclomaticComplexity
  @create = []
  @delete = []
  @environment_list = []

  return unless environments

  Spiceweasel::Log.debug("environments: #{environments}")
  envfiles = do_flattened_environments(cookbooks, environments)
  create_command("knife environment#{Spiceweasel::Config[:knife_options]} from file #{envfiles.uniq.sort.join(' ')}")
end

Instance Attribute Details

#createObject (readonly)

Returns the value of attribute create.



28
29
30
# File 'lib/spiceweasel/environments.rb', line 28

def create
  @create
end

#deleteObject (readonly)

Returns the value of attribute delete.



28
29
30
# File 'lib/spiceweasel/environments.rb', line 28

def delete
  @delete
end

#environment_listObject (readonly)

Returns the value of attribute environment_list.



28
29
30
# File 'lib/spiceweasel/environments.rb', line 28

def environment_list
  @environment_list
end

Instance Method Details

#do_cookbook_version(cookbooks, dep, environment) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/spiceweasel/environments.rb', line 98

def do_cookbook_version(cookbooks, dep, environment)
  Spiceweasel::Log.debug("environment: '#{environment}' cookbook: '#{dep}'")

  return if 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

#do_flattened_environments(cookbooks, environments) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/spiceweasel/environments.rb', line 42

def do_flattened_environments(cookbooks, environments)
  flatenvs = environments.map(&: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.map { |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.exist?("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
  envfiles
end

#do_ruby_environment_file(file) ⇒ Object



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

def do_ruby_environment_file(file)
  if Chef::VERSION.split('.')[0].to_i < 11
    env = Chef::Environment.new(false)
  else
    env = Chef::Environment.new
  end
  begin
    env.from_file(file)
  rescue SyntaxError => e
    STDERR.puts "ERROR: Environment '#{file}' has syntax errors."
    STDERR.puts e.message
    exit(-1)
  end
  env
end

#member?(environment) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/spiceweasel/environments.rb', line 123

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

#validate(environment, cookbooks) ⇒ Object

validate the content of the environment file



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/spiceweasel/environments.rb', line 74

def validate(environment, cookbooks) # rubocop:disable CyclomaticComplexity
  env = nil
  file = %W(environments/#{environment}.rb environments/#{environment}.json).find { |f| File.exist?(f) }
  environment = environment.split('/').last if environment =~ /\// # pull out directories
  if file
    case file
    when /\.json$/
      env = Chef::JSONCompat.from_json(IO.read(file))
    when /\.rb$/
      env = do_ruby_environment_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|
      do_cookbook_version(cookbooks, dep, environment)
    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