Commit Graph

109 Commits (cf9577a40edd7c3f8bebcf02aaccb7ef441e7dac)

Author SHA1 Message Date
Alejandro Sirgo Rica f67f3c598a rest: register image/update as a valid request
Accept both image/create and image/update requests from ogServer.
2025-01-31 14:31:17 +01:00
OpenGnSys Support Team 62b52ff364 src: update license header 2024-11-28 16:45:56 +01:00
Alejandro Sirgo Rica e4be5c34eb src: add support for direct command execution
Update live shell run mode for the new REST API interface.
Evaluate the "inline" field to diferentiate between execution of
script in /opt/opengnsys/shell/ and a cmd execution.

Remove usage of echo argument of the API REST.

Update Windows and Linux mode for direct command execution.
Set OutputEncoding environment variable to 'utf-8' in Windows to
unify the encoding of stdout for the invoked programs.

Decode stdout to utf-8-sig to remove potential BOM.

While at this, remove strange legacy ;|\n\r terminator.
2024-11-27 13:53:19 +01:00
Alejandro Sirgo Rica a36c4daa23 src: add user session detection implementation
Detect user login and logout for Linux and Windows.

Report an active interactive session through the /refresh response
so a new ogserver instance can update the session status.

Poll the session change in 5 second intervals in a thread. Use the
same event socket previously used by the old session detection
mechanism to notify a session change.

Use the method check_interactive_session_change in each
ogOperations.py to report the session status.
Return values:
	None: no session changes are found
	True: login
	False: logout

Windows
Verify if psutil.users() has any value.

Linux
Verify all the psutil.users() asociated to a terminal.
2024-11-26 13:02:45 +01:00
OpenGnSys Support Team 8f437bb954 live: remove unused probe
unused since ogserver's commit 87be2ce08 #980 Change initial probe to refresh
2024-09-30 14:42:29 +02:00
OpenGnSys Support Team 3d47b5069a src: use logging.warning()
logging.warn() is deprecated since 3.3.

And use .error() instead when command is unsupported or client is busy, that
should not ever happen.
2024-08-27 12:44:13 +02:00
Alejandro Sirgo Rica fe40f9c5d6 src: add POST cache/fetch method
Add API REST method to fetch an image.
Consolidate image fetch loging for cache/fetch and image/restore.

Resquest payload structure:
{
    'image': 'linux.img'
    'type': 'TIPTORRENT'
    'repository': '12.141.10.2'
}

The client will try to fetch'image' from 'repository' into cache.

Resquest response structure:
{
    'cache': [
        {'name': 'windows.img', 'size': 2432370213, checksum: '5d4dcc677bc19f40a647d0002f4ade90'},
        {'name': 'linux.img', 'size': 243234534213, checksum: '3eb22f888f88a55ad954f55644e1192e'}
    ]
}
2024-08-09 11:04:56 +02:00
OpenGnSys Support Team 9d8a95cc74 live: add checksum field to image/create response
Report image checksum to ogserver through HTTP response.
2024-07-15 09:50:30 +02:00
OpenGnSys Support Team 1c9a13cd96 rest: add cmd field to POST /shell/run
echo command that has been run for storage in ogserver, until GET /shell/output
is invoked.
2024-06-21 14:59:48 +02:00
OpenGnSys Support Team 19cd1b9a78 rest: add retcode field to POST /shell/run
provide return code as result to ogserver.

Update virtual mode driver to return dummy value, although this command
is unimplemented, this seems to be broken due to possible TypeError when
accessing result from caller.
2024-06-21 14:47:30 +02:00
Alejandro Sirgo Rica 60803fe0ed src: add cache info to the image/restore response
Add a 'cache' field into the json payload the client sends to
the server after a restore operation so the server can update
the new cache contents.

Resquest response structure:
{
    ...
    'cache': [
        {'name': 'windows.img', 'size': 2432370213, checksum: '5d4dcc677bc19f40a647d0002f4ade90'},
        {'name': 'linux.img', 'size': 243234534213, checksum: '3eb22f888f88a55ad954f55644e1192e'}
    ]
    ...
}
2024-05-30 17:22:23 +02:00
Alejandro Sirgo Rica 8de2b785a9 src: add POST cache/delete method
Add API REST method to delete cache contents.

Resquest payload structure:
{
    'images': ['windows.img', 'linux.img']
}

