Commit Graph

350 Commits (7d26f0f69b01016f1a8c563ac734253e2f9c6d5b)
 

Author SHA1 Message Date
Jose M. Guisado ecd735c9dc #1068 add -c/--config optional parameter
ogClient allows the user to specify the json config path with optional
parameters -c / --config:

	ogclient -c /foo/bar/foobar.json

If specified config file is malformed ogClient will fail to start.
2022-02-01 09:44:42 +01:00
Jose M. Guisado 355e6caad3 #1065 windows: use shutdown for poweroff and reboot operations
Running ogClient as a service (non interactive user) breaks
poweroff and reboot using ExitWindowsEx function in user32.dll.

Spawn a subshell using os.system and use the 'shutdown' command
instead.

This is a terminating command, we don't need fine grain from
subprocess module.
2021-12-10 12:56:50 +01:00
Jose M. Guisado 1601ad9b29 #1065 Init config_path according to platform
Commit 2dbcd18c06 breaks interfaceAdm path for live operations.

Keep OG_PATH to the value prior to commit 2dbcd18c06.

Check platform before reading config file. If platform is different
from linux then look for cfg/ogclient.json in current folder and
do not use OG_PATH.
2021-12-10 10:03:12 +01:00
Jose M. Guisado 05f2fd43c2 #1065 Add event subcommand arguments
ogClient can be invoked with additional arguments to send
event datagrams to the default event datagram socket at
127.0.0.1 port 55885.

ogclient session events invokation syntax:

	ogclient event login foobar
	ogclient event logout foobar

If event commands arguments are detected, ogclient sends the
datagram and closes afterwards.

Datagram syntax for session event is:

	session [start | stop] [user]
2021-11-30 12:44:52 +01:00
Jose M. Guisado 2465ef25b7 #1065 Add event datagram socket
ogClient can receive events via a datagram socket opened at 55885.
This socket is only opened when in windows or linux mode, for
event reporting from within the system.

Events reported this way are sent back to ogServer via a 103 Early
Hints HTTP message. Information regarding the event is sent in the
response's payload.
2021-11-29 12:58:13 +01:00
Jose M. Guisado 6ddc1da7ca #1065 Fix windows not reattempting refused connections
Windows does not report a refused connection the same way as Linux.
Unsuccesful connect socket will be kept in the exceptfds, and won't
be in the readable nor writable fds. The socket in this state will
have SO_ERROR set to ECONNREFUSED.

On the other hand, Linux does not use exceptfds for such case.
2021-11-23 12:46:31 +01:00
Jose M. Guisado b5d5d29d31 #1065 Support pyinstaller for Windows
Add required function call before running any process with
multiprocessing. This is required for windows executables to
work properly when using pyinstaller.

See:
https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing
2021-11-22 09:07:15 +01:00
Jose M. Guisado 2e9e5bcb3d #1065 windows: add poweroff and reboot
Uses ExitWindowsEx from user32.dll. Library is loaded using ctypes.

See https://stackoverflow.com/a/50824776
2021-11-19 11:13:05 +01:00
Jose M. Guisado 2dbcd18c06 #1065 revisit config_path initialization
If current platform is Linux (either live, virtual or linux) expect
/opt/opengnsys/ogclient/cfg/ to contain ogclient.json.

If current platform is not Linux then we fallback to current directory.
2021-11-18 16:31:36 +01:00
Jose M. Guisado f9511932ad #1065 Add windows mode
Add agent mode for windows platform.

Subprocess module for shell/run is cross-platform an no change was
needed. The subprocess will run with the same privilege as its parent,
ogclient.

TODO: Provide a windows installer. As of now,  an administrator
needs to install python and required libraries for this mode to be
usable.
2021-11-18 13:50:16 +01:00
Jose M. Guisado 3dfe54968b #1065 Use logging module instead of syslog
We can't use syslog if we want to execute ogClient in the Windows
platform.

Use the native logging library so we can attach different handlers
depending on the mode ogClient is executing.

Logging configuration is done via a python dict. There is a different
dict for linux and windows. These dicts define the configuration of the
root logger, handlers and formatters used.

As of now, it is only expected to use the root logger for everything
logging related. The root logger is obtained via:

	LOGGER = logging.getLogger()

More info about handlers, formatters and loggers:
	https://docs.python.org/3/howto/logging.html

Logging configuration is done at startup, just after parsing the json
(knowing ogclient mode). If json parsing goes bad, ogclient will only
print a message to stdout.
2021-11-18 10:29:46 +01:00
Jose M. Guisado fd1f01d76b #1065 Avoid SIGPIPE errors in windows mode
SIGPIPE is only available on Unix. Add try/except so that this
import does not crash ogclient when running in Windows mode.

Only assign SIG_DFL handler to SIGPIPE when mode is not windows.

