Jelastic - how to get the environment variables to a Docker container - jelastic

How do I get the environment placeholders to show up inside a Docker container?
The documentation doesn't seem to be exactly clear where those can be used. I have tried to just set those inside the node env as shown in a JPS file that is added to this question.
For example, it shows the environment as env-2461411 and its name as Environment Test in the environment options
Once the environment is launched, the placeholders are still as they show in the JPS file, and not replaced by the actual values.
Result
Placeholders don't seem to be replaced, and as such the shell doesn't like the environment it gets:
-bash: ${env.appid}: bad substitution
-bash: ${env.displayName}: bad substitution
-bash: ${env.domain}: bad substitution
-bash: ${env.envName}: bad substitution
-bash: ${env.name}: bad substitution
-bash: ${env.shortdomain}: bad substitution
-bash: ${env.url}: bad substitution
-bash: ${env.appid}: bad substitution
-bash: ${env.displayName}: bad substitution
-bash: ${env.domain}: bad substitution
-bash: ${env.envName}: bad substitution
-bash: ${env.name}: bad substitution
-bash: ${env.shortdomain}: bad substitution
-bash: ${env.url}: bad substitution
Expected result
Environment variables showing in the bash environment, such as
env_domain=env-2461411
env_name=Environment Test
Example JPS file
{
"jpsType": "install",
"jpsVersion": "1.4",
"name": "Environment Test",
"description":
{
"text": "Template for testing environment variables test",
"short": "Environment variables test"
},
"logo": "https://raw.githubusercontent.com/jelastic-jps/start-stop-scheduler/master/images/logo.png",
"homepage": "https://jelastic.com/",
"categories":
[
"apps/testing"
],
"success":
{
"text": "## Environment test is up and running\nOpen the Jelastic SSH gateway and locate the test-node, see its environment with `env`"
},
"nodes":
[
{
"image": "debian",
"extip": false,
"count": 1,
"cloudlets": 16,
"fixedCloudlets": 1,
"nodeType": "docker",
"nodeGroup": "test",
"displayName": "test-node",
"volumes":
[
"/root"
],
"env":
{
"env_appid": "${env.appid}",
"env_domain": "${env.domain}",
"env_url": "${env.url}",
"env_displayname": "${env.displayName}",
"env_envname": "${env.envName}",
"env_name": "${env.name}",
"env_shortdomain": "${env.shortdomain}"
}
}
],
"onInstall":
{
"log": "${placeholders}"
}
}

During the process of environment's creation mentioned placeholders are not initialized and can't be used.
You can use the method AddContainerEnvVars.
In your case, it will be like this.

Related

Appending to a configuration file

I am creating a script which updates hosts in an application, the config file for each host looks like that below. The script generates the hosts correctly but I need to append every } with a comma , except the last host.
I have tried numerous things but the closest I have got is putting the hosts content on a single line and running a IFS statement against it. Im also not sure how best to approach this, can anyone advise?
{
"cmd": "ssh user#webserver",
"inTerminal": "new",
"name": "webserver",
"theme": "basic",
"title": "Webserver",
}
example of what I am trying to achieve
{
"cmd": "ssh user#webserver",
"inTerminal": "new",
"name": "webserver",
"theme": "basic",
"title": "Webserver",
},
{
"cmd": "ssh user#db",
"inTerminal": "new",
"name": "db server",
"theme": "basic",
"title": "db",
},
{
"cmd": "ssh user#mail",
"inTerminal": "new",
"name": "mail server",
"theme": "basic",
"title": "mail server",
}
You can do things like:
#!/bin/sh
for f in $(generate-host-list); do
read -d \000 c < "$f"
list="$list${list+,
}$c"
done
echo "$list"
If you are just writing to a file that can be simpler (no need for the read, just cat the file). Similarly, if you don't care about munging whitespace, you could do list="$list${list+,}$(cat "$f"). If you are using bash or some other shells you can do non-portable things like += to clean it up.
You can do it like this:
sed '$q; s/^}$/},/' <in_file >out_file
The above sed command works as follows: First check if you've reached the last
line, and if so quit. Otherwise, it'll check if the only character on the line
is }, and if so replace it with },.

Why jq does not see environment variables when run in script?