The client will try to delete as many images in cache as available
with names matching the list of filenames in the 'images' field.

Resquest response structure:
{
    'cache': [
        {'name': 'windows.img', 'size': 2432370213, checksum: '5d4dcc677bc19f40a647d0002f4ade90'},
        {'name': 'linux.img', 'size': 243234534213, checksum: '3eb22f888f88a55ad954f55644e1192e'}
    ]
}
2024-05-30 17:22:23 +02:00
Alejandro Sirgo Rica dfde363aa6 src: log backtrace in unhandled error cases
Log an error message in known error cases and log a backtrace
otherwise.

Define a new error type OgError to be used in all the 'raise'
blocks to define the error message to log. The exception
propagates until it reaches send_internal_server_error() where
the exception type is checked. If the type is OgError we log
the exception message. Logs the backtrace for other types.

The initial error implementation printed a backtrace everytime
an error ocurred. The next iteration changed it to only print
a backtrace in a very particular case but ended up omiting too
much information such as syntax errors or unknown error context.
The actual implementation only logs the cases we already cover in
the codebase and logs a bracktrace in the others, enabling a
better debugging experience.
2024-04-03 13:31:10 +02:00
Alejandro Sirgo Rica 049b7a5a2b src: make exception messages more contextual and explicit
Provide more information in exception messages as those are the
source of the logging messages. Add information about paths, files
or configuration related to the operation associated to the
exception.
2024-03-21 10:29:57 +01:00
Alejandro Sirgo Rica 2a4ce65a20 src: centralize error logging into send_internal_server_error
Use only the exception messages as the main resource for error
messages.
The previous error code had string duplication in the form of:
	logging.error('msg here')
	raise Exception('msg here')

That approach also has the downside of having log duplication as
it had the local logging.err() and a global logging.exception()
inside send_internal_server_error capturing the exception message.
The actual code only requires raising an exception with a proper
error message.
Improve exception messages to give more error context.
Log every AssertionError as a backtrace.
Use the 'raise Exception from e' syntax to modify the a previously
raised exception 'e' into an exception with aditional context or
different type. This also prevents the message that warns about
newer exceptions being launch after an initial exception.
2024-03-21 10:29:57 +01:00
OpenGnSys Support Team 6c49815d73 live: report permissions and last update when creating image
add .permissions and .lastupdate to json to report to ogserver.
2023-12-12 17:51:50 +01:00
OpenGnSys Support Team b97c4d157a live: report image size when creating image
add .size json field to report the real size of the image file.
2023-12-12 11:33:48 +01:00
Jose M. Guisado 0c03d82ca8 ogclient: add support for X-Sequence header
Enable parsing of "X-Sequence" HTTP headers from incoming requests.
Add "seq" field in restRequest class.

Enable adding "X-Sequence" to outgoing responses.
Add "seq" field inside restResponse class.

Store current client sequence number inside ogClient class.

Ideally, the restRequest object should be used to retrieve the
sequence number but not all processing functions inside ogRest.py
receive the request as parameter (eg: process_refresh).
In the other hand, all processing functions receive the ogClient object.
2023-06-14 14:50:55 +02:00
Jose M. Guisado fa51f2be8f src: remove unused legacy software inventory code
Remove unnecessary InventarioSoftware invocation inside image_create
operation. Software inventory is executed after image creation
(see ogRest.py).

Remove legacy 'path' parameter. This parameter was used to specify the
path of a text file in which legacy bash scripts wrote the software
inventory of the client (something like "Csft-{ip}...").

Fixes: 04bb35bd86 ("live: rewrite software inventory")
Fixes: 2e3d47b7b8 ("Avoid writting /software output to a file")
2023-05-17 12:44:50 +02:00
Jose M. Guisado ee80dc89ad live: rewrite hardware inventory command
Replace legacy shell script InventarioHardware for helper functions
from hw_inventory.py

Use get_hardware_inventory to obtain a HardwareInventory object with
the hardware information. Map the HardwareInventory object to a legacy
response string with the legacy_list_hardware_inventory function.

Remove "Chrd-*" file reading logic, it's no longer needed. Legacy shell
script InventarioHardware uses that file.

