lmrc-cli 0.3.16

CLI tool for scaffolding LMRC Stack infrastructure projects
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use colored::Colorize;
use lmrc_config_validator::LmrcConfig;
use std::fs;
use std::path::Path;

use crate::error::Result;

pub fn generate_docs(project_path: &Path, config: &LmrcConfig) -> Result<()> {
    let docs_path = project_path.join("docs");

    // Generate README.md
    generate_readme(&docs_path, config)?;

    // Generate SECRETS.md with required GitLab variables
    generate_secrets_doc(&docs_path, config)?;

    // Generate SETUP.md with setup instructions
    generate_setup_doc(&docs_path, config)?;

    println!("  {} docs/", "Created:".green());

    Ok(())
}

fn generate_readme(docs_path: &Path, config: &LmrcConfig) -> Result<()> {
    let readme = format!(
        r#"# {}

{}

## Project Structure

```
.
├── apps/          # Application code
├── libs/          # Shared libraries
├── infra/         # Infrastructure code
│   └── pipeline/  # Deployment pipeline binary
├── docker/        # Docker configurations
├── docs/          # Documentation
└── lmrc.toml      # Project configuration
```

## Applications

{}

## Quick Start

### Prerequisites

- Rust 1.75 or later
- Docker and docker-compose
- Access to configured cloud providers

### Local Development

1. Start local services:
   ```bash
   cd docker
   docker-compose up -d
   ```

2. Build the project:
   ```bash
   cargo build
   ```

3. Run an application:
   ```bash
   cargo run -p <app-name>
   ```

### Pipeline Commands

Build the pipeline binary:
```bash
cd infra/pipeline
cargo build --release
```

Run pipeline commands:
```bash
# Show configuration
./target/release/pipeline config

# Provision infrastructure
./target/release/pipeline provision

# Setup services (K8s, databases, etc.)
./target/release/pipeline setup

# Deploy applications
./target/release/pipeline deploy

# Run full pipeline
./target/release/pipeline full
```

## Infrastructure Stack

- **Server Provider**: {}
- **Kubernetes**: {}
- **Database**: {}
- **DNS**: {}
- **CI/CD**: {}

## Configuration

The project configuration is stored in `lmrc.toml`. See the file for all available options.

## GitLab CI/CD

The project includes a `.gitlab-ci.yml` file that:
1. Builds the pipeline binary
2. Builds and tests all applications
3. Provisions infrastructure (manual trigger)
4. Sets up services
5. Deploys applications

### Required GitLab Variables

See [SECRETS.md](./SECRETS.md) for the complete list of required CI/CD variables.

## Documentation

- [SECRETS.md](./SECRETS.md) - Required secrets and GitLab variables
- [SETUP.md](./SETUP.md) - Detailed setup instructions

## License

Apache-2.0
"#,
        config.project.name,
        config.project.description,
        config
            .apps
            .applications
            .iter()
            .map(|app| {
                let app_type_desc = app
                    .app_type
                    .as_ref()
                    .map(|t| t.display_name())
                    .unwrap_or("Basic");
                format!("- **{}**: {}", app.name, app_type_desc)
            })
            .collect::<Vec<_>>()
            .join("\n"),
        config.providers.server,
        config.providers.kubernetes,
        config.providers.database,
        config.providers.dns,
        config.providers.git
    );

    fs::write(docs_path.join("README.md"), readme)?;

    Ok(())
}

