-
Notifications
You must be signed in to change notification settings - Fork 2
/
ship
executable file
·1214 lines (1061 loc) · 35.4 KB
/
ship
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
#
# Deploy an app into K8s/Marathon cluster by building
# the container localy, based on a template
#
# Decision6 SHIP Deployer
# github.com/decision6/ship.git
#
shopt -s expand_aliases
if [[ -L $0 ]]; then
BASEDIR=$(dirname "$(readlink $0)")
else
BASEDIR="$(cd "$(dirname "$0")" && pwd -P)"
fi
# shellcheck source=src/util/logger.sh
source ${BASEDIR}/src/util/logger.sh
# shellcheck source=src/util/slack.sh
source ${BASEDIR}/src/util/slack.sh
# shellcheck source=src/util/validators.sh
source ${BASEDIR}/src/util/validator.sh
# shellcheck source=src/plugins/docker.sh
source ${BASEDIR}/src/plugins/docker.sh
# shellcheck source=src/configure.sh
source ${BASEDIR}/src/configure.sh
# shellcheck source=src/util/config.sh
source ${BASEDIR}/src/util/config.sh
# Config files
config::setFile ship "${BASEDIR}/src/config/${CONFIG_FILE}"
config::setFile user "$HOME/.ship/config.json"
# Aliases for version compatibility
alias dsub=logger::sub
alias decho=logger::echo
alias dwarn=logger::warn
alias dstep=logger::step
alias dcode=logger::code
alias derror=logger::error
alias dtitle=logger::title
alias dprintf=logger::printf
APP_CONFIG=""
# Ship local docker network settings
SHIP_NET="ship.local"
SHIP_NET_ADDR="172.99.0.0"
SHIP_NET_MASK="16"
SHIP_DNS_IP="172.99.0.53"
# Default settings
SHIP_VOL_CONTAINER="ship-volumes"
SHIP_TMP_DIR=$HOME/.ship/tmp
TEMPLATES_DIR=$BASEDIR/templates
SLEEP_TIME=10
REMOTE_CHECKS=15
LOCAL_PRODUCTION=0
ALL_CONTAINERS=0
CONTAINER_MGMT="kubernetes"
CONTAINER_MGMT_URL=""
CONTAINER_MGMT_USR=""
CONTAINER_MGMT_PWD=""
APP_REPO_BASE_URL=""
UPDATED=0
TEMPLATES=""
DEPENDENCIES=""
USER_WORKSPACE=""
REAL_USER_WORKSPACE=""
SHIP_USERNAME=""
SHIP_HOSTNAME=""
FORCE=0
# Resource limits
RESOURCE_PROFILES=""
DEFAULT_RESOURCE_PROFILE=""
RESOURCE_MEM_MAX=1024
RESOURCE_MEM_MIN=10
RESOURCE_CPU_MIN=0.1
RESOURCE_CPU_MAX=1
# Registry settings
REGISTRY_URL=""
REGISTRY_USR=""
REGISTRY_PWD=""
REGISTRY_AUTH=""
PREVIOUS_TAG=""
# Slack settings
SLACK_HOOK=""
SLACK_CHANNEL=""
# App config
APP_TYPE=""
APP_SCOPE="none"
APP_MAIN_SCRIPT="app.js"
APP_VERSION=""
APP_EXPOSE=0
APP_PORT=""
APP_VOLUMES=""
APP_ENV=""
APP_LABELS=""
APP_HEALTH="/health"
APP_URL=""
APP_BRANCH="master"
APP_DEPS=""
APP_REPO=""
APP_RESOURCE_PROFILE=""
# Template variable replacements
APP_REPLACEMENTS=""
GLOBAL_REPLACEMENTS=""
# Detect OS
MAC="darwin"
LINUX="linux"
UNKNOWN='unknown'
OS=$UNKNOWN
#
# FUNCTIONS
#
function init() {
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]]; then
OS=$LINUX
elif [[ "$unamestr" == 'Darwin' ]]; then
OS=$MAC
fi
JQ=$(which jq)
if [[ "$JQ" == "" ]]; then
JQ=$BASEDIR/lib/$OS/jq
fi
if [[ -z ${REAL_USER+x} ]]; then
SHIP_USERNAME=$(whoami)
else
SHIP_USERNAME=${REAL_USER}
fi
if [[ -z ${REAL_HOST+x} ]]; then
SHIP_HOSTNAME=$(whoami)
else
SHIP_HOSTNAME=${REAL_HOST}
fi
}
function appConfig() {
local configFile currentDir
currentDir=$(pwd)
# When running inside a container, USER_WORKSPACE is the directory /workspace
# inside the container where the REAL_USER_WORKSPACE (the the real user workspace
# outside the container) is mounted so ship can look for ship.json files
# with that app config
export REAL_USER_WORKSPACE=$USER_WORKSPACE
if [[ "$SHIP_CONTAINER_WORKSPACE" != "" ]];
then
export USER_WORKSPACE=$SHIP_CONTAINER_WORKSPACE
fi
if [[ "$APP_NAME" == "" ]]; then
if [[ -f $currentDir/$APP_CONFIG_FILE ]]; then
configFile="$currentDir/$APP_CONFIG_FILE"
fi
else
if [[ "$APP_NAME" == "all" ]]; then
return 0;
fi
if [[ -f $USER_WORKSPACE/$APP_NAME/$APP_CONFIG_FILE ]]; then
configFile="$USER_WORKSPACE/$APP_NAME/$APP_CONFIG_FILE"
fi
fi
if [[ "$configFile" == "" ]]; then
return 1
fi
echo $configFile
}
function usage() {
dtitle "Usage"
echo
echo "ship ACTION APP_NAME [OPTIONS]"
echo
echo "APP_NAME The app name. Must be equal to it's git repo, image name in registry"
echo " and app folder on your workspace when using local dev deployments."
echo " App name = 'app-test' will assume:"
echo " Registry = <YOUR_CONFIGURED_REGISTRY>/app-test"
echo " Repository = <YOUR_CONFIGURED_REPOSITORY>/app-test.git"
echo
dsub "ACTIONS [REMOTE]:"
echo " deploy Build docker image, push to registry, create/update the app whith the new image"
echo " release Build docker image and push to registry only"
echo " build Build docker image only"
echo " rollback Rollback to the last saved version of the selected APP (doesn't work on ship container)"
echo " update Update an app to an existing image tag"
echo " list List apps deployed in the cluster"
echo " destroy CAUTION: Remove a production app (x_x)"
echo
dsub "ACTIONS [LOCAL]:"
echo " local:deploy | :deploy Deploy locally on your computer"
echo " local:destroy | :destroy Remove all containers created by ship or the one in the ship.json file on this dir"
echo " - local:destroy APP_NAME To destroy a specific container"
echo " - local:destroy all To destroy all containers"
echo " local:list | :list List all containers created by ship"
echo " local:shell | :shell Connect to a local container shell (sh)"
echo " local:logs | :logs Retrieve local container logs"
echo " local:restart | :restart Restarts a local container"
echo
dsub "ACTIONS [OTHER]:"
echo " config Configure ship when running directly in your computer"
echo " kube Jump to kubectl prompt"
echo " help Display this help"
echo " examples Display examples of usage"
echo " profiles Display all avaiable resource profiles"
echo
dsub "OPTIONS:"
echo " --version Deploy/update to selected version"
echo " --type Template to use (node|node4|node6|php|html)"
echo " --scope Scope of application (labeling only) [OPTIONAL]"
echo " Examples: app|api|site|monitoring|devops (Defaults to 'none')"
echo " Only needed for new applications that are not in the cluster yet"
echo " --script Path to the main script of the application [OPTIONAL]"
echo " --health-url Health check URI for the application (Defaults to '/health')"
echo " --expose Expose port so the app can be accessed externally [OPTIONAL]"
echo " --port Which host port to bind to when deploying locally [LOCAL][OPTIONAL]"
echo " Defaults to RANDOM free port over 3000"
echo " --url Comma-separated external URLs that an exposed service responds to [OPTIONAL]"
echo " Example: ship deploy app01 --url=\"app.mysite.com web.decision6.com\""
echo " --branch Which branch to checkout inside container for this application [OPTIONAL]"
echo " --repo The name of the repository for this application. [OPTIONAL]"
echo " --workspace Set your workspace path to mount WORKSPACE/APP_NAME inside container [LOCAL][OPTIONAL]"
echo " If not defined will deploy production version"
echo " --no-mount Skip mounting your workspace/app dir into container /app dir [LOCAL][OPTIONAL]"
echo " --force Force running update even if the remote version is the same as --version [OPTIONAL]"
echo
dsub "USEFUL:"
echo " On local deploys SHIP will mount your workspace in every container at /workspace."
echo " - You can use that folder to link multiple projects for example."
echo " All local containers share some folders to keep cache and config between them:"
echo " - [ /data, /shared, /root/.cache, /root/.npm, /root/.composer, /root/.sbt ]"
echo
echo ">> VOLUME MOUNTS and LABELS can be added in the ship.json file. See 'ship examples for help'."
echo
}
function usageExamples() {
dtitle "Usage EXAMPLES"
echo
echo "Deploy api-indicators with node template:"
dcode "ship deploy my-app --type=node"
echo
echo "Deploy a new application with node template, classifying as an api:"
dcode "ship deploy my-app --type=node --scope=api"
echo
echo "Deploy app-web setting the default script to app/app.js:"
dcode "ship deploy my-app --type=node --script=app/app.js"
dcode "ship deploy my-app-http --type=node --script=http-server"
echo
echo "Rollback a deployment to the previous version (saved in the previous deploy attempt):"
dcode "ship rollback my-app"
echo
echo "Update api-indicatos in kubernetes to a specific existing image tag:"
dcode "ship update my-app --version=32"
echo
echo "Deploy app-web from test branch with the name app-web-beta"
dcode "ship deploy my-app-beta --branch=test --repo=github.com:whoami/app-name.git --type=node"
echo
echo "Deploy a container locally with a specific production version"
dcode "ship :deploy my-app --type=node --version=999"
echo
echo "Deploy a container locally pointing to the code on your machine"
dcode "ship :deploy app01 --type=node (if you have the env var WORKSPACE set and not empty)"
dcode "ship :deploy app01 --type=node --workspace=/home/user/workspace/ (will look for /home/user/workspace/app01)"
echo
echo "Destroy all local containers created by Ship"
dcode "ship :destroy"
echo
echo "Destroy a single local container created by Ship"
dcode "ship :destroy my-app"
echo
echo "Volume MOUNTS and LABELS in APP_SRC/ship.json"
dconf "{"
dconf " \"ship\": {"
dconf " \"name\": \"my-app\","
dconf " \"type\": \"node\","
dconf " },"
dconf " \"remote\": {"
dconf " \"labels\": [{"
dconf " \"name\": \"enableHTTP\","
dconf " \"value\": \"1\""
dconf " }"
dconf " ],"
dconf " \"volumes\": [{"
dconf " \"container\": \"/data\","
dconf " \"host\": \"/nfs/persistent\""
dconf " }]"
dconf " }"
dconf "}"
echo
}
function shipInfo() {
echo
dtitle "SHIP Information"
echo
echo
dsub "SHIP CONFIG"
echo "--------------------------------------"
echo "SHIP_VERSION: $(version)"
echo "TEMPLATES DIR: $TEMPLATES_DIR"
echo "VCS DEFAULT URL: $APP_REPO_BASE_URL"
echo "SLACK HOOK: $SLACK_HOOK"
echo "SLACK CHANNEL: $SLACK_CHANNEL"
echo "LOCAL USERNAME: $USER_NAME"
echo "LOCAL HOSTNAME: $USER_HOSTNAME"
echo
dsub "CONTAINER MANAGEMENT"
echo "--------------------------------------"
echo "TYPE: $CONTAINER_MGMT"
echo "API URL: $CONTAINER_MGMT_URL"
echo "API USERNAME: $CONTAINER_MGMT_USR"
echo "API PASSWORD: $CONTAINER_MGMT_PWD"
echo
dsub "REGISTRY CONFIG"
echo "--------------------------------------"
echo "REGISTRY URL: $REGISTRY_URL"
echo "REGISTRY USR: $REGISTRY_USR"
echo "REGISTRY PWD: $REGISTRY_PWD"
echo
dsub "CURRENT APP CONFIG"
echo "--------------------------------------"
echo "TYPE: $APP_TYPE"
echo "SCOPE: $APP_SCOPE"
echo "MAIN SCRIPT: $APP_MAIN_SCRIPT"
echo "EXPOSE: $APP_EXPOSE"
echo "HEALTH CHECK URL: $APP_HEALTH"
echo "APP EXTERNAL URL: $APP_URL"
echo "APP VCS URL: $APP_REPO"
echo "APP BRANCH: $APP_BRANCH"
echo "APP DEPS: $APP_DEPS"
echo
dsub "OTHER ARGS"
echo "--------------------------------------"
echo "SKIP_LOCAL_MOUNT: $LOCAL_PRODUCTION"
echo "ALL_CONTAINERS: $ALL_CONTAINERS"
echo "FORCE: $FORCE"
echo
}
function deploy() {
dtitle "Deploying Application"
decho
# Check for app template
if [ "$APP_TYPE" == "" ]; then
derror "You must specify the template type using the --type option"
return 1
fi
# Needs to get latest tag?
if [ "$APP_VERSION" == "" ]; then
APP_VERSION=$(getNextTag)
fi
# Load resource limits and reservation (Resource Profile)
loadResourceProfile
if [ $? -eq 1 ]; then
derror "Deploy Failed"
return 1
fi
decho "---------------------------------------------------------------------"
decho "Deploying ${APP_NAME} with version: ${WHITE}${APP_VERSION}${NC}"
decho "Using template: ${WHITE}${APP_TYPE}${NC}"
decho "Repository: ${WHITE}${APP_REPO}${NC}"
decho "Branch: ${WHITE}${APP_BRANCH}${NC}"
decho "Resource Profile: ${WHITE}${APP_RESOURCE_PROFILE}${NC}"
decho " - CPU Limit: ${WHITE}${CPU_LIMIT}${NC}"
decho " - MEM Limit: ${WHITE}${MEMORY_LIMIT}M${NC}"
decho "---------------------------------------------------------------------"
# Build and tag image as both new version and latest
dockerBuild
if [[ $? -eq 1 ]]; then
derror "Docker Build Failed"
return 1
fi
# Push to private registry
dockerPush
if [[ $? -eq 1 ]]; then
derror "Docker Push Failed"
return 1
fi
# Cluster deploy
remoteDeploy
if [[ $? -eq 1 ]]; then
derror "Deploy Failed"
return 1
fi
slack::send "App $APP_NAME version $APP_VERSION deployed (from $USER_NAME @ $USER_HOSTNAME)"
dsuccess "Application Shipped!"
}
function update() {
dtitle "Updating Application"
# Load resource limits and reservation (Resource Profile)
loadResourceProfile
if [ $? -eq 1 ]; then
derror "Deploy Failed"
return 1
fi
decho "---------------------------------------------------------------------"
decho "Deploying ${APP_NAME} with version: ${WHITE}${APP_VERSION}${NC}"
decho "Using template: ${WHITE}${APP_TYPE}${NC}"
decho "Repository: ${WHITE}${APP_REPO}${NC}"
decho "Branch: ${WHITE}${APP_BRANCH}${NC}"
decho "Resource Profile: ${WHITE}${APP_RESOURCE_PROFILE}${NC}"
decho " - CPU Limit: ${WHITE}${CPU_LIMIT}${NC}"
decho " - MEM Limit: ${WHITE}${MEMORY_LIMIT}M${NC}"
decho "---------------------------------------------------------------------"
dsub "Verify Image Tag"
imageTagExists
if [[ $? -eq 1 ]]; then
derror "Docker image $REGISTRY_URL/$APP_NAME:$APP_VERSION not found"
derror "Update Failed"
return 1
fi
remoteDeploy
if [[ $? -eq 1 ]]; then
derror "Update Failed"
return 1
fi
slack::send "App $APP_NAME updated to version $APP_VERSION (from $USER_NAME @ $USER_HOSTNAME)"
dsuccess "Application Updated!"
}
function destroy() {
printf "${RED}"
echo " ############# "
echo " ## *## "
echo " # **# "
echo " # %% %% ***# "
echo " # %%%%%%% ****# "
echo " # %%%%% *****# "
echo " # ### % ###***# "
echo " # # #### #### #**# "
echo " # # # # #**# "
echo " # ##### # # #####***# "
echo " # # # *******# "
echo " ### # **# ### "
echo " # - - - - - - # "
echo " | | | | | | | "
echo
printf "${NC}"
dtitle "Production App Destroy"
if [[ "$APP_NAME" == "" ]]; then
derror "Missing application name"
return 1
fi
decho "TARGET: $APP_NAME"
decho
dsub "Destroy Confirmation:"
while true; do
printf "${ORANGE}Are you sure you wanna destroy a production app? [y/n] ${NC}"
read -r opt
case $opt in
[Yy]* ) break;;
[Nn]* ) exit 1;;
* ) decho "Please answer y or n";;
esac
done
while true; do
printf "${ORANGE}Ok sailor, if you are that brave, type 'I AM SURE': ${NC}"
read -r opt
opt=$(echo $opt | tr '[:upper:]' '[:lower:]')
if [ "$opt" == "i am sure" ]; then
break
else
derror "It seems that you are not so sure..."
return 1
fi
done
dsub "Triggering destroy..."
remoteDestroy
if [[ $? -eq 1 ]]; then
derror "Destroy Failed"
return 1
fi
slack::send "App $APP_NAME DESTROYED in PRODUCTION! (from $USER_NAME @ $USER_HOSTNAME)"
dsuccess "Application Destroyed!"
}
function loadResourceProfile() {
if [ "$APP_RESOURCE_PROFILE" == "" ]; then
if [ "$DEFAULT_RESOURCE_PROFILE" != "" ]; then
dwarn "Resource profile for this app not set. Using default: $DEFAULT_RESOURCE_PROFILE"
APP_RESOURCE_PROFILE=$DEFAULT_RESOURCE_PROFILE
else
derror "Resource profile for this app not set and no default value found."
return 1
fi
else
profileExists
if [[ ! $? ]]; then
derror "Resource profile '$APP_RESOURCE_PROFILE' does not exists."
return 1
fi
fi
CPU_LIMIT=$(echo $RESOURCE_PROFILES | $JQ ".$APP_RESOURCE_PROFILE.cpu" | tr -d '"')
MEMORY_LIMIT=$(echo $RESOURCE_PROFILES | $JQ ".$APP_RESOURCE_PROFILE.memory" | tr -d '"')
if [[ "$CPU_LIMIT" == "" || "$MEMORY_LIMIT" == "" ]]; then
derror "Resource profile '$APP_RESOURCE_PROFILE' must have 'cpu' and 'memory' defined."
return 1
fi
# Validate numeric
validator::isBetween $CPU_LIMIT $RESOURCE_CPU_MIN $RESOURCE_CPU_MAX
if [[ ! $? ]]; then
derror "CPU limit must be a number between $RESOURCE_CPU_MIN and $RESOURCE_CPU_MAX ($CPU_LIMIT)"
return 1
fi
# Validate integer limit
validator::isBetween $MEMORY_LIMIT $RESOURCE_MEM_MIN $RESOURCE_MEM_MAX
if [[ ! $? ]]; then
derror "Memory limit must be an integer between $RESOURCE_MEM_MIN and $RESOURCE_MEM_MAX ($MEMORY_LIMIT)"
return 1
fi
}
function profileExists() {
local count profile
if [[ "$RESOURCE_PROFILES" == "" ]]; then
return 1
fi
count=$(echo $RESOURCE_PROFILES | $JQ -c '. | length' | sed 's/null/0/g')
if [[ $count -eq 0 ]]; then
return 1
fi
profile=$(echo $RESOURCE_PROFILES | $JQ ".$APP_RESOURCE_PROFILE" | tr -d '"')
if [[ "$profile" == "" || "$profile" == "null" ]]; then
return 1
fi
return 0
}
function validate() {
if [[ ! -f $USER_CONFIG_FILE ]]; then
derror "Ship not configured, run 'ship config' first"
exit 1
fi
if [[ "$APP_NAME" == "" ]]; then
derror "You must specify app name"
exit 1
fi
if [[ "$APP_URL" == "" ]]; then
APP_URL=$APP_NAME
else
APP_URL=$(echo $APP_URL | sed 's#http[s]*://##g' | sed 's#,#|#g')
fi
# Create temporary dir
if [[ ! -d $SHIP_TMP_DIR/$APP_NAME/ ]]; then
mkdir -p $SHIP_TMP_DIR/$APP_NAME/
else
#find $SHIP_TMP_DIR/$APP_NAME/ ! -name 'previous_tag' -exec rm -rf {} +
rm -rf $SHIP_TMP_DIR/$APP_NAME/*.log
rm -rf $SHIP_TMP_DIR/$APP_NAME/*.json
rm -rf $SHIP_TMP_DIR/$APP_NAME/Dockerfile*
fi
}
function logs() {
local files
dtitle "Last Ship Logs"
decho "Displaying last ship logs for app: $APP"
if [[ ! -d $SHIP_TMP_DIR/$APP_NAME/ ]]; then
decho "No logs for app $APP_NAME"
else
files=$(tail $SHIP_TMP_DIR/$APP_NAME/*.log 2> /dev/null)
if [ "$files" == "" ];
then
decho "No logs for app $APP_NAME"
return 0
fi
echo
echo "BEGIN"
echo "-----"
echo
tail $SHIP_TMP_DIR/$APP_NAME/*.log
echo
echo "-----"
echo "END"
echo
fi
return 0
}
function updateShipImage() {
docker pull ${REGISTRY_URL}/ship:latest
return $?
}
function selfUpdate() {
local currentDir remoteRev originRev
decho "Updating SHIP..."
currentDir=$(pwd)
cd $BASEDIR || return 1
if [[ ! -d ".git" ]]; then
derror "Git repository not found!"
cd $currentDir && return 1
return 1
fi
remoteRev=$(git rev-parse origin/master)
# Could not get remote HEAD commit hash
if [[ ! $? -eq 0 ]]; then
derror "Error fetching git latest version. Could not update."
cd $currentDir && return 1
return 1
fi
originRev=$(git rev-parse HEAD)
# If updated return
if [[ "$remoteRev" == "$originRev" ]]; then
decho "SHIP already up-to-date."
cd $currentDir && return 1
return 0
fi
# Try to update
# git reset
git pull origin master > /dev/null
if [[ $? -eq 0 ]]; then
decho "SHIP was updated!"
cd $currentDir && return 1
return 0
else
decho "Errors were found while trying to update!"
cd $currentDir && return 1
return 1
fi
}
function readShipConf() {
readConf "ship"
return $?
}
function readShipUserConf() {
readConf "user"
return $?
}
function readConf() {
configFile=$1
# Check for custom templates dir
config::toVar "USER_TEMPLATES_DIR" '.ship.templates.dir' $configFile
config::toVar "USER_TEMPLATES_GIT_UPDATE" '.ship.templates.git_update' $configFile
config::toVar "USER_TEMPLATES_GIT_RESET" '.ship.templates.git_reset' $configFile
# Notification settings
config::toVar "SLACK_CHANNEL" '.ship.notify.slack.channel' $configFile
config::toVar "SLACK_HOOK" '.ship.notify.slack.hook' $configFile
# Repository data
config::toVar "APP_REPO_BASE_URL" '.ship.repository.repo_default_url' $configFile
config::toVar "APP_BRANCH" '.ship.repository.repo_default_branch' $configFile
# Create full vcs url based on base url and app name
if [ "$APP_REPO_BASE_URL" != "" ]; then
SEP="/"
[[ "${APP_REPO_BASE_URL: -1}" == "/" ]] && SEP=""
APP_REPO=${APP_REPO_BASE_URL}${SEP}${APP_NAME}.git
fi
# Cluster Settings
# Mount path
config::toVar "PERSISTENT_DIR" '.cluster.persistent_data_dir' $configFile
config::toVar "SHARED_DATA_DIR" '.cluster.shared_data_dir' $configFile
config::toVar "CONTAINER_PERSISTENT_DIR" '.cluster.container_persistent_dir' $configFile
config::toVar "CONTAINER_SHARED_DIR" '.cluster.container_shared_dir' $configFile
# Registry default settings
config::toVar "REGISTRY_URL" '.registry.url' $configFile
config::toVar "REGISTRY_USR" '.registry.username' $configFile
config::toVar "REGISTRY_PWD" '.registry.password' $configFile
if [[ "$REGISTRY_USR" != "" && "$REGISTRY_PWD" != "" ]]; then
REGISTRY_AUTH="-u ${REGISTRY_USR}:${REGISTRY_PWD}"
fi
# Default container orchestration plugin to load
config::toVar "CONTAINER_MGMT" '.container_management.type' $configFile
config::toVar "CONTAINER_MGMT_URL" '.container_management.api_url' $configFile
config::toVar "CONTAINER_MGMT_USR" '.container_management.api_username' $configFile
config::toVar "CONTAINER_MGMT_PWD" '.container_management.api_password' $configFile
config::toVar "USER_WORKSPACE" '.ship.user_workspace' $configFile
# Global dependencies
config::toVar "DEPENDENCIES" '.dev.dependencies' $configFile --keep-quotes
config::toVar "TEMPLATES" '.dev.templates' $configFile --keep-quotes
# Container Resource Limits
config::toVar "RESOURCE_PROFILES" '.resource_profiles.profiles' $configFile --keep-quotes
config::toVar "DEFAULT_RESOURCE_PROFILE" '.resource_profiles.default' $configFile
# Template custom replacement variables
config::toVar "GLOBAL_REPLACEMENTS" '.replacements' $configFile --keep-quotes
# Detect local machine username and hostname
if [[ "$USER_NAME" == "" ]]; then
USER_NAME=$(whoami)
fi
if [[ "$USER_HOSTNAME" == "" ]]; then
USER_HOSTNAME=$(hostname)
fi
return 0
}
function readAppConf() {
local config depName configFile="app"
config=$(appConfig)
[[ $? == 1 ]] && return 1
APP_CONFIG=$config
config::setFile "app" $config
# APP Ship Config
config::toVar "APP_NAME" '.ship.name' $configFile
config::toVar "APP_TYPE" '.ship.type' $configFile
config::toVar "APP_SCOPE" '.ship.scope' $configFile
config::toVar "APP_URL" '.ship.url' $configFile
config::toVar "APP_BRANCH" '.vcs.branch' $configFile
config::toVar "APP_REPO" '.vcs.url' $configFile
# Container management
config::toVar "CONTAINER_MGMT" '.container_management.type' $configFile
config::toVar "CONTAINER_MGMT_URL" '.container_management.api_url' $configFile
config::toVar "CONTAINER_MGMT_USR" '.container_management.api_username' $configFile
config::toVar "CONTAINER_MGMT_PWD" '.container_management.api_password' $configFile
# Local deployment config
config::toVar "APP_PORT_LOCAL" '.local.port' $configFile
# APP Container Config
config::toVar "APP_EXPOSE" '.remote.expose' $configFile
if [ "$APP_EXPOSE" == "true" ] || [ "$APP_EXPOSE" == "1" ]; then
APP_EXPOSE=1
else
APP_EXPOSE=0
fi
config::toVar "APP_HEALTH" '.remote.health' $configFile
config::toVar "APP_PORT" '.remote.port' $configFile
config::toVar "APP_VOLUMES" '.remote.volumes' $configFile --keep-quotes
# Custom labels
config::toVar "APP_LABELS" '.remote.labels' $configFile --keep-quotes
# Custom environment variables
config::toVar "APP_ENV" '.remote.env' $configFile --keep-quotes
# Container Resource Limits
config::toVar "APP_RESOURCE_PROFILE" '.remote.resource_profile' $configFile
# APP Registry Config
config::toVar "REGISTRY_URL" '.registry.url' $configFile
config::toVar "REGISTRY_USR" '.registry.username' $configFile
config::toVar "REGISTRY_PWD" '.registry.password' $configFile
if [ "$REGISTRY_USR" != "" ] && [ "$REGISTRY_PWD" != "" ]; then
export REGISTRY_AUTH="-u ${REGISTRY_USR}:${REGISTRY_PWD}"
fi
# Template custom replacement variables
config::toVar "APP_REPLACEMENTS" '.replacements' $configFile --keep-quotes
# APP Dependencies
config=$(cat $configFile)
count=$(echo $config | $JQ '.dependencies | length')
APP_DEPS=""
for ((i=0;i<$count;i++))
do
depName=$(echo $config | $JQ '.dependencies['${i}'].name' | tr -d '"')
APP_DEPS="$APP_DEPS $depName"
done
APP_DEPS=( $APP_DEPS )
return 0
}
function buildShip() {
local nextTag tmpDir imageID
dtitle "SHIP Docker Image Build"
APP_NAME="ship"
nextTag=$(getNextTag)
decho
decho "----------------------"
decho "NEXT TAG: ${WHITE}$nextTag${NC}"
decho "----------------------"
decho
if [ ! -d "./dockerfile" ]; then
derror "Directory ./dockerfile not found. Run this command from ship soure dir."
return 1
fi
if [ ! -f "./dockerfile/Dockerfile" ]; then
derror "Docker file not found in ./dockerfile/. May you need to reset ship source repository."
return 1
fi
dsub "Preparing template..."
tmpDir=/tmp/shipbuild
mkdir -p $tmpDir
cp ./dockerfile/* $tmpDir/
sed -i -e "s#__SHIP_VERSION__#$nextTag#g" $tmpDir/Dockerfile
dsub "Checking previous image..."
imageID=$(docker images $REGISTRY_URL/$APP_NAME:$nextTag --format="{{.ID}}")
if [[ "$imageID" != "" ]]; then
decho "Previous image with tag $nextTag found. Removing it..."
docker rmi -f $imageID
fi
dsub "Building image..."
cd $tmpDir || return 1
docker build -t $REGISTRY_URL/$APP_NAME:latest -t $REGISTRY_URL/$APP_NAME:$nextTag .
if [ ! $? -eq 0 ]; then
derror "Error building SHIP image"
fi
dsub "Cleaning up..."
rm -rf $tmpDir
dsuccess "Finished!"
return 0
}
function listProfiles() {
local count profiles
dtitle "Resource Profiles List"
decho
if [[ "$RESOURCE_PROFILES" == "" ]]; then
derror "No resource profiles found"
return 1
fi
count=$(echo $RESOURCE_PROFILES | $JQ -c '. | length' | sed 's/null/0/g')
if [[ $count -eq 0 ]]; then
derror "No resource profiles found"
return 1
fi
echo "+----------------------+------------+------------+"
printf "| %-20s | %-10s | %-10s |\n" "NAME" "CPU" "MEMORY"
echo "+----------------------+------------+------------+"
profiles=( $(echo $RESOURCE_PROFILES | $JQ 'keys[]') )
for profile in ${profiles[@]}; do
name=$(echo $profile | tr -d '"')
cpu=$(echo $RESOURCE_PROFILES | $JQ ".${profile}.cpu" | tr -d '"')
mem=$(echo $RESOURCE_PROFILES | $JQ ".${profile}.memory" | tr -d '"')
printf "| %-20s | %10s | %10s |\n" $name $cpu "${mem}M"
done
echo "+----------------------+------------+------------+"
}
function version() {
if [[ -z ${SHIP_VERSION+x} ]]; then
echo "UNKNOWN"
return 1
else
echo "$SHIP_VERSION"
return 0
fi
}
function header() {
declare -a shipList
shipList[1]="Jolly Roger — Peter Pan by J. M. Barrie"
shipList[2]="Nautilus — 20.000 Leagues Under the Sea"
shipList[3]="Vingilot — The Silmarillion by J. R. R. Tolkien"
shipList[4]="Black Pearl – Pirates of the Caribbean"
shipList[5]="Red October — The Hunt for Red October by Tom Clancy"
shipList[6]="Argonaut — Heroic Age"
shipList[7]="The Inferno – The Goonies"
shipList[8]="Jenny – Forrest Gump"
shipList[9]="Rachel – Moby-Dick, 1956, 1998"
shipList[10]="USS Scotia - 30,000 Leagues Under the Sea, 2007"
shipList[11]="Skyship Weatherlight - Magic The Gathering"
rand=$(awk -v min=1 -v max=11 'BEGIN{srand(); print int(min+rand()*(max-min+1))}')
# ' # <- Adding a single quote in a commentary just because the above
# 'rand' line is breaking the syntax highlighting from here on
decho
decho " ███████╗██╗ ██╗██╗██████╗ _~ "
decho " ██╔════╝██║ ██║██║██╔══██╗ _~ )_)_~ "
decho " ███████╗███████║██║██████╔╝ )_))_))_) "
decho " ╚════██║██╔══██║██║██╔═══╝ _!__!__!_ "
decho " ███████║██║ ██║██║██║ \_______/ "
decho " ╚══════╝╚═╝ ╚═╝╚═╝╚═ ~~~~~~~~~~~~~ "
decho "---------------------------------------------------------------------"
decho "█ Featured Ship: ${shipList[$rand]} █"
decho "---------------------------------------------------------------------"
decho
if [ "$APP_CONFIG" != "" ]; then
decho "APP config file: ${UNDERLINE}$APP_CONFIG${NC}"
else
decho "APP config file: ${RED}NOT FOUND${NC}"
fi
if [ "$USER_CONFIG_FILE" != "" ]; then
decho "SHIP config file: ${UNDERLINE}$USER_CONFIG_FILE${NC}"
else
decho "SHIP config file: ${RED}NOT FOUND${NC}"
fi
echo
}
#
# MAIN
#
init
ACTION=$1
APP_NAME=$2
shift
shift
# Read local conf file if found in current dir or in USER_WORKSPACE/APP_NAME
readShipConf
readShipUserConf
readAppConf
# Use custom templates dir from user
if [ "$USER_TEMPLATES_DIR" != "" ]; then
if [ -d $USER_TEMPLATES_DIR ]; then
TEMPLATES_DIR=$USER_TEMPLATES_DIR
decho "Using user defined templates dir: $TEMPLATES_DIR"
else