Class: ModuleDeployer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ ModuleDeployer

Returns a new instance of ModuleDeployer.



7
8
9
10
# File 'lib/release_manager/module_deployer.rb', line 7

def initialize(opts)
  opts[:modulepath] = Dir.getwd if opts[:modulepath].nil? 
  @options = opts
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/release_manager/module_deployer.rb', line 5

def options
  @options
end

Instance Method Details

#check_requirementsObject

Raises:



28
29
30
31
# File 'lib/release_manager/module_deployer.rb', line 28

def check_requirements
  raise PuppetfileNotFoundException unless File.exists?(puppetfile_path)
  raise ModNotFoundException if !mod_path || ! File.exists?(mod_path)
end

#control_repo_remoteObject



33
34
35
# File 'lib/release_manager/module_deployer.rb', line 33

def control_repo_remote
  @control_repo_remote ||= options[:remote] || puppetfile.source
end

#latest_versionObject



24
25
26
# File 'lib/release_manager/module_deployer.rb', line 24

def latest_version
  puppet_module.latest_tag
end

#mod_pathObject



16
17
18
# File 'lib/release_manager/module_deployer.rb', line 16

def mod_path
  @mod_path ||= options[:modulepath] || ENV['MOD_DIR'] || File.expand_path(Dir.getwd)
end

#puppet_moduleObject



20
21
22
# File 'lib/release_manager/module_deployer.rb', line 20

def puppet_module 
  @puppet_module ||= PuppetModule.new(mod_path)
end

#puppetfileObject



37
38
39
# File 'lib/release_manager/module_deployer.rb', line 37

def puppetfile
  @puppetfile ||= Puppetfile.new(puppetfile_path)
end

#puppetfile_pathObject



12
13
14
# File 'lib/release_manager/module_deployer.rb', line 12

def puppetfile_path
  @puppetfile_path ||= options[:puppetfile] || ENV['PUPPET_FILE_PATH'] || File.expand_path("~/repos/r10k-control/Puppetfile")
end

#runObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/release_manager/module_deployer.rb', line 41

def run
  begin
    check_requirements
    puts "Found module #{puppet_module.name} with version: #{latest_version}".green
    if options[:dry_run]
      puts "Would have updated module #{puppet_module.name} in Puppetfile to version: #{latest_version}".green
      puts "Would have committed with message: bump #{puppet_module.name} to version: #{latest_version}".green if options[:commit]
      puts "Would have just pushed branch: #{puppetfile.current_branch} to remote: #{control_repo_remote}".green if options[:push]
    else
      puts "Updated module #{puppet_module.name} in Puppetfile to version: #{latest_version}".green
      puppetfile.write_version(puppet_module.name, latest_version)
      puppetfile.write_source(puppet_module.name, puppet_module.source)
      puppetfile.write_to_file
      if options[:commit]
        puppetfile.commit("bump #{puppet_module.name} to version #{latest_version}")
        puts "Commited with message: bump #{puppet_module.name} to version #{latest_version}".green
      end
      if options[:push]
        puppetfile.push(control_repo_remote, puppetfile.current_branch)
        puts "Just pushed branch: #{puppetfile.current_branch} to remote: #{control_repo_remote}".green
      end
    end
  rescue InvalidMetadataSource
    puts "The puppet module's metadata.json source field must be a git url: ie. [email protected]:devops/module.git".red
  rescue PuppetfileNotFoundException
    puts "Cannot find the puppetfile at #{puppetfile_path}".red
    exit -1
  rescue InvalidModuleNameException => e
    puts e.message
    exit 1
  rescue ModNotFoundException
    puts "Invalid module path for #{mod_path}".red
    puts "This means that the metadata.json name field does not match\nthe module name found in the Puppetfile or this is not a puppet module".fatal
    exit -1
  end
end