Skip to content

Commit

Permalink
Merge pull request #1 from nir0s/phase-two
Browse files Browse the repository at this point in the history
Phase two
  • Loading branch information
nir0s committed Dec 30, 2015
2 parents ae0167c + 5c37ab6 commit 0b4c83d
Show file tree
Hide file tree
Showing 24 changed files with 576 additions and 259 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sudo: false
sudo: required
language: python
python:
- "2.7"
Expand All @@ -8,6 +8,8 @@ env:
- TOX_ENV=py26
install:
- pip install tox
- sudo pip install tox
script:
- tox -e $TOX_ENV
- sudo tox -e deploy

42 changes: 37 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ NOTE: Serv requires sudo permissions! (you can't write to /etc/init.d, /lib/syst

### Supported Init Systems

[systemd](http://www.freedesktop.org/wiki/Software/systemd/), [Upstart](http://upstart.ubuntu.com/) and [SysV](https://wiki.archlinux.org/index.php/SysVinit) are mostly supported now though SysV doesn't yet support retrieving a service's `status`.
[systemd](http://www.freedesktop.org/wiki/Software/systemd/), [Upstart](http://upstart.ubuntu.com/) and [SysVinit](https://wiki.archlinux.org/index.php/SysVinit) are mostly supported now though SysV doesn't yet support retrieving a service's `status`.
[nssm](https://nssm.cc/) (Non-Sucking Service Manager for Windows) is currently being worked on.

I intend to add:
Expand Down Expand Up @@ -55,6 +55,25 @@ sudo pip install https://github.com/nir0s/serv/archive/master.tar.gz

Before using, please read the caveats section!

```shell
$ sudo serv
...

Usage: serv [OPTIONS] COMMAND [ARGS]...

Options:
--help Show this message and exit.

Commands:
generate Creates (and maybe runs) a service.
remove Stops and Removes a service
status Retrieves a service's status.
...
```
### Creating a service
```shell
Expand Down Expand Up @@ -123,6 +142,13 @@ INFO - Service removed.
$ ss -lntp | grep 8000
```

### nssm-specific usage pattern

Windows support is provided via the Non-Sucking Service Manager (nssm).

There are some differences between Windows and Linux support. While the API is practically the same, it still requires the user to be a bit more cautious.

For instance, when providing the `--args` flag, single quotes won't do (e.g. '-m SimpleHTTPServer') but rather doubles must be used and cmd must be loaded as Administrator to be able to install the service as it requires elevated privileges.
## Python API
Expand All @@ -134,7 +160,7 @@ Kidding.. it's there, it's easy and it requires documentation.
## How does it work
Serv, unless explicitly specified by the user, looks up the the platform you're running on (Namely, linux distro and release) and deduces which init system is running on it by checking a static mapping table or an auto-lookup mechanism.
Serv, unless explicitly specified by the user, looks up the platform you're running on (Namely, linux distro and release) and deduces which init system is running on it by checking a static mapping table or an auto-lookup mechanism.

Once an init-system matching an existing implementation (i.e supported by Serv) is found, Serv renders template files based on a set of parameters; (optionally) deploys them to the relevant directories and (optionally) starts the service.

Expand All @@ -144,19 +170,21 @@ Since Serv is aware of the init system being used, it also knows which files it

* Init system identification is not robust. It relies on some assumptions (and as we all know, assumption is the mother of all fuckups). Some OS distributions have multiple init systems (Ubuntu 14.04 has Upstart, SysV and half (HALF!?) of systemd).
* Stupidly enough, I have yet to standardize the status JSON returned and it is different for each init system.
* Currently if anything fails during service creation, cleanup is not performed. This will be added in future versions.
* If anything fails during service creation, cleanup is not performed. This will be added in future versions.
* Currently, all errors exit on the same error level. This will be changed soon.

### Missing directories

In some situations, directories related to the specific init system do not exist and should be created. For instance, even if systemd (`systemctl`) is available, `/etc/sysconfig` does not exist. IT IS UP TO THE USER to create those directories if they don't exist as Serv should not change the system on that level. The exception to the rule is with `nssm`, which will create the required dir (`c:\nssm`) for it to operate.
The user will be notified of which directory is missing.
Required dirs are:
#### Systemd
* `/lib/systemd/system`
* `/etc/sysconfig` (required only if environment variables are provided.)
* `/etc/sysconfig`
#### SysV
Expand All @@ -167,6 +195,10 @@ Required dirs are:
* `/etc/init`
#### Nssm
The directory (`c:\nssm`) will be created for the user in case it doesn't exist.

## Testing

```shell
Expand All @@ -185,5 +217,5 @@ Pull requests are always welcome to deal with specific distributions or just for
* Under serv/init, add a file named <init_system_name>.py (e.g. runit.py).
* Implement a class named <init_system_name> (e.g. Runit). See [systemd](https://github.com/nir0s/serv/blob/master/serv/init/systemd.py) as a reference implementation.
* Pass the `Base` class which contains some basic parameter declarations and provides a method for generating files from templates to your class (e.g. `from serv.init.base import Base`).
* Add the relevant template files to serv/init/templates. The file names should be formatted as: `<init_system_name>_<init_system_version>.*.j2` (e.g. runit_default.j2).
* Add the relevant template files to `serv/init/templates`. The file names should be formatted as: `<init_system_name>_<init_system_version>.*` (e.g. runit_default).
* In `serv/init/__init__.py`, import the class you implemented (e.g. `from serv.init.runit import Runit`).
40 changes: 40 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
environment:

TOX_ENV: pywin

matrix:
- PYTHON: C:\Python27
PYTHON_VERSION: 2.7.8
PYTHON_ARCH: 32

install:

#################################
# Change Python Registry
#################################

- reg ADD HKCU\Software\Python\PythonCore\2.7\InstallPath /ve /d "C:\Python27" /t REG_SZ /f
- reg ADD HKLM\Software\Python\PythonCore\2.7\InstallPath /ve /d "C:\Python27" /t REG_SZ /f

#################################
# Installing Inno Setup
#################################

- choco install -y InnoSetup
- set PATH="C:\\Program Files (x86)\\Inno Setup 5";%PATH%

- SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%

- echo Upgrading pip...
- ps: (new-object System.Net.WebClient).Downloadfile('https://bootstrap.pypa.io/get-pip.py', 'C:\Users\appveyor\get-pip.py')
- ps: Start-Process -FilePath "C:\Python27\python.exe" -ArgumentList "C:\Users\appveyor\get-pip.py" -Wait -Passthru
- pip --version

build: false # Not a C# project, build stuff at the test step instead.

before_test:
- echo Installing tox (2.0.0)
- pip install tox==2.0.0

test_script:
- tox -e %TOX_ENV%
22 changes: 11 additions & 11 deletions serv/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@

SYSTEMD_SVC_PATH = '/lib/systemd/system'
SYSTEMD_ENV_PATH = '/etc/sysconfig'
UPSTART_SCRIPT_PATH = '/etc/init'
SYSV_SCRIPT_PATH = '/etc/init.d'
UPSTART_SVC_PATH = '/etc/init'
SYSV_SVC_PATH = '/etc/init.d'
SYSV_ENV_PATH = '/etc/default'
NSSM_BINARY_LOCATION = 'c:\\nssm'
NSSM_SCRIPT_PATH = 'c:\\nssm'
NSSM_BINARY_PATH = 'c:\\nssm'
NSSM_SVC_PATH = 'c:\\nssm'

TEMPLATES = {
'systemd': {
'default': {
'systemd_default.service.j2': '/lib/systemd/system',
'systemd_default.env.j2': '/etc/sysconfig'
'.service': '/lib/systemd/system',
'': '/etc/sysconfig'
}
},
'sysv': {
'default': {
'sysv_default.default.j2': '/etc/default',
'sysv_default.j2': '/etc/init.d'
'': '/etc/init.d',
'.defaults': '/etc/default'
}
},
'upstart': {
'default': {
'upstart_default.conf.j2': '/etc/init'
'.conf': '/etc/init'
},
'1.5': {
'upstart_1.5.conf.j2': '/etc/init'
'.conf': '/etc/init'
}
},
'nssm': {
'default': {
'nssm_default.conf.j2': 'c:\\nssm'
'.bat': 'c:\nssm'
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion serv/dictconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
import types

import six
from . import six

IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I)

Expand Down
Loading

0 comments on commit 0b4c83d

Please sign in to comment.