fn generate_secrets_doc(docs_path: &Path, config: &LmrcConfig) -> Result<()> {
    let mut secrets = vec![
        "# Required Secrets and Variables\n".to_string(),
        "This document lists all required secrets and CI/CD variables that must be configured in GitLab.\n".to_string(),
        "## GitLab Configuration\n".to_string(),
        format!(
            "Navigate to: `{}/{} > Settings > CI/CD > Variables`\n",
            config
                .infrastructure
                .gitlab
                .as_ref()
                .map(|g| g.url.as_str())
                .unwrap_or("https://gitlab.com"),
            config.project.name
        ),
    ];

    // Server provider secrets
    if config.providers.server == "hetzner" {
        secrets.push("\n## Hetzner Cloud\n".to_string());
        secrets.push("| Variable Name | Type | Description | Protected | Masked |\n".to_string());
        secrets.push("|---------------|------|-------------|-----------|--------|\n".to_string());
        secrets.push(
            "| `HETZNER_API_TOKEN` | Variable | Hetzner Cloud API token | Yes | Yes |\n"
                .to_string(),
        );
        secrets.push("\n**How to get:**\n".to_string());
        secrets.push("1. Log in to Hetzner Cloud Console\n".to_string());
        secrets.push("2. Go to your project\n".to_string());
        secrets.push("3. Navigate to Security > API Tokens\n".to_string());
        secrets.push("4. Generate a new token with Read & Write permissions\n".to_string());
    }

    // Database secrets
    if config.providers.database == "postgres" {
        secrets.push("\n## PostgreSQL\n".to_string());
        secrets.push("| Variable Name | Type | Description | Protected | Masked |\n".to_string());
        secrets.push("|---------------|------|-------------|-----------|--------|\n".to_string());
        secrets.push(
            "| `POSTGRES_PASSWORD` | Variable | PostgreSQL admin password | Yes | Yes |\n"
                .to_string(),
        );
        secrets.push(
            "| `DATABASE_URL` | Variable | Database connection string | Yes | Yes |\n".to_string(),
        );
    }

    // DNS provider secrets
    if config.providers.dns == "cloudflare" {
        secrets.push("\n## Cloudflare\n".to_string());
        secrets.push("| Variable Name | Type | Description | Protected | Masked |\n".to_string());
        secrets.push("|---------------|------|-------------|-----------|--------|\n".to_string());
        secrets.push(
            "| `CLOUDFLARE_API_TOKEN` | Variable | Cloudflare API token | Yes | Yes |\n"
                .to_string(),
        );
        secrets.push(
            "| `CLOUDFLARE_ZONE_ID` | Variable | Cloudflare Zone ID | No | No |\n".to_string(),
        );
        secrets.push("\n**How to get:**\n".to_string());
        secrets.push("1. Log in to Cloudflare Dashboard\n".to_string());
        secrets.push("2. Go to My Profile > API Tokens\n".to_string());
        secrets.push("3. Create token with Zone:DNS:Edit permissions\n".to_string());
        secrets.push("4. Get Zone ID from your domain's Overview page\n".to_string());
    }

    // Kubernetes secrets
    if config.providers.kubernetes == "k3s" {
        secrets.push(format!("\n## {} Cluster\n", config.providers.kubernetes));
        secrets.push("| Variable Name | Type | Description | Protected | Masked |\n".to_string());
        secrets.push("|---------------|------|-------------|-----------|--------|\n".to_string());
        secrets.push("| `KUBECONFIG` | File | Kubernetes config file | Yes | No |\n".to_string());
        secrets
            .push("| `K8S_NAMESPACE` | Variable | Kubernetes namespace | No | No |\n".to_string());
    }

    // SSH access
    secrets.push("\n## SSH Access\n".to_string());
    secrets.push("| Variable Name | Type | Description | Protected | Masked |\n".to_string());
    secrets.push("|---------------|------|-------------|-----------|--------|\n".to_string());
    secrets.push(
        "| `SSH_PRIVATE_KEY` | File | SSH private key for server access | Yes | Yes |\n"
            .to_string(),
    );
    secrets.push("| `SSH_KNOWN_HOSTS` | File | SSH known hosts file | No | No |\n".to_string());
    secrets.push("\n**How to set up:**\n".to_string());
    secrets.push("1. Create SSH keys in your project's `.ssh/` directory (see SETUP.md)\n".to_string());
    secrets.push("2. Upload the contents of `.ssh/id_rsa` as `SSH_PRIVATE_KEY` (type: File)\n".to_string());
    secrets.push("3. The pipeline will use this key to access provisioned servers\n".to_string());
    secrets.push("\n**Note:** The `.ssh/` directory is git-ignored and never committed to the repository.\n".to_string());

    // Container registry
    secrets.push("\n## Container Registry\n".to_string());
    secrets.push("| Variable Name | Type | Description | Protected | Masked |\n".to_string());
    secrets.push("|---------------|------|-------------|-----------|--------|\n".to_string());
    secrets.push(
        "| `CI_REGISTRY_USER` | Variable | Container registry username | No | No |\n".to_string(),
    );
    secrets.push(
        "| `CI_REGISTRY_PASSWORD` | Variable | Container registry password | Yes | Yes |\n"
            .to_string(),
    );
    secrets.push("\n**Note:** GitLab provides built-in registry variables. Use them if using GitLab Container Registry.\n".to_string());

    // Summary
    secrets.push("\n## Quick Setup Script\n".to_string());
    secrets.push("You can use the GitLab API to set variables programmatically:\n\n".to_string());
    secrets.push("```bash\n".to_string());
    secrets.push("#!/bin/bash\n".to_string());
    secrets.push("GITLAB_TOKEN=\"your-gitlab-token\"\n".to_string());
    secrets.push(format!("PROJECT_ID=\"{}\"\n", config.project.name));
    secrets.push(format!(
        "GITLAB_URL=\"{}\"\n\n",
        config
            .infrastructure
            .gitlab
            .as_ref()
            .map(|g| g.url.as_str())
            .unwrap_or("https://gitlab.com")
    ));
    secrets.push("# Example: Set Hetzner API token\n".to_string());
    secrets.push("curl --request POST --header \"PRIVATE-TOKEN: $GITLAB_TOKEN\" \\\n".to_string());
    secrets.push("  \"$GITLAB_URL/api/v4/projects/$PROJECT_ID/variables\" \\\n".to_string());
    secrets.push("  --form \"key=HETZNER_API_TOKEN\" \\\n".to_string());
    secrets.push("  --form \"value=your-token-here\" \\\n".to_string());
    secrets.push("  --form \"protected=true\" \\\n".to_string());
    secrets.push("  --form \"masked=true\"\n".to_string());
    secrets.push("```\n".to_string());

    fs::write(docs_path.join("SECRETS.md"), secrets.join(""))?;

    Ok(())
}

