@colisweb/rescript-toolkit 5.47.1 → 5.47.2

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.
@@ -62,7 +62,7 @@ extract_arg() {
62
62
  value=$3
63
63
  if [ "--$name" != "$passed" ]; then
64
64
  echo "missing argument $name"
65
- exit 1
65
+ return 1
66
66
  fi
67
67
  eval $name='$value'
68
68
  }
@@ -452,12 +452,12 @@ configure_kubectl_for() {
452
452
  database_k8s() {
453
453
  MODE=$1
454
454
  case $MODE in
455
- "tests") SSH_LOCAL_PORT=2224;PG_LOCAL_PORT=24440;CA_LOCAL_PORT=25430;ENV="tests";;
456
- "testing") SSH_LOCAL_PORT=2225;PG_LOCAL_PORT=24441;CA_LOCAL_PORT=25431;ENV="testing";;
457
- "staging") SSH_LOCAL_PORT=2226;PG_LOCAL_PORT=24442;CA_LOCAL_PORT=25432;ENV="staging";;
458
- "production") SSH_LOCAL_PORT=2227;PG_LOCAL_PORT=24443;CA_LOCAL_PORT=25433;ENV="production";;
459
- "production_rw") SSH_LOCAL_PORT=2227;PG_LOCAL_PORT=24444;CA_LOCAL_PORT=25434;ENV="production";;
460
- "recette") SSH_LOCAL_PORT=2228;PG_LOCAL_PORT=24446;CA_LOCAL_PORT=25436;ENV="recette";;
455
+ "tests") SSH_LOCAL_PORT=2224;COMP_LOCAL_PORT=25550;PG_LOCAL_PORT=24440;CA_LOCAL_PORT=25430;ENV="tests";;
456
+ "testing") SSH_LOCAL_PORT=2225;COMP_LOCAL_PORT=25551;PG_LOCAL_PORT=24441;CA_LOCAL_PORT=25431;ENV="testing";;
457
+ "staging") SSH_LOCAL_PORT=2226;COMP_LOCAL_PORT=25552;PG_LOCAL_PORT=24442;CA_LOCAL_PORT=25432;ENV="staging";;
458
+ "production") SSH_LOCAL_PORT=2227;COMP_LOCAL_PORT=25553;PG_LOCAL_PORT=24443;CA_LOCAL_PORT=25433;ENV="production";;
459
+ "production_rw") SSH_LOCAL_PORT=2227;COMP_LOCAL_PORT=25554;PG_LOCAL_PORT=24444;CA_LOCAL_PORT=25434;ENV="production";;
460
+ "recette") SSH_LOCAL_PORT=2228;COMP_LOCAL_PORT=25556; PG_LOCAL_PORT=24446;CA_LOCAL_PORT=25436;ENV="recette";;
461
461
  *) echo "Unsupported ENV : $MODE"; return 1 ;;
462
462
  esac
463
463
 
