Class: Veewee::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/veewee/environment.rb

Overview

Represents a single Veewee environment. A “Veewee environment” is defined as basically a folder with a “Veeweefile”. This class allows access to the VMs, CLI, etc. all in the scope of this environment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Environment

Returns a new instance of Environment.



54
55
56
57
58
59
60
61
62
63
64
65
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
99
100
101
102
103
104
105
106
107
# File 'lib/veewee/environment.rb', line 54

def initialize(options = {})

  cwd = ENV['VEEWEE_DIR'] || Dir.pwd
  # If a cwd was provided as option it overrules the default
  cwd = options[:cwd] if options.has_key?(:cwd)

  defaults = {
    :cwd => cwd,
    :veewee_filename => "Veeweefile",
    :loglevel => :info,
    :definition_dir => File.join(cwd, "definitions"),
    :template_path => [File.expand_path(File.join(File.dirname(__FILE__), "..", "..", 'templates')), "templates"],
    :iso_dir => File.join(cwd, "iso"),
    :validation_dir => File.join(File.expand_path(File.join(File.dirname(__FILE__), "..", "..")), "validation"),
    :tmp_dir => File.join(cwd, "tmp")
  }

  options = defaults.merge(options)
  veeweefile_config = defaults.keys.inject({}) do |memo, obj|
    if config.env.methods.include?(obj) && !config.env.send(obj).nil?
      memo.merge({ obj => config.env.send(obj) })
    else
      memo
    end
  end
  options = options.merge(veeweefile_config)

  # We need to set this variable before the first call to the logger object
  if options.has_key?("debug")
    if options["debug"] == true
      ENV['VEEWEE_LOG'] = "STDOUT"
    end
  end

  logger.info("environment") { "Environment initialized (#{self})" }

  # Injecting all variables of the options and assign the variables
  options.each do |key, value|
    instance_variable_set("@#{key}".to_sym, options[key])
    logger.info("environment") { " - #{key} : #{options[key]}" }
  end

  # Definitions
  @definitions = Veewee::Definitions.new(self)
  @templates = Veewee::Templates.new(self)
  @providers = Veewee::Providers.new(self, options)

  # Read ostypes
  yamlfile = File.join(File.dirname(__FILE__), "config", "ostypes.yml")
  logger.info "Reading ostype yamlfile #{yamlfile}"
  @ostypes = YAML.load_file(yamlfile)

  return self
end

Instance Attribute Details

#configConfig::Top

The configuration object represented by this environment. This will trigger the environment to load if it hasn’t loaded yet (see #load!).

Returns:

  • (Config::Top)


40
41
42
# File 'lib/veewee/environment.rb', line 40

def config
  @config
end

#cwdObject

The ‘cwd` that this environment represents



16
17
18
# File 'lib/veewee/environment.rb', line 16

def cwd
  @cwd
end

#definition_dirObject

Returns the value of attribute definition_dir.



31
32
33
# File 'lib/veewee/environment.rb', line 31

def definition_dir
  @definition_dir
end

#definitionsObject

Hash element of all definitions available



43
44
45
# File 'lib/veewee/environment.rb', line 43

def definitions
  @definitions
end

#iso_dirObject

Returns the value of attribute iso_dir.



32
33
34
# File 'lib/veewee/environment.rb', line 32

def iso_dir
  @iso_dir
end

#loglevelObject

Returns the value of attribute loglevel.



21
22
23
# File 'lib/veewee/environment.rb', line 21

def loglevel
  @loglevel
end

#ostypesObject (readonly)

Hash elelement of all OStypes



52
53
54
# File 'lib/veewee/environment.rb', line 52

def ostypes
  @ostypes
end

#providersObject

Hash element of all templates available



49
50
51
# File 'lib/veewee/environment.rb', line 49

def providers
  @providers
end

#template_pathObject

This initializes a new Veewee Environment settings argument is a hash with the following options

  • :definition_dir : where definitions are located

  • :template_path : paths that contains the template definitions that come with the veewee gem, defaults to the path relative to the gemfiles

  • :iso_dir : directory to look for iso files, defaults to $environment_dir/iso

  • :validation_dir : directory that contains a list of validation tests, that can be run after building a box

  • :tmp_dir : directory that will be used for creating temporary files, needs to be rewritable, default to $environment_dir/tmp



30
31
32
# File 'lib/veewee/environment.rb', line 30

def template_path
  @template_path
end

#templatesObject

Hash element of all templates available



46
47
48
# File 'lib/veewee/environment.rb', line 46

def templates
  @templates
end

#tmp_dirObject

Returns the value of attribute tmp_dir.



34
35
36
# File 'lib/veewee/environment.rb', line 34

def tmp_dir
  @tmp_dir
end

#uiUI

Returns the UI for the environment, which is responsible for talking with the outside world.

Returns:



127
128
129
# File 'lib/veewee/environment.rb', line 127

def ui
  @ui ||= UI.new(self)
end

#validation_dirObject

Returns the value of attribute validation_dir.



33
34
35
# File 'lib/veewee/environment.rb', line 33

def validation_dir
  @validation_dir
end

#veewee_filenameObject

The valid name for a Veeweefile for this environment



19
20
21
# File 'lib/veewee/environment.rb', line 19

def veewee_filename
  @veewee_filename
end

Instance Method Details

#cli(*args) ⇒ Object

Makes a call to the CLI with the given arguments as if they came from the real command line (sometimes they do!). An example:

env.cli("package", "--veeweefile", "Veeweefie")


175
176
177
# File 'lib/veewee/environment.rb', line 175

def cli(*args)
  CLI.start(args.flatten, :env => self)
end

#load!Object

Loads this entire environment, setting up the instance variables such as ‘vm`, `config`, etc. on this environment. The order this method calls its other methods is very particular.



146
147
148
149
150
151
152
153
154
155
# File 'lib/veewee/environment.rb', line 146

def load!
  if !loaded?
    @loaded = true

    logger.info("environment") { "Loading configuration..." }
    load_config!

    self
  end
end

#load_config!Object



157
158
159
160
161
# File 'lib/veewee/environment.rb', line 157

def load_config!
  @config = Config.new({ :env => self }).load_veewee_config()

  return self
end

#loaded?Bool

Returns a boolean representing if the environment has been loaded or not.

Returns:

  • (Bool)


139
140
141
# File 'lib/veewee/environment.rb', line 139

def loaded?
  !!@loaded
end

#loggerLogger

Accesses the logger for Veewee. This logger is a detailed logger which should be used to log internals only. For outward facing information, use #ui.

Returns:

  • (Logger)


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/veewee/environment.rb', line 188

def logger
  return @logger if @logger

  # Figure out where the output should go to.
  output = nil
  if ENV["VEEWEE_LOG"] == "STDOUT"
    output = STDOUT
  elsif ENV["VEEWEE_LOG"] == "NULL"
    output = nil
  elsif ENV["VEEWEE_LOG"]
    output = ENV["VEEWEE_LOG"]
  else
    output = nil #log_path.join("#{Time.now.to_i}.log")
  end

  # Create the logger and custom formatter
  @logger = ::Logger.new(output)
  @logger.formatter = Proc.new do |severity, datetime, progname, msg|
    "#{datetime} - #{progname} - [#{resource}] #{msg}\n"
  end

  @logger
end

#reload_config!Object

Reloads the configuration of this environment.



164
165
166
167
168
# File 'lib/veewee/environment.rb', line 164

def reload_config!
  @config = nil
  load_config!
  self
end

#resourceObject



179
180
181
# File 'lib/veewee/environment.rb', line 179

def resource
  "veewee"
end