Skip to content

Commit

Permalink
Initial commit of generator-systemd
Browse files Browse the repository at this point in the history
  • Loading branch information
enenkel committed Aug 20, 2017
0 parents commit 7a870dc
Show file tree
Hide file tree
Showing 15 changed files with 4,087 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
**/templates
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
coverage
.idea
temp
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
node_js:
- node
- 7
- 6

script:
- npm run test:ci
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016-2017 OMM Solutions GmbH (http://omm-solutions.de/)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# generator-systemd [![NPM version][npm-image]][npm-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Build Status][travis-image]][travis-url]
> Generates a systemd service unit file
## Installation

First, install [Yeoman](http://yeoman.io) and generator-systemd using [npm](https://www.npmjs.com/) (we assume you have pre-installed [node.js](https://nodejs.org/)).

```bash
npm install -g yo
npm install -g generator-systemd
```

Then generate your new project:

```bash
yo systemd
```

## License
MIT

[npm-image]: https://badge.fury.io/js/generator-systemd.svg
[npm-url]: https://npmjs.org/package/generator-systemd
[travis-image]: https://travis-ci.org/ommsolutions/generator-systemd.svg?branch=master
[travis-url]: https://travis-ci.org/ommsolutions/generator-systemd
[coveralls-image]:https://coveralls.io/repos/github/ommsolutions/generator-systemd/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/github/ommsolutions/generator-systemd?branch=master
69 changes: 69 additions & 0 deletions __tests__/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';
const path = require('path');
const assert = require('yeoman-assert');
const helpers = require('yeoman-test');
const TEMP_DIR = path.join(__dirname, 'temp');
const DEFAULT_NAME = 'test.service';

/**
* Test suite for the whole project
*/
describe('-- generator-systemd --', function () {
/**
* Test suite for the default generator which uses the defaults.
*/
describe('generator-systemd-DEFAULT', function () {
/**
* Checks if all files were generated.
*/
describe('Checking file generation', function () {
it('should have generated file: test.service', () => wrapAssertFile());
});

describe('Checking file content', function () {
const unitRegex = /\[Unit]/g;
const serviceRegex = /\[Service]/g;
const installRegex = /\[Install]/g;
const commentRegex = /#/;
const typeRegex = /Type=simple/g;
const restartRegex = /Restart=no/g;
const killModeRegex = /KillMode=control-group/g;
const wantedByRegex = /WantedBy=multi-user.target/g;
const defaultTestDescriptionRegex = /WantedBy=multi-user.target/g;
it('Should contain Unit', () => wrapAssertFileContent(unitRegex));
it('Should contain Service', () => wrapAssertFileContent(serviceRegex));
it('Should contain Install', () => wrapAssertFileContent(installRegex));
it('Should contain "#" in front of directives with no defaults specified by this generator',
() => wrapAssertFileContent(commentRegex));
it('Should contain Type default', () => wrapAssertFileContent(typeRegex));
it('Should contain Restart default', () => wrapAssertFileContent(restartRegex));
it('Should contain KillMode default', () => wrapAssertFileContent(killModeRegex));
it('Should contain WantedBy default', () => wrapAssertFileContent(wantedByRegex));
it('Should contain default test description', () => wrapAssertFileContent(defaultTestDescriptionRegex));
});
});
});

// ######### HELPERS #########
function run() {
return helpers.run(path.join(__dirname, '../generators/app'), {tmpdir: false})
.inDir(TEMP_DIR)
.withPrompts({
description: 'default settings applied'
});
}

function wrapAssertFile() {
return run().then(() => assert.file([DEFAULT_NAME]));
}

/**
* Assert some regex content for the generated systemd service unit
* @param regexToExpect The regex to match with
* @returns {Promise} Resolves if the regex matches, rejects with the stack otherwise.
*/
function wrapAssertFileContent(regexToExpect) {
return run().then(() =>
assert.fileContent(DEFAULT_NAME, regexToExpect));
}
// ######### HELPERS END #########
117 changes: 117 additions & 0 deletions generators/app/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const SERVICE_NAME = 'servicename';
const DESCRIPTION = 'description';
const AFTER = 'after';
const REQUIRES = 'requires';
const TYPE = 'type';
const USER = 'user';
const ENVIRONMENT_FILE = 'environmentfile';
const EXEC_START = 'execstart';
const RESTART = 'restart';
const RESTART_SEC = 'restartsec';
const KILL_MODE = 'killmode';
const WANTED_BY = 'wantedby';

const PROMPTS = [
{
type: 'input',
name: SERVICE_NAME,
message: 'Enter the name of the service.\n\n',
default: 'test'
},
{
type: 'input',
name: DESCRIPTION,
message: '(Description=) Describe the service.\n\n',
default: ''
},
{
type: 'input',
name: AFTER,
message: '(After=) A space-separated list of unit names. This service startup will be delayed until all of the defined ' +
'units did start up if they are started at the same time. \n\n',
default: undefined
},
{
type: 'input',
name: REQUIRES,
message: '(Requires=) Defines required dependencies. If this unit gets activated, the units listed here will be ' +
'activated as well. If one of the other units gets deactivated or its activation fails, ' +
'this unit will be deactivated. \n\n',
default: undefined
},
{
type: 'rawlist',
pageSize: 10,
name: TYPE,
message: '(Type=) Configures the process start-up type for this service unit. Documentation can be found here: ' +
'https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=\n\n',
choices: ['simple', 'forking', 'oneshot', 'dbus', 'notify', 'idle'],
default: 'simple'
},
{
type: 'input',
name: USER,
message: '(User=) Set the UNIX user or group that the processes are executed as.\n\n',
default: undefined
},
{
type: 'input',
name: ENVIRONMENT_FILE,
message: '(EnvironmentFile=) Allows to provide environment variables from a file. More information can be found here: ' +
'https://www.freedesktop.org/software/systemd/man/systemd.exec.html#EnvironmentFile=\n\n',
default: undefined
},
{
type: 'input',
name: EXEC_START,
message: '(ExecStart=) Commands with their arguments that are executed when this service is started. More information can be found here: ' +
'https://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStart=\n\n',
default: undefined
},
{
type: 'rawlist',
pageSize: 10,
name: RESTART,
message: '(Restart=) Configures whether the service shall be restarted when the service process exits, is killed or a timeout is reached.' +
' More information can be found here: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Restart=\n\n',
choices: ['no', 'on-success', 'on-failure', 'on-abnormal', 'on-watchdog', 'on-abort', 'always'],
default: 'no'
},
{
type: 'input',
name: RESTART_SEC,
message: '(RestartSec=) Configures the time to sleep before restarting a service (as configured with Restart=). Unitless value in seconds.\n\n',
default: undefined
},
{
type: 'rawlist',
pageSize: 10,
name: KILL_MODE,
message: '(KillMode=) Specifies how processes of this unit shall be killed. More information can be found here: ' +
'https://www.freedesktop.org/software/systemd/man/systemd.kill.html#KillMode=\n\n',
choices: ['control-group', 'process', 'mixed', 'done'],
default: 'control-group'
},
{
type: 'input',
name: WANTED_BY,
message: '(WantedBy=) The current unit will be started when the listed unit is started.\n\n',
default: 'multi-user.target'
}
];

module.exports = {
SERVICE_NAME,
DESCRIPTION,
AFTER,
REQUIRES,
TYPE,
USER,
ENVIRONMENT_FILE,
EXEC_START,
RESTART,
RESTART_SEC,
KILL_MODE,
WANTED_BY,
PROMPTS
};
64 changes: 64 additions & 0 deletions generators/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';
const CONSTANTS = require('./constants');
const Generator = require('yeoman-generator');
const chalk = require('chalk');
const yosay = require('yosay');

class BaseGenerator extends Generator {
initializing() {
this.log(yosay(
'Welcome to the terrific ' + chalk.red('generator-systemd') + ' generator!'
));
this.log('All files will be generated in the folder: "' + this.destinationRoot() + '"');
}

prompting() {
return this.prompt(CONSTANTS.PROMPTS).then(props => {
this.props = props;
});
}

writing() {
return this.fs.copyTpl(
this.templatePath('name.service'),
this.destinationPath(this.props[CONSTANTS.SERVICE_NAME] + '.service'),
this.getResponses()
);
}

end() {
this.log('Generation finished successfully.');
this.log('Check here for more information on systemd services: ' +
'https://www.freedesktop.org/software/systemd/man/systemd.service.html#');
this.log('your service', JSON.stringify(this.getResponses()));
}

getResponses() {
return {
description: this.props[CONSTANTS.DESCRIPTION] ?
'Description=' + this.props[CONSTANTS.DESCRIPTION] : '# Description=',
after: this.props[CONSTANTS.AFTER] ?
'After=' + this.props[CONSTANTS.AFTER] : '# After=',
requires: this.props[CONSTANTS.REQUIRES] ?
'Requires=' + this.props[CONSTANTS.REQUIRES] : '# Requires=',
type: this.props[CONSTANTS.TYPE] ?
'Type=' + this.props[CONSTANTS.TYPE] : '# Type=',
user: this.props[CONSTANTS.USER] ?
'User=' + this.props[CONSTANTS.USER] : '# User=',
envfile: this.props[CONSTANTS.ENVIRONMENT_FILE] ?
'EnvironmentFile=' + this.props[CONSTANTS.ENVIRONMENT_FILE] : '# EnvironmentFile=',
execstart: this.props[CONSTANTS.EXEC_START] ?
'ExecStart=' + this.props[CONSTANTS.EXEC_START] : '# ExecStart=',
restart: this.props[CONSTANTS.RESTART] ?
'Restart=' + this.props[CONSTANTS.RESTART] : '# Restart=',
restartsec: this.props[CONSTANTS.RESTART_SEC] ?
'RestartSec=' + this.props[CONSTANTS.RESTART_SEC] : '# RestartSec=',
killmode: this.props[CONSTANTS.KILL_MODE] ?
'KillMode=' + this.props[CONSTANTS.KILL_MODE] : '# KillMode=',
wantedby: this.props[CONSTANTS.WANTED_BY] ?
'WantedBy=' + this.props[CONSTANTS.WANTED_BY] : '# WantedBy='
};
}
}

module.exports = BaseGenerator;
16 changes: 16 additions & 0 deletions generators/app/templates/name.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
<%= description %>
<%= after %>
<%= requires %>

[Service]
<%= type %>
<%= user %>
<%= envfile %>
<%= execstart %>
<%= restart %>
<%= restartsec %>
<%= killmode %>

[Install]
<%= wantedby %>
23 changes: 23 additions & 0 deletions generators/homebridge/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Base = require('../app/index');

module.exports = class HomeBridgeGenerator extends Base {
initializing() {
super.initializing();
}

prompting() {
return super.prompting();
}

writing() {
this.fs.copy(
this.templatePath('homebridge.service'),
this.destinationPath('homebridge.service')
);

this.fs.copy(
this.templatePath('homebridge.opts'),
this.destinationPath('homebridge.opts')
);
}
};
7 changes: 7 additions & 0 deletions generators/homebridge/templates/homebridge.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Defaults / Configuration options for homebridge
# The following settings tells homebridge where to find the config.json file and where to persist the data (i.e. pairing and others)
HOMEBRIDGE_OPTS=-U /var/homebridge

# If you uncomment the following line, homebridge will log more
# You can display this via systemd's journalctl: journalctl -f -u homebridge
# DEBUG=*
15 changes: 15 additions & 0 deletions generators/homebridge/templates/homebridge.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Unit]
Description=Node.js HomeKit Server
After=syslog.target network-online.target

[Service]
Type=simple
User=homebridge
EnvironmentFile=/etc/default/homebridge
ExecStart=/usr/local/bin/homebridge $HOMEBRIDGE_OPTS
Restart=on-failure
RestartSec=10
KillMode=process

[Install]
WantedBy=multi-user.target
Loading

0 comments on commit 7a870dc

Please sign in to comment.