Class: Vagrant::Provisioners::YaybuProvisioner

Inherits:
Base
  • Object
show all
Defined in:
lib/vagrant-yaybu/provisioner.rb

Defined Under Namespace

Classes: Config

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config_classObject



104
105
106
# File 'lib/vagrant-yaybu/provisioner.rb', line 104

def self.config_class
  Config
end

Instance Method Details

#bootstrapObject



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
# File 'lib/vagrant-yaybu/provisioner.rb', line 108

def bootstrap
  ssh = env[:vm].channel
  begin
    ssh.sudo("which yaybu", :error_class => YaybuError, :_key => :yaybu_not_detected, :binary => "yaybu")
  rescue
    env[:ui].info "Yaybu not found so attempting to install it"

    if not config.yay_version then
        env[:ui].info "yay version not specified in Vagrantfile, determining from host system"
        config.yay_version = get_local_version?("yay")
    end

    if not config.yaybu_version then
        env[:ui].info "Yaybu version not specified in Vagrantfile, determining from host system"
        config.yaybu_version = get_local_version?("Yaybu")
    end

    env[:ui].info "Running apt-get updating and checking setuptools"
    ssh.sudo("apt-get update")
    ssh.sudo("apt-get install python-setuptools -y")

    env[:ui].info "Running 'easy_install yay==#{config.yay_version}'"
    ssh.sudo("easy_install yay==#{config.yay_version}")

    env[:ui].info "Running 'easy_install Yaybu==#{config.yaybu_version}'"
    ssh.sudo("easy_install Yaybu==#{config.yaybu_version}")
  end
end

#get_local_version?(mod) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
158
159
# File 'lib/vagrant-yaybu/provisioner.rb', line 152

def get_local_version?(mod)
    version = `#{config.python} -c 'print __import__("pkg_resources").get_distribution("#{mod}").version'`.strip
    if $?.to_i != 0 then
        raise YaybuError.new "Failed to get host version for '#{mod}'"
    end

    version
end

#get_vagrant_yaml?Boolean

Returns:

  • (Boolean)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/vagrant-yaybu/provisioner.rb', line 161

def get_vagrant_yaml?
    data = {}
    vagrant = data['vagrant'] = {}
    vms = vagrant['vms'] = {}

    env[:vm].env.vms.each do |name, vm|
        info = {
            "name" => vm.name.to_s,
            "interfaces" => [],
            }

        idx = 1
        vm.config.vm.networks.each do |type, opts|
            ip = opts[0]
            info["interfaces"] << {
                "name" => "eth#{idx}",
                "address" => ip,
                "netmask" => "255.255.255.0",
                "gateway" => (ip.split(".").slice(0, 3) + [1]).join("."),
                }
            idx += 1
        end

        info["interfaces"] << {
            "name" => "eth0",
            "type" => "dhcp",
            }

        vms[name.to_s] = info
    end

    name = env[:vm].name.to_s
    vagrant["vm"] = "${vagrant.vms.#{name}}"

    "#" + data.to_yaml
end

#prepareObject



137
138
# File 'lib/vagrant-yaybu/provisioner.rb', line 137

def prepare
end

#provision!Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/vagrant-yaybu/provisioner.rb', line 198

def provision!
  verify_import "yaybu"
  verify_import "yay"

  verify_local_binary "ssh"

  bootstrap

  deployment_script = TemplateRenderer.render_string($deploy_script, {
    :ssh_host => env[:vm].ssh.info[:host],
    :ssh_user => env[:vm].ssh.info[:username],
    :ssh_port => env[:vm].ssh.info[:port],
    :private_key_path => env[:vm].ssh.info[:private_key_path],
    :yay => config.yay,
    :vagrant => get_vagrant_yaml?,
    :searchpath => config.searchpath,
    :includes => config.include,
    })

  IO.popen("#{config.python} -c 'import sys; exec(sys.stdin.read())'", "r+") do |io|
    io.write(deployment_script)
    io.close_write

    while line = io.gets do
      env[:ui].info("#{line}")
    end
  end
end

#verify_import(mod) ⇒ Object



140
141
142
143
144
# File 'lib/vagrant-yaybu/provisioner.rb', line 140

def verify_import(mod)
    if not system("#{config.python}", "-c", "import #{mod}") then
      raise YaybuError.new "Module #{mod} not found"
    end
end

#verify_local_binary(binary) ⇒ Object



146
147
148
149
150
# File 'lib/vagrant-yaybu/provisioner.rb', line 146

def verify_local_binary(binary)
    if not system("which #{binary}") then
      raise YaybuError.new "Local binary #{binary} not found"
    end
end