Skip to content

Commit 6b50ac1

Browse files
committed
Add ability to update spec files for nodejs and gems
1 parent 776bd44 commit 6b50ac1

File tree

7 files changed

+127
-18
lines changed

7 files changed

+127
-18
lines changed

obal/data/modules/changelog.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ def main():
3535
module = AnsibleModule(
3636
argument_spec=dict(
3737
spec=dict(required=True),
38-
changelog=dict(default='- rebuilt')
38+
entry=dict(required=False)
3939
)
4040
)
4141

4242
spec = module.params['spec']
43-
changelog = module.params['changelog']
43+
entry = module.params['entry']
4444

4545
user = subprocess.check_output(['rpmdev-packager'], universal_newlines=True).strip()
4646
evr = get_specfile_evr(spec)
@@ -50,22 +50,27 @@ def main():
5050

5151
changed = False
5252

53-
if not changelog.startswith('-'):
54-
changelog = '- ' + changelog
53+
if entry is not None and not entry.startswith('-'):
54+
entry = '- ' + entry
5555

5656
for i, line in enumerate(lines):
5757
if line.startswith("%changelog"):
58-
with en_locale():
59-
date = time.strftime("%a %b %d %Y", time.gmtime())
60-
entry = "* %s %s - %s\n%s\n\n" % (date, user, evr, changelog)
61-
lines[i] += entry
62-
changed = True
58+
if entry:
59+
with en_locale():
60+
date = time.strftime("%a %b %d %Y", time.gmtime())
61+
entry = "* %s %s - %s\n%s\n\n" % (date, user, evr, entry)
62+
lines[i] += entry
63+
changed = True
64+
changelog = lines[i+1:]
6365
break
6466

65-
with open(spec, "w") as spec_file:
66-
spec_file.writelines(lines)
67+
if changed:
68+
with open(spec, "w") as spec_file:
69+
spec_file.writelines(lines)
6770

68-
module.exit_json(changed=changed)
71+
changelog = ''.join(changelog).rstrip()
72+
73+
module.exit_json(changed=changed, changelog=changelog)
6974

7075

7176
if __name__ == '__main__':

obal/data/playbooks/changelog/changelog.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
- name: 'ensure changelog'
1010
changelog:
1111
spec: "{{ spec_file_path }}"
12-
changelog: "{{ changelog | default('- rebuilt') }}"
12+
entry: "{{ changelog | default('- rebuilt') }}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
help: |
2+
Update a package's specfile based on a template
3+
4+
obal update-spec mypackage --type gem --template gem2rpm/scl.spec.erb
5+
variables:
6+
template:
7+
help: The template to use for updates
8+
type:
9+
help: The type of package being updated (e.g. gem)
10+
strategy:
11+
help: The strategy to use for an NPM package
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
- hosts:
3+
- packages
4+
serial: 1
5+
gather_facts: no
6+
roles:
7+
- role: setup_sources
8+
when: type == 'gem'
9+
- spec_file
10+
- update_spec_file

obal/data/playbooks/update/metadata.obal.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ variables:
2727
commit:
2828
help: When true, creates a git branch and commits the update changes to it.
2929
action: store_true
30+
template:
31+
help: The template to use for spec file updates

obal/data/roles/fetch_sources/tasks/main.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@
8989
- changelog is not defined
9090
- version is defined
9191

92-
- name: 'update spec file'
93-
include_role:
94-
name: update_spec_file
95-
9692
- name: 'update source files'
9793
include_role:
9894
name: setup_sources
95+
96+
- name: 'update spec file'
97+
include_role:
98+
name: update_spec_file

obal/data/roles/update_spec_file/tasks/main.yml

+82-1
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,89 @@
5858
- release is defined
5959
- release != 'keep'
6060

61+
- name: Set type
62+
set_fact:
63+
type: 'npm'
64+
when: type is not defined and 'nodejs' in inventory_hostname
65+
66+
- name: Set type
67+
set_fact:
68+
type: 'gem'
69+
when: type is not defined and 'rubygem' in inventory_hostname
70+
6171
- name: 'add changelog entry'
6272
changelog:
6373
spec: "{{ spec_file_path }}"
64-
changelog: "{{ changelog }}"
74+
entry: "{{ changelog }}"
6575
when: changelog is defined
76+
77+
- name: Read changelog
78+
changelog:
79+
spec: "{{ spec_file_path }}"
80+
register: changelog
81+
82+
- when: type == 'gem'
83+
block:
84+
- name: Run spec update template
85+
shell: "gem2rpm -o {{ spec_file_path }} -t {{ template }} {{ inventory_dir }}/{{ package_base_dir }}/{{ inventory_hostname }}/*.gem"
86+
args:
87+
chdir: "{{ inventory_dir }}"
88+
89+
- when: type == 'npm'
90+
block:
91+
- name: Find version
92+
shell: "rpmspec --srpm -q --queryformat=%{version} {{ spec_file_path }}"
93+
args:
94+
chdir: "{{ inventory_dir }}"
95+
register: version_output
96+
when: version is not defined
97+
98+
- name: Find release
99+
shell: "rpmspec --srpm -q --queryformat=%{release} {{ spec_file_path }}"
100+
args:
101+
chdir: "{{ inventory_dir }}"
102+
register: release_output
103+
when: release is not defined
104+
105+
- name: Set version
106+
set_fact:
107+
version: "{{ version_output.stdout }}"
108+
when: version is not defined
109+
110+
- name: Set release
111+
set_fact:
112+
release: "{{ release_output.stdout }}"
113+
when: release is not defined
114+
115+
- name: Find all sources to remove
116+
find:
117+
paths: "{{ inventory_dir }}/{{ package_base_dir }}/{{ inventory_hostname }}"
118+
patterns: "*.tgz"
119+
file_type: any
120+
register: files_to_remove
121+
122+
- name: Remove old sources
123+
file:
124+
state: absent
125+
path: "{{ item.path }}"
126+
with_items: "{{ files_to_remove.files }}"
127+
128+
- name: Run spec update template
129+
command: "npm2rpm -s {{ strategy | default('single') }} -n {{ name | default(inventory_hostname.replace('nodejs-', '')) }} -v {{ version }} -o . -c"
130+
args:
131+
chdir: "{{ inventory_dir }}/{{ package_base_dir }}/{{ inventory_hostname }}"
132+
133+
- name: Add release back
134+
replace:
135+
path: "{{ spec_file_path }}"
136+
regexp: "Release: 1"
137+
replace: "Release: {{ release }}"
138+
139+
- import_role:
140+
name: setup_sources
141+
142+
- name: Add back changelog
143+
lineinfile:
144+
path: "{{ spec_file_path }}"
145+
insertafter: "%changelog"
146+
line: "{{ changelog.changelog }}"

0 commit comments

Comments
 (0)