@@ -478,23 +478,28 @@ database_k8s() {
478
478
  HostName 127.0.0.1
479
479
  Port 2225
480
480
  LocalForward 24441 toutatis-testing-db.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:5432
481
+ LocalForward 25551 toutatis-testing-composite-db.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:5432
481
482
  LocalForward 25431 toutatis-testing-mysql-db.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:3306
482
483
  LocalForward 25531 testapirds.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:3306
484
+ LocalForward 25561 toutatis-testing-oracle-db.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:1521
483
485
  Host bastion_staging
484
486
  HostName 127.0.0.1
485
487
  Port 2226
486
488
  LocalForward 24442 toutatis-staging-db.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:5432
489
+ LocalForward 25552 toutatis-staging-composite-db.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:5432
487
490
  LocalForward 25432 toutatis-staging-mysql-db.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:3306
488
491
  Host bastion_recette
489
492
  HostName 127.0.0.1
490
493
  Port 2228
491
494
  LocalForward 24446 toutatis-recette-db.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:5432
495
+ LocalForward 25556 toutatis-recette-composite-db.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:5432
492
496
  LocalForward 25436 toutatis-recette-mysql-db.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:3306
493
497
  LocalForward 25536 testapirds.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:3306
494
498
  Host bastion_production
495
499
  HostName 127.0.0.1
496
500
  Port 2227
497
501
  LocalForward 24443 toutatis-production-db-replica.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:5432
502
+ LocalForward 25553 toutatis-production-composite-db.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:5432
498
503
  LocalForward 25433 toutatis-production-mysql-db-replica.ca0rjdmnxf1x.eu-west-1.rds.amazonaws.com:3306
499
504
  EOF
500
505
  if [ "$MODE" = "production_rw" ] ; then
@@ -508,6 +513,7 @@ EOF
508
513
  -F "$bastion_config" \
509
514
  "bastion_$ENV"
510
515
 
516
+ echo "sample command (composite) : 'psql postgres://postgres@127.0.0.1:$COMP_LOCAL_PORT'"
511
517
  echo "sample command : 'psql postgres://postgres@127.0.0.1:$PG_LOCAL_PORT'"
512
518
  echo "sample command : 'mysql -u colisweb -h 127.0.0.1 -P $CA_LOCAL_PORT -p db_name'"
513
519
 
@@ -863,13 +869,17 @@ function kstatus() {
863
869
  #!/usr/bin/env bash
864
870
 
865
871
  k8_nodes_stats() {
866
- kubectl get nodes -o name |
867
- xargs kubectl describe |
868
- grep "^Name\|workType\|cpu \|memory " |
869
- sed -r 's/[ :=]+/\t/g' |
870
- sed 's/\tworkType\t//g' |
871
- sed -r 's/^Name/---\nName/g' |
872
- grep --color "Name\|web\|workers\|cpu\|memory\|---"
872
+ ENV=${1:-testing}
873
+
874
+ configure_kubectl_for "${ENV}"
875
+
876
+ kubectl get nodes -o name |
877
+ xargs kubectl describe |
878
+ grep "^Name\|workType\|cpu \|memory " |
879
+ sed -r 's/[ :=]+/\t/g' |
880
+ sed 's/\tworkType\t//g' |
881
+ sed -r 's/^Name/---\nName/g' |
882
+ grep --color "Name\|web\|workers\|cpu\|memory\|---"
873
883
  }
874
884
 
875
885
  #!/usr/bin/env bash
@@ -935,14 +945,14 @@ pod_copy_to() {
935
945
 
936
946
  pick_pod() {
937
947
  ENV=$1
938
- POD_FILTER="pod/$2"
948
+ POD_FILTER=$2
939
949
  configure_kubectl_for $ENV
940
950
 
941
951
  if [ -z "$2" ] ; then
942
952
  kubectl -n $ENV get pods | gum filter | cut -f1 -d" "
943
953
  else
944
- if PODS=$(kubectl -n $ENV get pods -o=name | grep "$POD_FILTER"); then
945
- echo $PODS | head -1 | sed -e 's/pod\///'
954
+ if PODS=$(kubectl -n $ENV get pods | grep -m1 "$POD_FILTER" | cut -f1 -d" "); then
955
+ echo $PODS
946
956
  else
947
957
  echo "no pods found on $ENV matching $POD_FILTER" >&2
948
958
  fi
@@ -1422,6 +1432,182 @@ spec:
1422
1432
  }
1423
1433
 
1424
1434
 
1435
+ #!/usr/bin/env bash
1436
+
1437
+ # Usage info
1438
+ show_help_shell() {
1439
+ local help="""Usage: run_job_k8s -s SCRIPT [-e ENV] [-c CONFIG] [-p POD] [-f FOLDER] [ARGS]
1440
+ Create a k8s job executing a script
1441
+
1442
+ -h display this help and exit
1443
+ -s SCRIPT run script SCRIPT on a pod (SCRIPT must be a .sc file)
1444
+ -e ENV opt. set execution environment (default to testing)
1445
+ -c CONFIG opt. secret file needed for the script (must be a .sc file, not a .secret file)
1446
+ -p POD opt. name of the pod to create (default to $USERNAME)
1447
+ -f FOLDER opt. name of the folder containing the scripts to execute (if SCRIPT needs other files)
1448
+ ARGS opt. additional arguments for SCRIPT
1449
+
1450
+ The organisation of the files must be the same locally as on the pod :
1451
+ - /code containing the script to execute (arg -s) and the other needed files (if the arg -f is used, it must reference this directory)
1452
+ - /conf containing the secret file (arg -c if used)
1453
+ E.g. in the script \"/code/script.sc\", to use a secret file \"/conf/secret.sc\", the import should look like \"import \$file.^.conf.secret.sc\"
1454
+ """
1455
+ echo "$help"
1456
+ }
1457
+
1458
+ run_shell_k8s() {
1459
+
1460
+ #default values
1461
+ local namespace="testing"
1462
+ local name="$USERNAME"
1463
+ local secret=""
1464
+ local shell_folder=""
1465
+ local script_script=""
1466
+
1467
+ while getopts ":e:c:p:f:s:h" opt; do
1468
+ case $opt in
1469
+ e)
1470
+ namespace="$OPTARG" >&2
1471
+ ;;
1472
+ p)
1473
+ name="$OPTARG" >&2
1474
+ ;;
1475
+ c)
1476
+ secret="$OPTARG" >&2
1477
+ ;;
1478
+ f)
1479
+ shell_folder="$OPTARG" >&2
1480
+ ;;
1481
+ s)
1482
+ shell_script="$OPTARG" >&2
1483
+ ;;
1484
+ h)
1485
+ show_help_job
1486
+ return 0
1487
+ ;;
1488
+ :)
1489
+ echo "Option -$OPTARG requires an argument. Run run_cron_job_k8s -h for help" >&2
1490
+ return 0
1491
+ ;;
1492
+ \?)
1493
+ echo "Invalid option: -$OPTARG. Run run_cron_job_k8s -h for help" >&2
1494
+ return 0
1495
+ ;;
1496
+ esac
1497
+ done
1498
+
1499
+ if [ -z "$shell_script" ]; then
1500
+ echo 'Missing -s. Run run_job_k8s -h for help' >&2
1501
+ return 0
1502
+ fi
1503
+
1504
+ shift "$((OPTIND-1))"
1505
+
1506
+ local script_args=$(
1507
+ if [ "$#" -gt 0 ] ; then
1508
+ printf '"'
1509
+ join_by '", "' $*
1510
+ printf '"'
1511
+ fi
1512
+ )
1513
+
1514
+
1515
+
1516
+
1517
+ local IMAGE="949316342391.dkr.ecr.eu-west-1.amazonaws.com/docker-infra-builder:v3.1.0"
1518
+ local JOB_NAME="job-shell-$name"
1519
+
1520
+ if [[ ! -r "$shell_script" ]]; then
1521
+ echo "shell script not found $shell_script"
1522
+ return 2
1523
+ else
1524
+ local CONFIG_MAP="config-$JOB_NAME"
1525
+ local CONFIG_MAP_DIR="$(mktemp -d)"
1526
+ local SECRET_MAP="secret-$JOB_NAME"
1527
+
1528
+ configure_kubectl_for $namespace
1529
+
1530
+ if [[ ! -z $shell_folder && -d $shell_folder ]] ; then
1531
+ cp -r "$shell_folder/" "$CONFIG_MAP_DIR"
1532
+ fi
1533
+ cp "$shell_script" "$CONFIG_MAP_DIR/script.sh"
1534
+
1535
+ kubectl -n $namespace get configmap $CONFIG_MAP && kubectl -n $namespace delete configmap $CONFIG_MAP
1536
+ kubectl -n $namespace create configmap $CONFIG_MAP --from-file="$CONFIG_MAP_DIR"
1537
+
1538
+ kubectl -n $namespace get secret $SECRET_MAP && kubectl -n $namespace delete secret $SECRET_MAP
1539
+ kubectl -n $namespace create secret generic $SECRET_MAP --from-file="$secret"
1540
+
1541
+ kubectl -n $namespace get job $JOB_NAME && kubectl -n $namespace delete job $JOB_NAME
1542
+
1543
+ echo "starting $JOB_NAME with $IMAGE"
1544
+ fi
1545
+
1546
+ JOB_DEFINITION='
1547
+ apiVersion: batch/v1
1548
+ kind: Job
1549
+ metadata:
1550
+ name: '$JOB_NAME'
1551
+ namespace: '$namespace'
1552
+ spec:
1553
+ template:
1554
+ spec:
1555
+ containers:
1556
+ - name: '$JOB_NAME'
1557
+ command: ["bash", "/code/script.sh"]
1558
+ image: '$IMAGE'
1559
+ args: ['$script_args']
1560
+ env:
1561
+ - name: POD_NAME
1562
+ valueFrom:
1563
+ fieldRef:
1564
+ apiVersion: v1
1565
+ fieldPath: metadata.name
1566
+ - name: POD_NAMESPACE
1567
+ valueFrom:
1568
+ fieldRef:
1569
+ apiVersion: v1
1570
+ fieldPath: metadata.namespace
1571
+ - name: HOST_IP
1572
+ valueFrom:
1573
+ fieldRef:
1574
+ apiVersion: v1
1575
+ fieldPath: status.hostIP
1576
+ volumeMounts:
1577
+ - name: config
1578
+ mountPath: /code
1579
+ - name: secret
1580
+ mountPath: /conf
1581
+ readOnly: true
1582
+ resources:
1583
+ requests:
1584
+ cpu: 500m
1585
+ memory: 256Mi
1586
+ limits:
1587
+ cpu: 4000m
1588
+ memory: 1Gi
1589
+ nodeSelector:
1590
+ workType: workers
1591
+ restartPolicy: Never
1592
+ volumes:
1593
+ - name: config
1594
+ configMap:
1595
+ name: '$CONFIG_MAP'
1596
+ - name: secret
1597
+ secret:
1598
+ secretName: '$SECRET_MAP'
1599
+ - name: stockage
1600
+
1601
+ '
1602
+
1603
+
1604
+ echo $JOB_DEFINITION > /tmp/job.yaml
1605
+
1606
+ kubectl -n $namespace apply -f /tmp/job.yaml
1607
+
1608
+ }
1609
+
1610
+
1425
1611
  #!/usr/bin/env bash
