@glicogh/healthcare-cdk 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ echo "Deployment complete"
2
+ alert_slack 'success'
@@ -0,0 +1,35 @@
1
+ # install nginx
2
+ sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y
3
+ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
4
+ gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
5
+ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
6
+ echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx
7
+ sudo apt update -y
8
+ sudo apt install nginx nodejs dnsmasq supervisor -y
9
+
10
+ # copy appropriate server_name.conf based on env
11
+ cat $service_dir/nginx/microservices/server_name.{{envName}}.conf | sudo tee $service_dir/nginx/microservices/server_name.conf
12
+
13
+ # remove default virtual host
14
+ sudo rm -rf /etc/nginx/sites-enabled/default /etc/nginx/conf.d/default.conf
15
+ # create symlinks
16
+ sudo ln -sf $service_dir/nginx/microservices /etc/nginx/microservices
17
+ sudo ln -sf $service_dir/nginx/conf.d/microservices.conf /etc/nginx/conf.d/microservices.conf
18
+
19
+ # configure dnsmasq
20
+ echo "# ================================" | sudo tee -a /etc/dnsmasq.conf
21
+ echo "conf-dir=/etc/dnsmasq.d/,*.conf" | sudo tee -a /etc/dnsmasq.conf
22
+ echo "conf-dir=$service_dir/dnsmasq/conf.d/,*.conf" | sudo tee -a /etc/dnsmasq.conf
23
+ echo "port=5353" | sudo tee -a /etc/dnsmasq.conf
24
+
25
+ # configure supervisor
26
+ sudo sed -i "s|files = .*|files = /etc/supervisor/conf.d/*.conf ${service_dir}/supervisor/conf.d/*.conf|" /etc/supervisor/supervisord.conf
27
+
28
+ # start supervisor
29
+ sudo systemctl restart supervisor
30
+
31
+ # start dnsmasq
32
+ sudo systemctl restart dnsmasq
33
+
34
+ # start nginx server
35
+ sudo systemctl restart nginx
@@ -0,0 +1,166 @@
1
+ #!/bin/bash
2
+
3
+ # abort immediately on error
4
+ set -Eeuo pipefail
5
+
6
+ # set variables
7
+ deployment_started_at=$(date)
8
+ deployment_started_at_seconds=$(date +%s)
9
+ env_name={{envName}}
10
+ microservices_dir="/microservices"
11
+ services_dir="$microservices_dir/services"
12
+ service_dir="$services_dir/{{serviceName}}"
13
+ configuration_dir="$microservices_dir/config"
14
+ logs_dir="$microservices_dir/logs"
15
+
16
+ ssm_global_parameter_path=/{{envName}}/microservices/global/env/
17
+ ssm_service_parameter_path=/{{envName}}/microservices/{{serviceName}}/env/
18
+ deployment_log_file="$logs_dir/deployment.log"
19
+ deployment_git_branch="{{gitDeploymentBranch}}"
20
+ instance_id=$(ec2metadata --instance-id)
21
+ instance_private_ip=$(ec2metadata --local-ipv4)
22
+
23
+ # create directories
24
+ sudo mkdir -p $microservices_dir
25
+ sudo mkdir -p $services_dir
26
+ sudo mkdir -p $configuration_dir
27
+ sudo mkdir -p $logs_dir
28
+ cd $microservices_dir
29
+
30
+ # write outputs to a file
31
+ exec &>>"$deployment_log_file"
32
+
33
+ # update and install packages
34
+ sudo apt update -y
35
+ sudo apt install curl zip unzip -y
36
+
37
+ # remove redundant packages
38
+ sudo apt remove apache2 -y
39
+
40
+ # install AWS CLI
41
+ curl "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip" -o "awscliv2.zip"
42
+ unzip -o awscliv2.zip
43
+ sudo ./aws/install --update
44
+ sudo rm -rf awscliv2.zip aws
45
+
46
+ # slack
47
+ slack_api_key=""
48
+ alert_slack() {
49
+ deployment_ended_at=$(date)
50
+ deployment_ended_at_seconds=$(date +%s)
51
+ deployment_time_diff=$((deployment_ended_at_seconds - deployment_started_at_seconds))
52
+ deployment_duration=$(echo $deployment_time_diff | awk '{printf "%d:%02d:%02d", $1/3600, ($1/60)%60, $1%60}')
53
+
54
+ if [[ $slack_api_key == "" ]]; then
55
+ slack_api_key=$(aws ssm get-parameter --name ${ssm_global_parameter_path}SLACK_API_KEY --with-decryption --query "Parameter.Value" --output text)
56
+ fi
57
+
58
+ if [[ $1 == 'success' ]]; then
59
+ alert_title="Deployment successful :white_check_mark:"
60
+ else
61
+ alert_title="Deployment failed :fire:"
62
+ fi
63
+
64
+ alert_content=$(echo $(tail -c 600 $logs_dir/deployment.log) | jq -Rs .)
65
+
66
+ payload='{
67
+ "channel": "#deployments",
68
+ "blocks": [
69
+ {
70
+ "type": "header",
71
+ "text": {
72
+ "type": "plain_text",
73
+ "text": "'$alert_title'"
74
+ }
75
+ },
76
+ {
77
+ "type": "section",
78
+ "text": {
79
+ "type": "mrkdwn",
80
+ "text": "<!channel>"
81
+ }
82
+ },
83
+ {
84
+ "type": "context",
85
+ "elements": [
86
+ {
87
+ "type": "mrkdwn",
88
+ "text": "*Environment:* {{envName}}"
89
+ },
90
+ {
91
+ "type": "mrkdwn",
92
+ "text": "*Service:* {{serviceName}}"
93
+ },
94
+ {
95
+ "type": "mrkdwn",
96
+ "text": "*Instance:* '$instance_id' ('$instance_private_ip')"
97
+ }
98
+ ]
99
+ },
100
+ {
101
+ "type": "context",
102
+ "elements": [
103
+ {
104
+ "type": "mrkdwn",
105
+ "text": "*Started At:* '$deployment_started_at'"
106
+ },
107
+ {
108
+ "type": "mrkdwn",
109
+ "text": "*Ended At:* '$deployment_ended_at'"
110
+ },
111
+ {
112
+ "type": "mrkdwn",
113
+ "text": "*Duration:* '$deployment_duration'"
114
+ }
115
+ ]
116
+ },
117
+ {
118
+ "type": "rich_text",
119
+ "elements": [
120
+ {
121
+ "type": "rich_text_preformatted",
122
+ "elements": [
123
+ {
124
+ "type": "text",
125
+ "text": '$alert_content'
126
+ }
127
+ ],
128
+ "border": 0
129
+ }
130
+ ]
131
+ }
132
+ ]
133
+ }
134
+ '
135
+ echo $payload | curl --location 'https://slack.com/api/chat.postMessage' --header 'Content-Type: application/json' --header 'Authorization: Bearer '$slack_api_key --data @-
136
+ }
137
+
138
+ # catch errors
139
+ on_error_caught() {
140
+ echo 'Error encountered'
141
+ alert_slack 'error'
142
+ exit
143
+ }
144
+ trap 'on_error_caught' ERR
145
+
146
+ # create swap file
147
+ sudo fallocate -l 2G /swapfile
148
+ sudo chmod 600 /swapfile
149
+ sudo mkswap /swapfile
150
+ sudo swapon /swapfile
151
+ sudo cp /etc/fstab /etc/fstab.bak
152
+ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
153
+
154
+ sudo sysctl vm.swappiness=10
155
+ echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
156
+
157
+ sudo sysctl vm.vfs_cache_pressure=40
158
+ echo 'vm.vfs_cache_pressure=40' | sudo tee -a /etc/sysctl.conf
159
+
160
+ # load Global SSM parameters into ENV
161
+ aws ssm get-parameters-by-path --with-decryption --path $ssm_global_parameter_path --query "Parameters[*].{Name:Name,Value:Value}" | sudo sed 's,'"$ssm_global_parameter_path"',,' >$configuration_dir/global-ssm-env.json
162
+ sudo jq -jr '.[]|.Name,"=","\"",.Value,"\"","\n"' <$configuration_dir/global-ssm-env.json >$configuration_dir/global-ssm.env
163
+
164
+ # load Service-specific SSM parameters into ENV
165
+ aws ssm get-parameters-by-path --with-decryption --path $ssm_service_parameter_path --query "Parameters[*].{Name:Name,Value:Value}" | sudo sed 's,'"$ssm_service_parameter_path"',,' >$configuration_dir/service-ssm-env.json
166
+ sudo jq -jr '.[]|.Name,"=","\"",.Value,"\"","\n"' <$configuration_dir/service-ssm-env.json >$configuration_dir/service-ssm.env
@@ -0,0 +1,12 @@
1
+ # install Git
2
+ sudo apt install git -y
3
+
4
+ # retrieve github api key from SSM
5
+ github_api_key=$(aws ssm get-parameter --name ${ssm_global_parameter_path}GITHUB_API_KEY --with-decryption --query "Parameter.Value" --output text)
6
+
7
+ # clone project from github into service directory
8
+ sudo rm -rf $service_dir
9
+ sudo git clone https://$github_api_key@github.com/{{githubRepoPath}} $service_dir
10
+ cd $service_dir
11
+ git checkout $deployment_git_branch
12
+ git pull origin $deployment_git_branch
@@ -0,0 +1,86 @@
1
+ # install packages required for PHP/Laravel
2
+ sudo apt install curl gpg software-properties-common ca-certificates apt-transport-https lsb-release -y
3
+ sudo add-apt-repository ppa:ondrej/php -y
4
+ sudo apt install php8.3 supervisor -y
5
+ sudo apt install php8.3-cli php8.3-pdo php8.3-mysql php8.3-zip php8.3-gd php8.3-mbstring php8.3-curl php8.3-xml php8.3-bcmath php8.3-common php8.3-dev php8.3-redis php8.3-swoole -y
6
+
7
+ # install composer
8
+ sudo curl -sS https://getcomposer.org/installer | sudo php
9
+ sudo mv composer.phar /usr/local/bin/composer
10
+ sudo ln -sf /usr/local/bin/composer /usr/bin/composer
11
+
12
+ microservice_port=8080
13
+
14
+ # create .env file
15
+ sudo echo "MICROSERVICE_INTERNAL_IP=\"$instance_private_ip\"" >> $configuration_dir/local.env
16
+ sudo echo "MICROSERVICE_INTERNAL_PORT=\"$microservice_port\"" >> $configuration_dir/local.env
17
+ sudo echo "DEPLOYED_ENV={{envName}}" >> $configuration_dir/local.env
18
+ sudo cat $configuration_dir/global-ssm.env >> .env
19
+ sudo cat $configuration_dir/service-ssm.env >> .env
20
+ sudo cat $configuration_dir/local.env >> .env
21
+
22
+ # run composer as the ubuntu user. Composer doesn't like being run as root
23
+ sudo chown -R ubuntu:ubuntu $microservices_dir
24
+ su - ubuntu -c "ssh-keyscan -t rsa,dsa github.com >> ~/.ssh/known_hosts"
25
+ su - ubuntu -c "composer config --global github-oauth.github.com $github_api_key"
26
+ su - ubuntu -c "cd $service_dir && composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev"
27
+ #su - ubuntu -c "cd $service_dir && composer require glicogh/healthcare-laravel-package:dev-master --no-interaction --prefer-dist --optimize-autoloader"
28
+
29
+ # remove redundant packages
30
+ sudo apt remove apache2 -y
31
+
32
+ # install openswoole
33
+ # echo '>>> Installing openswoole'
34
+ # sudo pecl install --configureoptions 'enable-sockets="no" enable-openssl="no" enable-http2="no" enable-mysqlnd="no" enable-hook-curl="no" enable-cares="yes" with-postgres="no"' openswoole
35
+ # echo 'extension=openswoole.so' | sudo tee /etc/php/8.3/mods-available/openswoole.ini
36
+ # sudo ln -sf /etc/php/8.3/mods-available/openswoole.ini /etc/php/8.3/cli/conf.d/20-openswoole.ini
37
+ # echo '::: Openswoole installation complete'
38
+
39
+ php $service_dir/artisan octane:install --server swoole --no-interaction
40
+
41
+ # optimize and start laravel
42
+ php $service_dir/artisan migrate --force
43
+ if [[ ${env_name} != "prod" ]]; then
44
+ php artisan db:seed
45
+ fi
46
+
47
+ # php $service_dir/artisan cache:clear
48
+ php $service_dir/artisan route:cache
49
+ php $service_dir/artisan config:cache
50
+ php $service_dir/artisan view:cache
51
+
52
+ # create supervisor config for generic queue
53
+ cat > $configuration_dir/supervisor.{{serviceName}}-service-generic-queue.conf <<EOL
54
+ [program:{{serviceName}}-service-generic-queue]
55
+ process_name=%(program_name)s_%(process_num)02d
56
+ command=php $service_dir/artisan queue:work --sleep=3 --tries=3
57
+ autostart=true
58
+ autorestart=true
59
+ stopasgroup=true
60
+ killasgroup=true
61
+ user=ubuntu
62
+ numprocs=2
63
+ redirect_stderr=true
64
+ stdout_logfile=$logs_dir/{{serviceName}}-service-generic-queue.log
65
+ EOL
66
+ sudo ln -sf $configuration_dir/supervisor.{{serviceName}}-service-generic-queue.conf /etc/supervisor/conf.d/{{serviceName}}-service-generic-queue.conf
67
+
68
+ # create supervisor config for web server
69
+ cat > $configuration_dir/supervisor.{{serviceName}}-service-server.conf <<EOL
70
+ [program:{{serviceName}}-service-server]
71
+ process_name=%(program_name)s_%(process_num)02d
72
+ command=php $service_dir/artisan octane:start --port $microservice_port --host $instance_private_ip
73
+ autostart=true
74
+ autorestart=true
75
+ stopasgroup=true
76
+ killasgroup=true
77
+ user=ubuntu
78
+ numprocs=1
79
+ redirect_stderr=true
80
+ stdout_logfile=$logs_dir/{{serviceName}}-service-server.log
81
+ EOL
82
+ sudo ln -sf $configuration_dir/supervisor.{{serviceName}}-service-server.conf /etc/supervisor/conf.d/{{serviceName}}-service-server.conf
83
+
84
+ sudo systemctl restart supervisor
85
+
86
+ php $service_dir/artisan glico:healthcare:microservice:discovery
@@ -0,0 +1,22 @@
1
+ # install MySQL
2
+ sudo apt install mariadb-server -y
3
+
4
+ # make MySQL service/daemon
5
+ sudo systemctl enable mariadb
6
+
7
+ # start MySQL
8
+ sudo systemctl start mariadb
9
+
10
+ # setup/configure MySQL
11
+ mysql_root_password=$(aws ssm get-parameter --name ${ssm_global_parameter_path}MYSQL_ROOT_PASSWORD --with-decryption --query "Parameter.Value" --output text)
12
+ mysql_root_user=root
13
+ microservices_db_password=$(aws ssm get-parameter --name ${ssm_global_parameter_path}MICROSERVICES_DB_PASSWORD --with-decryption --query "Parameter.Value" --output text)
14
+ microservices_db_user=microservices
15
+ sudo mysql -e "SET PASSWORD FOR '$mysql_root_user'@'localhost' = PASSWORD('$mysql_root_password');"
16
+ sudo mysql -e "CREATE USER '$microservices_db_user'@'localhost' IDENTIFIED BY '$microservices_db_password'; GRANT ALL PRIVILEGES ON *.* TO '$microservices_db_user'@'localhost' WITH GRANT OPTION;"
17
+ sudo mysql -e "CREATE USER '$microservices_db_user'@'%' IDENTIFIED BY '$microservices_db_password'; GRANT ALL PRIVILEGES ON *.* TO '$microservices_db_user'@'%' WITH GRANT OPTION;"
18
+ sudo mysql -e "FLUSH PRIVILEGES;"
19
+ sudo sed -i "s/bind-address.*/bind-address = 0.0.0.0/" /etc/mysql/mariadb.conf.d/50-server.cnf
20
+
21
+ # restart MySQL
22
+ sudo systemctl restart mariadb
@@ -0,0 +1,5 @@
1
+ sudo curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
2
+ sudo apt install nodejs -y
3
+
4
+ sudo cp $configuration_dir/ssm.env .env
5
+ npm install
@@ -0,0 +1,32 @@
1
+ # install nginx
2
+ sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y
3
+ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
4
+ gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
5
+ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
6
+ echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx
7
+ sudo apt update -y
8
+ sudo apt install nginx npm -y
9
+
10
+ # create .env
11
+ sudo echo "DEPLOYED_ENV={{envName}}" >> $configuration_dir/local.env
12
+ sudo cat $configuration_dir/global-ssm.env >> .env
13
+ sudo cat $configuration_dir/service-ssm.env >> .env
14
+ sudo cat $configuration_dir/local.env >> .env
15
+
16
+ # copy appropriate server_name.conf based on env
17
+ cat $service_dir/nginx/portal/server_name.{{envName}}.conf | sudo tee $service_dir/nginx/portal/server_name.conf
18
+
19
+ # remove default virtual host
20
+ sudo rm -rf /etc/nginx/sites-enabled/default /etc/nginx/conf.d/default.conf
21
+
22
+ # create symlinks
23
+ sudo ln -sf $service_dir/nginx/portal /etc/nginx/portal
24
+ sudo ln -sf $service_dir/nginx/conf.d/portal.conf /etc/nginx/conf.d/portal.conf
25
+
26
+ # install dependencies and generate static files
27
+ cd $service_dir
28
+ sudo npm i
29
+ sudo NODE_OPTIONS="--max-old-space-size=2048 NUXT_TELEMETRY_DISABLED=1" npm run generate
30
+
31
+ # start nginx server
32
+ sudo systemctl restart nginx
@@ -0,0 +1,9 @@
1
+ sudo apt install lsb-release curl gpg -y
2
+ curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
3
+ sudo chmod 644 /usr/share/keyrings/redis-archive-keyring.gpg
4
+ echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
5
+ sudo apt update -y
6
+ sudo apt install redis -y
7
+ sudo sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g' /etc/redis/redis.conf
8
+ sudo sed -i 's/protected-mode yes/protected-mode no/g' /etc/redis/redis.conf
9
+ sudo systemctl restart redis-server
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@glicogh/healthcare-cdk",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "CDK automations",
5
5
  "bin": {
6
6
  "cdk": "bin/cdk.js"
7
7
  },
8
8
  "scripts": {
9
- "build": "rm -rf ./dist && tsc",
9
+ "build": "rm -rf ./dist && tsc && cp -r ./scripts ./dist/scripts",
10
10
  "watch": "tsc -w",
11
11
  "test": "jest",
12
12
  "cdk": "cdk",
13
- "pub": "npm run build && npm publish"
13
+ "ship": "npm run build && npm publish"
14
14
  },
15
15
  "main": "dist/index.js",
16
16
  "devDependencies": {