How do I activate a virtual enviroment on Armbian at startup? - bash

I need to activate a virtualenv Python enviroment on my NanoPI NEO with Armbian (Debian-based). I created a simple script to do so:
#!/bin/bash
echo "Activating application's virtual enviroment..."
source /mpoint/bin/activate
echo "Running main app.."
python /mpoint/mpoint.py
I chmodded all the necessary files, and copied the mentioned script to init.d and update-rc.d'd it.
Problem is, from what I understand I need to run that script as source, that is the script creates its own instance with the enviroment, then closes. Other questions mention various solutions, but none of them answer my question - how can I run this as source at startup?

It could be that you don't have to activate the virtual environment at all. Generally one only needs to call the Python interpreter in the virtual environment's bin directory explicitly. In your case the following should be good enough: /mpoint/bin/python /mpoint/mpoint.py.

Related

What is a foolproof way to make environment variables available to a script?

I have this script on an Ubuntu VM that uses an environment variable that is set in a script in /etc/profile.d/appsetup.sh.
The variable is used in my script, server-up.sh, to start my Java app:
export path=$KEYSTORE_PATH
java -jar -Dsecurity.keystore.path=$path [jarfile]
If I echo the variable, it works:
$ echo $KEYSTORE_PATH
/etc/ssl/certs
And if I run the script on my own (sudo sh server-up.sh) it runs and uses the environment variable just fine.
However, when the script is executed from Jenkins' "Execute Shell" step (on the same VM), it apparently can't access the environment variable, even though supposedly it's available system-wide.
I've tried setting the owner of server-up.sh to both root and jenkins and Jenkins runs it either way, but in neither case does it get the environment variables. In Jenkins, I also tried using the command sudo -E /[path]/server-up.sh but then the job fails and an error says sudo: sorry, you are not allowed to preserve the environment.
I've googled a dozen times for various things, but almost everything that comes up is people asking how to set environment variables in Jenkins, and I don't need to do that; I just want a script that Jenkins can execute have access to system environment variables.
What do I need to do to get this working?
Make a small change to allow the /etc/profile.d/appsetup.sh script to output the variable to a file, where the Jenkins job can access this variable to create an environment variable available for your job to run successfully.
I don't think the context and needs are explained sufficiently well to properly answer the question with a here's how you do it.
On a server, jenkins.war launches from a shell (or a root shell which invokes a shell script with commands which launches jenkins), which has an environment and to which you can set and pass parameters. Those exist in the context of the jenkins.war process. If you run from a daemon (initd / systemd) you get a non-in=teractive shell, which is set differently to your normal shell.
Your Jenkins will typically have Nodes launching agents on remote servers. Those are typically launched via a non-interactive shell (so no user .profile settings).
Then the jobs themselves run on one of the agents where the executor launches a shell for the job execution. Sub-shells may be launched for specific steps.
The two context you mention sudo sh server-up.sh and Jenkins' "Execute Shell" step (on the same VM), even on the same VM do not inherit the same environment as the Node is launched on it's own process using a non-interactive shell and is not aware of anything in your server-up.sh script; it (generally) just gets the /etc/profile.
You have options. You can set Global variables within Jenkins: ${JENKINS_URL}/configure
Global Properties
[ X ] Environment variables
[ X ] Prepare jobs environment (requires Env Inject plugin)
The same options also exist at the Node level
You can install the slaves-setup plugin, which allows you some customization when launching agents (aka slaves).
You can install the Environment Injector plugin, which adds the previously mentioned Prepare jobs environment feature.
It also adds jobs specific configuration options for:
[ X ] Prepare an environment for the run
Plus under the Build Environment section,
[ X ] Inject environment variables to the build process
and
[ X ] Inject passwords to the build as environment variables
Those are encrypted, and I believe are masked
Finally, you can add a build step to Inject environment variables, useful if you need to have different values for different steps.
BUT it's certs in a keystore!
Given that you also mention what you are trying to make available is $KEYSTORE_PATH=/etc/ssl/certs, I wonder if you've explored using the Credentials plugin? Is supports a wide variety of credential types, including:
Password
Username and password
SSH private key
Public Certificate and private key
Binary blob data
That OAuth thingy
The obvious benefit to using this approach vs cooking your own is it's been designed to work securely with Jenkins so your secrets don't get inadvertently exposed. Aside from the extensive documentation on the plugin site, there's more in the book on Using credentials, using them in a pipeline which also mentions the Snippet generator will stub it for you, and on the Cloudbees site - Injecting secrets into builds. You can probably find plenty of help here in S/O and DevOps.
You may also wish to explore the newly introduced Git credentials binding for sh, bat, and powershell, though not sure that's applicable in your case.

Ros Environment in root