Prefer signal.signal over signal alone to better distinguish module from
function.
2021-11-17 13:00:33 +01:00
Jose M. Guisado ab7abf96a6 #1065 linux: add shell run operation
- Executed script runs with same privilege as ogClient process.
- Uses subprocess.run instead of subprocess.Popen, it's a bit simpler.
  We can't specify executable, though. Shouldn't need so in Linux mode.
- Uses shell=True, keep in mind security considerations listed at:
  https://docs.python.org/3/library/subprocess.html#security-considerations
  (shlex.quote can be used for unix shells)
2021-11-15 13:39:42 +01:00
Jose M. Guisado 69d214f63b #1065 linux: add systray icon
Adds a systray icon for linux mode. Uses pystray module. Expects a
favicon.ico stored in the same folder as the main ogclient python
script, but if not found a placeholder image is used.
2021-11-15 13:39:36 +01:00
Jose M. Guisado 2d3d31bf61 #1065 src: add linux mode
ogClient can run in "linux" mode. In addition to live or virtual.
Serves as a substitute to the legacy ogagent, which has not received any
updates since 2020/07/23.

Linux mode initially supports remote reboot and poweroff. Requires
updated ogServer with the Linux ogclient state.

ogClient can be set up to run in linux mode by specifying it in
ogclient.json:

