Add a parameter into a conf file through shell script - linux

I have a conf file as shown below
{
"files": [
{
"paths": ["/srv/log/*"],
"fields": { "type": "appLog", "app": "my_app", "env": "integration", "server": "" }
},
{
"paths": ["/srv/log/tomcat7/*"],
"fields": { "type": "tomcat", "app": "my_app", "env": "integration", "server": "" }
},
{
"paths": ["/srv/log/nginx/*"],
"fields": { "type": "nginxLog", "app": "my_app", "env": "integration", "server": "" }
}
]
}
I have to define the server explicitly through a shell script which is stored in a variable. Which means that i need to add the server in the "" after "server" to another script, For example: $host = my_machine.test.net , say this server address needs to be added into the conf file through my shell script . So when i run my script the above conf becomes
{
"files": [
{
"paths": ["/srv/log/*"],
"fields": { "type": "appLog", "app": "my_app", "env": "integration", "server": "my_machine.test.net" }
},
{
"paths": ["/srv/log/tomcat7/*"],
"fields": { "type": "tomcat", "app": "my_app", "env": "integration", "server": "my_machine.test.net" }
},
{
"paths": ["/srv/log/nginx/*"],
"fields": { "type": "nginxLog", "app": "my_app", "env": "integration", "server": "my_machine.test.net" }
}
]
}
I tried using "sed -i" but couldn't make it work. Can someone help.

The config file is a json document. Use jq to modify it:
jq --arg server 'my.server.net' '.files[].fields.server=$server' conf.json
To change the file in place:
mv conf.json conf.backup.json
jq --arg server 'my.server.net' '.files[].fields.server=$server' conf.json > new
mv new conf.json

Using sed:
sed -i 's/""/"my_machine.test.net"/' file
Or bash script:
$ cat script.sh
#!/bin/bash
sed -i "s/\"\"/\"$1\"/g" file
How to use:
$ ./script.sh my_machine.test.net

Related

How to add base/baseHref to Nx/Vite manifest.json file

How can I prefix the file, css, and assets in a Vite manifest.json file with a CDN URL?
export default defineConfig({
base: 'https://my-cdn.com/',
build: {
manifest: true,
rollupOptions: {
input: '/path/to/main.js'
}
}
})
but the end result is:
{
"main.js": {
"file": "assets/main.4889e940.js",
"src": "main.js",
"isEntry": true,
"dynamicImports": [],
"css": ["assets/main.b82dbe22.css"],
"assets": ["assets/asset.0ab0f9cd.png"]
}
}
instead of:
{
"main.js": {
"file": "https://my-cdn.com/assets/main.4889e940.js",
"src": "main.js",
"isEntry": true,
"dynamicImports": [],
"css": ["https://my-cdn.com/assets/main.b82dbe22.css"],
"assets": ["https://my-cdn.com/assets/asset.0ab0f9cd.png"]
}
}
To achieve that, you need to add a base property with the URL or path in the project.json file under targets/build/options.
Here is an example project.json config file:
{
"name": "app",
"$schema": "node_modules/nx/schemas/project-schema.json",
"sourceRoot": "./src",
"projectType": "application",
"targets": {
"build": {
"executor": "#nrwl/vite:build",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"outputPath": "dist/app",
"base": "https://my-cdn.com"
}
}
}
}
And if you are using vite without NX, then you can provide a base argument to the vite build command.
An example:
vite build --base="https://my-cdn.com"

C++ & vimspector & -exec -enable-pretty-printing

Currently I have to enter -exec -enable-pretty-printing into vimspector debugger command line (debugging C++ code using vscode-cpptools) to see std::string as a "string" rather than as a STL container. Could you advice me how to modify my .vimspector.json to run the command automatically on vimspector launch? I tried to google it out but without success... My current .vimspector.json looks like this.
{
"configurations": {
"Vim - run a test": {
"adapter": "vscode-cpptools",
"configuration": {
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/main",
"args": [
"-enable-pretty-printing"
],
"cwd": "${workspaceRoot}",
"environment": [
{ "name": "VIMRUNTIME", "value": "${workspaceRoot}" }
],
"externalConsole": true,
"stopAtEntry": true,
"MIMode": "gdb",
"logging": {
"engineLogging": false
}
}
}
}
}
Thx,
Michal
PS: The mentioning -enable-pretty-printing as argument in .json file does not work.
OK, I have installed vscode and had a look at its configuration files. It seems that the following .vimspector.json file does what I want.
{
"configurations": {
"FinMat": {
"adapter": "vscode-cpptools",
"configuration": {
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/main",
"args": [],
"cwd": "${workspaceRoot}",
"environment": [
{ "name": "VIMRUNTIME", "value": "${workspaceRoot}" }
],
"externalConsole": true,
"stopAtEntry": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"logging": {
"engineLogging": false
}
}
}
}
}

