set Environment variable for OPC publisher - azure

i have an OPC-publisher module which i want to deploy as an iot edge module for this purpose i need to give connectionString. in the documentation given on github they have mentioned some environment variables which can be set for this purpose as follows:
There are a couple of environment variables which can be used to control the application:
_HUB_CS: sets the IoTHub owner connectionstring
_GW_LOGP: sets the filename of the log file to use
_TPC_SP: sets the path to store certificates of trusted stations
_GW_PNFP: sets the filename of the publishing configuration file
i want to know where to set them in a code, on azure portal where they provide an option for setting env variables, from command line, or in dockerfile.
any help will be appriciated.

Did you take a look at this? It explains in detail how to run the OPC publisher as an Edge module. Just copying here for completeness:
{
"Hostname": "pub-test",
"Cmd": [
"publisher",
"--pf=./pn.json",
"--di=60",
"--to",
"--aa",
"--si=0",
"--ms=0"
],
"HostConfig": {
"PortBindings": {
"62222/tcp": [{
"HostPort": "62222"
}]
},
"Binds": [
"x509certstores:/root/.dotnet/corefx/cryptography/x509stores",
"d:/iiotedge:/appdata"
],
"ExtraHosts": [
"localhost:127.0.0.1",
"opctestsvr:192.168.178.26"
]
}
}

Related

Default Azure Function app base path D:\home\site\wwwroot Azure Function 2.x

Currently, by default, Azure function base path is set to "D:\home\site\wwwroot". For example, when publishing, VS uploads app to this folder.
I need to read config file from this folder. We have problem of ExecutionContext is null via dependency injection via constructor
Setting a new environment variable might cause issue if the path is changed in the future.
My question is that how can I use app base path that is reliable and stable, that works with DI via constructor.
Azure Function 2.x
VS 2017
you can use function.json to have your configuration key pairs.
for example:
System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
and in function.json you can do like this:
"mykey": "myvalue"
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.24",
"configurationSource": "attributes",
"bindings": [
{
"direction":"in",
"type": "timerTrigger",
"useMonitor": true,
"runOnStartup": false,
"name": "myTimer",
"mykey": "myvalue"
}
],
"disabled": false,
"scriptFile": "../bin/**.dll",
"entryPoint": "**.Run"
}
There is an environment variable pointing to home directory. This would not change as many services including function app take dependency on it. Below is how function runtime fetches it in azure environment.
string home = GetEnvironmentVariable("HOME");
Path = System.IO.Path.Combine(home, "site", "wwwroot");

Deploy Azure Function dynamically enabled/disabled using ARM template

I have a class library, timer-based Azure Function that is deployed using an ARM template. Everything works fine except I would like a slightly different behavior based on the target environment. When deploying to a test environment I would like the function to be initially disabled but in production it should always be enabled. Is this possible?
My current workaround is to have an app setting that tells the function to immediately exit when set to a specific value. However, this seems like a poor solution, especially since the timer-triggered function is executed quite frequently. To solve this I manually disables the function using the following switch in the Azure portal:
Is there perhaps possible to specify the desired state of this switch from the ARM template?
Seems you don't need to set to a specific value in the app settings, azure function has a built-in property.
Try to use the setting in the template snippet below to disable the function, it should work.
"siteConfig": {
"appSettings": [
{
"name": "AzureWebJobs.MessageQueueMonitorFunction.Disabled",
"value": "true"
}
]
}
Expanding on Joy's answer, which worked like a charm!
For the benefit of others, the "name" property above is composed like so:
AzureWebJobs.<YouFunctionName>.Disabled
where <YouFunctionName> is specified in your template.json here:
{
"resources": [
"name": "<YourFunctionName>",
"type": "functions",
"properties": {
"config": {
"bindings": [
{
...
}
]
}
}
]
}

Run multiple custom scripts through Azure templates on same VM

