Class: Tenderloin::Env

Inherits:
Object
  • Object
show all
Extended by:
Util
Defined in:
lib/tenderloin/env.rb

Constant Summary collapse

BOXFILE_NAME =
"Tenderfile"
HOME_SUBDIRS =
["tmp", "boxes"]
@@persisted_vm =

Initialize class variables used

nil
@@root_path =
nil
@@box =
nil

Class Method Summary collapse

Methods included from Util

error_and_exit, included, logger, wrap_output

Class Method Details

.boxObject



14
15
16
17
# File 'lib/tenderloin/env.rb', line 14

def box
  load_box! unless @@box
  @@box
end

.boxes_pathObject



27
# File 'lib/tenderloin/env.rb', line 27

def boxes_path; File.join(home_path, "boxes"); end

.dotfile_pathObject



20
21
22
23
24
# File 'lib/tenderloin/env.rb', line 20

def dotfile_path
  loinsplit = File.split($ROOTFILE_NAME)
  loinsplit[-1] = "." + loinsplit[-1] + ".loinstate"
  File.join(root_path, *loinsplit)
end

.home_pathObject



25
# File 'lib/tenderloin/env.rb', line 25

def home_path; File.expand_path(Tenderloin.config.tenderloin.home); end

.load!Object



30
31
32
33
34
35
36
# File 'lib/tenderloin/env.rb', line 30

def load!
  load_root_path!
  load_config!
  load_home_directory!
  load_box!
  load_vm!
end

.load_box!Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/tenderloin/env.rb', line 70

def load_box!
  return unless root_path

  @@box = Box.find(Tenderloin.config.vm.box) if Tenderloin.config.vm.box

  if @@box
    logger.info("Reloading configuration to account for loaded box...")
    load_config!
  end
end

.load_config!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tenderloin/env.rb', line 38

def load_config!
  # Prepare load paths for config files
  load_paths = [File.join(PROJECT_ROOT, "config", "default.rb")]
  load_paths << File.join(box.directory, BOXFILE_NAME) if box
  load_paths << File.join(root_path, $ROOTFILE_NAME) if root_path

  # Then clear out the old data
  Config.reset!

  load_paths.each do |path|
    logger.info "Loading config from #{path}..."
    load path if File.exist?(path)
  end

  # Execute the configurations
  Config.execute!
end

.load_home_directory!Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tenderloin/env.rb', line 56

def load_home_directory!
  home_dir = File.expand_path(Tenderloin.config.tenderloin.home)

  dirs = HOME_SUBDIRS.collect { |path| File.join(home_dir, path) }
  dirs.unshift(home_dir)

  dirs.each do |dir|
    next if File.directory?(dir)

    logger.info "Creating home directory since it doesn't exist: #{dir}"
    FileUtils.mkdir_p(dir)
  end
end

.load_root_path!(path = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/tenderloin/env.rb', line 97

def load_root_path!(path=nil)
  path ||= Pathname.new(Dir.pwd)

  # Stop if we're at the root. 2nd regex matches windows drives
  # such as C:. and Z:. Portability of this check will need to be
  # researched.
  return false if path.to_s == '/' || path.to_s =~ /^[A-Z]:\.$/

  file = "#{path}/#{$ROOTFILE_NAME}"
  if File.exist?(file)
    @@root_path = path.to_s
    return true
  end

  load_root_path!(path.parent)
end

.load_vm!Object



81
82
83
84
85
86
87
88
89
# File 'lib/tenderloin/env.rb', line 81

def load_vm!
  return if !root_path || !File.file?(dotfile_path)

  File.open(dotfile_path) do |f|
    @@persisted_vm = Tenderloin::VM.find(f.read)
  end
rescue Errno::ENOENT
  @@persisted_vm = nil
end

.persist_vm(vm_id) ⇒ Object



91
92
93
94
95
# File 'lib/tenderloin/env.rb', line 91

def persist_vm(vm_id)
  File.open(dotfile_path, 'w+') do |f|
    f.write(vm_id)
  end
end

.persisted_vmObject



18
# File 'lib/tenderloin/env.rb', line 18

def persisted_vm; @@persisted_vm; end

.require_boxObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/tenderloin/env.rb', line 125

def require_box
  require_root_path

  if !box
    if !Tenderloin.config.vm.box
      error_and_exit(<<-msg)
No base box was specified! A base box is required as a staring point
for every tenderloin virtual machine. Please specify one in your Tenderfile
using `config.vm.box`
msg
    elsif !Tenderloin.config.vm.box_url
      error_and_exit(<<-msg)
Specified box `#{Tenderloin.config.vm.box}` does not exist!

The box must be added through the `tenderloin box add` command. Please view
the documentation associated with the command for more information.
msg
    end
  end
end

.require_persisted_vmObject



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/tenderloin/env.rb', line 146

def require_persisted_vm
  require_root_path

  if !persisted_vm
    error_and_exit(<<-error)
The task you're trying to run requires that the tenderloin environment
already be created, but unfortunately this tenderloin still appears to
have no box! You can setup the environment by setting up your
#{$ROOTFILE_NAME} and running `tenderloin up`
error
    return
  end
end

.require_root_pathObject



114
115
116
117
118
119
120
121
122
123
# File 'lib/tenderloin/env.rb', line 114

def require_root_path
  if !root_path
    error_and_exit(<<-msg)
A `#{$ROOTFILE_NAME}` was not found! This file is required for tenderloin to run
since it describes the expected environment that tenderloin is supposed
to manage. Please create a #{$ROOTFILE_NAME} and place it in your project
root.
msg
  end
end

.root_pathObject



19
# File 'lib/tenderloin/env.rb', line 19

def root_path; @@root_path; end

.tmp_pathObject



26
# File 'lib/tenderloin/env.rb', line 26

def tmp_path; File.join(home_path, "tmp"); end

.vms_pathObject



28
# File 'lib/tenderloin/env.rb', line 28

def vms_path; File.join(home_path, "vms"); end