@lbdudc/gp-code-uploader
v1.1.2
Published
Code Uploader is a flexible library that simplifies uploading code to a server and executing it via docker-compose. This module offers various code distribution options for SSH-based servers, AWS instances, and local installations.
Downloads
5
Readme
Code Uploader
Description
The Code Uploader is a versatile library designed to simplify the process of uploading code to a server and executing it via docker-compose. This library provides flexible strategies to facilitate code deployment across different environments, such as SSH-based servers, AWS instances, and local setups.
Installation
Install the package via npm:
npm install @lbdudc/gp-code-uploader
Pre-requisites
Have installed in your machine:
Must be a root user or have sudo privileges in the server where you want to upload the code
Known Issues :warning:
In AWS instances, the docker-compose up command can fail with the basic EC2 free tier instance. This is because the instance does not have enough memory to run the docker-compose up command. To solve this, you can use a bigger instance
Example Usages
More examples can be found in the ./examples
folder.
SSH (Debian/Ubuntu)
SSH Usage Example
import { Uploader, DebianUploadStrategy } from '@lbdudc/gp-code-uploader';
const uploader = new Uploader();
uploader.setUploadStrategy(new DebianUploadStrategy());
const config = {
host: '127.0.0.1',
port: 22,
username: 'root',
certRoute: '/home/certs/id_rsa', // Optional but recommended
repoPath: 'code/lps/output',
remoteRepoPath: '/home/username/code',
forceBuild: true, // default false
};
// Upload code, configure the instance and run docker-compose up
await uploader.uploadCode(config);
AWS
import { Uploader, AWSUploadStrategy } from '@lbdudc/gp-code-uploader';
// Create the uploader
const uploader = new Uploader();
// Set the upload strategy
uploader.setUploadStrategy(new AWSUploadStrategy());
// Create AWS instance
const hostIp = await uploader.createInstance({
AWS_SECRET_ACCESS_KEY: '',
AWS_REGION: 'eu-west-2',
AWS_AMI_ID: 'ami-08b064b1296caf3b2',
AWS_INSTANCE_TYPE: 't2.micro',
AWS_INSTANCE_NAME: 'my-aws-instance',
AWS_SECURITY_GROUP_ID: 'sg-xxxxxxxxxxxxxxxxx',
AWS_KEY_NAME: 'my-key-pair',
AWS_USERNAME: 'ec2-user',
AWS_SSH_PRIVATE_KEY_PATH:'./my-key-pair.pem',
AWS_ACCESS_KEY_ID: '',
});
// Upload code by SCP, configure instance, and run docker-compose up
await uploader.uploadCode({
host: hostIp,
username: 'ec2-user',
certRoute: './my-key-pair.pem',
awsRegion: 'eu-west-2',
repoPath: '../code',
remoteRepoPath: `/home/ec2-user/code`,
// forceBuild: true,
});
Methods
uploader.js
exposes the following methods:
uploadCode(options): Promise<UploadCodeResponse>
: Uploads the code, configures the instance and runs docker-compose up
AWS Instance Pre-requisites
You need to create an AWS instance with the following:
- An SSH key pair guide
- A security group with the following inbound rules: guide
- SSH (port 22) from your IP (or the IP of the server you want to access the instance from)
- HTTP (port 80) from your IP (or the IP of the server you want to access the instance from)
- A security group with the following outboud rules: guide
- All traffic (all ports) to your IP (or the IP of the server you want to access the instance from)
- An IAM role with the following permissions: guide
- AmazonEC2FullAccess
- Get your AWS access key and secret key: guide
Dependencies
- @aws-sdk/client-ec2: ^3.32.0
- dotenv: ^16.0.3
- jszip: ^3.10.1
Dev Dependencies
- @vitest/coverage-istanbul: ^0.32.2
- vitest: ^0.32.0
Author
Victor Lamas Email: [email protected]
License
This project is licensed under the MIT License - see the LICENSE.md file for details