Class: PuppetModule

Inherits:
WorkflowAction show all
Includes:
ReleaseManager::Git::Utilities, ReleaseManager::Logger, ReleaseManager::VCSManager
Defined in:
lib/release_manager/puppet_module.rb

Instance Attribute Summary collapse

Attributes included from ReleaseManager::VCSManager

#vcs

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReleaseManager::VCSManager

adapter_instance, adapter_types, default_instance

Methods included from ReleaseManager::Logger

#color, #log_level, #logger

Methods included from ReleaseManager::Git::Utilities

#add_all, #add_file, #add_remote, #apply_diff, #apply_patch, #author, #author_email, #author_name, #branch_exist?, #changed_files, #checkout_branch, #cherry_pick, #cli_create_commit, #clone, #create_branch, #create_commit, #create_diff, #create_diff_obj, #create_local_tag, #credentials, #current_branch, #current_branch?, #delete_branch, #fetch, #fetch_cli, #find_or_create_remote, #find_ref, #find_tag, #get_content, #git_command, #git_url?, #push_branch, #push_tags, #rebase_branch, #ref_exists?, #remote_exists?, #remote_from_name, #remote_from_url, #remote_url_matches?, #remove_file, #transports, #up2date?

Constructor Details

#initialize(mod_path, options = {}) ⇒ PuppetModule

Returns a new instance of PuppetModule.

Parameters:

  • mod_path (String)
    • the path to the module

  • options (Hash) (defaults to: {})
    • a hash of options

  • upstream (Hash)

    a customizable set of options

  • src_branch (Hash)

    a customizable set of options

Raises:



20
21
22
23
24
25
26
# File 'lib/release_manager/puppet_module.rb', line 20

def initialize(mod_path, options = {})
  raise ModNotFoundException.new("#{mod_path} is not a valid puppet module path") if mod_path.nil?
  @path = mod_path
  @options = options
  @upstream = options[:upstream]
  @metadata_file = File.join(mod_path, 'metadata.json')
end

Instance Attribute Details

#metadata_fileObject (readonly)

Returns the value of attribute metadata_file.



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

def 
  @metadata_file
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#sourceObject



80
81
82
# File 'lib/release_manager/puppet_module.rb', line 80

def source
  ['source']
end

#upstreamObject (readonly)

Returns the value of attribute upstream.



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

def upstream
  @upstream
end

#versionObject



115
116
117
# File 'lib/release_manager/puppet_module.rb', line 115

def version
  @version
end

Class Method Details

.check_requirements(path) ⇒ Object



32
33
34
35
36
# File 'lib/release_manager/puppet_module.rb', line 32

def self.check_requirements(path)
  pm = new(path)
  raise InvalidMetadataSource if pm.source !~ /\Agit\@/
  raise UpstreamSourceMatch unless pm.git_upstream_set?
end

.create(path, url, branch = 'master') ⇒ ControlRepo

Returns - creates a new control repo object and clones the url unless already cloned.

Parameters:

  • url (String)
    • the upstream url

  • branch (String) (defaults to: 'master')
    • the source branch

Returns:

  • (ControlRepo)
    • creates a new control repo object and clones the url unless already cloned



271
272
273
274
275
276
# File 'lib/release_manager/puppet_module.rb', line 271

def self.create(path, url, branch = 'master')
  options = {upstream: url, src_branch: branch}
  c = PuppetModule.new(path, options)
  c.clone(url, path)
  c
end

Instance Method Details

#add_upstream_remoteObject



60
61
62
# File 'lib/release_manager/puppet_module.rb', line 60

def add_upstream_remote
  add_remote(source,'upstream',true )
end

#already_latest?Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/release_manager/puppet_module.rb', line 55

def already_latest?
  return false unless latest_tag
  up2date?(latest_tag, src_branch)
end

#bump_major_versionObject

Updates the version in memory



166
167
168
# File 'lib/release_manager/puppet_module.rb', line 166

def bump_major_version
   ['version'] = next_version('major')
end

