-
Notifications
You must be signed in to change notification settings - Fork 5
/
mu
executable file
·782 lines (709 loc) · 29.2 KB
/
mu
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
#!/usr/bin/env bash
MU_CLI_VERSION="1.0.3"
####
## Sending command info
####
STATUS_MESSAGE=""
function status_echo() {
echo -n "$STATUS_MESSAGE";
}
function status_step() {
STATUS_MESSAGE="$STATUS_MESSAGE.";
echo -n ".";
}
####
## Implementation
####
function print_text_block() {
for var in "$@"
do
echo "$var"
done
}
function ensure_mu_cli_docker() {
container_hash=`docker ps -f "name=mucli" -q`
if [[ -z $container_hash ]] ;
then
docker run --volume /tmp:/tmp -i --name mucli --rm --entrypoint "tail" -d semtech/mu-cli:$MU_CLI_VERSION -f /dev/null
if [[ "$?" -ne "0" ]]
then
echo "I could not start the mu-cli container. Aborting operation." >> /dev/stderr
exit 1
fi
container_hash=`docker ps -f "name=mucli" -q`
while [[ -z $container_hash ]]
do
sleep 1;
echo "."
done
fi
}
####
## Ensure the container exists or ask the user if we can create it?
##
## Uses first argument as the service name in $service
####
function confirm_existing_service() {
service=$1
available_container_id=`docker compose $(print_source_docker_files) ps -a -q $service`
if [[ $? -ne 0 ]]; then
echo "Service $service not found"
return 1;
elif [[ -z $available_container_id ]]; then
# ask user if we can create a stopped container
echo "Container for $service does not exist yet. May we create a container for $service without starting it?"
read -p "Create container? [Y/n]: " -n 1 -s -r INPUT
case ${INPUT:-Y} in
[Yy]*) `docker compose $(print_source_docker_files) up --no-start $service >> /dev/null`;;
[Nn]*) echo "NOT creating container"; return 1 ;;
esac
else
return 0
fi
}
function print_commands_documentation() {
service=$1
command=$2
jq_documentation_get_command_local="jq -c '( .scripts[] | select(.documentation.command == \\\"$command\\\") )'"
command_documentation=`sh -c "$interactive_cli bash -c \"$local_cat_command | $jq_documentation_get_command_local\""`
command_description=`echo "$command_documentation" | $interactive_cli $jq_documentation_get_description`
command_description_indented=`echo "$command_description" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\n /g'`
command_arguments=`echo "$command_documentation" | $interactive_cli $jq_documentation_get_arguments`
print_text_block " $command:" \
" description: $command_description_indented"
echo -n " command: mu script $service $command"
for command_argument in $command_arguments
do
echo -n " [$command_argument]"
done
}
function print_source_docker_files() {
# NOTE: This is used to create the -f options for docker compose.
# If a user has specified DOCKER_COMPOSE_FILE (such as through bash
# script automatically picking up the .dev.yml), then we emit an
# empty string to let docker use its default processing.
if [[ -z "$DOCKER_COMPOSE_FILE" ]]
then
echo ""
else
echo `ls docker-compose*.yml | tac | awk '{ print "-f " $1 }' | tr '\n' ' '`
fi
}
function print_service_documentation() {
service=$1
if confirm_existing_service $service
then
service=$1
available_container_id=`docker compose $(print_source_docker_files) ps -a -q $service`
mkdir -p /tmp/mu/cache/$available_container_id
docker cp $available_container_id:/app/scripts /tmp/mu/cache/$available_container_id 2> /dev/null
local_cat_command="cat /tmp/mu/cache/$available_container_id/scripts/config.json"
if test -f "/tmp/mu/cache/$available_container_id/scripts/config.json"; then
supported_commands=`sh -c "$interactive_cli bash -c \"$local_cat_command | $jq_documentation_filter_commands\""`
for supported_command in $supported_commands
do
print_commands_documentation $service $supported_command
print_text_block "" ""
done
echo ""
else
print_text_block " no scripts found" \
""
fi
else
print_text_block "Cannot provide docs for $service without a container" \
""
fi
}
function print_service_scripts_documentation() {
# This is used for listing the scripts a service knows about
config_file_location=$1
local_cat_command="cat $config_file_location"
if [[ -f "$config_file_location" ]]
then
supported_commands=`sh -c "$interactive_cli bash -c \"$local_cat_command | $jq_documentation_filter_commands\""`
if [[ -n "$supported_commands" ]]
then
print_text_block "" \
"Discovered scripts are:" \
""
for supported_command in $supported_commands
do
print_commands_documentation "[service]" $supported_command
print_text_block "" ""
done
echo ""
else
print_text_block "" \
"There are no scripts available" \
""
fi
else
print_text_block "There are no scripts available" \
"";
fi
}
function print_available_services_information() {
ensure_mu_cli_docker
interactive_cli="docker exec -i mucli"
jq_documentation_filter_commands="jq -r '( .scripts[].documentation.command )'"
jq_documentation_get_description="jq -r .documentation.description"
jq_documentation_get_arguments="jq -r .documentation.arguments[]"
echo "...looking for containers..."
available_services=`docker compose $(print_source_docker_files) config --services`
echo ""
echo "found services:"
for available_service in $available_services
do
print_text_block "$available_service:"
print_service_documentation $available_service
done
}
if [[ "start" == $1 ]]
then
if [[ "dev" == $2 ]]
then
echo "Launching mu.semte.ch project for development ..."
docker compose -f docker-compose.yml -f docker-compose.dev.yml up -d "${@:3}"
elif [[ "prod" == $2 ]]
then
echo "Launching mu.semte.ch project for production ..."
docker compose -f docker-compose.yml up -d "${@:3}"
else
echo "Second argument should be one of [dev, prod]."
fi
elif [[ "logs" == $1 ]]
then
docker compose `print_source_docker_files` logs -f "${@:2}"
elif [[ "project" == $1 ]]
then
if [[ "new" == $2 ]]
then
PROJECT_NAME=$3
if [[ -z "$PROJECT_NAME" ]]
then
print_text_block "Please specify a project name." \
"" \
"The expected usage for this command is:" \
" mu project new [project name] [--with-auth]"
exit 1
fi
WITH_AUTH=$4
if [[ -n "$WITH_AUTH" && ( "$WITH_AUTH" != "--with-auth" ) && ( "$WITH_AUTH" != "--with-new-auth" ) ]]
then
print_text_block "$WITH_AUTH was not understood" \
"" \
"Please specify to use --with-auth or --with-new-auth or don't supply the parameter." \
"" \
"The expected usage for this command is:" \
" mu project new [project name] [--with-auth|--with-new-auth]"
exit 1
fi
echo "Creating new mu project for $PROJECT_NAME"
if [[ -n "$WITH_AUTH" ]]
then
if [[ "$WITH_AUTH" == "--with-auth" ]]
then
git clone -b "feature/add-auth" https://github.com/mu-semtech/mu-project.git $PROJECT_NAME
else
print_text_block "Will create using mu-cl-auth"
git clone -b "feature/mu-cl-auth" https://github.com/mu-semtech/mu-project.git $PROJECT_NAME
fi
else
git clone https://github.com/mu-semtech/mu-project.git $PROJECT_NAME
fi
cd $PROJECT_NAME
rm -Rf ./.git
git init .
git add .
git commit -m "Creating new mu project"
echo "Your mu project hack gear is ready to be hacked... hack on"
elif [[ "doc" == $2 ]]
then
echo "Generating documentation for project in `pwd`"
docker run --rm -v `pwd`/config/resources:/config -v `pwd`/doc:/config/output/ madnificent/cl-resources-plantuml-generator
echo "Generated JSONAPI svg"
docker run --rm -v `pwd`/config/resources:/config -v `pwd`/doc:/config/output/ madnificent/cl-resources-ttl-generator
echo "Generated ttl file for https://service.tib.eu/webvowl/ or https://webvowl.tools.redpencil.io/"
elif [[ "add" == $2 ]]
then
if [[ "service" == $3 ]]; then
service_image=$4
service_tag=$5
echo -n "Adding service "
if [[ "$service_tag" == "" ]]
then
service_tag="latest"
fi
# 1. get the service image
echo -n "."
ensure_mu_cli_docker
echo -n "."
interactive_cli="docker exec -i mucli"
echo -n "."
service_image_description=`curl -s -G -d "filter[microservice][:exact:title]=$service_image" -d "filter[:exact:version]=$service_tag" -d "page[size]=1" https://info.mu.semte.ch/microservice-revisions`
if [[ "$?" -ne "0" ]]
then
echo " FAILED"
echo ""
echo "Could not fetch image description. Are you online?"
exit 1
fi
echo -n "."
jq_infosemtech_image_name="jq -r .data[].attributes.image"
image_name=`echo "$service_image_description" | $interactive_cli $jq_infosemtech_image_name`
if [[ "$image_name" == "" ]]
then
echo " FAILED"
echo ""
echo "Could not find image in the repository."
exit 1
fi
echo -n "."
docker image inspect $image_name:$service_tag > /dev/null 2> /dev/null
found_docker_image="$?"
echo -n "."
if [[ $found_docker_image -ne "0" ]]
then
echo ""
echo "about to pull the image: $image_name:$service_tag"
fi
echo -n "."
docker run --name mu_cli_tmp_copy --entrypoint "/bin/sh" "$image_name:$service_tag"
echo -n "."
if [[ $found_docker_image -ne "0" ]]
then
echo "Adding service ......"
fi
echo -n "."
mkdir -p /tmp/mu/cache/mu_cli_tmp_copy/
# 2. copy scripts contents from the image
echo -n "."
docker cp mu_cli_tmp_copy:/app/scripts/install /tmp/mu/cache/mu_cli_tmp_copy/ > /dev/null 2> /dev/null
install_path_exists="$?"
echo -n "."
docker rm -f mu_cli_tmp_copy > /dev/null # remove the container before going further
if [[ $install_path_exists -ne "0" ]]
then
echo " FAILED"
echo ""
echo "Could not find install script for $image_name:$service_tag"
echo ""
echo "For more info on how to add an install script to an image,"
echo "see https://github.com/mu-semtech/mu-cli."
echo "Perhaps the maintainers would fancy a PR :-)"
exit 1
fi
docker cp docker-compose.yml mucli:/tmp/
echo -n "."
docker cp /tmp/mu/cache/mu_cli_tmp_copy/install/docker-compose-snippet.yml mucli:/tmp/
echo -n "."
# 3. extract script information
services_line_number=`docker exec mucli grep -n -P "^services:" /tmp/docker-compose.yml | awk -F ":" '{ print $1 }' | tail -n 1`
echo -n "."
last_root_object_line_number=`docker exec mucli grep -n -P "^\\w" /tmp/docker-compose.yml | awk -F ":" '{ print $1 }' | tail -n 1`
echo -n "."
docker exec mucli sed -i -e "s/^/ /" /tmp/docker-compose-snippet.yml
echo -n "."
# 4. append lines with info
if [ $services_line_number -ne $last_root_object_line_number ] ;
then
line_number_to_insert_at=$services_line_number
r="r"
docker exec mucli sed -i -e "$line_number_to_insert_at$r /tmp/docker-compose-snippet.yml" /tmp/docker-compose.yml
echo -n "."
else
docker exec mucli /bin/bash -c "echo '' >> /tmp/docker-compose.yml"
echo -n "."
docker exec mucli /bin/bash -c "cat /tmp/docker-compose-snippet.yml >> /tmp/docker-compose.yml"
echo -n "."
fi
docker cp mucli:/tmp/docker-compose.yml docker-compose.yml
echo " DONE"
exit 0
else
echo "To add a service use:"
echo "mu project add service [service name] [(optional) service tag]"
fi
else
echo "Don't know command $2"
echo "Known project commands: [ new, doc, add ]"
fi
elif [[ "script" == $1 ]]
then
# Check if we are in a project or in a service
if [[ -f ./docker-compose.yml && -f Dockerfile ]]
then
echo "mu script is not supported in folders which have a Dockerfile and a docker-compose.yml"
exit 1
elif [[ -f ./docker-compose.yml ]]
then
service=$2
command=$3
interactive_cli="docker exec -i mucli"
ensure_mu_cli_docker
# jq commands
jq_documentation_filter_commands="jq -r '( .scripts[].documentation.command )'"
jq_documentation_get_command="jq -c '( .scripts[] | select(.documentation.command == \\\"$command\\\") )'"
jq_documentation_get_description="jq -r .documentation.description"
jq_documentation_get_arguments="jq -r .documentation.arguments[]"
jq_command_get_mount_point="jq -r '.mounts.app // false'"
jq_command_get_script="jq -r .environment.script"
jq_command_get_image="jq -r .environment.image"
ensure_mu_cli_docker
if [[ "-h" == $service ]] || [[ -z $service ]] ;
then
echo ""
print_available_services_information
exit 0
fi
# get hold of the container that defines the script
confirm_existing_service $service
service_exists=$?
container_id=`docker compose $(print_source_docker_files) ps -a -q $service`
if [[ $? -ne 0 ]] ; # the service doesn't exist in the docker-compose
then
echo ""
print_available_services_information
exit 1
fi
if [[ $service_exists -ne 0 ]]
then
echo "Cannot execute script without existing container"
exit 1 # Service did not exist and user refused to create
fi
mkdir -p /tmp/mu/cache/$container_id
docker cp $container_id:/app/scripts /tmp/mu/cache/$container_id
cat_command="cat /tmp/mu/cache/$container_id/scripts/config.json"
if [[ "-h" == $command ]] || [[ -z $command ]] ;
then
print_text_block "The commands supported by $service are listed below." \
"you can invoke them by typing 'mu script $service [COMMAND] [ARGUMENTS]'." \
""
print_service_documentation $service
exit 0
fi
echo -n "Executing "
command_spec=`sh -c "$interactive_cli bash -c \"$cat_command | $jq_documentation_get_command\""`
if [[ -z $command_spec ]] ;
then
print_text_block "" \
"Error: could not find script: $command for service: $service." \
"Supported scripts are:"
print_service_documentation $service
exit 1
fi
echo -n "."
app_mount_point=`echo "$command_spec" | $interactive_cli bash -c "$jq_command_get_mount_point"`
app_folder="$PWD"
echo -n "."
script_path=`echo "$command_spec" | $interactive_cli $jq_command_get_script`
echo -n "."
script_folder_name=`dirname $script_path`
script_file_name=`basename $script_path`
folder_name="$script_folder_name"
entry_point="$script_file_name"
working_directory="/script"
arguments=("${@:4}")
echo -n "."
image_name=`echo "$command_spec" | $interactive_cli $jq_command_get_image`
echo -n "."
# NOTE: this approach for discovering the project name will
# not work when running installation scripts for a service
docker_compose_project_name=`docker inspect --format '{{ index .Config.Labels "com.docker.compose.project"}}' $container_id`
echo -n "."
interactive_mode=`echo "$command_spec" | $interactive_cli jq -r '.environment.interactive // false'`
echo -n "."
it=""
if [[ true == "$interactive_mode" ]];
then
it=" -it "
fi
echo -n "."
network_options=$()
join_networks=`echo "$command_spec" | $interactive_cli jq -r '.environment.join_networks // false'`
echo -n "."
if [[ true == "$join_networks" ]]
then
default_network_id=`docker network ls -f "label=com.docker.compose.project=$docker_compose_project_name" -f "label=com.docker.compose.network=default" -q`
network_options=("--network" "$default_network_id")
fi
echo -n "."
volume_mounts=(--volume /tmp/mu/cache/$container_id/scripts/$folder_name:/script)
if [[ false != "$app_mount_point" ]]
then
volume_mounts+=(--volume $PWD:$app_mount_point)
fi
docker run ${network_options[@]} ${volume_mounts[@]} $it -w $working_directory --rm --entrypoint ./$entry_point $image_name "${arguments[@]}"
elif [[ -f "Dockerfile" ]]
then
# A script for developing a microservice
STATUS_MESSAGE="Discovering script "
status_echo
image_name=`cat Dockerfile | grep -oP "^FROM \\K.*"`
status_step # 1
command=$2
interactive_cli="docker exec -i mucli"
ensure_mu_cli_docker
status_step # 2
# jq commands
jq_documentation_filter_commands="jq -r '( .scripts[].documentation.command )'"
jq_documentation_get_command="jq -c '( .scripts[] | select(.documentation.command == \\\"$command\\\") )'"
jq_documentation_get_description="jq -r .documentation.description"
jq_documentation_get_arguments="jq -r .documentation.arguments[]"
jq_command_get_mount_point="jq -r .mounts.service"
jq_command_get_script="jq -r .environment.script"
jq_command_get_image="jq -r .environment.image"
status_step # 3
image_id=`docker images -q $image_name`
status_step # 4
# make sure we have the image available
if [[ -z image_id ]]
then
echo " need to fetch base image."
echo -n "Fetching service base image ... "
docker pull $image_name 2> /dev/null
if [[ "$?" -ne "0" ]]
then
echo ""
echo "Could not find the image."
echo "Check if the FROM in your Dockerfile is correct."
echo "If the image is not available publicly,"
echo "make sure to build it locally"
exit 1
fi
echo "DONE"
status_echo
image_id=`docker images -q $image_name`
fi
status_step # 5
docker run --name mu_cli_tmp_copy --entrypoint /bin/sh $image_name
status_step # 6
mkdir -p /tmp/mu/cache/$image_id
status_step # 7
docker cp mu_cli_tmp_copy:/app/scripts /tmp/mu/cache/$image_id 2> /dev/null
status_step # 8
# cleaning up copy container
docker rm -f mu_cli_tmp_copy 2> /dev/null > /dev/null
status_step # 9
config_location="/tmp/mu/cache/$image_id/scripts/config.json"
cat_config_command="cat $config_location"
if [[ "-h" == $command || "" == "$command" ]]
then
echo " DONE"
echo ""
print_service_scripts_documentation $config_location
exit 0
fi
status_step # 10
command_spec=`sh -c "$interactive_cli bash -c \"$cat_config_command | $jq_documentation_get_command\""`
status_step # 11
if [[ -z $command_spec ]] ;
then
echo " DONE"
print_text_block "" \
"Error: Script not found" \
" Could not find script $command in $image_name" \
"" \
print_service_scripts_documentation $config_location
exit 1
fi
status_step # 12
service_mount_point=`echo "$command_spec" | $interactive_cli $jq_command_get_mount_point`
status_step # 13
service_folder="$PWD"
status_step # 14
script_path=`echo "$command_spec" | $interactive_cli $jq_command_get_script`
status_step # 15
script_folder_name=`dirname $script_path`
script_file_name=`basename $script_path`
folder_name="$script_folder_name"
entry_point="$script_file_name"
working_directory="/script"
status_step # 16
arguments=("${@:3}")
status_step # 17
image_name=`echo "$command_spec" | $interactive_cli $jq_command_get_image`
status_step # 18
interactive_mode=`echo "$command_spec" | $interactive_cli jq -r '.environment.interactive // false'`
status_step # 19
it=""
if [[ true = "$interactive_mode" ]];
then
it=" -it "
fi
status_step # 20
# docker arguments
docker_volumes=(
--volume $PWD:$service_mount_point
--volume /tmp/mu/cache/$image_id/scripts/$folder_name:/script)
docker_environment_variables=(
-e SERVICE_HOST_DIR="$PWD/")
status_step # 21
echo " DONE"
echo "Executing script $command ${arguments[@]}"
docker run ${docker_volumes[@]} ${docker_environment_variables[@]} $it -w $working_directory --rm --entrypoint ./$entry_point $image_name "${arguments[@]}"
exit 0
else
echo "Did not recognise location"
echo ""
echo "Please make sure you are in the top-level folder of either:"
echo " - a project (containing a docker-compose.yml)"
echo " - a service (containing a Dockerfile)"
exit 1
fi
elif [[ "service" == $1 ]]
then
# Mu service commands
if [[ "new" == $2 ]]
then
KNOWN_LANGUAGES="ruby, javascript, python" # note repeated in if below
LANGUAGE=$3
SERVICE_NAME=$4
if [[ $LANGUAGE != "ruby" && $LANGUAGE != "javascript" && $LANGUAGE != "python" ]]
then
if [ -z $LANGUAGE ]
then
print_text_block "Please specify a service language." \
"" \
"The expected usage for this command is:" \
" mu service new [$KNOWN_LANGUAGES] [service name]"
else
print_text_block "Unknown service language." \
"" \
"The expected usage for this command is:" \
" mu service new [$KNOWN_LANGUAGES] [service name]"
fi
exit 1
fi
if [[ -z "$SERVICE_NAME" ]]
then
print_text_block "Please specify a service name." \
"" \
"The expected usage for this command is:" \
" mu service new $LANGUAGE [service name]"
exit 1
fi
echo "Creating new service"
if [[ "ruby" == $LANGUAGE ]]
then
USER_NAME=`git config user.name`
EMAIL=`git config user.email`
USER=`whoami`
echo "Creating new ruby service for $SERVICE_NAME"
mkdir $SERVICE_NAME
cd $SERVICE_NAME
echo "FROM semtech/mu-ruby-template:3.1.0" >> Dockerfile
echo "LABEL maintainer=\"$USER_NAME <$EMAIL>\"" >> Dockerfile
echo "# see https://github.com/mu-semtech/mu-ruby-template for more info" >> Dockerfile
echo "# see https://github.com/mu-semtech/mu-ruby-template for more info" >> web.rb
echo "get '/' do" >> web.rb
echo " content_type 'application/json'" >> web.rb
echo " { data: { attributes: { hello: 'world' } } }.to_json" >> web.rb
echo "end" >> web.rb
git init .
git add .
git commit -m "Initializing new mu ruby service"
echo "You can add the following snippet in your pipeline"
echo "to hack this service live."
DOCKER_SERVICE_NAME=`echo $SERVICE_NAME | sed -e s/-service$//`
echo ""
echo " $DOCKER_SERVICE_NAME:"
echo " image: semtech/mu-ruby-template:3.1.0"
echo " ports:"
echo ' - "8888:80"'
echo ' - "9229:9229"'
echo " environment:"
echo ' RACK_ENV: "development"'
echo " volumes:"
echo " - \"`pwd`/:/app\""
echo ""
echo "All set to to hack!"
elif [[ "javascript" == $LANGUAGE ]]
then
USER_NAME=`git config user.name`
EMAIL=`git config user.email`
USER=`whoami`
echo "Creating new javascript service for $SERVICE_NAME"
mkdir $SERVICE_NAME
cd $SERVICE_NAME
echo "FROM semtech/mu-javascript-template:1.8.0" >> Dockerfile
echo "LABEL maintainer=\"$USER_NAME <$EMAIL>\"" >> Dockerfile
echo "" >> Dockerfile
echo "# see https://github.com/mu-semtech/mu-javascript-template for more info" >> Dockerfile
echo "// see https://github.com/mu-semtech/mu-javascript-template for more info" >> app.js
echo "" >> app.js
echo "import { app, query, errorHandler } from 'mu';" >> app.js
echo "" >> app.js
echo "app.get('/', function( req, res ) {" >> app.js
echo " res.send('Hello mu-javascript-template');" >> app.js
echo "} );" >> app.js
echo "" >> app.js
echo "app.use(errorHandler);" >> app.js
git init .
git add .
git commit -m "Initializing new mu javascript service"
echo "You can add the following snippet in your pipeline"
echo "to hack this service live."
DOCKER_SERVICE_NAME=`echo $SERVICE_NAME | sed -e s/-service$//`
echo ""
echo " $DOCKER_SERVICE_NAME:"
echo " image: semtech/mu-javascript-template:1.8.0"
echo " ports:"
echo ' - "8888:80"'
echo ' - "9229:9229"'
echo " environment:"
echo ' NODE_ENV: "development"'
echo " volumes:"
echo " - \"`pwd`/:/app\""
echo ""
echo "All set to to hack!"
elif [[ "python" == $LANGUAGE ]]
then
USER_NAME=`git config user.name`
EMAIL=`git config user.email`
USER=`whoami`
echo "Creating new python service for $SERVICE_NAME"
mkdir $SERVICE_NAME
cd $SERVICE_NAME
echo "FROM semtech/mu-python-template:2.0.0-beta.1" >> Dockerfile
echo "LABEL maintainer=\"$USER_NAME <$EMAIL>\"" >> Dockerfile
echo "" >> Dockerfile
echo "# see https://github.com/mu-semtech/mu-python-template for more info" >> Dockerfile
echo "# see https://github.com/mu-semtech/mu-python-template for more info" >> web.py
echo "" >> web.py
echo "@app.route(\"/hello\")" >> web.py
echo "def hello():" >> web.py
echo " return \"Hello from the mu-python-template!\"" >> web.py
echo "" >> web.py
git init .
git add .
git commit -m "Initializing new mu python service"
echo "You can add the following snippet in your pipeline"
echo "to hack this service live."
DOCKER_SERVICE_NAME=`echo $SERVICE_NAME | sed -e s/-service$//`
echo ""
echo " $DOCKER_SERVICE_NAME:"
echo " image: semtech/mu-python-template:2.0.0-beta.1"
echo " ports:"
echo ' - "8888:80"'
echo " environment:"
echo ' MODE: "development"'
echo " volumes:"
echo " - \"`pwd`/:/app\""
echo ""
echo "All set to to hack!"
fi
else
if [[ -z "$2" ]]
then
echo "Please specify argument for service command"
else
echo "Don't know service argument $2"
fi
echo "Known arguments: [ shell, new ]"
fi
else
echo "Don't know command $1"
echo "Known commands [ project, logs, service, script, start ]"
fi