I have python script1.py and bash script script2.sh to run after VM is created through ARM template using below snippet. For some reason when I add this script2.sh VM creation fails. 'fileUris' as well as commandToExecute are correct. What could be the reason , and where to look for errors ?
{
"name": "[concat(variables('web'),'/script1')]",
"properties":
{
"settings": {
"fileUris": ["https://.../script1.py"],
"commandToExecute": "python script1.py"
}
}
},
{
"name": "[concat(variables('web'),'/script2')]",
"properties":
{
"settings": {
"fileUris": ["https://.../script2.sh"],
"commandToExecute": "bash script2.sh"
}
}
},
I ommit type, apiVersion, location as well as publisher, type and typeHandlerVersion for clarity. Both scripts depend on "[concat('Microsoft.Compute/virtualMachines/', variables('web'))]"
For Azure VM extension, it's an Azure resource, not the property of a resource. So if you want to add the multi extensions to the VM in one template, you should make each extension as one resource. Here is the example.
update
And if there are two or more extensions in one template, you should make sure the order of the extensions to execute. Although multi extensions in one template, they are still executing one by one in the VM.
For example, the first extension named
"[concat(variables('vmName'),'/', 'antiMalwareExtension')]"
and you need to add "dependsOn" in the second extension:
"dependsOn":[
"[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]",
"[concat('Microsoft.Compute/virtualMachines/', variables('vmName'),'/', 'antiMalwareExtension')]"
],
The extensions after also should do like this.

Google Cloud Compute Engine API: createVM directly with setMetadata

I use #google-cloud/compute to create VM instances automatically.
Also I use startup scripts in those instances.
So, firstly I call Zone.createVM and then VM.setMetadata.
But in some regions startup script is not running. And it is running after VM reset, so looks like my VM.setMetadata call is just too late.
In the web-interface we can create VM directly with metadata. But I do not see this ability in API.
Can it be done with API?
To set up a startup script during instance deployment you can provide it as part of the metadata property in the API call:
POST https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances
{
...
"metadata": {
"items": [
{
"key": "startup-script",
"value": "#! /bin/bash\n\n# Installs apache and a custom homepage\napt-get update\napt-get install -y apache2\ncat <<EOF > /var/www/html/index.html\n<html><body><h1>Hello World</h1>\n<p>This page was created from a simple start up script!</p>\n</body></html>"
}
]
}
...
}
See the full reference for the resource "compute.instances" of the Compute Engine API here.
Basically, if you are using a Nodejs library to create the instance you are already calling this, so you will only need to add the metadata keys as documented.
Also, if you are doing this frequently I guess it would be more practical if you stored the script in a bucket in GCP and simply add the URI to the metadata like this:
"metadata": {
"items": [
{
"key": "startup-script-url",
"value": "gs://bucket/myfile"
}
]
},

Azure-functions: Can environment variables be used in function.json?

I'm currently using the git push deployment option to deploy a few copies of an azure-function. The function's function.json file has multiple "connection" entries linking to different storage accounts (i.e. for a blob trigger & table output). In different copies of the deployed function I'd like to connect to different storage accounts. Is there any special syntax that can be used in function.json to populate the "connection" string from an environment variable?
I guess an alternative would be to edit function.json as part of a custom kudu step, but environment variables seems more consistent with the other azure app service offerings.
This already works, and is actually the recommended way for you to handle connection strings, since you don't want those checked in with your source code. You can use an app setting name for the connection value, and we'll resolve it. In the following EventHub triggered function, the values MyEventHubReceiver, MyEventHubSender and MyEventHubPath will be auto resolved from app settings:
"bindings": [
{
"type": "eventHubTrigger",
"name": "input",
"direction": "in",
"connection": "MyEventHubReceiver",
"path": "%MyEventHubPath%"
},
{
"type": "eventHub",
"name": "output",
"direction": "out",
"connection": "MyEventHubSender",
"path": "%MyEventHubPath%"
}
]
}
In general, most of the binding properties support the %% resolution syntax, allowing you to store the actual values in app settings for both security as well as configurability.

Resources