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, #find_or_create_remote, #find_ref, #get_content, #git_command, #git_url?, #push_branch, #push_tags, #rebase_branch, #remote_exists?, #remote_from_name, #remote_from_url, #remote_url_matches?, #remove_file, #transports, #up2date?

Constructor Details

#initialize(mod_path, upstream = nil) ⇒ PuppetModule

Returns a new instance of PuppetModule.



16
17
18
19
20
21
# File 'lib/release_manager/puppet_module.rb', line 16

def initialize(mod_path, upstream = nil)
  raise ModNotFoundException.new("#{mod_path} is not a valid puppet module path") if mod_path.nil?
  @path = mod_path
  @upstream = 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

#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



75
76
77
# File 'lib/release_manager/puppet_module.rb', line 75

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



110
111
112
# File 'lib/release_manager/puppet_module.rb', line 110

def version
  @version
end

Class Method Details

.check_requirements(path) ⇒ Object



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

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.

Returns:

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



247
248
249
250
251
# File 'lib/release_manager/puppet_module.rb', line 247

def self.create(path, url, branch = 'master')
  c = PuppetModule.new(path, url)
  c.clone(url, path)
  c
end

Instance Method Details

#add_upstream_remoteObject



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

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

#already_latest?Boolean

Returns:

  • (Boolean)


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

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

#bump_major_versionObject

Updates the version in memory



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

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

#bump_minor_versionObject

Updates the version in memory



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

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

#bump_patch_versionObject

Updates the version in memory



150
151
152
# File 'lib/release_manager/puppet_module.rb', line 150

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



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/release_manager/puppet_module.rb', line 198

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



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] - 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

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



177
178
179
180
181
182
183
# File 'lib/release_manager/puppet_module.rb', line 177

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

#git_upstream_set?Boolean

Returns:

  • (Boolean)


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

def git_upstream_set?
   source == git_upstream_url
end

#git_upstream_urlObject



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

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



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

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



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

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



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

def mod_name
  ['name']
end

#namespaced_nameObject



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

def namespaced_name
  ['name']
end

#next_version(level = 'patch') ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/release_manager/puppet_module.rb', line 115

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



79
80
81
82
83
84
85
# File 'lib/release_manager/puppet_module.rb', line 79

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_upstreamObject



191
192
193
194
# File 'lib/release_manager/puppet_module.rb', line 191

def push_to_upstream
  push_branch(source, src_branch)
  push_tags(source)
end

#r10k_module?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/release_manager/puppet_module.rb', line 168

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

#repoObject



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

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

#src_branchObject

if r10k-control this branch will be dev, otherwise master



187
188
189
# File 'lib/release_manager/puppet_module.rb', line 187

def src_branch
  r10k_module? ? 'dev' : 'master'
end

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

Returns:

  • (Boolean)


233
234
235
236
237
238
239
# File 'lib/release_manager/puppet_module.rb', line 233

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) ⇒ 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



138
139
140
141
142
143
144
145
146
147
# File 'lib/release_manager/puppet_module.rb', line 138

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

#tagsObject



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

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

#to_metadata_fileObject



241
242
243
244
# File 'lib/release_manager/puppet_module.rb', line 241

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

#to_sObject



164
165
166
# File 'lib/release_manager/puppet_module.rb', line 164

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



88
89
90
# File 'lib/release_manager/puppet_module.rb', line 88

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