#bump_minor_versionObject

Updates the version in memory



161
162
163
# File 'lib/release_manager/puppet_module.rb', line 161

def bump_minor_version
   ['version'] = next_version('minor')
end

#bump_patch_versionObject

Updates the version in memory



156
157
158
# File 'lib/release_manager/puppet_module.rb', line 156

def bump_patch_version
   ['version'] = next_version('patch')
end

#commit_metadata(remote = false) ⇒ String

Returns the oid of the commit that was created.

Parameters:

  • remote (Boolean) (defaults to: false)

    if true creates the commit on the remote repo

Returns:

  • (String)

    the oid of the commit that was created



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/release_manager/puppet_module.rb', line 216

def (remote = false)
  message = "[ReleaseManager] - bump version to #{version}"
  if remote
    actions = [{
      action: 'update',
      file_path: .split(repo.workdir).last,
      content: JSON.pretty_generate()
    }]
    obj = vcs_create_commit(source, src_branch, message, actions)
    obj.id if obj
  else
    
    add_file()
    create_commit(message)
  end
end

#commit_metadata_source(remote = false) ⇒ String

Returns the oid of the commit that was created.

Returns:

  • (String)

    the oid of the commit that was created



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/release_manager/puppet_module.rb', line 234

def (remote = false)
  message = "[ReleaseManager] - change source to #{source}"
  if remote
    actions = [{
      action: 'update',
      file_path: .split(repo.workdir).last,
      content: JSON.pretty_generate()
    }]
    obj = vcs_create_commit(source, src_branch, message, actions)
    obj.id if obj
  else
    
    add_file()
    create_commit(message)
  end
end

#create_dev_branchObject

Deprecated.

Use #create_src_branch instead of this method which defaults to dev or master branch

ensures the dev branch has been created and is up to date



195
196
197
# File 'lib/release_manager/puppet_module.rb', line 195

def create_dev_branch
  create_src_branch(src_branch)
end

#create_src_branch(branch = src_branch) ⇒ Object

creates a branch and checkouts out the branch with the latest source of the upstream source

Parameters:

  • branch (String) (defaults to: src_branch)
    • the name of the source branch to create and rebase from



185
186
187
188
189
190
191
# File 'lib/release_manager/puppet_module.rb', line 185

def create_src_branch(branch = src_branch)
  fetch('upstream')
  create_branch(branch, "upstream/#{branch}")
  # ensure we have updated our local branch
  checkout_branch(branch)
  rebase_branch(branch, branch, 'upstream')
end

#git_upstream_set?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/release_manager/puppet_module.rb', line 68

def git_upstream_set?
   source == git_upstream_url
end

#git_upstream_urlObject



64
65
66
# File 'lib/release_manager/puppet_module.rb', line 64

def git_upstream_url
  repo.remotes['upstream'].url if remote_exists?('upstream')
end

#latest_tagString

Returns - the latest tag in a series of versioned tags.

Returns:

  • (String)
    • the latest tag in a series of versioned tags



98
99
100
101
102
103
# File 'lib/release_manager/puppet_module.rb', line 98

def latest_tag
  v = version_tags.sort do |a,b|
   Gem::Version.new(a.tr('v', '')) <=> Gem::Version.new(b.tr('v', ''))
  end
  v.last
end

#metadataObject



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

def 
  unless @metadata
    raise ModNotFoundException.new("#{path} does not contain a metadata file") unless File.exists?()
    @metadata ||= JSON.parse(File.read())
  end
  @metadata
end

#mod_nameObject



106
107
108
# File 'lib/release_manager/puppet_module.rb', line 106

def mod_name
  ['name']
end

#namespaced_nameObject



42
43
44
# File 'lib/release_manager/puppet_module.rb', line 42

def namespaced_name
  ['name']
end

#next_version(level = 'patch') ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/release_manager/puppet_module.rb', line 120

