6.1 KiB
6.1 KiB
Implementation Plan: Zinit OpenRPC Client Refactoring
Current State Analysis
- Multiple implementations (zinit.v, zinit_stateless.v, rpc.v, zprocess.v)
- Inconsistent use of OpenRPC vs. direct filesystem operations
- Duplication of functionality across multiple files
- Lack of a unified approach
Implementation Plan
Phase 1: Create a New Unified OpenRPC Client
- Create a new file
zinit_client.vwith a unifiedZinitClientstruct - Implement all methods using the OpenRPC protocol exclusively
- Ensure the client handles all error cases properly
- Add comprehensive documentation for all methods
graph TD
A[Create New OpenRPC Client] --> B[Implement Core Client Methods]
B --> C[Implement Service Management Methods]
C --> D[Implement Configuration Management Methods]
D --> E[Implement System Operations Methods]
E --> F[Implement Logging Methods]
Phase 2: Refactor Existing Implementations
- Refactor
ZinitStatelessto use the new client for all operations - Refactor
Zinitto use the new client for all operations - Refactor
ZProcessto use the new client for all operations - Update factory methods to use the new client
- Ensure backward compatibility by maintaining the same API/interface
graph TD
A[Refactor ZinitStateless] --> B[Refactor Zinit]
B --> C[Refactor ZProcess]
C --> D[Update Factory Methods]
D --> E[Ensure Backward Compatibility]
Phase 3: Update Tests and Examples
- Update existing tests to use the new client
- Add new tests to cover all OpenRPC methods
- Update examples to demonstrate the new client
- Create new examples to showcase best practices
graph TD
A[Update Unit Tests] --> B[Update Integration Tests]
B --> C[Update Examples]
C --> D[Create New Examples]
Phase 4: Documentation and Cleanup
- Update the README with comprehensive documentation
- Add detailed API documentation for all methods
- Add usage examples for common scenarios
- Mark old implementations as deprecated (with a migration guide)
graph TD
A[Update README] --> B[Add API Documentation]
B --> C[Add Usage Examples]
C --> D[Mark Deprecated Code]
Detailed Implementation Steps
1. Create New OpenRPC Client (zinit_client.v)
module zinit
import freeflowuniverse.herolib.schemas.jsonrpc
import json
// ZinitClient is a unified client for interacting with Zinit using OpenRPC
pub struct ZinitClient {
pub mut:
rpc_client &jsonrpc.Client
}
// new_client creates a new Zinit OpenRPC client
pub fn new_client(socket_path string) ZinitClient {
mut cl := jsonrpc.new_unix_socket_client(socket_path)
return ZinitClient{
rpc_client: cl
}
}
// Implement all OpenRPC methods...
2. Refactor ZinitStateless (zinit_stateless.v)
module zinit
import freeflowuniverse.herolib.core.pathlib
@[params]
pub struct ZinitConfig {
path string = '/etc/zinit'
pathcmds string = '/etc/zinit/cmds'
socket_path string = default_socket_path
}
pub struct ZinitStateless {
pub mut:
client ZinitClient
path pathlib.Path
pathcmds pathlib.Path
}
pub fn new_stateless(z ZinitConfig) !ZinitStateless {
return ZinitStateless{
client: new_client(z.socket_path)
path: pathlib.get_dir(path: z.path, create: true)!
pathcmds: pathlib.get_dir(path: z.pathcmds, create: true)!
}
}
// Refactor methods to use the OpenRPC client...
3. Refactor Zinit (zinit.v)
module zinit
import freeflowuniverse.herolib.core.pathlib
@[heap]
pub struct Zinit {
pub mut:
processes map[string]ZProcess
path pathlib.Path
pathcmds pathlib.Path
client ZinitClient
}
// Refactor methods to use the OpenRPC client...
4. Refactor ZProcess (zprocess.v)
module zinit
pub struct ZProcess {
pub:
name string = 'default'
pub mut:
cmd string
cmd_stop string
cmd_test string
workdir string
status ZProcessStatus
pid int
after []string
env map[string]string
oneshot bool
start bool = true
restart bool = true
description string
client &ZinitClient
}
// Refactor methods to use the OpenRPC client...
Key Changes Required
-
Replace Direct Filesystem Operations:
- Replace file creation/modification with
service_createOpenRPC calls - Replace file deletion with
service_deleteOpenRPC calls - Replace file reading with
service_getOpenRPC calls
- Replace file creation/modification with
-
Replace Shell Commands:
- Replace
zinit listshell commands withservice_listOpenRPC calls - Replace
zinit statusshell commands withservice_statusOpenRPC calls - Replace
zinit logshell commands withstream_currentLogsOpenRPC calls
- Replace
-
Unify Error Handling:
- Implement consistent error handling across all methods
- Properly propagate OpenRPC error responses to the caller
-
Maintain Backward Compatibility:
- Keep the same method signatures for public methods
- Ensure the same behavior for all methods
- Add deprecation notices for methods that will be removed in the future
Testing Strategy
-
Unit Tests:
- Test each OpenRPC method individually
- Test error handling for each method
- Test with mock responses for predictable testing
-
Integration Tests:
- Test with a real Zinit instance
- Test the full lifecycle of services (create, start, status, stop, delete)
- Test edge cases and error conditions
-
Backward Compatibility Tests:
- Test existing code that uses the old implementations
- Ensure no regressions in functionality
Documentation Updates
-
README.md:
- Update with comprehensive documentation
- Add examples for common use cases
- Add migration guide for users of the old implementations
-
API Documentation:
- Document all public methods
- Document all structs and their fields
- Document error conditions and how to handle them
-
Examples:
- Update existing examples
- Add new examples for common use cases
- Add examples for error handling