@colisweb/rescript-toolkit 5.8.0 → 5.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.gitlab-ci.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  include:
2
- - "https://colisweb-idl.gitlab.io/colisweb-open-source/ci-common/v17.7.0/templates/front.yml"
2
+ - "https://colisweb-idl.gitlab.io/colisweb-open-source/ci-common/v17.12.0/templates/front.yml"
3
3
 
4
4
  variables:
5
5
  GIT_COMMIT_FILES: ""
@@ -596,7 +596,7 @@ mysql_on_k8() {
596
596
  local query=$7
597
597
 
598
598
  kubectl -n ${namespace} run ${service}-mysql-init \
599
- --image widdpim/mysql-client \
599
+ --image arey/mysql-client \
600
600
  --restart=Never \
601
601
  --attach --rm \
602
602
  -- \
@@ -676,14 +676,14 @@ kube_init_datadog_in_database() {
676
676
  extract_args 8 namespace db_host db_port db_init_username db_init_password db_datadog_username db_datadog_password db_datadog_schema $*
677
677
 
678
678
  echo "======================="
679
- echo " Initializing Datadog Agent Requiement for namespace $namespace"
679
+ echo " Initializing Datadog Agent Requirement for namespace $namespace"
680
680
  echo "======================="
681
681
 
682
682
  echo "Checking if User '$db_datadog_username' exists"
683
683
  local service="datadog"
684
684
  found_db_users=$(mysql_on_k8 $namespace $service $db_host $db_port $db_init_username $db_init_password 'SELECT user FROM mysql.user;')
685
685
  set +e
686
- echo $found_db_users | grep "^$db_datadog_username$"
686
+ echo "$found_db_users" | grep "^$db_datadog_username$"
687
687
  return_code=$?
688
688
  set -e
689
689
 
@@ -873,6 +873,57 @@ kube_init_service_database() {
873
873
 
874
874
  #!/usr/bin/env bash
875
875
 
876
+ # Allow to use JMX connection to retrieve data and metrics from the pods within kubernetes
877
+ # You will need visualVM to use this tool https://visualvm.github.io/
878
+ # ex: bind_jmx testing notification
879
+ bind_jmx() {
880
+
881
+ local ENV=$1
882
+ local SERVICE_NAME=$2
883
+ local PORT=2242
884
+
885
+ start_ssh_bastion $ENV $PORT
886
+
887
+ echo "root" | ssh -f -N -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no -D 7777 root@127.0.0.1 -p 2242
888
+ local PODS=$(kubectl -n $ENV get pods -o wide | grep $SERVICE_NAME | grep -Eo '^[^ ]+')
889
+
890
+ echo "Choose one of the following pod to get metrics from..."
891
+ local POD_NAME=$(gum choose $PODS)
892
+ local POD_IP=$(
893
+ kubectl -n $ENV get pods -o jsonpath='{range .items[*]}{.metadata.name}{" "}{.status.podIP}{"\n"}{end}' |
894
+ grep $POD_NAME |
895
+ cut -d' ' -f2 |
896
+ head -1
897
+ )
898
+
899
+ jconsole -J-DsocksProxyHost=localhost \
900
+ -J-DsocksProxyPort=7777 \
901
+ service:jmx:rmi:///jndi/rmi://$POD_IP:7199/jmxrmi \
902
+ -J-DsocksNonProxyHosts= &
903
+
904
+ cat << EOF
905
+ Now start VisualVM
906
+ Preferences > Network > Manual Proxy Settings
907
+ SOCKS Proxy Line: Set 'localhost' and Port '7777'
908
+ File > Add JMX Connection
909
+ Set $POD_IP:7199, check 'do not require an SSL connection'
910
+ Remember to kill you bastion afterward using 'stop_ssh_bastion'
911
+ EOF
912
+ }
913
+ #!/usr/bin/env bash
914
+
915
+ k8_nodes_stats() {
916
+ kubectl get nodes -o name |
917
+ xargs kubectl describe |
918
+ grep "^Name\|workType\|cpu \|memory " |
919
+ sed -r 's/[ :=]+/\t/g' |
920
+ sed 's/\tworkType\t//g' |
921
+ sed -r 's/^Name/---\nName/g' |
922
+ grep --color "Name\|web\|workers\|cpu\|memory\|---"
923
+ }
924
+
925
+ #!/usr/bin/env bash
926
+
876
927
  # Port forward on the first matching pod
877
928
  # Ex :
878
929
  # pod_forward testing notification-http
@@ -950,12 +1001,58 @@ pick_pod() {
950
1001
 
951
1002
  #!/usr/bin/env bash
952
1003
 
1004
+ bastion_config_for_redis_ca() {
1005
+ ssh_config xufte6.0001.euw1.cache.amazonaws.com redis 2223 63789 tests testing recette-001 sandbox prod > $1
1006
+ }
1007
+
1008
+ bastion_config_for_redis_toutatis() {
1009
+ ssh_config xufte6.0001.euw1.cache.amazonaws.com toutatis 2223 63789 tests testing recette staging production > $1
1010
+ }
1011
+
1012
+ ssh_config() {
1013
+ host=$1
1014
+ host_prefix=$2
1015
+ port0=$3
1016
+ forward0=$4
1017
+ shift 4
1018
+ instance_names=("$@") # /!\ indices start at 1 with zsh
1019
+ ssh_header
1020
+
1021
+ environments=(tests testing recette staging production)
1022
+
1023
+ length=${#environments[@]}
1024
+ for (( i=1; i<=${length}; i++ ));
1025
+ do
1026
+ bastion_block bastion_${environments[$i]} $(($port0 + $i)) $(($forward0 + $i)) ${host_prefix}-${instance_names[$i]}.$host
1027
+ done
1028
+ }
1029
+
1030
+ ssh_header() {
1031
+ cat <<EOF
1032
+ UserKnownHostsFile /dev/null
1033
+ StrictHostKeyChecking no
1034
+ User root
1035
+ EOF
1036
+ }
1037
+
1038
+ bastion_block() {
1039
+ cat <<EOF
1040
+ Host $1
1041
+ HostName 127.0.0.1
1042
+ Port $2
1043
+ LocalForward $3 $4:6379
1044
+ EOF
1045
+ }
1046
+
953
1047
  redis_k8s() {
954
1048
  MODE=$1
1049
+ REDIS_INSTANCE=${2:-ca}
955
1050
  case $MODE in
1051
+ "tests") SSH_LOCAL_PORT=2224;REDIS_LOCAL_PORT=63790;ENV="tests";;
956
1052
  "testing") SSH_LOCAL_PORT=2225;REDIS_LOCAL_PORT=63791;ENV="testing";;
957
- "staging") SSH_LOCAL_PORT=2226;REDIS_LOCAL_PORT=63792;ENV="staging";;
958
- "production") SSH_LOCAL_PORT=2227;REDIS_LOCAL_PORT=63793;ENV="production";;
1053
+ "recette") SSH_LOCAL_PORT=2226;REDIS_LOCAL_PORT=63792;ENV="recette";;
1054
+ "staging") SSH_LOCAL_PORT=2227;REDIS_LOCAL_PORT=63793;ENV="staging";;
1055
+ "production") SSH_LOCAL_PORT=2228;REDIS_LOCAL_PORT=63794;ENV="production";;
959
1056
  *) echo "Unsupported ENV : $MODE"; return 1 ;;