Expect a change in the structure of hardware inventory response payload
in the future. This patch does not address the HTTP response containing
the hardware inventory as a '\n' separated string of hardware elements.
2023-04-18 17:15:57 +02:00
Jose M. Guisado 97e753cbfd ogRest: improve error logging when executing operations
Capture all possible Python exceptions in the try/except block of every
opengnsys operation.

Create an error handling function to deduplicate code in the except
block. The error handling function resets the ogRest state to IDLE and
sends the corresponding 500 Internal Server Error.

This *does not cover* every possible error. There are functions inside
ogThread which contain code that may raise errors that are not covered
by any try/except block.
2023-03-10 11:26:46 +01:00
Jose M. Guisado 5b5ef607ec ogRest: remove root logger constant
Remove unnecesary root logger constant: LOGGER

The root logger is used by default when executing:

	logging.debug()
	logging.info()
	logging.warning()
	...

There is no point in doing:

	LOGGER = logging.getLogger()  # Get root logger
	LOGGER.debug()  # Use root logger
2023-03-10 11:26:46 +01:00
Jose M. Guisado c9a3a763dd legacy: rewrite ogGetImageInfo
Rewrites this legacy script behavior using native Python code, using
subprocess module when executing programs like partclone.info or lzop

ogGetImageInfo is a bash script that retrieves information regarding an
OpenGnsys partition image, specifically:

- clonator
- compressor
- filesystem
- datasize (size of the partition image)

This rewrite only supports partclone and lzop compressed images. This is
standard behavior, we have no reports of other programs or compression
algorithms in use.

Keep this legacy function with hungarian notation to emphasize this is
still a legacy component that may be replaced in the future.
2023-03-02 13:15:10 +01:00
Jose M. Guisado 30fdcceea3 src: improve logging
Adds new logging handler redirecting messages to the log file
located in the Samba shared directory (applies to live mode
clients, i.e: ogLive)

Parses log level configuration from ogclient.json. See:

{
	"opengnsys": {
		...
                "log": "INFO",
		...
	}
	...
}

Adds --debug option to set root logger level to DEBUG when starting
ogClient. Overrides log level from config file.

In addition:

- Replaces any occurence of print with a corresponding logging function.
- Unsets log level for handlers, use root logger level instead.
- Default level for root logger is INFO.
- Replaces level from response log messages to debug (ogRest)
2022-06-08 10:27:06 +02: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 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 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 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
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 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 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
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 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
Javier Sánchez Parra 6502180fc8 Only import virtual functions when needed.
ogClient may runs on an OS that do not have all the dependencies needed
to use virtual functions.

This commit change the behaviour to only import virtual functions when
the ogClient has to work with Virtual Machines. This way ogClient works
on environments which do not need virtual functions.
2020-05-20 10:33:41 +02:00
Javier Sánchez Parra 621fb7a786 Adapt ogLinuxOperation to work with json config file
ogClient changed its config file format to json. This patch adapts
ogLinuxOperation to use new config file.

Co-authored-by: Roberto Hueso <rhueso@soleta.eu>
2020-05-18 08:33:37 +02:00
Roberto Hueso Gómez 3c055d198d Wait before polling QMP for host poweroff
This patch waits before polling qemu with QMP so that it has time to be ready
for requests. It also increases wait time in case the host machine is slow.
2020-05-14 17:28:50 +02:00
Roberto Hueso Gómez 269c7b5055 Rename operation 'execCMD' to 'shellrun'
This patch also ignores calls to shellrun when virtual mode is activated.
2020-05-13 17:30:12 +02:00
Roberto Hueso Gómez 38b6d77561 Switch config file to json
This patch makes configuration parsing easier as well as making the full
configuration available in many subclasses.
2020-05-13 14:10:01 +02:00
Roberto Hueso Gómez 6ca16dd200 Poweroff when no VM and no jobs are running
This patch calls poweroff in virtual mode when no VM is running and no jobs are
being executed. This is useful when the guest OS shutdowns so that the host OS
does not continue to run.
2020-05-11 11:12:11 +02:00
Roberto Hueso Gómez b576836e43 Avoid killing ogclient in virtual mode 2020-04-20 09:14:52 +02:00
Roberto Hueso Gómez 2e3d47b7b8 Avoid writting /software output to a file 2020-04-17 16:59:48 +02:00
Roberto Hueso Gómez 7ccc498014 Use samba for create and restore virtual partitions
This requires to configure user and password for samba repositories.
2020-04-17 15:50:35 +02:00