Class: Berks2Env::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.



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

def initialize(branch, berkslockfile)
  @branch = branch
  @berkslockfile = berkslockfile
end

Instance Attribute Details

#berkslockfileObject (readonly)

Returns the value of attribute berkslockfile.



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

def berkslockfile
  @berkslockfile
end

#branchObject (readonly)

Returns the value of attribute branch.



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

def branch
  @branch
end

Instance Method Details

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/berks2env.rb', line 16

def run
  environment = Chef::Environment.new
  environment.name(sanitize)
  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("#{@branch}.json", 'w')
  envfile.write(environment.to_json)
  envfile.close
  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

#sanitizeObject



42
43
44
45
46
47
48
# File 'lib/berks2env.rb', line 42

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