1426
1612
 
1427
1613
  run_task() {
@@ -1538,51 +1724,53 @@ jwt_token() {
1538
1724
  fi
1539
1725
  }
1540
1726
 
1541
- #!/bin/bash
1727
+ #!/usr/bin/env bash
1728
+
1729
+ alias update_devtool="git -C ~/.oh-my-zsh/custom/dev-tools/ pull"
1542
1730
 
1543
- SCRIPT_PATH=$(dirname $(readlink -f $0))
1731
+ SCRIPT_PATH=$SCRIPT_FULL_PATH/shell/run
1544
1732
  PATH="$PATH:$SCRIPT_PATH/script"
1545
1733
 
1546
1734
  function get_token {
1547
1735
  local ENV=$1
1548
- local LOGINFILE="$HOME/scriptlogin"
1736
+ local LOGIN_FILE="$HOME/scriptlogin"
1549
1737
 
1550
- if [ ! -f "$LOGINFILE" ]; then
1551
- cat > "$LOGINFILE" <<-'EOF'
1738
+ if [ ! -f "$LOGIN_FILE" ]; then
1739
+ cat > "$LOGIN_FILE" <<-'EOF'
1552
1740
  #!/bin/bash
1553
1741
  case $ENV in
1554
1742
  "testing")
1555
- local USERLOGIN=""
1556
- local PASSWORD=""
1743
+ local BO_USERNAME=""
1744
+ local BO_PASSWORD=""
1557
1745
  ;;
1558
1746
  "recette")
1559
- local USERLOGIN=""
1560
- local PASSWORD=""
1747
+ local BO_USERNAME=""
1748
+ local BO_PASSWORD=""
1561
1749
  ;;
1562
1750
  "staging")
1563
- local USERLOGIN=""
1564
- local PASSWORD=""
1751
+ local BO_USERNAME=""
1752
+ local BO_PASSWORD=""
1565
1753
  ;;
1566
1754
  *)
1567
- local USERLOGIN=""
1568
- local PASSWORD=""
1569
- echo "ENV ${ENV} inconue"
1755
+ local BO_USERNAME=""
1756
+ local BO_PASSWORD=""
1757
+ echo "ENV ${ENV} inconu"
1570
1758
  return
