ERROR.. Unhandled exception during build. When running pipeline - django

Trying to run a pipeline, my set up:
GitHub
Elastic Beanstalk with autoscaling and load balancer
S3 Bucket for persistent static-files
After running the pipeline it fails to deploy all instances with the same version.
So from my logs I've found out that it fails when running the container_commands.
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: project.settings
"PYTHONPATH": "/opt/python/current/:$PYTHONPATH"
"aws:elasticbeanstalk:container:python":
WSGIPath: project.wsgi:application
NumProcesses: 3
NumThreads: 20
"aws:elasticbeanstalk:environment:proxy:staticfiles":
/html: statichtml
/static-files: static-files
/media: media-files
**container_commands:
10_deploy_hook_permissions:
command: |
sudo find .platform/ -type f -iname "*.sh" -exec chmod -R 755 {} \;
sudo find /var/app/staging/.platform/ -type f -iname "*.sh" -exec chmod -R 755 {} \;**
Which it execute this:
#!/bin/sh
source /var/app/venv/staging-LQM1lest/bin/activate
python /var/app/current/manage.py collectstatic --noinput
python /var/app/current/manage.py migrate
NOTE: The app deploys perfectly fine when I run "eb deploy", but when deployment is triggered from pipeline than it goes in to "Degraded" status where the instance is still running but apparently no all the instances are running the same version.
Error from health:
over all : - Command failed on all instances.
- Incorrect application version found on all instances. Expected version
"app-221124_115515680871" (deployment 418).
instance : - Application deployment failed at 2022-11-24T11:32:55Z with exit status 1
and error: Engine execution has encountered an error.
- Incorrect application version "code-pipeline-575-
3e3311a14d472807da9bcf9f73d5778e38c9c651" (deployment 420). Expected
version "app-221124_1680871" (deployment 418).
from commands-logs
2022-11-24 15:36:24,190 P17748 [INFO] Command 10_deploy_hook_permissions
2022-11-24 15:36:24,239 P17748 [INFO] -----------------------Command Output-----------------------
2022-11-24 15:36:24,239 P17748 [INFO] find: ‘.platform/’: No such file or directory
2022-11-24 15:36:24,239 P17748 [INFO] find: ‘/var/app/staging/.platform/’: No such file or directory
2022-11-24 15:36:24,239 P17748 [INFO] ------------------------------------------------------------
2022-11-24 15:36:24,239 P17748 [ERROR] Exited with error code 1
Also tried with different config :
container_commands:
01_makemigrations:
command: "source /var/app/venv/*/bin/activate && python3 manage.py makemigrations --noinput"
02_migrate:
command: "source /var/app/venv/*/bin/activate && python3 manage.py migrate --noinput"
03_collectstatic:
command: "source /var/app/venv/*/bin/activate && python3 manage.py collectstatic --noinput"
But this time I get this wrror :
2022-11-25 08:56:46,187 P2294 [INFO] Command 01_makemigrations
2022-11-25 08:56:46,222 P2294 [INFO] -----------------------Command Output-----------------------
2022-11-25 08:56:46,222 P2294 [INFO] python3: can't open file 'manage.py': [Errno 2] No such file or directory
2022-11-25 08:56:46,222 P2294 [INFO] ------------------------------------------------------------
2022-11-25 08:56:46,222 P2294 [ERROR] Exited with error code 2
I've also set the variable globally in my Elastic Beanstalk Env. Properties
Not sure what to do anymore.
It is clear that it cannot find the file or run the commands

So the commands now run when i enter the backend folder
container_commands:
10_deploy_hook_permissions:
command: |
sudo find .platform/ -type f -iname "*.sh" -exec chmod -R 755 {} \;
sudo find /var/app/staging/backend/.platform/ -type f -iname "*.sh" -exec chmod -R 755 {} \;
However everything looks fine pipeline and commands wise but the server is not running, I don't get any errors messages.

Related

Bash Script in AWS codedeploy is not executed

