File size: 8,601 Bytes
8f05ad1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
DevOps Tools Module

Provides cloud and DevOps operation capabilities.
"""

from typing import Dict, List, Optional, Any
import re


class DevOpsTools:
    """Cloud and DevOps operation tools."""

    # Cloud provider templates
    CLOUD_TEMPLATES = {
        "aws": {
            "ec2": {
                "description": "AWS EC2 instance",
                "template": """# AWS EC2 Instance
resource "aws_instance" "app_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"

  tags = {
    Name = "Stack2.9-App"
  }
}"""
            },
            "s3": {
                "description": "AWS S3 bucket",
                "template": """# AWS S3 Bucket
resource "aws_s3_bucket" "data_store" {
  bucket = "stack29-data-store"

  tags = {
    Name        = "Stack2.9 Data"
    Environment = "production"
  }
}"""
            },
            "lambda": {
                "description": "AWS Lambda function",
                "template": """# AWS Lambda Function
resource "aws_lambda_function" "handler" {
  filename         = "handler.zip"
  function_name    = "stack29_handler"
  role             = aws_iam_role.lambda_role.arn
  handler          = "index.handler"
  source_code_hash = filebase64sha256("handler.zip")

  runtime = "python3.9"
}"""
            },
        },
        "gcp": {
            "compute": {
                "description": "GCP Compute Engine",
                "template": """# GCP Compute Engine
resource "google_compute_instance" "vm_instance" {
  name         = "stack29-vm"
  machine_type = "e2-micro"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }

  network_interface {
    network = "default"
  }
}"""
            },
            "storage": {
                "description": "GCP Cloud Storage",
                "template": """# GCP Cloud Storage
resource "google_storage_bucket" "bucket" {
  name          = "stack29-bucket"
  location      = "US"
  force_destroy = false

  labels = {
    environment = "production"
  }
}"""
            },
        },
        "docker": {
            "container": {
                "description": "Docker container configuration",
                "template": """# Dockerfile
FROM python:3.11-slim

WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application
COPY . .

# Run application
CMD ["python", "main.py"]"""
            },
            "compose": {
                "description": "Docker Compose configuration",
                "template": """# docker-compose.yml
version: '3.8'

services:
  app:
    build: .
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgres://db:5432/app
    depends_on:
      - db
      - redis

  db:
    image: postgres:15
    environment:
      - POSTGRES_DB=app
      - POSTGRES_PASSWORD=secret

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
"""
            },
        },
        "kubernetes": {
            "deployment": {
                "description": "Kubernetes Deployment",
                "template": """# k8s-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: stack29-app
  labels:
    app: stack29
spec:
  replicas: 3
  selector:
    matchLabels:
      app: stack29
  template:
    metadata:
      labels:
        app: stack29
    spec:
      containers:
      - name: app
        image: stack29:latest
        ports:
        - containerPort: 8000
        resources:
          limits:
            cpu: "500m"
            memory: "256Mi"
"""
            },
            "service": {
                "description": "Kubernetes Service",
                "template": """# k8s-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: stack29-service
spec:
  selector:
    app: stack29
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8000
  type: LoadBalancer
"""
            },
        },
    }

    # CI/CD templates
    CICD_TEMPLATES = {
        "github_actions": {
            "description": "GitHub Actions workflow",
            "template": """# .github/workflows/ci.yml
name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.11'

    - name: Install dependencies
      run: |
        pip install -r requirements.txt

    - name: Run tests
      run: |
        pytest tests/

    - name: Lint
      run: |
        ruff check .
"""
        },
        "gitlab_ci": {
            "description": "GitLab CI pipeline",
            "template": """# .gitlab-ci.yml
stages:
  - test
  - build
  - deploy

test:
  stage: test
  script:
    - pip install -r requirements.txt
    - pytest tests/
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

build:
  stage: build
  script:
    - docker build -t stack29:$CI_COMMIT_SHA .
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

deploy:
  stage: deploy
  script:
    - kubectl apply -f k8s/
  environment:
    name: production
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
"""
        },
    }

    # Infrastructure as Code templates
    TERRAFORM_VARIABLES = {
        "description": "Terraform variables",
        "template": """# variables.tf
variable "region" {
  description = "AWS region"
  type        = string
  default     = "us-east-1"
}

variable "environment" {
  description = "Environment name"
  type        = string
  default     = "production"
}

variable "instance_type" {
  description = "EC2 instance type"
  type        = string
  default     = "t3.micro"
}
"""
    }

    def __init__(self):
        """Initialize DevOps tools."""
        pass

    def get_cloud_template(
        self,
        provider: str,
        service: str,
    ) -> Optional[str]:
        """Get a cloud infrastructure template."""
        return self.CLOUD_TEMPLATES.get(provider, {}).get(service, {}).get("template")

    def get_cicd_template(self, platform: str) -> Optional[str]:
        """Get a CI/CD pipeline template."""
        return self.CICD_TEMPLATES.get(platform, {}).get("template")

    def list_available_templates(self) -> Dict[str, List[str]]:
        """List all available templates."""
        return {
            "cloud_providers": list(self.CLOUD_TEMPLATES.keys()),
            "cicd": list(self.CICD_TEMPLATES.keys()),
        }

    def generate_kubernetes_manifest(
        self,
        app_name: str,
        image: str,
        replicas: int = 3,
        port: int = 8000,
    ) -> str:
        """Generate a Kubernetes deployment manifest."""
        return f"""apiVersion: apps/v1
kind: Deployment
metadata:
  name: {app_name}
  labels:
    app: {app_name}
spec:
  replicas: {replicas}
  selector:
    matchLabels:
      app: {app_name}
  template:
    metadata:
      labels:
        app: {app_name}
    spec:
      containers:
      - name: {app_name}
        image: {image}
        ports:
        - containerPort: {port}
        resources:
          limits:
            cpu: "1000m"
            memory: "512Mi"
          requests:
            cpu: "100m"
            memory: "128Mi"
---

apiVersion: v1
kind: Service
metadata:
  name: {app_name}-service
spec:
  selector:
    app: {app_name}
  ports:
  - protocol: TCP
    port: 80
    targetPort: {port}
  type: LoadBalancer
"""

    def generate_dockerfile(
        self,
        language: str = "python",
        version: str = "3.11",
        port: int = 8000,
    ) -> str:
        """Generate a Dockerfile."""
        base_images = {
            "python": f"python:{version}-slim",
            "node": f"node:{version}-slim",
            "go": f"golang:{version}",
            "rust": f"rust:{version}-slim",
        }
        base = base_images.get(language, f"python:{version}-slim")

        return f"""FROM {base}

WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application
COPY . .

# Expose port
EXPOSE {port}

# Run application
CMD ["python", "main.py"]
"""

    def parse_docker_compose(self, compose_content: str) -> Dict[str, Any]:
        """Parse docker-compose content to extract services."""
        services = re.findall(r'^  (\w+):$', compose_content, re.MULTILINE)
        return {"services": services, "count": len(services)}

    def __repr__(self) -> str:
        templates = self.list_available_templates()
        return f"DevOpsTools(cloud={templates['cloud_providers']}, cicd={templates['cicd']})"