Skip to content

Commit 94c76fa

Browse files
committed
added custome rootfile support. adjusted for CRs
1 parent 4552f86 commit 94c76fa

File tree

10 files changed

+114
-60
lines changed

10 files changed

+114
-60
lines changed
File renamed without changes.

azure-pipeline/.taskkey

Lines changed: 0 additions & 1 deletion
This file was deleted.

azure-pipeline/example.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ pool:
77
steps:
88
- task: nginx-config-push@0
99
inputs:
10-
serviceConnectionName: '(sercive-connection-name)'
11-
resourceGroupName: '(resource-group-name)'
12-
subscriptionId: '(subscription-id)'
13-
deploymentName: '(deployment-name)'
14-
sourceConfigFolderPath: '(config-folder-file-path)'
15-
targetConfigFolderPath: '/etc/nginx/'
16-
10+
serviceConnectionName: '(Enter the name of the service connection to Azure)'
11+
resourceGroupName: '(Enter the name of the Azure resource group of the deployment)'
12+
subscriptionId: '(Enter the Azure subscription ID of the deployment)'
13+
deploymentName: '(Enter the name for this deployment)'
14+
configDirectoryInRepo: '(Enter the relative path to the Nginx configuration directory in the repository)'
15+
configDirectoryInDeployment: '(Enter the target path for the Nginx configuration directory in the deployment environment, e.g., /etc/nginx)'
16+
rootConfigFileName: '(Enter the name of the root configuration file and make sure it is in the config directory. e.g., nginx.conf)'

azure-pipeline/readme.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ pool:
3535
steps:
3636
- task: nginx-config-push@0
3737
inputs:
38-
serviceConnectionName: '(sercive-connection-name)'
39-
resourceGroupName: '(resource-group-name)'
40-
subscriptionId: '(subscription-id)'
41-
deploymentName: '(deployment-name)'
42-
sourceConfigFolderPath: '(config-folder-file-path)'
43-
targetConfigFolderPath: '/etc/nginx/'
38+
serviceConnectionName: '(Enter the name of the service connection to Azure)'
39+
resourceGroupName: '(Enter the name of the Azure resource group of the deployment)'
40+
subscriptionId: '(Enter the Azure subscription ID of the deployment)'
41+
deploymentName: '(Enter the name for this deployment)'
42+
configDirectoryInRepo: '(Enter the relative path to the Nginx configuration directory in the repository)'
43+
configDirectoryInDeployment: '(Enter the target path for the Nginx configuration directory in the deployment environment, e.g., /etc/nginx/)'
44+
rootConfigFileName: '(Enter the name of the root configuration file and make sure it is in the config directory. e.g., nginx.conf)'
4445

