Jenkinsfile check if env file exist based on value variable - jenkins

An environment variable like DISABLE_APPLICATION_NAME is optional. The APPLICATION_NAME is the name of the application. Inside our Jenkinsfile we have a loop where the "application name" is stored inside a variable "application_name". I left the loop and other code away, in the example below, but it should be sufficient to give an idea of what I try to accomplish.
I would like to check if the environment variable DISABLE_<application_name> exists and is set to TRUE or FALSE.
pipeline {
stages {
DISABLE_APPLICATION_TEST = True
}
steps {
deploy()
}
}
void deploy() {
application_name = "APPLICATION_TEST"
disable_variable = "DISABLE_${application_name}"
if(env."${disable_variable}"){
disable = env."${disable_variable}"
}else{
disable = False
}
}
This doesn't work, but is there any way to check if an environment variable exists, based on the contents of another variable? So like env["${env_name_stored_in_variable}"] ?

Related

GCE: Setting Environment Variables from Metadata

I am using Packer to generate an image on Google Compute Engine, and Terraform to create the instance. I have set this metadata:
key: env_vars
value: export test=10
Packer is using a script with something like this inside:
curl "http://metadata.google.internal/computeMetadata/v1/project/attributes/env_vars?recursive=tru&alt=text" -H "Metadata-Flavor: Google" -o /tmp/env_vars.sh
source /tmp/env_vars.sh # or . /tmp/env_vars.sh
The problem is that when I create an instance using this image through Terraform the env variables are not available. That means, If I run printenv or echo $test, it is empty.
Even if I write a startup-script for the instance, it doesn't work.
But, if I run the same exact script inside the instance via SSH, it does work.
In all scenarios described above, the file env_vars.sh is created.
I just want to set the env vars from my metadata for any instance.
Any suggestion on how can I achieve this?
EDIT:
Here's the terraform code:
# create instance
resource "google_compute_instance" "default" {
count = 1
name = var.machine_name
machine_type = var.machine_type
zone = var.region_zone
tags = ["allow-http-ssh-rule"]
boot_disk {
initialize_params {
image = var.source_image
}
}
network_interface {
network = "default"
access_config {
// Ephemeral IP
}
}
}
I have reproduced your issue in my own project, and you are right it seems that exportdoes not work on the strat-up script.
I also tried creating a start-up script in a bucket but it does not work.
On the other hand I was able to set the env var in my project:
I’m using a debian-9 image, so, I edited the /etc/profile to add the env vars.
I use the following code to create my VM with env variables:
provider "google" {
project = "<<PROJECT-ID>>"
region = "us-central1"
zone = "us-central1-c"
}
resource "google_compute_instance" "vm_instance" {
name = "terraform-instance"
machine_type = "f1-micro"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
# A default network is created for all GCP projects
network = "default"
access_config {
}
}
# defining metadata
metadata = {
foo = "bar"
}
metadata_startup_script = "echo ENVVAR=DEVELOPMENT2 >> /etc/profile"
}
After the creation of my instance I was able to see the correct values:
$ echo $ENVVAR
DEVELOPMENT2

Dynamic Quartz Configuration in Grails 3

In Grails 2, we had the following block of code in our Config.groovy file. The ConfigurationManagement class did a runtime lookup in a Configuration Management Database to determine if the quartz.autoStartup parameter should be true or false. We also used similar configuration to load additional Quartz properties.
quartz {
def autoStartupOnTheseServers = ConfigurationManagement.getValue("myApp.quartz.autoStartupOnTheseServers", "").split(",")
if ( autoStartupOnTheseServers.any { it.trim().toUpperCase() == hostName.toUpperCase() } ) {
autoStartup = true
}
else {
autoStartup = false
}
// Default for clustering (jdbcStore) has to be "false" so the app will run locally.
// Clustering (jdbcStore) will be true for DEV, TEST, QA, and PROD (set by Configuartion Management).
jdbcStore = ConfigurationManagement.getValue("myApp.quartz.jdbcStore", "false").toBoolean()
// don't set the props if not enabling quartz clustering...causes an exception.
if(jdbcStore == true) { props(quartzProps) }
}
In Grails 3, similar code used in application.groovy doesn't work and there isn't any facility for conditionals that I can find for application.yml. Is there any way in Grails 3 to do a similar dynamic configuration?
In Grails 3, it seems that you can't set these values directly, but you can declare a local variable and use that to set the property. So, for my example above, the following works:
def extServerList = ConfigurationManagement.getValue("myApp.quartz.autoStartupOnTheseServers", "").split(",")
def extAutoStartup = extServerList.any { it.trim().toUpperCase() == hostName.toUpperCase() }
def extJdbcStore = ConfigurationManagement.getValue("myApp.quartz.jdbcStore", "false").toBoolean()
quartz {
autoStartupOnTheseServers = extServerList
autoStartup = extAutoStartup
jdbcStore = extJdbcStore
}

