start Directus with Pm2 as Service , PM2 Error C:\PROGRAM ILES\NODEJS\NPM.CMD:1| :: Created by npm, please don’t edit manually.| ^start Directus with Pm2 as Service , How to start directus using pm2?Error


It may help others to run directus as background service.

npm install pm2 -g

Make it as environment variable

https://dev.to/gortron/environment-variables-with-pm2-the-right-way-42ij

create service.js ins the same folder of directus package.json and add below.

const exec = require(‘child_process’).exec;

const path = require(‘path’);

const client = exec(‘npm run start’,

{ windowsHide: true, cwd: path.join(__dirname, ‘./’), });

client.stdout.pipe(process.stdout); client.stderr.pipe(process.stderr);

pm2 start service.js

npx pm2 start service.js -f

If your pm2 process stop on logof use below command.

npx pm2 update

Offline DevopServer – Create Offline Nuget feed – nuget.exe


Recently I got a chance to configure Devops on premise server. Although installation was not quite difficult but migration form on-premise TFS to Devops was quite challenging.

Creating Build or release pipeline is easy in Azure Dveops but your challenge start if it is on-premise and there is no internet access. (security huh). To successfully build the pipeline with Nuget feed, make sure that nuget.config should

  1. Self hosted Agents
  2. Challenge is to Build Self hosted Agents (compute Power) to build our pipeline. Some tips which may help oters.
  3. Create Self hosted agents. https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/windows-agent?view=azure-devops
  4. Deployment Group : it help when there is no internet on the Devops or org wants their on-premise server or server setup in VMs.
  5. Setting up deployment group is quite simple, below is the link. Just run this power shell command on the server. Normally server should have access or run this deployment group on the same sever where you want to deployed the application.
  6. Link : https://learn.microsoft.com/en-us/azure/devops/pipelines/release/deployment-groups/deploying-azure-vms-deployment-groups?view=azure-devops

Artifact Feed

As we dont have acces to nuget.org so our pipeline to restore nuget will fail. So to make all nugets available offline we need to feed to our artifact feed so that pipline will source packages from there.

Add a nuget.config file to your project, in the same folder as your .csproj or .sln file

download nuget.exe in the same folder of solution file. open package manager console and write below commands.

Commands

nuget sources update -ValidAuthenticationTypes basic -Name myfeeed -UserName “af000” -Password yourPAT

nuget.exe push -Source “myfeed” -ApiKey az packages\refit.7.0.0.nupkg

If you found any error for conflict like below, then you need to remove your upstream. because in our case nuget.org was open thorough firewall rule. so when i try to upload above package i got conflict error form upstream, because mydevops is able to connect to nuget.org. if you got conflict with upstream, then remove the upstream from below screen shot.

Once you add all the dependent packages update your pipline to Feed to use option.

Another way we can also feed package to the project to build by placing packages in the folder in the root directory and use below nueget.config file. By doing below devops will look in to the below package folder to build.

<?xml version=”1.0″ encoding=”utf-8″?>

<configuration>

    <packageSources>

        <add key=”nuget.org” value=”.\packages” />

        <add key=”Azure.Core” value=”.\packages” />

    </packageSources>

    <config>  

        <add key=”globalPackagesFolder” value=”.\packages” />

        <add key=”defaultPushSource” value=”.\packages” />

    </config>

    <activePackageSource>

        <add key=”All” value=”.\packages” />

    </activePackageSource>

    <config>

        <add key=”repositoryPath” value=”$\..\packages” />

    </config>

</configuration>

Auto Fill – Google form – Submit using NodeJs – BOT –


In this blog we will use simple package puppeteer in node js to submit google form. To the point I am pasting the script which works async and sumit 100 Google form in loop.

  1. Create Google Form btw It can be any form
  2. Add Questions in the google form
  3. view the Google form in view state and get the ID or answer as mentioned below

And use below script to click on this label and click submit.

const puppeteer = require(‘puppeteer’);