4546
```
4647

@@ -49,7 +50,7 @@ What is needed right now is to get values of the needed variables with following
4950

5051
##### 1. Get your NGINX configuration file ready in the repository.
5152

52-
Upload your NGINX configuration files onto any folder of your Azure DevOps repository. Then update the ‘sourceConfigFolderPath’ to the path of the configuration folder in your repository. Note that the name of the main file of the NGINX configuration folder should be “nginx.conf”, and the ‘targetConfigFolderPath’ would usually be /etc/nginx/.
53+
Upload your NGINX configuration files onto any folder of your Azure DevOps repository. Then update the ‘configDirectoryInRepo’ to the path of the configuration folder in your repository.
5354

5455
![Image](images/readme-guidline-01.png)
5556

azure-pipeline/src/index.ts

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,36 @@ import {
1313
import {
1414
validatedEnvVar,
1515
validatedAuthParams,
16-
} from './utils/common';
16+
} from './utils/validation';
1717
import {
1818
ClientSecretCredential,
1919
AuthenticationError,
2020
} from '@azure/identity';
2121
import { info } from 'console';
2222

23-
23+
/**
24+
* Class ConfigUpdater is responsible for updating the Nginx configuration.
25+
* It provides methods to authenticate, compress, and upload user-specific configurations
26+
* using Azure identity and other utilities.
27+
*/
2428
class ConfigUpdater {
2529

2630
private f = new FileHandler;
2731

28-
// getAuthorizationTokenFromKey():
29-
// a function takes user's input given in the .yml file
30-
// and request an bearer token for authentication of later authorization
31-
// using @azure/identity
32-
private getAuthorizationTokenFromKey = async () => {
32+
/**
33+
* Retrieves the authorization token for later authentication by using Azure identity.
34+
* @returns {Promise<any>} The authorization token.
35+
* @throws {Error} If the authorization parameters validation fails.
36+
* @throws {AuthenticationError} If the access token fetch fails.
37+
*/
38+
private getAuthorizationTokenFromKey = async (
39+
tenantID: string,
40+
clientID: string,
41+
servicePrincipalKey: string
42+
) => {
3343
try {
34-
const params = {
35-
tenantID: validatedAuthParams('tenantid'),
36-
clientID: validatedAuthParams('servicePrincipalId'),
37-
servicePrincipalKey: validatedAuthParams('servicePrincipalKey'),
38-
}
3944
const clientSecretCredential: ClientSecretCredential = new ClientSecretCredential(
40-
params.tenantID, params.clientID, params.servicePrincipalKey
45+
tenantID, clientID, servicePrincipalKey
4146
);
4247
const accessToken = await clientSecretCredential.getToken(SCOPE);
4348
if (!accessToken) {
@@ -51,9 +56,10 @@ class ConfigUpdater {
5156
}
5257
}
5358

54-
// getConvertedFileObject():
55-
// compress config file folder
56-
// and make it ready for sending it to backend
59+
/**
60+
* Compresses the configuration file folder and prepares it for sending to the backend.
61+
* @returns {Promise<Object>} An object containing properties related to the compressed file.
62+
*/
5763
private getConvertedFileObject = async () => {
5864
const file = await this.f.compressFile(
5965
validatedEnvVar(INPUT.source),
@@ -62,26 +68,35 @@ class ConfigUpdater {
6268
);
6369
return {
6470
properties: {
65-
rootFile: `${validatedEnvVar(INPUT.target)}nginx.conf`,
71+
rootFile: `${validatedEnvVar(INPUT.target)}/${validatedEnvVar(INPUT.rootFile)}`.replace(/\/\/+/g, '/'),
6672
package: {
6773
data: this.f.convertFileToBase64String(file),
6874
}
6975
}
7076
};
7177
}
7278

73-
// getRequestConfig():
74-
// make up the bearer token from user's input
79+
/**
80+
* Constructs the request configuration, including the bearer token from the user's input.
81+
* @returns {Promise<AxiosRequestConfig>} The request configuration.
82+
*/
7583
private getRequestConfig = async () => {
76-
const token = await this.getAuthorizationTokenFromKey();
84+
const token = await this.getAuthorizationTokenFromKey(
85+
validatedAuthParams('tenantid'),
86+
validatedAuthParams('servicePrincipalId'),
87+
validatedAuthParams('servicePrincipalKey'),
88+
);
7789
const config: AxiosRequestConfig = { headers: {
7890
Authorization: `Bearer ${token.token}`,
7991
}}
8092
return config;
8193
}
8294

83-
// getRequestResource():
84-
// make up all needed parameters for the uploading api request
95+
/**
96+
* Constructs the required parameters for the uploading API request.
97+
* @returns {Object} An object containing the required parameters for the request,
98+
* including subscription ID, resource group name, deployment name, and API version.
99+
*/
85100
private getRequestResource = () => {
86101
return {
87102
subscriptionId: validatedEnvVar(INPUT.subscription),
@@ -91,8 +106,10 @@ class ConfigUpdater {
91106
}
92107
}
93108

94-
// updateNginxConfig():
95-
// main function to compress file, get authentication info, and call the api
109+
/**
110+
* Main function to compress the file, retrieve authentication info, and call the API to update the Nginx configuration.
111+
* @throws {Error} If the Nginx configuration uploading fails.
112+
*/
96113
updateNginxConfig = async () => {
97114
console.log('updateNginxConfig...')
98115
try {

azure-pipeline/src/task.json

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"id": "4b016be0-afe8-4f76-ace3-d7f0355d9605",
44
"name": "nginx-for-azure-config-sync",
55
"friendlyName": "nginxconfigpush",
6-
"description": "this task is designed for pushing nginx configs from azrue devops repository to azure nginx deployments",
7-
"helpMarkDown": "",
6+
"description": "This task is designed to synchronize Nginx configurations from an Azure DevOps repository or GitHub repository to NGINXaaS for Azure deployments.",
7+
"helpMarkDown": "Use this task to push Nginx configuration files from your repository to your NGINXaaS for Azure deployments",
88
"category": "Utility",
99
"author": "zaotest",
1010
"version": {
@@ -21,48 +21,56 @@
2121
],
2222
"type": "connectedService:AzureRM",
2323
"label": "Azure Service Connection Name",
24-
"helpMarkDown": "serviceConnectionName",
24+
"helpMarkDown": "Specify the Azure Service Connection to use for authentication.",
2525
"required": true
2626
},
2727
{
2828
"name": "resourceGroupName",
2929
"type": "string",
30-
"label": "resourceGroupName",
30+
"label": "Azure Resource Group Name",
3131
"defaultValue": "",
3232
"required": true,
33-
"helpMarkDown": "resourceGroupName"
33+
"helpMarkDown": "Enter the name of the Azure Resource Group where the Nginx deployment resides."
3434
},
3535
{
3636
"name": "subscriptionId",
3737
"type": "string",
38-
"label": "subscriptionId",
38+
"label": "Azure Subscription ID",
3939
"defaultValue": "",
4040
"required": true,
41-
"helpMarkDown": "subscriptionId"
41+
"helpMarkDown": "Enter the ID of the Azure subscription for the deployment."
4242
},
4343
{
4444
"name": "deploymentName",
4545
"type": "string",
46-
"label": "deploymentName",
46+
"label": "Deployment Name",
4747
"defaultValue": "",
4848
"required": true,
49-
"helpMarkDown": "deploymentName"
49+
"helpMarkDown": "Enter a name for the deployment that will receive the Nginx configuration."
5050
},
5151
{
52-
"name": "sourceConfigFolderPath",
52+
"name": "configDirectoryInRepo",
5353
"type": "string",
54-
"label": "sourceConfigFolderPath",
54+
"label": "Configuration Directory in Repository",
5555
"defaultValue": "",
5656
"required": true,
57-
"helpMarkDown": "a content type to append to all keys in the configuration file"
57+
"helpMarkDown": "Enter the relative path to the Nginx configuration directory in the repository."
5858
},
5959
{
60-
"name": "targetConfigFolderPath",
60+
"name": "configDirectoryInDeployment",
6161
"type": "string",
62-
"label": "targetConfigFolderPath Type",
63-
"defaultValue": "",
62+
"label": "Configuration Directory in Deployment",
63+
"defaultValue": "/etc/nginx",
64+
"required": true,
65+
"helpMarkDown": "Enter the target path for the Nginx configuration directory in the deployment environment."
66+
},
67+
{
68+
"name": "rootConfigFileName",
69+
"type": "string",
70+
"label": "Root Configuration File Name",
71+
"defaultValue": "nginx.conf",
6472
"required": true,
65-
"helpMarkDown": "targetConfigFolderPath"
73+
"helpMarkDown": "Enter the name of the root configuration file, such as 'nginx.conf', within the configuration directory."
6674
}
6775
],
6876
"execution": {

azure-pipeline/src/utils/constants.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export const OUTPUT_PATH: string = './uploading-file.tar.gz';
77

88
export const INPUT = {
99
connection: 'AZURESUBSCRIPTIONENDPOINT',
10-
source: 'SOURCECONFIGFOLDERPATH',
11-
target: 'TARGETCONFIGFOLDERPATH',
12-
subscription: 'SUBSCRIPTIONID',
1310
resource: 'RESOURCEGROUPNAME',
11+
subscription: 'SUBSCRIPTIONID',
1412
deployment: 'DEPLOYMENTNAME',
15-
13+
source: 'CONFIGDIRECTORYINREPO',
14+
target: 'CONFIGDIRECTORYINDEPLOYMENT',
15+
rootFile: 'ROOTCONFIGFILENAME',
1616
}

azure-pipeline/src/utils/file-handler.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import tar from 'tar';
2+
import fs from 'fs';
23

4+
/**
5+
* Class FileHandler provides utility functions to handle file operations,
6+
* such as compressing a file and converting it to a Base64 string.
7+
*/
38
export class FileHandler {
49

10+
/**
11+
* Compresses a file at the given source path using gzip and tar, and writes the compressed file to the specified output path.
12+
* @param sourcePath {string} The path of the source file to compress.
13+
* @param prefix {string} The prefix to use in the compressed file name.
14+
* @param outputPath {string} The path where the compressed file will be saved.
15+
* @returns {Promise<string>} The output path of the compressed file.
16+
* @throws {Error} If an error occurs during compression.
17+
*/
518
public compressFile = async (
619
sourcePath: string,
720
prefix: string,
@@ -25,8 +38,12 @@ export class FileHandler {
2538

2639
}
2740

41+
/**
42+
* Reads the file at the given path and converts its content to a Base64 string.
43+
* @param file {string} The path of the file to read.
44+
* @returns {string} The content of the file as a Base64 string.
45+
*/
2846
public convertFileToBase64String = (file: string) => {
29-
const fs = require('fs')
3047
const file_buffer = fs.readFileSync(file);
3148
return file_buffer.toString('base64');
3249
}

azure-pipeline/src/utils/http.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,19 @@ const headers = {
99

1010
const baseURL = 'https://management.azure.com';
1111

12+
/**
13+
* Class SimplifiedHttp provides a simplified interface for HTTP requests.
14+
* It encapsulates common configurations and error handling for making HTTP PUT requests.
15+
*/
1216
class SimplifiedHttp {
17+
/**
18+
* Makes an HTTP PUT request to the specified endpoint with the given data and configuration.
19+
* @param endpoint {string} The URL of the endpoint to send the request to.
20+
* @param data {T} Optional data to be sent in the request body.
21+
* @param config {AxiosRequestConfig} Optional configuration object for the request.
22+
* @returns {Promise<R>} A promise that resolves to the response of the request.
23+
* @throws {any} If an error occurs during the request.
24+
*/
1325
put<T = any, R = AxiosResponse<T>>(endpoint: string, data?: T, config?: AxiosRequestConfig): Promise<R> {
1426
const finalConfig = {
1527
...config,

0 commit comments

Comments
 (0)