Top Level Namespace

Defined Under Namespace

Modules: VagrantPlugins

Instance Method Summary collapse

Instance Method Details

#apply_aws_provider(node, settings) ⇒ Object



178
179
180
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
213
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 178

def apply_aws_provider(node, settings)
  return unless settings["aws"]
  node.vm.provider :aws do |aws, override|
    override.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"

    # Required parameters
    aws.ami = settings["aws"]["ami"] if settings["aws"]["ami"]
    aws.instance_type = settings["aws"]["instance_type"] if settings["aws"]["instance_type"]
    aws.keypair_name = settings["aws"]["keypair_name"] if settings["aws"]["keypair_name"]
    override.ssh.username = settings["aws"]["username"] if settings["aws"]["username"]
    override.ssh.private_key_path = settings["aws"]["private_key_path"] || ENV["AWS_PRIVATE_KEY_PATH"]

    # Alternative approach: add keys into your .bashrc or .zshrc profile
    # export AWS_SECRET_KEY=secret_key
    # export AWS_ACCESS_KEY=secret_key
    aws.access_key_id = settings["aws"]["access_key_id"] || ENV["AWS_ACCESS_KEY"]
    aws.secret_access_key = settings["aws"]["secret_access_key"] || ENV["AWS_SECRET_KEY"]
    aws.session_token = settings["aws"]["session_token"] || ENV["AWS_SESSION_TOKEN"]

    # optional settings
    aws.region = settings["aws"]["region"] if settings["aws"]["region"]
    aws.availability_zone = settings["aws"]["availability_zone"] if settings["aws"]["availability_zone"]
    aws.security_groups = settings["aws"]["security_groups"] if settings["aws"]["security_groups"]
    aws.tags = settings["aws"]["tags"] if settings["aws"]["tags"]
    aws.subnet_id = settings["aws"]["subnet_id"] if settings["aws"]["subnet_id"]
    aws.availability_zone = settings["aws"]["availability_zone"] if settings["aws"]["availability_zone"]
    aws.elastic_ip = settings["aws"]["elastic_ip"] if settings["aws"]["elastic_ip"]
    aws.use_iam_profile = settings["aws"]["use_iam_profile"] if settings["aws"]["use_iam_profile"]
    aws.private_ip_address = settings["aws"]["private_ip_address"] if settings["aws"]["private_ip_address"]
    aws.user_data = settings["aws"]["user_data"] if settings["aws"]["user_data"]
    aws.iam_instance_profile_name = settings["aws"]["iam_instance_profile_name"] if settings["aws"]["iam_instance_profile_name"]
    aws.iam_instance_profile_arn = settings["aws"]["iam_instance_profile_arn"] if settings["aws"]["iam_instance_profile_arn"]
    aws.instance_package_timeout = settings["aws"]["instance_package_timeout"] if settings["aws"]["instance_package_timeout"]
    aws.instance_ready_timeout = settings["aws"]["instance_ready_timeout"] if settings["aws"]["instance_ready_timeout"]
  end
end

#apply_digitalocean_providier(node, settings) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 215

def apply_digitalocean_providier(node, settings)
  return unless settings["digital_ocean"]

  # DEBUG:
  # puts "digital_ocean", settings["digital_ocean"]

  node.vm.provider :digital_ocean do |digital_ocean, override|
    override.vm.box_url = "https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box"

    digital_ocean.token = settings["digital_ocean"].include?("token") ? settings["digital_ocean"]["token"] : ENV["DIGITAL_OCEAN_TOKEN"]

    override.ssh.private_key_path = settings["digital_ocean"]["private_key_path"] || ENV["DIGITAL_OCEAN_PRIVATE_KEY_PATH"]
    override.ssh.username = settings["digital_ocean"]["username"] if settings["digital_ocean"]["username"]
    digital_ocean.ssh_key_name = settings["digital_ocean"].include?("ssh_key_name") ? settings["digital_ocean"]["ssh_key_name"] : ENV["DIGITAL_OCEAN_SSH_KEY_NAME"]

    # Optional
    digital_ocean.image = settings["digital_ocean"].include?("image") ? settings["digital_ocean"]["image"] : "ubuntu-14-04-x64"
    digital_ocean.region = settings["digital_ocean"].include?("region") ? settings["digital_ocean"]["region"] : "nyc2"
    digital_ocean.size = settings["digital_ocean"].include?("size") ? settings["digital_ocean"]["size"] : "512mb"
    digital_ocean.ipv6 = settings["digital_ocean"].include?("ipv6") ? settings["digital_ocean"]["ipv6"] : false
    digital_ocean.private_networking = settings["digital_ocean"].include?("private_networking") ? settings["digital_ocean"]["private_networking"] : false
    digital_ocean.backups_enabled = settings["digital_ocean"].include?("backups_enabled") ? settings["digital_ocean"]["backups_enabled"] : false
    digital_ocean.setup = settings["digital_ocean"].include?("setup") ? settings["digital_ocean"]["setup"] : true
  end