how to get certain property values from config file depending on the environment?

I have some keys that i want to keep in the config file. I have two different keys, one for use in development setting and another for use when the environment is set to production. Now, in grails we extract these property values from config file using
grailsApplication.config.[name of the property in config file]
is it possible to have conditional setting on config file that will return the right key depending on whether the environment is set to production or development? I appreciate any help! Thanks!
We use the approach of separate external config files for different environments and then include them in "config.groovy" depending on the environments like below
environments {
test {
grails.logging.jul.usebridge = true
grails.config.locations = ["file:${userHome}/.grails/${appName}-config-TEST.groovy"]
}
development {
grails.logging.jul.usebridge = true
grails.config.locations = ["file:${userHome}/.grails/${appName}-config-DEV.groovy"]
}
production {
grails.logging.jul.usebridge = false
grails.config.locations = ["file:${userHome}/.grails/${appName}-config-PROD.groovy"]
}
}
But if you want common file for all the environments then you can use the "Environment" available in "grails.util" package like below
package asia.grails.myexample
import grails.util.Environment
class SomeController {
def someAction() {
if (Environment.current == Environment.DEVELOPMENT) {
// insert Development environment specific key here
} else
if (Environment.current == Environment.TEST) {
// insert Test environment specific key here
} else
if (Environment.current == Environment.PRODUCTION) {
// insert Production environment specific key here
}
render "Environment is ${Environment.current}"
}
}

No such property: getFlatConfig when trying to access configuration

So have set up a couple of values in the groovy.config file which I want for my application.
Set them as follows:
environments {
development {
grails.logging.jul.usebridge = true
reslist = ['1400x1200','1200x1024','1024x800','800x600']
resdef = '1024x800'
mapregs = ['World', 'Europe', 'Asia', 'South America','Central America', 'Pacific','Africa']
mapdef = 'World'
Then I try to access them in a controller
if ( params.mapreq == null) {
mapreq = grailsApplication.config.grails.mapdef
} else {
mapreq = params.mapreq
}
It seems to work (kind a) I get something back, but looks like an object pointer in the format
groovy.util.ConfigObject#3764a904
Tried changing it to getFlatConfig
if ( params.mapreq == null) {
mapreq = grailsApplication.getFlatConfig.grails.mapdef
} else {
mapreq = params.mapreq
}
At which point I get a "No such property: getFlatConfig when trying to access configuration" instead
So any suggestions?
Also, would the same solution work for getting the lists (like the mapregs one)?
grailsApplication.config.grails.mapdef should be grailsApplication.config.mapdef since mapdef is at the top level of the config (within that environment block). Since there's nothing stored under grails.mapdef, the value will be a new ConfigObject. That's why config.a.b.c.d=1 works - each time you access a new level that doesn't exist, Groovy automatically creates a new ConfigObject to hold the value being set, but if you're getting and not setting, you end up with just the empty instance.
The 2nd one doesn't work because getFlatConfig should be getFlatConfig() or flatConfig. But you can't use the ConfigObject-style dots with the flat config because it's flattened. If mapdef was actually under grails you'd access it as grailsApplication.flatConfig.'grails.mapdef' or grailsApplication.flatConfig['grails.mapdef']. But like the other one it's not, so you'd use grailsApplication.flatConfig.mapdef.

how to set value in Config.groovy and get different value for same parameter according to environment on gsp in Grails?

I have a situation. I want to set one value to some parameter in Config.groovy in my Grails project. This parameter should have different value for each environment, i.e. for dev environment it is like abc = "devValue", for test environment like abc="testValue" and for production environment like abc="prodValue". and then I want to set that value as a hidden field value on gsp page according to running environment.
There's already an example of this in the Config.groovy that's generated for you:
environments {
development {
grails.logging.jul.usebridge = true
}
production {
grails.logging.jul.usebridge = false
}
}
so you can just add your setting there:
environments {
development {
grails.logging.jul.usebridge = true
abc = "devValue"
}
test {
abc = "testValue"
}
production {
grails.logging.jul.usebridge = false
abc = "prodValue"
}
}
Thanks Igor Artamonov,
I found the solution below.
I added code below in Config.groovy
environments {
development {
abc="devValue"
}
test {
abc="testValue"
}
production {
abc="prodValue"
}
}
And then in gsp i set the hidden field as below.
<input id="oid" type="hidden" name="oid" value="${grailsApplication.config.abc}">
Thank you.

Resources