I have a ros (kinetic) environment set up on a raspberry pi 3 and am trying to get ros to execute upon startup via a simple bash script which calls roslaunch. Ros works in the user domain but fails when called from root.
Here is my launch_ros.sh script:
#!/bin/bash
source /home/pi/ros_catkin_ws/devel/setup.bash
export PYTHONPATH=/opt/ros/kinetic/lib/python2.7/dist-packages
roslaunch my_pkg pkg_launch.launch
When I run sudo /home/pi/Desktop/ros_launch.sh the roscore crashes with
ERROR: cannot launch node of type [rosout/rosout]: can't locate node
[rosout] in package [rosout] failed to start core service [/rosout]
The traceback for the exception was written to the log file
But, if I comment out
source /home/pi/ros_catkin_ws/devel/setup.bash
and execute /home/pi/Desktop/ros_launch.sh, ros works fine.
Also worthy of noting is if I leave the above source line uncommented when running in the user domain I get the same error as I do in the root. I think this might be pointing me to the solution but I am still very new to ros.
Has anyone come across this issue and found a solution?
In order to run a node as root after changing your shell to root using commands like sudo -i, You can source your current bash profile thats located inside your normal user .bashrc and use it inside root shell.
Try the following code:
#!/bin/bash
source /opt/ros/kinetic/setup.bash
source /home/pi/ros_catkin_ws/devel/setup.bash
export PYTHONPATH=/opt/ros/kinetic/lib/python2.7/dist-packages
roslaunch my_pkg pkg_launch.launch
You need to source your workspace devel to be able to find your own package.
But, you need to source ROS devel to be able to use roscd, roslaunch, ...
In the code below I added:
source /opt/ros/kinetic/setup.bash
to source ROS and be able to use it.
PS: If it's still not working you should try a short delay before running roslaunch.
I was accidentally in a conda environment (base only) and it was messing up big-time. Try disabling any Python virtual environments.
It really worked and it inspired me to report that I didn't have rosnode as a program when I was planning to call ROS scripts in my own applications. Calling source .bashrc directly from the Raspberry Pi's system would refresh the terminal, but there was no way for my program to take over. The solution was to place the required ROS environment scripts in a separate script like name init_env.sh and then call source init_env.sh before any other ROS scripts were executed.

nativescript installation don't work everywhere

i'm beginer in nativescript,i have correctly install ANDROID_HOME environment variable which return my sdk path after echo $ANDROID_HOME but despite this it return me The ANDROID_HOME environment variable is not set or it points to a non-existent directory. You will not be able to perform any build-related operations for Android
but if i put my project in the same directory with sdk directory it return me
Cannot resolve the specified connected device by the provided index or identifier. To list currently connected devices and verify that the specified index or identifier exists, run 'tns device'
I also notice that after each computer restarting environment variable disappear and i must resume a same process , i have edit .profile file, .bashrc file and zshrc file for environnement variable i have a same result
please tell me what wrong ... thank in advance
my ~./bashrc file
export ANDROID_HOME=/home/user/Android/Sdk
export PATH=$PATH:/home/user/Android/Sdk/tools
export PATH=$PATH:/home/user/Android/Sdk/platform-tools
export LD_LIBRARY_PATH=/home/user/Android/Sdk/emulator/lib64
In /home/user/Android/Sdk should be tools and platform-tools folders.
That's enough for me. (Linux Mint 18)
Maybe this information will be useful to someone:
Linux environment variables configuration files
.bashrc
This file is a variable for a particular user. It is loaded every time the user creates a terminal session, that is, in other words, opens a new terminal. All the environment variables created in this file take effect every time a new terminal session begins.
.bash_profile
These variables take effect every time the user connects remotely over SSH. If this file is missing the system will look for .bash_login or .profile.
/etc/environment
This file is for creating, editing and deleting any environment variables at the system level. The environment variables created in this file are available for the entire system, for each user and even for a remote connection.
/etc/bash.bashrc
System bashrc. This file is executed for each user, each time he creates a new terminal session. This only works for local users, when connected through the Internet, such variables will not be visible.
/etc/profile
System file profile. All variables from this file are accessible to any user on the system only if he entered remotely. But they will not be available when creating a local terminal session, that is, if you just open the terminal.
All the Linux environment variables created with these files can be deleted only by removing them from there. Only after each change, you need to either log out and log in, or execute this command:
$ source file_name
So, the environment variable can be of three types:
Local environment variables
These variables are defined only for the current session. They will be irretrievably erased after the session is completed, whether it is remote access or terminal emulator. They are not stored in any files, but are created and deleted using special commands.
Custom shell variables
These shell variables in Linux are defined for a specific user and are loaded each time it logs in using the local terminal, or it is remotely connected. Such variables are usually stored in configuration files: .bashrc, .bash_profile, .bash_login, .profile or in other files located in the user's directory.
System environment variables
These variables are available throughout the system, for all users. They are loaded when the system starts from the system configuration files: / etc / environment, / etc / profile, /etc/profile.d/ /etc/bash.bashrc.
If you are using nvm to manage different nodejs version, then try disabling nvm and using only one global nodejs version.
Regarding the environment variables that are being volatile, make sure that you track down the proper profile file that is being parse and place your changes there.
It would help if you can be more specific about your current platform. Then, people will be able to respond with more precision.
hi i solve my problem by adding in profile file environnement variable
export ANDROID_HOME=~/Android/Sdk
export ANDROID_HOME=~/Android/Sdk/tools
export ANDROID_HOME=~/Android/Sdk/platforms-tools
then i erased all path generate by all commands line entries from my terminal in .bashrc file(i think that it was that the problem source) . finally it work well thanks a lot to everybody for your helps