end

#apply_local_scripts(node, settings) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 277

def apply_local_scripts(node, settings)
  node.vm.synced_folder @template_path, @local_template_path

  node.vm.provision "shell", inline: "touch #{@temp_path} && chmod +x #{@temp_path}"

  if settings["timezone"]
    node.vm.provision "shell" do |s|
      s.path = "#{@scripts_path}/tz.sh"
      s.args = [settings["timezone"]]
    end
  end

  if settings["update"]
    node.vm.provision "shell", path: "#{@scripts_path}/base.sh"
  end

  # if settings["rvm"]
  #   node.vm.provision "shell", path: "#{@scripts_path}/rvm.sh"
  # end

  if settings["git"]
    if File.exists? settings["git"].to_s
      gitconfig = settings["git"]
    else
      gitconfig = "#{@local_template_path}/gitconfig.conf"
    end

    node.vm.provision "shell" do |s|
      s.path = "#{@scripts_path}/git.sh"
      s.args = [gitconfig]
    end
  end

  if settings["mysql_root_password"]
    node.vm.provision "shell", inline: "echo export MYSQL_ROOT_PASSWORD=#{settings["mysql_root_password"]} >> #{@temp_path}"
  end

  if settings["databases"]
    node.vm.provision "shell", path: "#{@scripts_path}/mysql.sh"
    node.vm.provision "shell" do |s|
      s.path = "#{@scripts_path}/mysql-create-databases.sh"
      s.args = settings["databases"]
    end
  end

  if settings["variables"]
    settings["variables"].each do |pair|
      node.vm.provision "shell" do |s|
        s.path = "#{@scripts_path}/export_vars.sh"
        s.args = [ pair["key"], pair["value"] ]
      end
    end
  end

  if settings["nodejs"]
    node.vm.provision "shell", path: "#{@scripts_path}/nodejs.sh"

    if settings["nodejs"].kind_of?(Hash)
      settings["nodejs"].each do |key, value|
        if "global" == key
          node.vm.provision "shell" do |s|
            s.path = "#{@scripts_path}/install-nodejs-modules.sh"
            s.args = [value]
          end
        end
      end
    end
  end

  @profile = settings["webserver"] if settings["webserver"]
  @profile = settings["profile"] if settings["profile"]

  if File.exists?("#{@scripts_path}/#{@profile}.sh")
    node.vm.provision "shell", path: "#{@scripts_path}/#{@profile}.sh"
  end

  if settings["php"]
    node.vm.provision "shell", path: "#{@scripts_path}/php.sh"
    node.vm.provision "shell" do |s|
      s.path = "#{@scripts_path}/php_configure.sh"
      s.args = ["master"]
    end

    if settings["php"].include?('xdebug')
      xdebugconfig = "#{@local_template_path}/xdebug.ini"

      node.vm.provision "shell" do |s|
        s.path = "#{@scripts_path}/php_xdebug.sh"
        s.args = [xdebugconfig]
      end
    end

    node.vm.provision "shell", path: "#{@scripts_path}/composer.sh" if settings["php"].include?('composer')

    node.vm.provision "shell", path: "#{@scripts_path}/wpcli.sh" if settings["php"].include?('wp')
  end

  if settings["sites"]
    settings["sites"].each do |opts|
      opts = Hash[opts.map{ |key, value| [key.to_sym, value] }]
      @site = create_site(opts)

      next unless ! @site[:host].empty? && ! @site[:path].empty? && ! @site[:config].empty?

      backup_file = "#{Dir.pwd}/.#{@profile}-#{@site[:host]}.conf"
      File.open(backup_file, File::RDWR|File::CREAT) do |file|
        file.write(@site[:config].to_s)
      end

      node.vm.provision "shell" do |s|
        s.name = "nginx_site.sh: #{@site[:host]}, #{@site[:path]}"
        s.path = "#{@scripts_path}/nginx_site.sh"
        s.args = ["#{@site[:host]}", "#{@site[:path]}", "#{@site[:config]}"]
      end
    end
  end

  node.vm.provision "shell", inline: "rm -rf /etc/profile.d/vagrant.sh"

