Skip to content

fix(nodejs) : Fixed nodejs deployer scripts #1161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions test/deploy/linux/docker/install/roles/prepare/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,39 @@

when: ansible_pkg_mgr == 'apt'
become: yes

########################################
# Amazon Linux 2023 support for Docker installation
########################################
- block:
# Update the system packages
- name: Update all system packages
dnf:
name: "*"
state: latest

# Install Docker
- name: Install Docker
dnf:
name: docker
state: present

# Start and enable Docker service
- name: Start and enable Docker service
systemd:
name: docker
state: started
enabled: yes

# Add the ec2-user to the docker group
- name: Add ec2-user to the docker group
user:
name: ec2-user
groups: docker
append: yes

- debug:
msg: "Docker installation completed successfully."

when: ansible_distribution == 'Amazon' and ansible_distribution_version == '2023'
become: yes
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
- debug:
msg: Install Node.js, Create Dockerfile, and setup Node.js application
# import the nodeJs sample application tasks
- import_tasks: ../../../../nodejs/redhat/roles/configure/tasks/main.yml

# create a sample Dockerfile
- name: Create a sample Dockerfile
copy:
content: |
FROM node:18
WORKDIR /app
COPY . /app
RUN npm install
EXPOSE 3000
CMD ["node", "app.js"]
dest: "/home/{{ ansible_user }}/myNodeApp/Dockerfile"
mode: "0644"
110 changes: 71 additions & 39 deletions test/deploy/linux/nodejs/redhat/roles/configure/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,53 +1,85 @@
---
- debug:
msg: Install Nodejs and docker
msg: Install Node.js and setup Node.js application

- name: update yum packages
shell: yum update -y
become: true
# Create myNodeApp directory
- name: Create myNodeApp directory
file:
path: "/home/{{ ansible_user }}/myNodeApp"
state: directory

- name: install docker
shell: yum install docker
become: true
# Create package.json
- name: Create package.json
copy:
content: |
{
"name": "myNodeApp",
"version": "1.0.0",
"description": "A sample Node.js application",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.17.1"
}
}
dest: "/home/{{ ansible_user }}/myNodeApp/package.json"
mode: "0644"

- name: RHEL - Start docker service
shell: "service docker start"
# Install Node.js using NodeSource
- name: Install Node.js using NodeSource
become: yes
shell: curl -sL https://rpm.nodesource.com/setup_18.x | bash -
args:
executable: /bin/bash

# - name: RHEL - Granting permissions to docker from ec2-user
# shell: "usermod -a -G docker ec2-user"
- name: Install Node.js
become: yes
yum:
name: nodejs
state: present

# - name: Install Node.js
# shell: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# become: true
- name: Verify Node.js installation
command: node -v
register: node_version
become_user: "{{ ansible_user }}"

# - name: load nvm
# shell: source ~/.bashrc
- name: Print Node.js version
debug:
msg: "Node.js version installed: {{ node_version.stdout }}"

# - name: install node.js 20.5.1
# shell: nvm install 20.5.1
# create a node app
- name: Create sample Node.js application
copy:
content: |
const http = require('http');

# - name: set node.js version 20.5.1 as default
# shell: nvm alias default 20.5.1
const hostname = '127.0.0.1';
const port = 3000;

# - name: Create node.js app directory
# file:
# path: /home/ec2-user/myNodeApp
# state: directory
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, Node.js!\n');
});

# - name: Copy files to myNodeApp directory
# synchronize:
# src: "{{ item }}"
# dest: /home/ec2-user/myNodeApp
# mode: push
# with_fileglob:
# - "../../../../templates/*"
# become: true
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
dest: "/home/{{ ansible_user }}/myNodeApp/app.js"
mode: "0644"

# - name: create package.json
# shell: cd /home/ec2-user/myNodeApp && npm init -y
# become: true

# - name: install express
# shell: cd /home/ec2-user/myNodeApp && npm install express
# become: true
# install npm dependencies
- name: Install npm dependencies
shell: npm install
args:
chdir: "/home/{{ ansible_user }}/myNodeApp"
become: yes
become_user: "{{ ansible_user }}"

# Give all permissions to the app.js file
- name: Change permissions for the app.js file
file:
path: "/home/{{ ansible_user }}/myNodeApp/app.js"
mode: "0755"
17 changes: 11 additions & 6 deletions test/manual/definitions/apm/node/node-js-docker.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@
"provider": "aws",
"type": "ec2",
"size": "t2.medium",
"ami_name": "amzn2-ami-hvm-2.0.????????.?-x86_64-gp2",
"ami_name": "al2023-ami-2023.6.20250128.0-kernel-6.1-x86_64",
"user_name": "ec2-user"
}
],
"services": [
{
"id": "docker",
"source_repository": "https://github.com/newrelic/open-install-library.git",
"deploy_script_path": "test/deploy/linux/docker/install/roles",
"port": 9999,
"destinations": ["node-js-docker"]
},
{
"id": "nodejs",
"source_repository": "https://github.com/newrelic/open-install-library.git",
"deploy_script_path": "test/deploy/linux/nodejs/redhat/roles",
"port": 8080,
"destinations": [
"node-js-docker"
]
"deploy_script_path": "test/deploy/linux/nodejs/docker-flow/roles",
"port": 80,
"destinations": ["node-js-docker"]
}
]
}
52 changes: 20 additions & 32 deletions test/manual/definitions/apm/node/node-linux2.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
{
"global_tags": {
"owning_team": "virtuoso",
"Environment": "development",
"Department": "product",
"Product": "virtuoso"
"owning_team": "virtuoso",
"Environment": "development",
"Department": "product",
"Product": "virtuoso"
},

"resources": [
{
"id": "nodelinux2",
"provider": "aws",
"type": "ec2",
"size": "t3.micro",
"ami_name": "newrelic-install-linux-node-pm2"
}
{
"id": "node-js",
"provider": "aws",
"type": "ec2",
"size": "t2.medium",
"ami_name": "al2023-ami-2023.6.20250128.0-kernel-6.1-x86_64",
"user_name": "ec2-user"
}
],

"services": [
{
"id": "nodetron",
"source_repository": "https://github.com/newrelic/demo-nodetron.git",
"deploy_script_path": "deploy/linux/pm2/roles",
"port": 5001,
"destinations": ["nodelinux2"],
"files": [
{
"destination_filepath": "engine/cronjob.json",
"content": [
{
"frequency": "* * * * *",
"job": "for ((i=1;i<=30;i++)); do curl '[service:nodetron:url]/'; curl '[service:nodetron:url]/api/inventory'; sleep 1; done"
}
]
}
]
}
{
"id": "nodejs",
"source_repository": "https://github.com/newrelic/open-install-library.git",
"deploy_script_path": "test/deploy/linux/nodejs/redhat/roles",
"port": 80,
"destinations": ["node-js"]
}
]
}
}
Loading