const timeout = ms => new Promise(resolve => setTimeout(resolve, ms));

async function recursion() {

  for (var x=1;x<10;x++)

  {

    await myfunc();

  }

}

async function myfunc ()  {

  const browser = await puppeteer.launch({

    headless: false

});  const page = await browser.newPage();

//replace XXX with your form Id

  await page.goto(‘https://docs.google.com/forms/d/e/XXXXX/viewform&#8217;);

  await page.click(‘#i5’, ‘Yes’);

  await page.waitForTimeout(500); // wait for 5 seconds

 await page.click(‘.uArJ5e’);

  await browser.close();

};

recursion();

Featured

My Azure Diaries. (App Service Series)


1 . App Service uses federated identity, in which a third-party identity provider manages the user identities and authentication flow for you.

Microsoft Identity Platform/.auth/login/aadApp Service Microsoft Identity Platform login
Facebook/.auth/login/facebookApp Service Facebook login
Google/.auth/login/googleApp Service Google login
Twitter/.auth/login/twitterApp Service Twitter login
Any OpenID Connect provider/.auth/login/<providerName>App Service OpenID Connect login

The authentication and authorization module runs in the same sandbox as your application code. When it’s enabled, every incoming HTTP request passes through it before being handled by your application code. This module handles several things for your app:

  • Authenticates users with the specified provider
  • Validates, stores, and refreshes tokens
  • Manages the authenticated session
  • Injects identity information into request headers

 Note

In Linux and containers the authentication and authorization module runs in a separate container, isolated from your application code. Because it does not run in-process, no direct integration with specific language frameworks is possible.

For authenticated requests, App Service also passes along authentication information in the HTTP headers.This option provides more flexibility in handling anonymous requests. It lets you present multiple sign-in providers to your users.

to be continued……

Configure application settings

app settings are variables passed as environment variables to the application code. For Linux apps and custom containers, 

f you are using deployment slots you can specify if your setting is swappable or not. In the dialog, you can stick the setting to the current slot.

In a default, or custom, Linux container any nested JSON key structure in the app setting name like ApplicationInsights:InstrumentationKey needs to be configured in App Service as ApplicationInsights__InstrumentationKey for the key name. In other words, any : should be replaced by __ (double underscore).

Map a custom domain to an App Service app using CLI

Sign in to Azure

subscription=”” # add subscription here

az account set -s $subscription # …or use ‘az login’

Create WebApp

Map a custom domain to an App Service app

set -e # exit if error

Variable block

let “randomIdentifier=$RANDOM*$RANDOM”
location=”East US”
resourceGroup=”msdocs-app-service-rg-$randomIdentifier”
tag=”configure-custom-domain-webapp-only.sh”
appServicePlan=”msdocs-app-service-plan-$randomIdentifier”
webapp=”msdocs-web-app-$randomIdentifier”

Create a resource group.

echo “Creating $resourceGroup in “$location”…”
az group create –name $resourceGroup –location “$location” –tag $tag

Create an App Service plan in SHARED tier (minimum required by custom domains).

echo “Creating $appServicePlan”
az appservice plan create –name $appServicePlan –resource-group $resourceGroup –sku SHARED

Create a web app.

echo “Creating $webapp”
az webapp create –name $webapp –resource-group $resourceGroup –plan $appServicePlan

Copy the result of the following command into a browser to see the static HTML site.

site=”http://$webapp.azurewebsites.net&#8221;
echo $site
curl “$site”

fqdn=<Replace with www.{yourdomain}>

az webapp config hostname add –webapp-name $webappname –resource-group myResourceGroup –hostname $fqdn echo “You can now browse to http://$fqdn&#8221;

Data Engineering

The difference between GRS and RA GRS is fairly simple, GRS only allows to be read in the secondary zone in the even of a failover from the primary to secondary while RA GRS allows the option to read in the secondary whenever.

Azure Cli-Create WebApp with Git Repository – Azure Cli Command


We can use below command to Create web app with git repository

$gitrepo=https://github.com/repo webappname=mywebappname001 #create a resource group az group create — – location westeurop – – name myResourcegroupname #create an app service plan in Free Tier az appservice plan create – – name $webappname -resource-group myResourcegroupname -sku Free #create a webapp az webapp create — – name $webappname -resource-group myResourcegroupname – – plan $webappname #deploy a code from git repositotry az webapp deployment source config – – name -resource-group myResourcegroupname – – repo-url $gitrepo -branch master – – manual-integration

Using SSL in Node JS – CSR to PFX using Open SSL


In this blog I will walk through how we can deploy SSL for node Js using Open SSL Command. In my case I had to deploy nodejs api on Windows server, I guess for Linux it would be more easier.

  1. First we need to create a CSR using Open SSL . Link to Generate CSR – https://www.ssl.com/how-to/manually-generate-a-certificate-signing-request-csr-using-openssl/
  2. You provide this CSR to SSL issuing body, in my case It was Digicert. Keep the .key file with you, we will use it once the SSL isued.
  3. Once SSL issued to you, you need to download below files.

Crate a folder and download below commands form Digicert Email, which you receive for SSL.

certificatename.pem

Digicert TLS RSA…..pem file

also paste the key file in the above same folder.

Use below command to generate pfx file , which we can use in the node js application.

openssl pkcs12 -export -out certificate.pfx -inkey 1_.key -in 2_.pem -certfile 3_.pem

Replace 1,2,3 in above with pem and key.

It will ask for passphrase which we will give nodejs to validate pfx file.

below is the code to create https request on node js,we will use the above pfx file generated by open ssl.

const optionsY = {
pfx: fs.readFileSync(‘certificate.pfx’),
passphrase: ‘your pass pharase which we use in above commad’
};
var port=443;
https.createServer(optionsY, app).listen(port, () => {
console.log(‘Listening…’);
})

Azure Cosmos DB account that would need to use the Table API


I am noting all my back end learning so that It may help others and me in future. I am writing the below command to create cosmos db account.

creating vriables

$accountname=”abdulaziztest”

$resourcegroupname=”abdulaziztest-rg”

$location=”westus”

$tableName=”table1″

az login

#Crate a Cosmos account for Table API

az cosmosdb create – n $accountname -g $resourcegroupname –capabilities EableTable –default-consitency-level Eventual

–locations regionName =’West US 2′ failoverPriority=0 isZoneRedundant=False

–location regionName=”East US 2′ failoverPriority=1 isZoneRedundant =False

#Create a Table API for Table

az cosmosdb table create -a $ $accountname

-g $resourcegroupname

-n $tableName=”table1″

–throughput 400

running nodejs app on windows node-windws


Dealing with deploying nodejs application on the windows server we found that linux is much better option with vast and easy capabilities.

With all of other option below are the script which create windows service and run node js app on as server on windows.

Below are the script which create windows services and point out the script to run without any delay.

 

–Create windows Service

var Service = require(‘node-windows’).Service;
// Create a new service object
var svc = new Service({
name:’Hello World’,
description: ‘The nodejs.org example web server.’,
script: ‘D:\\mysolution\\src\\index.js’
});

// Listen for the “install” event, which indicates the
// process is available as a service.

svc.on(‘install’,function(){
svc.start();
});

svc.install();

 

—————— Below snippet is to remove the service.

 

var Service = require(‘node-windows’).Service;

// Create a new service object
var svc = new Service({
name:’Hello World’,
script: require(‘path’).join(__dirname,’src\\index.js’)
});

// Listen for the “uninstall” event so we know when it’s done.
svc.on(‘uninstall’,function(){
console.log(‘Uninstall complete.’);
console.log(‘The service exists: ‘,svc.exists);
});

// Uninstall the service.
svc.uninstall();

 

In the end , my personal opinion is linux is best option when dealing with nodej applications.

 

 

 

#sharingiscaring #helpmeinfuture #offcourse

 

Reference :https://github.com/coreybutler/node-windows