end

#apply_vagrant_hostupdater(node, settings) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 48

def apply_vagrant_hostupdater(node, settings)
  return if ! defined? VagrantPlugins::HostsUpdater

  return if !! ENV['VAGRANT_DEFAULT_PROVIDER'].to_s.index('digital_ocean')

  return if ! get_cli_flag('digital_ocean')

  if settings["hostname"] && settings["ip"]
    sites = get_config_part('sites', settings)
    node.hostsupdater.aliases = sites
  else
    puts "yarrs-and-yamls: ERROR - vagrant-hostsupdater not installed. Please update your /etc/hosts file for: #{settings}"
  end
end

#apply_vagrant_settings(node, settings) ⇒ Object



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
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 71

def apply_vagrant_settings(node, settings)
  node.vm.box = settings["box"]
  node.vm.box_url = settings["box_url"]
  node.vm.box_check_update = settings["box_check_update"]
  node.vm.boot_timeout = settings["boot_timeout"]
  node.vm.graceful_halt_timeout = settings["graceful_halt_timeout"]
  node.vm.guest = settings["guest"]
  node.vm.post_up_message = settings["post_up_message"]
  node.vm.usable_port_range = settings["usable_port_range"]

  node.vm.provider :virtualbox do |virtualbox, override|
    virtualbox.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
    virtualbox.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    virtualbox.customize ["modifyvm", :id, "--ostype", "Ubuntu_64"]
  end

  node.vm.provider :virtualbox do |virtualbox, override|
    virtualbox.gui = true if settings["gui"]
    virtualbox.cpus = settings["cpus"] if settings["cpus"]
    virtualbox.memory = settings["memory"] if settings["memory"]
  end

  ["vmware_fusion", "vmware_workstation"].each do |vmware|
    node.vm.provider vmware do |vmware, override|
      vmware.vmx["displayName"] = settings["hostname"]
      override.vm.node_url = "http://files.vagrantup.com/precise64_vmware.node"
      vmware.gui = true if settings["gui"]
      vmware.vmx["numvcpus"] = settings["cpus"] if settings["cpus"]
      vmware.vmx["memsize"] = settings["cpus"] if settings["memory"]
    end
  end

  node.vm.network "private_network", ip: settings["ip"], :netmask => "255.255.255.0" if settings["ip"]

  if settings["ports"]
      settings["ports"].each do |args|
        args = Hash[args.map{ |key, value| [key.to_sym, value] }]
        node.vm.network "forwarded_port", args
      end
  end

end

#apply_vagrant_shared_folders(node, settings) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 165

def apply_vagrant_shared_folders(node, settings)
  return unless settings["synced_folders"]

  settings["synced_folders"].each do |item|
    next unless ( item.include?("host") or item.include?("guest") )
    args = item.dup
    args.delete('host')
    args.delete('guest')
    args = Hash[args.map{ |key, value| [key.to_sym, value] }]
    node.vm.synced_folder item["host"], item["guest"], args
  end
end

#apply_vagrant_shell_provisioning(node, settings) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 148

def apply_vagrant_shell_provisioning(node, settings)
  return unless settings["provision"]

  settings["provision"].each do |script|
    node.vm.provision "shell" do |s|
        s.inline = script["inline"] if script["inline"]
        s.path = script["path"] if script["path"]
        s.args = script["args"] if script["args"]
        s.privileged = script["privileged"] if script["privileged"]
        s.binary = script["binary"] if script["binary"]
        s.upload_path = script["upload_path"] if script["upload_path"]
        s.keep_color = script["keep_color"] if script["keep_color"]
        s.powershell_args = script["powershell_args"] if script["powershell_args"]
    end
  end
end

#apply_vagrant_ssh_settings(node, settings) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 114