Setting environment variables at Qt Creator by sourcing a shell script

I am trying to configure the "Run Settings" on Qt Creator 3.0.1.
I would like to set the value of many environment variables by sourcing a shell script 'myScript'.
However, Deployment's Custom Process Step apparently doesn't accept 'source' as a command.
If I execute the script instead of sourcing it, it sets the environment variables of the shell process in which it is executed, which is not the same where Qt Creator runs the executable.
Is it possible to use 'myScript' to configure the environment variables ? How should I call it?
Ofcorse, You can run qt via scrypt. This is very simply. My script is below. Ofcorse you need environment variables in concret file. In my case this file is environment-setup
#!/bin/bash
echo "ustawienie zmiennej /usr/local/angstrom/arm/environment-setup"
# set concret variables important is . /
. /usr/local/angstrom/arm/environment-setup
echo "Uruchomienie qtCreator"
# lunched qtcreator
$HOME/Qt/Tools/QtCreator/bin/qtcreator
You also can set this script as linked to main icon qt in your start menu. After this, all you need to run qt is only click in your shortcut in menu
I think currently there's no proper way to set QtCreator build environment variables using a script. For example in this guide by ICS three options are mentioned in the "Set Up the Environment" section:
1. You can always start Qt Creator in the same shell/console session where you previously ran the environment setup script (i.e. /opt/poky/2.2.1/environment-setup-armv5e-poky-linux-gnueabi). This option is the easiest, but means that you can’t simply launch Qt Creator from a desktop shortcut or similar method. It might also cause problems if you want to build for the desktop or another embedded platform within the same session.
2. You can add the environment variables to the kit’s settings. This option is a little more work to set up, but avoids the issues of the shell setup.
3. A third option is to add the environment variables to your project settings, but that means adding it to every project that uses the kit.

Set global environment variables inside Xcode build phase run script

I'm using Jenkins to do continuous integration builds. I have quite a few jobs that have much of the same configuration code. I'm in the midst of pulling this all out into a common script file that I'd like to run pre and post build.
I've been unable to figure out how to set some environment variables within that script, so that both the Xcode build command, and the Jenkins build can see them.
Does anyone know if this is possible?
It is not possible to do exactly what you ask. A process cannot change the environment variables of another process. The pre and post and actual build steps run in different processes.
But you can create a script that sets the common environment variables and share that script between all your builds.
The would first call your shell to execute the commands in the script and then call xcodebuild:
# Note the dot in the beginning of the next line. It is not a typo.
. set_environment.sh
xcodebuild myawesomeapp.xcodeproj
The script could look like this:
export VARIABLE1=value1
export VARIABLE2=value2
How exactly your jobs will share the script depends on your environment and use case. You can
place the script in some well-known location on the Jenkins host or
place the script in the version controlled source tree if all your jobs share the same repository or
place the script in a repository of its own and make a Jenkins build which archives the script as a build artifact. All the other jobs would then use Copy Artifact plugin to get a copy of the script from the artifacts of script job.
From Apple's Technical Q&A QA1067 it appears that if you create the file /Users/YOU/.MacOSX/environment.plist and populate it with your desired environment variables that all processes (launched by the user with the environment.plist file in their home dir) will pick up these environment variables. You may need to restart your computer (or just log out and back in) before a newly launched process will pick up the variables.
This article also claims that Xcode will also pass these variables to a build phase script. I have not tested it yet but next time I restart my MacBook I will let you know if it worked.
From http://developer.apple.com/library/mac/#/legacy/mac/library/qa/qa1067/_index.html
Q: How do I set environment for all processes launched by a specific
user?
A: It is actually a fairly simple process to set environment variables
for processes launched by a specific user.
There is a special environment file which loginwindow searches for
each time a user logs in. The environment file is:
~/.MacOSX/environment.plist (be careful it's case sensitive). Where
'~' is the home directory of the user we are interested in. You will
have to create the .MacOSX directory yourself using terminal (by
typing mkdir .MacOSX). You will also have to create the environment
file yourself. The environment file is actually in XML/plist format
(make sure to add the .plist extension to the end of the filename or
this won't work).

Resources