fn generate_setup_doc(docs_path: &Path, config: &LmrcConfig) -> Result<()> {
    let setup = format!(
        r#"# Setup Guide

This guide walks you through setting up the {} infrastructure project.

## Prerequisites

### Development Machine

- Rust 1.75 or later
- Docker and docker-compose
- Git
- kubectl (for Kubernetes management)

### Cloud Accounts

{}

## Step 1: Clone and Configure

1. Clone the repository:
   ```bash
   git clone <repository-url>
   cd {}
   ```

2. Review and update `lmrc.toml` if needed

3. Verify the configuration:
   ```bash
   cd infra/pipeline
   cargo run -- config
   ```

## Step 2: Set Up SSH Keys

The project uses SSH keys for secure server access. These keys are stored locally in the project directory and are NOT committed to version control.

1. Create the SSH directory in your project:
   ```bash
   mkdir -p .ssh
   chmod 700 .ssh
   ```

2. Generate an SSH key pair (or copy your existing keys):
   ```bash
   # Generate new key
   ssh-keygen -t rsa -b 4096 -f .ssh/id_rsa -N ""

   # Or copy existing keys
   cp ~/.ssh/id_rsa .ssh/id_rsa
   cp ~/.ssh/id_rsa.pub .ssh/id_rsa.pub
   ```

3. Set correct permissions:
   ```bash
   chmod 600 .ssh/id_rsa
   chmod 644 .ssh/id_rsa.pub
   ```

4. Add the public key to your servers:
   ```bash
   # Copy the public key
   cat .ssh/id_rsa.pub

   # Then add it to ~/.ssh/authorized_keys on each server
   ```

**Important Notes:**
- The `.ssh/` directory is in `.gitignore` and will never be committed
- You can override the default SSH key path using the `SSH_KEY_PATH` environment variable
- For CI/CD, use GitLab secrets to store the private key (see SECRETS.md)

## Step 3: Configure GitLab CI/CD Variables

Follow the instructions in [SECRETS.md](./SECRETS.md) to configure all required secrets and variables in GitLab.

## Step 4: Local Development Setup

1. Start local services:
   ```bash
   cd docker
   docker-compose up -d
   ```

2. Build all applications:
   ```bash
   cargo build
   ```

3. Run tests:
   ```bash
   cargo test
   ```

## Step 5: Infrastructure Provisioning

### Option A: Using GitLab CI/CD (Recommended)

1. Push your code to GitLab
2. Go to CI/CD > Pipelines
3. Manually trigger the `provision` job
4. The pipeline will automatically run setup and deploy after provisioning

### Option B: Manual Provisioning

1. Build the pipeline binary:
   ```bash
   cd infra/pipeline
   cargo build --release
   ```

2. Run provisioning:
   ```bash
   ./target/release/pipeline provision
   ```

3. Run setup:
   ```bash
   ./target/release/pipeline setup
   ```

4. Deploy applications:
   ```bash
   ./target/release/pipeline deploy
   ```

## Step 6: Verify Deployment

{}

## Troubleshooting

### Pipeline fails to build
- Check that all required GitLab variables are set
- Verify Rust version is 1.75 or later

### Provisioning fails
- Verify cloud provider credentials are correct
- Check that your account has sufficient permissions
- Review the logs for specific error messages

### Deployment fails
- Ensure Kubernetes cluster is accessible
- Verify kubeconfig is correctly configured
- Check that Docker images built successfully

## Next Steps

- Set up monitoring and logging
- Configure backups
- Set up alerting
- Review security settings

## Support

For issues and questions, please refer to the project documentation or create an issue in the repository.
"#,
        config.project.name,
        generate_cloud_accounts_section(config),
        config.project.name,
        generate_verification_section(config)
    );

    fs::write(docs_path.join("SETUP.md"), setup)?;

    Ok(())
}

fn generate_cloud_accounts_section(config: &LmrcConfig) -> String {
    let mut accounts = Vec::new();

    if config.providers.server == "hetzner" {
        accounts.push("- Hetzner Cloud account with billing enabled");
    }

    if config.providers.dns == "cloudflare" {
        accounts.push("- Cloudflare account with domain registered");
    }

    if config.providers.git == "gitlab" {
        accounts.push("- GitLab account with project created");
    }

    accounts.join("\n")
}

fn generate_verification_section(config: &LmrcConfig) -> String {
    let mut checks = Vec::new();

    if config.providers.kubernetes == "k3s" || config.providers.kubernetes == "kubernetes" {
        checks.push(
            r#"1. Check cluster status:
   ```bash
   kubectl get nodes
   kubectl get pods --all-namespaces
   ```
"#,
        );
    }

    if config.providers.database == "postgres" {
        checks.push(
            r#"2. Verify database connection:
   ```bash
   psql $DATABASE_URL -c "SELECT version();"
   ```
"#,
        );
    }

    checks.push(
        r#"3. Check application endpoints:
   ```bash
   curl https://your-domain.com/health
   ```
"#,
    );

    checks.join("\n")
}