|
| 1 | +# X2Ansible Backstage Plugin |
| 2 | + |
| 3 | +This is a Backstage plugin workspace providing web UI for the [X2Ansible](https://github.com/x2ansible/x2a-convertor) project. |
| 4 | + |
| 5 | +## Development Environment Setup |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +- Node.js >20 |
| 10 | +- Yarn package manager |
| 11 | +- Kubernetes cluster access (optional, for Kubernetes features) |
| 12 | + |
| 13 | +### Installation |
| 14 | + |
| 15 | +1. Install dependencies: |
| 16 | + |
| 17 | + ```sh |
| 18 | + yarn install |
| 19 | + ``` |
| 20 | + |
| 21 | +2. Start the development environment: |
| 22 | + |
| 23 | + ```sh |
| 24 | + yarn dev |
| 25 | + ``` |
| 26 | + |
| 27 | + This command runs both the frontend and backend plugins in parallel. The frontend will be available at `http://localhost:3000` and the backend at `http://localhost:7007`. |
| 28 | + |
| 29 | +## Adding New API Endpoints |
| 30 | + |
| 31 | +To add a new API endpoint, follow these steps: |
| 32 | + |
| 33 | +### 1. Update the OpenAPI Specification |
| 34 | + |
| 35 | +Edit `plugins/x2a-backend/src/schema/openapi.yaml` to add your new endpoint definition. For example: |
| 36 | + |
| 37 | +```yaml |
| 38 | +paths: |
| 39 | + /projects/{projectId}/status: |
| 40 | + get: |
| 41 | + summary: Returns the status of a project. |
| 42 | + parameters: |
| 43 | + - in: path |
| 44 | + name: projectId |
| 45 | + schema: |
| 46 | + type: string |
| 47 | + required: true |
| 48 | + responses: |
| 49 | + '200': |
| 50 | + description: Project status. |
| 51 | + content: |
| 52 | + application/json: |
| 53 | + schema: |
| 54 | + type: object |
| 55 | + properties: |
| 56 | + status: |
| 57 | + type: string |
| 58 | +``` |
| 59 | +
|
| 60 | +### 2. Regenerate the API Code |
| 61 | +
|
| 62 | +After updating `openapi.yaml`, regenerate the server and client API code: |
| 63 | + |
| 64 | +```sh |
| 65 | +yarn workspace @red-hat-developer-hub/backstage-plugin-x2a-backend openapi-generate |
| 66 | +``` |
| 67 | + |
| 68 | +This command: |
| 69 | + |
| 70 | +- Generates TypeScript types and server-side code in `plugins/x2a-backend/src/schema/openapi/generated/` |
| 71 | +- Generates client-side code in `plugins/x2a-common/client/src/schema/openapi/generated/` |
| 72 | + |
| 73 | +### 3. Implement the Endpoint Handler |
| 74 | + |
| 75 | +**Note:** Always regenerate the API code before implementing new endpoints to ensure TypeScript types are up to date. |
| 76 | + |
| 77 | +Add your endpoint implementation in `plugins/x2a-backend/src/router.ts`. Import the generated types and implement the handler: |
| 78 | + |
| 79 | +```typescript |
| 80 | +import { ProjectStatusGet } from './schema/openapi'; |
| 81 | +
|
| 82 | +router.get('/projects/:projectId/status', async (req, res) => { |
| 83 | + const endpoint = 'GET /projects/:projectId/status'; |
| 84 | + const projectId = req.params.projectId; |
| 85 | +
|
| 86 | + // Your implementation here |
| 87 | + const status = await x2aDatabase.getProjectStatus({ projectId }); |
| 88 | +
|
| 89 | + const response: ProjectStatusGet['response'] = status; |
| 90 | + res.json(response); |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +## Database Configuration |
| 95 | + |
| 96 | +By default, the plugin uses SQLite with an in-memory database for local development. The database configuration is located in `app-config.yaml`: |
| 97 | + |
| 98 | +```yaml |
| 99 | +backend: |
| 100 | + database: |
| 101 | + client: better-sqlite3 |
| 102 | + connection: ':memory:' |
| 103 | +``` |
| 104 | + |
| 105 | +### Using a Plugin-Specific Database |
| 106 | + |
| 107 | +To configure a separate database for the x2a plugin: |
| 108 | + |
| 109 | +```yaml |
| 110 | +backend: |
| 111 | + database: |
| 112 | + client: better-sqlite3 |
| 113 | + connection: ':memory:' |
| 114 | + plugin: |
| 115 | + x2a: |
| 116 | + connection: |
| 117 | + filename: ./data/x2a.db |
| 118 | +``` |
| 119 | + |
| 120 | +### Using Other Database Backends |
| 121 | + |
| 122 | +The plugin uses Knex.js, which supports multiple database backends (PostgreSQL, MySQL, etc.). To use a different database: |
| 123 | + |
| 124 | +1. Install the appropriate database client package (e.g., `pg` for PostgreSQL) |
| 125 | +2. Update `app-config.yaml`: |
| 126 | + |
| 127 | +```yaml |
| 128 | +backend: |
| 129 | + database: |
| 130 | + client: pg |
| 131 | + connection: |
| 132 | + host: localhost |
| 133 | + port: 5432 |
| 134 | + user: postgres |
| 135 | + password: ${POSTGRES_PASSWORD} |
| 136 | + database: x2a |
| 137 | +``` |
| 138 | + |
| 139 | +### Database Migrations |
| 140 | + |
| 141 | +Database migrations are located in `plugins/x2a-backend/migrations/`. Migrations run automatically when the backend starts. To create a new migration: |
| 142 | + |
| 143 | +1. Create a new file in `plugins/x2a-backend/migrations/` following the naming pattern: `YYYYMMDDHH_description.ts` |
| 144 | +2. Implement `up()` and `down()` functions: |
| 145 | + |
| 146 | +```typescript |
| 147 | +import { Knex } from 'knex'; |
| 148 | +
|
| 149 | +export async function up(knex: Knex): Promise<void> { |
| 150 | + await knex.schema.createTable('new_table', table => { |
| 151 | + table.uuid('id').primary(); |
| 152 | + // ... other columns |
| 153 | + }); |
| 154 | +} |
| 155 | +
|
| 156 | +export async function down(knex: Knex): Promise<void> { |
| 157 | + await knex.schema.dropTable('new_table'); |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +## Kubernetes Connection Setup |
| 162 | + |
| 163 | +The plugin uses the Kubernetes client library to interact with Kubernetes clusters. |
| 164 | + |
| 165 | +By default, it loads configuration from your local `~/.kube/config` file. |
| 166 | + |
| 167 | +Additional methods are planed to be implemented later. |
| 168 | + |
| 169 | +### Local Development Setup |
| 170 | + |
| 171 | +1. Ensure you have a valid Kubernetes configuration file at `~/.kube/config` |
| 172 | +2. The plugin will automatically detect and use this configuration |
| 173 | + |
| 174 | +### Using a Custom Kubeconfig Location |
| 175 | + |
| 176 | +Set the `KUBECONFIG` environment variable to point to your kubeconfig file: |
| 177 | + |
| 178 | +```sh |
| 179 | +export KUBECONFIG=/path/to/your/kubeconfig |
| 180 | +yarn dev |
| 181 | +``` |
| 182 | + |
| 183 | +### In-Cluster Configuration |
| 184 | + |
| 185 | +When running inside a Kubernetes cluster, the plugin will automatically fall back to in-cluster configuration if no local kubeconfig is found. |
| 186 | + |
| 187 | +### Verifying Kubernetes Connection |
| 188 | + |
| 189 | +The plugin's `KubeService` provides methods to interact with Kubernetes resources. Check the logs when starting the backend to see if the Kubernetes configuration was loaded successfully: |
| 190 | + |
| 191 | +``` |
| 192 | +Loaded Kubernetes configuration from ~/.kube/config |
| 193 | +``` |
| 194 | + |
| 195 | +## Additional Commands |
| 196 | + |
| 197 | +- `yarn test` - Run tests |
| 198 | +- `yarn lint` - Run linter |
| 199 | +- `prettier:fix` - Fix by prettier |
| 200 | +- `yarn build:all` - Build all packages |
| 201 | +- `yarn clean` - Clean build artifacts |
| 202 | + |
| 203 | +## Project Structure |
| 204 | + |
| 205 | +- `plugins/x2a/` - Frontend plugin |
| 206 | +- `plugins/x2a-backend/` - Backend plugin |
| 207 | + - `src/schema/openapi.yaml` - OpenAPI specification |
| 208 | + - `src/schema/openapi/generated/` - Generated server-side API code |
| 209 | + - `src/router.ts` - API route handlers |
| 210 | + - `src/services/` - Business logic services |
| 211 | + - `migrations/` - Database migrations |
| 212 | +- `plugins/x2a-common/` - Shared code between frontend and backend |
| 213 | + - `client/src/schema/openapi/generated/` - Generated client-side API code |
0 commit comments