Class: Vagrant

Inherits:
Object
  • Object
show all
Defined in:
lib/zergrush_vagrant/init.rb

Overview

give this class the name you want for your command zergrush_vagrant

Instance Method Summary collapse

Instance Method Details

#check_dependenciesObject



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/zergrush_vagrant/init.rb', line 249

def check_dependencies
    abort("ERROR: Vagrant not installed!") unless which("vagrant") != nil
    required_plugins = ["vagrant-aws", "vagrant-omnibus", "vagrant-berkshelf"]
    if /linux|arch/i === RbConfig::CONFIG['host_os']
        required_plugins.push("vagrant-libvirt")
    end

    required_plugins.each { |plugin|
        plugin_check_pid = Process.spawn("vagrant plugin list | grep #{plugin}")
        Process.wait(plugin_check_pid)

        if $?.exitstatus != 0
            puts "Installing #{plugin}"
            plugin_check_pid = Process.spawn("vagrant plugin install #{plugin}")
            Process.wait(plugin_check_pid)
            abort("ERROR: #{plugin} installation failed!") unless $?.exitstatus == 0
        end
    }
end

#clean(hive_location, task_name, task_hash, debug) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/zergrush_vagrant/init.rb', line 129

def clean hive_location, task_name, task_hash, debug
    check_dependencies
    puts("Cleaning task #{task_name} ...")

    renderer = Renderer.new(
        hive_location, 
        task_name, 
        task_hash)        
    renderer.render

    # run vagrant cleanup
    debug_string = (debug == true) ? " --debug" : ""
    
    last_defined_driveroption = nil

    for index in 0..task_hash["num_instances"] - 1

        # grab last defined driver options set, or keep the current one
        last_defined_driveroption = (task_hash["vm"]["driver"]["driveroptions"][index] == nil) ? last_defined_driveroption : task_hash["vm"]["driver"]["driveroptions"][index]

        cleanup_pid = Process.spawn(
            {
                "VAGRANT_CWD" => File.join("#{hive_location}", "driver", task_hash["vm"]["driver"]["drivertype"], task_name),
                "VAGRANT_DEFAULT_PROVIDER" => last_defined_driveroption["providertype"]
            },
            "vagrant destroy zergling_#{index} --force#{debug_string}")
        Process.wait(cleanup_pid)
        abort("ERROR: vagrant failed!") unless $?.exitstatus == 0
    end

    last_defined_driveroption = nil
    
    # last explicitly defined vm instance. 
    last_defined_vm = nil

    for index in 0..task_hash["num_instances"] - 1
        # grab last defined driver options set, or keep the current one
        last_defined_driveroption = (task_hash["vm"]["driver"]["driveroptions"][index] == nil) ? last_defined_driveroption : task_hash["vm"]["driver"]["driveroptions"][index]

        # grab last defined vm instance, or keep the current one
        last_defined_vm = (task_hash["vm"]["instances"][index] == nil) ? last_defined_vm : task_hash["vm"]["instances"][index]
        
        boxname = Digest::SHA1.hexdigest "#{task_name}#{last_defined_driveroption["providertype"]}#{last_defined_vm["basebox"]}"
        cleanup_pid = Process.spawn(
            {
                "VAGRANT_CWD" => File.join("#{hive_location}", "driver", task_hash["vm"]["driver"]["drivertype"], task_name)
            },
            "vagrant box remove #{boxname} #{last_defined_driveroption["providertype"]}#{debug_string}")
        Process.wait(cleanup_pid)
    end
end

#folder_schemaObject



226
227
228
# File 'lib/zergrush_vagrant/init.rb', line 226

def folder_schema
    return File.open(File.join("#{File.dirname(__FILE__)}", "..", "..", "resources", "folders_schema.template"), 'r').read
end

#halt(hive_location, task_name, task_hash, debug) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/zergrush_vagrant/init.rb', line 181

def halt hive_location, task_name, task_hash, debug
    check_dependencies
    puts("Halting all vagrant virtual machines for task #{task_name} ...")
    
    renderer = Renderer.new(
        hive_location, 
        task_name, 
        task_hash)        
    renderer.render

    debug_string = (debug == true) ? " --debug" : ""  

    # halt all machines
    halt_pid = nil

    last_defined_driveroption = nil

    for index in 0..task_hash["num_instances"] - 1

        # grab last defined driver options set, or keep the current one
        last_defined_driveroption = (task_hash["vm"]["driver"]["driveroptions"][index] == nil) ? last_defined_driveroption : task_hash["vm"]["driver"]["driveroptions"][index]
        
        halt_pid = Process.spawn(
            {
                "VAGRANT_CWD" => File.join("#{hive_location}", "driver", task_hash["vm"]["driver"]["drivertype"], task_name),
                "VAGRANT_DEFAULT_PROVIDER" => last_defined_driveroption["providertype"]
            },
            "vagrant halt zergling_#{index}#{debug_string}")
        Process.wait(halt_pid)
        abort("ERROR: vagrant halt failed on machine zergling_#{index}!") unless $?.exitstatus == 0
    end
end

#option_schemaObject



222
223
224
# File 'lib/zergrush_vagrant/init.rb', line 222

def option_schema
    return File.open(File.join("#{File.dirname(__FILE__)}", "..", "..", "resources", "option_schema.template"), 'r').read
end

#port_schemaObject



230
231
232
# File 'lib/zergrush_vagrant/init.rb', line 230

def port_schema
    return File.open(File.join("#{File.dirname(__FILE__)}", "..", "..", "resources", "ports_schema.template"), 'r').read
