From 38b7940d7d840bef3d5d9c6117a1ecd75d58bda7 Mon Sep 17 00:00:00 2001 From: Bob Gaudaen Date: Fri, 16 Mar 2018 09:38:16 +0100 Subject: [PATCH 1/3] Added '%config' feature on spec file generation --- lib/spec.js | 14 +++++- templates/spec.mustache | 3 ++ .../my-cool-api-with-config-files.json | 17 +++++++ .../my-cool-api-with-config-files.spec | 49 +++++++++++++++++++ test/spec.js | 8 +++ 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/my-cool-api-with-config-files.json create mode 100644 test/fixtures/my-cool-api-with-config-files.spec diff --git a/lib/spec.js b/lib/spec.js index 3163578..322ab3f 100644 --- a/lib/spec.js +++ b/lib/spec.js @@ -41,6 +41,17 @@ function getExecutableFiles(pkg) { }; } +function getConfigFiles(pkg) { + var name = pkg.name; + + return _.get(pkg, 'spec.config', []).map(function (o) { + return { + file: path.join('/usr/lib/', name, o.file), + noreplace: o.noreplace || false + }; + }); +} + function getPostInstallCommands(pkg) { return _.get(pkg, 'spec.post', []); } @@ -58,7 +69,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) diff --git a/templates/spec.mustache b/templates/spec.mustache index 68156dd..be5f0ce 100644 --- a/templates/spec.mustache +++ b/templates/spec.mustache @@ -62,3 +62,6 @@ rm -rf %{buildroot} {{.}} {{/executableFiles}} {{/hasExecutableFiles}} +{{#configFiles}} +%config{{#noreplace}}(noreplace){{/noreplace}} {{file}} +{{/configFiles}} diff --git a/test/fixtures/my-cool-api-with-config-files.json b/test/fixtures/my-cool-api-with-config-files.json new file mode 100644 index 0000000..3616e42 --- /dev/null +++ b/test/fixtures/my-cool-api-with-config-files.json @@ -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": "bob@example.com", + "license": "MIT", + "spec": { + "config": [ + {"file": "./no-replace.js", "noreplace": true}, + {"file": "./config.js"} + ] + } +} diff --git a/test/fixtures/my-cool-api-with-config-files.spec b/test/fixtures/my-cool-api-with-config-files.spec new file mode 100644 index 0000000..a50cd00 --- /dev/null +++ b/test/fixtures/my-cool-api-with-config-files.spec @@ -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 diff --git a/test/spec.js b/test/spec.js index a7135e5..65d8abd 100644 --- a/test/spec.js +++ b/test/spec.js @@ -68,6 +68,14 @@ describe('spec', function () { assert.equal(spec, expected); }); + it('adds all of the config files from the spec.config property in package.json', function () { + var pkg = require('./fixtures/my-cool-api-with-config-files'); + var expected = loadFixture('my-cool-api-with-config-files.spec'); + var spec = createSpecFile(pkg); + + assert.equal(spec, expected); + }); + it('includes post-install actions from the spec.post property in package.json', function () { var pkg = require('./fixtures/my-cool-api-with-post'); var expected = loadFixture('my-cool-api-with-post.spec'); From 81a9dd8537d474d47318255bccb89dbdaad95511 Mon Sep 17 00:00:00 2001 From: Bob Gaudaen Date: Fri, 16 Mar 2018 09:55:18 +0100 Subject: [PATCH 2/3] Documented spec '%config' feature --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 6dfdb3e..88a2205 100644 --- a/README.md +++ b/README.md @@ -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: From 61b7e4e5f51031093815ee7d96a8f5263871f6ee Mon Sep 17 00:00:00 2001 From: Bob Gaudaen Date: Fri, 23 Mar 2018 17:45:53 +0100 Subject: [PATCH 3/3] Fixed some eslint errors... --- lib/spec.js | 4 ++-- test/spec.js | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/spec.js b/lib/spec.js index d604d6f..25e89ea 100644 --- a/lib/spec.js +++ b/lib/spec.js @@ -44,9 +44,9 @@ function getExecutableFiles(pkg) { } function getConfigFiles(pkg) { - var name = pkg.name; + const name = pkg.name; - return _.get(pkg, 'spec.config', []).map(function (o) { + return _.get(pkg, 'spec.config', []).map((o) => { return { file: path.join('/usr/lib/', name, o.file), noreplace: o.noreplace || false diff --git a/test/spec.js b/test/spec.js index ab4fab2..654998c 100644 --- a/test/spec.js +++ b/test/spec.js @@ -69,11 +69,11 @@ describe('spec', () => { assert.equal(spec, expected); }); - - it('adds all of the config files from the spec.config property in package.json', function () { - var pkg = require('./fixtures/my-cool-api-with-config-files'); - var expected = loadFixture('my-cool-api-with-config-files.spec'); - var spec = createSpecFile(pkg); + + 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); });