I'm trying to set environment variables through the commands module within cfn-init.
"commands": {
"set-env": {
"env": {
"HOSTNAME": "fb-iaas.elevatebilling.com"
},
"cwd": "~",
"command": "echo \"$HOSTNAME\" > test.txt"
}
},
And this is the output I see in cfn-init.log
2016-09-22 10:26:53,638 DEBUG Running command set-env
2016-09-22 10:26:53,639 DEBUG No test for command set-env
2016-09-22 10:26:53,655 INFO Command set-env succeeded
2016-09-22 10:26:53,655 DEBUG Command set-env output:
But environment variable HOSTNAME doesn't get updated. Can someone please explain what am I doing wrong?
Thanks
When you say that the variable doesn't get updated, how have you found this out? Did you open "test.txt" and read what was piped to it or did you ssh into the instance and run an "echo $HOSTNAME"? The reason why I ask is because the AWS docs on running commands with cfn-init (http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html) say this for the "env" option:
env -
Optional. Sets environment variables for the command. This property
overwrites, rather than appends, the existing environment.
From my interpretation of this and from my experience, environment variables set using "env" exist only for the command in which it resides. If you read in the "test.txt" file that the variable is set to what you want but not when you run an "echo $HOSTNAME" it's because the variable exists only for the command. I suggest that if you want your environment variables to persist, you set them using "export" in the UserData section of the Cloudformation template.
The AWS documentation gives the syntax for Linux. Since your script starts with echo, I suppose you are running Windows. In that case, the environment variable syntax should be %HOSTNAME%, instead of $HOSTNAME.
Related
I have nodejs installed and I can access it in my default shell (bash).
The problem is that when I try to run a file it tries to open it with /bin/sh and thus it says /bin/sh: node: command not found
The problem disappears when I set in configuration run in integrated terminal as I have set the default shell for the terminal to bash.
How to change the default shell used in the code runner to bash so that I can run the file?
The only solution I found to your same problem is to make code runner run into the vscode integrated terminal.
My settings.json
"code-runner.executorMapByGlob": {
"*.ts": "ts-node --esm $fullFileName",
},
"code-runner.runInTerminal": true
In my case it stopped happening when opened vs code on bash, in that scenario code runner runs with bash, so probably you should take a look what starts the vs code and how.
If your problem is that sh doesn't have the same path as bash just add to the /etc/profile.d a file with the following code and should work:
if [ -n "${BASH_VERSION}" ]; then
# include ~/.bashrc if it exists
if [ -f "${HOME}/.bashrc" ]; then
. "${HOME}/.bashrc"
fi
fi
I using following link to create :
https://console.bluemix.net/docs/containers/cs_tutorials.html#cs_cluster_tutorial
In Lesson3, Step 3 "Verify that the KUBECONFIG environment variable is set properly.", I am unable to set the variable KUBECONFIG.
I am able to execute the following command:
ibmcloud ks cluster-config clusternameabc
I am getting following message
The configuration for clusternameabc was downloaded successfully. Export environment variables to start using Kubernetes, with the following message:
SET KUBECONFIG=C:\Users\AAA.bluemix\plugins\container-service\clusters\customernameabc\kube-config-hou02-clusternameabc.yml
When I run the above SET command in powershell, I just get the prompt back with environment variable not set.
When I do
$Env:KUBECONFIG=C:\Users\AAA.bluemix\plugins\container-service\clusters\customernameabc\kube-config-hou02-clusternameabc.yml
Notepad gets opened and this file is shown. I want to just set the environment variable and unable to. Can someone please tell me what I am doing wrong? I tried searching and could not find answers and IBM documentation has only Mac examples.
In Powershell
1) $env:KUBECONFIG = "C:\Users\AAA.bluemix\plugins\container-service\clusters\customernameabc\kube-config-hou02-clusternameabc.yml"
2) ls env:KUBECONFIG
Slightly easier to remember.
Based on my tests, you have to wrap the value of the environment environment in double quotes, like so:
$Env:KUBECONFIG="C:\Users\AAA.bluemix\plugins\container-service\clusters\customernameabc\kube-config-hou02-clusternameabc.yml"
Then, you can check the environment variables in PowerShell via this command:
Get-ChildItem Env:
Try this:
$Env:KUBECONFIG = ibmcloud cs cluster-config --export clusternameabc | Select -First 1 | % {$_.replace("SET KUBECONFIG=","")}
I've created aliases in c:\Users\user\.bash_profile and in C:\Program Files\Git\etc\profile.d\aliases.sh but both configs getting ignored by VSCode integrated terminal, which is configured to use git bash:
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
if I open GitBash itself - aliases works fine
how do I force integrated terminal to respect the configs?
You can try adding to the settings:
// The command line arguments to use when on the Windows terminal.
"terminal.integrated.shellArgs.windows": [
"--login", "-i"
],
-i - force the shell to run interactively.
--login - make this shell act as if it had been directly invoked by login. When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
When invoked as an interactive shell with the name sh, Bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files.
Read more.
As an alternative you can use the .bashrc file instead of .bash_profile.
The solution using the "--login" and "-i" shell arguments did not work for me. What did work was using the bash "-rcfile" shell argument, like this, in my settings file:
"terminal.integrated.shellArgs.windows": [
"-rcfile",
"c:\Users\\.bash_profile", ],
... where <userid> is my Windows userid and the alias commands are in a file called ".bash_profile" that is located in c:\Users\<userid>
I simply switched to my root user directory c:\Users\user then ran source .bashrc. This did the trick on my machine, hope it helps.
Create .bashrc in c:\Users\username
Add an alias, e.g alias gotossh="cd /c/users/username/.ssh"
In your terminal, run source .bashrc while in c:\Users\username
Confirm that the alias works by running alias
You might not have a .bashrc file in the users folder which is important to note.
Also remember to write the paths correct and don't leave any space between the equals in e.g alias="somecommand"
As of 2020 there is no need to add the shellArgs.
I'm currently working on a Node.js project, and would like to automate the setting of environment variables. I've written a script (set_env.ps1) that looks something like this:
Write-Host "Setting environment variables...";
$env:CLIENT_KEY="xxxxxxxxxxxxxxxx";
$env:CLIENT_SECRET="xxxxxxxxxxxxx";
[etc]
And I call this from some npm scripts:
"scripts": {
"install:windows": "powershell ./set_env.ps1",
"start": "npm run install:windows && node index",
"monitor": "npm run install:windows && nodemon index"
},
It doesn't seem to work though. I can enter those same commands manually, one at a time, in the command line, and environment variables are set. Just by running the script, though, "Setting environment variables..." prints to the powershell prompt, but no variables get set.
Everything on the Internet seems to want to do this from the prompt, one env var at a time. This is extremely tedious with a long list of environment variables, and the embedded Powershell prompt in Webstorm has a habit of not accepting pasted strings.
Answering this question because it's likely somebody else will run into this issue.
As the two comments indicated, a Powershell script process's environment is lost on termination. Therefore, environment variables set inside the script won't get passed to the Powershell prompt.
Instead of chaining npm scripts to call everything we need, instead we can call npm run monitor or npm run start from the powershell script, like so:
Write-Host "Setting environment variables...";
$env:CLIENT_KEY="xxxxxxxxxxxxxxxxx";
$env:CLIENT_SECRET="xxxxxxxxxxxxxxxxxxxxxxx";
[etc]
[...]
Write-Host "Initializing process..."
npm run monitor
Then, we have our npm scripts like so:
"scripts": {
"start:windows": "powershell ./start.ps1",
"start": "node index",
"monitor": "nodemon index"
}
To initiate, run npm run start:windows from the command prompt.
I want to make an updater for my Electron application, and I stuck on the GitHub access token.
I have generated a token from my GitHub account, and after that, I tried to set that token in my Windows environmental variables.
When I go to my application and I run this file publish.sh
publish.sh
#!/bin/sh
if [ -z "$GH_TOKEN" ]; then
echo "You must set the GH_TOKEN environment variable."
echo "See README.md for more details."
exit 1
fi
# This will build, package and upload the app to GitHub.
node_modules/.bin/build --win --mac -p always
I run this file ./publish.sh and I get this message:
You must set the GH_TOKEN environment variable.
I want to achieve step 4 and 5 in this example:
https://github.com/iffy/electron-updater-example
I tried to run this command from the Git Bash export GH_TOKEN="435468246872235283762846848267", but I get a return code of 0.
How do I set an environment variable on Windows 10, which was generated from GitHub?
Make sure to restart a new CMD session (in which you can type bash) in order to make sure your session does inherit the new Windows environment variable you have just set.
Once you have done that, you can check in the (new) Git Bash session which are the environment variables already set, with:
env
env | grep GH
Make sure your script starts with
#!/bin/bash
The OP George points out in the comments that the correct form is:
export GH_TOKEN=MY_VARIABLE_NAME
(no double quotes)