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 simple build and service overrides #84

Open
wants to merge 1 commit into
base: master
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 @@ -269,6 +269,22 @@ If you need to perform any actions after installing your package (such as moving
}
```

### Build Steps

If you need to perform any actions during the building of your package (such as yarn install or using some other package manager) you can specify these inline using the `buildSteps` property:

```json
{
"spec": {
"buildSteps": [
"yarn install --production"
]
}
}
```

If you've specified something here it will disable the prune and rebuild steps.

### Environment variable

If you need to specify environment variables during startup (NODE_ENV for example) you can specify these inline using the spec.environment property:
Expand Down Expand Up @@ -299,6 +315,8 @@ If you need to set specific [systemd service options](https://www.freedesktop.or
}
```

These will overwrite the defaults for ExecStart, Restart, StandardOutput, and StandardError if you set them. Make sure to use complete and correct paths to the executable for your ExecStart overrides.

#### Unit Options

You can set specific [systemd unit options](https://www.freedesktop.org/software/systemd/man/systemd.unit.html) - in the `[Unit]` section of the .service file, you can specify these using the spec.unitOptions property:
Expand Down
1 change: 0 additions & 1 deletion lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ const template = handlebars.compile(templateFile);

module.exports = function (pkg) {
const serviceProperties = getServiceProperties(pkg);

return template(serviceProperties);
};
18 changes: 17 additions & 1 deletion lib/serviceProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,30 @@ function convertToKeyValueFromSpec(spec, prop) {
}
}

const serviceDefaults = {
ExecStart: '/usr/bin/npm start',
Restart: 'always',
StandardOutput: 'syslog',
StandardError: 'syslog'
};

function createServiceDefaults(spec = {}) {
const combinedConfig = Object.assign(
{},
serviceDefaults,
spec.serviceOptions
);
return convertToKeyValueFromSpec({ combinedConfig }, 'combinedConfig');
}

module.exports = function (pkg) {
return Object.assign(
{
name: pkg.name,
username: truncate(pkg.name),
description: pkg.description,
environment: convertToKeyValueFromSpec(pkg.spec, 'environment'),
serviceOptions: convertToKeyValueFromSpec(pkg.spec, 'serviceOptions'),
serviceOptions: createServiceDefaults(pkg.spec),
unitOptions: convertToKeyValueFromSpec(pkg.spec, 'unitOptions')
}
);
Expand Down
10 changes: 7 additions & 3 deletions lib/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ function getExecutableFiles(pkg) {
}

module.exports = function (pkg, release) {
const serviceProperties = Object.assign(
const specProperties = Object.assign(
{
release: getReleaseNumber(release),
requires: getValueFromSpec(pkg.spec, 'requires', []),
buildRequires: getValueFromSpec(pkg.spec, 'buildRequires', []),
buildSteps: getValueFromSpec(pkg.spec, 'buildSteps'),
postInstallCommands: getValueFromSpec(pkg.spec, 'post', []),
nodeVersion: getValueFromSpec(pkg.spec, 'nodeVersion'),
version: getVersionNumber(pkg),
Expand All @@ -73,6 +74,9 @@ module.exports = function (pkg, release) {
getExecutableFiles(pkg),
getServiceProperties(pkg)
);

return template(serviceProperties);
if (specProperties.buildSteps) {
specProperties.prune = false;
specProperties.rebuild = false;
}
return template(specProperties);
};
10 changes: 3 additions & 7 deletions templates/service.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ After=network.target nss-lookup.target
{{/unitOptions}}

[Service]
ExecStart=/usr/bin/npm start
{{#serviceOptions}}
{{key}}={{value}}
{{/serviceOptions}}
WorkingDirectory=/usr/lib/{{name}}
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier={{name}}
User={{username}}
Group={{username}}
{{#environment}}
Environment={{key}}={{value}}
{{/environment}}
{{#serviceOptions}}
{{key}}={{value}}
{{/serviceOptions}}

[Install]
WantedBy=multi-user.target
3 changes: 3 additions & 0 deletions templates/spec.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ npm prune --production
{{#rebuild}}
npm rebuild
{{/rebuild}}
{{#buildSteps}}
{{{.}}}
{{/buildSteps}}

%pre
getent group {{username}} >/dev/null || groupadd -r {{username}}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/my-cool-api-environment.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ After=network.target nss-lookup.target

[Service]
ExecStart=/usr/bin/npm start
WorkingDirectory=/usr/lib/my-cool-api
Restart=always
StandardOutput=syslog
StandardError=syslog
WorkingDirectory=/usr/lib/my-cool-api
SyslogIdentifier=my-cool-api
User=my-cool-api
Group=my-cool-api
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/my-cool-api-with-service-options.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ After=network.target nss-lookup.target

[Service]
ExecStart=/usr/bin/npm start
WorkingDirectory=/usr/lib/my-cool-api
Restart=always
StandardOutput=syslog
StandardError=syslog
CPUSchedulingPriority=50
LimitNOFILE=10000
WorkingDirectory=/usr/lib/my-cool-api
SyslogIdentifier=my-cool-api
User=my-cool-api
Group=my-cool-api
CPUSchedulingPriority=50
LimitNOFILE=10000

[Install]
WantedBy=multi-user.target
2 changes: 1 addition & 1 deletion test/fixtures/my-cool-api-with-unit-options.service
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Requires=my-coolest-api.service

[Service]
ExecStart=/usr/bin/npm start
WorkingDirectory=/usr/lib/my-cool-api
Restart=always
StandardOutput=syslog
StandardError=syslog
WorkingDirectory=/usr/lib/my-cool-api
SyslogIdentifier=my-cool-api
User=my-cool-api
Group=my-cool-api
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/my-cool-api.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ After=network.target nss-lookup.target

[Service]
ExecStart=/usr/bin/npm start
WorkingDirectory=/usr/lib/my-cool-api
Restart=always
StandardOutput=syslog
StandardError=syslog
WorkingDirectory=/usr/lib/my-cool-api
SyslogIdentifier=my-cool-api
User=my-cool-api
Group=my-cool-api
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/my-super-long-long-long-long-cat-api.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ After=network.target nss-lookup.target

[Service]
ExecStart=/usr/bin/npm start
WorkingDirectory=/usr/lib/my-super-long-long-long-long-cat-api
Restart=always
StandardOutput=syslog
StandardError=syslog
WorkingDirectory=/usr/lib/my-super-long-long-long-long-cat-api
SyslogIdentifier=my-super-long-long-long-long-cat-api
User=my-super-long-long-long-long-cat
Group=my-super-long-long-long-long-cat
Expand Down