Class: Vagrantomatic::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrantomatic/instance.rb

Constant Summary collapse

VAGRANTFILE =
"Vagrantfile"
VAGRANTFILE_JSON =
"#{VAGRANTFILE}.json"
MASTER_VAGRANTFILE =

We ship our own Vagrantfile with all variables externalised inside this gem and get it into position by symlinking B-)

File.join(File.dirname(File.expand_path(__FILE__)), "../../res/#{VAGRANTFILE}")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vagrant_vm_dir, name, logger: nil, config: nil) ⇒ Instance

Returns a new instance of Instance.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vagrantomatic/instance.rb', line 14

def initialize(vagrant_vm_dir, name, logger: nil, config:nil)
  @name           = name
  @vagrant_vm_dir = vagrant_vm_dir
  @logger         = ::Vagrantomatic::Logger.new(logger).logger

  # use supplied config if present, otherwise load from file
  if config
    @config = config
  else
    @config = configfile_hash
  end
  @logger.debug "initialized vagrantomatic instance for #{name}"
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/vagrantomatic/instance.rb', line 12

def config
  @config
end

Instance Method Details

#configfileObject



36
37
38
# File 'lib/vagrantomatic/instance.rb', line 36

def configfile
  File.join(vm_instance_dir, VAGRANTFILE_JSON)
end

#configfile_hashObject

return a hash of the configfile or empty hash if error encountered



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vagrantomatic/instance.rb', line 41

def configfile_hash

  config  = {}
  begin
    json    = File.read(configfile)
    config  = JSON.parse(json)
  rescue Errno::ENOENT
    @logger.debug("#{configfile} does not exist")
  rescue JSON::ParserError
    @logger.error("JSON parser error in #{configfile}")
  end
  config
end

#configured?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/vagrantomatic/instance.rb', line 55

def configured?
  configured = false
  if Dir.exists? (vm_instance_dir) and File.exists?(configfile) and File.exists?(vagrantfile)
    configured = configfile_hash.has_key?("box")
  end
  configured
end

#ensure_configObject

Vagrant to be driven from a .json config file, all the parameters are externalised here



72
73
74
75
76
77
78
# File 'lib/vagrantomatic/instance.rb', line 72

def ensure_config
  if ! in_sync?
    File.open(configfile,"w") do |f|
      f.write(@config.to_json)
    end
  end
end

#ensure_vagrantfileObject

The Vagrantfile itself is shipped as part of this module and delivered via pluginsync, so we just need to symlink it for each directory. This gives us the benefit being to update by dropping a new module too



84
85
86
# File 'lib/vagrantomatic/instance.rb', line 84

def ensure_vagrantfile
  FileUtils.ln_sf(MASTER_VAGRANTFILE, vagrantfile)
end

#execute_and_log(op) ⇒ Object



103
104
105
106
107
108
# File 'lib/vagrantomatic/instance.rb', line 103

def execute_and_log(op)
  get_vm.execute(op) { |stdout, stderr|
    # only one of these will ever be set at a time, other one is nil
    @logger.debug "#{stdout}#{stderr}".strip
  }.success?
end

#get_vmObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/vagrantomatic/instance.rb', line 89

def get_vm
  # Create an instance (represents a Vagrant **installation**)
  instance = Derelict.instance(::Vagrantomatic::Vagrantomatic::DEFAULT_VAGRANT_DIR)
  result = instance.execute('--version') # Derelict::Executer object (vagrant --version)
  if result.success?
    # vagrant present and working, connect to our vm INSTANCE
    vm = instance.connect(vm_instance_dir)
  else
    raise "Error connecting to vagrant! (vagrant --version failed)"
  end

  vm
end

#in_sync?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
119
# File 'lib/vagrantomatic/instance.rb', line 110

def in_sync?
  configured  = false
  have_config = configfile_hash

  if have_config == @config
    configured = true
  end

  configured
end

#purgeObject



129
130
131
132
133
134
# File 'lib/vagrantomatic/instance.rb', line 129

def purge
  execute_and_log(:destroy)
  if Dir.exists? vm_instance_dir
    FileUtils::rm_rf(vm_instance_dir)
  end
end

#reloadObject



136
137
138
# File 'lib/vagrantomatic/instance.rb', line 136

def reload
  execute_and_log(:reload)
end

#resetObject



140
141
142
143
# File 'lib/vagrantomatic/instance.rb', line 140

def reset
  execute_and_log(:destroy)
  execute_and_log(:up)
end

#run(command) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/vagrantomatic/instance.rb', line 145

def run(command)
  # arrayify
  command = [command]
  command.unshift("-c")

  messages = []
  vm = get_vm
  # throw the command over the wall to derelect whatever the state of instance
  # for now just support ssh - for windows we could do `powershell -c` or
  # maybe even winRM
  executor = vm.execute(:ssh, command) { |stdout,stderr|
    line = "#{stdout}#{stderr}".strip
    @logger.debug line
    messages << line
  }
  @logger.info("command '#{command}' resulted in #{messages.size} lines")
  return executor.status, messages
end

#saveObject



63
64
65
66
67
68
# File 'lib/vagrantomatic/instance.rb', line 63

def save
  @logger.debug("saving vm settings...")
  FileUtils.mkdir_p(vm_instance_dir)
  ensure_config
  ensure_vagrantfile
end

#startObject



121
122
123
# File 'lib/vagrantomatic/instance.rb', line 121

def start
  execute_and_log(:up)
end

#stopObject



125
126
127
# File 'lib/vagrantomatic/instance.rb', line 125

def stop
  execute_and_log(:suspend)
end

#vagrantfileObject



32
33
34
# File 'lib/vagrantomatic/instance.rb', line 32

def vagrantfile
  File.join(vm_instance_dir, VAGRANTFILE)
end

#vm_instance_dirObject



28
29
30
# File 'lib/vagrantomatic/instance.rb', line 28

def vm_instance_dir
  File.join(@vagrant_vm_dir, @name)
end