Class: Veewee::Environment

Inherits:
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Environment

Returns a new instance of Environment.



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
108
# File 'lib/veewee/environment.rb', line 57

def initialize(options = {})
  # symbolify commandline options
  options = options.inject({}) {|result,(key,value)| result.update({key.to_sym => value})}

  # If a cwd was provided as option it overrules the default
  # cwd is again merged later with all options but it has to merged here
  # because several defaults are generated from it
  cwd = options[:cwd] || Veewee::Environment.workdir

  defaults = {
    :cwd => cwd,
    :veewee_filename => "Veeweefile",
    :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)

  @config_filepath = File.join(options[:cwd], options[:veewee_filename])

  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)

  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)


38
39
40
# File 'lib/veewee/environment.rb', line 38

def config
  @config
end

#config_filepathObject (readonly)

Path to the config file



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

def config_filepath
  @config_filepath
end

#current_providerObject

Returns the value of attribute current_provider.



55
56
57
# File 'lib/veewee/environment.rb', line 55

def current_provider
  @current_provider
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.



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

def definition_dir
  @definition_dir
end

#definitionsObject

Hash element of all definitions available



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

def definitions
  @definitions
end

#iso_dirObject

Returns the value of attribute iso_dir.



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

def iso_dir
  @iso_dir
end

#ostypesObject (readonly)

Hash element of all OS types



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

def ostypes
  @ostypes
end

#providersObject

Hash element of all providers available



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

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



28
29
30
# File 'lib/veewee/environment.rb', line 28

def template_path
  @template_path
end

#templatesObject

Hash element of all templates available



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

def templates
  @templates
end

#tmp_dirObject

Returns the value of attribute tmp_dir.



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

def tmp_dir
  @tmp_dir
end

#uiUI

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

Returns:



132
133
134
# File 'lib/veewee/environment.rb', line 132

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

#validation_dirObject

Returns the value of attribute validation_dir.



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

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

Class Method Details

.workdirObject



110
111
112
# File 'lib/veewee/environment.rb', line 110

def self.workdir
  ENV['VEEWEE_DIR'] || Dir.pwd
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")


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

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

#get_box(name) ⇒ Object

Get box from current provider



214
215
216
217
218
219
220
# File 'lib/veewee/environment.rb', line 214

def get_box(name)
  if current_provider.nil?
    raise "Provider is unset in the environment."
  else
    providers[current_provider].get_box(name)
  end
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.



151
152
153
154
155
156
157
158
159
160
# File 'lib/veewee/environment.rb', line 151

def load!
  if !loaded?
    @loaded = true

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

    self
  end
end

#load_config!Object



162
163
164
165
# File 'lib/veewee/environment.rb', line 162

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)


144
145
146
# File 'lib/veewee/environment.rb', line 144

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)


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

def logger
  return @logger if @logger

  output = nil
  loglevel = Logger::ERROR

  # Figure out where the output should go to.
  if ENV["VEEWEE_LOG"]
    output = STDOUT
    loglevel = Logger.const_get(ENV["VEEWEE_LOG"].upcase)
  end

    # Create the logger and custom formatter
  @logger = ::Logger.new(output)
  @logger.level = loglevel
  @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.



168
169
170
171
172
# File 'lib/veewee/environment.rb', line 168

def reload_config!
  @config = nil
  load_config!
  self
end

#resourceObject



183
184
185
# File 'lib/veewee/environment.rb', line 183

def resource
  "veewee"
end