I tried to deploy an api service from github to ec2 via code deploy, code from github is transferred to the appropriate lo location but when i try to execute the bash script it is failing
appspec.yml
version: 0.0
os: linux
files:
- source: /
destination: /home/ec2-user/Gritly-backend-v2
hooks:
AfterInstall:
- location: build.sh
timeout: 400
runas: root
build.sh
#!/bin/bash
cd /home/ec2-user/Gritly-backend-v2
docker build . -t gritly_backend -f Dockerfile.dev
docker stop gritly_backend
docker rm gritly_backend
docker run -d -p 80:80 --name gritly_backend gritly_backend
OUTPUT=$(docker images --filter "dangling=true" -q --no-trunc | wc -l)
if [ ${OUTPUT} != 0 ];
then
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
fi
Error
LifecycleEvent - AfterInstall
Script - build.sh
[stderr]bash: /opt/codedeploy-agent/deployment-root/4ceeb37e-1874-4290-9e55-a92d655d1558/d-G03KT19HJ/deployment-archive/build.sh: /bin/bash^M: bad interpreter: No such file or directory
tried to run sh build.sh
: No such file or directory/ec2-user/Gritly-backend-v2
: no such file or directoryunable to evaluate symlinks in Dockerfile path: lstat /home/ec2-user/Gritly-backend-v2/Dockerfile.dev
Error response from daemon: No such container: gritly_backend
Error: No such container: gritly_backend
docker: invalid reference format.
See 'docker run --help'.
build.sh: line 8: $'\r': command not found

Publish beanstalk environment hook issues

I have an issue with my script. I use beanstalk to deploy my ASP.NET Core code. And in my post deploy I have this code:
#!/usr/bin/env bash
file1=sudo cat /opt/elasticbeanstalk/config/ebenvinfo/region
file2=/opt/elasticbeanstalk/bin/get-config container -k environment_name
file3=$file2.$file1.elasticbeanstalk.com
echo $file3
sudo certbot -n -d $file3 --nginx --agree-tos --email al#gmail.com
It works perfectly if I launch it on the instance but in the postdeploy script I have the error:
[ERROR] An error occurred during execution of command [app-deploy] - [RunAppDeployPostDeployHooks]. Stop running the command. Error: Command .platform/hooks/postdeploy/00_get_certificate.sh failed with error fork/exec .platform/hooks/postdeploy/00_get_certificate.sh: exec format error
PS: My script has .ebextension which allows exec rights
container_commands:
00_permission_hook:
command: "chmod +x .platform/hooks/postdeploy/00_get_certificate.sh"
What's wrong?
I had the same issue and added
#!/bin/bash
to the top of the sh file and also ran "chmod +x" to the sh file and it was solved

Google Cloud Run error: Invalid Command \"/bin/sh\": file not found when deploying from Docker image

I am trying to deploy a fairly simple Python web app with FastAPI and Gunicorn on Google Cloud Run with a Docker container following this tutorial and upon deploying I keep falling on the same error:
Invalid ENTRYPOINT. [name: "gcr.io/<project_id>/<image_name>#sha256:xxx" error: "Invalid command \"/bin/sh\": file not found" ].
It works fine to build the image and push it to the Container Registry.
On the cloud run I have set my secrets for database connection and I am passing as an argument to the Dockerfile which settings.py file to use for the production environment, as I did locally to build/run the container.
Any idea on what am I missing or doing wrong in the process? It's my first attempt to deploying a web app on a cloud service so I might not have all the concepts on point just yet.
Dockerfile
FROM ubuntu:latest
ENV PYTHONUNBUFFERED 1
RUN apt update && apt upgrade -y
RUN apt install -y -q build-essential python3-pip python3-dev
RUN pip3 install -U pip setuptools wheel
RUN pip3 install gunicorn uvloop httptools
COPY requirements.txt /code/requirements.txt
RUN pip3 install -r /code/requirements.txt
COPY . code/
# Pass the settings_module as an argument when building the image
ARG SETTINGS_MODULE
ENV SETTINGS_MODULE $SETTINGS_MODULE
EXPOSE $PORT
CMD exec /usr/local/bin/gunicorn -b :$PORT -w 4 -k uvicorn.workers.UvicornWorker app.main:app --chdir /code
cloudbuild.yaml
steps:
- name: gcr.io/cloud-builders/docker
args: ["build", "--build-arg", "SETTINGS_MODULE=app.settings_production", "-t", "gcr.io/$PROJECT_ID/<image_name>", "."]
images:
- gcr.io/$PROJECT_ID/<image_name>
gcloud builds submit --config=cloudbuild.yaml
Update
I replaced ubuntu:latest (==20.04) with debian:buster-slim and it worked.
Previously
Deploying to Cloud Run, I receive the error too...I suspect it's the PORT, investigating. Not the PORT. Curiously, the image runs locally. Trying a different OS!
I repro'd your Dockerfile and cloudbuild.yaml in a project and the build and run succeed for me:
docker run \
--interactive --tty \
--env=PORT=8888 \
gcr.io/${PROJECT}/67486954
[2021-05-11 16:09:44 +0000] [1] [INFO] Starting gunicorn 20.1.0
[2021-05-11 16:09:44 +0000] [1] [INFO] Listening at: http://0.0.0.0:8888 (1)
NOTE To build from a Dockerfile, you need not create a cloudbuild.yaml and can just gcloud builds submit --tag gcr.io/PROJECT_ID/...
A good way to diagnose the issue is to run the docker build locally:
docker build \
--build-arg=SETTINGS_MODULE=app.settings_production \
--tag=gcr.io/$PROJECT_ID/<image_name> \
.
And then attempt to run it:
docker run \
--interactive --tty --rm \
gcr.io/$PROJECT_ID/<image_name>
This isolates Cloud Build as the issue and will likely result in the same error.
The error suggests that the container isn't finding a shell (/bin/sh) in the ubuntu:latest image which is curious.
I think you can|should drop the `exec` after `CMD`
NOTE I read through Google's tutorial and see that the instructions include CMD exec ..., I'm unclear why that would be necessary but presumably it's not a problem.
Can you run the gunicorn command locally without issue??
/usr/local/bin/gunicorn -b :$PORT -w 4 -k uvicorn.workers.UvicornWorker app.main:app --chdir /code
The placement of --chdir /code is curious too. How about:
WORKDIR code
COPY . .
...
CMD /usr/local/bin/gunicorn -b :$PORT -w 4 -k uvicorn.workers.UvicornWorker app.main:app
Hmmm link perhaps move the --chdir before the app.main:app too so that it's applied to the gunicorn rather than your app.
/usr/local/bin/gunicorn -b :$PORT -w 4 -k uvicorn.workers.UvicornWorker --chdir /code app.main:app

