Class: Convert

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

Overview

This class takes and dumps out environment json files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(branch, berkslockfile) ⇒ Convert

Returns a new instance of Convert.



10
11
12
13
14
# File 'lib/berks2env.rb', line 10

def initialize(branch, berkslockfile)
  @branch = branch
  @berkslockfile = berkslockfile
  @environment = Chef::Environment.new
end

Instance Attribute Details

#berkslockfileObject (readonly)

Returns the value of attribute berkslockfile.



8
9
10
# File 'lib/berks2env.rb', line 8

def berkslockfile
  @berkslockfile
end

#branchObject (readonly)

Returns the value of attribute branch.



8
9
10
# File 'lib/berks2env.rb', line 8

def branch
  @branch
end

Instance Method Details

#create_latestObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/berks2env.rb', line 33

def create_latest
  if @branch.match(/\d+\.\d+\.\d+\z/)
    xver = @branch.split('.')
    xver = "#{xver[0]}.#{xver[1]}.LATEST"
    # Create the major.minor.X environment to go with the major.minor.patch environment
    @environment.name(xver.gsub('.', '_'))
    @environment.override_attributes({ :server_env => { :version => { :real => @branch, :virt => xver }}})
    envfile = File.open("#{xver.gsub('.', '_')}.json", 'w')
    envfile.write(@environment.to_json)
    envfile.close
  end
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/berks2env.rb', line 16

def run
  clean_branch = self.sanitize_branch
  @environment.name(clean_branch)
  berksfile = Berkshelf::Lockfile.from_file(@berkslockfile)
  locks = berksfile.graph.locks.inject({}) do |hash, (name, dependency)|
    hash[name] = "= #{dependency.locked_version.to_s}"
    hash
  end
  @environment.cookbook_versions(locks)

  @environment.override_attributes({ :server_env => { :version => { :real => @branch, :virt => @branch }}})
  envfile = File.open("#{clean_branch}.json", 'w')
  envfile.write(@environment.to_json)
  envfile.close
  self.create_latest
end

#sanitize_branchObject



47
48
49
50
51
52
53
# File 'lib/berks2env.rb', line 47

def sanitize_branch
  if self.branch =~ /\d+\.\d+\.\d+/
    self.branch.gsub('.','_')
  else
    self.branch.gsub(/[\-;:,.\/\\']/,'_')
  end
end