Class: PuppetX::PuppetVagrant::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/puppet_vagrant.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, box = false, provision = false, synced_folder = false, memory = false, cpu = false, user = false, ip = false, puppet_master_fqdn = false, puppet_master_ip = false, certname = false, pp_role = false, challenge_password = false, act = true) ⇒ Instance

Returns a new instance of Instance



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
# File 'lib/puppet_x/puppet_vagrant.rb', line 77

def initialize(
    name,
    box                 = false,
    provision           = false,
    synced_folder       = false,
    memory              = false,
    cpu                 = false,
    user                = false,
    ip                  = false,
    puppet_master_fqdn  = false,
    puppet_master_ip    = false,
    certname            = false,
    pp_role             = false,
    challenge_password  = false,
    act                 = true)

  @user   = user
  @config = {
    "name"                => name,
    "box"                 => box,
    "provision"           => provision,
    "synced_folder"       => synced_folder,
    "memory"              => memory,
    "cpu"                 => cpu,
    "ip"                  => ip,
    "puppet_master_fqdn"  => puppet_master_fqdn,
    "puppet_master_ip"    => puppet_master_ip,
    "certname"            => certname,
    "pp_role"             => pp_role,
    "challenge_password"  => challenge_password,
  }


  if act
    if ! Dir.exists?(vm_instance_dir)
      FileUtils.mkdir_p(vm_instance_dir)
    end

    ensure_config
    ensure_vagrantfile
  end
end

Class Method Details

.instancesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet_x/puppet_vagrant.rb', line 48

def self.instances
  instance_wildcard = File.join(VAGRANT_VM_DIR, "*", VAGRANTFILE)
  instances = {}
  Dir.glob(instance_wildcard).each { |f|
    elements = f.split(File::SEPARATOR)
    # /var/lib/vagrant_vms/mycoolvm/vagrantfile
    # ---------------------^^^^^^^^------------
    name = elements[elements.size - 2]

    instances[name] = parse_instance(name)
  }

  instances
end

.parse_instance(instance_name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/puppet_x/puppet_vagrant.rb', line 25

def self.parse_instance(instance_name)
  instance_dir = File.join(VAGRANT_VM_DIR, instance_name)
  config_file = File.join(instance_dir, VAGRANTFILE_JSON)
  if File.exists?(config_file)

    # json validity test
    json = File.read(config_file)
    config = JSON.parse(json)

    # anything != :present becomes :absent, so :running == :absent. I give
    # up...
    config["ensure"] = :present
  else
    # VM missing or damaged
    config = {}
    config["ensure"] = :absent
    config["name"]   = instance_name
  end


  config
end

Instance Method Details

#configfileObject



149
150
151
# File 'lib/puppet_x/puppet_vagrant.rb', line 149

def configfile
  File.join(vm_instance_dir, VAGRANTFILE_JSON)
end

#configured?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet_x/puppet_vagrant.rb', line 63

def configured?
  configured = false
  if Dir.exists? (vm_instance_dir) and File.exists?(configfile) and File.exists?(vagrantfile)

    json = File.read(configfile)
    have_config = JSON.parse(json)

    if have_config == @config
      configured = true
    end
  end
  configured
end

#delete_vagrantfileObject



137
138
139
# File 'lib/puppet_x/puppet_vagrant.rb', line 137

def delete_vagrantfile
  FileUtils.rm_r(vm_instance_dir)
end

#ensure_configObject

Vagrant to be driven from a .json config file, all the parameters are externalised here



122
123
124
125
126
# File 'lib/puppet_x/puppet_vagrant.rb', line 122

def ensure_config
  File.open(configfile,"w") do |f|
    f.write(@config.to_json)
  end
end

#ensure_vagrantfileObject

The Vagrantfile itself is shipped as part of this module and delivered via pluginsync, so we just need to symlink it for each directory. This gives us the benefit being to update by dropping a new module too



132
133
134
135
# File 'lib/puppet_x/puppet_vagrant.rb', line 132

def ensure_vagrantfile
  source_file = File.join(Puppet[:factpath].split(':')[0], 'Vagrantfile')
  FileUtils.ln_sf(source_file, vagrantfile)
end

#get_vmObject



153
154
155
156
157
158
159
160
161
# File 'lib/puppet_x/puppet_vagrant.rb', line 153

def get_vm
  # Create an instance (represents a Vagrant installation)
  instance = Derelict.instance(PuppetX::PuppetVagrant::VAGRANT_DIR)
  result = instance.execute('--version') # Derelict::Executer object
  print "success" if result.success?
  vm = instance.connect(vm_instance_dir)

  vm
end

#purgeObject



176
177
178
179
180
# File 'lib/puppet_x/puppet_vagrant.rb', line 176

def purge
  Puppet.notice "purging Vagrant_vm[#{@config['name']}]"
  get_vm.execute(:destroy)
  delete_vagrantfile
end

#reloadObject



182
183
184
185
# File 'lib/puppet_x/puppet_vagrant.rb', line 182

def reload
  Puppet.notice "Reloading Vagrant_vm[#{@config['name']}]"
  get_vm.execute(:reload)
end

#startObject



164
165
166
167
168
169
# File 'lib/puppet_x/puppet_vagrant.rb', line 164

def start
  Puppet.notice "Starting Vagrant_vm[#{@config['name']}]"
  result = get_vm.execute(:up)
  result.success?
  puts "*************up! #{result.success?}"
end

#stopObject



171
172
173
174
# File 'lib/puppet_x/puppet_vagrant.rb', line 171

def stop
  Puppet.notice "Stopping Vagrant_vm[#{@config['name']}]"
  get_vm.execute(:suspend)
end

#vagrantfileObject



145
146
147
# File 'lib/puppet_x/puppet_vagrant.rb', line 145

def vagrantfile
  File.join(vm_instance_dir, VAGRANTFILE)
end

#vm_instance_dirObject



141
142
143
# File 'lib/puppet_x/puppet_vagrant.rb', line 141

def vm_instance_dir
  File.join(PuppetX::PuppetVagrant::VAGRANT_VM_DIR, @config["name"])
end