This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rb_deploy.c
executable file
·1909 lines (1721 loc) · 66.4 KB
/
rb_deploy.c
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
/*
* File: rb_deploy.c
* Author: ZeroC0D3 Engineer (zeroc0d3)
*
* Created on Oct 22nd, 2017, 08:00
*
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
int ret;
char cmdRun[2048];
char DATE_TIME[100];
char SNAP_FOLDER[100];
char SNAP_FOLDER_RELEASE[512];
int IS_ROLLBACK = 0; // Rollback Status (0 = no rollback migration, 1 = rollback on failed deploy)
int IS_ERROR_DEPLOY = 0; // Error Deployment Status (0 = no error, 1 = still error)
/* =======================================
USER CONFIGURATION
======================================= */
int NUM_RELEASE = 10; // Maximum Number of Release Folder
char ENV[64] = "staging"; // Selected Environment (staging / production)
int NUM_LOG_VIEW = 50; // Maximum Line Number Viewing Log
int RAILS_VERSION = 5; // Rails Version (default: 5)
int ENABLE_MIGRATION = 0; // Force Enable Migration (0 = disable/default, 1 = enable)
int ENABLE_BUNDLE_INSTALL = 1; // Enable Running "bundle install" (0 = disable/default, 1 = enable)
int ENABLE_CLOBBER_ASSETS = 1; // Enable Running Clobber/Cleanup Assets (0 = disable/default, 1 = enable)
int ENABLE_COMPILE_ASSETS = 1; // Enable Running Assets Precompile (0 = disable/default, 1 = enable)
int ENABLE_FAYE_SERVICE = 1; // Enable Running Faye Service (0 = disable/default, 1 = enable)
int ENABLE_MONGODB_SERVICE = 1; // Enable Running MongoDB Service (0 = disable/default, 1 = enable)
int ENABLE_PUSHR_SERVICE = 1; // Enable Running Pushr Service (0 = disable/default, 1 = enable)
int ENABLE_PUSHGCM_SERVICE = 1; // Enable Running Push GCM (Google Notification) Service (0 = disable/default, 1 = enable)
int ENABLE_REDIS_SERVICE = 1; // Enable Running Redis Service (0 = disable/default, 1 = enable)
int ENABLE_SIDEKIQ_SERVICE = 1; // Enable Running Sidekiq Service (0 = disable/default, 1 = enable)
int CONF_WEB_SERVER = 2; // Default Running Webserver (1 = Unicorn, 2 = Puma / default)
// Repository
char REPO_NAME[1024] = "[email protected]:zeroc0d3/ruby-deploy-gcc.git";
char REPO_BRANCH[64] = "master";
// Shared Folders
char *LIST_SHARED_FOLDERS[] = {
"log",
"public/assets",
"public/system",
"public/uploads",
"tmp/cache",
"tmp/pids",
"tmp/sockets",
"vendor/bundle",
NULL
};
// Shared Files
char *LIST_SHARED_FILES[] = {
".env",
"faye.ru",
"config.ru",
"config/application.yml",
"config/database.yml",
"config/mongoid.yml",
"config/pushr-staging.yml",
"config/pushr-production.yml",
"config/secrets.yml",
"config/sidekiq.yml",
"config/puma/staging.rb",
"config/puma/production.rb",
"config/unicorn/staging.rb",
"config/unicorn/production.rb",
NULL
};
/* =======================================
ENVIRONMENT CONFIGURATION
======================================= */
// DEVELOPMENT CONFIGURATION //
// Development Environment
char DEV_APP_ROOT[512] = "/home/deploy/zeroc0d3lab.dev"; // Development Root Path
char DEV_CONFIG_FAYE[512] = "/home/deploy/zeroc0d3lab.dev/current/faye.ru"; // Development Faye Config
char DEV_CONFIG_PUSHR[512] = "/home/deploy/zeroc0d3lab.dev/current/config/pushr-staging.yaml"; // Development Pushr Config
char DEV_CONFIG_SIDEKIQ[512] = "/home/deploy/zeroc0d3lab.dev/current/config/sidekiq.yml"; // Development Sidekiq Config
char DEV_CONFIG_UNICORN[512] = "/home/deploy/zeroc0d3lab.dev/current/config/unicorn/staging.rb"; // Development Unicorn Config
char DEV_CONFIG_PUMA[512] = "/home/deploy/zeroc0d3lab.dev/current/config/puma/staging.rb"; // Development Puma Config
char DEV_CONFIG_RU[512] = "/home/deploy/zeroc0d3lab.dev/current/config/config.ru"; // Development config.ru
char DEV_PID_FAYE[512] = "/home/deploy/zeroc0d3lab.dev/current/tmp/pids/faye.pid"; // Development Path PID Faye
char DEV_PID_PUSHR[512] = "/home/deploy/zeroc0d3lab.dev/current/tmp/pids/pushr.pid"; // Development Path PID Pushr
char DEV_PID_PUSH_GCM[512] = "/home/deploy/zeroc0d3lab.dev/current/tmp/pids/push_gcm.pid"; // Development Path PID GCM (Google Cloud Messaging)
char DEV_PID_SIDEKIQ[512] = "/home/deploy/zeroc0d3lab.dev/current/tmp/pids/sidekiq.pid"; // Development Path PID Sidekiq
char DEV_PID_UNICORN[512] = "/home/deploy/zeroc0d3lab.dev/current/tmp/pids/unicorn.pid"; // Development Path PID Unicorn
char DEV_PID_PUMA[512] = "/home/deploy/zeroc0d3lab.dev/current/tmp/pids/puma.pid"; // Development Path PID Puma
char DEV_PID_STATE_PUMA[512] = "/home/deploy/zeroc0d3lab.dev/current/tmp/pids/puma.state"; // Development Path PID State Puma
char DEV_LOG_ENV[512] = "/home/deploy/zeroc0d3lab.dev/current/log/staging.log"; // Development Path Log Environment
char DEV_LOG_PUSHR[512] = "/home/deploy/zeroc0d3lab.dev/current/log/pushr.log"; // Development Path Log Pushr
char DEV_LOG_SIDEKIQ[512] = "/home/deploy/zeroc0d3lab.dev/current/log/sidekiq.log"; // Development Path Log Sidekiq
char DEV_LOG_UNICORN[512] = "/home/deploy/zeroc0d3lab.dev/current/log/unicorn.log"; // Development Path Log Unicorn
char DEV_LOG_PUMA[512] = "/home/deploy/zeroc0d3lab.dev/current/log/puma.log"; // Development Path Log Puma
char DEV_LOG_PUSH_GCM[512] = "/home/deploy/zeroc0d3lab.dev/current/log/push_gcm.log"; // Development Path Log Push GCM (Google Cloud Messaging Notification for Mobile)
char DEV_PATH_BUNDLE[512] = "/home/deploy/.rbenv/shims/bundle"; // Development Path of Bundle Binary
char DEV_PATH_GEM[512] = "/home/deploy/.rbenv/shims/gem"; // Development Path of Gem Binary
char DEV_PATH_RACKUP[512] = "/home/deploy/.rbenv/shims/rackup"; // Development Path of Rackup Binary
// >= Rails v5.0
char DEV_PATH_RAILS[512] = "/home/deploy/.rbenv/shims/rails"; // Development Path of Rails Binary
// < Rails v5.0
char DEV_PATH_RAKE[512] = "/home/deploy/.rbenv/shims/rake"; // Development Path of Rake Binary
char DEV_PATH_PUSHR[512] = "/home/deploy/.rbenv/shims/pushr"; // Development Path of Pushr Binary
char DEV_PATH_PUSH_GCM[512] = "/home/deploy/.rbenv/shims/push"; // Development Path of Push GCM Binary
char DEV_PATH_SIDEKIQ[512] = "/home/deploy/.rbenv/shims/sidekiq"; // Development Path of Sidekiq Binary
char DEV_PATH_UNICORN[512] = "/home/deploy/.rbenv/shims/unicorn"; // Development Path of Unicorn Binary
char DEV_PATH_PUMA[512] = "/home/deploy/.rbenv/shims/puma"; // Development Path of Puma Binary
// PRODUCTION CONFIGURATION //
// Production Environment
char PROD_APP_ROOT[512] = "/home/deploy/zeroc0d3lab"; // Production Root Path
char PROD_CONFIG_FAYE[512] = "/home/deploy/zeroc0d3lab/current/faye.ru"; // Production Faye Config
char PROD_CONFIG_PUSHR[512] = "/home/deploy/zeroc0d3lab/current/config/pushr-production.yaml"; // Production Pushr Config
char PROD_CONFIG_SIDEKIQ[512] = "/home/deploy/zeroc0d3lab/current/config/sidekiq.yml"; // Production Sidekiq Config
char PROD_CONFIG_UNICORN[512] = "/home/deploy/zeroc0d3lab/current/config/unicorn/production.rb"; // Production Unicorn Config
char PROD_CONFIG_PUMA[512] = "/home/deploy/zeroc0d3lab/current/config/puma/production.rb"; // Production Puma Config
char PROD_CONFIG_RU[512] = "/home/deploy/zeroc0d3lab/current/config/config.ru"; // Production config.ru
char PROD_PID_FAYE[512] = "/home/deploy/zeroc0d3lab/current/tmp/pids/faye.pid"; // Production Path PID Faye
char PROD_PID_PUSHR[512] = "/home/deploy/zeroc0d3lab/current/tmp/pids/pushr.pid"; // Production Path PID Pushr
char PROD_PID_PUSH_GCM[512] = "/home/deploy/zeroc0d3lab/current/tmp/pids/push_gcm.pid"; // Production Path PID GCM (Google Cloud Messaging)
char PROD_PID_SIDEKIQ[512] = "/home/deploy/zeroc0d3lab/current/tmp/pids/sidekiq.pid"; // Production Path PID Sidekiq
char PROD_PID_UNICORN[512] = "/home/deploy/zeroc0d3lab/current/tmp/pids/unicorn.pid"; // Production Path PID Unicorn
char PROD_PID_PUMA[512] = "/home/deploy/zeroc0d3lab/current/tmp/pids/puma.pid"; // Production Path PID Puma
char PROD_PID_STATE_PUMA[512] = "/home/deploy/zeroc0d3lab/current/tmp/pids/puma.state"; // Production Path PID State Puma
char PROD_LOG_ENV[512] = "/home/deploy/zeroc0d3lab/current/log/production.log"; // Production Path Log Environment
char PROD_LOG_PUSHR[512] = "/home/deploy/zeroc0d3lab/current/log/pushr.log"; // Production Path Log Pushr
char PROD_LOG_SIDEKIQ[512] = "/home/deploy/zeroc0d3lab/current/log/sidekiq.log"; // Production Path Log Sidekiq
char PROD_LOG_UNICORN[512] = "/home/deploy/zeroc0d3lab/current/log/unicorn.log"; // Production Path Log Unicorn
char PROD_LOG_PUMA[512] = "/home/deploy/zeroc0d3lab/current/log/puma.log"; // Production Path Log Unicorn
char PROD_LOG_PUSH_GCM[512] = "/home/deploy/zeroc0d3lab/current/log/push_gcm.log"; // Production Path Log Push GCM (Google Cloud Messaging Notification for Mobile)
char PROD_PATH_BUNDLE[512] = "/home/deploy/.rbenv/shims/bundle"; // Production Path of Bundle Binary
char PROD_PATH_GEM[512] = "/home/deploy/.rbenv/shims/gem"; // Production Path of Gem Binary
char PROD_PATH_RACKUP[512] = "/home/deploy/.rbenv/shims/rackup"; // Production Path of Rackup Binary
// >= Rails v5.0
char PROD_PATH_RAILS[512] = "/home/deploy/.rbenv/shims/rails"; // Production Path of Rails Binary
// < Rails v5.0
char PROD_PATH_RAKE[512] = "/home/deploy/.rbenv/shims/rake"; // Production Path of Rake Binary
char PROD_PATH_PUSHR[512] = "/home/deploy/.rbenv/shims/pushr"; // Production Path of Pushr Binary
char PROD_PATH_PUSH_GCM[512] = "/home/deploy/.rbenv/shims/push"; // Production Path of Push GCM Binary
char PROD_PATH_SIDEKIQ[512] = "/home/deploy/.rbenv/shims/sidekiq"; // Production Path of Sidekiq Binary
char PROD_PATH_UNICORN[512] = "/home/deploy/.rbenv/shims/unicorn"; // Production Path of Unicorn Binary
char PROD_PATH_PUMA[512] = "/home/deploy/.rbenv/shims/puma"; // Production Path of Puma Binary
/* =======================================
SYSTEM CONFIGURATION
======================================= */
char VERSION[16] = "1.5.2"; // Version
char APP_ROOT[512]; // Root Path
char APP_CURRENT[64] = "current"; // Current Folder
char APP_RELEASE[64] = "release"; // Release Folder
char APP_SHARED[64] = "shared"; // Shared Folder
char CURRENT_FOLDER[1024]; // CURRENT_FOLDER = APP_ROOT/APP_CURRENT
char SHARED_FOLDER[1024]; // SHARED_FOLDER = APP_ROOT/APP_SHARED
char PREINSTALL[64] = "preinstall.sh"; // Preinstallation Script Before Server-Up
char AFTERINSTALL[64] = "afterinstall.sh"; // Preinstallation Script Before Server-Up
int DEBUG_LINE = 1; // Debug Version (Show Command), (0 = disable, 1 = enable)
int LOG_LINE = 1; // Add Logfile (Log All Command), (0 = disable, 1 = enable)
char LOGFILE[64] = "rb_deploy.log"; // Logfile Deploy
// GENERAL CONFIGURATION //
// Config
char CONFIG_FAYE[512]; // Faye Config
char CONFIG_PUMA[512]; // Puma Config
char CONFIG_PUSHR[512]; // Pushr Config
char CONFIG_RU[512]; // config.ru Config
char CONFIG_SIDEKIQ[512]; // Sidekiq Config
char CONFIG_UNICORN[512]; // Unicorn Config
// PID
char PID_FAYE[512]; // Path PID Faye
char PID_PUMA[512]; // Path PID Puma
char PID_STATE_PUMA[512]; // Path PID State Puma
char PID_PUSHR[512]; // Path PID Pushr
char PID_PUSH_GCM[512]; // Path PID Push GCM (Google Cloud Messaging)
char PID_SIDEKIQ[512]; // Path PID Sidekiq
char PID_UNICORN[512]; // Path PID Unicorn
// Binary
char PATH_BUNDLE[512]; // Path of Bundle Binary
char PATH_GEM[512]; // Path of Gem Binary
char PATH_RACKUP[512]; // Path of Rackup Binary
char PATH_RAILS[512]; // Path of Rails Binary
char PATH_RAKE[512]; // Path of Rake Binary
char PATH_PUMA[512]; // Path of Puma Binary
char PATH_PUSHR[512]; // Path of Pushr Binary
char PATH_PUSH_GCM[512]; // Path of Push GCM (Google Cloud Messaging)
char PATH_SIDEKIQ[512]; // Path of Sidekiq Binary
char PATH_UNICORN[512]; // Path of Unicorn Binary
// Log
char SYS_LOG_ENV[512]; // Path Log Environment
char SYS_LOG_PUMA[512]; // Path Log Puma
char SYS_LOG_PUSHR[512]; // Path Log Pushr
char SYS_LOG_PUSH_GCM[512]; // Path Log Push GCM (Google Cloud Messaging)
char SYS_LOG_SIDEKIQ[512]; // Path Log Sidekiq
char SYS_LOG_UNICORN[512]; // Path Log Unicorn
char SYS_LOG_NGINX_ERROR[512] = "/var/log/nginx/error.log"; // Path Log NGINX Error
char SYS_LOG_NGINX_ACCESS[512] = "/var/log/nginx/access.log"; // Path Log NGINX Access
char SYS_LOG_MONGODB[512] = "/var/log/mongodb.log"; // Path Log MongoDB
char SYS_LOG_MEMCACHED[521] = "/var/log/memcached.log"; // Path Log Memcached
char SYS_LOG_REDIS[521] = "/var/log/redis/redis-server.log"; // Path Log Redis
/* =======================================
SUB MAIN PROGRAM
======================================= */
char *trim(char *str)
{
size_t len = 0;
char *frontp = str;
char *endp = NULL;
if (str == NULL) {
return NULL;
}
if (str[0] == '\0') {
return str;
}
len = strlen(str);
endp = str + len;
/* Move the front and back pointers to address the first non-whitespace
* characters from each end.
*/
while (isspace((unsigned char)*frontp)) {
++frontp;
}
if (endp != frontp) {
while (isspace((unsigned char)*(--endp)) && endp != frontp) {
}
}
if (str + len - 1 != endp)
*(endp + 1) = '\0';
else if (frontp != str && endp == frontp)
*str = '\0';
/* Shift the string so that it starts at str so that if it's dynamically
* allocated, we can still free it on the returned pointer. Note the reuse
* of endp to mean the front of the string buffer now.
*/
endp = str;
if (frontp != str) {
while (*frontp) {
*endp++ = *frontp++;
}
*endp = '\0';
}
return str;
}
void get_time()
{
char buff[100];
time_t now = time(0);
strftime(buff, 100, "%Y-%m-%d %H:%M:%S", localtime(&now));
sprintf(DATE_TIME, "%s", buff);
}
void get_folder()
{
char buff[100];
int buff_unix;
time_t now = time(0);
buff_unix = (int)time(NULL);
strftime(buff, 100, "%Y%m%d%H%M", localtime(&now));
// Unix Timestamp
// sprintf(SNAP_FOLDER, "%d", buff_unix);
// Full Timestamp
sprintf(SNAP_FOLDER, "%s_%d", buff, buff_unix);
}
void get_folder_release()
{
sprintf(SNAP_FOLDER_RELEASE, "%s/%s/%s", APP_ROOT, APP_RELEASE, SNAP_FOLDER);
}
void get_folder_current()
{
sprintf(CURRENT_FOLDER, "%s/%s", APP_ROOT, APP_CURRENT);
}
void get_command(char *cmdRun)
{
if (DEBUG_LINE == 1) {
printf("\033[22;36m%s\n", cmdRun);
}
}
/* ---------------------------------------
Select Environment
--------------------------------------- */
void select_env()
{
int environment = strcmp(ENV, "production");
if (environment == 0)
{
sprintf(APP_ROOT, "%s", PROD_APP_ROOT);
sprintf(CONFIG_FAYE, "%s", PROD_CONFIG_FAYE);
sprintf(CONFIG_PUMA, "%s", PROD_CONFIG_PUMA);
sprintf(CONFIG_PUSHR, "%s", PROD_CONFIG_PUSHR);
sprintf(CONFIG_RU, "%s", PROD_CONFIG_RU);
sprintf(CONFIG_SIDEKIQ, "%s", PROD_CONFIG_SIDEKIQ);
sprintf(CONFIG_UNICORN, "%s", PROD_CONFIG_UNICORN);
sprintf(PID_FAYE, "%s", PROD_PID_FAYE);
sprintf(PID_PUMA, "%s", PROD_PID_PUMA);
sprintf(PID_STATE_PUMA, "%s", PROD_PID_STATE_PUMA);
sprintf(PID_PUSHR, "%s", PROD_PID_PUSHR);
sprintf(PID_PUSH_GCM, "%s", PROD_PID_PUSH_GCM);
sprintf(PID_SIDEKIQ, "%s", PROD_PID_SIDEKIQ);
sprintf(PID_UNICORN, "%s", PROD_PID_UNICORN);
sprintf(SYS_LOG_ENV, "%s", PROD_LOG_ENV);
sprintf(SYS_LOG_PUMA, "%s", PROD_LOG_PUMA);
sprintf(SYS_LOG_PUSHR, "%s", PROD_LOG_PUSHR);
sprintf(SYS_LOG_PUSH_GCM, "%s", PROD_LOG_PUSH_GCM);
sprintf(SYS_LOG_SIDEKIQ, "%s", PROD_LOG_SIDEKIQ);
sprintf(SYS_LOG_UNICORN, "%s", PROD_LOG_UNICORN);
sprintf(PATH_BUNDLE, "%s", PROD_PATH_BUNDLE);
sprintf(PATH_GEM, "%s", PROD_PATH_GEM);
sprintf(PATH_PUMA, "%s", PROD_PATH_PUMA);
sprintf(PATH_PUSHR, "%s", PROD_PATH_PUSHR);
sprintf(PATH_PUSH_GCM, "%s", PROD_PATH_PUSH_GCM);
sprintf(PATH_RACKUP, "%s", PROD_PATH_RACKUP);
sprintf(PATH_RAILS, "%s", PROD_PATH_RAILS);
sprintf(PATH_RAKE, "%s", PROD_PATH_RAKE);
sprintf(PATH_SIDEKIQ, "%s", PROD_PATH_SIDEKIQ);
sprintf(PATH_UNICORN, "%s", PROD_PATH_UNICORN);
}
else
{
sprintf(APP_ROOT, "%s", DEV_APP_ROOT);
sprintf(CONFIG_FAYE, "%s", DEV_CONFIG_FAYE);
sprintf(CONFIG_PUMA, "%s", DEV_CONFIG_PUMA);
sprintf(CONFIG_PUSHR, "%s", DEV_CONFIG_PUSHR);
sprintf(CONFIG_RU, "%s", DEV_CONFIG_RU);
sprintf(CONFIG_SIDEKIQ, "%s", DEV_CONFIG_SIDEKIQ);
sprintf(CONFIG_UNICORN, "%s", DEV_CONFIG_UNICORN);
sprintf(PID_FAYE, "%s", DEV_PID_FAYE);
sprintf(PID_PUMA, "%s", DEV_PID_PUMA);
sprintf(PID_STATE_PUMA, "%s", DEV_PID_STATE_PUMA);
sprintf(PID_PUSHR, "%s", DEV_PID_PUSHR);
sprintf(PID_PUSH_GCM, "%s", DEV_PID_PUSH_GCM);
sprintf(PID_SIDEKIQ, "%s", DEV_PID_SIDEKIQ);
sprintf(PID_UNICORN, "%s", DEV_PID_UNICORN);
sprintf(SYS_LOG_ENV, "%s", DEV_LOG_ENV);
sprintf(SYS_LOG_PUMA, "%s", DEV_LOG_PUMA);
sprintf(SYS_LOG_PUSHR, "%s", DEV_LOG_PUSHR);
sprintf(SYS_LOG_PUSH_GCM, "%s", DEV_LOG_PUSH_GCM);
sprintf(SYS_LOG_SIDEKIQ, "%s", DEV_LOG_SIDEKIQ);
sprintf(SYS_LOG_UNICORN, "%s", DEV_LOG_UNICORN);
sprintf(PATH_BUNDLE, "%s", DEV_PATH_BUNDLE);
sprintf(PATH_GEM, "%s", DEV_PATH_GEM);
sprintf(PATH_PUMA, "%s", DEV_PATH_PUMA);
sprintf(PATH_PUSHR, "%s", DEV_PATH_PUSHR);
sprintf(PATH_PUSH_GCM, "%s", DEV_PATH_PUSH_GCM);
sprintf(PATH_RACKUP, "%s", DEV_PATH_RACKUP);
sprintf(PATH_RAILS, "%s", DEV_PATH_RAILS);
sprintf(PATH_RAKE, "%s", DEV_PATH_RAKE);
sprintf(PATH_SIDEKIQ, "%s", DEV_PATH_SIDEKIQ);
sprintf(PATH_UNICORN, "%s", DEV_PATH_UNICORN);
}
}
void logo()
{
// printf("\033[22;32m===============================================================================\033[0m\n");
// printf("\033[22;31m __________ _________ _______ .___________ \033[0m\n");
// printf("\033[22;31m \____ /___________ ____ \_ ___ \\ _ \ __| _/\_____ \ LAB \033[0m\n");
// printf("\033[22;31m / // __ \_ __ \/ _ \/ \ \// /_\ \ / __ | _(__ < \033[0m\n");
// printf("\033[22;31m / /\ ___/| | \( <_> ) \___\ \_/ \/ /_/ | / \ \033[0m\n");
// printf("\033[22;31m /_______ \___ >__| \____/ \______ /\_____ /\____ | /______ / \033[0m\n");
// printf("\033[22;31m \/ \/ \/ \/ \/ \/ \033[0m\n");
printf("\033[22;32m===============================================================================\033[0m\n");
printf("\033[22;34m ZeroC0D3 Ruby Deploy :: ver-%s \033[0m\n", VERSION);
printf("\033[22;34m (c) 2017-2018 ZeroC0D3 Team \033[0m\n");
}
void header()
{
system("clear");
logo();
printf("\033[22;32m===============================================================================\033[0m\n");
get_time();
printf("\033[22;37m# BEGIN PROCESS..... (Please Wait) \033[0m\n");
printf("\033[22;33m# Start at: %s \033[0m\n", DATE_TIME);
}
void footer()
{
get_time();
printf("\033[22;32m===============================================================================\033[0m\n");
printf("\033[22;33m# Finish at: %s \033[0m\n", DATE_TIME);
printf("\033[22;37m# END PROCESS..... \033[0m\n\n");
}
/* =======================================
MENU INITIALIZE
======================================= */
/* ---------------------------------------
Core Menus
--------------------------------------- */
void menu_environment()
{
printf("\033[22;34m # ENVIRONMENT : \033[22;32m%s \033[0m\n", ENV);
printf("\033[22;34m # PATH ROOT : \033[22;32m%s \033[0m\n", APP_ROOT);
printf("\033[22;34m # RAILS VERSION : \033[22;32m%d \033[0m\n", RAILS_VERSION);
if (CONF_WEB_SERVER == 1) {
printf("\033[22;34m # WEB SERVER : \033[22;32mUNICORN \033[0m\n");
} else {
printf("\033[22;34m # WEB SERVER : \033[22;32mPUMA \033[0m\n");
}
}
void menu_nginx()
{
printf("\033[22;34m # ./rb_deploy -no --> Reload NGINX \033[0m\n");
printf("\033[22;34m # ./rb_deploy -nr --> Restart NGINX \033[0m\n");
}
void menu_assets()
{
printf("\033[22;34m # ./rb_deploy -ac --> Assets Clobber (Rollback) \033[0m\n");
printf("\033[22;34m # ./rb_deploy -ap --> Assets Precompile \033[0m\n");
}
/* ---------------------------------------
Restart Service Menus
--------------------------------------- */
void menu_restart_faye()
{
if (ENABLE_FAYE_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -rf --> Restart Faye \033[0m\n");
}
}
void menu_restart_push_gcm()
{
if (ENABLE_PUSHGCM_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -rg --> Restart Push GCM (Google Cloud Messaging) \033[0m\n");
}
}
void menu_restart_mongodb()
{
if (ENABLE_MONGODB_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -rm --> Restart MongoDB \033[0m\n");
}
}
void menu_restart_pushr()
{
if (ENABLE_PUSHR_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -rp --> Restart Pushr \033[0m\n");
}
}
void menu_restart_sidekiq()
{
if (ENABLE_SIDEKIQ_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -rq --> Restart Sidekiq \033[0m\n");
}
}
void menu_restart_redis()
{
if (ENABLE_REDIS_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -rs --> Restart Redis \033[0m\n");
}
}
void menu_restart_web_service()
{
if (CONF_WEB_SERVER == 1) {
printf("\033[22;34m # ./rb_deploy -ru --> Restart Unicorn \033[0m\n");
} else {
printf("\033[22;34m # ./rb_deploy -ru --> Restart Puma \033[0m\n");
}
}
void menu_restart_services()
{
menu_restart_faye();
menu_restart_push_gcm();
menu_restart_mongodb();
menu_restart_pushr();
menu_restart_sidekiq();
menu_restart_redis();
menu_restart_web_service();
}
/* ---------------------------------------
Stop Service Menus
--------------------------------------- */
void menu_stop_faye()
{
if (ENABLE_FAYE_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -df --> Stop Faye \033[0m\n");
}
}
void menu_stop_push_gcm()
{
if (ENABLE_PUSHGCM_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -dg --> Stop Push GCM (Google Cloud Messaging) \033[0m\n");
}
}
void menu_stop_mongodb()
{
if (ENABLE_MONGODB_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -dm --> Stop MongoDB \033[0m\n");
}
}
void menu_stop_pushr()
{
if (ENABLE_PUSHR_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -dp --> Stop Pushr \033[0m\n");
}
}
void menu_stop_sidekiq()
{
if (ENABLE_SIDEKIQ_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -dq --> Stop Sidekiq \033[0m\n");
}
}
void menu_stop_redis()
{
if (ENABLE_REDIS_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -ds --> Stop Redis \033[0m\n");
}
}
void menu_stop_web_service()
{
if (CONF_WEB_SERVER == 1) {
printf("\033[22;34m # ./rb_deploy -du --> Stop Unicorn \033[0m\n");
} else {
printf("\033[22;34m # ./rb_deploy -du --> Stop Puma \033[0m\n");
}
}
void menu_stop_services()
{
menu_stop_faye();
menu_stop_push_gcm();
menu_stop_mongodb();
menu_stop_pushr();
menu_stop_sidekiq();
menu_stop_redis();
menu_stop_web_service();
}
/* ---------------------------------------
View Log Menus
--------------------------------------- */
void menu_view_log_environment()
{
printf("\033[22;34m # ./rb_deploy -l-env --> View Environment's Log \033[0m\n");
}
void menu_view_log_push_gcm()
{
if (ENABLE_PUSHGCM_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -l-gcm --> View Push GCM Log \033[0m\n");
}
}
void menu_view_log_memcached()
{
printf("\033[22;34m # ./rb_deploy -l-memcached --> View Memcached Log \033[0m\n");
}
void menu_view_log_mongodb()
{
if (ENABLE_MONGODB_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -l-mongodb --> View MongoDB Log \033[0m\n");
}
}
void menu_view_log_pushr()
{
if (ENABLE_PUSHR_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -l-pushr --> View Pushr Log \033[0m\n");
}
}
void menu_view_log_redis()
{
if (ENABLE_REDIS_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -l-redis --> View Redis Log \033[0m\n");
}
}
void menu_view_log_sidekiq()
{
if (ENABLE_SIDEKIQ_SERVICE == 1) {
printf("\033[22;34m # ./rb_deploy -l-sidekiq --> View Sidekiq Log \033[0m\n");
}
}
void menu_view_log_web_service()
{
if (CONF_WEB_SERVER == 1) {
printf("\033[22;34m # ./rb_deploy -l-unicorn --> View Unicorn Log \033[0m\n");
} else {
printf("\033[22;34m # ./rb_deploy -l-puma --> View Puma Log \033[0m\n");
}
}
void menu_view_log_nginx()
{
printf("\033[22;34m # ./rb_deploy -la-nginx --> View NGINX Access Log \033[0m\n");
printf("\033[22;34m # ./rb_deploy -le-nginx --> View NGINX Error Log \033[0m\n");
}
void menu_view_logs()
{
menu_view_log_environment();
menu_view_log_push_gcm();
menu_view_log_memcached();
menu_view_log_mongodb();
menu_view_log_pushr();
menu_view_log_redis();
menu_view_log_web_service();
menu_view_log_nginx();
}
/* ---------------------------------------
Utilities Server Menus
--------------------------------------- */
void menu_utilities()
{
printf("\033[22;34m # ./rb_deploy -key --> Generate Secret Token \033[0m\n");
printf("\033[22;34m # ./rb_deploy -up --> Server Up \033[0m\n");
printf("\033[22;34m # ./rb_deploy -down --> Server Down \033[0m\n");
printf("\033[22;34m # ./rb_deploy -deploy / dep --> Running Deploy \033[0m\n");
}
/* ---------------------------------------
Main Menu
--------------------------------------- */
void menu()
{
select_env();
system("clear");
logo();
printf("\033[22;32m===============================================================================\033[0m\n");
printf("\033[22;32m ### ENVIRONMENT DEPLOY ### \033[0m\n");
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
menu_environment();
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
printf("\033[22;32m ### NGINX SERVICES ### \033[0m\n");
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
menu_nginx();
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
printf("\033[22;32m ### ASSETS SERVICES ### \033[0m\n");
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
menu_assets();
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
printf("\033[22;32m ### RESTART SERVICES ### \033[0m\n");
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
menu_restart_services();
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
printf("\033[22;32m ### STOP SERVICES ### \033[0m\n");
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
menu_stop_services();
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
printf("\033[22;32m ### VIEW LOGS ### \033[0m\n");
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
menu_view_logs();
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
printf("\033[22;32m ### SERVER ### \033[0m\n");
printf("\033[22;32m-------------------------------------------------------------------------------\033[0m\n");
menu_utilities();
printf("\033[22;32m===============================================================================\033[0m\n\n");
}
/* ---------------------------------------
Running Command
--------------------------------------- */
void message_service(char STR_DESCRIPTION[512])
{
get_time();
printf("\n\033[22;34m[ %s ] ##### %s \033[0m\n", DATE_TIME, STR_DESCRIPTION);
}
void message_ok(char STR_SERVICE[512])
{
get_time();
printf("\n\033[22;32m[ %s ] :: [ ✔ ] \033[0m", DATE_TIME);
printf("\033[22;32m %s \033[0m\n", STR_SERVICE);
}
void message_error(char STR_SERVICE[512])
{
get_time();
printf("\n\033[22;31m[ %s ] :: [ ✘ ] \033[0m", DATE_TIME);
printf("\033[22;32m %s \033[0m\n", STR_SERVICE);
}
void run_fastcmd(char STR_COMMAND[1024])
{
get_command(STR_COMMAND);
sprintf(cmdRun, "%s", STR_COMMAND);
ret = system(cmdRun);
}
void run_cmd(char STR_SERVICE[512],
char STR_DESCRIPTION[512],
char STR_COMMAND[1024])
{
message_service(STR_DESCRIPTION);
get_command(STR_COMMAND);
sprintf(cmdRun, "%s", STR_COMMAND);
ret = system(cmdRun);
if (!ret) {
IS_ERROR_DEPLOY = 0;
message_ok(STR_SERVICE);
} else {
IS_ERROR_DEPLOY = 1;
message_error(STR_SERVICE);
}
/* --------------------------------- *
* For Test Only *
* --------------------------------- */
//message_ok(STR_SERVICE);
}
void run_single(char STR_SERVICE[512],
char STR_DESCRIPTION[512],
char STR_COMMAND[1024])
{
message_service(STR_DESCRIPTION);
get_command(STR_COMMAND);
sprintf(cmdRun, "%s", STR_COMMAND);
system(cmdRun);
message_ok(STR_SERVICE);
}
/* ---------------------------------------
NGINX
--------------------------------------- */
void nginx_restart()
{
select_env();
char STR_DESCRIPTION[512] = "Restart NGINX Service";
char STR_SERVICE[512] = "NGINX Restarting...";
char STR_COMMAND[1024] = "sudo /etc/init.d/nginx restart";
header();
run_cmd(STR_SERVICE, STR_DESCRIPTION, STR_COMMAND);
sleep(1);
footer();
}
void nginx_reload()
{
select_env();
char STR_DESCRIPTION[512] = "Reload NGINX Service";
char STR_SERVICE[512] = "NGINX Reloading...";
char STR_COMMAND[1024] = "sudo /etc/init.d/nginx reload";
header();
run_cmd(STR_SERVICE, STR_DESCRIPTION, STR_COMMAND);
sleep(1);
footer();
}
/* ---------------------------------------
MONGODB
--------------------------------------- */
void kill_mongodb()
{
char STR_DESCRIPTION[512] = "Stop MongoDB Service";
char STR_SERVICE[512] = "MongoDB Stop...";
char STR_COMMAND[1024] = "ps aux | grep -i mongod | awk {'print $2'} | sudo xargs kill -9";
run_single(STR_SERVICE, STR_DESCRIPTION, STR_COMMAND);
sleep(1);
}
void run_mongodb()
{
select_env();
char STR_DESCRIPTION[512] = "Start MongoDB Service";
char STR_SERVICE[512] = "MongoDB Start...";
char STR_COMMAND[1024];
// Goto App Current Folder
get_folder_current();
sprintf(STR_COMMAND, "cd %s; sudo mongod --fork --logpath %s", APP_CURRENT, SYS_LOG_MONGODB);
run_cmd(STR_SERVICE, STR_DESCRIPTION, STR_COMMAND);
sleep(1);
}
void restart_mongodb_process()
{
kill_mongodb();
run_mongodb();
}
void restart_mongodb()
{
header();
restart_mongodb_process();
footer();
}
void stop_mongodb()
{
header();
kill_mongodb();
footer();
}
/* ---------------------------------------
REDIS
--------------------------------------- */
void kill_redis()
{
char STR_DESCRIPTION[512] = "Stop Redis Service";
char STR_SERVICE[512] = "Redis Stop...";
char STR_COMMAND[1024] = "ps aux | grep -i redis-server | awk {'print $2'} | sudo xargs kill -9";
run_single(STR_SERVICE, STR_DESCRIPTION, STR_COMMAND);
sleep(1);
}
void run_redis()
{
select_env();
char STR_DESCRIPTION[512] = "Start Redis Service (Daemonize)";
char STR_SERVICE[512] = "Redis Start...";
char STR_COMMAND[1024] = "redis-server --daemonize yes";
run_cmd(STR_SERVICE, STR_DESCRIPTION, STR_COMMAND);
sleep(1);
}
void restart_redis_process()
{
kill_redis();
run_redis();
}
void restart_redis()
{
header();
restart_redis_process();
footer();
}
void stop_redis()
{
header();
kill_redis();
footer();
}
/* ---------------------------------------
Assets
--------------------------------------- */
void asset_precompile_process()
{
select_env();
char STR_DESCRIPTION[512] = "Precompile Assets";
char STR_SERVICE[512] = "Precompiling All Assets...";
char STR_COMMAND[1024];
// Goto App Release Folder
get_folder_release();
if (RAILS_VERSION >= 5)
{
// Run: bundle exec rake assets:precompile RAILS_ENV=[environment]
sprintf(STR_COMMAND, "cd %s; %s exec %s assets:precompile RAILS_ENV=%s --trace", SNAP_FOLDER_RELEASE, PATH_BUNDLE, PATH_RAILS, ENV);
}
else
{
// Run: bundle exec rails assets:precompile RAILS_ENV=[environment]
sprintf(STR_COMMAND, "cd %s; %s exec %s assets:precompile RAILS_ENV=%s --trace", SNAP_FOLDER_RELEASE, PATH_BUNDLE, PATH_RAKE, ENV);
}
run_cmd(STR_SERVICE, STR_DESCRIPTION, STR_COMMAND);
sleep(1);
}
void asset_precompile()
{
header();
asset_precompile_process();
footer();
}
void asset_rollback_process()
{
select_env();
char STR_DESCRIPTION[512] = "Rollback Assets";
char STR_SERVICE[512] = "Rollingback (Cleanup) All Assets...";
char STR_COMMAND[1024];
// Goto App Release Folder
get_folder_release();
if (RAILS_VERSION >= 5)
{
// Run: bundle exec rake assets:clobber RAILS_ENV=[environment]
sprintf(STR_COMMAND, "cd %s; %s exec %s assets:clobber RAILS_ENV=%s --trace", SNAP_FOLDER_RELEASE, PATH_BUNDLE, PATH_RAILS, ENV);
}
else
{
// Run: bundle exec rails assets:clobber RAILS_ENV=[environment]
sprintf(STR_COMMAND, "cd %s; %s exec %s assets:clobber RAILS_ENV=%s --trace", SNAP_FOLDER_RELEASE, PATH_BUNDLE, PATH_RAKE, ENV);
}
run_cmd(STR_SERVICE, STR_DESCRIPTION, STR_COMMAND);
sleep(1);
}
void asset_rollback()
{
header();
asset_rollback_process();
footer();
}
/* ---------------------------------------
Puma
--------------------------------------- */
void kill_puma()
{
select_env();
char STR_DESCRIPTION[512] = "Stop Puma Service";
char STR_SERVICE[512] = "Puma Terminated...";
char STR_COMMAND[1024] = "ps aux | grep -i puma | awk {'print $2'} | sudo xargs kill -9";
run_single(STR_SERVICE, STR_DESCRIPTION, STR_COMMAND);
sleep(1);
}
void run_puma()
{
select_env();
char STR_DESCRIPTION[512] = "Run Puma Service";
char STR_SERVICE[512] = "Puma Running...";
char STR_COMMAND[1024];
// Goto App Current Folder
get_folder_current();
// Run: bundle exec puma -D -c [config_puma] -E [environment]
//sprintf(STR_COMMAND, "cd %s; %s exec %s -C %s RAILS_ENV=%s", CURRENT_FOLDER, PATH_BUNDLE, PATH_PUMA, CONFIG_PUMA, ENV);
sprintf(STR_COMMAND, "cd %s; RAILS_ENV=%s %s exec %s -C %s %s", CURRENT_FOLDER, ENV, PATH_BUNDLE, PATH_PUMA, CONFIG_PUMA, CONFIG_RU);
run_cmd(STR_SERVICE, STR_DESCRIPTION, STR_COMMAND);
sleep(1);
}
void restart_puma_process()
{
kill_puma();
run_puma();
}
void restart_puma()
{
header();
restart_puma_process();
footer();
}
void stop_puma()
{
header();
kill_puma();
footer();
}
/* ---------------------------------------
Unicorn
--------------------------------------- */
void kill_unicorn()
{
select_env();
char STR_DESCRIPTION[512] = "Stop Unicorn Service";
char STR_SERVICE[512] = "Unicorn Terminated...";
char STR_COMMAND[1024] = "ps aux | grep -i unicorn | awk {'print $2'} | sudo xargs kill -9";