Run a python script from the startup script

An instance is created when a new file is uploaded to the storage. The startup runs a python script that generates a pdf file for the new file, uploads the pdf back to the storage and deletes the instance. Since the python script has pretty lengthy, I have stored the startup script and the python script in the same location (Cloud Storage). I have passed the paths are metadata while creating the instance. The input to the python script is the file name of the new file. I checked the logs of the instance, its throwing some errors there. Can someone point out what is it that I am doing wrong.
Edited
Error Message:
{
"cpuPlatform": "Intel Haswell",
"creationTimestamp": "2021-08-02T06:40:36.346-07:00",
"deletionProtection": false,
"disks": [
{
"autoDelete": true,
"boot": true,
"deviceName": "xyz",
"diskSizeGb": "10",
"guestOsFeatures": [
{
"type": "UEFI_COMPATIBLE"
},
{
"type": "VIRTIO_SCSI_MULTIQUEUE"
}
],
"index": 0,
"interface": "SCSI",
"kind": "compute#attachedDisk",
"licenses": [
"projects/debian-cloud/global/licenses/debian-10-buster"
],
"mode": "READ_WRITE",
"source": "projects/patch-us/zones/us-central1-a/disks/instance-name",
"type": "PERSISTENT"
}
],
"fingerprint": "XlZ7biyVpAI=",
"id": "3984870299667155772",
"kind": "compute#instance",
"labelFingerprint": "42WmSpB8rSM=",
"lastStartTimestamp": "2021-08-02T06:40:46.210-07:00",
"machineType": "projects/project-name/zones/us-central1-a/machineTypes/e2-medium",
"metadata": {
"fingerprint": "f5o3Pxed5VY=",
"items": [
{
"key": "startup-script-url",
"value": "https://storage.cloud.google.com/project-name.appspot.com/start-up-script/start-script.sh"
},
{
"key": "file_name",
"value": "123456"
},
{
"key": "python_script_name",
"value": "https://storage.cloud.google.com/project-name.appspot.com/start-up-script/generate_fd_report.py"
}
],
"kind": "compute#metadata"
},
"name": "instance-name",
"networkInterfaces": [
{
"accessConfigs": [
{
"kind": "compute#accessConfig",
"name": "External NAT",
"natIP": "35.202.255.222",
"networkTier": "PREMIUM",
"type": "ONE_TO_ONE_NAT"
}
],
"fingerprint": "565TD6a2Y2c=",
"kind": "compute#networkInterface",
"name": "nic0",
"network": "projects/project-name/global/networks/default",
"networkIP": "10.128.0.29",
"stackType": "IPV4_ONLY",
"subnetwork": "projects/project-name/regions/us-central1/subnetworks/default"
}
],
"scheduling": {
"automaticRestart": true,
"onHostMaintenance": "MIGRATE",
"preemptible": false
},
"selfLink": "projects/project-name/zones/us-central1-a/instances/instance-name",
"serviceAccounts": [
{
"email": "project-id-compute#developer.gserviceaccount.com",
"scopes": [
"https://www.googleapis.com/auth/cloud-platform"
]
}
],
"shieldedInstanceConfig": {
"enableIntegrityMonitoring": true,
"enableSecureBoot": false,
"enableVtpm": true
},
"shieldedInstanceIntegrityPolicy": {
"updateAutoLearnPolicy": true
},
"startRestricted": false,
"status": "RUNNING",
"tags": {
"fingerprint": "42WmSpB8rSM="
},
"zone": "projects/project-name/zones/us-central1-a"
}
start-script-sh
#! /bin/bash
ECG_FILE_PATH = $(curl http://metadata/computeMetadata/v1/instance/attributes/file_path -H "Metadata-Flavor: Google")
PYTHON_FILE_PATH = $(curl http://metadata/computeMetadata/v1/instance/attributes/python_script_name -H "Metadata-Flavor: Google")
ECG_FILE_NAME = $(curl http://metadata/computeMetadata/v1/instance/attributes/file_name -H "Metadata-Flavor: Google")
curl -s -o generate_fd.py PYTHON_FILE_PATH
chmod +x generate_fd.py
python3 generate_fd.py ECG_FILE_PATH &
generate_fd_report.py
#!/usr/bin/env python3
def main(file_name):
print("Hello")
main(file_name)
Logs
To download the script, URL path to which you've saved as the metadata value is as follows:
curl -s -o filename.txt $(curl -s http://metadata/computeMetadata/v1/instance/attributes/filename -H "Metadata-Flavor: Google")

CloudFormation how to export a variable

The following is a part of our template. We export a path variable there. However, running from template, this does not work. If I SSH into the server and run the same line, it works and I can use gradle. But just from the template, it somehow doesn't get executed. The other chmod commands work, so the block is clearly execute. Any help is highly appreciated.
...
"LaunchConfiguration": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"packages": {
"yum": {
"java-1.8.0-openjdk-devel": []
}
},
"sources": {
"/opt": "https://services.gradle.org/distributions/gradle-3.4.1-bin.zip",
"/home/ec2-user": "https://github.com/ABC/XYZ/archive/master.zip"
},
"files": {
"/tmp/gradle_config": {
"content": {
"Fn::Join": ["",
[
"#!/bin/bash -ex\n",
"chmod -R 755 gradle-3.4.1/\n",
"export PATH=$PATH:/opt/gradle-3.4.1/bin\n" //<<<< This does not work
]
]
},
"mode": "000500",
"owner": "root",
"group": "root"
},
"/tmp/app_config": {
"content": {
"Fn::Join": ["",
[
"#!/bin/bash -ex\n",
"chmod -R 777 XYZ-master/\n"
]
]
},
"mode": "000500",
"owner": "root",
"group": "root"
}
},
"commands": {
"01_config": {
"command": "/tmp/gradle_config",
"cwd" : "/opt"
},
"02_config": {
"command": "/tmp/app_config",
"cwd" : "/home/ec2-user"
}
}
}
}
}, ...
I found the solution. All those lines are executed as root user. The export PATH... therefore wasn't for my ec2-user. The way I handled is was by putting the path variable (globally) into the /etc/environment file.
In my code snippet, just replace
"export PATH=$PATH:/opt/gradle-3.4.1/bin\n",
with
"echo \"PATH=$PATH:/opt/gradle-3.4.1/bin\" >> /etc/environment"",

Symfony2 composer adding own bundle requirement

How can I install a specific Bundle (for example SonataGoutteBundle) to my Symfony2 project via composer?
I tried this, but it doesn't work for me. Any ideas what's wrong?
"repositories": [
{
"type": "package",
"package": {
"name": "sonata-project/sonatagouttebundle",
"version": "dev-master",
"dist": {
"url": "https://github.com/sonata-project/SonataGoutteBundle.git",
"type": "git"
}
}
}
],
"require": {
"php": ">=5.3.3",
[...],
"sonata-project/sonatagouttebundle": "dev-master"
}
Try something like
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/sonata-project/SonataGoutteBundle.git"
}
],
"require": {
"php": ">=5.3.3",
"vendor/bundle": "dev-master"
}
}
The SonataGoutteBundle must have a composer.json
In this particular case, this would work:
"repositories": [
{
"type": "package",
"package": {
"name": "sonata-project/goutte",
"version": "dev-master",
"source": {
"url": "https://github.com/sonata-project/SonataGoutteBundle.git",
"type": "git",
"reference": "master"
}
}
}
],
"require": {
"php": ">=5.3.3",
"sonata-project/goutte": "dev-master"
}
For all options see the documentation
You should provide a link to an archive (like zip) in "dist" section. If you want to use git you should define "source" section instead:
{
"repositories": [
{
"type": "package",
"package": {
"name": "sonata-project/sonatagouttebundle",
"version": "dev-master",
"source": {
"url": "https://github.com/sonata-project/SonataGoutteBundle.git",
"type": "git",
"reference": "master"
}
}
}
],
"require": {
"php": ">=5.3.3",
"sonata-project/sonatagouttebundle": "dev-master"
}
}
More about defining custom repositories: http://getcomposer.org/doc/04-schema.md#repositories

Resources