1571
1759
  ;;
1572
1760
  esac
1573
1761
  EOF
1574
1762
  fi
1575
1763
 
1576
- source "${LOGINFILE}"
1764
+ source "${LOGIN_FILE}"
1577
1765
 
1578
- if [ -z "$PASSWORD" ] || [ -z "$USERLOGIN" ]
1766
+ if [ -z "$BO_PASSWORD" ] || [ -z "$BO_USERNAME" ]
1579
1767
  then
1580
- echo éditer le ficher "$LOGINFILE"
1768
+ echo éditer le ficher "$LOGIN_FILE"
1581
1769
  return 1
1582
1770
  fi
1583
1771
 
1584
1772
  curl -o /dev/null -D - "https://api.$ENV.colisweb.com/api/v6/authent/external/session" \
1585
- --data-raw '{"username":"'"${USERLOGIN}"'","password":"'"${PASSWORD/\"/\\\"}"'"}' \
1773
+ --data-raw '{"username":"'"${BO_USERNAME}"'","password":"'"${BO_PASSWORD/\"/\\\"}"'"}' \
1586
1774
  --compressed 2> /dev/null | grep set-cook | sed -e 's/.*session=//g;s/;.*//g'
1587
1775
  }
1588
1776
 
@@ -1597,29 +1785,53 @@ function bash_array_to_json {
1597
1785
  }
1598
1786
 
1599
1787
  function get_random_street {
1600
- if [ ! -f "rue.lst" ]; then
1601
- curl --output tmp1.gz https://adresse.data.gouv.fr/data/ban/adresses/latest/csv/adresses-59.csv.gz
1788
+ local CODE_POSTAUX_ARG=${1:-59000}
1789
+ IFS=',' read -r -a CODE_POSTAUX <<< "$CODE_POSTAUX_ARG"
1790
+ for CODE_POSTAL in "${CODE_POSTAUX[@]}"; do
1791
+ if [[ ! "$CODE_POSTAL" =~ ^[0-9]{5}$ ]]; then
1792
+ echo "Chaque CODE_POSTAL doit avoir une taille de 5 chiffre : $CODE_POSTAL"
1793
+ exit 1
1794
+ fi
1795
+ done
1796
+ local CODE_POSTAL=$(echo "${CODE_POSTAUX[@]}" | tr " " "\n" | sort -u -R | head -n 1)
1797
+
1798
+ get_random_street_in_cp $CODE_POSTAL
1799
+ }
1800
+
1801
+ function get_random_street_in_cp {
1802
+ local CODE_POSTAL=$1
1803
+
1804
+ FILENAME="rue-$CODE_POSTAL.lst"
1805
+ if [ ! -f "$FILENAME" ]; then
1806
+ curl --output tmp1.gz https://adresse.data.gouv.fr/data/ban/adresses/latest/csv/adresses-"${CODE_POSTAL:0:2}".csv.gz
1602
1807
  gzip -d tmp1.gz
1603
- cut -d\; -f3,5,6,8 tmp1 | sed '/;Lille/!d' > rue.lst
1604
- rm tmp
1808
+ cut -d\; -f3,5,6,8 tmp1 | sed "/;$CODE_POSTAL;/!d" > "$FILENAME"
1809
+ rm tmp1
1605
1810
  fi
1606
1811
 
1607
- sort -R rue.lst | head -n 1
1812
+ sort -R "$FILENAME" | head -n 1
1608
1813
  }
1609
1814
 
1610
1815
  function rand_slot {
1611
- DATE="$1"
1612
-
1613
- USAGE=$(cat <<-EOF
1614
- {"start":"${DATE}T06:00:00.000Z", "end":"${DATE}T08:00:00.000Z" }
1615
- {"start":"${DATE}T08:00:00.000Z", "end":"${DATE}T10:00:00.000Z" }
1616
- {"start":"${DATE}T10:00:00.000Z", "end":"${DATE}T12:00:00.000Z" }
1617
- {"start":"${DATE}T16:00:00.000Z", "end":"${DATE}T18:00:00.000Z" }
1618
- {"start":"${DATE}T18:00:00.000Z", "end":"${DATE}T20:00:00.000Z" }
1619
- EOF
1620
- )
1621
-
1622
- echo "$USAGE" | sort -u -R | head -n 1
1816
+
1817
+ local SCENARIO=$2
1818
+ if [ -f "$SCENARIO" ]; then
1819
+ source "$SCENARIO"
1820
+ fi
1821
+ local ORDER_DATE="$1"
1822
+
1823
+ DEFAULT=(
1824
+ "06:00+01:00[Europe/Paris]-08:00+01:00[Europe/Paris]"
1825
+ "08:00+01:00[Europe/Paris]-10:00+01:00[Europe/Paris]"
1826
+ "10:00+01:00[Europe/Paris]-12:00+01:00[Europe/Paris]"
1827
+ "16:00+01:00[Europe/Paris]-18:00+01:00[Europe/Paris]"
1828
+ "18:00+01:00[Europe/Paris]-20:00+01:00[Europe/Paris]"
1829
+ )
1830
+ USAGE=${DELIVERY_SLOTS:-${DEFAULT[@]}}
1831
+
1832
+ IFS="-" read -r start_time end_time < <(echo "${USAGE[@]}" | tr " " "\n" | sort -u -R | head -n 1 )
1833
+
1834
+ echo '{"start":"'"${ORDER_DATE}T${start_time}"'", "end":"'"${ORDER_DATE}T${end_time}"'" }'
1623
1835
  }
1624
1836
 