I have the following JSON file:
{
"1":
{
"media_content":"test3.xspf"
},
"2":
{
"media_content":"test3.xspf"
}
}
In the terminal, using bash as shell, I can execute the following commands:
export schedules="1"
echo $(jq '.[env.schedules]["media_content"]' json_file.json)
Which results in outputing this:
test3.xspf
So it works as expected, but when I place that jq command in a script and run it, it just returns null.
I did echo the values of schedules to make sure the value is non-null inside the script, and it is ok:
echo $schedules
But I did not manage to find the reason, why this command works when run directly in shell and does not work when run in script.
I run the script in the following ways:
bash script.sh
./script.sh
PS: yes, I did offer execute permission: chmod +x script.sh
HINT: env.schedules represents the environment variable 'schedules', and I did make sure that it is assigned in the script before calling jq.
EDIT: I am posting now a whole script, specifying the files tree.
There is one directory containing:
script.sh
json_file.json
static.json
script.sh:
export zone=$(cat static.json | jq '.["1"]');
echo "json block: "$zone
export schedules="$(echo $zone | jq '.schedules')"
echo "environment variable: "$schedules
export media_content=$(jq '.[env.schedules]["media_content"]' json_file.json)
echo "What I want to get: \"test3.xspf\""
echo "What I get: "$media_content
json_file.json:
{
"1":
{
"media_content":"test3.xspf"
},
"2":
{
"media_content":"test3.xspf"
}
}
static.json:
{
"1":
{
"x": "0",
"y": "0",
"width": "960",
"height": "540",
"schedules":"1"
}
}
If I run the script, it displays:
json block: { "x": "0", "y": "0", "width": "960", "height": "540", "schedules": "1" }
environment variable: "1"
What I want to get: "test3.xspf"
What I get: null
If I hardcode the variable:
export schedules="1"
The problem no longer occurs
The problem is simple.
It's not jq's fault.
It the unproper way the schedule's value is piped to the next command.
You have to remove the "s that surround the variable's value, add the second command that uses sed to do that:
export schedules="$(echo $zone | jq '.schedules')"
schedules=$( echo $schedules | sed s/\"//g )
Long answer
Let's see:
here schedules is a string and echo shows its value as being 1:
export schedules="1" ; echo $schedules
here even though double quotes are not mentioned:
export schedules=1 ; echo $schedules
But the result from this also generates additional "s:
export schedules=$(echo $zone | jq '.schedules')
If you print it now you will see additional "s:
echo $schedules # "1"
So just remove the "s from the value:
schedules=$( echo $schedules | sed s/\"//g )

Passing bash shell variable to jq filter

Trying to pass a bash shell variable into a jq filter
JSON:
{
"server1": {
"port": 3333,
"name": "foo",
"timesincelast": 248054
},
"server2": {
"port": 4444,
"name": "bar",
"timesincelast": 248054
}
}
Bash command trying to execute:
servername='server1'
jq --arg servername "$servername" '.$servername .port'
But getting an error
jq: error: syntax error, unexpected '$' (Unix shell quoting issues?) at
<top-level>, line 1:
But direct substitution works
jq '.server1 .port'
You could write:
.[$servername] | .port
or simply:
.[$servername].port

Changing files (using sed) in Packer script leaves files unchanged

currently I am looking into building a build-pipeline using packer and docker.
This is my packer.json:
{
"builders": [{
"type": "docker",
"image": "php:7.0-apache",
"commit": true
}],
"provisioners": [
{
"type": "file",
"source": "./",
"destination": "/var/www/html/"
},
{
"type": "shell",
"inline": [
"chown -R www-data:www-data /var/www/html",
"sed '/<Directory \\/var\\/www\\/>/,/<\\/Directory>/ s/AllowOverride None/AllowOverride all/' /etc/apache2/apache2.conf",
"sed '/<VirtualHost/,/<\\/VirtualHost>/ s/DocumentRoot \\/var\\/www\\/html/DocumentRoot \\/var\\/www\\/html\\/web/' /etc/apache2/sites-enabled/000-default.conf"
]
}
]
}
The Shell Script inside the provisioners section contains some sed commands for changing the AllowOverride and DocumentRoot variables inside the apache config.
When packer runs this script it is working all fine and I am getting a positive sed output, so sed seems to work fine. But in the docker image the files are unchanged.
Copying the files in the file provisioner is working fine.
What am I doing wrong?
It seems you're missing -i (or --in-place) flag in your sed commands. Try with:
"sed -i <expression> <file>"

kubectl jsonpath expression for named path

I have kube service running with 2 named ports like this:
$ kubectl get service elasticsearch --output json
{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
... stuff that really has nothing to do with my question ...
},
"spec": {
"clusterIP": "10.0.0.174",
"ports": [
{
"name": "http",
"nodePort": 31041,
"port": 9200,
"protocol": "TCP",
"targetPort": 9200
},
{
"name": "transport",
"nodePort": 31987,
"port": 9300,
"protocol": "TCP",
"targetPort": 9300
}
],
"selector": {
"component": "elasticsearch"
},
"sessionAffinity": "None",
"type": "NodePort"
},
"status": {
"loadBalancer": {}
}
}
I'm trying to get output containing just the 'http' port:
$ kubectl get service elasticsearch --output jsonpath={.spec.ports[*].nodePort}
31041 31987
Except when I add the test expression as hinted in the cheatsheet here http://kubernetes.io/docs/user-guide/kubectl-cheatsheet/ for the name I get an error
$ kubectl get service elasticsearch --output jsonpath={.spec.ports[?(#.name=="http")].nodePort}
-bash: syntax error near unexpected token `('
( and ) mean something in bash (see subshell), so your shell interpreter is doing that first and getting confused. Wrap the argument to jsonpath in single quotes, that will fix it:
$ kubectl get service elasticsearch --output jsonpath='{.spec.ports[?(#.name=="http")].nodePort}'
For example:
# This won't work:
$ kubectl get service kubernetes --output jsonpath={.spec.ports[?(#.name=="https")].targetPort}
-bash: syntax error near unexpected token `('
# ... but this will:
$ kubectl get service kubernetes --output jsonpath='{.spec.ports[?(#.name=="https")].targetPort}'
443
I had this issues on Windows in Powershell:
Error executing template: unrecognized identifier http2
When specifying a string value wrapped in double quotes in the jsonpath - to solve the error there are 2 ways:
Swap the single and double quotes:
kubectl -n istio-system get service istio-ingressgateway -o jsonpath="{.spec.ports[?(#.name=='http2')].port}"
Or escape the single quote encapsulated in the double quotes:
kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(#.name==\"http2\")].port}'
For me, it was giving the error on windows machine :
kubectl --namespace=app-test get svc proxy --output jsonpath='{.spec.ports[?(#.name=="web")].nodePort}'
> executing jsonpath "'{.spec.ports[?(#.name==web)].nodePort}'":
> unrecognized identifier web
Even though my json contains the name field in the ports array. Online it was working fine.
Instead of using the name field, then i have tried with the port field, which was of type integer and it works.
So, if any one is facing the same issue and if port field is predefined, then they can go with it.
kubectl --namespace=app-test get svc proxy --output jsonpath='{.spec.ports[?(#.port==9000)].nodePort}'

Resources