{
	"opengnsys": {
		"ip": "192.168.56.10",
		"port": 8889,
		"log": "DEBUG",
		"mode": "linux",
	...
}
2021-11-15 13:37:40 +01:00
OpenGnSys Support Team e549bd819e #1056 stop thread if connection with server is lost
Abort command if the connection with the server is lost.

Otherwise, a race condition that leaves ogServer and ogClient
out-of-sync might occur:

1. ogClient is busy running a command (on the worker thread), for example,
   image/restore.
2. ogServer is stopped OR ogClient loses connection with ogServer (due to
   transient network problem).
3. ogClient reconnects and ogServer sends a refresh command.
4. ogClient worker thread finishes and it sends a reply to image/restore.
5. ogServer gets confused because it expects a reply to the refresh
   command, not the old image/restore.
2021-11-11 13:12:45 +01:00
Jose M. Guisado cc0d98786d #1060 virtual: enable audio and wifi vfio by default 2021-09-01 13:33:18 +02:00
Jose M. Guisado 0c00f64669 #1059 virtual: replace qmp polling for event listening
Polling for a qmp port availability is undesirable, as QEMU only handles
one connection to the qmp port at a time, ogClient may interfere with
cloneer-manager.

Check vm thread now connects to a separate qmp tcp socket, listening for
a shutdown guest event.

When ogClient is run just after ogVDI installation (before guest
installation) it will try to connect until it's possible, ie: after an
iso is specified and a qemu vm is started that exposes the appropiate
qmp tcp port.
2021-09-01 13:29:58 +02:00
OpenGnSys Support Team 082079ad78 #1056 stop request processing on connection closure
If server closes the connection, close the socket, reconnect and stop
processing. self.connect() already cleans up the internal state, including the
socket state.
2021-07-15 13:08:51 +02:00
OpenGnSys Support Team c61964fa85 #1056 add cleanup method and use it
add method to clean up interval socket state and use it
2021-07-15 13:08:43 +02:00
Javier Sánchez Parra 7940887068 #1000 Fix HTTP request header length parsing
OgClient miscalculates the body size of the request.

ogServer delimits HTTP headers with "\r\n\r\n" to comply with RFC 2616.
But ogClient searches for the first "\r\n" delimiter, hence, ogClient
stops at the first HTTP header field instead of at the end of the header.
Hence, it incorrectly assumes the body starts after the first "\r\n".

This commit updates ogClient to search for the "\r\n\r\n" delimiter.

Example:

POST /shell/run HTTP/1.1\r\n <-- ogClient considers body starts here (WRONG!)
Content-Length: 952\r\n
Content-Type: application/json\r\n
\r\n                         <-- Here is where the body starts
{"json-body":...}
2021-07-13 14:07:57 +02:00
Javier Sánchez Parra b5c3f58cc4 #1037 Add disk type
Add ogClient support to receive, parse and send disk type data from the cloning
engine when refreshing disks configuration.

See also commits with #1037 in ogServer and WebConsole repo.
2021-05-26 17:27:33 +02:00
OpenGnSys Support Team cb9edc8d95 ogClient is AGPLv3+
Update license header in files.
2021-05-14 00:19:28 +02:00
Jose M. Guisado bd98dd1da0 #995 Add link speed in probe responses
Separates probe method into separate ogclient modes (virtual, vdi) so
future supported OS can easily have a tailored probe responses.

Link speed is retrieved using a minimal ethtool command sent using fcntl
module from python.
2021-05-04 18:30:40 +02:00
OpenGnSys Support Team 8b959c8be9 #980 Broken TCP connection times out after 120 seconds through keepalive
Enable TCP keepalive to detect if the ogServer is gone (hard reset). If no reply
after 120 seconds, then release the connection to the server.
2021-04-23 00:34:39 +02:00
Jose M. Guisado baa03debd7 #1000 live: check imageCreate for non-zero return code
When running image_create operation an underlying script "imageCreate"
is called using the subprocess python module. This script may fail, for
example if the repository in which the image is to be stored has no
sufficent capacity.

Check for non-zero when the imageCreate process has finished.
2020-12-15 12:16:19 +01:00
Jose M. Guisado f0aa3df485 Rename 'linux' folder and operations to 'live'
ogLive related operations are named inside a 'Linux' folder, also its
python class is named OgLinuxOperations. Rename every 'linux' occurrence
with live to further clarify this folder and operations.

 - OgLinuxOperations -> OgLiveOperations
 - src/linux/ -> src/live/

Fixes: 1377acee ('Rename 'linux' mode to 'live' mode')
2020-12-03 13:38:43 +01:00
Javier Sánchez Parra 900a1c8f08 #1010 Change POST /software to GET
ogClient /software gets a representation of the target resource’s state.
GET method is more appropriate than POST.

Change /software method from POST to GET.
2020-12-02 14:12:40 +01:00
Jose M. Guisado f8e566bf63 #1000 ogRest: set idle state after processing bad request
Before this patch the ogRest would hang indifinitely in a BUSY state
when a bad request was received. Fix this by returning ogRest state to
IDLE once the corresponding bad request response has been sent.

This accounts for the following cases:

 - Unknown GET action
 - Unknown POST action
 - Unknown HTTP verb
2020-12-01 13:32:18 +01:00
Javier Sánchez Parra a11224d6f5 #1008 Add support to work with GPT table
ogClient /setup in linux mode do not support to indicate which table
type the user want to use. It always supposes that the partition table
is MBR/MSDOS.

Add ogClient support to work with GPT tables. Add new field table type
to /setup linux mode that expects a string with "MSDOS" or "GPT".

Example old JSON:
{
  "disk": "1",
  "cache": "0",
  "cache_size": "0",
  "partition_setup": [...]
}

Example new JSON:
{
  "type": "GPT",
  "disk": "1",
  "cache": "0",
  "cache_size": "0",
  "partition_setup": [...]
}
2020-11-27 12:52:54 +01:00
Javier Sánchez Parra 0411d2bf94 #1008 Adapt virtual disk setup JSON format
Previous commits updates ogClient and ogServer to support several disks
in Linux (ogLive) mode. This changes disk setup JSON format from an
object to an array. ogClient in virtual mode also need to satisfy this
new format, although it not support several disks.

Adapt ogClient virtual mode to satisfy new disk setup JSON format.

Old format:

    "disk_setup": {
        "disk": 1,
        "partition": 0,
        "code": "0",
        "filesystem": "",
        "os": "",
        "size": 32685957,
        "used_size": 6
    },

New format:

    "disk_setup": [
        {
            "disk": 1,
            "partition": 0,
            "code": "0",
            "filesystem": "",
            "os": "",
            "size": 32685957,
            "used_size": 6
        }
    ],
2020-11-19 13:20:08 +01:00
Javier Sánchez Parra 91f034e371 #1008 Add support for several disks
ogClient expect to receive information of 1 disk from the Cloning
Engine's script "getConfiguration". ogAdmClient, the deprecated ogLive
client daemon, supported for several disk.

Add ogClient support for several disk.
2020-11-18 13:11:55 +01:00
Jose M. Guisado 29d3d06a9c Fix license name in setup.py
The file containing the license was being referred as LICENSE when it
was renamed to COPYING.

Fixes: 173fabb ("Rename LICENSE to COPYING")
2020-11-06 12:05:56 +01:00
Javier Sánchez Parra b138fbc0db #1004 Send datasize in bytes
Image datasize is expressed in kibibytes but the existing REST API field
represent data in bytes.

This commit changes ogClient to send datasize in bytes.
2020-09-29 12:34:05 +02:00
Javier Sánchez Parra c86eae48fe #1004 Add new fields to /image/create response
Extend ogClient to include more information about the image that has been
created. This patch modifies ogClient to read an info file created by image
creation script, add this info to the JSON response and then remove the file.

Example of new /image/create response:

{
  "disk": "1",
  "partition": "1",
  "code": "131",
  "id": "1",
  "name": "ubuntu",
  "repository": "192.168.56.10",
  "software": "Ubuntu 18.04.5 LTS \naccountsservice 0.6.45\n...",
  "clonator": "PARTCLONE",
  "compressor": "LZOP",
  "filesystem": "EXTFS",
  "datasize": 2100000
}

New fields are "clonator", "compressor", "filesystem" and "datasize".
2020-09-10 14:46:37 +02:00
Javier Sánchez Parra 36b5064970 #1000 Fix ogClient HTTP length handling
Irina reports that "Partition assistant"/"Asistente de particionado" is
not working. This is happening because ogClient is not reading the full
data ogServer sends when the entire HTTP PDU is larger than 1024.
However, ogClient should read the whole message, reading until read data
length is greater or equal to "Content-Length" header value.

ogClient fails to obtain "Content-Length" value because is looking for
"content-length", be aware of the case sensitivity. It also needs to
take into account the header length because read data length also
includes headers.

This patch updates ogClient to:

1) look for "Content-Length instead of "content-length".