1625
1837
  function call_create_sfh_order {
@@ -1627,36 +1839,58 @@ function call_create_sfh_order {
1627
1839
  local TOKEN=$2
1628
1840
  source "$3"
1629
1841
  local POS=$4
1630
- local BARECODES="$5"
1631
- local PACKAGES=$(echo "$BARECODES" | jq '[{
1842
+ local BARCODES="$5"
1843
+ local CODE_POSTAUX="$6"
1844
+ local PACKAGES=$(echo "$BARCODES" | jq '[{
1632
1845
  "barcode": .[],
1633
- "length": 10.5,
1634
- "height": 9.0,
1635
- "width": 9.0,
1636
- "weight": 10.11,
1637
- "description": "test parel",
1846
+ "length": 20.0,
1847
+ "height": 15.0,
1848
+ "width": 4.0,
1849
+ "weight": 1.5,
1850
+ "description": "test parcel",
1638
1851
  "options": [],
1639
1852
  "productTypology": "Classical",
1640
1853
  "packageType": "Parcel"
1641
1854
  }
1642
1855
  ]')
1643
1856
 
1644
- IFS=";" read -r nu rue code_postal ville < <(get_random_street)
1857
+ DELIVERY_OPTIONS_P='['
1858
+ for option in "${DELIVERY_OPTIONS[@]}"; do
1859
+ if [ "$DELIVERY_OPTIONS_P" != '[' ]; then
1860
+ DELIVERY_OPTIONS_P+=", "
1861
+ fi
1862
+ DELIVERY_OPTIONS_P+="\"$option\""
1863
+ done
1864
+ DELIVERY_OPTIONS_P+=']'
1865
+
1866
+ IFS=";" read -r nu rue code_postal ville < <(get_random_street "$CODE_POSTAUX")
1867
+
1868
+ if [ -n "$PICKUP_STORE_CODE" ]; then
1869
+ PICKUP_LOCATION='{
1870
+ "type": "store",
1871
+ "storeCode": "'"$PICKUP_STORE_CODE"'"
1872
+ }'
1873
+ elif [ -n "$PICKUP_WAREHOUSE_CODE" ]; then
1874
+ PICKUP_LOCATION='{
1875
+ "type": "Warehouse",
1876
+ "warehouseCode": "'"$PICKUP_WAREHOUSE_CODE"'"
1877
+ }'
1878
+ else
1879
+ echo PICKUP_WAREHOUSE_CODE ou PICKUP_STORE_CODE doit être définie dans la "$3"
1880
+ exit 1
1881
+ fi
1645
1882
  JSON='{
1646
1883
  "primaryOrderReference": "'"${PRIMARY_REF}${POS}"'",
1647
1884
  "secondaryOrderReference": null,
