-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskfile.yaml
More file actions
4061 lines (3319 loc) · 142 KB
/
Taskfile.yaml
File metadata and controls
4061 lines (3319 loc) · 142 KB
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Runner Codes Taskfile
# https://taskfile.dev
version: '3'
vars:
FC_WORKDIR: '{{.FC_WORKDIR | default "/srv/firecracker"}}'
FC_VERSION: '{{.FC_VERSION | default "v1.7.0"}}'
GUEST_CID: '{{.GUEST_CID | default "3"}}'
VSOCK_PORT: '{{.VSOCK_PORT | default "5000"}}'
BIN_DIR: bin
ROOTFS_DIR: rootfs
# AWS Configuration
AWS_REGION: '{{.AWS_REGION | default "us-east-1"}}'
AWS_INSTANCE_TYPE: '{{.AWS_INSTANCE_TYPE | default "m5zn.metal"}}'
AWS_KEY_NAME: '{{.AWS_KEY_NAME | default "firesandbox-key"}}'
AWS_INSTANCE_NAME: '{{.AWS_INSTANCE_NAME | default "runner-codes-test"}}'
AWS_VPC_ID: '{{.AWS_VPC_ID | default "vpc-0dd1e4d3c5ed4f3c7"}}'
AWS_SUBNET_ID: '{{.AWS_SUBNET_ID | default "subnet-004de53997864f0a3"}}'
PROJECT_DIR: '{{.PROJECT_DIR | default "/Users/andrebassi/works/.solutions/fly.io/runner-codes"}}'
# S3 Configuration for rootfs storage
S3_BUCKET: '{{.S3_BUCKET | default "runner-codes-rootfs"}}'
S3_REGION: '{{.S3_REGION | default "us-east-1"}}'
tasks:
# ===========================================================================
# Build Tasks
# ===========================================================================
default:
desc: Show available tasks
cmds:
- task --list
build:all:
desc: Build all components
cmds:
- task: build:host-agent
- task: build:guest-runner
- task: build:proxy
silent: false
build:host-agent:
desc: Build host-agent for current OS
cmds:
- mkdir -p {{.BIN_DIR}}
- cd host-agent && go build -ldflags="-s -w" -o ../{{.BIN_DIR}}/host-agent .
- echo "Built {{.BIN_DIR}}/host-agent"
build:guest-runner:
desc: Build guest-runner for Linux (auto-detect arch)
vars:
GOARCH:
sh: |
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then echo "amd64"
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then echo "arm64"
else echo "amd64"; fi
cmds:
- mkdir -p {{.ROOTFS_DIR}}
- cd guest-runner && GOOS=linux GOARCH={{.GOARCH}} go build -ldflags="-s -w" -o ../{{.ROOTFS_DIR}}/guest-runner .
- echo "Built {{.ROOTFS_DIR}}/guest-runner ({{.GOARCH}})"
build:guest-runner-amd64:
desc: Build guest-runner for Linux/amd64
cmds:
- mkdir -p {{.ROOTFS_DIR}}
- cd guest-runner && GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ../{{.ROOTFS_DIR}}/guest-runner-amd64 .
- echo "Built {{.ROOTFS_DIR}}/guest-runner-amd64"
build:guest-runner-arm64:
desc: Build guest-runner for Linux/arm64
cmds:
- mkdir -p {{.ROOTFS_DIR}}
- cd guest-runner && GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o ../{{.ROOTFS_DIR}}/guest-runner-arm64 .
- echo "Built {{.ROOTFS_DIR}}/guest-runner-arm64"
build:proxy:
desc: Build vsock-proxy
cmds:
- mkdir -p {{.BIN_DIR}}
- cd proxy && go build -ldflags="-s -w" -o ../{{.BIN_DIR}}/vsock-proxy .
- echo "Built {{.BIN_DIR}}/vsock-proxy"
# ===========================================================================
# Setup Tasks
# ===========================================================================
setup:firecracker:
desc: Setup Firecracker on Ubuntu/Zorin (requires sudo)
cmds:
- sudo ./setup/setup-firecracker-ubuntu.sh
preconditions:
- test -f ./setup/setup-firecracker-ubuntu.sh
setup:deps:
desc: Download Go dependencies
cmds:
- cd host-agent && go mod download
- cd guest-runner && go mod download
- cd proxy && go mod download
- echo "Dependencies downloaded"
setup:check:
desc: Check if host is ready for Firecracker
cmds:
- |
echo "Checking Firecracker prerequisites..."
echo ""
echo "KVM:"
if [ -e /dev/kvm ]; then
echo " /dev/kvm: OK"
else
echo " /dev/kvm: NOT FOUND (run: sudo modprobe kvm)"
fi
echo ""
echo "vhost_vsock:"
if [ -e /dev/vhost_vsock ]; then
echo " /dev/vhost_vsock: OK"
else
echo " /dev/vhost_vsock: NOT FOUND (run: sudo modprobe vhost_vsock)"
fi
echo ""
echo "Firecracker binary:"
if command -v firecracker &> /dev/null; then
echo " firecracker: $(which firecracker)"
firecracker --version
else
echo " firecracker: NOT FOUND"
fi
# ===========================================================================
# Rootfs Tasks
# ===========================================================================
rootfs:prepare:
desc: Prepare rootfs with guest-runner (requires sudo)
deps:
- build:guest-runner
cmds:
- |
if [ ! -f {{.FC_WORKDIR}}/rootfs.ext4 ]; then
echo "Rootfs not found. Run 'task setup:firecracker' first."
exit 1
fi
echo "Mounting rootfs..."
sudo mount -o loop {{.FC_WORKDIR}}/rootfs.ext4 /mnt
echo "Copying guest-runner..."
sudo cp {{.ROOTFS_DIR}}/guest-runner /mnt/usr/local/bin/guest-runner
sudo chmod +x /mnt/usr/local/bin/guest-runner
echo "Unmounting..."
sudo umount /mnt
echo "Rootfs prepared with guest-runner"
rootfs:mount:
desc: Mount rootfs for inspection
cmds:
- sudo mount -o loop {{.FC_WORKDIR}}/rootfs.ext4 /mnt
- echo "Rootfs mounted at /mnt"
- echo "Run 'task rootfs:umount' when done"
rootfs:umount:
desc: Unmount rootfs
cmds:
- sudo umount /mnt
- echo "Rootfs unmounted"
# ===========================================================================
# Run Tasks
# ===========================================================================
run:test:
desc: Run a test execution (requires Firecracker running)
deps:
- build:host-agent
cmds:
- |
sudo ./{{.BIN_DIR}}/host-agent \
-kernel {{.FC_WORKDIR}}/vmlinux \
-rootfs {{.FC_WORKDIR}}/rootfs.ext4 \
-lang python \
-timeout 10
run:host-agent:
desc: Run host-agent with custom parameters
deps:
- build:host-agent
cmds:
- |
sudo ./{{.BIN_DIR}}/host-agent \
-kernel {{.KERNEL | default (printf "%s/vmlinux" .FC_WORKDIR)}} \
-rootfs {{.ROOTFS | default (printf "%s/rootfs.ext4" .FC_WORKDIR)}} \
-lang {{.LANG | default "python"}} \
-code '{{.CODE | default ""}}' \
-timeout {{.TIMEOUT | default "10"}} \
-cid {{.GUEST_CID}} \
-port {{.VSOCK_PORT}}
run:proxy:
desc: Run vsock-proxy
deps:
- build:proxy
cmds:
- |
sudo ./{{.BIN_DIR}}/vsock-proxy \
-socket /tmp/fc-proxy.sock \
-cid {{.GUEST_CID}} \
-port {{.VSOCK_PORT}}
# ===========================================================================
# Development Tasks
# ===========================================================================
dev:tidy:
desc: Tidy Go modules
cmds:
- cd host-agent && go mod tidy
- cd guest-runner && go mod tidy
- cd proxy && go mod tidy
dev:fmt:
desc: Format Go code
cmds:
- cd host-agent && go fmt ./...
- cd guest-runner && go fmt ./...
- cd proxy && go fmt ./...
dev:lint:
desc: Lint Go code
cmds:
- cd host-agent && go vet ./...
- cd guest-runner && go vet ./...
- cd proxy && go vet ./...
test:unit:
desc: Run unit tests for all packages
cmds:
- echo "Running unit tests..."
- go test -v ./pkg/host/...
- go test -v ./pkg/guest/...
- echo "All tests passed!"
test:coverage:
desc: Run tests with coverage report
cmds:
- echo "Running tests with coverage..."
- go test -coverprofile=coverage.out ./pkg/...
- go tool cover -func=coverage.out
- echo "Coverage report generated: coverage.out"
test:coverage-html:
desc: Generate HTML coverage report
cmds:
- echo "Generating HTML coverage report..."
- go test -coverprofile=coverage.out ./pkg/...
- go tool cover -html=coverage.out -o coverage.html
- echo "HTML report generated: coverage.html"
- |
if command -v open &> /dev/null; then
open coverage.html
elif command -v xdg-open &> /dev/null; then
xdg-open coverage.html
fi
test:all:
desc: Run all tests (unit + coverage)
cmds:
- task: lint
- task: test:unit
- task: test:coverage
- echo "All tests and coverage completed!"
test:short:
desc: Run quick tests (skip integration)
cmds:
- go test -short ./pkg/...
test:race:
desc: Run tests with race detector
cmds:
- go test -race ./pkg/...
# ===========================================================================
# Clean Tasks
# ===========================================================================
clean:
desc: Clean built binaries
cmds:
- rm -rf {{.BIN_DIR}}
- rm -f {{.ROOTFS_DIR}}/guest-runner
- rm -f {{.ROOTFS_DIR}}/guest-runner-amd64
- rm -f {{.ROOTFS_DIR}}/guest-runner-arm64
- echo "Cleaned"
clean:all:
desc: Clean everything including Go cache
cmds:
- task: clean
- go clean -cache
- echo "All cleaned"
# ===========================================================================
# AWS Infrastructure Tasks
# ===========================================================================
aws:check-creds:
desc: Check AWS credentials are configured
cmds:
- |
echo "Checking AWS credentials..."
aws sts get-caller-identity || {
echo "ERROR: AWS credentials not configured"
echo "Run: export AWS_ACCESS_KEY_ID=xxx && export AWS_SECRET_ACCESS_KEY=xxx"
exit 1
}
aws:create-keypair:
desc: Create SSH key pair for EC2 access
cmds:
- |
echo "Creating SSH key pair..."
mkdir -p {{.PROJECT_DIR}}/aws/keys
if aws ec2 describe-key-pairs --key-names {{.AWS_KEY_NAME}} --region {{.AWS_REGION}} 2>/dev/null; then
echo "Key pair {{.AWS_KEY_NAME}} already exists"
else
aws ec2 create-key-pair \
--key-name {{.AWS_KEY_NAME}} \
--region {{.AWS_REGION}} \
--query 'KeyMaterial' \
--output text > {{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem
chmod 400 {{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem
echo "Key pair created: {{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem"
fi
aws:create-security-group:
desc: Create security group for Firecracker test instance
cmds:
- |
echo "Creating security group..."
SG_ID=$(aws ec2 describe-security-groups \
--filters "Name=group-name,Values=firesandbox-sg" "Name=vpc-id,Values={{.AWS_VPC_ID}}" \
--region {{.AWS_REGION}} \
--query 'SecurityGroups[0].GroupId' \
--output text 2>/dev/null)
if [ "$SG_ID" != "None" ] && [ -n "$SG_ID" ]; then
echo "Security group already exists: $SG_ID"
else
SG_ID=$(aws ec2 create-security-group \
--group-name firesandbox-sg \
--description "Runner Codes test security group" \
--vpc-id {{.AWS_VPC_ID}} \
--region {{.AWS_REGION}} \
--query 'GroupId' \
--output text)
# Allow SSH
aws ec2 authorize-security-group-ingress \
--group-id $SG_ID \
--protocol tcp \
--port 22 \
--cidr 0.0.0.0/0 \
--region {{.AWS_REGION}}
echo "Security group created: $SG_ID"
fi
echo $SG_ID > {{.PROJECT_DIR}}/aws/.sg-id
aws:get-ami:
desc: Get latest Ubuntu 22.04 AMI ID
cmds:
- |
echo "Finding Ubuntu 22.04 AMI..."
AMI_ID=$(aws ec2 describe-images \
--owners 099720109477 \
--filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*" \
"Name=state,Values=available" \
--query 'Images | sort_by(@, &CreationDate) | [-1].ImageId' \
--region {{.AWS_REGION}} \
--output text)
echo "Ubuntu 22.04 AMI: $AMI_ID"
echo $AMI_ID > {{.PROJECT_DIR}}/aws/.ami-id
aws:launch:
desc: Launch EC2 instance for Firecracker testing
deps:
- aws:check-creds
- aws:create-keypair
- aws:create-security-group
- aws:get-ami
cmds:
- |
echo "Launching EC2 instance..."
AMI_ID=$(cat {{.PROJECT_DIR}}/aws/.ami-id)
SG_ID=$(cat {{.PROJECT_DIR}}/aws/.sg-id)
# Check if instance already exists
EXISTING=$(aws ec2 describe-instances \
--filters "Name=tag:Name,Values={{.AWS_INSTANCE_NAME}}" \
"Name=instance-state-name,Values=running,pending" \
--region {{.AWS_REGION}} \
--query 'Reservations[0].Instances[0].InstanceId' \
--output text 2>/dev/null)
if [ "$EXISTING" != "None" ] && [ -n "$EXISTING" ]; then
echo "Instance already running: $EXISTING"
echo $EXISTING > {{.PROJECT_DIR}}/aws/.instance-id
exit 0
fi
# Launch new instance
INSTANCE_ID=$(aws ec2 run-instances \
--image-id $AMI_ID \
--instance-type {{.AWS_INSTANCE_TYPE}} \
--key-name {{.AWS_KEY_NAME}} \
--security-group-ids $SG_ID \
--subnet-id {{.AWS_SUBNET_ID}} \
--associate-public-ip-address \
--user-data file://{{.PROJECT_DIR}}/aws/user-data.sh \
--block-device-mappings '[{"DeviceName":"/dev/sda1","Ebs":{"VolumeSize":50,"VolumeType":"gp3"}}]' \
--tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value={{.AWS_INSTANCE_NAME}}},{Key=Project,Value=runner-codes}]" \
--region {{.AWS_REGION}} \
--query 'Instances[0].InstanceId' \
--output text)
echo "Instance launched: $INSTANCE_ID"
echo $INSTANCE_ID > {{.PROJECT_DIR}}/aws/.instance-id
echo "Waiting for instance to be running..."
aws ec2 wait instance-running --instance-ids $INSTANCE_ID --region {{.AWS_REGION}}
# Get public IP
PUBLIC_IP=$(aws ec2 describe-instances \
--instance-ids $INSTANCE_ID \
--region {{.AWS_REGION}} \
--query 'Reservations[0].Instances[0].PublicIpAddress' \
--output text)
echo $PUBLIC_IP > {{.PROJECT_DIR}}/aws/.public-ip
echo ""
echo "==================================="
echo "Instance ready!"
echo "Instance ID: $INSTANCE_ID"
echo "Public IP: $PUBLIC_IP"
echo ""
echo "Connect with:"
echo " ssh -i {{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem ubuntu@$PUBLIC_IP"
echo ""
echo "Wait ~3-5 minutes for user-data script to complete"
echo "==================================="
aws:status:
desc: Show EC2 instance status
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.instance-id ]; then
echo "No instance found. Run 'task aws:launch' first."
exit 1
fi
INSTANCE_ID=$(cat {{.PROJECT_DIR}}/aws/.instance-id)
echo "Instance Status:"
aws ec2 describe-instances \
--instance-ids $INSTANCE_ID \
--region {{.AWS_REGION}} \
--query 'Reservations[0].Instances[0].{ID:InstanceId,State:State.Name,Type:InstanceType,IP:PublicIpAddress}' \
--output table
aws:ssh:
desc: SSH into EC2 instance
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.public-ip ]; then
echo "No instance IP found. Run 'task aws:launch' first."
exit 1
fi
PUBLIC_IP=$(cat {{.PROJECT_DIR}}/aws/.public-ip)
echo "Connecting to $PUBLIC_IP..."
ssh -i {{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem -o StrictHostKeyChecking=no ubuntu@$PUBLIC_IP
aws:logs:
desc: View setup logs from EC2 instance
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.public-ip ]; then
echo "No instance IP found."
exit 1
fi
PUBLIC_IP=$(cat {{.PROJECT_DIR}}/aws/.public-ip)
ssh -i {{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem -o StrictHostKeyChecking=no ubuntu@$PUBLIC_IP \
"sudo cat /var/log/firesandbox-setup.log 2>/dev/null || echo 'Setup log not found yet...'"
aws:deploy:
desc: Deploy project code to EC2 instance
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.public-ip ]; then
echo "No instance IP found."
exit 1
fi
PUBLIC_IP=$(cat {{.PROJECT_DIR}}/aws/.public-ip)
KEY={{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem
echo "Building guest-runner for amd64..."
cd {{.PROJECT_DIR}}/guest-runner && GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ../rootfs/guest-runner .
echo "Building host-agent for amd64..."
cd {{.PROJECT_DIR}}/host-agent && GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ../bin/host-agent .
echo "Deploying to $PUBLIC_IP..."
ssh -i $KEY -o StrictHostKeyChecking=no ubuntu@$PUBLIC_IP "sudo mkdir -p /opt/runner-codes/{bin,rootfs} && sudo chown -R ubuntu:ubuntu /opt/runner-codes"
scp -i $KEY -o StrictHostKeyChecking=no \
{{.PROJECT_DIR}}/bin/host-agent \
ubuntu@$PUBLIC_IP:/opt/runner-codes/bin/
scp -i $KEY -o StrictHostKeyChecking=no \
{{.PROJECT_DIR}}/rootfs/guest-runner \
ubuntu@$PUBLIC_IP:/opt/runner-codes/rootfs/
echo "Deployment complete!"
echo ""
echo "Next steps on the instance:"
echo " 1. SSH: task aws:ssh"
echo " 2. Inject guest-runner into rootfs"
echo " 3. Run test: task aws:test"
aws:inject-guest-runner:
desc: Inject guest-runner into rootfs on EC2 instance
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.public-ip ]; then
echo "No instance IP found."
exit 1
fi
PUBLIC_IP=$(cat {{.PROJECT_DIR}}/aws/.public-ip)
KEY={{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem
echo "Injecting guest-runner into rootfs..."
ssh -i $KEY -o StrictHostKeyChecking=no ubuntu@$PUBLIC_IP 'bash -s' << 'ENDSSH'
set -e
echo "Mounting rootfs..."
sudo mkdir -p /mnt/rootfs
sudo mount -o loop /srv/firecracker/rootfs.ext4 /mnt/rootfs
echo "Copying guest-runner..."
sudo cp /opt/runner-codes/rootfs/guest-runner /mnt/rootfs/usr/local/bin/guest-runner
sudo chmod +x /mnt/rootfs/usr/local/bin/guest-runner
echo "Creating systemd service..."
echo -e "[Unit]\nDescription=LLM FireSandbox Guest Runner\nAfter=network.target\n\n[Service]\nType=simple\nExecStart=/usr/local/bin/guest-runner -port 5000\nRestart=always\nRestartSec=1\n\n[Install]\nWantedBy=multi-user.target" | sudo tee /mnt/rootfs/etc/systemd/system/guest-runner.service > /dev/null
sudo ln -sf /etc/systemd/system/guest-runner.service /mnt/rootfs/etc/systemd/system/multi-user.target.wants/guest-runner.service 2>/dev/null || true
echo "Unmounting..."
sudo umount /mnt/rootfs
echo "Guest-runner injected successfully!"
ENDSSH
aws:test:
desc: Run Firecracker test on EC2 instance
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.public-ip ]; then
echo "No instance IP found."
exit 1
fi
PUBLIC_IP=$(cat {{.PROJECT_DIR}}/aws/.public-ip)
KEY={{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem
echo "Running Firecracker test on $PUBLIC_IP..."
ssh -i $KEY -o StrictHostKeyChecking=no ubuntu@$PUBLIC_IP 'bash -s' << 'ENDSSH'
set -e
cd /opt/runner-codes
echo "=== Checking prerequisites ==="
echo "KVM: $(ls -la /dev/kvm 2>/dev/null || echo 'NOT FOUND')"
echo "Vsock: $(ls -la /dev/vhost-vsock 2>/dev/null || echo 'NOT FOUND')"
echo "Firecracker: $(firecracker --version 2>/dev/null || echo 'NOT FOUND')"
echo ""
echo "=== Running host-agent test ==="
sudo ./bin/host-agent \
-kernel /srv/firecracker/vmlinux \
-rootfs /srv/firecracker/rootfs.ext4 \
-lang python \
-code "print('Hello from Firecracker microVM!')" \
-timeout 30
ENDSSH
aws:test-languages:
desc: Test all supported languages on EC2
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.public-ip ]; then
echo "No instance IP found."
exit 1
fi
PUBLIC_IP=$(cat {{.PROJECT_DIR}}/aws/.public-ip)
KEY={{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem
echo "Testing all languages on $PUBLIC_IP..."
ssh -i $KEY -o StrictHostKeyChecking=no ubuntu@$PUBLIC_IP 'bash -s' << 'ENDSSH'
cd /opt/runner-codes
echo "=== Testing Python ==="
sudo ./bin/host-agent -kernel /srv/firecracker/vmlinux -rootfs /srv/firecracker/rootfs.ext4 \
-lang python -code "import sys; print(f'Python {sys.version}')" -timeout 30
echo ""
echo "=== Testing Bash ==="
sudo ./bin/host-agent -kernel /srv/firecracker/vmlinux -rootfs /srv/firecracker/rootfs.ext4 \
-lang bash -code "echo 'Bash version:' && bash --version | head -1" -timeout 30
echo ""
echo "=== Testing Node.js ==="
sudo ./bin/host-agent -kernel /srv/firecracker/vmlinux -rootfs /srv/firecracker/rootfs.ext4 \
-lang node -code "console.log('Node.js ' + process.version)" -timeout 30 || echo "Node.js not available in rootfs"
echo ""
ENDSSH
aws:stop:
desc: Stop EC2 instance (keeps it for later)
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.instance-id ]; then
echo "No instance found."
exit 0
fi
INSTANCE_ID=$(cat {{.PROJECT_DIR}}/aws/.instance-id)
echo "Stopping instance $INSTANCE_ID..."
aws ec2 stop-instances --instance-ids $INSTANCE_ID --region {{.AWS_REGION}}
echo "Instance stopped. Run 'task aws:start' to start again."
aws:start:
desc: Start stopped EC2 instance
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.instance-id ]; then
echo "No instance found."
exit 1
fi
INSTANCE_ID=$(cat {{.PROJECT_DIR}}/aws/.instance-id)
echo "Starting instance $INSTANCE_ID..."
aws ec2 start-instances --instance-ids $INSTANCE_ID --region {{.AWS_REGION}}
echo "Waiting for instance to be running..."
aws ec2 wait instance-running --instance-ids $INSTANCE_ID --region {{.AWS_REGION}}
# Update public IP (may change after stop/start)
PUBLIC_IP=$(aws ec2 describe-instances \
--instance-ids $INSTANCE_ID \
--region {{.AWS_REGION}} \
--query 'Reservations[0].Instances[0].PublicIpAddress' \
--output text)
echo $PUBLIC_IP > {{.PROJECT_DIR}}/aws/.public-ip
echo "Instance running. New IP: $PUBLIC_IP"
aws:terminate:
desc: Terminate EC2 instance (permanent delete)
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.instance-id ]; then
echo "No instance found."
exit 0
fi
INSTANCE_ID=$(cat {{.PROJECT_DIR}}/aws/.instance-id)
echo "WARNING: This will permanently delete instance $INSTANCE_ID"
read -p "Are you sure? (yes/no): " confirm
if [ "$confirm" = "yes" ]; then
aws ec2 terminate-instances --instance-ids $INSTANCE_ID --region {{.AWS_REGION}}
rm -f {{.PROJECT_DIR}}/aws/.instance-id {{.PROJECT_DIR}}/aws/.public-ip
echo "Instance terminated."
else
echo "Cancelled."
fi
aws:cleanup:
desc: Clean up all AWS resources
cmds:
- |
echo "Cleaning up AWS resources..."
# Terminate instance
if [ -f {{.PROJECT_DIR}}/aws/.instance-id ]; then
INSTANCE_ID=$(cat {{.PROJECT_DIR}}/aws/.instance-id)
echo "Terminating instance $INSTANCE_ID..."
aws ec2 terminate-instances --instance-ids $INSTANCE_ID --region {{.AWS_REGION}} 2>/dev/null || true
fi
# Wait for termination
sleep 10
# Delete security group
SG_ID=$(aws ec2 describe-security-groups \
--filters "Name=group-name,Values=firesandbox-sg" \
--region {{.AWS_REGION}} \
--query 'SecurityGroups[0].GroupId' \
--output text 2>/dev/null)
if [ "$SG_ID" != "None" ] && [ -n "$SG_ID" ]; then
echo "Deleting security group $SG_ID..."
aws ec2 delete-security-group --group-id $SG_ID --region {{.AWS_REGION}} 2>/dev/null || true
fi
# Delete key pair
echo "Deleting key pair {{.AWS_KEY_NAME}}..."
aws ec2 delete-key-pair --key-name {{.AWS_KEY_NAME}} --region {{.AWS_REGION}} 2>/dev/null || true
# Clean local files
rm -f {{.PROJECT_DIR}}/aws/.instance-id {{.PROJECT_DIR}}/aws/.public-ip {{.PROJECT_DIR}}/aws/.ami-id {{.PROJECT_DIR}}/aws/.sg-id
rm -rf {{.PROJECT_DIR}}/aws/keys
echo "Cleanup complete."
# ===========================================================================
# Full Test Pipeline
# ===========================================================================
aws:full-test:
desc: Complete AWS test pipeline (launch, deploy, test)
cmds:
- task: aws:launch
- echo "Waiting 180 seconds for user-data to complete..."
- sleep 180
- task: aws:deploy
- task: aws:inject-guest-runner
- task: aws:test
# ===========================================================================
# Rootfs with Multi-Language Support
# ===========================================================================
aws:rootfs-expand:
desc: Expand rootfs to 2GB for installing languages
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.public-ip ]; then
echo "No instance IP found."
exit 1
fi
PUBLIC_IP=$(cat {{.PROJECT_DIR}}/aws/.public-ip)
KEY={{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem
echo "Expanding rootfs to 2GB..."
ssh -i $KEY -o StrictHostKeyChecking=no ubuntu@$PUBLIC_IP 'bash -s' << 'ENDSSH'
set -e
cd /srv/firecracker
# Backup original
sudo cp rootfs.ext4 rootfs.ext4.original
# Create new larger image
echo "Creating 2GB rootfs..."
sudo dd if=/dev/zero of=rootfs-2g.ext4 bs=1M count=2048
# Format as ext4
sudo mkfs.ext4 rootfs-2g.ext4
# Mount both
sudo mkdir -p /mnt/old /mnt/new
sudo mount -o loop rootfs.ext4 /mnt/old
sudo mount -o loop rootfs-2g.ext4 /mnt/new
# Copy contents
echo "Copying rootfs contents..."
sudo cp -a /mnt/old/* /mnt/new/
# Unmount
sudo umount /mnt/old
sudo umount /mnt/new
# Replace
sudo mv rootfs-2g.ext4 rootfs.ext4
echo "Rootfs expanded to 2GB!"
ls -lh /srv/firecracker/rootfs.ext4
ENDSSH
aws:rootfs-install-python3:
desc: Install Python 3.10 in rootfs
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.public-ip ]; then
echo "No instance IP found."
exit 1
fi
PUBLIC_IP=$(cat {{.PROJECT_DIR}}/aws/.public-ip)
KEY={{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem
echo "Installing Python 3 in rootfs..."
ssh -i $KEY -o StrictHostKeyChecking=no ubuntu@$PUBLIC_IP 'bash -s' << 'ENDSSH'
set -e
# Mount rootfs
sudo mkdir -p /mnt/rootfs
sudo mount -o loop /srv/firecracker/rootfs.ext4 /mnt/rootfs
# Setup resolv.conf for DNS
sudo cp /etc/resolv.conf /mnt/rootfs/etc/resolv.conf
# Mount required filesystems for chroot
sudo mount --bind /dev /mnt/rootfs/dev
sudo mount --bind /proc /mnt/rootfs/proc
sudo mount --bind /sys /mnt/rootfs/sys
echo "Installing Python 3..."
sudo chroot /mnt/rootfs /bin/bash -c "
apt-get update
apt-get install -y --no-install-recommends python3 python3-pip
python3 --version
pip3 --version
"
# Cleanup
sudo umount /mnt/rootfs/sys
sudo umount /mnt/rootfs/proc
sudo umount /mnt/rootfs/dev
sudo umount /mnt/rootfs
echo "Python 3 installed successfully!"
ENDSSH
aws:rootfs-install-nodejs:
desc: Install Node.js 20 LTS in rootfs
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.public-ip ]; then
echo "No instance IP found."
exit 1
fi
PUBLIC_IP=$(cat {{.PROJECT_DIR}}/aws/.public-ip)
KEY={{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem
echo "Installing Node.js in rootfs..."
ssh -i $KEY -o StrictHostKeyChecking=no ubuntu@$PUBLIC_IP 'bash -s' << 'ENDSSH'
set -e
# Mount rootfs
sudo mkdir -p /mnt/rootfs
sudo mount -o loop /srv/firecracker/rootfs.ext4 /mnt/rootfs
# Setup resolv.conf for DNS
sudo cp /etc/resolv.conf /mnt/rootfs/etc/resolv.conf
# Mount required filesystems for chroot
sudo mount --bind /dev /mnt/rootfs/dev
sudo mount --bind /proc /mnt/rootfs/proc
sudo mount --bind /sys /mnt/rootfs/sys
echo "Installing Node.js 20 LTS..."
sudo chroot /mnt/rootfs /bin/bash -c "
apt-get update
apt-get install -y curl gnupg
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt-get install -y nodejs
node --version
npm --version
"
# Cleanup
sudo umount /mnt/rootfs/sys
sudo umount /mnt/rootfs/proc
sudo umount /mnt/rootfs/dev
sudo umount /mnt/rootfs
echo "Node.js installed successfully!"
ENDSSH
aws:rootfs-install-go:
desc: Install Go 1.21 in rootfs
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.public-ip ]; then
echo "No instance IP found."
exit 1
fi
PUBLIC_IP=$(cat {{.PROJECT_DIR}}/aws/.public-ip)
KEY={{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem
echo "Installing Go in rootfs..."
ssh -i $KEY -o StrictHostKeyChecking=no ubuntu@$PUBLIC_IP 'bash -s' << 'ENDSSH'
set -e
# Mount rootfs
sudo mkdir -p /mnt/rootfs
sudo mount -o loop /srv/firecracker/rootfs.ext4 /mnt/rootfs
echo "Downloading Go 1.21..."
wget -q https://go.dev/dl/go1.21.13.linux-amd64.tar.gz -O /tmp/go.tar.gz
echo "Installing Go..."
sudo tar -C /mnt/rootfs/usr/local -xzf /tmp/go.tar.gz
# Add to PATH
echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee /mnt/rootfs/etc/profile.d/go.sh
sudo chmod +x /mnt/rootfs/etc/profile.d/go.sh
# Create symlink
sudo ln -sf /usr/local/go/bin/go /mnt/rootfs/usr/bin/go
# Verify
sudo chroot /mnt/rootfs /usr/bin/go version
sudo umount /mnt/rootfs
echo "Go installed successfully!"
rm -f /tmp/go.tar.gz
ENDSSH
aws:rootfs-install-rust:
desc: Install Rust (rustc, cargo) in rootfs
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.public-ip ]; then
echo "No instance IP found."
exit 1
fi
PUBLIC_IP=$(cat {{.PROJECT_DIR}}/aws/.public-ip)
KEY={{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem
echo "Installing Rust in rootfs..."
ssh -i $KEY -o StrictHostKeyChecking=no ubuntu@$PUBLIC_IP 'bash -s' << 'ENDSSH'
set -e
# Mount rootfs
sudo mkdir -p /mnt/rootfs
sudo mount -o loop /srv/firecracker/rootfs.ext4 /mnt/rootfs
# Setup resolv.conf for DNS
sudo cp /etc/resolv.conf /mnt/rootfs/etc/resolv.conf
# Mount required filesystems for chroot
sudo mount --bind /dev /mnt/rootfs/dev
sudo mount --bind /proc /mnt/rootfs/proc
sudo mount --bind /sys /mnt/rootfs/sys
echo "Installing Rust via rustup..."
sudo chroot /mnt/rootfs /bin/bash -c "
apt-get update
apt-get install -y curl build-essential
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable --profile minimal
source /root/.cargo/env
rustc --version
cargo --version
"
# Make rust available system-wide
sudo chroot /mnt/rootfs /bin/bash -c "
cp -r /root/.cargo /opt/cargo
cp -r /root/.rustup /opt/rustup
echo 'export CARGO_HOME=/opt/cargo' > /etc/profile.d/rust.sh
echo 'export RUSTUP_HOME=/opt/rustup' >> /etc/profile.d/rust.sh
echo 'export PATH=/opt/cargo/bin:\$PATH' >> /etc/profile.d/rust.sh
chmod +x /etc/profile.d/rust.sh
ln -sf /opt/cargo/bin/rustc /usr/bin/rustc
ln -sf /opt/cargo/bin/cargo /usr/bin/cargo
"
# Cleanup
sudo umount /mnt/rootfs/sys
sudo umount /mnt/rootfs/proc
sudo umount /mnt/rootfs/dev
sudo umount /mnt/rootfs
echo "Rust installed successfully!"
ENDSSH
aws:rootfs-install-all:
desc: Install all languages (Python3, Node.js, Go, Rust) in rootfs
cmds:
- task: aws:rootfs-expand
- task: aws:rootfs-install-python3
- task: aws:rootfs-install-nodejs
- task: aws:rootfs-install-go
- task: aws:rootfs-install-rust
- task: aws:inject-guest-runner
- echo ""
- echo "All languages installed! Run 'task aws:snapshot-create' to create snapshot."
aws:rootfs-verify:
desc: Verify all languages are installed in rootfs
cmds:
- |
if [ ! -f {{.PROJECT_DIR}}/aws/.public-ip ]; then
echo "No instance IP found."
exit 1
fi
PUBLIC_IP=$(cat {{.PROJECT_DIR}}/aws/.public-ip)
KEY={{.PROJECT_DIR}}/aws/keys/{{.AWS_KEY_NAME}}.pem
echo "Verifying language installations..."
ssh -i $KEY -o StrictHostKeyChecking=no ubuntu@$PUBLIC_IP 'bash -s' << 'ENDSSH'
sudo mkdir -p /mnt/rootfs
sudo mount -o loop /srv/firecracker/rootfs.ext4 /mnt/rootfs
echo "=== Language Versions ==="
echo -n "Python: "; sudo chroot /mnt/rootfs python3 --version 2>/dev/null || echo "NOT INSTALLED"