diff --git a/basis/bin/auto_env.sh b/basis/bin/auto_env.sh index 5473dfd1..769b38e1 100755 --- a/basis/bin/auto_env.sh +++ b/basis/bin/auto_env.sh @@ -1,8 +1,12 @@ #!/bin/bash -# Enable BASH history for Stack Trace. But do not store it. -set -o history -o histexpand -unset HISTFILE +# Enable BASH history for Stack Trace. +# - Do not store in HISTFILE +# - Do not use it when env.sh is called from bash directly. +if [ "$0" != "-bash" ]; then + unset HISTFILE + set -o history -o histexpand +fi if [[ -z "${BIN_DIR}" ]]; then export BIN_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) @@ -133,6 +137,11 @@ else export KUBECONFIG=$TARGET_DIR/kubeconfig_starter fi + if [ "$TF_VAR_db_type" == "nosql" ]; then + # export TF_VAR_nosql_endpoint="nosql.${TF_VAR_region}.oci.oraclecloud.com" + export TF_VAR_nosql_endpoint=`oci nosql table list --compartment-id $TF_VAR_compartment_ocid -d 2>&1 | grep "Endpoint: https" | sed "s#.* https:\/\/##" | sed "s#/.*##"` + fi + # OpenAPI Spec if [ -f $PROJECT_DIR/src/app/openapi_spec.yaml ]; then export TF_VAR_openapi_spec=$(cat $PROJECT_DIR/src/app/openapi_spec.yaml) @@ -263,8 +272,10 @@ if [ -f $STATE_FILE ]; then if [ "$TF_VAR_deploy_type" == "kubernetes" ] || [ -f $PROJECT_DIR/src/terraform/oke.tf ]; then # OKE get_output_from_tfstate "OKE_OCID" "oke_ocid" - export TF_VAR_ingress_ip=`kubectl get service -n ingress-nginx ingress-nginx-controller -o jsonpath="{.status.loadBalancer.ingress[0].ip}"` - export INGRESS_LB_OCID=`oci lb load-balancer list --compartment-id $TF_VAR_compartment_ocid | jq -r '.data[] | select(.["ip-addresses"][0]["ip-address"]=="'$TF_VAR_ingress_ip'") | .id'` + if [ -f $KUBECONFIG ]; then + export TF_VAR_ingress_ip=`kubectl get service -n ingress-nginx ingress-nginx-controller -o jsonpath="{.status.loadBalancer.ingress[0].ip}"` + export INGRESS_LB_OCID=`oci lb load-balancer list --compartment-id $TF_VAR_compartment_ocid | jq -r '.data[] | select(.["ip-addresses"][0]["ip-address"]=="'$TF_VAR_ingress_ip'") | .id'` + fi fi # JMS diff --git a/basis/bin/done.sh b/basis/bin/done.sh index 429470b1..17321d9a 100755 --- a/basis/bin/done.sh +++ b/basis/bin/done.sh @@ -24,11 +24,6 @@ if [ ! -z "$UI_URL" ]; then kubectl get all sleep 5 fi - if [ "$TF_VAR_deploy_type" == "compute" ]; then - # Get the compute logs - scp -r -o StrictHostKeyChecking=no -i $TF_VAR_ssh_private_path opc@$COMPUTE_IP:/home/opc/*.log target/. - scp -r -o StrictHostKeyChecking=no -i $TF_VAR_ssh_private_path opc@$COMPUTE_IP:/home/opc/app/*.log target/. - fi # Retry several time. Needed for ORDS or Go or Tomcat that takes more time to start x=1 @@ -57,6 +52,12 @@ if [ ! -z "$UI_URL" ]; then rm /tmp/cookie.txt fi curl $UI_URL/app/info -b /tmp/cookie.txt -c /tmp/cookie.txt -L --retry 5 --retry-max-time 20 -D /tmp/result_info.log > /tmp/result.info + + if [ "$TF_VAR_deploy_type" == "compute" ]; then + # Get the compute logs + scp -r -o StrictHostKeyChecking=no -i $TF_VAR_ssh_private_path opc@$COMPUTE_IP:/home/opc/*.log target/. + scp -r -o StrictHostKeyChecking=no -i $TF_VAR_ssh_private_path opc@$COMPUTE_IP:/home/opc/app/*.log target/. + fi fi if [ "$TF_VAR_ui_type" != "api" ]; then echo - User Interface: $UI_URL/ diff --git a/basis/bin/shared_bash_function.sh b/basis/bin/shared_bash_function.sh index 48fb8c1c..417b4c25 100755 --- a/basis/bin/shared_bash_function.sh +++ b/basis/bin/shared_bash_function.sh @@ -107,7 +107,7 @@ error_exit() { echo LEN=${#BASH_LINENO[@]} printf "%-40s %-10s %-20s\n" "STACK TRACE" "LINE" "FUNCTION" - for (( INDEX=0; INDEX<$LEN; INDEX++ )) + for (( INDEX=${LEN}-1; INDEX>=0; INDEX--)) do printf " %-37s %-10s %-20s\n" ${BASH_SOURCE[${INDEX}]#$PROJECT_DIR/} ${BASH_LINENO[$(($INDEX-1))]} ${FUNCNAME[${INDEX}]} done @@ -520,15 +520,17 @@ certificate_dir_before_terraform() { # Certificate - Post Deploy certificate_post_deploy() { - if [ "$TF_VAR_deploy_type" == "kubernetes" ]; then + if [ "$TF_VAR_tls" == "new_http_01" ]; then + if [ "$TF_VAR_deploy_type" == "compute" ]; then + certificate_run_certbot_http_01 + elif [ "$TF_VAR_deploy_type" == "kubernetes" ]; then + echo "Skip: TLS - Kubernetes - HTTP_01" + fi + elif [ "$TF_VAR_deploy_type" == "kubernetes" ]; then # Set the TF_VAR_ingress_ip get_ui_url src/terraform/apply.sh --auto-approve -no-color exit_on_error - elif [ "$TF_VAR_tls" == "new_http_01" ]; then - if [ "$TF_VAR_deploy_type" == "compute" ]; then - certificate_run_certbot_http_01 - fi fi } diff --git a/basis/src/app/app.j2.yaml b/basis/src/app/app.j2.yaml index 4c788ceb..8ba04b9d 100644 --- a/basis/src/app/app.j2.yaml +++ b/basis/src/app/app.j2.yaml @@ -28,14 +28,6 @@ spec: secretKeyRef: name: {{ prefix }}-db-secret key: jdbc_url -{%- else %} -{%- if java_framework == "springboot" %} - - name: SPRING_APPLICATION_JSON - valueFrom: - secretKeyRef: - name: {{ prefix }}-db-secret - key: spring_application_json -{%- endif %} {%- endif %} - name: JDBC_URL valueFrom: @@ -48,7 +40,7 @@ spec: secretKeyRef: name: {{ prefix }}-db-secret key: db_url -{%- endif %} +{%- endif %} - name: DB_USER valueFrom: secretKeyRef: @@ -62,7 +54,19 @@ spec: - name: POD_IP valueFrom: fieldRef: - fieldPath: status.podIP + fieldPath: status.podIP +{%- if db_type == "nosql" %} + - name: TF_VAR_compartment_ocid + valueFrom: + secretKeyRef: + name: {{ prefix }}-db-secret + key: TF_VAR_compartment_ocid + - name: TF_VAR_nosql_endpoint + valueFrom: + secretKeyRef: + name: {{ prefix }}-db-secret + key: TF_VAR_nosql_endpoint +{%- endif %} imagePullSecrets: - name: ocirsecret --- diff --git a/option/oke/ingress-app.j2.yaml b/option/oke/ingress-app.j2.yaml new file mode 100644 index 00000000..2a0e038f --- /dev/null +++ b/option/oke/ingress-app.j2.yaml @@ -0,0 +1,47 @@ +# Use 2 Ingress since the parameter replacement work differently in NGINX for / and /app (see annotation) +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ##PREFIX##-app-ingress + annotations: +{%- if language == "apex" %} + nginx.ingress.kubernetes.io/rewrite-target: /ords/r/apex_app/apex_app/$2 + nginx.ingress.kubernetes.io/backend-protocol: "HTTPS" + nginx.ingress.kubernetes.io/upstream-vhost: "##ORDS_HOST##" +{%- elif language == "ords" %} + nginx.ingress.kubernetes.io/rewrite-target: /ords/starter/module/$2 + nginx.ingress.kubernetes.io/backend-protocol: "HTTPS" + nginx.ingress.kubernetes.io/upstream-vhost: "##ORDS_HOST##" +{%- elif language == "java" and java_framework == "tomcat" %} + nginx.ingress.kubernetes.io/rewrite-target: /starter-1.0/$2 +{%- else %} + nginx.ingress.kubernetes.io/rewrite-target: /$2 +{%- endif %} + # nginx.ingress.kubernetes.io/affinity: "cookie" + # nginx.ingress.kubernetes.io/session-cookie-path: "/" +spec: + ingressClassName: nginx +{%- if tls == "new_http_01" %} + tls: + - hosts: + - ##DNS_NAME## + secretName: ##PREFIX##-tls-secret + rules: + - host: ##DNS_NAME## + http: +{%- else %} + rules: + - http: +{%- endif %} + paths: + - path: /##PREFIX##/app(/|$)(.*) + pathType: ImplementationSpecific + backend: + service: + name: ##PREFIX##-app-service + port: +{%- if language in [ "apex", "ords" ] %} + number: 443 +{%- else %} + number: 80 +{%- endif %} diff --git a/option/oke/ingress-app.yaml b/option/oke/ingress-app.yaml deleted file mode 100644 index 02442f97..00000000 --- a/option/oke/ingress-app.yaml +++ /dev/null @@ -1,22 +0,0 @@ -# Use 2 Ingress since the parameter replacement work differently in NGINX for / and /app (see annotation) -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - name: ##PREFIX##-app-ingress - annotations: - nginx.ingress.kubernetes.io/rewrite-target: /$2 - # nginx.ingress.kubernetes.io/affinity: "cookie" - # nginx.ingress.kubernetes.io/session-cookie-path: "/" -spec: - ingressClassName: nginx - rules: - - http: - paths: - - path: /##PREFIX##/app(/|$)(.*) - pathType: ImplementationSpecific - backend: - service: - name: ##PREFIX##-app-service - port: - number: 80 - diff --git a/option/oke/ingress-ui.j2.yaml b/option/oke/ingress-ui.j2.yaml new file mode 100644 index 00000000..6720bc4c --- /dev/null +++ b/option/oke/ingress-ui.j2.yaml @@ -0,0 +1,39 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ##PREFIX##-ui-ingress + annotations: + nginx.ingress.kubernetes.io/rewrite-target: /$2 + # nginx.ingress.kubernetes.io/affinity: "cookie" + # nginx.ingress.kubernetes.io/session-cookie-path: "/" +{%- if tls == "new_http_01" %} + cert-manager.io/issuer: "letsencrypt-prod" + # Logs: + # - kubectl get certificate + # - kubectl get certificaterequest + # - kubectl describe issuer letsencrypt-prod + external-dns.alpha.kubernetes.io/hostname: ##DNS_NAME## + # Logs: kubectl logs external-dns-xxxx +{%- endif %} +spec: + ingressClassName: nginx +{%- if tls == "new_http_01" %} + tls: + - hosts: + - ##DNS_NAME## + secretName: ##PREFIX##-tls-secret + rules: + - host: ##DNS_NAME## + http: +{%- else %} + rules: + - http: +{%- endif %} + paths: + - path: /##PREFIX##(/|$)(.*) + pathType: ImplementationSpecific + backend: + service: + name: ##PREFIX##-ui-service + port: + number: 80 diff --git a/option/oke/ingress-ui.yaml b/option/oke/ingress-ui.yaml deleted file mode 100644 index 27240ea9..00000000 --- a/option/oke/ingress-ui.yaml +++ /dev/null @@ -1,18 +0,0 @@ -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - name: ##PREFIX##-ui-ingress - annotations: - nginx.ingress.kubernetes.io/rewrite-target: /$2 -spec: - ingressClassName: nginx - rules: - - http: - paths: - - path: /##PREFIX##(/|$)(.*) - pathType: ImplementationSpecific - backend: - service: - name: ##PREFIX##-ui-service - port: - number: 80 diff --git a/option/oke/oke_deploy.sh b/option/oke/oke_deploy.sh index d2653497..ced54ff9 100755 --- a/option/oke/oke_deploy.sh +++ b/option/oke/oke_deploy.sh @@ -13,10 +13,35 @@ if [ ! -f $KUBECONFIG ]; then # Deploy Latest ingress-nginx kubectl create clusterrolebinding starter_clst_adm --clusterrole=cluster-admin --user=$TF_VAR_user_ocid - LATEST_INGRESS_CONTROLLER=`curl --silent "https://api.github.com/repos/kubernetes/ingress-nginx/releases/latest" | jq -r .name` - echo LATEST_INGRESS_CONTROLLER=$LATEST_INGRESS_CONTROLLER - kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/$LATEST_INGRESS_CONTROLLER/deploy/static/provider/cloud/deploy.yaml - + # LATEST_INGRESS_CONTROLLER=`curl --silent "https://api.github.com/repos/kubernetes/ingress-nginx/releases/latest" | jq -r .name` + # echo LATEST_INGRESS_CONTROLLER=$LATEST_INGRESS_CONTROLLER + # kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/$LATEST_INGRESS_CONTROLLER/deploy/static/provider/cloud/deploy.yaml + if [ "$TF_VAR_tls" == "new_http_01" ]; then + helm install ingress-nginx ingress-nginx --repo https://kubernetes.github.io/ingress-nginx \ + --namespace ingress-nginx \ + --create-namespace \ + --set controller.enableExternalDNS=true + + # ccm-letsencrypt-prod.yaml + sed "s&##CERTIFICATE_EMAIL##&${TF_VAR_certificate_email}&" src/oke/tls/ccm-letsencrypt-prod.yaml > $TARGET_DIR/ccm-letsencrypt-prod.yaml + kubectl apply -f $TARGET_DIR/ccm-letsencrypt-prod.yaml + sed "s&##CERTIFICATE_EMAIL##&${TF_VAR_certificate_email}&" src/oke/tls/ccm-letsencrypt-staging.yaml > $TARGET_DIR/ccm-letsencrypt-staging.yaml + kubectl apply -f $TARGET_DIR/ccm-letsencrypt-staging.yaml + + # external-dns-config.yaml + sed "s&##COMPARTMENT_OCID##&${TF_VAR_compartment_ocid}&" src/oke/tls//external-dns-config.yaml > $TARGET_DIR/external-dns-config.yaml + kubectl create secret generic external-dns-config --from-file=$TARGET_DIR/external-dns-config.yaml + + # external-dns.yaml + sed "s&##COMPARTMENT_OCID##&${TF_VAR_compartment_ocid}&" src/oke/tls/external-dns.yaml > $TARGET_DIR/external-dns.yaml + sed "s&##REGION##&${TF_VAR_region}&" $TARGET_DIR/external-dns.yaml > $TARGET_DIR/external-dns-config.yaml + kubectl apply -f $TARGET_DIR/external-dns.yaml + else + helm install ingress-nginx ingress-nginx --repo https://kubernetes.github.io/ingress-nginx \ + --namespace ingress-nginx \ + --create-namespace + fi + # Wait for the deployment echo "Waiting for Ingress Controller Pods..." kubectl wait --namespace ingress-nginx --for=condition=ready pod --selector=app.kubernetes.io/component=controller --timeout=600s @@ -41,13 +66,20 @@ if [ ! -f $KUBECONFIG ]; then kubectl create secret docker-registry ocirsecret --docker-server=$TF_VAR_ocir --docker-username="$TF_VAR_namespace/$TF_VAR_username" --docker-password="$TF_VAR_auth_token" --docker-email="$TF_VAR_email" # XXXX - This should be by date kubectl delete secret ${TF_VAR_prefix}-db-secret --ignore-not-found=true - kubectl create secret generic ${TF_VAR_prefix}-db-secret --from-literal=db_user=$TF_VAR_db_user --from-literal=db_password=$TF_VAR_db_password --from-literal=db_url=$DB_URL --from-literal=jdbc_url=$JDBC_URL --from-literal=spring_application_json='{ "db.info": "Java - SpringBoot" }' + kubectl create secret generic ${TF_VAR_prefix}-db-secret --from-literal=db_user=$TF_VAR_db_user --from-literal=db_password=$TF_VAR_db_password --from-literal=db_url=$DB_URL --from-literal=jdbc_url=$JDBC_URL --from-literal=TF_VAR_compartment_ocid=$TF_VAR_compartment_ocid --from-literal=TF_VAR_nosql_endpoint=$TF_VAR_nosql_endpoint fi # Using & as separator sed "s&##DOCKER_PREFIX##&${DOCKER_PREFIX}&" src/app/app.yaml > $TARGET_DIR/app.yaml sed "s&##DOCKER_PREFIX##&${DOCKER_PREFIX}&" src/ui/ui.yaml > $TARGET_DIR/ui.yaml cp src/oke/ingress-app.yaml $TARGET_DIR/ingress-app.yaml +cp src/oke/ingress-ui.yaml $TARGET_DIR/ingress-ui.yaml + +# TLS - Domain Name +if [ "$TF_VAR_tls" == "new_http_01" ]; then + sed -i "s&##DNS_NAME##&$TF_VAR_dns_name&" $TARGET_DIR/ingress-app.yaml + sed -i "s&##DNS_NAME##&$TF_VAR_dns_name&" $TARGET_DIR/ingress-ui.yaml +fi # If present, replace the ORDS URL if [ "$ORDS_URL" != "" ]; then @@ -66,5 +98,5 @@ kubectl wait --for=delete deployment/${TF_VAR_prefix}-dep --timeout=30s kubectl apply -f $TARGET_DIR/app.yaml kubectl apply -f $TARGET_DIR/ui.yaml kubectl apply -f $TARGET_DIR/ingress-app.yaml -kubectl apply -f src/oke/ingress-ui.yaml +kubectl apply -f $TARGET_DIR/ingress-ui.yaml diff --git a/option/oke/oke_destroy.sh b/option/oke/oke_destroy.sh index a419148e..5f798207 100755 --- a/option/oke/oke_destroy.sh +++ b/option/oke/oke_destroy.sh @@ -33,5 +33,6 @@ fi kubectl delete ingress,services --all # Delete the ingress controller -kubectl delete -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.4.0/deploy/static/provider/cloud/deploy.yaml +helm uninstall ingress-nginx --namespace ingress-nginx +# kubectl delete -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.4.0/deploy/static/provider/cloud/deploy.yaml diff --git a/option/oke/tls/ccm-letsencrypt-prod.yaml b/option/oke/tls/ccm-letsencrypt-prod.yaml new file mode 100644 index 00000000..adb10a94 --- /dev/null +++ b/option/oke/tls/ccm-letsencrypt-prod.yaml @@ -0,0 +1,21 @@ +apiVersion: cert-manager.io/v1 +kind: Issuer +metadata: + name: letsencrypt-prod +spec: + acme: + # The ACME server URL + server: https://acme-v02.api.letsencrypt.org/directory + # Email address used for ACME registration + # XXXXX + email: ##CERTIFICATE_EMAIL## + # Name of a secret used to store the ACME account private key + privateKeySecretRef: + name: letsencrypt-prod + # Enable the HTTP-01 challenge provider + solvers: + - http01: + ingress: + class: nginx + # in 1.13 + # ingressClassName: nginx \ No newline at end of file diff --git a/option/oke/tls/ccm-letsencrypt-staging.yaml b/option/oke/tls/ccm-letsencrypt-staging.yaml new file mode 100644 index 00000000..81eb0fec --- /dev/null +++ b/option/oke/tls/ccm-letsencrypt-staging.yaml @@ -0,0 +1,18 @@ +apiVersion: cert-manager.io/v1 +kind: Issuer +metadata: + name: letsencrypt-staging +spec: + acme: + # The ACME server URL + server: https://acme-staging-v02.api.letsencrypt.org/directory + # Email address used for ACME registration + email: ##CERTIFICATE_EMAIL## + # Name of a secret used to store the ACME account private key + privateKeySecretRef: + name: letsencrypt-staging + # Enable the HTTP-01 challenge provider + solvers: + - http01: + ingress: + ingressClassName: nginx \ No newline at end of file diff --git a/option/oke/tls/external-dns-config.yaml b/option/oke/tls/external-dns-config.yaml new file mode 100644 index 00000000..149b721b --- /dev/null +++ b/option/oke/tls/external-dns-config.yaml @@ -0,0 +1,5 @@ +auth: + region: ##REGION## + useWorkloadIdentity: true +compartment: ##COMPARMENT_OCID## + diff --git a/option/oke/tls/external-dns.yaml b/option/oke/tls/external-dns.yaml new file mode 100644 index 00000000..cd0e01ae --- /dev/null +++ b/option/oke/tls/external-dns.yaml @@ -0,0 +1,73 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: external-dns +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: external-dns +rules: +- apiGroups: [""] + resources: ["services","endpoints","pods"] + verbs: ["get","watch","list"] +- apiGroups: ["extensions","networking.k8s.io"] + resources: ["ingresses"] + verbs: ["get","watch","list"] +- apiGroups: [""] + resources: ["nodes"] + verbs: ["list"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: external-dns-viewer +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: external-dns +subjects: +- kind: ServiceAccount + name: external-dns + namespace: default +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: external-dns +spec: + strategy: + type: Recreate + selector: + matchLabels: + app: external-dns + template: + metadata: + labels: + app: external-dns + spec: + serviceAccountName: external-dns + containers: + - name: external-dns + image: registry.k8s.io/external-dns/external-dns:v0.14.0 + args: + - --source=service + - --source=ingress + - --provider=oci + - --policy=upsert-only # prevent ExternalDNS from deleting any records, omit to enable full synchronization + - --txt-owner-id=starter-owner + - --oci-auth-instance-principal + - --oci-compartment-ocid=##COMPARTMENT_OCID## + # Specifies the OCI DNS Zone scope, defaults to GLOBAL. + # May be GLOBAL, PRIVATE, or an empty value to specify both GLOBAL and PRIVATE OCI DNS Zones + # - --oci-zone-scope=GLOBAL + # Specifies the zone cache duration, defaults to 0s. If set to 0s, the zone cache is disabled. + # Use of zone caching is recommended to reduce the amount of requests sent to OCI DNS. + # - --oci-zones-cache-duration=0s +# volumeMounts: +# - name: config +# mountPath: /etc/kubernetes/ +# volumes: +# - name: config +# secret: +# secretName: external-dns-config \ No newline at end of file diff --git a/option/src/app/apex/ingress-app.yaml b/option/src/app/apex/ingress-app.yaml deleted file mode 100644 index 7bc85339..00000000 --- a/option/src/app/apex/ingress-app.yaml +++ /dev/null @@ -1,20 +0,0 @@ -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - name: ##PREFIX##-app-ingress - annotations: - nginx.ingress.kubernetes.io/rewrite-target: /ords/r/apex_app/apex_app/$2 - nginx.ingress.kubernetes.io/backend-protocol: "HTTPS" - nginx.ingress.kubernetes.io/upstream-vhost: "##ORDS_HOST##" -spec: - ingressClassName: nginx - rules: - - http: - paths: - - path: /##PREFIX##/app(/|$)(.*) - pathType: ImplementationSpecific - backend: - service: - name: ##PREFIX##-app-service - port: - number: 443 diff --git a/option/src/app/dotnet/src/Controllers/DeptController.cs.orig b/option/src/app/dotnet/src/Controllers/DeptController.cs.orig deleted file mode 100644 index eb2ab039..00000000 --- a/option/src/app/dotnet/src/Controllers/DeptController.cs.orig +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.Data; -using Oracle.ManagedDataAccess.Client; -using Microsoft.AspNetCore.Mvc; - -namespace starter.Controllers; - -public class DeptController : ControllerBase -{ - private readonly ILogger _logger; - - public static string user = Environment.GetEnvironmentVariable("DB_USER"); - public static string pwd = Environment.GetEnvironmentVariable("DB_PASSWORD"); - public static string db = Environment.GetEnvironmentVariable("DB_URL"); - - public DeptController(ILogger logger) - { - _logger = logger; - } - - [Route("dept")] - public IEnumerable Get() - { - List a = new List(); - string conStringUser = "User Id=" + user + ";Password=" + pwd + ";Data Source=" + db + ";"; - - using (OracleConnection con = new OracleConnection(conStringUser)) - { - using (OracleCommand cmd = con.CreateCommand()) - { - try - { - con.Open(); - Console.WriteLine("Successfully connected to Oracle Database as " + user); - Console.WriteLine(); - - //Retrieve sample data - cmd.CommandText = "SELECT deptno, dname, loc FROM dept"; - OracleDataReader reader = cmd.ExecuteReader(); - while (reader.Read()) - { - Dept d = new Dept { deptno = reader.GetString(0), dname = reader.GetString(1), loc = reader.GetString(2) }; - a.Add(d); - } - reader.Dispose(); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - } - } - return a.ToArray(); - } - - [Route("info")] - public String Info() - { - return ".NET - Oracle"; - } -} diff --git a/option/src/app/dotnet/src/start.sh b/option/src/app/dotnet/src/start.j2.sh similarity index 64% rename from option/src/app/dotnet/src/start.sh rename to option/src/app/dotnet/src/start.j2.sh index b276fd66..acb3c0a1 100755 --- a/option/src/app/dotnet/src/start.sh +++ b/option/src/app/dotnet/src/start.j2.sh @@ -1,8 +1,9 @@ #!/bin/bash SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) cd $SCRIPT_DIR + +{% import "start_sh.j2_macro" as m with context %} +{{ m.env() }} + export PATH=$HOME/.dotnet:$PATH -export DB_USER="##DB_USER##" -export DB_PASSWORD="##DB_PASSWORD##" -export DB_URL="##DB_URL##" dotnet run 2>&1 | tee app.log diff --git a/option/src/app/fn/fn_dotnet/src/Function/Program.cs.orig b/option/src/app/fn/fn_dotnet/src/Function/Program.cs.orig deleted file mode 100644 index 376bfd70..00000000 --- a/option/src/app/fn/fn_dotnet/src/Function/Program.cs.orig +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using Fnproject.Fn.Fdk; -using Oracle.ManagedDataAccess.Client; -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("Function.Tests")] -namespace Function -{ - class Starter - { - public static string user = Environment.GetEnvironmentVariable("DB_USER"); - public static string pwd = Environment.GetEnvironmentVariable("DB_PASSWORD"); - public static string db = Environment.GetEnvironmentVariable("DB_URL"); - - public string dept() - { - string result = "["; - string conStringUser = "User Id=" + user + ";Password=" + pwd + ";Data Source=" + db + ";"; - - using (OracleConnection con = new OracleConnection(conStringUser)) - { - using (OracleCommand cmd = con.CreateCommand()) - { - try - { - con.Open(); - Console.WriteLine("Successfully connected to Oracle Database as " + user); - Console.WriteLine(); - - //Retrieve sample data - cmd.CommandText = "SELECT deptno, dname, loc FROM dept"; - OracleDataReader reader = cmd.ExecuteReader(); - bool bFirst = true; - while (reader.Read()) - { - if (!bFirst) - { - result += ","; - } - else - { - bFirst = false; - } - result += "{ \"deptno\": \"" + reader.GetString(0) + "\", \"dname\": \"" + reader.GetString(1) + "\", \"loc\": \"" + reader.GetString(2) + "\"}"; - } - reader.Dispose(); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - } - } - result += "]"; - return result; - } - - static void Main(string[] args) { Fdk.Handle(args[0]); } - } -} \ No newline at end of file diff --git a/option/src/app/fn/fn_go/func.go.orig b/option/src/app/fn/fn_go/func.go.orig deleted file mode 100644 index 8e3a599f..00000000 --- a/option/src/app/fn/fn_go/func.go.orig +++ /dev/null @@ -1,49 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - "io" - fdk "github.com/fnproject/fdk-go" - "database/sql" - _ "github.com/godror/godror" - "os" -) - -type Dept struct { - Deptno string `json:"deptno"` - Dname string `json:"dname"` - Loc string `json:"loc"` -} - -func main() { - fdk.Handle(fdk.HandlerFunc(myHandler)) -} - -func myHandler(ctx context.Context, in io.Reader, out io.Writer) { - db, err := sql.Open("godror", os.Getenv("DB_USER")+"/"+os.Getenv("DB_PASSWORD")+"@"+os.Getenv("DB_URL")) - if err != nil { - fmt.Println(err) - return - } - defer db.Close() - - rows,err := db.Query("select deptno, dname, loc from dept") - if err != nil { - fmt.Println("Error running query") - fmt.Println(err) - return - } - defer rows.Close() - fmt.Println(rows) - - var d []Dept - for rows.Next() { - var dept=new(Dept) - rows.Scan(&dept.Deptno, &dept.Dname, &dept.Loc) - fmt.Println(dept.Deptno, dept.Dname, dept.Loc) - d = append(d, *dept) - } - json.NewEncoder(out).Encode(&d) -} \ No newline at end of file diff --git a/option/src/app/fn/fn_java/pom.j2.xml b/option/src/app/fn/fn_java/pom.j2.xml index 3b1de722..ef8be01d 100644 --- a/option/src/app/fn/fn_java/pom.j2.xml +++ b/option/src/app/fn/fn_java/pom.j2.xml @@ -1,74 +1,68 @@ - 4.0.0 - - UTF-8 - 1.0.157 - - com.example.fn - hello - 1.0.0 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + UTF-8 + 1.0.157 + + com.example.fn + hello + 1.0.0 - -{%- if db_family !="none" %} - - {{ pomGroupId }} - {{ pomArtifactId }} - {{ pomVersion }} - -{%- endif %} - - com.fnproject.fn - api - ${fdk.version} - - - javax.json - javax.json-api - 1.1.4 - + + {% import "java.j2_macro" as m with context %} + {{ m.dependencies() }} + + com.fasterxml.jackson.core + jackson-databind + 2.16.1 + + + com.fnproject.fn + api + ${fdk.version} + + + com.fnproject.fn + testing-core + ${fdk.version} + test + + + com.fnproject.fn + testing-junit4 + ${fdk.version} + test + + + junit + junit + 4.13.1 + test + + - - com.fnproject.fn - testing-core - ${fdk.version} - test - - - com.fnproject.fn - testing-junit4 - ${fdk.version} - test - - - junit - junit - 4.13.1 - test - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.3 - - 17 - 17 - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.22.1 - - false - - - - + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 17 + 17 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + false + + + + diff --git a/option/src/app/fn/fn_java/src/main/java/com/example/fn/Dept.java b/option/src/app/fn/fn_java/src/main/java/com/example/fn/Dept.java new file mode 100644 index 00000000..51f4ad94 --- /dev/null +++ b/option/src/app/fn/fn_java/src/main/java/com/example/fn/Dept.java @@ -0,0 +1,3 @@ +package com.example.fn; + +public record Dept( int deptno, String dname, String loc ) {}; \ No newline at end of file diff --git a/option/src/app/fn/fn_java/src/main/java/com/example/fn/HelloFunction.j2.java b/option/src/app/fn/fn_java/src/main/java/com/example/fn/HelloFunction.j2.java index bb859f42..986b0032 100644 --- a/option/src/app/fn/fn_java/src/main/java/com/example/fn/HelloFunction.j2.java +++ b/option/src/app/fn/fn_java/src/main/java/com/example/fn/HelloFunction.j2.java @@ -1,85 +1,16 @@ +{% import "java.j2_macro" as m with context %} package com.example.fn; import com.fnproject.fn.api.RuntimeContext; import java.sql.*; -{%- if db_family == "oracle" %} -import oracle.ucp.jdbc.PoolDataSource; -import oracle.ucp.jdbc.PoolDataSourceFactory; -{%- endif %} +import com.fasterxml.jackson.databind.*; +{{ m.import() }} public class HelloFunction { + public HelloFunction() {} - {%- if db_family != "none" %} - private final String dbUser = System.getenv().get("DB_USER"); - private final String dbPassword = System.getenv().get("DB_PASSWORD"); - private final String dbUrl = System.getenv().get("DB_URL"); - {%- endif %} - - {%- if db_family == "oracle" %} - final static String CONN_FACTORY_CLASS_NAME = "oracle.jdbc.pool.OracleDataSource"; - private PoolDataSource poolDataSource; - - public HelloFunction() { - System.out.println("Setting up pool data source"); - poolDataSource = PoolDataSourceFactory.getPoolDataSource(); - try { - poolDataSource.setConnectionFactoryClassName(CONN_FACTORY_CLASS_NAME); - poolDataSource.setURL(dbUrl); - poolDataSource.setUser(dbUser); - poolDataSource.setPassword(dbPassword); - poolDataSource.setConnectionPoolName("UCP_POOL"); - } catch (SQLException e) { - System.out.println("Pool data source error!"); - e.printStackTrace(); - } - System.out.println("Pool data source setup..."); - System.setProperty("oracle.jdbc.fanEnabled", "false"); - } - {%- else %} - public HelloFunction() {} - {%- endif %} - - public String handleRequest(String input) { - {%- if db_family == "none" %} - return """ - [ - { "deptno": "10", "dname": "ACCOUNTING", "loc": "Seoul"}, - { "deptno": "20", "dname": "RESEARCH", "loc": "Cape Town"}, - { "deptno": "30", "dname": "SALES", "loc": "Brussels"}, - { "deptno": "40", "dname": "OPERATIONS", "loc": "San Francisco"} - ] - """; - {%- else %} - // System.out.println("dbUser=" + dbUser + " / dbPassword=" + dbPassword + " / dbUurl=" + dbUrl); - int counter = 0; - StringBuffer sb = new StringBuffer(); - sb.append("["); - try { - System.out.println("Before classForName"); - Class.forName("{{ jdbcDriverClassName }}"); - {%- if db_family == "oracle" %} - Connection conn = poolDataSource.getConnection(); - {%- else %} - Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword); - {%- endif %} - System.out.println("After connection"); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT deptno, dname, loc FROM dept"); - while (rs.next()) { - if (counter++ > 0) { - sb.append(","); - } - sb.append("{\"deptno\": \"" + rs.getInt(1) + "\", \"dname\": \"" + rs.getString(2) + "\", \"loc\": \"" - + rs.getString(3) + "\"}"); - } - stmt.close(); - conn.close(); - } catch (Exception e) { - System.err.println("Exception:" + e.getMessage()); - e.printStackTrace(); + public String handleRequest(String input) { + {{ m.dept_string() }} + return json; } - sb.append("]"); - return sb.toString(); - {%- endif %} - } } diff --git a/option/src/app/go/src/rest.j2.go b/option/src/app/go/src/rest.j2.go index f3b20464..0bb1adec 100644 --- a/option/src/app/go/src/rest.j2.go +++ b/option/src/app/go/src/rest.j2.go @@ -21,7 +21,7 @@ func dept(c *gin.Context) { } func info(c *gin.Context) { - var s string = "GoLang / {{ dbName }}" + var s string = "GoLang - {{ dbName }}" c.Data(http.StatusOK, "text/html", []byte(s)) } diff --git a/option/src/app/go/src/start.sh b/option/src/app/go/src/start.j2.sh similarity index 58% rename from option/src/app/go/src/start.sh rename to option/src/app/go/src/start.j2.sh index 260bc8e9..7e6589c5 100755 --- a/option/src/app/go/src/start.sh +++ b/option/src/app/go/src/start.j2.sh @@ -2,7 +2,6 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) cd $SCRIPT_DIR -export DB_USER="##DB_USER##" -export DB_PASSWORD="##DB_PASSWORD##" -export DB_URL="##DB_URL##" +{% import "start_sh.j2_macro" as m with context %} +{{ m.env() }} ./app > app.log 2>&1 \ No newline at end of file diff --git a/option/src/app/group_common/build_app.sh b/option/src/app/group_common/build_app.sh index 61483c63..571f5c17 100755 --- a/option/src/app/group_common/build_app.sh +++ b/option/src/app/group_common/build_app.sh @@ -49,6 +49,10 @@ if [ -z "$TF_VAR_opensearch_ocid" ]; then get_id_from_tfstate "TF_VAR_opensearch_ocid" "starter_opensearch" fi +if [ -z "$TF_VAR_nosql_ocid" ]; then + get_id_from_tfstate "TF_VAR_nosql_ocid" "starter_nosql_table" +fi + get_output_from_tfstate "TF_VAR_oke_ocid" "oke_ocid" if [ -z "$TF_VAR_apigw_ocid" ]; then @@ -127,6 +131,7 @@ conditional_append database TF_VAR_db_ocid conditional_append mysql TF_VAR_mysql_ocid conditional_append psql TF_VAR_psql_ocid conditional_append opensearch TF_VAR_opensearch_ocid +conditional_append nosql TF_VAR_nosql_ocid conditional_append oke TF_VAR_oke_ocid conditional_append apigw TF_VAR_apigw_ocid conditional_append fnapp TF_VAR_fnapp_ocid diff --git a/option/src/app/java_helidon/pom.j2.xml b/option/src/app/java_helidon/pom.j2.xml index 4028843c..8ce0fc25 100644 --- a/option/src/app/java_helidon/pom.j2.xml +++ b/option/src/app/java_helidon/pom.j2.xml @@ -1,224 +1,211 @@ - 4.0.0 - - io.helidon.applications - helidon-mp - 3.1.0 - - - me.opc-helidon - helidon - 1.0-SNAPSHOT + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + io.helidon.applications + helidon-mp + 3.1.0 + + + me.opc-helidon + helidon + 1.0-SNAPSHOT - - + + - -{%- if db_family != "none" %} - - {{ pomGroupId }} - {{ pomArtifactId }} - runtime -{%- if db_family == "opensearch" %} - {{ pomVersion }} -{%- endif %} - -{%- endif %} - - org.glassfish - javax.json - 1.1.4 - - - io.helidon.microprofile.bundles - helidon-microprofile-core - - - org.eclipse.microprofile.metrics - microprofile-metrics-api - - - io.helidon.microprofile.metrics - helidon-microprofile-metrics - - - io.helidon.microprofile.health - helidon-microprofile-health - - - com.fasterxml.jackson.core - jackson-databind - - - org.glassfish.jersey.media - jersey-media-json-jackson - runtime - - - io.helidon.integrations.cdi - helidon-integrations-cdi-hibernate - runtime - - - io.helidon.integrations.cdi - helidon-integrations-cdi-datasource-hikaricp - runtime - - - jakarta.annotation - jakarta.annotation-api - - - jakarta.enterprise - jakarta.enterprise.cdi-api - - - jakarta.inject - jakarta.inject-api - - - jakarta.ws.rs - jakarta.ws.rs-api - - - jakarta.persistence - jakarta.persistence-api - - - jakarta.transaction - jakarta.transaction-api - - - io.helidon.common - helidon-common - - - jakarta.xml.bind - jakarta.xml.bind-api - - - io.helidon.integrations.cdi - helidon-integrations-cdi-jta-weld - runtime - - - io.helidon.integrations.cdi - helidon-integrations-cdi-jpa - runtime - - - org.hibernate.validator - hibernate-validator - runtime - - - org.glassfish - jakarta.el - runtime - - - org.jboss - jandex - runtime - - - jakarta.activation - jakarta.activation-api - runtime - - - org.junit.jupiter - junit-jupiter-api - test - - - io.helidon.microprofile.tests - helidon-microprofile-tests-junit5 - test - - - org.hamcrest - hamcrest-all - test - - + + {% import "java.j2_macro" as m with context %} + {{ m.dependencies() }} + + io.helidon.microprofile.bundles + helidon-microprofile-core + + + org.eclipse.microprofile.metrics + microprofile-metrics-api + + + io.helidon.microprofile.metrics + helidon-microprofile-metrics + + + io.helidon.microprofile.health + helidon-microprofile-health + + + com.fasterxml.jackson.core + jackson-databind + + + org.glassfish.jersey.media + jersey-media-json-jackson + runtime + + + io.helidon.integrations.cdi + helidon-integrations-cdi-hibernate + runtime + + + io.helidon.integrations.cdi + helidon-integrations-cdi-datasource-hikaricp + runtime + + + jakarta.annotation + jakarta.annotation-api + + + jakarta.enterprise + jakarta.enterprise.cdi-api + + + jakarta.inject + jakarta.inject-api + + + jakarta.ws.rs + jakarta.ws.rs-api + + + jakarta.persistence + jakarta.persistence-api + + + jakarta.transaction + jakarta.transaction-api + + + io.helidon.common + helidon-common + + + jakarta.xml.bind + jakarta.xml.bind-api + + + io.helidon.integrations.cdi + helidon-integrations-cdi-jta-weld + runtime + + + io.helidon.integrations.cdi + helidon-integrations-cdi-jpa + runtime + + + org.hibernate.validator + hibernate-validator + runtime + + + org.glassfish + jakarta.el + runtime + + + org.jboss + jandex + runtime + + + jakarta.activation + jakarta.activation-api + runtime + + + org.junit.jupiter + junit-jupiter-api + test + + + io.helidon.microprofile.tests + helidon-microprofile-tests-junit5 + test + + + org.hamcrest + hamcrest-all + test + + - - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-libs - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - -verbose - - - - - - org.hibernate.orm.tooling - hibernate-enhance-maven-plugin - - - - true - true - true - - - enhance - - - - - - com.evolvedbinary.maven.jvnet - jaxb30-maven-plugin - 0.15.0 - - - Generate persistence.xml Java objects - - generate - - - io.helidon.archetypes.tests.jaxb - true - - - - jakarta.persistence - jakarta.persistence-api - jakarta/persistence/persistence_3_0.xsd - - - - false - - - - - - org.jboss.jandex - jandex-maven-plugin - - - make-index - - - - - + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-libs + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + -verbose + + + + + + org.hibernate.orm.tooling + hibernate-enhance-maven-plugin + + + + true + true + true + + + enhance + + + + + + com.evolvedbinary.maven.jvnet + jaxb30-maven-plugin + 0.15.0 + + + Generate persistence.xml Java objects + + generate + + + io.helidon.archetypes.tests.jaxb + true + + + + jakarta.persistence + jakarta.persistence-api + jakarta/persistence/persistence_3_0.xsd + + + + false + + + + + + org.jboss.jandex + jandex-maven-plugin + + + make-index + + + + + diff --git a/option/src/app/java_helidon/src/main/java/me/opc/mp/database/DeptResource.j2.java b/option/src/app/java_helidon/src/main/java/me/opc/mp/database/DeptResource.j2.java index 2465ea86..785cca83 100644 --- a/option/src/app/java_helidon/src/main/java/me/opc/mp/database/DeptResource.j2.java +++ b/option/src/app/java_helidon/src/main/java/me/opc/mp/database/DeptResource.j2.java @@ -1,22 +1,17 @@ {% import "java.j2_macro" as m with context %} package me.opc.mp.database; -import java.util.*; -import java.util.stream.*; -import java.io.*; -import java.net.*; -import javax.json.*; - import jakarta.persistence.*; import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; +{{ m.import() }} /** * Dept Table */ @Path("/") public class DeptResource { - {%- if db_family != "none" and db_family != "opensearch" %} + {%- if db_family_type == "sql" %} @PersistenceContext(unitName = "pu1") private EntityManager entityManager; {%- endif %} @@ -25,12 +20,10 @@ public class DeptResource { @Path("dept") @Produces(MediaType.APPLICATION_JSON) public List getDept() throws Exception { - {%- if db_family == "none" %} - {{ m.nodb() }} - {%- elif db_family == "opensearch" %} - {{ m.opensearch() }} - {%- else %} + {%- if db_family_type == "sql" %} return entityManager.createNamedQuery("getDept", Dept.class).getResultList(); + {%- else %} + {{ m.dept() }} {%- endif %} } @@ -38,6 +31,6 @@ public List getDept() throws Exception { @Path("info") @Produces(MediaType.TEXT_PLAIN) public String getInfo() { - return "Java - Helidon / {{ dbName }}"; + return "Java - Helidon - {{ dbName }}"; } } diff --git a/option/src/app/java_helidon4/start.sh b/option/src/app/java_helidon/start.j2.sh similarity index 77% rename from option/src/app/java_helidon4/start.sh rename to option/src/app/java_helidon/start.j2.sh index 0c45c839..1f7a4ddd 100755 --- a/option/src/app/java_helidon4/start.sh +++ b/option/src/app/java_helidon/start.j2.sh @@ -2,9 +2,8 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) cd $SCRIPT_DIR -export JDBC_URL="##JDBC_URL##" -export JAVAX_SQL_DATASOURCE_DS1_DATASOURCE_URL=$JDBC_URL -export TF_VAR_java_vm=##TF_VAR_java_vm## +{% import "start_sh.j2_macro" as m with context %} +{{ m.env() }} if [ "$TF_VAR_java_vm" == "graalvm-native" ]; then ./helidon -Doracle.jdbc.fanEnabled=false > app.log 2>&1 diff --git a/option/src/app/java_helidon4/app.yaml.orig b/option/src/app/java_helidon4/app.yaml.orig deleted file mode 100644 index 9eaa51e9..00000000 --- a/option/src/app/java_helidon4/app.yaml.orig +++ /dev/null @@ -1,45 +0,0 @@ -apiVersion: v1 -kind: Pod -metadata: - name: ##PREFIX##-app - labels: - app: ##PREFIX##-app -spec: - containers: - - name: ##PREFIX##-app - image: ##DOCKER_PREFIX##/##PREFIX##-app - ports: - - containerPort: 8080 - name: app-port - env: - - name: JAVAX_SQL_DATASOURCE_DS1_DATASOURCE_URL - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: jdbc_url - - name: DB_USER - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: db_user - - name: DB_PASSWORD - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: db_password - imagePullSecrets: - - name: ocirsecret ---- -apiVersion: v1 -kind: Service -metadata: - name: ##PREFIX##-app-service -spec: - selector: - app: ##PREFIX##-app - ports: - - name: http-service-port - protocol: TCP - port: 80 - targetPort: app-port ---- \ No newline at end of file diff --git a/option/src/app/java_helidon/start.sh b/option/src/app/java_helidon4/start.j2.sh similarity index 77% rename from option/src/app/java_helidon/start.sh rename to option/src/app/java_helidon4/start.j2.sh index 0c45c839..1f7a4ddd 100755 --- a/option/src/app/java_helidon/start.sh +++ b/option/src/app/java_helidon4/start.j2.sh @@ -2,9 +2,8 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) cd $SCRIPT_DIR -export JDBC_URL="##JDBC_URL##" -export JAVAX_SQL_DATASOURCE_DS1_DATASOURCE_URL=$JDBC_URL -export TF_VAR_java_vm=##TF_VAR_java_vm## +{% import "start_sh.j2_macro" as m with context %} +{{ m.env() }} if [ "$TF_VAR_java_vm" == "graalvm-native" ]; then ./helidon -Doracle.jdbc.fanEnabled=false > app.log 2>&1 diff --git a/option/src/app/java_helidon_opensearch/src/main/java/me/opc/mp/database/DeptResource.java.orig b/option/src/app/java_helidon_opensearch/src/main/java/me/opc/mp/database/DeptResource.java.orig deleted file mode 100644 index e9514d88..00000000 --- a/option/src/app/java_helidon_opensearch/src/main/java/me/opc/mp/database/DeptResource.java.orig +++ /dev/null @@ -1,50 +0,0 @@ -package me.opc.mp.database; - -import java.util.*; -import java.util.stream.*; -import java.io.*; -import java.net.*; -import javax.json.Json; -import javax.json.JsonArray; -import javax.json.JsonObject; -import jakarta.ws.rs.*; -import jakarta.ws.rs.core.MediaType; - -/** - * Dept Table - */ -@Path("/") -public class DeptResource { - - @GET - @Path("dept") - @Produces(MediaType.APPLICATION_JSON) - public List getDept() throws Exception { - String s=System.getenv("JDBC_URL"); - String db_url=s.substring(s.indexOf("https://")+8,s.indexOf(":9200")); - URL url = new URL("https://"+db_url+":9200/dept/_search?size=1000&scroll=1m&pretty=true"); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("GET"); - BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); - String body = br.lines().collect(Collectors.joining()); - conn.disconnect(); - - List rows = new ArrayList(); - JsonObject jsonObject = Json.createReader(new StringReader(body)).readObject(); - JsonArray hitsArray = jsonObject.getJsonObject("hits").getJsonArray("hits"); - for (JsonObject hit : hitsArray.getValuesAs(JsonObject.class)) { - JsonObject source = hit.getJsonObject("_source"); - rows.add(new Dept(Integer.valueOf(source.getString("deptno")), source.getString("dname"), source.getString("loc") )); - } - return rows; - } - - @GET - @Path("info") - @Produces(MediaType.TEXT_PLAIN) - public String getInfo() { - return "Java - Helidon / OpenSearch"; - } -} - - diff --git a/option/src/app/java_micronaut/app.yaml.orig b/option/src/app/java_micronaut/app.yaml.orig deleted file mode 100644 index 55353f4e..00000000 --- a/option/src/app/java_micronaut/app.yaml.orig +++ /dev/null @@ -1,54 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: ##PREFIX##-dep - labels: - app: ##PREFIX##-app -spec: - replicas: 1 - selector: - matchLabels: - app: ##PREFIX##-app - template: - metadata: - labels: - app: ##PREFIX##-app - spec: - containers: - - name: ##PREFIX##-app - image: ##DOCKER_PREFIX##/##PREFIX##-app - ports: - - containerPort: 8080 - name: app-port - env: - - name: JDBC_URL - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: jdbc_url - - name: DB_USER - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: db_user - - name: DB_PASSWORD - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: db_password - imagePullSecrets: - - name: ocirsecret ---- -apiVersion: v1 -kind: Service -metadata: - name: ##PREFIX##-app-service -spec: - selector: - app: ##PREFIX##-app - ports: - - name: http-service-port - protocol: TCP - port: 80 - targetPort: app-port ---- \ No newline at end of file diff --git a/option/src/app/java_micronaut/pom.j2.xml b/option/src/app/java_micronaut/pom.j2.xml index c8496792..58809f95 100644 --- a/option/src/app/java_micronaut/pom.j2.xml +++ b/option/src/app/java_micronaut/pom.j2.xml @@ -32,16 +32,8 @@ -{%- if db_family !="none" %} - - {{ pomGroupId }} - {{ pomArtifactId }} - runtime -{%- if db_family == "opensearch" %} - {{ pomVersion }} -{%- endif %} - -{%- endif %} + {% import "java.j2_macro" as m with context %} + {{ m.dependencies() }} io.micronaut micronaut-http-server-netty @@ -161,5 +153,3 @@ - - diff --git a/option/src/app/java_micronaut/src/main/java/com/example/DeptController.j2.java b/option/src/app/java_micronaut/src/main/java/com/example/DeptController.j2.java index cf7b3c71..53a04e56 100644 --- a/option/src/app/java_micronaut/src/main/java/com/example/DeptController.j2.java +++ b/option/src/app/java_micronaut/src/main/java/com/example/DeptController.j2.java @@ -7,38 +7,34 @@ import io.micronaut.scheduling.TaskExecutors; import io.micronaut.scheduling.annotation.ExecuteOn; import java.net.URI; -import java.util.*; import jakarta.inject.Inject; import static io.micronaut.http.HttpHeaders.LOCATION; +{{ m.import() }} @ExecuteOn(TaskExecutors.IO) @Controller("/") class DeptController { - {%- if db_family != "none" %} + {%- if db_family_type == "sql" %} @Inject DeptRepository deptRepository; - {%- endif %} DeptController() { } @Get(uri = "dept") @Produces(MediaType.APPLICATION_JSON) - List dept() { - {%- if db_family == "none" %} - {{ m.nodb() }} - {%- elif db_family == "opensearch" %} - // Use a custom find to be able to specify the exact SQL command. - return deptRepository.findDept(); - {%- else %} + List dept() throws Exception { + {%- if db_family_type == "sql" %} return deptRepository.findAll(); - {%- endif %} + {%- else %} + {{ m.dept() }} + {%- endif %} } @Get(uri = "info") @Produces(MediaType.TEXT_PLAIN) String info() { - return "Java - Micronaut / {{ dbName }}"; + return "Java - Micronaut - {{ dbName }}"; } } diff --git a/option/src/app/java_micronaut/src/main/resources/application.j2.properties b/option/src/app/java_micronaut/src/main/resources/application.j2.properties index efef6953..4f48c63f 100644 --- a/option/src/app/java_micronaut/src/main/resources/application.j2.properties +++ b/option/src/app/java_micronaut/src/main/resources/application.j2.properties @@ -1,5 +1,5 @@ micronaut.application.name=demo -{%- if db_family !="none" %} +{%- if db_family_type == "sql" %} datasources.default.url=${JDBC_URL} datasources.default.username=${DB_USER} datasources.default.password=${DB_PASSWORD} @@ -9,10 +9,6 @@ datasources.default.db-type=oracle datasources.default.dialect=oracle {%- elif db_family == "mysql" %} datasources.default.dialect=mysql -{%- elif db_family == "opensearch" %} -datasources.default.dialect=ansi -datasources.default.read-only=true -datasources.default.auto-commit=true {%- endif %} {%- endif %} netty.default.allocator.max-order=3 diff --git a/option/src/app/java_micronaut/start.sh b/option/src/app/java_micronaut/start.j2.sh similarity index 63% rename from option/src/app/java_micronaut/start.sh rename to option/src/app/java_micronaut/start.j2.sh index 19a32c7a..e7151e74 100755 --- a/option/src/app/java_micronaut/start.sh +++ b/option/src/app/java_micronaut/start.j2.sh @@ -2,10 +2,8 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) cd $SCRIPT_DIR -export DB_USER=##DB_USER## -export DB_PASSWORD=##DB_PASSWORD## -export JDBC_URL="##JDBC_URL##" -export TF_VAR_java_vm=##TF_VAR_java_vm## +{% import "start_sh.j2_macro" as m with context %} +{{ m.env() }} if [ "$TF_VAR_java_vm" == "graalvm-native" ]; then ./demo > app.log 2>&1 diff --git a/option/src/app/java_springboot/app.yaml.orig b/option/src/app/java_springboot/app.yaml.orig deleted file mode 100644 index ba49c34d..00000000 --- a/option/src/app/java_springboot/app.yaml.orig +++ /dev/null @@ -1,50 +0,0 @@ -apiVersion: v1 -kind: Pod -metadata: - name: ##PREFIX##-app - labels: - app: ##PREFIX##-app -spec: - containers: - - name: ##PREFIX##-app - image: ##DOCKER_PREFIX##/##PREFIX##-app - ports: - - containerPort: 8080 - name: app-port - env: - - name: SPRING_APPLICATION_JSON - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: spring_application_json - - name: JDBC_URL - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: jdbc_url - - name: DB_USER - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: db_user - - name: DB_PASSWORD - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: db_password - imagePullSecrets: - - name: ocirsecret ---- -apiVersion: v1 -kind: Service -metadata: - name: ##PREFIX##-app-service -spec: - selector: - app: ##PREFIX##-app - ports: - - name: http-service-port - protocol: TCP - port: 80 - targetPort: app-port ---- \ No newline at end of file diff --git a/option/src/app/java_springboot/pom.j2.xml b/option/src/app/java_springboot/pom.j2.xml index 8d89c05a..9434b28c 100644 --- a/option/src/app/java_springboot/pom.j2.xml +++ b/option/src/app/java_springboot/pom.j2.xml @@ -1,60 +1,52 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.0.0 - - - com.example - demo - 0.0.1-SNAPSHOT - demo - Demo project for Spring Boot - - 17 - - -{%- if db_family != "none" %} - - {{ pomGroupId }} - {{ pomArtifactId }} -{%- if db_family == "opensearch" %} - {{ pomVersion }} -{%- endif %} - -{%- endif %} -{%- if db_family == "oracle" %} - - com.oracle.database.jdbc - ucp - -{%- endif %} - - org.springframework.boot - spring-boot-starter-web - +xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> +4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.0.0 + + + com.example + demo + 0.0.1-SNAPSHOT + demo + Demo project for Spring Boot + + 17 + + + {% import "java.j2_macro" as m with context %} + {{ m.dependencies() }} + {%- if db_family == "oracle" %} + + com.oracle.database.jdbc + ucp + + {%- endif %} + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - org.graalvm.buildtools - native-maven-plugin - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.graalvm.buildtools + native-maven-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/option/src/app/java_springboot_none/src/main/java/com/example/demo/DemoController.java b/option/src/app/java_springboot/src/main/java/com/example/demo/DemoController.j2.java similarity index 61% rename from option/src/app/java_springboot_none/src/main/java/com/example/demo/DemoController.java rename to option/src/app/java_springboot/src/main/java/com/example/demo/DemoController.j2.java index cccb8642..49017200 100644 --- a/option/src/app/java_springboot_none/src/main/java/com/example/demo/DemoController.java +++ b/option/src/app/java_springboot/src/main/java/com/example/demo/DemoController.j2.java @@ -1,17 +1,15 @@ +{% import "java.j2_macro" as m with context %} package com.example.demo; import org.springframework.web.bind.annotation.*; import org.springframework.beans.factory.annotation.Autowired; -import java.sql.*; -import java.util.*; import java.net.Inet4Address; +import java.sql.*; +{{ m.import() }} @RestController public class DemoController { - private String dbUrl; - private String dbUser; - private String dbPassword; public record Dept( int deptno, String dname, String loc ) {}; @@ -20,18 +18,13 @@ public DemoController(DbProperties properties) { } @RequestMapping(value = "/dept", method = RequestMethod.GET, produces = { "application/json" }) - public List query() { - List rows = new ArrayList(); - rows.add(new Dept(10, "ACCOUNTING", "Seoul" )); - rows.add(new Dept(20, "RESEARCH", "Cape Town" )); - rows.add(new Dept(30, "SALES", "Brussels")); - rows.add(new Dept(40, "OPERATIONS", "San Francisco")); - return rows; + public List query() throws Exception { + {{ m.dept() }} } @RequestMapping(value = "/info", method = RequestMethod.GET, produces ={ "text/plain" }) public String info() throws Exception { String IP = (System.getenv("POD_IP")==null)?Inet4Address.getLocalHost().getHostAddress():System.getenv("POD_IP"); - return "Java - SpringBoot / No Database - IP " + IP; + return "Java - SpringBoot - {{ dbName }} - IP=" + IP; } } diff --git a/option/src/app/java_springboot/src/main/java/com/example/demo/DemoController.java b/option/src/app/java_springboot/src/main/java/com/example/demo/DemoController.java deleted file mode 100644 index b36dfffa..00000000 --- a/option/src/app/java_springboot/src/main/java/com/example/demo/DemoController.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.example.demo; - -import org.springframework.web.bind.annotation.*; -import org.springframework.beans.factory.annotation.Autowired; -import java.net.Inet4Address; -import java.sql.*; -import java.util.*; - -@RestController - -public class DemoController { - private String dbUrl; - private String dbUser; - private String dbPassword; - private String dbInfo; - - public record Dept( int deptno, String dname, String loc ) {}; - - @Autowired - public DemoController(DbProperties properties) { - dbInfo = properties.getInfo(); - dbUrl = System.getenv("JDBC_URL"); - dbUser = System.getenv("DB_USER"); - dbPassword = System.getenv("DB_PASSWORD"); - } - - @RequestMapping(value = "/dept", method = RequestMethod.GET, produces = { "application/json" }) - public List query() { - List rows = new ArrayList(); - try { - Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT deptno, dname, loc FROM dept"); - while (rs.next()) { - rows.add(new Dept(rs.getInt(1), rs.getString(2), rs.getString(3) )); - } - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException e) { - System.err.println(e.getMessage()); - } - return rows; - } - - @RequestMapping(value = "/info", method = RequestMethod.GET, produces ={ "text/plain" }) - public String info() throws Exception { - String IP = (System.getenv("POD_IP")==null)?Inet4Address.getLocalHost().getHostAddress():System.getenv("POD_IP"); - return "Java - SpringBoot - IP " + IP; - } -} diff --git a/option/src/app/java_springboot/start.sh b/option/src/app/java_springboot/start.j2.sh similarity index 59% rename from option/src/app/java_springboot/start.sh rename to option/src/app/java_springboot/start.j2.sh index 46d0474f..ad3ac0c3 100755 --- a/option/src/app/java_springboot/start.sh +++ b/option/src/app/java_springboot/start.j2.sh @@ -2,11 +2,8 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) cd $SCRIPT_DIR -export DB_USER=##DB_USER## -export DB_PASSWORD=##DB_PASSWORD## -export JDBC_URL="##JDBC_URL##" -export SPRING_APPLICATION_JSON='{ "db.url": "Java - SpringBoot" }' -export TF_VAR_java_vm=##TF_VAR_java_vm## +{% import "start_sh.j2_macro" as m with context %} +{{ m.env() }} # Start Java with Native or JIT (JDK/GraalVM) if [ "$TF_VAR_java_vm" == "graalvm-native" ]; then diff --git a/option/src/app/java_springboot_oracle/src/main/java/com/example/demo/DemoController.java b/option/src/app/java_springboot_oracle/src/main/java/com/example/demo/DemoController.j2.java similarity index 97% rename from option/src/app/java_springboot_oracle/src/main/java/com/example/demo/DemoController.java rename to option/src/app/java_springboot_oracle/src/main/java/com/example/demo/DemoController.j2.java index 9646af27..60355e0c 100644 --- a/option/src/app/java_springboot_oracle/src/main/java/com/example/demo/DemoController.java +++ b/option/src/app/java_springboot_oracle/src/main/java/com/example/demo/DemoController.j2.java @@ -59,6 +59,6 @@ public List query() { @RequestMapping(value = "/info", method = RequestMethod.GET, produces = { "text/plain" }) public String info() throws Exception { String IP = (System.getenv("POD_IP")==null)?Inet4Address.getLocalHost().getHostAddress():System.getenv("POD_IP"); - return "Java - SpringBoot - IP " + IP; + return "Java - SpringBoot - Oracle - IP=" + IP; } } diff --git a/option/src/app/java_tomcat/app.yaml.orig b/option/src/app/java_tomcat/app.yaml.orig deleted file mode 100644 index 2350d3c3..00000000 --- a/option/src/app/java_tomcat/app.yaml.orig +++ /dev/null @@ -1,45 +0,0 @@ -apiVersion: v1 -kind: Pod -metadata: - name: ##PREFIX##-app - labels: - app: ##PREFIX##-app -spec: - containers: - - name: ##PREFIX##-app - image: ##DOCKER_PREFIX##/##PREFIX##-app - ports: - - containerPort: 8080 - name: app-port - env: - - name: JDBC_URL - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: jdbc_url - - name: DB_USER - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: db_user - - name: DB_PASSWORD - valueFrom: - secretKeyRef: - name: ##PREFIX##-db-secret - key: db_password - imagePullSecrets: - - name: ocirsecret ---- -apiVersion: v1 -kind: Service -metadata: - name: ##PREFIX##-app-service -spec: - selector: - app: ##PREFIX##-app - ports: - - name: http-service-port - protocol: TCP - port: 80 - targetPort: app-port ---- \ No newline at end of file diff --git a/option/src/app/java_tomcat/ingress-app.yaml b/option/src/app/java_tomcat/ingress-app.yaml deleted file mode 100644 index 1b7e2e54..00000000 --- a/option/src/app/java_tomcat/ingress-app.yaml +++ /dev/null @@ -1,20 +0,0 @@ -# Use 2 Ingress since the parameter replacement work differently in NGINX for / and /app (see annotation) -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - name: ##PREFIX##-app-ingress - annotations: - nginx.ingress.kubernetes.io/rewrite-target: /starter-1.0/$2 -spec: - ingressClassName: nginx - rules: - - http: - paths: - - path: /##PREFIX##/app(/|$)(.*) - pathType: ImplementationSpecific - backend: - service: - name: ##PREFIX##-app-service - port: - number: 80 - diff --git a/option/src/app/java_tomcat/pom.j2.xml b/option/src/app/java_tomcat/pom.j2.xml index d3eb3d28..d57db521 100644 --- a/option/src/app/java_tomcat/pom.j2.xml +++ b/option/src/app/java_tomcat/pom.j2.xml @@ -6,19 +6,18 @@ 1.0 war -{%- if db_family !="none" %} - - {{ pomGroupId }} - {{ pomArtifactId }} - runtime - {{ pomVersion }} - -{%- endif %} + {% import "java.j2_macro" as m with context %} + {{ m.dependencies() }} jakarta.servlet jakarta.servlet-api 5.0.0 + + com.fasterxml.jackson.core + jackson-databind + 2.16.1 + diff --git a/option/src/app/java_tomcat/src/main/java/demo/Dept.java b/option/src/app/java_tomcat/src/main/java/demo/Dept.java new file mode 100644 index 00000000..3a693de6 --- /dev/null +++ b/option/src/app/java_tomcat/src/main/java/demo/Dept.java @@ -0,0 +1,3 @@ +package demo; + +public record Dept( int deptno, String dname, String loc ) {}; \ No newline at end of file diff --git a/option/src/app/java_tomcat/src/main/java/demo/DeptServlet.j2.java b/option/src/app/java_tomcat/src/main/java/demo/DeptServlet.j2.java index 3d8421e9..a2b774cc 100644 --- a/option/src/app/java_tomcat/src/main/java/demo/DeptServlet.j2.java +++ b/option/src/app/java_tomcat/src/main/java/demo/DeptServlet.j2.java @@ -1,62 +1,33 @@ +{% import "java.j2_macro" as m with context %} package demo; import java.sql.*; +import com.fasterxml.jackson.databind.*; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; +{{ m.import() }} /** * Servlet implementation class DeptServlet */ public class DeptServlet extends HttpServlet { - /** - * @see HttpServlet#HttpServlet() - */ - public DeptServlet() { - } + /** + * @see HttpServlet#HttpServlet() + */ + public DeptServlet() { + } - /** - * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse - * response) - */ - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - {%- if db_family == "none" %} - response.getWriter().append(""" - [ - { "deptno": "10", "dname": "ACCOUNTING", "loc": "Seoul"}, - { "deptno": "20", "dname": "RESEARCH", "loc": "Cape Town"}, - { "deptno": "30", "dname": "SALES", "loc": "Brussels"}, - { "deptno": "40", "dname": "OPERATIONS", "loc": "San Francisco"} - ] - """); - {%- else %} - int counter = 0; - StringBuffer sb = new StringBuffer(); - sb.append("["); - try { - Class.forName("{{ jdbcDriverClassName }}"); - Connection conn = DriverManager.getConnection(System.getenv("JDBC_URL"), System.getenv("DB_USER"), - System.getenv("DB_PASSWORD")); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT deptno, dname, loc FROM dept"); - while (rs.next()) { - if (counter++ > 0) { - sb.append(","); - } - sb.append("{\"deptno\": \"" + rs.getInt(1) + "\", \"dname\": \"" + rs.getString(2) + "\", \"loc\": \"" - + rs.getString(3) + "\"}"); - } - } catch (Exception e) { - System.err.println("Exception:" + e.getMessage()); - e.printStackTrace(); - } - sb.append("]"); - response.getWriter().append(sb); - {%- endif %} - } + /** + * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse + * response) + */ + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + {{ m.dept_string() }} + response.getWriter().append( json ); + } } diff --git a/option/src/app/java_tomcat/src/main/java/demo/InfoServlet.java b/option/src/app/java_tomcat/src/main/java/demo/InfoServlet.j2.java similarity index 91% rename from option/src/app/java_tomcat/src/main/java/demo/InfoServlet.java rename to option/src/app/java_tomcat/src/main/java/demo/InfoServlet.j2.java index e68f2fa5..90d1759f 100644 --- a/option/src/app/java_tomcat/src/main/java/demo/InfoServlet.java +++ b/option/src/app/java_tomcat/src/main/java/demo/InfoServlet.j2.java @@ -26,6 +26,6 @@ public InfoServlet() { */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub - response.getWriter().append("Java - Tomcat (Servlet/JSP)"); + response.getWriter().append("Java - Tomcat (Servlet/JSP) - {{ dbName }}"); } } diff --git a/option/src/app/java_tomcat/src/start.sh b/option/src/app/java_tomcat/src/start.j2.sh similarity index 58% rename from option/src/app/java_tomcat/src/start.sh rename to option/src/app/java_tomcat/src/start.j2.sh index 21169fc2..7438750f 100755 --- a/option/src/app/java_tomcat/src/start.sh +++ b/option/src/app/java_tomcat/src/start.j2.sh @@ -1,7 +1,6 @@ #!/bin/bash -export DB_USER=##DB_USER## -export DB_PASSWORD=##DB_PASSWORD## -export JDBC_URL="##JDBC_URL##" +{% import "start_sh.j2_macro" as m with context %} +{{ m.env() }} /opt/tomcat/bin/startup.sh # curl http://localhost:8080/starter-1.0/info diff --git a/option/src/app/node/src/rest.j2.js b/option/src/app/node/src/rest.j2.js index f4e95870..b4334c76 100644 --- a/option/src/app/node/src/rest.j2.js +++ b/option/src/app/node/src/rest.j2.js @@ -5,7 +5,7 @@ const port = 8080 {{ m.import() }} app.get('/info', (req, res) => { - res.send('NodeJS - Express / {{ dbName }}') + res.send('NodeJS - Express - {{ dbName }}') }) app.get('/dept', async (req, res) => { diff --git a/option/src/app/node/src/start.sh b/option/src/app/node/src/start.j2.sh similarity index 59% rename from option/src/app/node/src/start.sh rename to option/src/app/node/src/start.j2.sh index edb512c6..20d767de 100755 --- a/option/src/app/node/src/start.sh +++ b/option/src/app/node/src/start.j2.sh @@ -2,8 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) cd $SCRIPT_DIR -export DB_USER="##DB_USER##" -export DB_PASSWORD="##DB_PASSWORD##" -export DB_URL="##DB_URL##" +{% import "start_sh.j2_macro" as m with context %} +{{ m.env() }} node rest.js > app.log 2>&1 diff --git a/option/src/app/ords/ingress-app.yaml b/option/src/app/ords/ingress-app.yaml deleted file mode 100644 index 10ca683c..00000000 --- a/option/src/app/ords/ingress-app.yaml +++ /dev/null @@ -1,22 +0,0 @@ -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - name: ##PREFIX##-app-ingress - annotations: - nginx.ingress.kubernetes.io/rewrite-target: /ords/starter/module/$2 - nginx.ingress.kubernetes.io/backend-protocol: "HTTPS" - nginx.ingress.kubernetes.io/upstream-vhost: "##ORDS_HOST##" -spec: - ingressClassName: nginx - rules: - - http: - paths: - - path: /##PREFIX##/app(/|$)(.*) - pathType: ImplementationSpecific - backend: - service: - name: ##PREFIX##-app-service - port: - number: 443 - - diff --git a/option/src/app/php/src/html/info.j2.php b/option/src/app/php/src/html/info.j2.php index a2b39f08..869445cf 100644 --- a/option/src/app/php/src/html/info.j2.php +++ b/option/src/app/php/src/html/info.j2.php @@ -1 +1 @@ - + diff --git a/option/src/app/python/src/app.j2.py b/option/src/app/python/src/app.j2.py index e6c1751a..4cc0110a 100644 --- a/option/src/app/python/src/app.j2.py +++ b/option/src/app/python/src/app.j2.py @@ -19,7 +19,7 @@ def dept(): @app.route('/info') def info(): - return "Python - Flask / {{ dbName }}" + return "Python - Flask - {{ dbName }}" if __name__ == "__main__": app.run(host="0.0.0.0", port=8080) diff --git a/option/src/app/python/src/start.sh b/option/src/app/python/src/start.j2.sh similarity index 60% rename from option/src/app/python/src/start.sh rename to option/src/app/python/src/start.j2.sh index 8d63513d..9c0bff11 100755 --- a/option/src/app/python/src/start.sh +++ b/option/src/app/python/src/start.j2.sh @@ -2,7 +2,6 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) cd $SCRIPT_DIR -export DB_USER="##DB_USER##" -export DB_PASSWORD="##DB_PASSWORD##" -export DB_URL="##DB_URL##" +{% import "start_sh.j2_macro" as m with context %} +{{ m.env() }} python3.9 app.py 2>&1 | tee app.log diff --git a/option/src/app/ruby/src/start.sh b/option/src/app/ruby/src/start.j2.sh similarity index 50% rename from option/src/app/ruby/src/start.sh rename to option/src/app/ruby/src/start.j2.sh index cee1dc64..fe8755e3 100755 --- a/option/src/app/ruby/src/start.sh +++ b/option/src/app/ruby/src/start.j2.sh @@ -2,7 +2,6 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) cd $SCRIPT_DIR -export DB_USER="##DB_USER##" -export DB_PASSWORD="##DB_PASSWORD##" -export DB_URL="##DB_URL##" -node rest.js +{% import "start_sh.j2_macro" as m with context %} +{{ m.env() }} +xxxx rest.js diff --git a/option/src/db/nosql/db_init.sh b/option/src/db/nosql/db_init.sh new file mode 100755 index 00000000..70546bb0 --- /dev/null +++ b/option/src/db/nosql/db_init.sh @@ -0,0 +1,22 @@ +#!/bin/bash +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +cd $SCRIPT_DIR + +# Install oci-sdk +sudo dnf -y install oraclelinux-developer-release-el8 +sudo dnf -y install python36-oci-cli + +# OCI CLI config file +mkdir $HOME/.oci +touch $HOME/.oci/config +oci setup repair-file-permissions --file $HOME/.oci/config + +export TF_VAR_compartment_ocid=`curl -s -H "Authorization: Bearer Oracle" -L http://169.254.169.254/opc/v2/instance/ | jq -r .compartmentId` +export TF_VAR_region=`curl -s -H "Authorization: Bearer Oracle" -L http://169.254.169.254/opc/v2/instance/ | jq -r .region` +export OCI_CLI_AUTH=instance_principal +# oci nosql table create -c $TF_VAR_compartment_ocid --name dept --ddl-statement "CREATE TABLE dept(deptno LONG, dname STRING, loc STRING, PRIMARY KEY (SHARD(deptno)) ) USING TTL 1 DAYS" --table-limits "{ \"maxReadUnits\": 50, \"maxStorageInGBs\": 1,\"maxWriteUnits\": 1}" +oci nosql query execute -c $TF_VAR_compartment_ocid --statement 'INSERT INTO dept (deptno, dname, loc) values (10, "ACCOUNTING", "BRUSSELS")' --auth instance_principal +oci nosql query execute -c $TF_VAR_compartment_ocid --statement 'INSERT INTO dept (deptno, dname, loc) values (20, "RESEARCH", "NOSQL")' +oci nosql query execute -c $TF_VAR_compartment_ocid --statement 'INSERT INTO dept (deptno, dname, loc) values (30, "SALES", "ROME")' +oci nosql query execute -c $TF_VAR_compartment_ocid --statement 'INSERT INTO dept (deptno, dname, loc) values (40, "OPERATIONS", "MADRID")' +oci nosql query execute -c $TF_VAR_compartment_ocid --statement 'SELECT * FROM dept' diff --git a/option/src/j2_macro/dotnet.j2_macro b/option/src/j2_macro/dotnet.j2_macro index 04d81a84..104cd8a9 100644 --- a/option/src/j2_macro/dotnet.j2_macro +++ b/option/src/j2_macro/dotnet.j2_macro @@ -5,6 +5,8 @@ {%- elif db_family == "psql" %} + {%- elif db_family == "nosql" %} + {%- endif %} {%- endmacro -%} @@ -17,10 +19,11 @@ using MySql.Data.MySqlClient; using Npgsql; {%- elif db_family == "opensearch" %} using System.Text.Json; +{%- elif db_family == "nosql" %} +using Oracle.NoSQL.SDK; {%- endif %} {%- endmacro -%} - {% macro class_def() -%} {%- if db_family == "opensearch" %} public class Source @@ -81,11 +84,38 @@ using System.Text.Json; { Console.WriteLine(ex.Message); } + {%- elif db_family == "nosql" %} + List a = new List(); + var client = new NoSQLClient( new NoSQLConfig{ + Endpoint=Environment.GetEnvironmentVariable("TF_VAR_nosql_endpoint"), + Compartment=Environment.GetEnvironmentVariable("TF_VAR_compartment_ocid"), + {%- if deploy_type in ["compute", "kubernetes", "instance_pool"] %} + AuthorizationProvider=IAMAuthorizationProvider.CreateWithInstancePrincipal() + {%- else %} + AuthorizationProvider=IAMAuthorizationProvider.CreateWithResourcePrincipal() + {%- endif %} + }); + try { + var options = new QueryOptions(); + do { + var task = Task.Run(() => client.QueryAsync("select deptno, dname, loc from dept", options)); + task.Wait(); + var result = task.Result; + foreach(var row in result.Rows) { + a.Add( new Dept{ deptno = row.GetValueAtIndex(0).AsInt32.ToString(), dname = row.GetValueAtIndex(1).AsString, loc = row.GetValueAtIndex(2).AsString } ); + } + options.ContinuationKey = result.ContinuationKey; + } + while(options.ContinuationKey != null); + } + catch(Exception ex) { + Console.WriteLine(ex.Message); + } {%- else %} + List a = new List(); string user = Environment.GetEnvironmentVariable("DB_USER"); string pwd = Environment.GetEnvironmentVariable("DB_PASSWORD"); string db = Environment.GetEnvironmentVariable("DB_URL"); - List a = new List(); try { {%- if db_family == "oracle" %} diff --git a/option/src/j2_macro/go.j2_macro b/option/src/j2_macro/go.j2_macro index 12ce0ac2..f43d976e 100644 --- a/option/src/j2_macro/go.j2_macro +++ b/option/src/j2_macro/go.j2_macro @@ -6,6 +6,8 @@ github.com/go-sql-driver/mysql v1.7.0 {%- elif db_family == "psql" %} github.com/lib/pq v1.10.9 + {%- elif db_family == "nosql" %} + github.com/oracle/nosql-go-sdk v1.4.2 {%- endif %} {%- endmacro -%} @@ -13,7 +15,7 @@ {%- if db_family != "none" %} "fmt" "os" - {%- if db_family != "opensearch" %} + {%- if db_family != "opensearch" and db_family != "nosql" %} "database/sql" {%- endif %} {%- endif %} @@ -30,6 +32,10 @@ {%- else %} "encoding/json" {%- endif %} + {%- elif db_family == "nosql" %} + "github.com/oracle/nosql-go-sdk/nosqldb" + "github.com/oracle/nosql-go-sdk/nosqldb/auth/iam" + "github.com/oracle/nosql-go-sdk/nosqldb/types" {%- endif %} {%- endmacro -%} @@ -81,6 +87,39 @@ type Result struct { for _, hit := range body.Hits.Hits { d = append(d, Dept{hit.Source.Deptno, hit.Source.Dname,hit.Source.Loc}) } + {%- elif db_family == "nosql" -%} + {%- if deploy_type in ["compute", "kubernetes", "instance_pool"] %} + sp, err := iam.NewSignatureProviderWithInstancePrincipal(os.Getenv("TF_VAR_compartment_ocid")) + {%- else %} + sp, err := iam.NewSignatureProviderWithResourcePrincipal(os.Getenv("TF_VAR_compartment_ocid")) + {%- endif %} + if err != nil { + fmt.Println("NoSQL Login error") + return + } + cfg := nosqldb.Config{ + AuthorizationProvider: sp, + Endpoint: os.Getenv("TF_VAR_nosql_endpoint"), + } + client, err := nosqldb.NewClient(cfg) + queryReq := &nosqldb.QueryRequest{ Statement: "select deptno, dname, loc from dept" } + var d []*types.MapValue + for { + queryRes, err := client.Query(queryReq) + if err != nil { + fmt.Printf("Query failed: %v\n", err) + return + } + res, err := queryRes.GetResults() + if err != nil { + fmt.Printf("GetResults() failed: %v\n", err) + return + } + d = append(d, res...) + if queryReq.IsDone() { + break + } + } {%- else %} {%- if db_family == "oracle" -%} db, err := sql.Open("godror", os.Getenv("DB_USER")+"/"+os.Getenv("DB_PASSWORD")+"@"+os.Getenv("DB_URL")) diff --git a/option/src/j2_macro/java.j2_macro b/option/src/j2_macro/java.j2_macro index 950d1b20..a2761a39 100644 --- a/option/src/j2_macro/java.j2_macro +++ b/option/src/j2_macro/java.j2_macro @@ -1,13 +1,103 @@ -{% macro nodb() -%} + +{% macro import() -%} +import java.io.*; +import java.util.*; +{%- if db_family == "nosql" %} +import oracle.nosql.driver.*; +import oracle.nosql.driver.iam.*; +import oracle.nosql.driver.ops.*; +import oracle.nosql.driver.values.*; +{%- elif db_family == "opensearch" %} +import java.net.*; +import java.util.stream.*; +import javax.json.*; +{%- endif %} +{%- endmacro -%} + +{% macro dependencies() -%} +{%- if db_family == "nosql" %} + + com.oracle.nosql.sdk + nosqldriver + 5.4.14 + +{%- elif db_family_type == "sql" %} + + {{ pomGroupId }} + {{ pomArtifactId }} + {%- if java_framework != "springboot" or deploy_type == "function" %} + {{ pomVersion }} + {%- endif %} + +{%- endif %} + + org.glassfish + javax.json + 1.1.4 + +{%- endmacro -%} + +{% macro dept() -%} + {%- if db_family != "sql" %} + {{ dept_no_return() }} + return rows; + {%- endif %} +{%- endmacro -%} + +{% macro dept_string() -%} + String json = "-"; + try { + {%- if db_family_type == "sql" %} + Class.forName("{{ jdbcDriverClassName }}"); + {%- endif %} + {{ dept_no_return() }} + // Jackson + ObjectMapper objectMapper = new ObjectMapper(); + json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rows); + } catch (Exception e) { + System.err.println("Exception:" + e.getMessage()); + e.printStackTrace(); + } +{%- endmacro -%} + + +{% macro dept_no_return() -%} List rows = new ArrayList(); + {%- if db_family_type == "sql" %} + {{ sql() }} + {%- elif db_family == "none" %} + {{ nodb() }} + {%- elif db_family == "opensearch" %} + {{ opensearch() }} + {%- elif db_family == "nosql" %} + {{ nosql() }} + {%- endif %} +{%- endmacro -%} + +{% macro sql() -%} + try { + Connection conn = DriverManager.getConnection( System.getenv("JDBC_URL") , System.getenv("DB_USER"), System.getenv("DB_PASSWORD")); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT deptno, dname, loc FROM dept"); + while (rs.next()) { + rows.add(new Dept(rs.getInt(1), rs.getString(2), rs.getString(3) )); + } + rs.close(); + stmt.close(); + conn.close(); + } catch (SQLException e) { + System.err.println(e.getMessage()); + e.printStackTrace(); + } +{%- endmacro -%} + +{% macro nodb() -%} rows.add(new Dept(10, "ACCOUNTING", "Seoul" )); rows.add(new Dept(20, "RESEARCH", "Cape Town" )); rows.add(new Dept(30, "SALES", "Brussels")); rows.add(new Dept(40, "OPERATIONS", "San Francisco")); - return rows; {%- endmacro -%} - {% macro opensearch() -%} String s=System.getenv("JDBC_URL"); String db_url=s.substring(s.indexOf("https://")+8,s.indexOf(":9200")); @@ -18,12 +108,32 @@ String body = br.lines().collect(Collectors.joining()); conn.disconnect(); - List rows = new ArrayList(); JsonObject jsonObject = Json.createReader(new StringReader(body)).readObject(); JsonArray hitsArray = jsonObject.getJsonObject("hits").getJsonArray("hits"); for (JsonObject hit : hitsArray.getValuesAs(JsonObject.class)) { JsonObject source = hit.getJsonObject("_source"); rows.add(new Dept(Integer.valueOf(source.getString("deptno")), source.getString("dname"), source.getString("loc") )); } - return rows; +{%- endmacro -%} + +{% macro nosql() -%} + System.out.println( "TF_VAR_compartment_ocid=" + System.getenv("TF_VAR_compartment_ocid") ); + System.out.println( "TF_VAR_nosql_endpoint=" + System.getenv("TF_VAR_nosql_endpoint") ); + {%- if deploy_type in ["compute", "kubernetes", "instance_pool"] %} + SignatureProvider authProvider = SignatureProvider.createWithInstancePrincipal(); + {%- else %} + SignatureProvider authProvider = SignatureProvider.createWithResourcePrincipal(); + {%- endif %} + NoSQLHandleConfig config = new NoSQLHandleConfig( System.getenv("TF_VAR_nosql_endpoint"), authProvider); + config.setDefaultCompartment( System.getenv("TF_VAR_compartment_ocid") ); + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + + QueryRequest queryRequest = new QueryRequest().setStatement("SELECT deptno, dname, loc FROM dept"); + QueryResult queryResult = handle.query(queryRequest); + do { + List results = queryResult.getResults(); + for (MapValue row : results) { + rows.add( new Dept( row.get("deptno").asInteger().getValue(), row.get("dname").asString().getValue(), row.get("loc").asString().getValue() ) ); + } + } while (!queryRequest.isDone()); {%- endmacro -%} diff --git a/option/src/j2_macro/node.j2_macro b/option/src/j2_macro/node.j2_macro index 8b142807..bec61e0e 100644 --- a/option/src/j2_macro/node.j2_macro +++ b/option/src/j2_macro/node.j2_macro @@ -7,6 +7,8 @@ ,"pg": "^8.11.3" {%- elif db_family == "opensearch" %} ,"node-fetch": "^2.6.1" + {%- elif db_family == "nosql" -%} + ,"oracle-nosqldb": "^5.2" {%- endif %} {%- endmacro -%} @@ -19,6 +21,25 @@ const mysql = require('mysql2/promise'); const Pool = require('pg').Pool {%- elif db_family == "opensearch" -%} const fetch = require("node-fetch"); +{%- elif db_family == "nosql" -%} +const NoSQLClient = require('oracle-nosqldb').NoSQLClient; +let client = createClient(); + +function createClient() { + return new NoSQLClient({ + endpoint: process.env.TF_VAR_nosql_endpoint, + compartment:process.env.TF_VAR_compartment_ocid, + auth: { + iam: { + {%- if deploy_type in ["compute", "kubernetes", "instance_pool"] %} + useInstancePrincipal: true + {%- else %} + useResourcePrincipal: true + {%- endif %} + } + } + }); +} {%- endif %} {%- endmacro -%} @@ -28,6 +49,7 @@ const fetch = require("node-fetch"); {%- elif db_family == "mysql" -%}{{ mysql() }} {%- elif db_family == "psql" -%}{{ psql() }} {%- elif db_family == "opensearch" -%}{{ opensearch() }} +{%- elif db_family == "nosql" -%}{{ nosql() }} {%- endif -%} {%- endmacro -%} @@ -90,10 +112,24 @@ const fetch = require("node-fetch"); const response = await fetch(url); const j = await response.json(); console.log(j); - rows = []; + let rows = []; for (i in j.hits.hits) { hit = j.hits.hits[i] rows.push({"deptno":hit._source.deptno,"dname":hit._source.dname,"loc":hit._source.loc }) } {%- endmacro -%} +{% macro nosql() -%} + let rows = []; + const opt = {}; + do { + const result = await client.query('SELECT deptno, dname, loc FROM dept', opt); + for(let row of result.rows) { + rows.push( row ); + } + opt.continuationKey = result.continuationKey; + } while(opt.continuationKey); + +{%- endmacro -%} + + diff --git a/option/src/j2_macro/python.j2_macro b/option/src/j2_macro/python.j2_macro index 8d41e010..c40dcefd 100644 --- a/option/src/j2_macro/python.j2_macro +++ b/option/src/j2_macro/python.j2_macro @@ -8,6 +8,9 @@ mysql-connector-python==8.0.28 psycopg2-binary {%- elif db_family == "opensearch" %} requests +{%- elif db_family == "nosql" %} +borneo +oci {%- endif %} {%- endmacro -%} @@ -20,6 +23,10 @@ import mysql.connector import psycopg2 {%- elif db_family == "opensearch" %} import requests +{%- elif db_family == "nosql" %} +from borneo import NoSQLHandle, NoSQLHandleConfig, Regions +from borneo.iam import SignatureProvider +from borneo import QueryRequest {%- endif %} {%- endmacro -%} @@ -67,9 +74,27 @@ import requests print(j, flush=True) for hit in j["hits"]["hits"]: a.append( {"deptno": hit["_source"]["deptno"], "dname": hit["_source"]["dname"], "loc": hit["_source"]["loc"]} ) + {%- elif db_family == "nosql" %} + {%- if deploy_type in ["compute", "kubernetes", "instance_pool"] %} + provider = SignatureProvider.create_with_instance_principal() + {%- else %} + provider = SignatureProvider.create_with_resource_principal() + {%- endif %} + config = NoSQLHandleConfig( os.getenv('TF_VAR_nosql_endpoint'), provider ) + config.set_default_compartment( os.getenv('TF_VAR_compartment_ocid') ) + {%- if deploy_type == "function" %} + config.set_logger( None ) + {%- endif %} + handle = NoSQLHandle(config) + request = QueryRequest().set_statement('select deptno, dname, loc from dept') + while True: + result = handle.query_iterable(request) + for row in result: + a.append(row) + if request.is_done(): break {%- endif %} - {%- if db_family != "opensearch" %} + {%- if db_family != "opensearch" and db_family != "nosql" %} print("Successfully connected to database", flush=True) cursor = conn.cursor() cursor.execute("SELECT deptno, dname, loc FROM dept") @@ -80,7 +105,7 @@ import requests except Exception as e: print(traceback.format_exc(), flush=True) print(e, flush=True) - {%- if db_family != "opensearch" %} + {%- if db_family != "opensearch" and db_family != "nosql" %} finally: cursor.close() conn.close() diff --git a/option/src/j2_macro/start_sh.j2_macro b/option/src/j2_macro/start_sh.j2_macro new file mode 100644 index 00000000..b3f7157a --- /dev/null +++ b/option/src/j2_macro/start_sh.j2_macro @@ -0,0 +1,24 @@ +{%- macro env() %} +{%- if language == "java" -%} +export TF_VAR_java_vm=##TF_VAR_java_vm## +{%- endif %} +{% if db_family == "nosql" -%} +curl -s -H "Authorization: Bearer Oracle" -L http://169.254.169.254/opc/v2/instance/ > /tmp/instance.json +export TF_VAR_compartment_ocid=`cat /tmp/instance.json | jq -r .compartmentId` +export region=`cat /tmp/instance.json | jq -r .region` +export regionDomain=`cat /tmp/instance.json | jq -r .regionInfo.realmDomainComponent` +export TF_VAR_nosql_endpoint="nosql.${region}.oci.${regionDomain}" +{%- elif language == "java" -%} +export DB_USER="##DB_USER##" +export DB_PASSWORD="##DB_PASSWORD##" +export JDBC_URL="##JDBC_URL##" +{%- if java_framework == "helidon" or java_framework == "helidon4" %} +export JAVAX_SQL_DATASOURCE_DS1_DATASOURCE_URL=$JDBC_URL +{%- endif %} +{%- else -%} +export DB_USER="##DB_USER##" +export DB_PASSWORD="##DB_PASSWORD##" +export DB_URL="##DB_URL##" +{%- endif %} +{%- endmacro -%} + diff --git a/option/terraform/container_instance.j2.tf b/option/terraform/container_instance.j2.tf index c3bed733..cf3389d4 100644 --- a/option/terraform/container_instance.j2.tf +++ b/option/terraform/container_instance.j2.tf @@ -6,6 +6,10 @@ variable docker_image_app { default="" } +{%- if db_type == "nosql" %} +variable nosql_endpoint {} +{%- endif %} + resource oci_container_instances_container_instance starter_container_instance { count = var.docker_image_ui == "" ? 0 : 1 availability_domain = data.oci_identity_availability_domain.ad.name @@ -23,7 +27,10 @@ resource oci_container_instances_container_instance starter_container_instance { "DB_PASSWORD" = var.db_password, "JAVAX_SQL_DATASOURCE_DS1_DATASOURCE_URL" = local.jdbc_url {%- endif %} - "SPRING_APPLICATION_JSON" = "{ \"db.info\": \"Java - SpringBoot\" }", + {%- if db_type == "nosql" %} + "TF_VAR_compartment_ocid" = var.compartment_ocid, + "TF_VAR_nosql_endpoint" = var.nosql_endpoint, + {%- endif %} } } containers { diff --git a/option/terraform/function.j2.tf b/option/terraform/function.j2.tf index 47c10128..c2484d8d 100644 --- a/option/terraform/function.j2.tf +++ b/option/terraform/function.j2.tf @@ -52,6 +52,9 @@ locals { {%- if group_name is not defined %} variable "fn_image" { default = "" } variable "fn_db_url" { default = "" } +{%- if db_type == "nosql" %} +variable nosql_endpoint {} +{%- endif %} resource "oci_functions_function" "starter_fn_function" { #Required @@ -61,9 +64,17 @@ resource "oci_functions_function" "starter_fn_function" { image = var.fn_image memory_in_mbs = "2048" config = { + {%- if language == "java" %} + JDBC_URL = var.fn_db_url, + {%- else %} DB_URL = var.fn_db_url, + {%- endif %} DB_USER = var.db_user, DB_PASSWORD = var.db_password, + {%- if db_type == "nosql" %} + TF_VAR_compartment_ocid = var.compartment_ocid, + TF_VAR_nosql_endpoint = var.nosql_endpoint, + {%- endif %} } #Optional timeout_in_seconds = "300" diff --git a/option/terraform/nosql.j2.tf b/option/terraform/nosql.j2.tf new file mode 100644 index 00000000..86f8009e --- /dev/null +++ b/option/terraform/nosql.j2.tf @@ -0,0 +1,48 @@ +// NoSQL +// See https://docs.oracle.com/en/cloud/paas/nosql-cloud/dtddt/#GUID-BF198255-AA9E-4BA2-9849-D5C2E5B57ED9 +// https://docs.oracle.com/en/cloud/paas/nosql-cloud/uxxxx/#articletitle + +{%- if nosql_ocid is defined %} +// Existing + +{%- else %} +resource "oci_nosql_table" "starter_nosql_table" { + #Required + compartment_id = var.compartment_ocid + ddl_statement = "CREATE TABLE IF NOT EXISTS dept(deptno INTEGER, dname STRING, loc STRING, PRIMARY KEY(SHARD(deptno)))" + name = "dept" + + table_limits { + #Required + max_read_units = "10" + max_write_units = "1" + max_storage_in_gbs = "1" + } +} + +resource "oci_identity_dynamic_group" "starter_nosql_dyngroup" { + compartment_id = var.tenancy_ocid + name = "${var.prefix}-nosql-dyngroup" + description = "${var.prefix}-nosql-dyngroup" + matching_rule = "ANY {instance.compartment.id = '${var.compartment_ocid}', ALL {resource.type = 'fnfunc', resource.compartment.id ='${var.compartment_ocid}'}, ALL {resource.type = 'computecontainerinstance', resource.compartment.id ='${var.compartment_ocid}' }}" + freeform_tags = local.freeform_tags +} + +resource "oci_identity_policy" "starter_nosql_policy" { + name = "${var.prefix}-nosql-policy" + description = "${var.prefix}-nosql-policy" + compartment_id = var.compartment_ocid + statements = [ + "Allow dynamic-group ${var.prefix}-nosql-dyngroup to manage nosql-family in compartment id ${var.compartment_ocid}", + ] + freeform_tags = local.freeform_tags +} +{%- endif %} + +{%- if group_name is not defined %} +locals { + db_host = "none" + db_url = "none" + jdbc_url = "none" +} +{%- endif %} diff --git a/option/terraform/oke.j2.tf b/option/terraform/oke.j2.tf index d60c7a6a..a7084eea 100644 --- a/option/terraform/oke.j2.tf +++ b/option/terraform/oke.j2.tf @@ -14,7 +14,7 @@ variable "oke_shape" { default = "VM.Standard.E3.Flex" } -variable "node_pool_node_config_details_size" { +variable "node_pool_size" { default = 1 } @@ -398,11 +398,11 @@ resource "oci_containerengine_node_pool" "starter_node_pool" { compartment_id = local.lz_appdev_cmp_ocid kubernetes_version = data.oci_containerengine_node_pool_option.starter_node_pool_option.kubernetes_versions[length(data.oci_containerengine_node_pool_option.starter_node_pool_option.kubernetes_versions)-1] name = "${var.prefix}-pool" - node_shape = var.oke_shape - node_shape_config { - memory_in_gbs = "32" - ocpus = "1" - } + node_shape = var.oke_shape + node_shape_config { + memory_in_gbs = "32" + ocpus = "1" + } node_source_details { #Required @@ -419,7 +419,7 @@ resource "oci_containerengine_node_pool" "starter_node_pool" { #optional fault_domains = ["FAULT-DOMAIN-1", "FAULT-DOMAIN-3"] } - size = var.node_pool_node_config_details_size + size = var.node_pool_size # node_pool_pod_network_option_details { # cni_type = "OCI_VCN_IP_NATIVE" @@ -441,6 +441,7 @@ resource oci_containerengine_addon starter_oke_addon_dboperator { cluster_id = oci_containerengine_cluster.starter_oke.id remove_addon_resources_on_delete = "true" } +*/ # WebLogic Operator resource oci_containerengine_addon starter_oke_addon_wlsoperator { @@ -455,7 +456,6 @@ resource oci_containerengine_addon starter_oke_addon_certmanager { cluster_id = oci_containerengine_cluster.starter_oke.id remove_addon_resources_on_delete = "true" } -*/ #---------------------------------------------------------------------------- # OUTPUTS diff --git a/option/terraform/tls.j2.tf b/option/terraform/tls.j2.tf index b4ead54e..2bf874b8 100644 --- a/option/terraform/tls.j2.tf +++ b/option/terraform/tls.j2.tf @@ -7,11 +7,40 @@ locals { dns_ip = local.compute_public_ip {%- elif deploy_type == "instance_pool" %} dns_ip = local.instance_pool_lb_ip +{%- elif deploy_type == "kubernetes" and tls == "new_http_01" %} +# No Locals {%- else %} dns_ip = local.apigw_ip {%- endif %} } +{%- if deploy_type == "kubernetes" and tls == "new_http_01" %} +{%- if oke_ocid is defined %} +# Policy defined in group_common +{%- else %} +# Todo: Better use Workload Access Principal - https://docs.oracle.com/en-us/iaas/Content/ContEng/Tasks/contenggrantingworkloadaccesstoresources.htm + +# This is needed for External DNS +resource "oci_identity_dynamic_group" "starter_instance_dyngroup" { + compartment_id = var.tenancy_ocid + name = "${var.prefix}-instance-dyngroup" + description = "${var.prefix}-instance-dyngroup" + matching_rule = "instance.compartment.id = '${var.compartment_ocid}'" + freeform_tags = local.freeform_tags +} + +resource "oci_identity_policy" "oke_tls_policy" { + name = "oke-tls-policy" + description = "oke-tls-policy" + compartment_id = var.compartment_ocid + + statements = [ + "Allow dynamic-group ${var.prefix}-instance-dyngroup to manage dns in compartment id ${var.compartment_ocid}", + "Allow any-user to manage dns in compartment id ${var.compartment_ocid} where all {request.principal.type='workload',request.principal.cluster_id='${local.oke_ocid}',request.principal.service_account='external-dns'}" + ] +} +{%- endif %} +{%- else %} resource "oci_dns_rrset" "starter_rrset" { # XXXX Advanced case with DNS not in OCI XXXX ? count = var.dns_zone_name=="" ? 0 : 1 @@ -29,6 +58,7 @@ resource "oci_dns_rrset" "starter_rrset" { ttl = 300 } } +{%- endif %} {%- if deploy_type == "instance_pool" %} resource "oci_load_balancer_listener" "starter_lb_https_listener" { diff --git a/option/test_suite.sh b/option/test_suite.sh index c8eaea77..3d532510 100755 --- a/option/test_suite.sh +++ b/option/test_suite.sh @@ -56,6 +56,11 @@ loop_db() { loop_shape OPTION_DB=opensearch loop_shape + # NoSQL has no PHP Support + if [ "$OPTION_LANG" != "php" ]; then + OPTION_DB=nosql + loop_shape + fi fi OPTION_DB=none loop_shape @@ -84,6 +89,7 @@ loop_java_framework () { loop_java_vm OPTION_JAVA_FRAMEWORK=micronaut loop_java_vm + OPTION_JAVA_VM=jdk OPTION_JAVA_FRAMEWORK=tomcat loop_db # Reset the value to default @@ -100,6 +106,7 @@ loop_lang () { else loop_java_framework fi + # OCI Function has no PHP support if [ "$OPTION_DEPLOY" != "function" ]; then OPTION_LANG=php loop_db @@ -185,11 +192,16 @@ loop_tls() { OPTION_DEPLOY=compute build_option + OPTION_TLS=new_http_01 + OPTION_DEPLOY=kubernetes + build_option + OPTION_TLS=new_dns_01 OPTION_DEPLOY=container_instance build_option OPTION_GROUP_NAME=dummy + OPTION_TLS=none } loop_deploy() { diff --git a/option/test_suite_shared.sh b/option/test_suite_shared.sh index d130cfb0..8edfb139 100755 --- a/option/test_suite_shared.sh +++ b/option/test_suite_shared.sh @@ -13,6 +13,8 @@ OPTION_GROUP_NAME=dummy OPTION_DB_INSTALL=default OPTION_SHAPE=amd OPTION_INFRA_AS_CODE=terraform_local +OPTION_JAVA_FRAMEWORK=springboot +OPTION_JAVA_VM=jdk # No color for terraforms logs export nocolorarg=1 @@ -90,7 +92,7 @@ build_test () { fi echo "RESULT INFO: "`cat /tmp/result.info` | cut -c 1-100 else - echo "${COLOR_RED}No file /tmp/result.html${COLOR_NONE}" + echo -e "${COLOR_RED}ERROR: No file /tmp/result.html${COLOR_NONE}" fi mv /tmp/result.html ${TEST_DIR}_result_$BUILD_ID.html 2>/dev/null; mv /tmp/result.json ${TEST_DIR}_result_$BUILD_ID.json 2>/dev/null; @@ -193,6 +195,7 @@ build_option() { -mysql_ocid $TF_VAR_mysql_ocid \ -psql_ocid $TF_VAR_psql_ocid \ -opensearch_ocid $TF_VAR_opensearch_ocid \ + -nosql_ocid $TF_VAR_nosql_ocid \ -auth_token $OCI_TOKEN \ -apigw_ocid $TF_VAR_apigw_ocid \ -bastion_ocid $TF_VAR_bastion_ocid \ @@ -259,10 +262,12 @@ pre_test_suite() { git clone https://github.com/mgueury/oci-starter cd $TEST_HOME/oci-starter - ./oci_starter.sh -group_name tsall -group_common atp,mysql,psql,opensearch,database,fnapp,apigw,oke -compartment_ocid $EX_COMPARTMENT_OCID -db_password $TEST_DB_PASSWORD -auth_token $OCI_TOKEN + ./oci_starter.sh -group_name tsall -group_common atp,mysql,psql,opensearch,nosql,database,fnapp,apigw,oke -compartment_ocid $EX_COMPARTMENT_OCID -db_password $TEST_DB_PASSWORD -auth_token $OCI_TOKEN exit_on_error mv output/group_common ../group_common cd $TEST_HOME/group_common + echo "# Test Suite use 2 nodes to avoid error: Too Many Pods (110 pods/node K8s limit)" >> env.sh + echo "export TF_VAR_node_pool_size=2" >> env.sh ./build.sh exit_on_error date diff --git a/py_oci_starter.py b/py_oci_starter.py index 8cda6db0..92e75358 100755 --- a/py_oci_starter.py +++ b/py_oci_starter.py @@ -80,7 +80,7 @@ def mandatory_options(mode): } no_default_options = ['-compartment_ocid', '-oke_ocid', '-vcn_ocid', - '-atp_ocid', '-db_ocid', '-db_compartment_ocid', '-pdb_ocid', '-mysql_ocid', '-psql_ocid', '-opensearch_ocid', + '-atp_ocid', '-db_ocid', '-db_compartment_ocid', '-pdb_ocid', '-mysql_ocid', '-psql_ocid', '-opensearch_ocid', '-nosql_ocid', '-db_user', '-fnapp_ocid', '-apigw_ocid', '-bastion_ocid', '-auth_token', '-tls', '-subnet_ocid','-public_subnet_ocid','-private_subnet_ocid','-shape','-db_install', '-ui', '-deploy', '-database', '-license'] @@ -108,7 +108,7 @@ def allowed_options(): '-java_version': {'8', '11', '17', '21'}, '-kubernetes': {'oke', 'docker'}, '-ui_type': {'html', 'jet', 'angular', 'reactjs', 'jsp', 'php', 'api', 'apex', 'none'}, - '-db_type': {'atp', 'database', 'dbsystem', 'rac', 'db_free', 'pluggable', 'pdb', 'mysql', 'psql', 'opensearch', 'none'}, + '-db_type': {'atp', 'database', 'dbsystem', 'rac', 'db_free', 'pluggable', 'pdb', 'mysql', 'psql', 'opensearch', 'nosql', 'none'}, '-license_model': {'included', 'LICENSE_INCLUDED', 'byol', 'BRING_YOUR_OWN_LICENSE'}, '-infra_as_code': {'terraform_local', 'terraform_object_storage', 'resource_manager'}, '-mode': {CLI, GIT, ZIP}, @@ -168,7 +168,7 @@ def db_rules(): error(f'Pluggable Database needs an existing DB_OCID or PDB_OCID') if params.get('db_user') == None: default_users = {'autonomous': 'admin', 'database': 'system', 'db_free': 'system', - 'pluggable': 'system', 'mysql': 'root', 'psql': 'postgres', 'opensearch': '', + 'pluggable': 'system', 'mysql': 'root', 'psql': 'postgres', 'opensearch': '', 'nosql': '', 'none': ''} params['db_user'] = default_users[params['db_type']] if params.get('db_type')=='none': @@ -328,13 +328,13 @@ def help(): -auth_token (optional) -bastion_ocid' (optional) -compartment_ocid (default tenancy_ocid) - -database (default atp | dbsystem | pluggable | mysql | psql | opensearch | none ) + -database (default atp | dbsystem | pluggable | mysql | psql | opensearch | nosql | none ) -db_ocid (optional) -db_password (mandatory) -db_user (default admin) -deploy (mandatory) compute | kubernetes | function | container_instance -fnapp_ocid (optional) - -group_common (optional) atp | database | mysql | psql | opensearch | fnapp | apigw | oke | jms + -group_common (optional) atp | database | mysql | psql | opensearch | nosql | fnapp | apigw | oke | jms -group_name (optional) -java_framework (default helidon | springboot | tomcat) -java_version (default 21 | 17 | 11 | 8) @@ -345,6 +345,7 @@ def help(): -mysql_ocid (optional) -psql_ocid (optional) -opensearch_ocid (optional) + -nosql_ocid (optional) -oke_ocid (optional) -prefix (default starter) -public_subnet_ocid (optional) @@ -772,6 +773,8 @@ def create_output_dir(): db_family = "psql" elif params['db_type'] == "opensearch": db_family = "opensearch" + elif params['db_type'] == "nosql": + db_family = "nosql" elif params['db_type'] == "none": db_family = "none" params['db_family'] = db_family @@ -840,13 +843,10 @@ def create_output_dir(): output_move("src/oke/oke_deploy.sh", "bin/oke_deploy.sh") output_move("src/oke/oke_destroy.sh", "bin/oke_destroy.sh") - if os.path.exists(output_dir+"/src/app/ingress-app.yaml"): - output_move("src/app/ingress-app.yaml", "src/oke/ingress-app.yaml") - output_replace('##PREFIX##', params["prefix"], "src/app/app.yaml") output_replace('##PREFIX##', params["prefix"], "src/ui/ui.yaml") - output_replace('##PREFIX##', params["prefix"], "src/oke/ingress-app.yaml") - output_replace('##PREFIX##', params["prefix"], "src/oke/ingress-ui.yaml") + output_replace('##PREFIX##', params["prefix"], "src/oke/ingress-app.j2.yaml") + output_replace('##PREFIX##', params["prefix"], "src/oke/ingress-ui.j2.yaml") elif params.get('deploy_type') == "function": cp_terraform_existing("fnapp_ocid", "function.j2.tf") @@ -882,7 +882,7 @@ def create_output_dir(): if params.get('tls'): cp_terraform("tls.j2.tf") - if params.get('deploy_type') == 'kubernetes': + if params.get('deploy_type') == 'kubernetes' and params.get('tls') != 'new_http_01': cp_terraform_apigw("apigw_kubernetes_tls_append.tf") if os.path.exists(output_dir + "/src/app/openapi_spec_append.yaml"): @@ -925,6 +925,9 @@ def create_output_dir(): if params.get('db_type') == "opensearch": cp_terraform_existing("opensearch_ocid", "opensearch.j2.tf") + if params.get('db_type') == "nosql": + cp_terraform_existing("nosql_ocid", "nosql.j2.tf") + if os.path.exists(output_dir + "/src/app/db"): allfiles = os.listdir(output_dir + "/src/app/db") # iterate on all files to move them to destination folder @@ -970,6 +973,9 @@ def create_group_common_dir(): if "opensearch" in a_group_common: cp_terraform_existing("opensearch_ocid", "opensearch.j2.tf") + if "nosql" in a_group_common: + cp_terraform_existing("nosql_ocid", "nosql.j2.tf") + if 'oke' in a_group_common: cp_terraform_existing("oke_ocid", "oke.j2.tf") if 'oke_ocid' not in params: @@ -1010,13 +1016,16 @@ def create_group_common_dir(): jinja2_db_params = { "oracle": { + "db_family_type": "sql", "pomGroupId": "com.oracle.database.jdbc", "pomArtifactId": "ojdbc8", - "pomVersion": "19.3.0.0", + "pomVersion": "21.11.0.0", "jdbcDriverClassName": "oracle.jdbc.OracleDriver", - "dbName": "Oracle" + "dbName": "Oracle", + }, "mysql": { + "db_family_type": "sql", "pomGroupId": "mysql", "pomArtifactId": "mysql-connector-java", "pomVersion": "8.0.31", @@ -1024,6 +1033,7 @@ def create_group_common_dir(): "dbName": "MySQL" }, "psql": { + "db_family_type": "sql", "pomGroupId": "org.postgresql", "pomArtifactId": "postgresql", "pomVersion": "42.7.0", @@ -1031,13 +1041,19 @@ def create_group_common_dir(): "dbName": "PostgreSQL" }, "opensearch": { + "db_family_type": "none", "pomGroupId": "org.opensearch.driver", "pomArtifactId": "opensearch-sql-jdbc", "pomVersion": "1.4.0.1", "jdbcDriverClassName": "org.opensearch.jdbc.Driver", "dbName": "OpenSearch" }, + "nosql": { + "db_family_type": "none", + "dbName": "NoSQL" + }, "none": { + "db_family_type": "none", "dbName": "No Database" } } @@ -1131,7 +1147,7 @@ def jinja2_replace_template(): # Use a bastion only for the database if params.get('db_type')!='none': params['bastion_ocid'] = TO_FILL - to_ocid = { "atp": "atp_ocid", "database": "db_ocid", "mysql": "mysql_ocid", "psql": "psql_ocid", "opensearch": "opensearch_ocid", "oke": "oke_ocid", "fnapp": "fnapp_ocid", "apigw": "apigw_ocid", "jms": "jms_ocid", "compute": "compute_ocid"} + to_ocid = { "atp": "atp_ocid", "database": "db_ocid", "mysql": "mysql_ocid", "psql": "psql_ocid", "opensearch": "opensearch_ocid", "nosql": "nosql_ocid", "oke": "oke_ocid", "fnapp": "fnapp_ocid", "apigw": "apigw_ocid", "jms": "jms_ocid", "compute": "compute_ocid"} for x in a_group_common: if x in to_ocid: ocid = to_ocid[x]