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 = 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
# 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       = 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"       => puppet_master,
    "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



147
148
149
# File 'lib/puppet_x/puppet_vagrant.rb', line 147

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



135
136
137
# File 'lib/puppet_x/puppet_vagrant.rb', line 135

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



120
121
122
123
124
# File 'lib/puppet_x/puppet_vagrant.rb', line 120

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



130
131
132
133
# File 'lib/puppet_x/puppet_vagrant.rb', line 130

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

#get_vmObject



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

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



174
175
176
177
178
# File 'lib/puppet_x/puppet_vagrant.rb', line 174

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

#reloadObject



180
181
182
183
# File 'lib/puppet_x/puppet_vagrant.rb', line 180

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

#startObject



162
163
164
165
166
167
# File 'lib/puppet_x/puppet_vagrant.rb', line 162

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

#stopObject



169
170
171
172
# File 'lib/puppet_x/puppet_vagrant.rb', line 169

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

#vagrantfileObject



143
144
145
# File 'lib/puppet_x/puppet_vagrant.rb', line 143

def vagrantfile
  File.join(vm_instance_dir, VAGRANTFILE)
end

#vm_instance_dirObject



139
140
141
# File 'lib/puppet_x/puppet_vagrant.rb', line 139

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