def next_version(level = 'patch')
  return unless version
  pieces = version.split('.')
  raise "invalid semver structure #{version}" if pieces.count != 3

  case level
    when 'major'
      pieces[2] = '0'
      pieces[1] = '0'
      pieces[0] = pieces[0].next
    when 'minor'
      pieces[2] = '0'
      pieces[1] = pieces[1].next
    when 'patch'
      pieces[2] = pieces[2].next
    else
      raise "expected semver release level major, minor or patch"
  end
  pieces.join('.')
end

#pad_version_string(version_string) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/release_manager/puppet_module.rb', line 84

def pad_version_string(version_string)
  parts = version_string.split('.').reject {|x| x == '*'}
  while parts.length < 3
    parts << '0'
  end
  parts.join '.'
end

#push_to_upstream(id = nil) ⇒ Object

pushes the source and tags

Parameters:

  • id (String) (defaults to: nil)
    • a ref spec to push



209
210
211
212
# File 'lib/release_manager/puppet_module.rb', line 209

def push_to_upstream(id = nil)
  push_branch(source, src_branch)
  push_tags(source, id)
end

#r10k_module?Boolean

Returns - true if the module is an r10k-control repository.

Returns:

  • (Boolean)
    • true if the module is an r10k-control repository



175
176
177
# File 'lib/release_manager/puppet_module.rb', line 175

def r10k_module?
  mod_name =~ /r10k[-_]?control/i
end

#repoObject



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

def repo
  @repo ||= Rugged::Repository.new(path)
end

#src_branchObject

if the user supplied the src_branch we use that otherwise if the module is r10k-control this branch will be dev, if the module is not r10k-control we use master



203
204
205
# File 'lib/release_manager/puppet_module.rb', line 203

def src_branch
  options[:src_branch] || (r10k_module? ? 'dev' : 'master')
end

#tag_exists?(tag, remote = false) ⇒ Boolean

Returns - returns true if the tag exists.

Parameters:

  • tag (String)
    • the name of the tag

  • remote (String) (defaults to: false)
    • check the remote for the tag, defaults to false

Returns:

  • (Boolean)
    • returns true if the tag exists



254
255
256
257
258
259
260
# File 'lib/release_manager/puppet_module.rb', line 254

def tag_exists?(tag, remote = false)
  if remote
    remote_tag_exists?(source, tag)
  else
    latest_tag == tag
  end
end

#tag_module(remote = false, id = nil, release_notes = nil) ⇒ Object

Parameters:

  • remote (Boolean) (defaults to: false)
    • create the tag remotely using the remote VCS

  • id (String) (defaults to: nil)
    • the commit id to tag to

  • remote (Boolean) (defaults to: false)
    • use the vcs adapter instead of local tag

  • release_notes (Boolean) (defaults to: nil)
    • add release notes to the tag when remote is true



145
146
147
148
149
150
151
152
153
# File 'lib/release_manager/puppet_module.rb', line 145

def tag_module(remote = false, id = nil, release_notes = nil)
  id ||= repo.head.target_id
  if remote
    # where we get the latest from the changelog
    create_tag(source, "v#{version}", id, "v#{version}", release_notes)
  else
    create_local_tag("v#{version}", id)
  end
end

#tagsObject



72
73
74
# File 'lib/release_manager/puppet_module.rb', line 72

def tags
  repo.tags.map{|v| pad_version_string(v.name)}
end

#to_metadata_fileObject

creates a file with the puppet metadata.json content written to disk



263
264
265
266
# File 'lib/release_manager/puppet_module.rb', line 263

def 
  logger.info("Writing to file #{}")
  File.write(, to_s)
end

#to_sObject



170
171
172
# File 'lib/release_manager/puppet_module.rb', line 170

def to_s
  JSON.pretty_generate()
end

#version_tagsArray<String>

Returns - returns a array of version strings with the v in the name.

Returns:

  • (Array<String>)
    • returns a array of version strings with the v in the name



93
94
95
# File 'lib/release_manager/puppet_module.rb', line 93

def version_tags
  tags.find_all {|tag| tag =~ /\Av\d/ }
end