diff --git a/url-shortener/frontend/Dockerfile b/url-shortener/frontend/Dockerfile index 11405b7..6d94900 100644 --- a/url-shortener/frontend/Dockerfile +++ b/url-shortener/frontend/Dockerfile @@ -1,38 +1,22 @@ -FROM node:18-alpine AS deps -LABEL authors="arnavrneo" +FROM node:latest -RUN apk add --no-cache libc6-compat -WORKDIR /app +# Set the working directory inside the container +WORKDIR /app -COPY package.json package-lock.json ./ -RUN npm install --production +# Copy package.json and package-lock.json to the container +COPY package*.json ./ -FROM node:18-alpine AS builder -WORKDIR /app -COPY --from=deps /app/node_modules ./node_modules -COPY . . +# Install dependencies +RUN npm install -ENV NEXT_TELEMETRY_DISABLED 1 +# Copy the app source code to the container +COPY . . -RUN npm run build +# Build the Next.js app +RUN npm run build -FROM node:18-alpine AS runner -WORKDIR /app - -ENV NODE_ENV production -ENV NEXT_TELEMETRY_DISABLED 1 - -RUN addgroup --system --gid 1001 nodejs -RUN adduser --system --uid 1001 nextjs - -COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next -COPY --from=builder /app/node_modules ./node_modules -COPY --from=builder /app/package.json ./package.json - -USER nextjs - -EXPOSE 3000 - -ENV PORT 3000 +# Expose the port the app will run on +EXPOSE 3000 +# Start the app CMD ["npm", "start"] diff --git a/url-shortener/playbook/1_create_ec2.yaml b/url-shortener/playbook/1_create_ec2.yaml new file mode 100644 index 0000000..2eb9c41 --- /dev/null +++ b/url-shortener/playbook/1_create_ec2.yaml @@ -0,0 +1,79 @@ +--- +- + name: Create EC2 instances + hosts: localhost_group + connection: local + gather_facts: false + + # vars: + # region: us-east-1 + # image: ami-080e1f13689e07408 + # instance_type: t2.micro + # sec_group: "sg-0cb1234c398b2a4fe" + + vars_prompt: + - name: aws_profile + prompt: "Enter aws profile: " + private: False + + - name: region + prompt: "Enter region: " + private: False + default: us-east-1 + + - name: image + prompt: "Enter ami-id: " + private: False + default: ami-080e1f13689e07408 + + - name: instance_type + prompt: "Enter instance type: " + private: False + default: t2.micro + + - name: sec_group + prompt: "Enter security group id: " + private: False + #default: "sg-0cb1234c398b2a4fe" + + tasks: + - name: Create SG + amazon.aws.ec2_security_group: + name: "{{ sec_group }}" + description: "Test sg" + region: "{{ region }}" + aws_profile: "{{ aws_profile }}" + #aws_profile: test-ec2 + #aws_access_key: "{{ ec2_access_key }}" + #aws_secret_key: "{{ ec2_secret_key }}" + rules: + - proto: tcp + ports: + - 22 + cidr_ip: 0.0.0.0/0 + rule_desc: allow_ssh + + - proto: tcp + ports: + - 3000 + cidr_ip: 0.0.0.0/0 + rule_desc: frontend-exposed + + + - name: Create an instance + amazon.aws.ec2_instance: + key_name: ansible + aws_profile: "{{ aws_profile }}" + #aws_profile: test-ec2 + instance_type: "{{ instance_type }}" + region: "{{ region }}" + image_id: "{{ image }}" + security_group: "{{ sec_group }}" + network: + assign_public_ip: true + wait: true + exact_count: 1 + tags: + Ansible: urlShortenerServer + +... diff --git a/url-shortener/playbook/2_configure_ec2.yaml b/url-shortener/playbook/2_configure_ec2.yaml new file mode 100644 index 0000000..838388d --- /dev/null +++ b/url-shortener/playbook/2_configure_ec2.yaml @@ -0,0 +1,38 @@ +--- + +- name: Configure EC2 instance + hosts: ansible + connection: ssh + become: true + + tasks: + - name: Update the machine + command: sudo apt-get clean && update -y + + - name: Installing essentials + command: apt-get install apt-transport-https ca-certificates curl software-properties-common -y + + - name: Installing Curl + command: apt-get install curl -y + + - name: Pre-steps + command: "{{ item }}" + with_items: + - curl -fsSL https://get.docker.com -o get-docker.sh + - sudo sh get-docker.sh + + - name: Adding user to group + command: sudo usermod -aG docker ${USER} + + - name: Restarting docker service + command: sudo service docker restart + + - name: Updating the machine 2 + command: sudo apt-get update + + - name: Installing Python Pip + command: sudo apt-get install python-pip -y + + - name: Installing Docker compose + command: sudo apt-get install docker-compose-plugin +... diff --git a/url-shortener/playbook/3_docker_book.yaml b/url-shortener/playbook/3_docker_book.yaml new file mode 100644 index 0000000..0abaca2 --- /dev/null +++ b/url-shortener/playbook/3_docker_book.yaml @@ -0,0 +1,16 @@ +--- +- + name: Setting up and running docker compose + hosts: ansible + #connection: local + become: yes + #become_user: root + + vars_prompt: + - name: docker_input + prompt: "1) Setup Files\n2) Compose up\n3) Compose rm\n4) Copy .env files" + private: False + + roles: + - docker +... diff --git a/url-shortener/playbook/ansible.cfg b/url-shortener/playbook/ansible.cfg index b16ea3d..8767dc9 100644 --- a/url-shortener/playbook/ansible.cfg +++ b/url-shortener/playbook/ansible.cfg @@ -1,3 +1,7 @@ [defaults] -inventory = hosts +inventory = inventory/hosts host_key_checking = False +enable_plugins = aws_ec2 + +[inventory] +enable_plugins = aws_ec2 diff --git a/url-shortener/playbook/ansible.pem b/url-shortener/playbook/ansible.pem new file mode 100644 index 0000000..7ee8691 --- /dev/null +++ b/url-shortener/playbook/ansible.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAjBk5iof4j6ooAAnEAMa/UNEhvT8xEWgCOVUOsK3hhor+RGtD +FFpBvVGFsQnMgoxf0wy3lh4r9yL/FoUmIr8jKaXawMhHfBkl09KmvKbtJNbo4BjG +VvAJ/8wDtiESLV0eZ7+3LbUyb7DLTTTVUI/c2qOTkKf0xOmp2d6cZVeLyyw70vpk +rqJihmohKuHb6NYMY8Y5TvxYVa5JGHr//d1VtVZjORw2S5DLJUzRli2fF39iqcxy +GCx6Dp+6Skj9oXy/TrH0JJEzrqknfFDQRVnYHkw7rt9tRWRmjcOKqSNhIuNGysWG +4mkvbkuRgxEbe3Sn4GSj/T9meDF7xhKtVAmiwQIDAQABAoIBAD5pAkwnNGBzkawq +rYkXAKU09M3Ed8xkRBFcRdYJL91qz8iU6+YGKyD2TxNF5TgG5q0zDhmuyTEoAvD6 +UbnK4hQskV2uGsI6xbIuNh72UdBGeKlUAA88kYfFT6Xz55AT2nanXifbf2NoI7j6 +H0jrPZycoCEh4RVTblnjJFK1BE52dleudyExdGTdSXY2ZLOnfgVZFB/crrTzDHKo +AlJdA6WLXlnI8nSHUMte7EyFlyUk+pVaJiwaT96JOvK4sNWS/oTdrZDhONs58Gfr +AcXzbUBUpVcWRYa46rj0WlnYd9g4piz0h2z31e2gtJzJezpBkHSMfZPoZtjG3bsK +qWTrmh0CgYEAvsv+xrLj4GmFq5cGd3Vp9jhd6U9aZ/eQCkOayQxNFeVp01r5iU4a +hHiDU0047QfwoZfEcJaTFIF5idiOjxDeVoIeKWrHwtNsSNHYJGF9uHdI9gqc23dW +NJG82ozjWJSOl/fhsTMwvIhEPu7VzkHoQDWFEPafV3SvUBxRwg7cZsMCgYEAu/nZ +YtVOYPJ2vk8WAKwZ1LsQH06Rxfu8f4LyUnBR3EQQeqwvzx0H9IZCngGHEHmRuGmz +y9KQfyyzw2FpurfVZYeXwqLb2Td1jave6FUj1OknyZggrqUcYQVAA7eqtNp5I62O +RqD13pu3vIaoMLhlr2uOOgeUyMsBh8c/cAxkICsCgYEApRWap0/njaef6dzt0nS8 +LzeMQi/P2H9CmS7vgZ9sH1L1otNnb98tYItpja0WYTjqgtbvOD+AmVCB2byPdMgy +qIVDFbD3kOIgIk23B5t3DSHg9koueFIkO64RysP2fhFj46euFwVk2OvKqO07/4/V +JaFpNkk9i41Blz45m4ACQcMCgYEAqK5qTvBPJ1wtBlX9zpihjW5X52AgG+PsoRgm +z6ePLfO+Pfbp6BeLHfilKxj/1N7izvO7rjnEBPteTQPLVwzEaBmnK7ftRR0pVfpX +S3Grd9HKLHNhUJdFwiJt8WgNvcEXV9bUh7jFKf/ef0TvBNlUFniT5qsK7qyNMQSx +fqD/8IUCgYBFOTjmxqA9lZ1OGzfG4HMly2HmNgBWJ4YDpT74a048V74U7gRIuk/P +tesMxPbmXaRqIZJiRRE6j/5Sv5XSZaGnJaV/359cJ1hbWddki4VEGs5NtP8pJMvY +ERICQvJSNadOgeqqoouDPNe97F2k0NALVP7CNLQyOLN7RduImDA7nA== +-----END RSA PRIVATE KEY----- \ No newline at end of file diff --git a/url-shortener/playbook/dockerBook.yaml b/url-shortener/playbook/dockerBook.yaml deleted file mode 100644 index d5a92a4..0000000 --- a/url-shortener/playbook/dockerBook.yaml +++ /dev/null @@ -1,15 +0,0 @@ ---- -- - hosts: localhost_group - connection: local - become: yes - become_user: root - - vars_prompt: - - name: docker_input - prompt: "1) Setup Files\n2) Compose up\n3) Compose rm" - private: False - - roles: - - docker -... diff --git a/url-shortener/playbook/ec2_playbook.yaml b/url-shortener/playbook/ec2_playbook.yaml deleted file mode 100644 index 6473600..0000000 --- a/url-shortener/playbook/ec2_playbook.yaml +++ /dev/null @@ -1,45 +0,0 @@ ---- -- - hosts: localhost - connection: local - gather_facts: false - - tasks: - - name: Create a SG for SSH & HTTP access - amazon.aws.ec2_security_group: - name: ansible - description: Ansible SG - region: us-east-1 - vpc_id: vpc-0353f47c70b42ca4d - rules: - - proto: tcp - from_port: 80 - to_port: 80 - cidr_ip: 0.0.0.0/0 - - proto: tcp - from_port: 22 - to_port: 22 - cidr_ip: 0.0.0.0/0 - - - name: Provision an instance - amazon.aws.ec2_instance: - key_name: ansible - instance_type: t2.micro - image_id: ami-0c101f26f147fa7fd - region: us-east-1 - wait: true - exact_count: 1 - tags: - Ansible: AnsibleNginxWebServers - register: ec2 - - - name: Add all instance public IPs to host group - ansible.builtin.add_host: - hostname: "{{ item.public_ip_address }}" - groups: ansiblehosts - with_items: "{{ ec2.instances }}" - - - name: Show group - debug: - var: groups.ansiblehosts -... diff --git a/url-shortener/playbook/group_vars/ansible b/url-shortener/playbook/group_vars/ansible new file mode 100644 index 0000000..18cc05a --- /dev/null +++ b/url-shortener/playbook/group_vars/ansible @@ -0,0 +1,14 @@ +--- +ansible_ssh_private_key_file: ansible.pem +ansible_user: ubuntu +ansible_become: true +#ansible_ssh_common_args: '-o StrictHostKeyChecking=no' # not in prod +# docker_github_token: !vault | +# $ANSIBLE_VAULT;1.1;AES256 +# 61333462616136663461316334366130633061393365383763633936616233646334343833666437 +# 6465386231656632353038343631333464313533623133330a326230666663366236616337626238 +# 63376531323461303935616133653936306630383463666265386236313739633165396634323731 +# 3164643166613230630a363361363532323139353837353532663464386631383731353561316165 +# 65303061363465653963303662393432633563363736333863626365396532333236336634313031 +# 3365373731373437663830623434363332633532636662326231 +... diff --git a/url-shortener/playbook/group_vars/ansible.pem b/url-shortener/playbook/group_vars/ansible.pem new file mode 100644 index 0000000..7ee8691 --- /dev/null +++ b/url-shortener/playbook/group_vars/ansible.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAjBk5iof4j6ooAAnEAMa/UNEhvT8xEWgCOVUOsK3hhor+RGtD +FFpBvVGFsQnMgoxf0wy3lh4r9yL/FoUmIr8jKaXawMhHfBkl09KmvKbtJNbo4BjG +VvAJ/8wDtiESLV0eZ7+3LbUyb7DLTTTVUI/c2qOTkKf0xOmp2d6cZVeLyyw70vpk +rqJihmohKuHb6NYMY8Y5TvxYVa5JGHr//d1VtVZjORw2S5DLJUzRli2fF39iqcxy +GCx6Dp+6Skj9oXy/TrH0JJEzrqknfFDQRVnYHkw7rt9tRWRmjcOKqSNhIuNGysWG +4mkvbkuRgxEbe3Sn4GSj/T9meDF7xhKtVAmiwQIDAQABAoIBAD5pAkwnNGBzkawq +rYkXAKU09M3Ed8xkRBFcRdYJL91qz8iU6+YGKyD2TxNF5TgG5q0zDhmuyTEoAvD6 +UbnK4hQskV2uGsI6xbIuNh72UdBGeKlUAA88kYfFT6Xz55AT2nanXifbf2NoI7j6 +H0jrPZycoCEh4RVTblnjJFK1BE52dleudyExdGTdSXY2ZLOnfgVZFB/crrTzDHKo +AlJdA6WLXlnI8nSHUMte7EyFlyUk+pVaJiwaT96JOvK4sNWS/oTdrZDhONs58Gfr +AcXzbUBUpVcWRYa46rj0WlnYd9g4piz0h2z31e2gtJzJezpBkHSMfZPoZtjG3bsK +qWTrmh0CgYEAvsv+xrLj4GmFq5cGd3Vp9jhd6U9aZ/eQCkOayQxNFeVp01r5iU4a +hHiDU0047QfwoZfEcJaTFIF5idiOjxDeVoIeKWrHwtNsSNHYJGF9uHdI9gqc23dW +NJG82ozjWJSOl/fhsTMwvIhEPu7VzkHoQDWFEPafV3SvUBxRwg7cZsMCgYEAu/nZ +YtVOYPJ2vk8WAKwZ1LsQH06Rxfu8f4LyUnBR3EQQeqwvzx0H9IZCngGHEHmRuGmz +y9KQfyyzw2FpurfVZYeXwqLb2Td1jave6FUj1OknyZggrqUcYQVAA7eqtNp5I62O +RqD13pu3vIaoMLhlr2uOOgeUyMsBh8c/cAxkICsCgYEApRWap0/njaef6dzt0nS8 +LzeMQi/P2H9CmS7vgZ9sH1L1otNnb98tYItpja0WYTjqgtbvOD+AmVCB2byPdMgy +qIVDFbD3kOIgIk23B5t3DSHg9koueFIkO64RysP2fhFj46euFwVk2OvKqO07/4/V +JaFpNkk9i41Blz45m4ACQcMCgYEAqK5qTvBPJ1wtBlX9zpihjW5X52AgG+PsoRgm +z6ePLfO+Pfbp6BeLHfilKxj/1N7izvO7rjnEBPteTQPLVwzEaBmnK7ftRR0pVfpX +S3Grd9HKLHNhUJdFwiJt8WgNvcEXV9bUh7jFKf/ef0TvBNlUFniT5qsK7qyNMQSx +fqD/8IUCgYBFOTjmxqA9lZ1OGzfG4HMly2HmNgBWJ4YDpT74a048V74U7gRIuk/P +tesMxPbmXaRqIZJiRRE6j/5Sv5XSZaGnJaV/359cJ1hbWddki4VEGs5NtP8pJMvY +ERICQvJSNadOgeqqoouDPNe97F2k0NALVP7CNLQyOLN7RduImDA7nA== +-----END RSA PRIVATE KEY----- \ No newline at end of file diff --git a/url-shortener/playbook/group_vars/localhost_group.yml b/url-shortener/playbook/group_vars/localhost_group.yaml similarity index 100% rename from url-shortener/playbook/group_vars/localhost_group.yml rename to url-shortener/playbook/group_vars/localhost_group.yaml diff --git a/url-shortener/playbook/host_vars/localhost.yml b/url-shortener/playbook/host_vars/localhost.yaml similarity index 100% rename from url-shortener/playbook/host_vars/localhost.yml rename to url-shortener/playbook/host_vars/localhost.yaml diff --git a/url-shortener/playbook/inventory/aws_ec2.yaml b/url-shortener/playbook/inventory/aws_ec2.yaml new file mode 100644 index 0000000..53d1532 --- /dev/null +++ b/url-shortener/playbook/inventory/aws_ec2.yaml @@ -0,0 +1,8 @@ +plugin: aws_ec2 +profile: test-ec2 +hostnames: + - ip-address +regions: + - us-east-1 +groups: + ansible: "'urlShortenerServer' in tags.Ansible" \ No newline at end of file diff --git a/url-shortener/playbook/hosts b/url-shortener/playbook/inventory/hosts similarity index 100% rename from url-shortener/playbook/hosts rename to url-shortener/playbook/inventory/hosts diff --git a/url-shortener/playbook/roles/docker/tasks/copy_env.yaml b/url-shortener/playbook/roles/docker/tasks/copy_env.yaml new file mode 100644 index 0000000..79145f7 --- /dev/null +++ b/url-shortener/playbook/roles/docker/tasks/copy_env.yaml @@ -0,0 +1,34 @@ +--- +- name: Copying .env's + block: + - name: Copying frontend .env + ansible.builtin.copy: + src: frontend/.env + dest: url-shortener/url-shortener/frontend/ + mode: '0644' + register: frontend_copy + when: docker_input == "4" + + - name: Copying go server .env + ansible.builtin.copy: + src: go/.env + dest: url-shortener/url-shortener/servers/go/ + mode: '0644' + register: go_copy + when: docker_input == "4" + + - name: Copying express js .env + ansible.builtin.copy: + src: express_js/.env + dest: url-shortener/url-shortener/servers/express-js + mode: '0644' + register: express_copy + when: docker_input == "4" + +- name: Debug Output + ansible.builtin.debug: + var: + - frontend_copy + - go_copy + - express_copy +... diff --git a/url-shortener/playbook/roles/docker/tasks/docker_compose.yml b/url-shortener/playbook/roles/docker/tasks/docker_compose.yaml similarity index 100% rename from url-shortener/playbook/roles/docker/tasks/docker_compose.yml rename to url-shortener/playbook/roles/docker/tasks/docker_compose.yaml diff --git a/url-shortener/playbook/roles/docker/tasks/main.yaml b/url-shortener/playbook/roles/docker/tasks/main.yaml new file mode 100644 index 0000000..104c28f --- /dev/null +++ b/url-shortener/playbook/roles/docker/tasks/main.yaml @@ -0,0 +1,10 @@ +--- +- name: Copying the required files + ansible.builtin.import_tasks: setup_files.yaml + +- name: Running Docker Compose + ansible.builtin.import_tasks: docker_compose.yaml + +- name: Copying env files + ansible.builtin.import_tasks: copy_env.yaml +... diff --git a/url-shortener/playbook/roles/docker/tasks/main.yml b/url-shortener/playbook/roles/docker/tasks/main.yml deleted file mode 100644 index 13dc2b3..0000000 --- a/url-shortener/playbook/roles/docker/tasks/main.yml +++ /dev/null @@ -1,7 +0,0 @@ ---- -- name: Copying the required files - ansible.builtin.import_tasks: setup_files.yml - -- name: Running Docker Compose - ansible.builtin.import_tasks: docker_compose.yml -... diff --git a/url-shortener/playbook/roles/docker/tasks/setup_files.yml b/url-shortener/playbook/roles/docker/tasks/setup_files.yaml similarity index 68% rename from url-shortener/playbook/roles/docker/tasks/setup_files.yml rename to url-shortener/playbook/roles/docker/tasks/setup_files.yaml index 1117cf6..0d5a18f 100644 --- a/url-shortener/playbook/roles/docker/tasks/setup_files.yml +++ b/url-shortener/playbook/roles/docker/tasks/setup_files.yaml @@ -14,6 +14,16 @@ register: script_result when: docker_input == "1" + - name: Install pip + ansible.builtin.command: sudo apt-get install python3-pip -y + when: docker_input == "1" + changed_when: false + + - name: Install docker python + ansible.builtin.command: pip install docker==6.1.3 # only this works + when: docker_input == "1" + changed_when: false + - name: Install docker-compose python package ansible.builtin.pip: name: docker-compose diff --git a/url-shortener/playbook/roles/docker/vars/main.yaml b/url-shortener/playbook/roles/docker/vars/main.yaml new file mode 100644 index 0000000..8bb511b --- /dev/null +++ b/url-shortener/playbook/roles/docker/vars/main.yaml @@ -0,0 +1,12 @@ +--- +# vars file for docker +docker_script_dest: ./ +docker_github_token: !vault | + $ANSIBLE_VAULT;1.1;AES256 + 61333462616136663461316334366130633061393365383763633936616233646334343833666437 + 6465386231656632353038343631333464313533623133330a326230666663366236616337626238 + 63376531323461303935616133653936306630383463666265386236313739633165396634323731 + 3164643166613230630a363361363532323139353837353532663464386631383731353561316165 + 65303061363465653963303662393432633563363736333863626365396532333236336634313031 + 3365373731373437663830623434363332633532636662326231 +... diff --git a/url-shortener/playbook/roles/docker/vars/main.yml b/url-shortener/playbook/roles/docker/vars/main.yml deleted file mode 100644 index a6fd314..0000000 --- a/url-shortener/playbook/roles/docker/vars/main.yml +++ /dev/null @@ -1,12 +0,0 @@ ---- -# vars file for docker -docker_script_dest: /home/neo/Documents/projs/ansible-prac/dockerPractice/ -docker_github_token: !vault | - $ANSIBLE_VAULT;1.1;AES256 - 33366266353635346265376534613339663633386131666337363739633831353962633639383431 - 6333316538313163393337663462613032363766666464660a303838613531326164316432626366 - 36326132633365623734303566643838616261303436663264303731633231363737323438336336 - 3230386638623763350a346664343861396533363836663135633735316330383466343365386564 - 38646235613533656131343862313763653339313730313737616530373937323566326334353932 - 3866663537373337393535313432303061326236616432653633 -...