Class: ModuleDeployer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ReleaseManager::Logger

#color, #log_level, #logger

Constructor Details

#initialize(opts) ⇒ ModuleDeployer

Returns a new instance of ModuleDeployer.



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

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.



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

def options
  @options
end

Instance Method Details

#add_module(puppet_module) ⇒ Object

raises

Parameters:

  • puppet_module (PuppetModule)
    • the puppet module to check for existance



50
51
52
53
54
55
56
57
# File 'lib/release_manager/module_deployer.rb', line 50

def add_module(puppet_module)
  unless puppetfile.mod_exists?(puppet_module.name)
    answer = ask("The #{puppet_module.name} module does not exist, do you want to add it? (y/n): ", String) { |q| q =~ /y|n/i }.downcase unless options[:auto]
    if answer == 'y' or options[:auto]
      puppetfile.add_module(puppet_module.name, git: puppet_module.repo, tag: "v#{puppet_module.version}") unless options[:dry_run]
    end
  end
end

#check_requirementsObject



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

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

#control_repo_remoteObject



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

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

#latest_versionObject



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

def latest_version
  puppet_module.latest_tag
end

#mod_pathObject



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

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

#puppet_moduleObject



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

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

#puppetfileObject



40
41
42
# File 'lib/release_manager/module_deployer.rb', line 40

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

#puppetfile_pathObject



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

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

#remote_deploy?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/release_manager/module_deployer.rb', line 44

def remote_deploy?
  options[:remote]
end

#runObject



59
60
61
62
63
64
65
66
67
68
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
# File 'lib/release_manager/module_deployer.rb', line 59

def run
  begin
    check_requirements
    logger.info "Deploying module #{puppet_module.name} with version: #{latest_version}"
    add_module(puppet_module)
    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
      puppetfile.write_version(puppet_module.name, latest_version)
      puppetfile.write_source(puppet_module.name, puppet_module.source)
      updated = puppetfile.write_to_file
      unless updated
        logger.warn "Module #{puppet_module.name} with version #{latest_version} has already been deployed, skipping deployment"
        return
      end
      logger.info "Updated module #{puppet_module.name} in Puppetfile to version: #{latest_version}"
      if options[:commit]
        puppetfile.commit("bump #{puppet_module.name} to version #{latest_version}")
      end
      if remote_deploy?
        puppetfile.push(control_repo_remote, puppetfile.current_branch)
        logger.info "Just pushed branch: #{puppetfile.current_branch} to remote: #{control_repo_remote}"
      end
    end
  rescue InvalidMetadataSource
    logger.fatal "The puppet module's metadata.json source field must be a git url: ie. [email protected]:devops/module.git"
  rescue PuppetfileNotFoundException
    logger.fatal "Cannot find the puppetfile at #{puppetfile_path}"
    exit -1
  rescue InvalidModuleNameException => e
    logger.fatal e.message
    exit 1
  rescue ModNotFoundException
    logger.fatal "Invalid module path for #{mod_path}"
    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