960
1057
  esac
961
1058
 
@@ -964,23 +1061,11 @@ redis_k8s() {
964
1061
  lsof -ti tcp:$REDIS_LOCAL_PORT | xargs kill
965
1062
 
966
1063
  bastion_config=$(mktemp)
967
- cat > "$bastion_config" <<EOF
968
- UserKnownHostsFile /dev/null
969
- StrictHostKeyChecking no
970
- User root
971
- Host bastion_testing
972
- HostName 127.0.0.1
973
- Port 2225
974
- LocalForward 63791 redis-testing.xufte6.0001.euw1.cache.amazonaws.com:6379
975
- Host bastion_staging
976
- HostName 127.0.0.1
977
- Port 2226
978
- LocalForward 63792 redis-sandbox.xufte6.0001.euw1.cache.amazonaws.com:6379
979
- Host bastion_production
980
- HostName 127.0.0.1
981
- Port 2227
982
- LocalForward 63793 redis-prod.xufte6.0001.euw1.cache.amazonaws.com:6379
983
- EOF
1064
+ case $REDIS_INSTANCE in
1065
+ "ca") bastion_config_for_redis_ca "$bastion_config";;
1066
+ "toutatis") bastion_config_for_redis_toutatis "$bastion_config";;
1067
+ *) echo "Unsupported redis instance (ca or toutatis available) : $REDIS_INSTANCE"; return 1;;
1068
+ esac
984
1069
 
