Class: Armature::Environments

Inherits:
Object
  • Object
show all
Defined in:
lib/armature/environments.rb

Defined Under Namespace

Classes: InvalidNameError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, cache) ⇒ Environments

path is the path to the directory containing all the environments



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/armature/environments.rb', line 31

def initialize(path, cache)
  @cache = cache
  @logger = Logging.logger[self]
  @fix_environment_names = false

  if not File.directory? path
    abort "Puppet environments path does not exist: '#{path}'"
  end

  @path = File.realpath(path)
end

Instance Attribute Details

#fix_environment_namesObject

Returns the value of attribute fix_environment_names.



28
29
30
# File 'lib/armature/environments.rb', line 28

def fix_environment_names
  @fix_environment_names
end

#pathObject (readonly)

Returns the value of attribute path.



27
28
29
# File 'lib/armature/environments.rb', line 27

def path
  @path
end

Class Method Details

.assert_valid_environment_name(name) ⇒ Object



14
15
16
17
18
# File 'lib/armature/environments.rb', line 14

def self.assert_valid_environment_name(name)
  if ! valid_environment_name?(name)
    raise InvalidNameError, "Invalid environment name '#{name}'"
  end
end

.assert_valid_module_name(name) ⇒ Object

Same rules as for a class



21
22
23
24
25
# File 'lib/armature/environments.rb', line 21

def self.assert_valid_module_name(name)
  if name !~ /\A[a-z][a-z0-9_]*\Z/
    raise InvalidNameError, "Invalid module name '#{name}'"
  end
end

.valid_environment_name?(name) ⇒ Boolean

The documentation claims that uppercase letters are invalid, but in practice they seem to be fine.

docs.puppet.com/puppet/latest/reference/lang_reserved.html#environments

Returns:

  • (Boolean)


10
11
12
# File 'lib/armature/environments.rb', line 10

def self.valid_environment_name?(name)
  name =~ /\A[A-Za-z0-9_]+\Z/
end

Instance Method Details

#check_out_ref(repo, ref_str, name = ref_str) ⇒ Object

Create an environment from a ref

This will add and update a modules dir in any repo, even if the repo is used in a Puppetfile. (Perhaps the cache is used for multiple repos?)



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
# File 'lib/armature/environments.rb', line 69

def check_out_ref(repo, ref_str, name=ref_str)
  name = normalize_environment_name(name)

  @cache.lock File::LOCK_SH do
    @logger.info "Deploying ref '#{ref_str}' from '#{repo}' as" \
      " environment '#{name}'"

    begin
      ref_path = repo.general_ref(ref_str).check_out()
    rescue Armature::RefError
      @logger.info "Ref '#{ref_str}' does not exist; ensuring environment" \
        " '#{name}' is gone"
      remove(name)
      return
    end

    puppetfile_path = "#{ref_path}/Puppetfile"
    if File.exist?(puppetfile_path)
      @logger.debug "Found Puppetfile in environment '#{name}'"
      module_refs = Armature::Puppetfile.new(@cache).include(puppetfile_path)
      @logger.debug "Loaded Puppetfile in environment '#{name}' with" \
        " #{module_refs.length} modules"
    else
      @logger.debug "No Puppetfile in environment '#{name}'"
      module_refs = {}
    end

    update_modules(ref_path, module_refs)

    # Make the change live
    @cache.atomic_symlink(ref_path, "#{@path}/#{name}")
    @logger.debug "Done deploying ref '#{ref_str}' from '#{repo}' as" \
      " environment '#{name}'"
  end
end

#namesObject



43
44
45
# File 'lib/armature/environments.rb', line 43

def names()
  Dir["#{@path}/*"].map { |path| File.basename(path) }
end

#normalize_environment_name(name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/armature/environments.rb', line 54

def normalize_environment_name(name)
  if @fix_environment_names && ! self.class.valid_environment_name?(name)
    old = name
    name = old.gsub(/[^A-Za-z0-9_]/, "_")
    @logger.warn("Changing invalid environment name \"#{old}\" to \"#{name}\"")
  end

  self.class.assert_valid_environment_name(name)
  name
end

#remove(name) ⇒ Object



47
48
49
50
51
52
# File 'lib/armature/environments.rb', line 47

def remove(name)
  File.delete("#{@path}/#{name}")
  @logger.debug "Environment '#{name}' deleted"
rescue Errno::ENOENT
  @logger.debug "Environment '#{name}' does not exist"
end