-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathc9cli.sh
1565 lines (1372 loc) · 35.4 KB
/
c9cli.sh
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
#!/bin/bash
VERSION="5.18"
if [ "$(id -u)" != "0" ]; then
echo "c9cli must be run as root!" 1>&2
exit 1
fi
ubuntu_version=$(lsb_release -r | awk '{print $2}')
check_update() {
echo "Checking for available updates..."
REPO_URL="https://hostingjaya.ninja/api/mirror/c9cli/c9cli?raw=true"
max_attempts=3
attempt=1
while [ $attempt -le $max_attempts ]; do
if latest_info=$(curl -s --connect-timeout 10 "$REPO_URL/c9cli"); then
latest_version=$(echo "$latest_info" | grep -o 'VERSION="[0-9]*\.[0-9]*"' | cut -d '"' -f 2)
if [ -n "$latest_version" ]; then
if [ "$latest_version" != "$VERSION" ]; then
echo "New version available: v$latest_version (current: v$VERSION)"
echo "Run 'c9cli update' to update."
else
echo "You are using the latest version (v$VERSION)."
fi
return 0
else
echo "Failed to extract version information."
fi
else
echo "Failed to connect to update server on attempt $attempt of $max_attempts."
fi
attempt=$((attempt + 1))
[ $attempt -le $max_attempts ] && {
echo "Retrying in 2 seconds..."
sleep 2
}
done
echo "Failed to check for updates after $max_attempts attempts."
return 1
}
LAST_CHECK_FILE="/tmp/c9cli_last_check"
CHECK_INTERVAL=86400
current_time=$(date +%s)
if [ -f "$LAST_CHECK_FILE" ]; then
last_check=$(cat "$LAST_CHECK_FILE")
time_diff=$((current_time - last_check))
if [ $time_diff -gt $CHECK_INTERVAL ]; then
check_update
echo "$current_time" >"$LAST_CHECK_FILE"
fi
else
echo "$current_time" >"$LAST_CHECK_FILE"
check_update
fi
# COMMANDS
banner() {
echo " ___ _ _ "
echo " ___ / _ \ ___| (_)"
echo " / __| (_) |/ __| | |"
echo " | (__ \__, | (__| | |"
echo " \___| /_/ \___|_|_|"
echo
echo "===== CLOUD9 CLI MANAGER ====="
echo
}
about() {
echo "Name of File : c9cli.sh"
echo "Version : $VERSION"
echo "Tested on :"
echo " - Debian : Ubuntu 18.04, 20.04, 22.04"
echo
echo "Built with love♡ by gvoze32"
}
# =========== DON'T CHANGE THE ORDER OF THIS FUNCTION =========== #
bantuan() {
echo "How to use:"
echo "c9cli must be run as root"
echo "c9cli [command] [argument] [argument]"
echo
echo "Command Lists:"
echo "quickcreate : Quick create C9 workspace in root"
echo " restart : Restart quick created C9"
echo " fix : Fix quick created C9 installation"
echo " status : Show status of quick created C9"
echo " stop : Stop quick created C9"
echo "create"
echo " systemd : Create a new SystemD workspace"
echo " systemdlimit : Create a new SystemD workspace with limited RAM"
echo " docker : Create a new Docker container"
echo " dockerlimit : Create a new Docker container with limited RAM"
echo "manage"
echo " systemd"
echo " stop : Stop workspace"
echo " start : Start workspace"
echo " delete : Delete workspace"
echo " status : Show workspace status"
echo " restart : Restart workspace"
echo " password : Change user password"
echo " schedule : Schedule workspace deletion"
echo " scheduled : Show scheduled workspace deletion"
echo " convert : Convert user to superuser"
echo " docker"
echo " stop : Stop Docker container"
echo " start : Start Docker container"
echo " delete : Delete Docker container"
echo " status : Show container status"
echo " restart : Restart running containers"
echo " restartall : Restart (all) running containers"
echo " reset : Reset Docker container"
echo " password : Change user password, port & update limited RAM for dockerlimit"
echo " schedule : Schedule container deletion"
echo " scheduled : Show scheduled container deletion"
echo " list : Show Docker container lists"
echo " configure : Stop, start or restart running container"
echo "port : Show used port lists"
echo "backup : Backup workspace data with Rclone (Docker only)"
echo "update : Update c9cli to the latest version"
echo "help : Show help"
echo "version : Show version"
echo
echo "Options:"
echo "-u : Username"
echo "-p : Password"
echo "-o : Port number"
echo "-l : Memory limit (e.g., 1024m)"
echo "-c : CPU limit (e.g., 10% or 1.0)"
echo "-i : Image (e.g., gvoze32/cloud9:jammy)"
echo "-t : Type (e.g., 1 for Docker, 2 for Docker Memory Limit)"
echo "-n : Rclone remote name"
echo "-h : Backup hour"
echo "-f : Backup folder name"
echo "-s : Backup service provider"
echo
echo "Copyright (c) 2024 c9cli (under MIT License)"
echo "Built with love♡ by gvoze32"
}
# CREATE SYSTEMD
createnewsystemd() {
while getopts "u:p:o:" opt; do
case $opt in
u) user="$OPTARG" ;;
p) pw="$OPTARG" ;;
o) port="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Username: " user
fi
if [[ -z "$pw" ]]; then
read -p "Password: " pw
fi
if [[ -z "$port" ]]; then
read -p "Port: " port
fi
echo
echo "Creating workspace:"
echo "Username: $user"
echo "Password: $pw"
echo "Port: $port"
apt-get update -y
apt-get upgrade -y
apt-get update -y
adduser --disabled-password --gecos "" $user
case $ubuntu_version in
22.04 | 20.04)
echo "$user:$pw" | chpasswd
mkdir -p /home/$user/my-projects /home/$user/c9sdk
chown -R $user:$user /home/$user
chmod 700 /home/$user
sudo -u $user git clone --depth=5 https://github.com/c9/core.git /home/$user/c9sdk
sudo -u $user bash -c "cd /home/$user/c9sdk && scripts/install-sdk.sh"
;;
18.04)
echo -e "$pw\n$pw" | passwd $user
mkdir -p /home/$user/my-projects /home/$user/c9sdk
chown $user.$user /home/$user -R
chmod 700 /home/$user
sudo -u $user git clone --depth=5 https://github.com/c9/core.git /home/$user/c9sdk
sudo -u $user -H sh -c "cd /home/$user/c9sdk; scripts/install-sdk.sh"
;;
esac
cat >/lib/systemd/system/c9-$user.service <<EOF
[Unit]
Description=c9 for $user
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/node /home/${user}/c9sdk/server.js -a $user:$pw --listen 0.0.0.0 --packed -w /home/$user/my-projects
Environment=NODE_ENV=production PORT=$port
User=$user
Group=$user
UMask=0002
Restart=on-failure
StandardOutput=journal
StandardError=journal
SyslogIdentifier=c9-$user
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable c9-$user.service
systemctl restart c9-$user.service
sleep 10
systemctl status c9-$user.service
}
createnewsystemdlimit() {
while getopts "u:p:o:l:c:" opt; do
case $opt in
u) user="$OPTARG" ;;
p) pw="$OPTARG" ;;
o) port="$OPTARG" ;;
l) limit="$OPTARG" ;;
c) cpu_limit="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Username: " user
fi
if [[ -z "$pw" ]]; then
read -p "Password: " pw
fi
if [[ -z "$port" ]]; then
read -p "Port: " port
fi
if [[ -z "$limit" ]]; then
read -p "Memory Limit (e.g., 1024m): " limit
fi
if [[ -z "$cpu_limit" ]]; then
read -p "CPU Limit (e.g., 10%): " cpu_limit
fi
echo
echo "Creating workspace with memory limit:"
echo "Username: $user"
echo "Password: $pw"
echo "Port: $port"
echo "Memory Limit: $limit"
echo "CPU Limit: $cpu_limit"
apt-get update -y
apt-get upgrade -y
apt-get update -y
adduser --disabled-password --gecos "" $user
case $ubuntu_version in
22.04 | 20.04)
echo "$user:$pw" | chpasswd
mkdir -p /home/$user/my-projects /home/$user/c9sdk
chown -R $user:$user /home/$user
chmod 700 /home/$user
sudo -u $user git clone --depth=5 https://github.com/c9/core.git /home/$user/c9sdk
sudo -u $user bash -c "cd /home/$user/c9sdk && scripts/install-sdk.sh"
;;
18.04)
echo -e "$pw\n$pw" | passwd $user
mkdir -p /home/$user/my-projects /home/$user/c9sdk
chown $user.$user /home/$user -R
chmod 700 /home/$user
sudo -u $user git clone --depth=5 https://github.com/c9/core.git /home/$user/c9sdk
sudo -u $user -H sh -c "cd /home/$user/c9sdk; scripts/install-sdk.sh"
;;
esac
cat >/lib/systemd/system/c9-$user.service <<EOF
[Unit]
Description=c9 for $user
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/node /home/${user}/c9sdk/server.js -a $user:$pw --listen 0.0.0.0 --packed -w /home/$user/my-projects
Environment=NODE_ENV=production PORT=$port
User=$user
Group=$user
UMask=0002
MemoryMax=$limit
CPUQuota=$cpu_limit
Restart=on-failure
StandardOutput=journal
StandardError=journal
SyslogIdentifier=c9-$user
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable c9-$user.service
systemctl restart c9-$user.service
sleep 10
systemctl status c9-$user.service
}
createnewdocker() {
while getopts "u:p:o:i:" opt; do
case $opt in
u) user="$OPTARG" ;;
p) pw="$OPTARG" ;;
o) port="$OPTARG" ;;
i) image="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Username: " user
fi
if [[ -z "$pw" ]]; then
read -p "Password: " pw
fi
if [[ -z "$port" ]]; then
read -p "Port: " port
fi
if [[ -z "$image" ]]; then
echo "Select image:"
echo "1. Ubuntu 20.04"
echo "2. Ubuntu 22.04"
echo "3. Ubuntu 24.04"
read -p "Enter image option (1/3): " image_choice
if [ "$image_choice" == "1" ]; then
image="gvoze32/cloud9:focal"
elif [ "$image_choice" == "2" ]; then
image="gvoze32/cloud9:jammy"
elif [ "$image_choice" == "3" ]; then
image="gvoze32/cloud9:noble"
else
echo "Invalid option, using default image."
image="gvoze32/cloud9:jammy"
fi
else
echo "Using provided image: $image"
fi
echo
echo "Creating docker container:"
echo "Username: $user"
echo "Password: $pw"
echo "Port: $port"
echo "Image: $image"
cd /home/c9users
rm .env
cat >/home/c9users/.env <<EOF
NAMA_PELANGGAN=$user
PASSWORD_PELANGGAN=$pw
PORT=$port
DOCKER_IMAGE=$image
EOF
docker compose -p $user up -d
if [ -d "/home/c9users/$user" ]; then
cd /home/c9users/$user
### Your custom default bundling files goes here, it's recommended to put it on resources directory
### START
### END
cd
else
echo -e "\033[33mWARN! Workspace directory not found - Ignore this message if you are not adding default bundling files\033[0m"
fi
}
# CREATE DOCKERLIMIT
createnewdockermemlimit() {
while getopts "u:p:o:l:c:i:" opt; do
case $opt in
u) user="$OPTARG" ;;
p) pw="$OPTARG" ;;
o) port="$OPTARG" ;;
l) limit="$OPTARG" ;;
c) cpu_limit="$OPTARG" ;;
i) image="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Username: " user
fi
if [[ -z "$pw" ]]; then
read -p "Password: " pw
fi
if [[ -z "$port" ]]; then
read -p "Port: " port
fi
if [[ -z "$limit" ]]; then
read -p "Memory Limit (e.g., 1024m): " limit
fi
if [[ -z "$cpu_limit" ]]; then
read -p "CPU Limit (e.g., 1.0 for 1 core): " cpu_limit
fi
if [[ -z "$image" ]]; then
echo "Select image:"
echo "1. Ubuntu 20.04"
echo "2. Ubuntu 22.04"
echo "3. Ubuntu 24.04"
read -p "Enter image option (1/3) : " image_choice
if [ "$image_choice" == "1" ]; then
image="gvoze32/cloud9:focal"
elif [ "$image_choice" == "2" ]; then
image="gvoze32/cloud9:jammy"
elif [ "$image_choice" == "3" ]; then
image="gvoze32/cloud9:noble"
else
echo "Invalid option, using default image."
image="gvoze32/cloud9:jammy"
fi
else
echo "Using provided image: $image"
fi
echo
echo "Creating docker container with memory limit:"
echo "Username: $user"
echo "Password: $pw"
echo "Port: $port"
echo "Memory Limit: $limit"
echo "CPU Limit: $cpu_limit"
echo "Image: $image"
cd /home/c9usersmemlimit
rm .env
cat >/home/c9usersmemlimit/.env <<EOF
NAMA_PELANGGAN=$user
PASSWORD_PELANGGAN=$pw
PORT=$port
MEMORY=$limit
CPU_LIMIT=$cpu_limit
DOCKER_IMAGE=$image
EOF
docker compose -p $user up -d
if [ -d "/home/c9usersmemlimit/$user" ]; then
cd /home/c9usersmemlimit/$user
### Your custom default bundling files goes here, it's recommended to put it on resources directory
### START
### END
cd
else
echo -e "\033[33mWARN! Workspace directory not found - Ignore this message if you are not adding default bundling files\033[0m"
fi
}
# MANAGE SYSTEMD
stopsystemd() {
while getopts "u:" opt; do
case $opt in
u) user="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Input User: " user
fi
sleep 3
systemctl stop c9-$user.service
}
startsystemd() {
while getopts "u:" opt; do
case $opt in
u) user="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Input User: " user
fi
sleep 3
systemctl start c9-$user.service
}
deletesystemd() {
while getopts "u:" opt; do
case $opt in
u) user="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Input User: " user
fi
sleep 3
systemctl stop c9-$user.service
sleep 3
killall -u $user
sleep 3
userdel $user
# OPTIONAL: Remove user directory
# rm -rf /home/$user
}
statussystemd() {
while getopts "u:" opt; do
case $opt in
u) user="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Input User: " user
fi
systemctl status c9-$user.service
}
restartsystemd() {
while getopts "u:" opt; do
case $opt in
u) user="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Input User: " user
fi
systemctl daemon-reload
systemctl enable c9-$user.service
systemctl restart c9-$user.service
sleep 10
systemctl status c9-$user.service
}
changepasswordsystemd() {
while getopts "u:p:o:" opt; do
case $opt in
u) user="$OPTARG" ;;
p) password="$OPTARG" ;;
o) port="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Input User: " user
fi
if [[ -z "$password" ]]; then
read -p "Input New Password: " password
fi
if [[ -z "$port" ]]; then
read -p "Input New Port: " port
fi
sed -i "s/-a $user:.*/-a $user:$password/" /lib/systemd/system/c9-$user.service
sed -i "s/PORT=.*/PORT=$port/" /lib/systemd/system/c9-$user.service
systemctl daemon-reload
systemctl restart c9-$user.service
sleep 10
systemctl status c9-$user.service
}
schedulesystemd() {
read -p "Input User: " user
echo " "
echo "Format Example for Time: "
echo " "
echo "10:00 AM 6/22/2015"
echo "10:00 AM July 25"
echo "10:00 AM"
echo "10:00 AM Sun"
echo "10:00 AM next month"
echo "10:00 AM tomorrow"
echo "now + 1 hour"
echo "now + 30 minutes"
echo "now + 1 week"
echo "now + 1 year"
echo "midnight"
echo " "
read -p "Time: " waktu
at $waktu <<END
sleep 3
systemctl stop c9-$user.service
# OPTIONAL: Remove user directory
# sleep 3
# killall -u $user
# sleep 3
# userdel $user
END
}
scheduledatq() {
atq
}
convertsystemd() {
read -p "Input User: " user
echo "Input user password"
passwd $user
echo "Warning, C9 will be restart!"
usermod -aG sudo $user
systemctl daemon-reload
systemctl enable c9-$user.service
systemctl restart c9-$user.service
sleep 10
systemctl status c9-$user.service
}
# MANAGE DOCKER
stopdocker() {
while getopts "u:t:" opt; do
case $opt in
u) user="$OPTARG" ;;
t) type="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Input User: " user
fi
if [[ -z "$type" ]]; then
echo "Are the file is using Docker or Docker Memory Limit?"
echo "1. Docker"
echo "2. Docker Memory Limit"
read -r -p "Choose: " response
else
response="$type"
fi
case "$response" in
1)
cd /home/c9users
;;
*)
cd /home/c9usersmemlimit
;;
esac
docker compose -p $user stop
}
startdocker() {
while getopts "u:t:" opt; do
case $opt in
u) user="$OPTARG" ;;
t) type="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Input User: " user
fi
if [[ -z "$type" ]]; then
echo "Are the file is using Docker or Docker Memory Limit?"
echo "1. Docker"
echo "2. Docker Memory Limit"
read -r -p "Choose: " response
else
response="$type"
fi
case "$response" in
1)
cd /home/c9users
;;
*)
cd /home/c9usersmemlimit
;;
esac
docker compose -p $user start
}
deletedocker() {
while getopts "u:t:" opt; do
case $opt in
u) user="$OPTARG" ;;
t) type="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Input User: " user
fi
if [[ -z "$type" ]]; then
echo "Are the file is using Docker or Docker Memory Limit?"
echo "1. Docker"
echo "2. Docker Memory Limit"
read -r -p "Choose: " response
else
response="$type"
fi
case "$response" in
1)
cd /home/c9users
;;
*)
cd /home/c9usersmemlimit
;;
esac
docker compose -p $user down
# OPTIONAL: Remove user directory
# rm -rf $user
}
listdocker() {
docker ps
}
statusdocker() {
docker stats
}
changepassworddocker() {
while getopts "u:p:t:o:l:c:" opt; do
case $opt in
u) user="$OPTARG" ;;
p) newpw="$OPTARG" ;;
t) type="$OPTARG" ;;
o) port="$OPTARG" ;;
l) mem="$OPTARG" ;;
c) cpu_limit="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Input Username: " user
fi
if [[ -z "$newpw" ]]; then
read -p "Input New Password: " newpw
fi
if [[ -z "$port" ]]; then
read -p "Input Port: " port
fi
if [[ -z "$type" ]]; then
echo "Is the user using Docker or Docker Memory Limit?"
echo "1. Docker"
echo "2. Docker Memory Limit"
read -r -p "Choose: " response
else
response="$type"
fi
case "$response" in
1)
base_dir="/home/c9users"
mem=""
cpu_limit=""
;;
2)
base_dir="/home/c9usersmemlimit"
if [[ -z "$mem" ]]; then
read -p "Memory Limit (e.g., 1024m): " mem
fi
if [[ -z "$cpu_limit" ]]; then
read -p "CPU Limit (e.g., 1.0 for 1 core): " cpu_limit
fi
;;
*)
echo "Invalid option"
return
;;
esac
cd "$base_dir" || return
cat >.env <<EOF
NAMA_PELANGGAN=$user
PASSWORD_PELANGGAN=$newpw
PORT=$port
EOF
if [ "$response" = "2" ]; then
cat >>.env <<EOF
MEMORY=$mem
CPU_LIMIT=$cpu_limit
DOCKER_IMAGE=gvoze32/cloud9:jammy
EOF
fi
if [ -d "$base_dir/$user" ]; then
cd "$base_dir/$user" || return
cat >.env <<EOF
PORT=$port
NAMA_PELANGGAN=$user
PASSWORD_PELANGGAN=$newpw
EOF
if [ "$response" = "2" ]; then
cat >>.env <<EOF
MEMORY=$mem
CPU_LIMIT=$cpu_limit
EOF
fi
cd "$base_dir"
echo "Password, port and .env updated for user $user"
docker compose -p $user down
docker compose -p $user up -d
echo "Docker container recreated for user $user"
else
echo "User $user does not exist or workspace directory not found"
fi
}
scheduledocker() {
read -p "Input User: " user
echo Are the file is using docker or dockermemlimit?
read -r -p "Answer Y if you are using docker and answer N if you are using dockermemlimit [y/N] " response
echo " "
echo "Format Example for Time: "
echo " "
echo "10:00 AM 6/22/2015"
echo "10:00 AM July 25"
echo "10:00 AM"
echo "10:00 AM Sun"
echo "10:00 AM next month"
echo "10:00 AM tomorrow"
echo "now + 1 hour"
echo "now + 30 minutes"
echo "now + 1 week"
echo "now + 1 year"
echo "midnight"
echo " "
read -p "Time: " waktu
case "$response" in
[yY][eE][sS] | [yY])
at $waktu <<END
cd /home/c9users
docker compose -p $user stop
# OPTIONAL: Remove user setup
# docker compose -p $user down
END
;;
*)
at $waktu <<END
cd /home/c9usersmemlimit
docker compose -p $user stop
# OPTIONAL: Remove user setup
# docker compose -p $user down
END
;;
esac
}
configuredocker() {
read -p "Input User: " user
echo 1. Stop
echo 2. Start
echo 3. Restart
read -r -p "Choose: " response
case "$restart" in
1)
echo Are the file is using Docker or Docker Memory Limit?
echo 1. Docker
echo 2. Docker Memory Limit
read -r -p "Choose: " response
case "$response" in
1)
cd /home/c9users
;;
*)
cd /home/c9usersmemlimit
;;
esac
docker container stop $user
# OPTIONAL: Remove user setup
# docker compose -p $user down
;;
2)
echo Are the file is using Docker or Docker Memory Limit?
echo 1. Docker
echo 2. Docker Memory Limit
read -r -p "Choose: " response
case "$response" in
1)
cd /home/c9users
;;
*)
cd /home/c9usersmemlimit
;;
esac
docker container start $user
;;
*)
echo Are the file is using Docker or Docker Memory Limit?
echo 1. Docker
echo 2. Docker Memory Limit
read -r -p "Choose: " response
case "$response" in
1)
cd /home/c9users
;;
*)
cd /home/c9usersmemlimit
;;
esac
docker container stop $user
docker container start $user
# OPTIONAL: Remove user setup
# docker compose -p $user down
# docker compose -p $user up -d
;;
esac
}
restartdocker() {
while getopts "u:" opt; do
case $opt in
u) user="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
containers=$(docker ps --format "{{.Names}}")
echo "Container lists:"
echo "$containers" | sed 's/^c9-//'
if [[ -z "$user" ]]; then
read -p "Input User: " user
fi
container_name="c9-$user"
if echo "$containers" | grep -qw "$container_name"; then
docker restart "$container_name"
if [ $? -eq 0 ]; then
echo "Container '$container_name' restarted successfully."
else
echo "Container restart '$container_name' failed."
fi
else
echo "Container name '$container_name' invalid. Make sure the name is correct."
fi
}
restartdockerall() {
docker restart $(docker ps -q)
}
resetdocker() {
while getopts "u:t:" opt; do
case $opt in
u) user="$OPTARG" ;;
t) type="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [[ -z "$user" ]]; then
read -p "Input User: " user
fi
if [[ -z "$type" ]]; then
echo "Are the file is using Docker or Docker Memory Limit?"
echo "1. Docker"
echo "2. Docker Memory Limit"
read -r -p "Choose: " response
else