Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rpm spec %config support #47

Open
wants to merge 4 commits into
base: files-whitelist
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,24 @@ If you have scripts that need to be executable when they're installed on your ta
}
```

### Config files

If you have files that need to be preserved after every `yum update`, you can add them to the `config` array.
On update you can achieve following two behaviors:
1. `"noreplace": false` (default): Local edited file will be renamed with `.rpmsave`
2. `"noreplace": true`: Local edited file will be preserved and new file from update will be renamed with `.rpmnew`

```json
{
"spec": {
"config": [
{"file": "./my-config.js", "noreplace": true},
{"file": "./my-other-config.js"}
]
}
}
```

### Post Install Actions

If you need to perform any actions after installing your package (such as moving files on the target server) you can specify these inline using the `post` property:
Expand Down
14 changes: 13 additions & 1 deletion lib/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ function getExecutableFiles(pkg) {
};
}

function getConfigFiles(pkg) {
const name = pkg.name;

return _.get(pkg, 'spec.config', []).map((o) => {
return {
file: path.join('/usr/lib/', name, o.file),
noreplace: o.noreplace || false
};
});
}

function getPostInstallCommands(pkg) {
return _.get(pkg, 'spec.post', []);
}
Expand All @@ -60,7 +71,8 @@ module.exports = function (pkg, release) {
nodeVersion: getNodeVersion(pkg),
version: pkg.version,
license: pkg.license,
prune: shouldPrune(pkg)
prune: shouldPrune(pkg),
configFiles: getConfigFiles(pkg)
},
getExecutableFiles(pkg),
getServiceProperties(pkg)
Expand Down
3 changes: 3 additions & 0 deletions templates/spec.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ rm -rf %{buildroot}
%attr(755, {{username}}, {{username}}) {{.}}
{{/executableFiles}}
{{/hasExecutableFiles}}
{{#configFiles}}
%config{{#noreplace}}(noreplace){{/noreplace}} {{file}}
{{/configFiles}}
17 changes: 17 additions & 0 deletions test/fixtures/my-cool-api-with-config-files.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "my-cool-api",
"version": "1.1.1",
"scripts": {
"start": "node index.js"
},
"description": "My Cool API",
"main": "index.js",
"author": "[email protected]",
"license": "MIT",
"spec": {
"config": [
{"file": "./no-replace.js", "noreplace": true},
{"file": "./config.js"}
]
}
}
49 changes: 49 additions & 0 deletions test/fixtures/my-cool-api-with-config-files.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
%define name my-cool-api
%define version 1.1.1
%define release 1
%define buildroot %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)

Name: %{name}
Version: %{version}
Release: %{release}
Summary: my-cool-api

Group: Installation Script
License: MIT
Source: %{name}.tar.gz
BuildRoot: %{buildroot}
Requires: nodejs
BuildRequires: nodejs
AutoReqProv: no

%description
My Cool API

%prep
%setup -q -c -n %{name}

%build
npm prune --production
npm rebuild

%pre
getent group my-cool-api >/dev/null || groupadd -r my-cool-api
getent passwd my-cool-api >/dev/null || useradd -r -g my-cool-api -G my-cool-api -d / -s /sbin/nologin -c "my-cool-api" my-cool-api

%install
mkdir -p %{buildroot}/usr/lib/my-cool-api
cp -r ./ %{buildroot}/usr/lib/my-cool-api
mkdir -p %{buildroot}/var/log/my-cool-api

%post
systemctl enable /usr/lib/my-cool-api/my-cool-api.service

%clean
rm -rf %{buildroot}

%files
%defattr(644, my-cool-api, my-cool-api, 755)
/usr/lib/my-cool-api
/var/log/my-cool-api
%config(noreplace) /usr/lib/my-cool-api/no-replace.js
%config /usr/lib/my-cool-api/config.js
8 changes: 8 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ describe('spec', () => {
assert.equal(spec, expected);
});

it('adds all of the config files from the spec.config property in package.json', () => {
const pkg = require('./fixtures/my-cool-api-with-config-files');
const expected = loadFixture('my-cool-api-with-config-files.spec');
const spec = createSpecFile(pkg);

assert.equal(spec, expected);
});

it('includes post-install actions from the spec.post property in package.json', () => {
const pkg = require('./fixtures/my-cool-api-with-post');
const expected = loadFixture('my-cool-api-with-post.spec');
Expand Down