-
Notifications
You must be signed in to change notification settings - Fork 0
/
exerciser.sh
executable file
·1484 lines (1020 loc) · 39.6 KB
/
exerciser.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
#!/usr/bin/env bash
###################
# set -e;
export step0_BEGIN_BY_CLEANING=0;
export step1_ONCE_ONLY_INITIALIZATIONS=$((${step0_BEGIN_BY_CLEANING}+1));
export step2_PROJECT_INITIALIZATIONS=$((${step1_ONCE_ONLY_INITIALIZATIONS}+1));
export step3_BUILD_AND_UPLOAD=$((${step2_PROJECT_INITIALIZATIONS}+1));
export step4_PREPARE_FOR_SSH_RPC=$((${step3_BUILD_AND_UPLOAD}+1));
export step5_INSTALL_SERVER_SCRIPTS=$((${step4_PREPARE_FOR_SSH_RPC}+1));
export step6_INITIATE_DEPLOY=$((${step5_INSTALL_SERVER_SCRIPTS}+1));
function InstalledAlready() {
declare INSTALLED_ALREADY=${1};
[ $(which ${INSTALLED_ALREADY}) ] && \
echo "'${INSTALLED_ALREADY}' is installed." || \
sudo apt-get -y install ${INSTALLED_ALREADY};
};
declare TEST_VARS_FILE="${HOME}/.h4mVars.sh";
## Preparing file of test variables for getting started with Habitat For Meteor
function PrepareNecessaryShellVarsForExerciser() {
if [ ! -f ${TEST_VARS_FILE} ]; then
cat << EOSVARS > ${TEST_VARS_FILE}
#################################################################################
##### Non-secret test variables for getting started with Habitat For Meteor #####
#################################################################################
### Controlling exerciser execution
# Specify whether the script should assume **NO MISSING DETAILS**
export NON_STOP="NO"; # don't ask for secrets and fail if not found
# Specify the execution stage you want to begin from
export EXECUTION_STAGE="step1_ONCE_ONLY_INITIALIZATIONS";
### Locating public files within the developer VM
# Location of developer tools
export HABITA4METEOR_PARENT_DIR="\${HOME}/tools";
# Location of your projects
export TARGET_PROJECT_PARENT_DIR="\${HOME}/projects";
# The SSH secrets directory
export SSH_KEY_PATH="\${HOME}/.ssh";
export SSH_CONFIG_FILE="\${SSH_KEY_PATH}/config";
# The SSH keys of the current user, for ssh-add
export SETUP_USER_UID="\$(whoami)";
export SETUP_USER_SSH_KEY_PRIV="\${SSH_KEY_PATH}/id_rsa";
export SETUP_USER_SSH_KEY_PUBL="\${SSH_KEY_PATH}/id_rsa.pub";
### Locating secrets within the developer VM
# Habitat for Meteor secrets directory
export HABITAT_FOR_METEOR_SECRETS_DIR="\${SSH_KEY_PATH}/hab_vault";
export SOURCE_SECRETS=;
export SOURCE_SECRETS_FILE="secrets.sh";
# export METEOR_SETTINGS=;
# export METEOR_SETTINGS_FILE="settings.json";
# export METEOR_SETTINGS_EXAMPLE_FILE="\${METEOR_SETTINGS_FILE}.example";
# export METEOR_SETTINGS_FILE_PATH="\${HABITAT_FOR_METEOR_SECRETS_DIR}/\${METEOR_SETTINGS_FILE}";
# Habitat for Meteor user secrets directory
export HABITAT_FOR_METEOR_USER_SECRETS_DIR=\${HABITAT_FOR_METEOR_SECRETS_DIR}/habitat_user;
### Specifying your fork of HabitatForMeteor ###
# Name of your HabitatForMeteorFork
export HABITA4METEOR_FORK_NAME="HabitatForMeteor";
# Organization of your HabitatForMeteorFork
export HABITA4METEOR_FORK_ORG="yourOrg";
# URI of your fork of HabitatForMeteor #
export URIofYourHabitatForMeteorFork="https://github.com/\${HABITA4METEOR_FORK_ORG}/\${HABITA4METEOR_FORK_NAME}";
### Parameters for creating a SSH key pair for the 'hab' user.
export HABITAT_USER_SSH_KEY_COMMENT="DevopsTeamLeader";
export HABITAT_USER_SSH_PASS_PHRASE="memorablegobbledygook";
export HABITAT_USER_SSH_KEY_PATH="\${HABITAT_FOR_METEOR_SECRETS_DIR}/habitat_user";
export HABITAT_USER_SSH_KEY_PRIV="\${HABITAT_USER_SSH_KEY_PATH}/id_rsa";
export HABITAT_USER_SSH_KEY_PUBL="\${HABITAT_USER_SSH_KEY_PATH}/id_rsa.pub";
### Specifying parameters for creating SSL certificates for your project domain ###
# SSL certificate parameters of the server where the project will be deployed
# Virtual host domain name
export VHOST_DOMAIN="moon.planet.sun";
export VHOST_CERT_PATH="\${HABITAT_FOR_METEOR_SECRETS_DIR}/\${VHOST_DOMAIN}";
export SOURCE_SECRETS_FILE_PATH="\${HABITAT_FOR_METEOR_SECRETS_DIR}/\${VHOST_DOMAIN}/\${SOURCE_SECRETS_FILE}";
export SOURCE_SECRETS_EXAMPLE_FILE="\${HABITAT_FOR_METEOR_SECRETS_DIR}/\${VHOST_DOMAIN}/\${SOURCE_SECRETS_FILE}.example";
# Virtual host domain name
export VHOST_CERT_PASSPHRASE="memorablegibberish";
export SOURCE_SECRETS_FILE="\${HABITAT_FOR_METEOR_SECRETS_DIR}/${VHOST_DOMAIN}/secrets.sh";
### Specifying your fork of a target project ###
# Name of your fork of one the target projects
export TARGET_PROJECT_NAME="";
TARGET_PROJECT_NAME="todos"; # git clone https://github.com/meteor/todos meteor_todos
# TARGET_PROJECT_NAME="mmks"; # git clone https://github.com/warehouseman/meteor-mantra-kickstarter mmks
# Your github organization for your fork of "todos" or "mmks"
export YOUR_ORG="yourse1f-yourorg";
# URI of your project fork #
export URIofYourProjectFork="https://github.com/\${YOUR_ORG}/\${TARGET_PROJECT_NAME}";
# The release tag you want to attach to the above project. It must be the
# newest release anywhere: locally, or on GitHub, or on apps.habitat.sh
export RELEASE_TAG="0.0.48";
### Specifying your public GitHub access ###
# Your full name
export YOUR_NAME="You Yourself";
# Your email address
export YOUR_EMAIL="[email protected]";
# Path to the SSH keys of the current user, for ssh-add
export YOUR_ORG_IDENTITY_FILE="\${SSH_KEY_PATH}/\${YOUR_ORG}_rsa";
### Specifying access parameters for your server VM ###
# Domain name of the server where the project will be deployed
export TARGET_SRVR="hab4metsrv";
# The 'habitat' admin account on the server
export HABITAT_USER="hab";
# Compatibility .....
export SETUP_USER="\${SETUP_USER_UID}";
# export METEOR_SETTINGS_FILE="\${METEOR_SETTINGS_FILE_PATH}";
export VIRTUAL_HOST_DOMAIN_NAME="\${VHOST_DOMAIN}";
export SOURCE_CERTS_DIR="\${HABITAT_FOR_METEOR_SECRETS_DIR}";
export YOUR_PKG="\${TARGET_PROJECT_NAME}";
EOSVARS
chmod 600 ${TEST_VARS_FILE};
echo -e "
The file of environment variables '${TEST_VARS_FILE}' has been generated.
Please edit as required, and then re-run this script with :
./exerciser.sh;
";
exit 0;
fi;
}
InstalledAlready "expect";
echo -e "
${PRTY} Prepare environment variables used by exerciser ";
PrepareNecessaryShellVarsForExerciser;
## Sourcing test variables file ....
source ${TEST_VARS_FILE};
source habitat/scripts/target/secrets.sh.example;
### secrets.sh provides obligatory secret settings for ...
# The password the Meteor app will use to connect to a localhost MongoDB
# The sudoer password for the account that will install Habitat
# The sudoer password to give the 'hab' user account when it is created
# The client-side path and filename of a key to be added to the remote host authorized_keys file
# The path on the remote server where Nginx should look for SSL cert passwords
# A string to insert in the Nginx config that will enable
# A string to insert in the Nginx config that will enable certificate password storage
# The paths to the location of your signed site certificates.
echo "YOUR_ORG_IDENTITY_FILE = ${YOUR_ORG_IDENTITY_FILE}";
#
export THE_PROJECT_ROOT=${TARGET_PROJECT_PARENT_DIR}/${TARGET_PROJECT_NAME};
export PROJECT_UUID=${YOUR_ORG}/${TARGET_PROJECT_NAME};
export PROJECT_HABITAT_DIR=${THE_PROJECT_ROOT}/.habitat;
export PROJECT_RELEASE_NOTES_DIR=${PROJECT_HABITAT_DIR}/release_notes;
export HABITA4METEOR_SOURCE_DIR=${HABITA4METEOR_PARENT_DIR}/${HABITA4METEOR_FORK_NAME}/habitat;
export HABITA4METEOR_SCRIPTS_DIR=${HABITA4METEOR_SOURCE_DIR}/scripts;
echo -e "
Test variables are ready for use:
* edit with 'nano ${TEST_VARS_FILE};'
* then re-source with 'source ${TEST_VARS_FILE};'
";
function startSSHAgent() {
if [ ! -S "${SSH_AUTH_SOCK}" ]; then
echo -e "${PRTY} Starting 'ssh-agent' because SSH_AUTH_SOCK='${SSH_AUTH_SOCK}'...";
eval $(ssh-agent -s);
echo -e "${PRTY} Started 'ssh-agent' ...";
fi;
};
function AddSSHkeyToAgent() {
local KEY_PRESENT=$(ssh-add -l | grep -c ${1});
if [[ "${KEY_PRESENT}" -lt "1" ]]; then
echo -e "${PRTY} Remembering SSH key: '${1}'...";
ssh-add ${1};
fi;
};
function ConfigureSSHConfigForUser() {
local THE_USER="${1}";
local THE_HOST="${2}";
local THE_KEYS="${3}";
echo -e "Preparing SSH config file for:
* user - '${THE_USER}'
* host - '${THE_HOST}'
* keys - '${THE_KEYS}'
";
if [ ! -f ${THE_KEYS} ]; then
echo -e "Unable to find SSH key at: '${THE_KEYS}' ... ";
exit 1;
fi;
chmod go-rwx ${THE_KEYS};
export PTRN="# ${THE_USER} account on ${THE_HOST}";
export PTRNB="${PTRN} «begins»";
export PTRNE="${PTRN} «ends»";
#
mkdir -p ${SSH_KEY_PATH};
touch ${SSH_CONFIG_FILE};
if [[ ! -f ${SSH_CONFIG_FILE}_BK ]]; then
cp ${SSH_CONFIG_FILE} ${SSH_CONFIG_FILE}_BK;
chmod ugo-w ${SSH_CONFIG_FILE}_BK;
fi;
sed -i "/${PTRNB}/,/${PTRNE}/d" ${SSH_CONFIG_FILE};
#
#
echo -e "${PTRNB}
Host ${THE_USER}.${THE_HOST}
HostName ${THE_HOST}
User ${THE_USER}
PreferredAuthentications publickey
IdentityFile ${THE_KEYS}
${PTRNE}
" >> ${SSH_CONFIG_FILE}
sed -i "s/ *$//" ${SSH_CONFIG_FILE}; # trim whitespace to EOL
sed -i "/^$/N;/^\n$/D" ${SSH_CONFIG_FILE}; # blank lines to 1 line
startSSHAgent;
AddSSHkeyToAgent ${THE_KEYS};
};
ConfigureSSHConfigForUser ${YOUR_ORG} github.com ${YOUR_ORG_IDENTITY_FILE};
# function ConfigureSSHConfigForHabitatUserIfNotDone() {
# echo -e "Preparing SSH config file.";
# SETUP_USER_UID=$(whoami);
# #
# export PTRN="# ${SETUP_USER_UID} account on ${TARGET_SRVR}";
# export PTRNB="${PTRN} «begins»";
# export PTRNE="${PTRN} «ends»";
# #
# mkdir -p ${SSH_KEY_PATH};
# touch ${SSH_CONFIG_FILE};
# if [[ ! -f ${SSH_CONFIG_FILE}_BK ]]; then
# cp ${SSH_CONFIG_FILE} ${SSH_CONFIG_FILE}_BK;
# chmod ugo-w ${SSH_CONFIG_FILE}_BK;
# fi;
# sed -i "/${PTRNB}/,/${PTRNE}/d" ${SSH_CONFIG_FILE};
# #
# echo -e "
# ${PTRNB}
# Host ${TARGET_SRVR}
# HostName ${TARGET_SRVR}
# User ${SETUP_USER_UID}
# PreferredAuthentications publickey
# IdentityFile ${SETUP_USER_SSH_KEY_PRIV}
# ${PTRNE}
# " >> ${SSH_CONFIG_FILE}
# HABITAT_USER_SSH_KEY_PRIV="${SSH_KEY_PATH}/hab_vault/habitat_user/id_rsa";
# #
# export PTRN="# ${HABITAT_USER} account on ${TARGET_SRVR}";
# export PTRNB="${PTRN} «begins»";
# export PTRNE="${PTRN} «ends»";
# #
# sed -i "/${PTRNB}/,/${PTRNE}/d" ${SSH_CONFIG_FILE};
# #
# echo -e "
# ${PTRNB}
# Host ${TARGET_SRVR}
# HostName ${TARGET_SRVR}
# User ${HABITAT_USER}
# PreferredAuthentications publickey
# IdentityFile ${HABITAT_USER_SSH_KEY_PRIV}
# ${PTRNE}
# " >> ${SSH_CONFIG_FILE}
# #
# sed -i "s/ *$//" ${SSH_CONFIG_FILE}; # trim whitespace to EOL
# sed -i "/^$/N;/^\n$/D" ${SSH_CONFIG_FILE}; # blank lines to 1 line
# echo -e "Done preparing SSH config file.";
# };
function PrepareDependencies() {
#
# Get dependencies
# sudo apt -y install expect;
# sudo apt -y install curl;
# sudo apt -y install git;
# sudo apt -y install build-essential;
# sudo apt -y install whois;
InstalledAlready expect;
InstalledAlready curl;
InstalledAlready git;
InstalledAlready jq;
InstalledAlready build-essential;
InstalledAlready whois;
#
# Prepare 'git'
git config --global user.email "${YOUR_EMAIL}";
git config --global user.name "${YOUR_NAME}";
git config --global push.default simple;
#
};
function CheckHostsFile() {
echo -e "Verifying hosts file settings";
HSTS="/etc/hosts";
READY=0;
if ! (( $( grep -c "${TARGET_SRVR}" ${HSTS} ) )); then echo "No mapping for '${TARGET_SRVR}'."; READY=1; fi;
if ! (( $( grep -c "${VHOST_DOMAIN}" ${HSTS} ) )); then echo "No mapping for '${VHOST_DOMAIN}'."; READY=1; fi;
((READY)) && exit 1;
if ! ping -c1 ${TARGET_SRVR} &>/dev/null; then echo "Can't reach '${TARGET_SRVR}'."; export READY=1; fi;
if ! ping -c1 ${VHOST_DOMAIN} &>/dev/null; then echo "Can't reach '${VHOST_DOMAIN}'."; export READY=1; fi;
((READY)) && exit 1;
return 0;
#
};
function CheckForHabitatOriginKeys() {
if [ ! -d ${HABITAT_FOR_METEOR_USER_SECRETS_DIR} ]; then
echo -e "Cannot find Habitat Origin Keys in '${HABITAT_FOR_METEOR_USER_SECRETS_DIR}'!";
mkdir -p ${HABITAT_FOR_METEOR_USER_SECRETS_DIR};
exit 1;
fi;
pushd ${HABITAT_FOR_METEOR_USER_SECRETS_DIR} >/dev/null;
if [ ! -f ${YOUR_ORG}-*.sig.key ]; then
echo -e "
*** Cannot find Habitat Origin Keys! ***
The directory '${HABITAT_FOR_METEOR_USER_SECRETS_DIR}'
is expected to contain two files named as follows :
- yourse1f-yourorg-yyyymmddhhmmss.pub
- yourse1f-yourorg-yyyymmddhhmmss.sig.key
";
exit 1;
fi;
popd >/dev/null;
};
function PrepareSemVer() {
SEMVER_SH="semver.sh";
if [ ! -f ${HABITA4METEOR_SCRIPTS_DIR}/${SEMVER_SH} ]; then
pushd ${HABITA4METEOR_SCRIPTS_DIR} >/dev/null;
## 'semver_shell' parses and compares version numbers
SEMVER_UTIL="semver_shell";
SU_VERSION="0.2.0";
SEMVER_TAR="${SEMVER_UTIL}-${SU_VERSION}";
# https://github.com/warehouseman/semver_shell/archive/v0.2.0.tar.gz
wget -nc -O ${SEMVER_TAR}.tar.gz https://github.com/warehouseman/${SEMVER_UTIL}/archive/v${SU_VERSION}.tar.gz;
tar zxvf ${SEMVER_TAR}.tar.gz ${SEMVER_TAR}/semver.sh;
mv ${SEMVER_TAR}/semver.sh .;
rm -fr ${SEMVER_TAR}*;
popd >/dev/null;
fi;
source ${HABITA4METEOR_SCRIPTS_DIR}/${SEMVER_SH};
};
function RefreshHabitatOriginKeys() {
if [ ! -d ${HABITAT_FOR_METEOR_USER_SECRETS_DIR} ]; then
echo -e "Cannot find Habitat Origin Keys in '${HABITAT_FOR_METEOR_USER_SECRETS_DIR}'!";
mkdir -p ${HABITAT_FOR_METEOR_USER_SECRETS_DIR};
exit 1;
fi;
pushd ${HABITAT_FOR_METEOR_USER_SECRETS_DIR} >/dev/null;
if [ ! -f ${YOUR_ORG}-*.sig.key ]; then
echo -e "Cannot find Habitat Origin Keys!";
exit 1;
fi;
cat ${YOUR_ORG}-*.pub | sudo hab origin key import; echo ;
cat ${YOUR_ORG}-*.sig.key | sudo hab origin key import; echo ;
popd >/dev/null;
};
function GetHabitatForMeteor() {
mkdir -p ${HABITA4METEOR_PARENT_DIR};
pushd ${HABITA4METEOR_PARENT_DIR} >/dev/null;
git clone https://github.com/${HABITA4METEOR_FORK_ORG}/${HABITA4METEOR_FORK_NAME};
popd >/dev/null;
};
function GetMeteor() {
if [ ! -d ${HOME}/.meteor ]; then
curl https://install.meteor.com/ | sh;
fi;
};
function need_to_clone() {
[ -d ${1} ] || return 0;
pushd ${1} >/dev/null;
# if git diff-index --quiet HEAD --; then
if git diff-index HEAD --; then
return 1;
else
echo -e "
*** BAD STATE :: Commits required on project ${1}! ***
Cannot continue. Review 'git status' and correct as
as needed, before retrying.
*** ";
git status;
exit 1;
fi
popd >/dev/null;
return 1;
}
function GetMeteorProject() {
mkdir -p ${TARGET_PROJECT_PARENT_DIR};
pushd ${TARGET_PROJECT_PARENT_DIR} >/dev/null;
if need_to_clone ${TARGET_PROJECT_NAME}; then
echo "Git cloning : '[email protected]:${PROJECT_UUID}.git'.";
git clone [email protected]:${PROJECT_UUID}.git;
else
echo "Repo ${TARGET_PROJECT_NAME} exists unaltered.";
fi;
popd >/dev/null;
};
function FixGitPrivileges() {
pushd .git >/dev/null;
local OLD_URL_PATTERN="url = git";
local NEW_URL_LINE=" url = git@${YOUR_ORG}.github.com:${PROJECT_UUID}.git";
echo "Setting repo ownership: '[email protected]:${PROJECT_UUID}.git'.";
sed -i "s|.*url = git.*|${NEW_URL_LINE}|" ./config;
echo "Identifying repo owner: '[email protected]:${PROJECT_UUID}.git'.";
if [[ -z $(grep "email" config) ]]; then
echo -e "[user]
name = ${YOUR_NAME}
email = ${YOUR_EMAIL}" >> ./config
popd >/dev/null;
fi;
};
# P1 : the url to verify
# P2 : additional commands to meteor. Eg; test-packages
function LaunchDefaultMeteorProcess()
{
METEOR_URL=$1;
STARTED=false;
echo -e "Verifying Meteor execution :
- METEOR_URL = ${METEOR_URL}
- METEOR_SETTINGS = ${2}
";
# export METEOR_SETTINGS="${METEOR_SETTINGS}";
until wget -q --spider ${METEOR_URL};
do
echo "Waiting for ${METEOR_URL}";
if ! ${STARTED}; then
meteor ${2} &
STARTED=true;
fi;
sleep 5;
done
echo "Meteor is running on ${METEOR_URL}";
}
# P1 : the url to verify
# P2 : Meteor startup command
function LaunchMeteorScript()
{
METEOR_URL=$1;
STARTED=false;
echo -e "Verifying Meteor execution :
- METEOR_URL = ${METEOR_URL}
- METEOR_SETTINGS = ${2}
- NON_STOP = ${NON_STOP}
";
# export METEOR_SETTINGS="${METEOR_SETTINGS}";
until wget -q --spider ${METEOR_URL};
do
if ! ${STARTED}; then
echo "Starting Meteor app on '${METEOR_URL}'.";
export NON_STOP="YES";
nohup ${2} &
STARTED=true;
fi;
sleep 5;
echo "Testing comms with '${METEOR_URL}'...";
done
echo "Ok! Found Meteor running on ${METEOR_URL}";
rm -f nohup.out;
}
function KillMeteorProcess()
{
EXISTING_METEOR_PIDS=$(ps aux | grep meteor | grep -v grep | grep ~/.meteor/packages | awk '{print $2}');
# echo ">${IFS}< ${EXISTING_METEOR_PIDS} ";
for pid in ${EXISTING_METEOR_PIDS}; do
echo "Kill Meteor process : >> ${pid} <<";
kill -9 ${pid};
done;
}
function IncorporateExternalPkgsIfAny() {
if [[ $(find .pkgs/* -maxdepth 0 -type d | wc -l) -gt 0 ]]; then
mkdir -p node_modules;
pushd .pkgs >/dev/null;
echo "~~~~~~~~~~ Copy external modules to node_modules directory ~~~~~~~~~~~~~~~~~~~~~~";
for dir in ./*/
do
DNAME=${dir/#.\/};
DNAME=${DNAME/%\//};
echo "~~~~~~~~~~ Copying module '${DNAME}' ~~~~~~~~~~~~~~~~~~~~~~";
pushd ${dir} >/dev/null;
meteor npm -y install;
popd >/dev/null;
rm -fr ../node_modules/${DNAME};
cp -r ${DNAME} ../node_modules;
done
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
popd >/dev/null;
fi;
}
function has_JSON_element() {
return $(cat ${1} | jq .${2} | grep -c null 2>/dev/null);
}
function TrialBuildMeteorProject() {
pushd ${THE_PROJECT_ROOT} >/dev/null;
if has_JSON_element package.json scripts.install_all; then
echo -e "Will use project's own installation script";
pwd;
meteor npm run install_all;
else
export NON_STOP="YES";
meteor npm run install;
fi;
popd >/dev/null;
};
function TrialRunMeteorProject() {
pushd ${THE_PROJECT_ROOT} >/dev/null;
if has_JSON_element "package.json" "scripts.run_development"; then
echo " # Will use project's own installation script"
LaunchMeteorScript "http://localhost:3000/" "meteor npm run sanity_check";
else
echo " # Start it up, look for any other issues and test on URL :: http://localhost:3000/."
LaunchDefaultMeteorProcess "http://localhost:3000/" "--settings=${METEOR_SETTINGS_FILE_PATH}";
fi;
KillMeteorProcess;
popd >/dev/null;
};
function PerformanceFix() {
# Optimize file change responsivity
INOT_STTNG="fs.inotify.max_user_watches=524288";
SYSCTL="/etc/sysctl.conf";
if [[ "$(cat ${SYSCTL} | grep -c ${INOT_STTNG})" -lt "1" ]]; then
echo ${INOT_STTNG} | sudo tee -a ${SYSCTL} && sudo sysctl -p;
fi;
};
function PrepareMeteorProject() {
pushd ${HABITA4METEOR_PARENT_DIR}/${HABITA4METEOR_FORK_NAME} >/dev/null;
./Update_or_Install_H4M_into_Meteor_App.sh ${THE_PROJECT_ROOT};
popd >/dev/null;
};
function PreparePlanFile() {
pushd ${THE_PROJECT_ROOT}/.habitat >/dev/null;
cp ${HABITA4METEOR_SOURCE_DIR}/plan.sh.example plan.sh;
sed -i "/^pkg_origin/c\pkg_origin=${YOUR_ORG}" plan.sh;
sed -i "/^pkg_name/c\pkg_name=${TARGET_PROJECT_NAME}" plan.sh;
sed -i "/^pkg_version/c\pkg_version=${RELEASE_TAG}" plan.sh;
sed -i "/^pkg_maintainer/c\pkg_maintainer=\"${YOUR_NAME} <${YOUR_EMAIL}>\"" plan.sh;
sed -i "/^pkg_upstream_url/c\pkg_upstream_url=https://github.com/${PROJECT_UUID};" plan.sh;
popd >/dev/null;
};
function InitializeMeteorProject() {
pushd ${THE_PROJECT_ROOT} >/dev/null;
./.habitat/scripts/Update_or_Install_Dependencies.sh;
popd >/dev/null;
};
function updateOrAddKVPair() {
# echo "PKG_JSN -- ${PKG_JSN}";
# echo "MRKR -- ${MRKR}";
local KEY="\"${1}\"";
local VAL="\"${2}\"";
local REPLACEMENT="";
echo "KEY -- ${KEY}";
echo "VAL -- ${VAL}";
if grep ${KEY} ${PKG_JSN}; then
sed -i "/${KEY}/c\ \ ${KEY}: ${VAL}," ${PKG_JSN};
else
REPLACEMENT+=" ${KEY}: ${VAL},";
REPLACEMENT+="\n ${MRKR}: {";
sed -i "s|.*${MRKR}.*|${REPLACEMENT}|" ${PKG_JSN};
fi;
echo -e "-------
$(grep ${KEY} ${PKG_JSN})
--------";
}
function PatchPkgJson() {
export PKG_JSN=${1};
export MRKR="\"scripts\"";
head -n 15 ${PKG_JSN};
updateOrAddKVPair "name" "${TARGET_PROJECT_NAME}";
updateOrAddKVPair "version" "0.1.4";
updateOrAddKVPair "license" "MIT";
updateOrAddKVPair "repository" "https://github.com/${PROJECT_UUID}";
};
# function PatchPkgJson() {
# local REPLACEMENT="";
# REPLACEMENT+=" \"name\": \"${TARGET_PROJECT_NAME}\",";
# REPLACEMENT+="\n \"version\": \"0.1.4\",";
# REPLACEMENT+="\n \"license\": \"MIT\",";
# REPLACEMENT+="\n \"repository\": \"https://github.com/${PROJECT_UUID}\",";
# REPLACEMENT+="\n \"scripts\": {";
# sed -i "s|.*scripts.*|${REPLACEMENT}|" ./package.json;
# };
function FixPkgJson() {
local PKGJSN="./package.json";
# Be sure we even have package.json
if [[ ! -f ${PKGJSN} ]]; then
echo "Project has no file : '${PKGJSN}'";
exit 1;
fi;
# Adding required fields if missing
grep -c repository ${PKGJSN} >/dev/null || PatchPkgJson ${PKGJSN};
# echo "Fixing '${PLAN}' version to: '${RELEASE_TAG}' ";
local REPLACEMENT=" \"version\": \"${RELEASE_TAG}\",";
sed -i "s|.*\"version\":.*|${REPLACEMENT}|" ${PKGJSN};
};
function FixPlanSh() {
local PLAN="${THE_PROJECT_ROOT}/.habitat/plan.sh";
# Be sure we even have plan.sh
if [[ ! -f ${PLAN} ]]; then
echo "Project has no file : '${PLAN}'";
exit 1;
fi;
# echo "Fixing '${PLAN}' version to: '${RELEASE_TAG}' ";
local REPLACEMENT="pkg_version=${RELEASE_TAG}";
sed -i "s|^pkg_version.*|${REPLACEMENT}|" ${PLAN};
grep "pkg_version=" ${THE_PROJECT_ROOT}/.habitat/plan.sh;
};
function FixReleaseNote() {
pushd ${PROJECT_RELEASE_NOTES_DIR} >/dev/null;
if [ ! -f ./${RELEASE_TAG}_note.txt ]; then
cp ${HABITA4METEOR_SOURCE_DIR}/release_notes/0.0.0_note.txt.example ./${RELEASE_TAG}_note.txt;
sed -i "s|0.0.0|${RELEASE_TAG}|g" ./${RELEASE_TAG}_note.txt;
fi;
popd >/dev/null;
git add ${PROJECT_RELEASE_NOTES_DIR}/${RELEASE_TAG}_note.txt;
};
function CommitAndPush() {
echo -e " - Commit ";
git add -A;
if [[ "X$(git status -s)X" = "XX" ]]; then
echo - "Nothing left to commit ...";
else
echo - "Committing now ...";
git commit -a -m "Release version v${RELEASE_TAG}";
fi;
echo -e " - Push ";
git push;
echo -e " Repo is clean";
};
function determineLatestPackagePublished() {
if [[ "XX" == "X${YOUR_ORG}X" ]]; then
echo -e "Could not determine latest package published. No Habitat Origin is defined.
";
return;
fi;
# echo -e "Finding :: ${PACKAGE_PATH} ";
# echo -e "Found :: $(sudo hab pkg search ${YOUR_ORG}) ";
local PACKAGES=($(sudo hab pkg search ${YOUR_ORG} ));
export PKG_CHK=$(echo ${PACKAGES[@]} | grep -c "No packages found");
if (( ${PKG_CHK} > 0 )); then
local LATEST_VERSION="0.0.0-alpha0.0";
else
local LATEST_VERSION="${LATEST_PUBLISHED_PACKAGE_VERSION}";
# echo -e "INITIAL LATEST_VERSION: ${LATEST_VERSION} ";
# echo -e "PACKAGES: ${PACKAGES} ";
for PACKAGE in "${PACKAGES[@]}"
do
if [[ "XX" != "X$(echo ${PACKAGE} | grep ${PROJECT_UUID})X" ]]; then
VERSION=${PACKAGE#${PROJECT_UUID}/};
UNIQUE_VERSION=$(echo ${VERSION} | cut -f1 -d/);
# echo -e "Package : ${PACKAGE} Path: ${PROJECT_UUID} extracted version: ${VERSION} unique version: ${UNIQUE_VERSION}";
# echo -e " LATEST_VERSION: ${LATEST_VERSION} ";
semverGT ${UNIQUE_VERSION} ${LATEST_VERSION} && LATEST_VERSION=${UNIQUE_VERSION};
fi;
done
fi;
LATEST_PUBLISHED_PACKAGE_VERSION=${LATEST_VERSION};
# echo -e "Quitting with '${LATEST_VERSION}'... ";
}
function IncrementReleaseTag() {
echo -e "Increment release number...";
# THE_TAG=$( grep RELEASE_TAG ${TEST_VARS_FILE} | cut -d '"' -f 2 );
THE_TAG=${RELEASE_TAG};
A=(${THE_TAG//./ })
(( A[2]++ ));
NEW_TAG="${A[0]}.${A[1]}.${A[2]}";
echo ${NEW_TAG};
sed -i "s|.*RELEASE_TAG.*| export RELEASE_TAG=\"${A[0]}.${A[1]}.${A[2]}\";|" ${TEST_VARS_FILE};
};
function SetReleaseTag() {
pushd ${THE_PROJECT_ROOT} >/dev/null;
echo "${PRTY} Getting old release tag.. ";
local LATEST_PUBLISHED_PACKAGE_VERSION="0.0.0a";
determineLatestPackagePublished;
echo "LATEST_PUBLISHED_PACKAGE_VERSION -- ${LATEST_PUBLISHED_PACKAGE_VERSION}";
if [[ "${LATEST_PUBLISHED_PACKAGE_VERSION}" > "${RELEASE_TAG}" ]]; then
RELEASE_TAG=${LATEST_PUBLISHED_PACKAGE_VERSION};
fi;
IncrementReleaseTag;
source ${TEST_VARS_FILE};
echo "RELEASE_TAG -- ${RELEASE_TAG}";
echo "${PRTY} Fixing package.json version ";
FixPkgJson;
echo "${PRTY} Fixing plan.sh version ";
FixPlanSh;
echo "${PRTY} Fixing release notes ";
FixReleaseNote;
popd >/dev/null;
};
function PreparingKeysAndPrivileges() {
pushd ${THE_PROJECT_ROOT} >/dev/null;
echo "${PRTY} Fixing git privileges ";
FixGitPrivileges;
echo "${PRTY} Refreshing Habitat Origin keys ";
RefreshHabitatOriginKeys;
};
function BuildAndUploadMeteorProject() {
pushd ${THE_PROJECT_ROOT} >/dev/null;
# echo "${PRTY} Prepare Meteor settings file ";
# PrepareMeteorSettingsFile;
# cp ${METEOR_SETTINGS_FILE} ${HABITAT_FOR_METEOR_SECRETS_DIR};
echo "${PRTY} Start tagging if none";
if [[ "X$(git describe 2> /dev/null)X" = "XX" ]]; then
git tag -a ${RELEASE_TAG} -m "Starting versioning with Habitat"
fi;
echo "${PRTY} Committing and pushing project ";
CommitAndPush;
echo "${PRTY} Building and uploading ";
./.habitat/BuildAndUpload.sh ${RELEASE_TAG};
popd >/dev/null;
};
function VerifyHostsFile() {
local HSTS="/etc/hosts";
echo -e "Check '${HSTS}' file mappings ";