Class: Vagrant::Foodtaster::Server::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-foodtaster-server/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Server

Returns a new instance of Server.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vagrant-foodtaster-server/server.rb', line 5

def initialize(app, env)
  @env = env
  @app = app

  begin
    require 'sahara/session/virtualbox'
  rescue LoadError
    raise RuntimeError, <<-EOT.strip_heredoc
      Cannot find `sahara' plugin. Please, make sure that `sahara' plugin is installed using command:
      $ vagrant plugin list

      If `sahara' plugin is not installed, install it:
      $ vagrant plugin install sahara
    EOT
  end
end

Instance Method Details

#execute_command_on_vm(vm_name, command) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/vagrant-foodtaster-server/server.rb', line 122

def execute_command_on_vm(vm_name, command)
  vm = get_vm(vm_name)
  exec_result = {}

  exec_result[:exit_status] = vm.communicate.sudo(command, error_check: false) do |stream_type, data|
    exec_result[stream_type] = exec_result[stream_type].to_s + data
  end

  exec_result
end

#get_file_from_vm(vm_name, vm_fn, local_fn) ⇒ Object



93
94
95
96
# File 'lib/vagrant-foodtaster-server/server.rb', line 93

def get_file_from_vm(vm_name, vm_fn, local_fn)
  vm = get_vm(vm_name)
  vm.communicate.download(vm_fn, local_fn)
end

#initial_snapshot_made_on_vm?(vm_name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/vagrant-foodtaster-server/server.rb', line 55

def initial_snapshot_made_on_vm?(vm_name)
  vm = get_vm(vm_name)
  sahara_for(vm).is_snapshot_mode_on?
end

#make_initial_snapshot_on_vm(vm_name) ⇒ Object



31
32
33
34
# File 'lib/vagrant-foodtaster-server/server.rb', line 31

def make_initial_snapshot_on_vm(vm_name)
  vm = get_vm(vm_name)
  sahara_for(vm).on
end

#put_file_to_vm(vm_name, local_fn, vm_fn) ⇒ Object



88
89
90
91
# File 'lib/vagrant-foodtaster-server/server.rb', line 88

def put_file_to_vm(vm_name, local_fn, vm_fn)
  vm = get_vm(vm_name)
  vm.communicate.upload(local_fn, vm_fn)
end

#redirect_stdstreams(stdout, stderr) ⇒ Object



26
27
28
29
# File 'lib/vagrant-foodtaster-server/server.rb', line 26

def redirect_stdstreams(stdout, stderr)
  $stdout = stdout
  $stderr = stderr
end

#rollback_vm(vm_name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vagrant-foodtaster-server/server.rb', line 60

def rollback_vm(vm_name)
  vm = get_vm(vm_name)

  sahara_for(vm).rollback

  # wait for SSH connection
  # workaround for MacOS issue
  retry_number = 0
  while !vm.communicate.ready? && retry_number < 20
    sleep 0.5
    retry_number += 1
  end
end

#run_chef_on_vm(vm_name, current_run_config) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/vagrant-foodtaster-server/server.rb', line 102

def run_chef_on_vm(vm_name, current_run_config)
  vm = get_vm(vm_name)

  validate_vm!(vm)

  chef_solo_config = vm.config.vm.provisioners.find { |p| p.name == :chef_solo }

  provisioner_klass = Vagrant.plugin("2").manager.provisioners[:chef_solo]
  provisioner = provisioner_klass.new(vm, chef_solo_config.config)

  current_run_chef_solo_config = apply_current_run_config(vm.config, current_run_config)
  provisioner.configure(current_run_chef_solo_config)

  begin
    provisioner.provision
  rescue StandardError => e
    raise RuntimeError, "Chef Run failed on #{vm_name} with config #{current_run_config.inspect}.\n\nOriginal Exception was:\n#{e.class.name}\n#{e.message}"
  end
end

#shutdown_vm(vm_name) ⇒ Object



74
75
76
77
78
# File 'lib/vagrant-foodtaster-server/server.rb', line 74

def shutdown_vm(vm_name)
  vm = get_vm(vm_name)

  vm.action(:halt)
end

#start_vm(vm_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vagrant-foodtaster-server/server.rb', line 36

def start_vm(vm_name)
  vm = get_vm(vm_name)
  chef_run_list = get_chef_solo_run_list(vm)
  provision_types = [:shell, :puppet]

  unless chef_run_list.empty?
    provision_types << :chef_solo
  end

  vm.action(:up,
            provision_types: provision_types,
            provision_enabled: true)
end

#versionObject



22
23
24
# File 'lib/vagrant-foodtaster-server/server.rb', line 22

def version
  Vagrant::Foodtaster::Server::VERSION
end

#vm_defined?(vm_name) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/vagrant-foodtaster-server/server.rb', line 98

def vm_defined?(vm_name)
  @env.machine_names.include?(vm_name)
end

#vm_ip(vm_name) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/vagrant-foodtaster-server/server.rb', line 80

def vm_ip(vm_name)
  vm = get_vm(vm_name)
  networks = vm.config.vm.networks
  private_network_conf = networks.find { |n| n.first == :private_network }

  private_network_conf ? private_network_conf.last[:ip] : nil
end

#vm_running?(vm_name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/vagrant-foodtaster-server/server.rb', line 50

def vm_running?(vm_name)
  vm = get_vm(vm_name)
  vm.state.id.to_s == 'running'
end