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?, #update_cli_index

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



104
105
106
# File 'lib/release_manager/puppet_module.rb', line 104

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



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

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



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

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

#bump_minor_versionObject

Updates the version in memory



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

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

#bump_patch_versionObject

Updates the version in memory



144
145
146
# File 'lib/release_manager/puppet_module.rb', line 144

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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/release_manager/puppet_module.rb', line 192

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



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/release_manager/puppet_module.rb', line 210

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



171
172
173
174
175
176
177
# File 'lib/release_manager/puppet_module.rb', line 171

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_tagObject



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

def latest_tag
  v = 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



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

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/release_manager/puppet_module.rb', line 109

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



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

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

#r10k_module?Boolean

Returns:

  • (Boolean)


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

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



181
182
183
# File 'lib/release_manager/puppet_module.rb', line 181

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

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

Returns:

  • (Boolean)


227
228
229
230
231
232
233
# File 'lib/release_manager/puppet_module.rb', line 227

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



132
133
134
135
136
137
138
139
140
141
# File 'lib/release_manager/puppet_module.rb', line 132

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



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

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

#to_sObject



158
159
160
# File 'lib/release_manager/puppet_module.rb', line 158

def to_s
  JSON.pretty_generate()
end