end

#rush(hive_location, task_name, task_hash, debug) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/zergrush_vagrant/init.rb', line 30

def rush hive_location, task_name, task_hash, debug
    check_dependencies
    puts ("Will perform task #{task_name} with contents:\n #{task_hash.ai}")

    renderer = Renderer.new(
        hive_location, 
        task_name, 
        task_hash)        
    renderer.render

    debug_string = (debug == true) ? " --debug" : ""                

    # last explicitly defined driver option set
    last_defined_driveroption = nil

    # bring up all of the VMs first.
    puts("Starting vagrant in #{File.join("#{hive_location}", "driver", task_hash["vm"]["driver"]["drivertype"], task_name)}")
    for index in 0..task_hash["num_instances"] - 1

        # grab last defined driver options set, or keep the current one
        last_defined_driveroption = (task_hash["vm"]["driver"]["driveroptions"][index] == nil) ? last_defined_driveroption : task_hash["vm"]["driver"]["driveroptions"][index]

        create_pid = Process.spawn(
            {
                "VAGRANT_CWD" => File.join("#{hive_location}", "driver", task_hash["vm"]["driver"]["drivertype"], task_name),
                "VAGRANT_DEFAULT_PROVIDER" => last_defined_driveroption["providertype"]
            },
            "vagrant up zergling_#{index} --no-provision#{debug_string}")
        Process.wait(create_pid)
        
        if $?.exitstatus != 0
            puts "Vagrant failed while creating one of the VMs. Will clean task #{task_name}:"
            clean(hive_location, task_name, task_hash, debug)
            abort("ERROR: vagrant failed!")
        end
    end

    last_defined_driveroption = nil
    puts("Running tasks in vagrant virtual machines...")
    # and provision them all at once (sort of)
    provisioners = Array.new
    provision_pid = nil
    for index in 0..task_hash["num_instances"] - 1
        # grab last defined driver options set, or keep the current one
        last_defined_driveroption = (task_hash["vm"]["driver"]["driveroptions"][index] == nil) ? last_defined_driveroption : task_hash["vm"]["driver"]["driveroptions"][index]

        provision_pid = Process.spawn(
            {
                "VAGRANT_CWD" => File.join("#{hive_location}", "driver", task_hash["vm"]["driver"]["drivertype"], task_name),
                "VAGRANT_DEFAULT_PROVIDER" => last_defined_driveroption["providertype"]
            },
            "vagrant provision zergling_#{index}#{debug_string}")
        provisioners.push({:name => "zergling_#{index}", :pid => provision_pid})
    end

    # wait for everything to finish...
    errors = Array.new
    lock = Mutex.new
    provisioners.each { |provisioner| 
        Thread.new { 
            Process.wait(provisioner[:pid]); 
            lock.synchronize do
                errors.push(provisioner[:name]) unless $?.exitstatus == 0    
            end
        }.join 
    }

    # halt only the machines that are not marked as "keepalive"
    last_defined_driveroption = nil
    last_defined_vm = nil

    halt_pid = nil
    keepalive_left = false
    for index in 0..task_hash["num_instances"] - 1

        # grab last defined driver options set, or keep the current one
        last_defined_driveroption = (task_hash["vm"]["driver"]["driveroptions"][index] == nil) ? last_defined_driveroption : task_hash["vm"]["driver"]["driveroptions"][index]
        
        # grab last defined vm instance, or keep the current one
        last_defined_vm = (task_hash["vm"]["instances"][index] == nil) ? last_defined_vm : task_hash["vm"]["instances"][index]
        
        if last_defined_vm["keepalive"] != true
            halt_pid = Process.spawn(
                {
                    "VAGRANT_CWD" => File.join("#{hive_location}", "driver", task_hash["vm"]["driver"]["drivertype"], task_name),
                    "VAGRANT_DEFAULT_PROVIDER" => last_defined_driveroption["providertype"]
                },
                "vagrant halt zergling_#{index}#{debug_string}")
            Process.wait(halt_pid)
            abort("ERROR: vagrant halt failed on machine zergling_#{index}!") unless $?.exitstatus == 0
        else
            keepalive_left = true
            puts "zergling_#{index} is marked as keepalive, skipping halt..."
        end
    end
    abort("ERROR: Finished with errors in: #{errors.to_s}") unless errors.length == 0
    puts "Some instances were left running." unless keepalive_left == false
end

#snapshot(hive_location, task_name, task_hash, base) ⇒ Object



214
215
216
# File 'lib/zergrush_vagrant/init.rb', line 214

def snapshot hive_location, task_name, task_hash, base
    abort("ERROR: Not implemented!")
end

#ssh_schemaObject



234
235
236
# File 'lib/zergrush_vagrant/init.rb', line 234

def ssh_schema
    return File.open(File.join("#{File.dirname(__FILE__)}", "..", "..", "resources", "ssh_schema.template"), 'r').read
end

#task_schemaObject



218
219
220
# File 'lib/zergrush_vagrant/init.rb', line 218

def task_schema
    return File.open(File.join("#{File.dirname(__FILE__)}", "..", "..", "resources", "tasks_schema.template"), 'r').read
end

#which(cmd) ⇒ Object



238
239
240
241
242
243
244
245
246
247
# File 'lib/zergrush_vagrant/init.rb', line 238

def which(cmd)
    exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
    ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
        exts.each { |ext|
        exe = File.join(path, "#{cmd}#{ext}")
        return exe if File.executable? exe
        }
    end
  return nil
end