Cannot Start Django in Docker

I am trying to run Django-packages in Docker, but I am unable to.
I am getting the following:-
django_1 | Postgres is up - continuing...
django_1 | python: can't open file 'manage.py': [Errno 2] No such file or directory
django_1 | python: can't open file 'manage.py': [Errno 2] No such file or directory
Everything seems to be fine though.The daemon is running.
docker ps
C:\djangopackages-master>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker volume ls
C:\djangopackages-master>docker volume ls
DRIVER VOLUME NAME
local djangopackagesmaster_postgres_backup_dev
local djangopackagesmaster_postgres_data_dev
local ef5505952d82c1472e74e21a8d2921018b2f7ee5570742268c8560335fe5762b
Cant seem to figure out what might be the issue.
How i built the docker container:
Ran docker-compose -f dev.yml up after cloning the following repo.
https://github.com/djangopackages/djangopackages
Docker Files present for creation:-
https://github.com/djangopackages/djangopackages/blob/master/compose/caddy/Dockerfile
https://github.com/djangopackages/djangopackages/blob/master/compose/django/Dockerfile
https://github.com/djangopackages/djangopackages/blob/master/compose/django/Dockerfile-dev
https://github.com/djangopackages/djangopackages/blob/master/compose/postgres/Dockerfile
https://github.com/djangopackages/djangopackages/blob/master/compose/redis/Dockerfile
In this particular dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
# Requirements have to be pulled and installed here, otherwise caching won't work
COPY ./requirements.txt /requirements.txt
COPY ./manage.py /manage.py
RUN pip install -r /requirements.txt
COPY ./compose/django/entrypoint.sh /entrypoint.sh
RUN sed -i 's/\r//' /entrypoint.sh
RUN chmod +x /entrypoint.sh
COPY ./compose/django/start-dev.sh /start-dev.sh
RUN sed -i 's/\r//' /start-dev.sh
RUN chmod +x /start-dev.sh
WORKDIR /app
ENTRYPOINT ["/entrypoint.sh"]
If I tell to copy manage.py as well then manage.py runs(with errors of course). It seems it is not running the manage.py in the correct path. not sure what to do here anymore.

gunicorn systemd startup not working with django inside a vagrant box