def apply_vagrant_ssh_settings(node, settings)
  node.ssh.username = settings["ssh_username"] if settings["ssh_username"]
  node.ssh.password = settings["ssh_password"] if settings["ssh_password"]
  node.ssh.host = settings["ssh_host"] if settings["ssh_host"]
  node.ssh.port = settings["ssh_port"] if settings["ssh_port"]
  node.ssh.guest_port = settings["ssh_guest_port"] if settings["ssh_guest_port"]
  node.ssh.private_key_path = settings["ssh_private_key_path"] if settings["ssh_private_key_path"]
  node.ssh.forward_agent = settings["ssh_forward_agent"] if settings["ssh_forward_agent"]
  node.ssh.forward_x11 = settings["ssh_forward_x11"] if settings["ssh_forward_x11"]
  node.ssh.insert_key = settings["ssh_insert_key"] if settings["ssh_insert_key"]
  node.ssh.proxy_command = settings["ssh_proxy_command"] if settings["ssh_proxy_command"]
  node.ssh.pty = settings["ssh_pty"] if settings["ssh_pty"]
  node.ssh.shell = settings["ssh_shell"] if settings["ssh_shell"]

  # Configure The Public Key For SSH Access
  if settings["authorize"]
    config.vm.provision "shell" do |s|
      s.inline = "echo $1 | grep -xq \"$1\" /home/vagrant/.ssh/authorized_keys || echo $1 | tee -a /home/vagrant/.ssh/authorized_keys"
      s.args = [File.read(File.expand_path(settings["authorize"]))]
    end
  end

  # Copy The SSH Private Keys To The Box
  if settings["keys"]
    settings["keys"].each do |key|
      config.vm.provision "shell" do |s|
        s.privileged = false
        s.inline = "echo \"$1\" > /home/vagrant/.ssh/$2 && chmod 600 /home/vagrant/.ssh/$2"
        s.args = [File.read(File.expand_path(key)), key.split('/').last]
      end
    end
  end
end

#create_site(opts) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 419

def create_site(opts)
  site = {
    host: '',
    path: '',
    port: 80,
    ssl: false,
    template: "#{@template_path}/nginx_vhost.erb",
    config: '',
    vhost: '',
  }

  opts[:host] = opts[:map] || opts[:host]
  opts[:path] = opts[:to] || opts[:path]

  opts.each do |key, value|
    if !!key.to_s.index('.')
      site[:host] = key
    end
    if !!value.to_s.index('/')
      opts[:path] = value
    end
  end

  site.each do |key, val|
    if opts[key.to_sym]
      site[key.to_sym] = opts[key.to_sym]
    end
  end

  site[:config] = ERB.new(File.read(site[:template]))
  .result(OpenStruct.new(site).instance_eval { binding })

  site
end

#get_cli_flag(flag) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 63

def get_cli_flag(flag)
    ARGV.each do |arg|
        return true if arg.index(flag)
    end

    return false
end

#get_config_from_file(configfile) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 241

def get_config_from_file(configfile)

  config = {}

  return config unless configfile.index('.yaml')

  configfile = File.expand_path(configfile)
  localfile  = File.expand_path(configfile.dup)
  localfile  = localfile.insert(configfile.index('.yaml'), '.local')

  # DEBUG:
  # puts "configfile", configfile
  # puts "localfile", localfile

  if File.exists? localfile
      config = YAML.load_file(localfile)
  elsif File.exists? configfile
      config = YAML.load_file(configfile)
  end

  config
end

#get_config_part(key, config) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 398

def get_config_part(key, config)
  result = nil

  if 'sites' == key && config.include?(key)
    sites = []

    config["sites"].each do |opts|
      opts = Hash[opts.map{ |key, value| [key.to_sym, value] }]
      site = create_site(opts)

      next unless ! site[:host].empty? && ! site[:path].empty? && ! site[:config].empty?

      sites.push(site[:host])
    end

    result = sites
  end

  result
end

#prepare(v = nil) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 264

def prepare(v=nil)
    boxes = {}

    return boxes if v["nodes"].empty?

    v["nodes"].collect do |box|
        next unless box["hostname"]
        boxes[box["hostname"]] = box
    end

    boxes
end

#yarrs(yamlfile, vagrant_config) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vagrant-yarrs-and-yamls/api.rb', line 11

def yarrs(yamlfile, vagrant_config)
    raise 'Missing required parameters, try: yarrs_and_yamls(\'Yarrs.yaml\', config)' if ! vagrant_config

    # load yaml config from file provided in Vagrantfile
    config = get_config_from_file(yamlfile)

    # convert single node to multi-node data structure
    config = { "nodes" => [config] } unless config.include?('nodes')

    # format nodes into instances
    instances = prepare(config)

    # DEBUG:
    # puts "instances", instances

    # loop through each instance and apply settings
    instances.each do |hostname, settings|

        vagrant_config.vm.define hostname do |node|

          apply_vagrant_settings(node, settings)
          apply_vagrant_ssh_settings(node, settings)
          apply_vagrant_shell_provisioning(node, settings)
          apply_vagrant_shared_folders(node, settings)
          apply_aws_provider(node, settings)
          apply_digitalocean_providier(node, settings)
          apply_local_scripts(node, settings)
          apply_vagrant_hostupdater(node, settings)

        end

        yield hostname, settings if block_given?
    end

    instances
end