1648
1885
  "stages": [
1649
1886
  {
1650
1887
  "type": "Pickup",
1651
- "packageBarcodes": '"$BARECODES"',
1652
- "location": {
1653
- "type": "Warehouse",
1654
- "warehouseCode": "'"$PICKUP_WAREHOUSE_CODE"'"
1655
- }
1888
+ "packageBarcodes": '"$BARCODES"',
1889
+ "location": '"$PICKUP_LOCATION"'
1656
1890
  },
1657
1891
  {
1658
1892
  "type": "Dropoff",
1659
- "packageBarcodes": '"$BARECODES"',
1893
+ "packageBarcodes": '"$BARCODES"',
1660
1894
  "location": {
1661
1895
  "type": "Address",
1662
1896
  "address": {
@@ -1676,13 +1910,18 @@ function call_create_sfh_order {
1676
1910
  ],
1677
1911
  "packages": '"$PACKAGES"',
1678
1912
  "owner": {
1679
- "accountIdentifier": "'$ACCOUNTIDENTIFIER'"
1913
+ "accountIdentifier": "'$ACCOUNT_IDENTIFIER'"
1680
1914
  },
1681
- "deliveryOptions": [],
1682
- "ecommerceValidationDate": "'"${DATE}"'"
1915
+ "deliveryOptions": '"$DELIVERY_OPTIONS_P"',
1916
+ "ecommerceValidationDate": "'"${ORDER_DATE}"'"
1683
1917
  }'
1684
1918
 
1685
- curl -X POST https://api.$ENV.colisweb.com/api/v6/order/external/warehouse/orders -H 'content-type: application/json' --cookie session="$TOKEN" --data-raw "$JSON"
1919
+ RESULT=$(curl -s -X POST https://api.$ENV.colisweb.com/api/v6/order/external/warehouse/orders -H 'content-type: application/json' --cookie session="$TOKEN" --data-raw "$JSON")
1920
+ ORDER_ID=$(jq ".orderId" -r <<< "$RESULT")
1921
+
1922
+ echo "nouvelle commande : https://bo.$ENV.colisweb.com/admin/orders/$ORDER_ID" >&2
1923
+
1924
+ echo "$RESULT"
1686
1925
  }
1687
1926
 
1688
1927
 
@@ -1690,10 +1929,9 @@ function call_scan {
1690
1929
  local ENV=$1
1691
1930
  local TOKEN=$2
1692
1931
  source "$3"
1693
- local BARECODES="$4"
1694
- local SCAN=$(echo "$BARECODES" | jq '[{"barcode" :.[], "context": "shuttle"}]')
1932
+ local BARCODES="$4"
1933
+ local SCAN=$(echo "$BARCODES" | jq '[{"barcode" :.[], "context": "shuttle"}]')
1695
1934
 
1696
- IFS=";" read -r nu rue code_postal ville < <(get_random_street)
1697
1935
  JSON='{"scans":'$SCAN'}'
1698
1936
 
1699
1937
  curl -X POST https://api.$ENV.colisweb.com/api/v6/parcel/external/units/scans/bulk -H 'content-type: application/json' --cookie session="$TOKEN" --data-raw "$JSON"
@@ -1707,18 +1945,21 @@ function call_register_delivery {
1707
1945
  SCENARIO=$3
1708
1946
  source "$SCENARIO"
1709
1947
 
1710
- local ORDERID=$4
1711
- local BARECODES="$5"
1712
-
1713
- curl -X POST https://api.$ENV.colisweb.com/api/v6/order/external/warehouse/orders/"$ORDERID"/deliveries \
1714
- --cookie session="$TOKEN" --data-raw '{
1715
- "slot": '"$(rand_slot ${DELIVERY_DATE})"',
1716
- "storeIdOwner":"'"$STOREIDOWNER"'",
1948
+ local ORDER_ID=$4
1949
+ local BARCODES="$5"
1950
+
1951
+ DATA='{
1952
+ "slot": '"$(rand_slot "${DELIVERY_DATE}" "$SCENARIO")"',
1953
+ "storeIdOwner":"'"$STORE_ID_OWNER"'",
1717
1954
  "pickup":{"type":"hub","code":"'"$HUB"'"},
1718
- "barcodes":'"$BARECODES"',
1955
+ "barcodes":'"$BARCODES"',
1719
1956
  "price":{"origin":"auto","amount":25.9},
1720
- "allowCustomerSlotUpdate":false
1957
+ "allowCustomerSlotUpdate":false,
1958
+ "withForcedSlot": false
1721
1959
  }'
1960
+
1961
+ curl -X POST https://api.$ENV.colisweb.com/api/v6/order/external/warehouse/orders/"$ORDER_ID"/deliveries \
1962
+ --cookie session="$TOKEN" --data-raw "$DATA"
1722
1963
  }
1723
1964
 
1724
1965
 
@@ -1727,25 +1968,45 @@ function _create_scenario_file_if_not_exist () {
1727
1968
  if [ ! -f "$SCENARIO" ]
1728
1969
  then
1729
1970
  cat > "$SCENARIO" <<-'EOF'
1730
- DELIVERY_DATE=$(date -v+7d '+%Y-%m-%d')
1731
- ENV="testing"
1732
- # ENV="staging"
1733
- # ENV="recette"
1734
-
1735
- ACCOUNTIDENTIFIER="102"
1736
- HUB="duck"
1737
- STOREIDOWNER="184"
1738
-
1739
- PICKUP_WAREHOUSE_CODE="422"
1740
-
1741
- BARECODES_COUNT=5
1742
- PREF="aaaa"
1743
-
1744
-
1745
- DATE=$(date '+%Y-%m-%d')
1746
- RAND=$(date +%y%m%d%H%M%S)
1747
- BARECODE_PART=0000$RAND
1748
- PRIMARY_REF=$PREF$RAND
1971
+ DELIVERY_DATE=$(date -v+7d '+%Y-%m-%d') # ici on demande de crée a date d'aujourd'hui + 7 jours
1972
+ # peu remplacer -v+7d par -v+1d pour une livrasion programmer demain
1973
+ # utiliser que par create_many_sfh_order_and_delivery
1974
+ ENV="testing" # sur quelle enviromement lancer le scripts
1975
+ # ENV="staging"
1976
+ # ENV="recette"
1977
+
1978
+ ACCOUNT_IDENTIFIER="102" # pour la creation de order force utilies owner.accountIdentifier
1979
+ # sur l'appel api/v6/order/external/warehouse/orders
1980
+ # (creation de la commade)
1981
+ HUB="duck" # pour sur l'appel api/v6/order/external/warehouse/orders
1982
+ # parametre pickup.code (type est a "hub")
1983
+ STORE_ID_OWNER="184" # parametre pickup.storeIdOwner
1984
+ # sur l'appel api/v6/order/external/warehouse/orders
1985
+ # PICKUP_STORE_CODE="2" # si non commenté alors départ du magasin
1986
+ PICKUP_WAREHOUSE_CODE="422" # pour un départ d'entrepôt
1987
+
1988
+ BARCODES_COUNT=5 # nombres packages
1989
+ PREF="aaaa" # doit faire 4 caractères utilies pour générer les barecode
1990
+ # des packtages
1991
+
1992
+ CODE_POSTAUX=("59000", "75001") # liste code postale sur lequelle une addresse aléatoire seras choisi
1993
+ # (creation de la commade)
1994
+ DELIVERY_SLOTS=( # liste des horraires de créneau de livraison choisi aléatoirement
1995
+ "06:00+01:00[Europe/Paris]-08:00+01:00[Europe/Paris]"
1996
+ "08:00+01:00[Europe/Paris]-10:00+01:00[Europe/Paris]"
1997
+ "10:00+01:00[Europe/Paris]-12:00+01:00[Europe/Paris]"
1998
+ "16:00+01:00[Europe/Paris]-18:00+01:00[Europe/Paris]"
1999
+ "18:00+01:00[Europe/Paris]-20:00+01:00[Europe/Paris]"
2000
+ )
2001
+
2002
+ # DELIVERY_OPTIONS=("skill1" "skill2") # liste des nom skill - a décommanter
2003
+
2004
+ # normalement pas bessoin modifer
2005
+ ORDER_DATE=$(date '+%Y-%m-%d') # date du jour
2006
+ RAND=$(date +%y%m%d%H%M%S) # valueur peudo aleadoire (ici basé par date) doit faire 17 caractères
2007
+ BARCODE_PART=0000$RAND # utiliser pour générer les bare code les barecode sont :
2008
+ # {BARECODE_PART}{00000} a {BARECODE_PART}{BARECODES_COUNT}
2009
+ PRIMARY_REF=$PREF$RAND # primaryOrderReference de la commande
1749
2010
  EOF
1750
2011
  echo "éditer le fichier $SCENARIO"
1751
2012
  return 1
@@ -1780,46 +2041,68 @@ cleanup_grouped_merged_mr() {
1780
2041
  --url "https://gitlab.com/api/v4/projects/$MR"
1781
2042
  done
1782
2043
  }
1783
- # FIXME
1784
- # image index (docker manifest) does not have tags and images are tagged but not marked as related to the index.
1785
- # Should be fixed using more complex procedure to relate index and images.
2044
+
1786
2045
  # you will need jq to use these commands. You can install it using "brew install jq"
1787
- # cleanup_ecr_images colisweb_api 8
1788
- # will delete images older than 8 weeks
1789
- cleanup_ecr_images() {
2046
+ # cleanup_all_ecr_images 12
2047
+ # will delete images in all repositories older than 12 weeks
2048
+ # cleanup_single_ecr_repository colisweb-api 8
2049
+ # will delete images older than 8 weeks in the colisweb-api repository
2050
+ cleanup_all_ecr_images() {
2051
+ WEEKS=$1
1790
2052
 
1791
- REPO=$1
1792
- WEEKS=${2:-16}
2053
+ # OR to work on bash and zsh
2054
+ CLEAN_BEFORE=$(date -v-${WEEKS}w +%F || date --date="-${WEEKS} weeks" +'%Y-%m-%d')
2055
+ REPOSITORIES=$(aws ecr describe-repositories --output json | jq -r '.[] |.[].repositoryName')
1793
2056
 
1794
- WEEKS_AGO=$(date -v-${WEEKS}w +%F)
2057
+ while read -r REPOSITORY; do
2058
+ echo "processing ECR repository $REPOSITORY before $CLEAN_BEFORE"
2059
+ cleanup_single_ecr_repository "$CLEAN_BEFORE" "$REPOSITORY"
2060
+ done <<< "$REPOSITORIES"
2061
+ }
1795
2062
 
1796
- #Get all ecr images
1797
- IMAGES=$(aws ecr describe-images --repository-name $REPO --output json)
2063
+ cleanup_single_ecr_repository() {
2064
+ BEFORE=$1
2065
+ REPOSITORY=$2
1798
2066
 
1799
- #Filter unnecessary values and map `imagePushedAt` to EPOCH
1800
- NON_LATEST_IMAGES=$(echo $IMAGES | jq '[.imageDetails[] | select(.imageTags | any(endswith("latest")) | not)]')
2067
+ echo "gettings tags for repository $REPOSITORY before $BEFORE"
1801
2068
 
1802
- #Filter on EPOCH
1803
- OLD_IMAGES=$(echo $NON_LATEST_IMAGES | jq --arg date $WEEKS_AGO '.[] | select(.imagePushedAt[0:10] < $date).imageDigest')
1804
- while IFS= read -r IMAGE; do
1805
- if [ "$IMAGE" != "" ]; then
1806
- echo "Deleting $IMAGE from $REPO"
1807
- AWS_PAGER="" aws ecr batch-delete-image --repository-name $REPO --image-ids imageDigest=$IMAGE
1808
- fi
1809
- done <<< "$OLD_IMAGES"
1810
- }
2069
+ ALL_TAGS=$(aws ecr describe-images --repository-name "$REPOSITORY" --output json |
2070
+ jq '.imageDetails' |
2071
+ jq '. |= sort_by(.imagePushedAt)' |
2072
+ jq --arg date $BEFORE '.[] | select(.imagePushedAt[0:10] < $date)' |
2073
+ jq 'select((.imageTags != null) or (.imageTags == []))' |
2074
+ jq 'select(.imageTags | any(endswith("latest")) | not)' |
2075
+ jq -r '.imageTags | join(" ")' |
2076
+ sort -u)
1811
2077
 
1812
- # cleanup_all_ecr_images 12
1813
- # will delete images in all repositories older than 12 weeks
1814
- cleanup_all_ecr_images() {
1815
- REPOSITORIES=$(aws ecr describe-repositories --output json | jq -r '.[]|.[].repositoryName')
2078
+ if [ -z "${ALL_TAGS}" ]; then
2079
+ echo "no tag to delete for repository $REPOSITORY"
2080
+ else
2081
+ echo "deleting $(echo $ALL_TAGS | wc -l) tags for $REPOSITORY"
2082
+
2083
+ while read image_tags; do
2084
+ SINGLE_TAG=$(echo $image_tags | grep -o '^\S*')
2085
+
2086
+ DIGESTS_TO_DELETE=$(docker buildx imagetools inspect \
2087
+ 949316342391.dkr.ecr.eu-west-1.amazonaws.com/$REPOSITORY:$SINGLE_TAG --raw |
2088
+ jq -r '[.manifests | .[].digest] | join(" imageDigest=") | "imageDigest=" + .' ||
2089
+ echo "")
2090
+
2091
+ TAGS_TO_DELETE=$(echo "$image_tags" | sed 's/[^ ]* */imageTag=&/g')
2092
+
2093
+ export AWS_PAGER=""
2094
+
2095
+ aws ecr batch-delete-image --repository-name "$REPOSITORY" --image-ids $(echo $TAGS_TO_DELETE) > /dev/null 2>&1
2096
+ test -z $DIGESTS_TO_DELETE ||
2097
+ aws ecr batch-delete-image --repository-name "$REPOSITORY" --image-ids $(echo $DIGESTS_TO_DELETE)> /dev/null 2>&1
2098
+ done <<< $ALL_TAGS
2099
+
2100
+ echo "deleted $(echo $ALL_TAGS | wc -l) tags"
2101
+ fi
1816
2102
 
1817
- while IFS= read -r REPO; do
1818
- echo "processing ECR repository $REPO"
1819
- cleanup_ecr_images $REPO $1
1820
- done <<< "$REPOSITORIES"
1821
2103
  }
1822
2104
 
2105
+
1823
2106
  cleanup_ci_cache() {
1824
2107
  DATE=${1:-$(date -v-1m +%F)}
1825
2108
  CACHE_BUCKET=${2:-"s3://gitlab-colisweb-distributed-cache/project/"}
@@ -1841,6 +2124,23 @@ cleanup_ci_cache() {
1841
2124
  done < <(aws s3 ls $CACHE_BUCKET --recursive)
1842
2125
  }
1843
2126
 
2127
+ cleanup_batch_definitions() {
2128
+ DEFINITION_NAME=$1
2129
+ ARNs=($(
2130
+ aws batch describe-job-definitions \
2131
+ --status ACTIVE \
2132
+ --job-definition-name "$DEFINITION_NAME" |
2133
+ jq '.jobDefinitions | sort_by(-.revision)' |
2134
+ jq 'del( .[0])' |
2135
+ jq -r '.[] | .jobDefinitionArn'
2136
+ )
2137
+ )
2138
+ for A in ${ARNs[@]}; do
2139
+ echo "deregister $A"
2140
+ aws batch deregister-job-definition --job-definition $A
2141
+ done
2142
+ echo "cleaned up all definitions except latest"
2143
+ }
1844
2144
  #!/usr/bin/env bash
1845
2145
 
1846
2146
  ftp_ikea_k8s() {
@@ -2296,15 +2596,15 @@ extract_yaml_config_variable() {
2296
2596
 
2297
2597
  if [ ! -f ${CONFIGS_PATH}/common.yaml ]; then
2298
2598
  echo >&2 "Missing $CONFIGS_PATH/common.yaml configuration file"
2299
- exit 1
2599
+ return 1
2300
2600
  fi
2301
2601
  if [ ! -f ${CONFIGS_PATH}/${ENVIRONMENT}.yaml ]; then
2302
2602
  echo >&2 "Missing $CONFIGS_PATH/$ENVIRONMENT.yaml configuration file"
2303
- exit 1
2603
+ return 1
2304
2604
  fi
2305
2605
  if [ ! -f ${CONFIGS_PATH}/${ENVIRONMENT}-secrets.yaml ]; then
2306
2606
  echo >&2 "Missing $CONFIGS_PATH/$ENVIRONMENT-secrets.yaml configuration file"
2307
- exit 1
2607
+ return 1
2308
2608
  fi
2309
2609
 
2310
2610
  result=$(yq -r ${VARIABLE} "$CONFIGS_PATH/$ENVIRONMENT-secrets.yaml")
@@ -2315,10 +2615,10 @@ extract_yaml_config_variable() {
2315
2615
  if [ $? -ne 0 ] || [ "$result" = "null" ]; then
2316
2616
  if [ $OPTIONAL = true ]; then
2317
2617
  echo ""
2318
- exit 0
2618
+ return 0
2319
2619
  else
2320
2620
  echo >&2 "Missing path $VARIABLE in $CONFIGS_PATH/$ENVIRONMENT-secrets.yaml, $CONFIGS_PATH/$ENVIRONMENT.yaml or $CONFIGS_PATH/common.yaml"
2321
- exit 1
2621
+ return 1
2322
2622
  fi
2323
2623
  fi
2324
2624
  fi
@@ -2591,7 +2891,7 @@ deploy_chart() {
2591
2891
  if [ ! -d ${root_path}/${path_chart} ] || [ ! -f ${root_path}/${path_chart}/Chart.yaml ]; then
2592
2892
  echo "Bad Chart $root_path/$path_chart : does not exists or missing Chart.yaml"
2593
2893
  print_usage
2594
- exit 1
2894
+ return 1
2595
2895
  fi
2596
2896
 
2597
2897
  # Unset Kubectl configuration made via the KUBECONFIG env variable
@@ -2674,7 +2974,7 @@ verify_deployments() {
2674
2974
 
2675
2975
  if [ $? -ne 0 ]; then
2676
2976
  echo "at least one deployment failed or timed out (after $TIMEOUT)"
2677
- exit 1
2977
+ return 1
2678
2978
  fi
2679
2979
  done
2680
2980
 
@@ -2697,7 +2997,7 @@ check_config_file() {
2697
2997
  if [ ! -f ${filename} ]; then
2698
2998
  echo "Missing $filename configuration file"
2699
2999
  print_usage
2700
- exit 1
3000
+ return 1
2701
3001
  fi
2702
3002
  }
2703
3003
 
@@ -2922,7 +3222,7 @@ emit_datadog_deploy_event() {
2922
3222
  echo "event successfully created check in datadog UI : $url"
2923
3223
  else
2924
3224
  echo " failed to create event "
2925
- exit 1
3225
+ return 1
2926
3226
  fi
2927
3227
  }
2928
3228