I translated this tutorial into a chef recipe. And so far everything apart from starting gunicorn (the right way) seems to be working.
For example when I shut down the machine after the initial setup and provisioning via vagrant halt and then start it up again with vagrant up - I always get an 502 Bad Gateway error.
Then I have to ssh into the box and run these commands manualy
sudo systemctl daemon-reload
sudo systemctl restart gunicorn
After that everything is working again.
What I dont understand is when I run sudo systemctl status gunicorn before I reload the daemon and restart gunicorn - it tells me that gunicorn is running.
Here is my gunicorn.service file contents that get written to /etc/systemd/sytem/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/vagrant_data
ExecStart=/vagrant_data/myprojectenv/bin/gunicorn --workers 5 --bind unix:/home/ubuntu/run/myproject.sock myproject.wsgi:application --reload
[Install]
WantedBy=multi-user.target
My projects folder structure is:
/home/ubuntu/myproject ls
manage.py myproject myprojectenv
/home/ubuntu/run ls
myproject.sock
I symlinked the myproject folder to vagrant_data which is setup to be the vm.synced_folder in my Vagrantfile.
this is all running on a ubuntu/xenial64 vagrant box.
UPDATE:
include_recipe 'locale'
include_recipe 'apt'
execute 'install requirements' do
command 'sudo apt-get install -y python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx'
not_if ('sudo dpkg -l | grep postgresql')
end
bash 'setup database and user' do
user 'postgres'
code <<-EOF
echo "CREATE DATABASE #{node['dbname']};" | psql
echo "CREATE USER #{node['dbuser']} WITH PASSWORD '#{node['dbpass']}';" | psql
echo "ALTER ROLE #{node['dbuser']} SET client_encoding TO 'utf8';" | psql
echo "ALTER ROLE #{node['dbuser']} SET default_transaction_isolation TO 'read committed';" | psql
echo "ALTER ROLE #{node['dbuser']} SET timezone TO 'UTC';" | psql
echo "GRANT ALL PRIVILEGES ON DATABASE #{node['dbname']} TO #{node['dbuser']};" | psql
EOF
not_if { `sudo -u postgres psql -tAc \"SELECT * FROM pg_database WHERE datname='#{node['dbname']}'\" | wc -l`.chomp == "1" }
end
execute 'install virtualenv' do
command 'sudo pip3 install virtualenv'
not_if ('sudo pip3 list | grep virtualenv')
end
link "/home/ubuntu/#{node['projectDir']}" do
to '/vagrant_data'
owner 'ubuntu'
group 'www-data'
end
directory '/home/ubuntu/run' do
owner 'ubuntu'
group 'www-data'
action :create
end
bash 'configure and install django' do
code <<-EOF
cd /home/ubuntu/#{node['projectDir']}
virtualenv myprojectenv
source myprojectenv/bin/activate
pip install django gunicorn psycopg2
django-admin.py startproject #{node['projectDir']} .
deactivate
EOF
not_if { ::File::exists?("/home/ubuntu/#{node['projectDir']}/#{node['projectDir']}")}
end
###############
# Note : In development set workers to 1 which will reload the code after each request
# in production set it to cores x 2 + 1 ... which would mostly result in 5 workers
##########
template '/etc/systemd/system/gunicorn.service' do
source 'gunicorn.erb'
owner 'root'
group 'root'
end
template '/etc/nginx/sites-available/myproject' do
source 'test.erb'
owner 'www-data'
group 'www-data'
end
execute 'link to sites-enabled' do
command 'sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled'
not_if { ::File.symlink?('/etc/nginx/sites-enabled/myproject')}
end
execute 'remove default host' do
command 'sudo rm /etc/nginx/sites-enabled/default'
only_if { ::File.exists?('/etc/nginx/sites-enabled/default') }
end
bash 'enable gunicorn' do
code <<-EOF
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
EOF
#not_if { ::File.exists?('/home/ubuntu/run/myproject.sock')}
end
execute 'test nginx' do
command 'sudo nginx -t'
end
execute 'restart nginx' do
command 'sudo service nginx restart'
end
Does anyone know what I am doing wrong ?
UPDATE:
Still not working - after trying almost everything google had to offer.
Now I switched to a kind of workaround with the vagrant-triggers plugin and defined the needed commands for gunicorn in Vagrantfile.
config.trigger.after :up do
run_remote "sudo systemctl daemon-reload"
run_remote "sudo systemctl restart gunicorn"
end
That way I don't have to call vagrant up --provision every time I turn on the machine.
But still I would really like to know how to get that thing started the right way.