Module: Katar

Defined in:
lib/katar.rb,
lib/katar/globals.rb,
lib/katar/vagrant.rb,
lib/katar/version.rb,
lib/katar/commands/application.rb,
lib/katar/commands/edit_command.rb,
lib/katar/commands/init_command.rb

Defined Under Namespace

Modules: Commands

Constant Summary collapse

DIR =
File.expand_path("~/.katar")
VERSION =
"2.2.1"

Class Method Summary collapse

Class Method Details

.configure(config, settings) ⇒ Object



2
3
4
5
6
7
8
9
10
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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/katar/vagrant.rb', line 2

def Katar.configure(config, settings)
    # Set The VM Provider
    ENV['VAGRANT_DEFAULT_PROVIDER'] = settings["provider"] = "virtualbox"

    # Configure Local Variable To Access Scripts From Remote Location
    scriptDir = File.expand_path('../../scripts', __FILE__)

    # Prevent TTY Errors
    config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'"

    # Allow SSH Agent Forward from The Box
    config.ssh.forward_agent = true

    # Configure The Box
    config.vm.box = settings["box"] ||= "acmitch/katar-box"
    config.vm.box_version = settings["version"] ||= ">= 0"
    config.vm.hostname = settings["hostname"] ||= "katar"

    # Configure A Private Network IP
    config.vm.network :private_network, ip: settings["ip"] ||= "192.168.10.11"

    # Configure A Few VirtualBox Settings
    config.vm.provider "virtualbox" do |vb|
        vb.name = settings["name"] ||= "katar"
        vb.gui = settings["desktop"] ||= false
        vb.customize ["modifyvm", :id, "--memory", settings["memory"] ||= "2048"]
        vb.customize ["modifyvm", :id, "--cpus", settings["cpus"] ||= "1"]
        vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
        vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
        vb.customize ["modifyvm", :id, "--ostype", "Ubuntu_64"]
        vb.customize ["modifyvm", :id, "--clipboard", "bidirectional"]
        vb.customize ["modifyvm", :id, "--monitorcount", settings["monitors"] ||= "1"]
    end
 
    # Configure A Few Parallels Settings
    config.vm.provider "parallels" do |v|
        v.update_guest_tools = true
        v.memory = settings["memory"] ||= 2048
        v.cpus = settings["cpus"] ||= 1
    end

    # Configure Proxies
    if settings.include? "proxies"
        if !Vagrant.has_plugin?("vagrant-proxyconf") 
            system('vagrant plugin install vagrant-proxyconf')     
            raise("vagrant-proxyconf installed. Run command again.");
        end

        settings["proxies"].each do |proxy|
            # Environment and package managers
            config.proxy.http = proxy["http"] ||= nil
            config.proxy.https = proxy["https"] ||= nil 
        end
    end 
    
    # Standardize Ports Naming Schema
    if (settings.has_key?("ports"))
        settings["ports"].each do |port|
            port["guest"] ||= port["to"]
            port["host"] ||= port["send"]
            port["protocol"] ||= "tcp"
        end
    else
        settings["ports"] = []
    end

    # Default Port Forwarding
    default_ports = {
        80   => 8000,
        443  => 44300,
        3306 => 33060,
        5432 => 54320
    }

    # Use Default Port Forwarding Unless Overridden
    default_ports.each do |guest, host|
        unless settings["ports"].any? { |mapping| mapping["guest"] == guest }
            config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
        end
    end

    # Add Custom Ports From Configuration
    if settings.has_key?("ports")
        settings["ports"].each do |port|
            config.vm.network "forwarded_port", guest: port["guest"], host: port["host"], protocol: port["protocol"], auto_correct: true
        end
    end

    # Configure The Public Key For SSH Access
    if settings.include? 'authorize'
        if File.exists? File.expand_path(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
    end

    # Copy The SSH Private Keys To The Box
    if settings.include? '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

    if settings.include? 'folders'
      settings["folders"].each do |folder|
        mount_opts = []

        if (folder["type"] == "nfs")
            mount_opts = folder["mount_options"] ? folder["mount_options"] : [ 'actimeo=' << (folder["version"] ? folder["version"] : '1'), 'nolock']
        elsif (folder["type"] == "smb")
            mount_opts = folder["mount_options"] ? folder["mount_options"] : [ 'vers=' << (folder["version"] ? folder["version"] : '3.02'), 'mfsymlinks']
        end

        # For b/w compatibility keep separate 'mount_opts', but merge with options
        options = (folder["options"] || {}).merge({ mount_options: mount_opts })

        # Double-splat (**) operator only works with symbol keys, so convert
        options.keys.each{|k| options[k.to_sym] = options.delete(k) }

        config.vm.synced_folder folder["map"], folder["to"], type: folder["type"] ||= nil, **options

        # Bindfs support to fix shared folder (NFS) permission issue on Mac
        if Vagrant.has_plugin?("vagrant-bindfs")
          config.bindfs.bind_folder folder["to"], folder["to"]
        end
      end
    end
 
    # Configure All Of The Server Environment Variables
    config.vm.provision "shell" do |s|
        s.path = scriptDir + "/clear-variables.sh"
    end

    if settings.has_key?("variables")
        settings["variables"].each do |var|
            config.vm.provision "shell" do |s|
                s.inline = "echo \"\n# Set Katar Environment Variable\nexport $1=$2\" >> /home/vagrant/.profile"
                s.args = [var["key"], var["value"]]
            end
        end
    end
     
end