Giter VIP home page Giter VIP logo

systemctl-php's Introduction

systemctl-php

CI Pipeline Code Climate Test Coverage Join the chat at https://gitter.im/icanhazstring/systemctl-php

PHP wrapper for systemctl

How to install

$ composer require icanhazstring/systemctl-php

Current supported units

See Units

If you like to add support for more units, feel free to grab an issue and contribute.

Current supported commands

  • start
  • stop
  • enable
  • disable
  • reload
  • restart
  • isEnabled
  • isActive

If you like to add support for more commands, feel free to contribute.

How to change the binary

SystemCtl::setBinary('/bin/systemctl');

How to change command timeout

To change command tmeout simply call the static method setTimeout.

SystemCtl::setTimeout(10);

The default timeout is set to 3 seconds

"I need sudo to run commands"

If you need sudo, you should execute the bin executable with sudo. The incode support was dropped due to security reason.

How do I start/stop/restart a unit?

Simply is that. First we instantiate a SystemCtl instance an load a unit from a specific type. Here we use a Service. You will always get back true if the command succeeded. Otherwise the method will throw a CommandFailedException.

$systemCtl = new SystemCtl();

// start/stop/enable/disable/reload/restart
$systemCtl->getService('nginx')->start();
$systemCtl->getService('nginx')->stop();

How to Contribute

Clone the repo and install using composer

$ composer install

Make your changes and make sure you run test, codesniffer and phpstan.

$ composer test
> vendor/bin/phpunit
PHPUnit 9.5.3 by Sebastian Bergmann and contributors.

...............................................................  63 / 128 ( 49%)
............................................................... 126 / 128 ( 98%)
..                                                              128 / 128 (100%)

Time: 00:00.033, Memory: 10.00 MB

OK (128 tests, 192 assertions)

$ composer cs
> vendor/bin/phpcs --standard=PSR2 src/ && vendor/bin/phpcs --standard=PSR2 tests/

$

$ composer analyse
> vendor/bin/phpstan analyse --no-progress
Note: Using configuration file /data/systemctl-php/phpstan.neon.


 [OK] No errors

$

Credits

This library is heavily influenced by @mjanser php-systemctl.

systemctl-php's People

Contributors

func0der avatar gitter-badger avatar icanhazstring avatar marcingy avatar peter279k avatar ragezbla avatar wirone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

systemctl-php's Issues

Add Unit 'mount'

First of all, the Mount unit type should implement the UnitInterface similar to the Timer or Service.
Despite that everything, that is possible with a Timer should also be possible with a Mount.

In the most simple case, you only need to provide the Mount class implenting the UnitInterface extending AbstractUnit. The class should provide the correct suffix.

For more information about a Unit or Mount see systemd documentation:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
https://www.freedesktop.org/software/systemd/man/systemd.mount.html

Also see PR #27 from @peter279k for reference.

Add Unit 'device'

First of all, the Device unit type should implement the UnitInterface similar to the Timer or Service.
Despite that everything, that is possible with a Timer should also be possible with a Device.

In the most simple case, you only need to provide the Device class implenting the UnitInterface extending AbstractUnit. The class should provide the correct suffix.

For more information about a Unit or Device see systemd documentation:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
https://www.freedesktop.org/software/systemd/man/systemd.device.html

Also see PR #27 from @peter279k for reference.

Feature: reset-failed

If there are units in failed state we can't clear the list of units using disable() or stop().
These units needs to be removed using systemctl reset-failed.

We need to introduce a new method similar to daemonReload()

Consider migrating PHPUnit configuration to be compatible with latest PHPUnit version

When running the vendor/bin/phpunit with php-7.4 and PHPUnit 9.4.x versions, it presents following message:

PHPUnit 9.4.2 by Sebastian Bergmann and contributors.

Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

And I think we can consider using the vendor/bin/phpunit --migrate-configuration to resolve this issue.

Add Unit 'swap'

First of all, the Swap unit type should implement the UnitInterface similar to the Timer or Service.
Despite that everything, that is possible with a Timer should also be possible with a Swap.

In the most simple case, you only need to provide the Swap class implementing the UnitInterface extending AbstractUnit. The class should provide the correct suffix.

For more information about a Unit or Swap see systemd documentation:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
https://www.freedesktop.org/software/systemd/man/systemd.swap.html

Also see PR #27 from @peter279k for reference.

Make ProcessBuilder as optional dependency

Currently the ProcessBuilder is a hard wired dependency of SystemCtl.
Adding this as an optional dependency will improve testability for mocking the ProcessBuilder.

Something like
$systemCtl->setProcessBuilderBuilder($processBuilder)

There should not be an required dependency in the constructor. This way the default construction is still valid. The getProcessBuilder() should be used to call the ProcessBuilderBuilder for an instance of ProcessBuilder

This should be done for v0.x as well as v1.x

Add Unit 'slice'

First of all, the Slice unit type should implement the UnitInterface similar to the Timer or Service.
Despite that everything, that is possible with a Timer should also be possible with a Slice.

In the most simple case, you only need to provide the Slice class implementing the UnitInterface extending AbstractUnit. The class should provide the correct suffix.

For more information about a Unit or Slice see systemd documentation:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
https://www.freedesktop.org/software/systemd/man/systemd.slice.html

Also see PR #27 from @peter279k for reference.

Add Unit 'target'

First of all, the Target unit type should implement the UnitInterface similar to the Timer or Service.
Despite that everything, that is possible with a Timer should also be possible with a Target.

In the most simple case, you only need to provide the Target class implementing the UnitInterface extending AbstractUnit. The class should provide the correct suffix.

For more information about a Unit or Target see systemd documentation:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
https://www.freedesktop.org/software/systemd/man/systemd.target.html

Also see PR #27 from @peter279k for reference.

Can't invoke systemctl --user.

I'm no expert on systemctl or its related technology, far from it.
I do know that systemctl and systemctl --user are two separated concepts on the linux distro I'm working with.

At first glance it doesn't seem possible to add the option user to the command.

Running ->getService('test') works if the unit test is configured as a system unit.

$ systemctl status test

Running ->getService('test') works if the unit test is configured as a user unit but throws an (expected) UnitNotFoundException exception.

$ systemctl --user status test

I hope this is on the road map or, better yet, I'm just missing the obvious solution.

Run command with sudo

If you need sudo, you should execute the bin executable with sudo. The incode support was dropped due to security reason.

How can I do this? If I do this:
$cmd = "php_systemctl"; \SystemCtl\SystemCtl::setBinary("sudo $cmd");
I get an error:
sh: sudo /home/.../php_systemctl: No such file or directory

Add user scope for UnitInstaller

Currently the UnitInstaller does not work with user scope.
The $installPath is fix for system scope installations.

This should work similar to switching the scope to find a unit.

$systemCtl->user()->install($template); // default: ~/.config/systemd/user/
$systemCtl->system()->install($template); // default: /etc/systemd/system/

user home should be retrieved automatically.
http://php.net/manual/en/function.posix-getpwuid.php

To resolve the paths we should use the currently empty PathResolver

Add Unit 'automount'

First of all, the Automount unit type should implement the UnitInterface similar to the Timer or Service.
Despite that everything, that is possible with a Timer should also be possible with a Automount.

In the most simple case, you only need to provide the Automount class implenting the UnitInterface extending AbstractUnit. The class should provide the correct suffix.

For more information about a Unit or Automount see systemd documentation:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
https://www.freedesktop.org/software/systemd/man/systemd.automount.html

Also see PR #27 from @peter279k for reference.

systemctl status support

Hi.

Would it be possible to add a status command, so we can check if a service is currently running, or is enabled?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.