2) compare read date length with content length plus headers
   length.
2020-08-25 10:55:45 +02:00
Javier Sánchez Parra 0593119352 #999 Fix ogClient session command
Disconnect gracefully from ogServer after booting OS, the script to start the
OS calls kexec, so everything is gone after it. For Windows, this results in a
reboot.

This commit also improves the disconnect function to make sure the
disconnection is synchronous.
2020-08-21 18:39:35 +02:00
Javier Sánchez Parra de129f7449 Add Virtual status
Since version 1.2.0, OpenGnsys supports ogVDI hypervisor OS. This commit
a new status which indicates that clients are running ogVDI
2020-06-26 14:39:21 +02:00
Roberto Hueso Gómez 93f1b35fcd Add syslog logs for HTTP requests and responses
This is useful for debuging and getting information on the processes that are
being executed in ogclient.

syslog outputs are something similar to:

Jun 26 10:36:40 ogAdministrator /ogclient: GET refresh HTTP/1.1
Jun 26 10:36:40 ogAdministrator /ogclient: HTTP/1.0 500 Internal Server Err
2020-06-26 12:37:09 +02:00
OpenGnSys Support Team 0ada33c695 rename main.py to ogclient 2020-06-26 11:04:32 +02:00
OpenGnSys Support Team 87c2a6ae4b fix shebang 2020-06-26 11:02:26 +02:00
Javier Sánchez Parra ffbcf7ebba Check return code on restore image command
When restore image command was sent and the restoration failed, you
could see in WebConsole the image as restored and the command completed,
as it if had not failed. This happened because ogClient did not check
the return code of restoration script.

This commit adds return code check on restore image. So, when return
code is a non-zero value ogClient responses with an error 500. When
ogServer receives this error response, it did not set in the database
the image as restored and command as completed without errors.
2020-06-23 13:42:39 +02:00
OpenGnSys Support Team 173fabb84c Rename LICENSE to COPYING
From https://www.gnu.org/licenses/gpl-howto.en.html:

"You should also include a copy of the license itself somewhere in the
distribution of your program. All programs, whether they are released under the
GPL or LGPL, should include the text version of the GPL. In GNU programs we
conventionally put the license in a file called COPYING."
2020-06-18 16:45:46 +02:00
Roberto Hueso Gómez 118572c3b6 Add setup.py installation script
This script can be useful to install ogclient on distributions that do not have
an already built ogclient package.

It is necessary to install ogclient in the right path, so the command you have
to use looks similar to:
python3 setup.py install --prefix=/opt/opengnsys/ogclient
                         --install-lib=/opt/opengnsys/ogclient
                         --install-script=/opt/opengnsys/ogclient
2020-06-16 10:22:30 +02:00
Roberto Hueso Gómez 93bbc39d4b Import OgLinuxOperations only when necessary
This fixes a circular import error produced by the import of ogClient inside of
OgLinuxOperations.
2020-06-08 14:26:35 +02:00
Roberto Hueso Gómez 1377aceec2 Rename 'linux' mode to 'live' mode
'linux' represents ogLive mode that is the reason for the rename.
2020-06-05 14:55:10 +02:00
Roberto Hueso Gómez d7b7b0b175 Delete ogConfig.py
This file is only used for its OG_PATH variable, this variable is now in
ogClient.
2020-06-05 13:08:00 +02:00
Roberto Hueso Gómez 3205e0f1e8 Refresh partitions.json after image restore
This is necessary for cloneer-manager to start the guest OS after the next
reboot.
2020-05-29 13:29:16 +02:00
Roberto Hueso Gómez b29b2eb452 Move check_vm_state_loop() into OgVirtualOperations
Improves code encapsulation by moving check_vm_state_loop method into
OgVirtualOperations class. This also fixes import error when running ogclient in
'linux' mode.
2020-05-26 11:24:26 +02:00
Roberto Hueso Gómez 80b7023ecb Add virtual config example file
This file is an example of the configuration for an ogclient running in virtual
mode.
2020-05-25 13:39:57 +02:00