985
1070
  ssh -f -N \
986
1071
  -F "$bastion_config" \
@@ -1823,11 +1908,11 @@ datadog_schedule_downtime_single() {
1823
1908
  {
1824
1909
  "active": true,
1825
1910
  "downtime_type": 0,
1826
- "start": $START,
1827
- "end": $END,
1828
- "message": "CA Deployment - performance for $SERVICE may be lower for next $DOWNTIME_MINUTES min",
1911
+ "start": '$START',
1912
+ "end": '$END',
1913
+ "message": "CA Deployment - performance for '$SERVICE' may be lower for next '$DOWNTIME_MINUTES' min",
1829
1914
  "monitor_tags": [
1830
- "service:$SERVICE",
1915
+ "service:'$SERVICE'",
1831
1916
  "performance"
1832
1917
  ],
1833
1918
  "scope": [
@@ -1837,6 +1922,7 @@ datadog_schedule_downtime_single() {
1837
1922
  }
1838
1923
  '
1839
1924
  }
1925
+
1840
1926
  #!/usr/bin/env bash
1841
1927
 
1842
1928
  docker_build_push() {
@@ -1992,7 +2078,7 @@ init_migrate_db() {
1992
2078
 
1993
2079
  unset KUBECONFIG
1994
2080
 
1995
- configure_kubectl_for_ci ${ENVIRONMENT}
2081
+ configure_kubectl_for ${ENVIRONMENT}
1996
2082
 
1997
2083
  kube_init_service_database \
1998
2084
  --namespace ${ENVIRONMENT} \
@@ -2038,7 +2124,7 @@ flyway_migrate() {
2038
2124
  CONFIGMAP_NAME="$service-flyway-migration-sql"
2039
2125
  POD_NAME="$service-flyway-migration"
2040
2126
 
2041
- configure_kubectl_for_ci $environment
2127
+ configure_kubectl_for $environment
2042
2128
 
2043
2129
  kubectl -n $namespace delete configmap $CONFIGMAP_NAME --ignore-not-found
2044
2130
  kubectl -n $namespace delete pod $POD_NAME --ignore-not-found
@@ -2091,7 +2177,7 @@ flyway_migrate() {
2091
2177
 
2092
2178
  flyway_sql_folder=$(pwd)/${MIGRATION_SQL_PATH}
2093
2179
 
2094
- configure_kubectl_for_ci "${ENVIRONMENT}"
2180
+ configure_kubectl_for "${ENVIRONMENT}"
2095
2181
  POD_NAME="${APPLICATION}-flyway-repair"
2096
2182
  CONFIGMAP_NAME="${APPLICATION}-flyway-repair-sql"
2097
2183
 
@@ -2217,7 +2303,7 @@ deploy_chart_v3() {
2217
2303
  unset KUBECONFIG
2218
2304
 
2219
2305
  # Configure Kubectl
2220
- configure_kubectl_for_ci ${environment}
2306
+ configure_kubectl_for ${environment}
2221
2307
 
2222
2308
  # Configure helm3
2223
2309
  helm3 version --namespace ${namespace} || true
@@ -2318,40 +2404,6 @@ check_config_file() {
2318
2404
  fi
2319
2405
  }
2320
2406
 
2321
- #!/usr/bin/env bash
2322
-
2323
- configure_kubectl_for_ci() {
2324
- if [ -z ${GITLAB_PAT} ]; then
2325
- echo "Cannot configure kubectl: no GITLAB_PAT configured"
2326
- exit 1
2327
- fi
2328
-
2329
- infra_env="$1"
2330
- valid_envs="[testing][staging][production][performance][tests][recette]"
2331
- echo "$valid_envs" | grep -q "\[$infra_env\]"
2332
-
2333
- if [ $? -ne 0 ]; then
2334
- echo "Cannot configure kubectl for invalid env : $infra_env"
2335
- echo "choose one of $valid_envs"
2336
- exit 1
2337
- fi
2338
-
2339
- mkdir -p ~/.kube
2340
- curl -fsS \
2341
- --header "PRIVATE-TOKEN: $GITLAB_PAT" \
2342
- "https://gitlab.com/api/v4/projects/8141053/jobs/artifacts/$infra_env/raw/$infra_env.kubeconfig?job=4_kubernetes_config_output" \
2343
- > ~/.kube/$infra_env.kubeconfig
2344
-
2345
- curl_return_code=$?
2346
- if [ ${curl_return_code} -ne 0 ]; then
2347
- echo "Cannot configure kubectl for $infra_env, get configuration failed with code $curl_return_code"
2348
- exit ${curl_return_code}
2349
- fi
2350
-
2351
- rm -f ~/.kube/config
2352
- ln -s ~/.kube/$infra_env.kubeconfig ~/.kube/config
2353
- echo "Configured kubectl for env : $infra_env"
2354
- }
2355
2407
  notify_new_deployment() {
2356
2408
  jq --version || (apt update && apt install -y jq)
2357
2409
 
@@ -577,7 +577,7 @@ psql_on_k8() {
577
577
  CONNECTION=$3
578
578
  shift 3
579
579
 
580
- kubectl -n $NAMESPACE run ${SERVICE}-database-init \
580
+ kubectl -n $NAMESPACE run ${SERVICE}-postgres-init \
581
581
  --image jbergknoff/postgresql-client \
582
582
  --restart=Never \
583
583
  --attach --rm \
@@ -588,14 +588,15 @@ psql_on_k8() {
588
588
 
589
589
  mysql_on_k8() {
590
590
  local namespace=$1
591
- local db_host=$2
592
- local db_port=$3
593
- local db_init_username=$4
594
- local db_init_password=$5
595
- local query=$6
596
-
597
- kubectl -n ${namespace} run datadog-database-init \
598
- --image widdpim/mysql-client \
591
+ local service=$2
592
+ local db_host=$3
593
+ local db_port=$4
594
+ local db_init_username=$5
595
+ local db_init_password=$6
596
+ local query=$7
597
+
598
+ kubectl -n ${namespace} run ${service}-mysql-init \
599
+ --image arey/mysql-client \
599
600
  --restart=Never \
600
601
  --attach --rm \
601
602
  -- \
@@ -675,12 +676,14 @@ kube_init_datadog_in_database() {
675
676
  extract_args 8 namespace db_host db_port db_init_username db_init_password db_datadog_username db_datadog_password db_datadog_schema $*
676
677
 
677
678
  echo "======================="
678
- echo " Initializing Datadog Agent Requiement for namespace $namespace"
679
+ echo " Initializing Datadog Agent Requirement for namespace $namespace"
679
680
  echo "======================="
680
681
 
681
682
  echo "Checking if User '$db_datadog_username' exists"
683
+ local service="datadog"
684
+ found_db_users=$(mysql_on_k8 $namespace $service $db_host $db_port $db_init_username $db_init_password 'SELECT user FROM mysql.user;')
682
685
  set +e
683
- mysql_on_k8 $namespace $db_host $db_port $db_init_username $db_init_password 'SELECT user FROM mysql.user;' | grep "^$db_datadog_username$"
686
+ echo "$found_db_users" | grep "^$db_datadog_username$"
684
687
  return_code=$?
685
688
  set -e
686
689
 
@@ -691,29 +694,29 @@ kube_init_datadog_in_database() {
691
694
 
692
695
  # All the query come from this docs : https://docs.datadoghq.com/fr/database_monitoring/setup_mysql/selfhosted/?tab=mysql56
693
696
 
694
- mysql_on_k8 $namespace $db_host $db_port $db_init_username $db_init_password 'CREATE USER '"$db_datadog_username"'@"%" IDENTIFIED BY '"'$db_datadog_password'"';'
697
+ mysql_on_k8 $namespace $service $db_host $db_port $db_init_username $db_init_password 'CREATE USER '"$db_datadog_username"'@"%" IDENTIFIED BY '"'$db_datadog_password'"';'
695
698
  echo "USER created $db_datadog_username"
696
699
 
697
- mysql_on_k8 $namespace $db_host $db_port $db_init_username $db_init_password 'GRANT REPLICATION CLIENT ON *.* TO datadog@"%" WITH MAX_USER_CONNECTIONS 5;'
700
+ mysql_on_k8 $namespace $service $db_host $db_port $db_init_username $db_init_password 'GRANT REPLICATION CLIENT ON *.* TO datadog@"%" WITH MAX_USER_CONNECTIONS 5;'
698
701
  echo "ALTER USER $db_datadog_username"
699
702
 
700
- mysql_on_k8 $namespace $db_host $db_port $db_init_username $db_init_password 'GRANT PROCESS ON *.* TO '"$db_datadog_username"'@"%";'
703
+ mysql_on_k8 $namespace $service $db_host $db_port $db_init_username $db_init_password 'GRANT PROCESS ON *.* TO '"$db_datadog_username"'@"%";'
701
704
  echo "Granted PROCESS for $db_datadog_username"
702
705
 
703
- mysql_on_k8 $namespace $db_host $db_port $db_init_username $db_init_password 'GRANT SELECT ON performance_schema.* TO '"$db_datadog_username"'@"%";'
706
+ mysql_on_k8 $namespace $service $db_host $db_port $db_init_username $db_init_password 'GRANT SELECT ON performance_schema.* TO '"$db_datadog_username"'@"%";'
704
707
  echo "Granted SELECT on performance_schema for $db_datadog_username"
705
708
 
706
- mysql_on_k8 $namespace $db_host $db_port $db_init_username $db_init_password 'CREATE SCHEMA IF NOT EXISTS datadog;'
709
+ mysql_on_k8 $namespace $service $db_host $db_port $db_init_username $db_init_password 'CREATE SCHEMA IF NOT EXISTS datadog;'
707
710
  echo "CREATE SCHEMA datadog"
708
711
 
709
- mysql_on_k8 $namespace $db_host $db_port $db_init_username $db_init_password 'GRANT EXECUTE ON datadog.* to '"$db_datadog_username"'@"%";'
712
+ mysql_on_k8 $namespace $service $db_host $db_port $db_init_username $db_init_password 'GRANT EXECUTE ON datadog.* to '"$db_datadog_username"'@"%";'
710
713
  echo "Granted 'GRANT EXECUTE for $db_datadog_username on datadog"
711
714
 
712
- mysql_on_k8 $namespace $db_host $db_port $db_init_username $db_init_password 'GRANT CREATE TEMPORARY TABLES ON datadog.* TO '"$db_datadog_username"'@"%";'
715
+ mysql_on_k8 $namespace $service $db_host $db_port $db_init_username $db_init_password 'GRANT CREATE TEMPORARY TABLES ON datadog.* TO '"$db_datadog_username"'@"%";'
713
716
  echo "Granted CREATE TEMPORARY TABLES for $db_datadog_username"
714
717
 
715
718
 
716
- mysql_on_k8 $namespace $db_host $db_port $db_init_username $db_init_password 'DROP PROCEDURE IF EXISTS datadog.explain_statement;
719
+ mysql_on_k8 $namespace $service $db_host $db_port $db_init_username $db_init_password 'DROP PROCEDURE IF EXISTS datadog.explain_statement;
717
720
  DELIMITER $$
718
721
  CREATE PROCEDURE datadog.explain_statement(IN query TEXT)
719
722
  SQL SECURITY DEFINER
@@ -726,7 +729,7 @@ kube_init_datadog_in_database() {
726
729
  DELIMITER ;'
727
730
  echo "CREATE PROCEDURE PROCEDURE datadog.explain_statement"
728
731
 
729
- mysql_on_k8 $namespace $db_host $db_port $db_init_username $db_init_password 'DROP PROCEDURE IF EXISTS '"$db_datadog_username"'.explain_statement;
732
+ mysql_on_k8 $namespace $service $db_host $db_port $db_init_username $db_init_password 'DROP PROCEDURE IF EXISTS '"$db_datadog_username"'.explain_statement;
730
733
  DELIMITER $$
731
734
  CREATE PROCEDURE '"$db_datadog_username"'.explain_statement(IN query TEXT)
732
735
  SQL SECURITY DEFINER
@@ -740,7 +743,7 @@ kube_init_datadog_in_database() {
740
743
  GRANT EXECUTE ON PROCEDURE '"$db_datadog_username"'.explain_statement TO datadog@"%";'
741
744
  echo "CREATE PROCEDURE on SCHEMA $db_datadog_schema for $db_datadog_username"
742
745
 
743
- mysql_on_k8 $namespace $db_host $db_port $db_init_username $db_init_password 'DROP PROCEDURE IF EXISTS datadog.enable_events_statements_consumers;
746
+ mysql_on_k8 $namespace $service $db_host $db_port $db_init_username $db_init_password 'DROP PROCEDURE IF EXISTS datadog.enable_events_statements_consumers;
744
747
  DELIMITER $$
745
748
  CREATE PROCEDURE datadog.enable_events_statements_consumers()
746
749
  SQL SECURITY DEFINER
@@ -818,7 +821,7 @@ kube_init_datadog_in_postgres_database() {
818
821
  RETURNS NULL ON NULL INPUT
819
822
  SECURITY DEFINER;"
820
823
 
821
- kubectl -n $namespace run $service-database-init \
824
+ kubectl -n $namespace run $service-postgres-init \
822
825
  --image jbergknoff/postgresql-client \
823
826
  --restart=Never \
824
827
  --attach --rm \
@@ -870,6 +873,57 @@ kube_init_service_database() {
870
873
 
871
874
  #!/usr/bin/env bash
872
875
 
876
+ # Allow to use JMX connection to retrieve data and metrics from the pods within kubernetes
877
+ # You will need visualVM to use this tool https://visualvm.github.io/
878
+ # ex: bind_jmx testing notification
879
+ bind_jmx() {
880
+
881
+ local ENV=$1
882
+ local SERVICE_NAME=$2
883
+ local PORT=2242
884
+
885
+ start_ssh_bastion $ENV $PORT
886
+
887
+ echo "root" | ssh -f -N -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no -D 7777 root@127.0.0.1 -p 2242
888
+ local PODS=$(kubectl -n $ENV get pods -o wide | grep $SERVICE_NAME | grep -Eo '^[^ ]+')
889
+
890
+ echo "Choose one of the following pod to get metrics from..."
891
+ local POD_NAME=$(gum choose $PODS)
892
+ local POD_IP=$(
893
+ kubectl -n $ENV get pods -o jsonpath='{range .items[*]}{.metadata.name}{" "}{.status.podIP}{"\n"}{end}' |
894
+ grep $POD_NAME |
895
+ cut -d' ' -f2 |
896
+ head -1
897
+ )
898
+
899
+ jconsole -J-DsocksProxyHost=localhost \
900
+ -J-DsocksProxyPort=7777 \
901
+ service:jmx:rmi:///jndi/rmi://$POD_IP:7199/jmxrmi \
902
+ -J-DsocksNonProxyHosts= &
903
+
904
+ cat << EOF
905
+ Now start VisualVM
906
+ Preferences > Network > Manual Proxy Settings
907
+ SOCKS Proxy Line: Set 'localhost' and Port '7777'
908
+ File > Add JMX Connection
909
+ Set $POD_IP:7199, check 'do not require an SSL connection'
910
+ Remember to kill you bastion afterward using 'stop_ssh_bastion'
911
+ EOF
912
+ }
913
+ #!/usr/bin/env bash
914
+
915
+ k8_nodes_stats() {
916
+ kubectl get nodes -o name |
917
+ xargs kubectl describe |
918
+ grep "^Name\|workType\|cpu \|memory " |
919
+ sed -r 's/[ :=]+/\t/g' |
920
+ sed 's/\tworkType\t//g' |
921
+ sed -r 's/^Name/---\nName/g' |
922
+ grep --color "Name\|web\|workers\|cpu\|memory\|---"
923
+ }
924
+
925
+ #!/usr/bin/env bash
926
+
873
927
  # Port forward on the first matching pod
874
928
  # Ex :
875
929
  # pod_forward testing notification-http
@@ -947,12 +1001,58 @@ pick_pod() {
947
1001
 
948
1002
  #!/usr/bin/env bash
949
1003
 
1004
+ bastion_config_for_redis_ca() {
1005
+ ssh_config xufte6.0001.euw1.cache.amazonaws.com redis 2223 63789 tests testing recette-001 sandbox prod > $1
1006
+ }
1007
+
1008
+ bastion_config_for_redis_toutatis() {
1009
+ ssh_config xufte6.0001.euw1.cache.amazonaws.com toutatis 2223 63789 tests testing recette staging production > $1
1010
+ }
1011
+
1012
+ ssh_config() {
1013
+ host=$1
1014
+ host_prefix=$2
1015
+ port0=$3
1016
+ forward0=$4
1017
+ shift 4
1018
+ instance_names=("$@") # /!\ indices start at 1 with zsh
1019
+ ssh_header
1020
+
1021
+ environments=(tests testing recette staging production)
1022
+
1023
+ length=${#environments[@]}
1024
+ for (( i=1; i<=${length}; i++ ));
1025
+ do
1026
+ bastion_block bastion_${environments[$i]} $(($port0 + $i)) $(($forward0 + $i)) ${host_prefix}-${instance_names[$i]}.$host
1027
+ done
1028
+ }
1029
+
1030
+ ssh_header() {
1031
+ cat <<EOF
1032
+ UserKnownHostsFile /dev/null
1033
+ StrictHostKeyChecking no
1034
+ User root
1035
+ EOF
1036
+ }
1037
+
1038
+ bastion_block() {
1039
+ cat <<EOF
1040
+ Host $1
1041
+ HostName 127.0.0.1
1042
+ Port $2
1043
+ LocalForward $3 $4:6379
1044
+ EOF
1045
+ }
1046
+
950
1047
  redis_k8s() {
951
1048
  MODE=$1
1049
+ REDIS_INSTANCE=${2:-ca}
952
1050
  case $MODE in
1051
+ "tests") SSH_LOCAL_PORT=2224;REDIS_LOCAL_PORT=63790;ENV="tests";;
953
1052
  "testing") SSH_LOCAL_PORT=2225;REDIS_LOCAL_PORT=63791;ENV="testing";;
954
- "staging") SSH_LOCAL_PORT=2226;REDIS_LOCAL_PORT=63792;ENV="staging";;
955
- "production") SSH_LOCAL_PORT=2227;REDIS_LOCAL_PORT=63793;ENV="production";;
1053
+ "recette") SSH_LOCAL_PORT=2226;REDIS_LOCAL_PORT=63792;ENV="recette";;
1054
+ "staging") SSH_LOCAL_PORT=2227;REDIS_LOCAL_PORT=63793;ENV="staging";;
1055
+ "production") SSH_LOCAL_PORT=2228;REDIS_LOCAL_PORT=63794;ENV="production";;
956
1056
  *) echo "Unsupported ENV : $MODE"; return 1 ;;
957
1057
  esac
958
1058
 
@@ -961,23 +1061,11 @@ redis_k8s() {
961
1061
  lsof -ti tcp:$REDIS_LOCAL_PORT | xargs kill
962
1062
 
963
1063
  bastion_config=$(mktemp)
964
- cat > "$bastion_config" <<EOF
965
- UserKnownHostsFile /dev/null
966
- StrictHostKeyChecking no
967
- User root
968
- Host bastion_testing
969
- HostName 127.0.0.1
970
- Port 2225
971
- LocalForward 63791 redis-testing.xufte6.0001.euw1.cache.amazonaws.com:6379
972
- Host bastion_staging
973
- HostName 127.0.0.1
974
- Port 2226
975
- LocalForward 63792 redis-sandbox.xufte6.0001.euw1.cache.amazonaws.com:6379
976
- Host bastion_production
977
- HostName 127.0.0.1
978
- Port 2227
979
- LocalForward 63793 redis-prod.xufte6.0001.euw1.cache.amazonaws.com:6379
980
- EOF
1064
+ case $REDIS_INSTANCE in
1065
+ "ca") bastion_config_for_redis_ca "$bastion_config";;
1066
+ "toutatis") bastion_config_for_redis_toutatis "$bastion_config";;
1067
+ *) echo "Unsupported redis instance (ca or toutatis available) : $REDIS_INSTANCE"; return 1;;
1068
+ esac
981
1069
 
982
1070
  ssh -f -N \
983
1071
  -F "$bastion_config" \
@@ -1820,11 +1908,11 @@ datadog_schedule_downtime_single() {
1820
1908
  {
1821
1909
  "active": true,
1822
1910
  "downtime_type": 0,
1823
- "start": $START,
1824
- "end": $END,
1825
- "message": "CA Deployment - performance for $SERVICE may be lower for next $DOWNTIME_MINUTES min",
1911
+ "start": '$START',
1912
+ "end": '$END',
1913
+ "message": "CA Deployment - performance for '$SERVICE' may be lower for next '$DOWNTIME_MINUTES' min",
1826
1914
  "monitor_tags": [
1827
- "service:$SERVICE",
1915
+ "service:'$SERVICE'",
1828
1916
  "performance"
1829
1917
  ],
1830
1918
  "scope": [
@@ -1834,6 +1922,7 @@ datadog_schedule_downtime_single() {
1834
1922
  }
1835
1923
  '
1836
1924
  }
1925
+
1837
1926
  #!/usr/bin/env bash
1838
1927
 
1839
1928
  docker_build_push() {
@@ -1989,7 +2078,7 @@ init_migrate_db() {
1989
2078
 
1990
2079
  unset KUBECONFIG
1991
2080
 
1992
- configure_kubectl_for_ci ${ENVIRONMENT}
2081
+ configure_kubectl_for ${ENVIRONMENT}
1993
2082
 
1994
2083
  kube_init_service_database \
1995
2084
  --namespace ${ENVIRONMENT} \
@@ -2035,7 +2124,7 @@ flyway_migrate() {
2035
2124
  CONFIGMAP_NAME="$service-flyway-migration-sql"
2036
2125
  POD_NAME="$service-flyway-migration"
2037
2126
 
2038
- configure_kubectl_for_ci $environment
2127
+ configure_kubectl_for $environment
2039
2128
 
2040
2129
  kubectl -n $namespace delete configmap $CONFIGMAP_NAME --ignore-not-found
2041
2130
  kubectl -n $namespace delete pod $POD_NAME --ignore-not-found
@@ -2088,7 +2177,7 @@ flyway_migrate() {
2088
2177
 
2089
2178
  flyway_sql_folder=$(pwd)/${MIGRATION_SQL_PATH}
2090
2179
 
2091
- configure_kubectl_for_ci "${ENVIRONMENT}"
2180
+ configure_kubectl_for "${ENVIRONMENT}"
2092
2181
  POD_NAME="${APPLICATION}-flyway-repair"
2093
2182
  CONFIGMAP_NAME="${APPLICATION}-flyway-repair-sql"
2094
2183
 
@@ -2214,7 +2303,7 @@ deploy_chart_v3() {
2214
2303
  unset KUBECONFIG
2215
2304
 
2216
2305
  # Configure Kubectl
2217
- configure_kubectl_for_ci ${environment}
2306
+ configure_kubectl_for ${environment}
2218
2307
 
2219
2308
  # Configure helm3
2220
2309
  helm3 version --namespace ${namespace} || true
@@ -2315,40 +2404,6 @@ check_config_file() {
2315
2404
  fi
2316
2405
  }
2317
2406
 
2318
- #!/usr/bin/env bash
2319
-
2320
- configure_kubectl_for_ci() {
2321
- if [ -z ${GITLAB_PAT} ]; then
2322
- echo "Cannot configure kubectl: no GITLAB_PAT configured"
2323
- exit 1
2324
- fi
2325
-
2326
- infra_env="$1"
2327
- valid_envs="[testing][staging][production][performance][tests][recette]"
2328
- echo "$valid_envs" | grep -q "\[$infra_env\]"
2329
-
2330
- if [ $? -ne 0 ]; then
2331
- echo "Cannot configure kubectl for invalid env : $infra_env"
2332
- echo "choose one of $valid_envs"
2333
- exit 1
2334
- fi
2335
-
2336
- mkdir -p ~/.kube
2337
- curl -fsS \
2338
- --header "PRIVATE-TOKEN: $GITLAB_PAT" \
2339
- "https://gitlab.com/api/v4/projects/8141053/jobs/artifacts/$infra_env/raw/$infra_env.kubeconfig?job=4_kubernetes_config_output" \
2340
- > ~/.kube/$infra_env.kubeconfig
2341
-
2342
- curl_return_code=$?
2343
- if [ ${curl_return_code} -ne 0 ]; then
2344
- echo "Cannot configure kubectl for $infra_env, get configuration failed with code $curl_return_code"
2345
- exit ${curl_return_code}
2346
- fi
2347
-
2348
- rm -f ~/.kube/config
2349
- ln -s ~/.kube/$infra_env.kubeconfig ~/.kube/config
2350
- echo "Configured kubectl for env : $infra_env"
2351
- }
2352
2407
  notify_new_deployment() {
2353
2408
  jq --version || (apt update && apt install -y jq)
2354
2409