Class: PuppetLocal::Command::Install

Inherits:
PuppetLocal::Command show all
Defined in:
lib/puppet_local/command/install.rb

Instance Method Summary collapse

Methods inherited from PuppetLocal::Command

#run, #system_command

Instance Method Details

#applyObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/puppet_local/command/install.rb', line 56

def apply
  puppet_code = 'include hiera("classes", [])'
  params = {
      '--debug' => debug?,
      '-e' => puppet_code,
      '--hiera_config' => puppet_hiera_config,
      '--modulepath' => puppet_module_path
  }
  system_command 'puppet apply', params
end

#clone_git_modulesObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/puppet_local/command/install.rb', line 40

def clone_git_modules
  @puppet_module_paths = []
  if @hiera.include? 'git-modules'
    @hiera['git-modules'].each do |source, version|
      repo_path = local_path Digest::MD5.hexdigest(source)
      if File.directory? repo_path
        repo = Git.open repo_path
      else
        repo = Git.clone source, repo_path
      end
      repo.checkout version
      @puppet_module_paths.push repo.dir.path + '/modules'
    end
  end
end

#create_puppet_hiera_configObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puppet_local/command/install.rb', line 27

def create_puppet_hiera_config
  hiera_config = {
      :backends => ['json'],
      :hierarchy => [File.basename(@hiera_path).sub(/\.json$/, '')],
      :json => {
          :datadir => File.dirname(hiera_path_absolute)
      }
  }
  file = File.new(puppet_hiera_config, 'w')
  file.puts(hiera_config.to_yaml.gsub('!ruby/sym ', ':'))
  file.close
end

#executeObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/puppet_local/command/install.rb', line 7

def execute
  puts 'Setting up environment...'
  setup
  create_puppet_hiera_config
  puts 'Checking out git-modules...'
  clone_git_modules
  puts 'Applying catalog...'
  apply
  print on_green ' '
  puts ' Puppet apply succeeded'
end

#hiera_path_absoluteObject



67
68
69
# File 'lib/puppet_local/command/install.rb', line 67

def hiera_path_absolute
  File.expand_path @hiera_path
end

#local_path(path = '') ⇒ Object



71
72
73
# File 'lib/puppet_local/command/install.rb', line 71

def local_path(path = '')
  ENV['HOME'] + '/.puppet-local/' + Digest::MD5.hexdigest(hiera_path_absolute) + '/' + path
end

#puppet_hiera_configObject



75
76
77
# File 'lib/puppet_local/command/install.rb', line 75

def puppet_hiera_config
  local_path 'hiera.yaml'
end

#puppet_module_pathObject



79
80
81
# File 'lib/puppet_local/command/install.rb', line 79

def puppet_module_path
  @puppet_module_paths.join(':')
end

#setupObject



19
20
21
22
23
24
25
# File 'lib/puppet_local/command/install.rb', line 19

def setup
  raise '`' + @hiera_path + '` does not exist' unless File.exists? @hiera_path
  FileUtils.mkdir_p local_path
  file = File.new(hiera_path_absolute, 'r')
  @hiera = JSON.parse file.read
  file.close
end