I have a defined variable service which can be either redis-db or other-stuff in my Grafana Dashboard.
I wish to do:
if $service =~ ".*redis.*":
promql-query1
else
promql-query2
What I've achieved, based on this post, is
sum(kube_statefulset_status_replicas_ready{statefulset=~".*redis.*", namespace="$namespace"}) and on() (vector(1) > vector(0)) or on() sum(kube_deployment_status_replicas_available{deployment=~".*$service.*",pod_template_hash="",namespace="$namespace"})
It works but I wish to change the (vector(1) > vector(0)) for ($service =~ ".*redis.*"), so when variable redis is selected, the panel's query is the first one.
I am currently working with Azure Devops Build Pipelines, and am trying to call a template file to do some tasks from my build yaml.
I am facing some difficulties to pass parameters to the template file. Let assume that this is my template file (simplified) which works fine :
parameters:
iterations: []
steps:
- ${{ each i in parameters.iterations }}:
- task: PowerShell#2
displayName: "Get key values ${{i}}"
name: getKeyValues_${{i}}
inputs:
targetType: 'inline'
script: |
$item = "${{i}}"
Write-Host "item : $($item)"
$keyVal = $item -split "_"
Write-Host $keyVal
Write-Host "key: $($keyVal[0]) | value: $($keyVal[1])"
echo "##vso[task.setvariable variable=key;isOutput=true]$($keyVal[0])"
echo "##vso[task.setvariable variable=value;isOutput=true]$($keyVal[1])"
So I want my iterations parameter contain something like this :
iterations: ["1_60", "2_40"]
Inside my Yaml pipeline I have the following code(also simplified) :
Not working scenario
- task: PowerShell#2
displayName: Calculate iterations for $(copies) copies
name: calculateIterations
inputs:
targetType: 'inline'
script: |
# Do some stuf here to get the arrow below from int value = 100
$iterations = ["1_60, "2_40"]
echo "##vso[task.setvariable variable=iterations;isOutput=true]$($iterations)"
- template: container-template.yml
parameters:
iterations: $(calculateIterations.iterations)
Working scenario
- task: PowerShell#2
displayName: Calculate iterations for $(copies) copies
name: calculateIterations
inputs:
targetType: 'inline'
script: |
# Do some stuf here to get the arrow below from int value = 100
$iterations = ["1_60, "2_40"]
echo "##vso[task.setvariable variable=iterations;isOutput=true]$($iterations)"
- template: container-template.yml
parameters:
iterations: ["1_60, "2_40"]
As you can see, the problem is that I am unable to use the output variable of my script to pass it as parameter to my template.
When I run the not working scenario I have the following error :
I have found this post, but no solutions yet...
As what 4c74356b41 said, this is the dilemma at present. In another word, the Not working scenario you mentioned does not support to achieve until now.
Now, we must let the template know the clear text at compile time. Because during this compile time, we have difficulty to do 2 or more things in one step at same time, especially contain compiling variable value, passing to corresponding template dynamic arguments and etc.
Expected a sequence or mapping. Actual value
'$(calculateIterations.iterations)'
More detailed, during compile time (after you click Run but before pipeline start truely):
1) Firstly we map the value that come from YAML pipeline, to make sure the - ${{ each i in parameters.iterations }} has clear value to start.
2) After it is done, then parse exact value on name: getKeyValues_${{i}} in script order.
In your scenario, it cannot even satisfy the first step since what you passed is a variable, and we do not has parse value process here. That's why you saw error said Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)'.
Another expression of this error message is: we(template) are looking forward to exact value(s) to map our dynamic parameters, but what you give is a unrecognized content $(calculateIterations.iterations). Sorry, we cannot start to run.
Here is a cloudformation template that works as expected.
https://github.com/shantanuo/cloudformation/blob/master/updated/so2.tpl.txt
But when I change the last line to something like this...
/home/ec2-user/mysecret.txt`'' --valid-ips !Ref MyIpAddress >
It silently ignores the command. Is there any other way to substitute the MyIpAddress variable?
Instead of using 'Fn::Join' you can use 'Fn::Sub'. This will make your template more readable as won't have to break your script inot multiple lines and you can reference MyIpAddress as ${MyIpAddress}.
I do not know how and why does it work. But this is what I was looking for.
- >-
/usr/local/bin/aws-ec2-assign-elastic-ip --access-key ''`cat
/home/ec2-user/myaccesskey.txt`'' --secret-key ''`cat
/home/ec2-user/mysecret.txt`'' --valid-ips '
- !Ref MyIpAddress
- |
'
Thanks to Pat's comment!
I am using kitchen with ec2 driver. I would like to add Name tag to ec2 instances based on the instance name kitchen creates. If I had a 'default' suite and was using centos7.2, kitchen list would name the instance 'default-centos-72'.
I could hard code something like this:
suites:
- name: default
driver_config:
tags: { "Name": "kitchen-default-centos-72" }
But what I'd really like is something like this:
suites:
- name: default
driver_config:
tags: { "Name": <%= figure out instance name and prepend kitchen- %> }
My example suggests using ERB which seems like the way to go to me. But I can't seem to figure out what code to use to get the name of the instance. I tried using a bit of Kitchen::Config.new... but couldn't figure out something that worked. Any suggestions would be much appreciated.
Took me a while but I finally ran across an example that may have showed me the light. While looking through the InSpec options for kitchen I found you can have it output a results file with the platform and suite name that was used during the test run. The below syntax in your platforms: block nested under the driver: option should work. I haven't tested this by examining the instance during a run but hopefully I can find some time to do that soon. If it doesn't work let me know and we can tweak it until it does.
platforms:
- name: ubuntu
driver:
tags:
Name: test-kitchen-%{platform}-%{suite}
How this should work is that the .kitchen.yml file gets run through an ERB pre-processor so the %{platform} resolves to an instance variable during the loop across the platforms and suites arrays.
As far as I can tell there seems to be no straightforward way to include instance properties in the kitchen YAML. I added the following snippet to my kitchen.yml to check what is available in the kitchen YAML's ERB namespace:
<%
puts "Instance vars: #{instance_variables}"
puts "Local vars: #{local_variables}"
puts "Global vars: #{global_variables}"
puts "Methods: #{methods}"
%>
The results when running kitchen create for a specific instance were disappointing, containing nothing that looks like instance specification data:
Instance vars: []
Local vars: [:_erbout, :spec, :bin_file]
Global vars: [:$-0, :$\, :$DEBUG, :$-W, :$0, :$-d, :$-p, :$PROGRAM_NAME, :$:, :$-I, :$LOAD_PATH, :$", :$LOADED_FEATURES, :$,, :$/, :$INPUT_LINE_NUMBER, :$-l, :$-a, :$INPUT_RECORD_SEPARATOR, :$ORS, :$OUTPUT_RECORD_SEPARATOR, :$PROCESS_ID, :$NR, :$#, :$!, :$DEFAULT_INPUT, :$PID, :$PREMATCH, :$CHILD_STATUS, :$LAST_MATCH_INFO, :$LAST_READ_LINE, :$DEFAULT_OUTPUT, :$MATCH, :$fileutils_rb_have_lchown, :$POSTMATCH, :$LAST_PAREN_MATCH, :$IGNORECASE, :$ARGV, :$fileutils_rb_have_lchmod, :$stdin, :$stdout, :$stderr, :$>, :$<, :$., :$FILENAME, :$-i, :$*, :$SAFE, :$thor_runner, :$_, :$~, :$;, :$-F, :$?, :$$, :$ERROR_INFO, :$&, :$`, :$', :$+, :$=, :$KCODE, :$-K, :$ERROR_POSITION, :$FS, :$FIELD_SEPARATOR, :$OFS, :$OUTPUT_FIELD_SEPARATOR, :$RS, :$VERBOSE, :$-v, :$-w]
Methods: [:inspect, :to_s, :to_yaml, :to_json, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :methods, :instance_variable_set, :protected_methods, :instance_variables, :instance_variable_get, :private_methods, :public_methods, :method, :define_singleton_method, :public_send, :singleton_method, :public_method, :extend, :to_enum, :enum_for, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :object_id, :send, :display, :class, :nil?, :hash, :dup, :singleton_class, :clone, :then, :itself, :yield_self, :untaint, :taint, :tainted?, :untrusted?, :trust, :frozen?, :untrust, :singleton_methods, :equal?, :!, :__id__, :==, :instance_exec, :!=, :instance_eval, :__send__]
The local variable spec looked hopeful at first, but turned out to be a GemSpec object.
All things considered, you will probably have to create a convention to always specify the instance in some external way. You could use for example an environment variable of your choice, which you could then access in the template as <%= ENV['<VARNAME>'] %> (where you replace <VARNAME> with the name of your environment variable). There are probably other ways of getting the information in there, but you will still have to specify it in more places than just the Test Kitchen command.
I'm trying to write a syntax definition for Gradle in Sublime Text 3. Many pieces of a Gradle build file are really just Groovy and so I'm trying to take advantage of the current Groovy highlighting support by using include. Thus far this is working fairly well, by I'm stuck on how to apply it to a particular piece.
Here is the Gradle snippet I am trying to highlight:
task copyTask (group: 'Install NGA - deploy', type: Copy, dependsOn: 'whoCares') {
from 'resources'
into 'target'
include('**/*.txt')
}
And this is the syntax I'm using to match that snippet:
- name: copy.task.source.gradle
begin: '\s*(task)\s+(\w+)\s*\((.*type: Copy.*)\)\s*{'
comment: 'Copy task definition'
beginCaptures:
'1': {name: keyword.task.source.gradle}
'2': {name: entity.name.function}
'3': {name: source.groovy}
end: '}'
contentName: copy.body.source.gradle
patterns:
- include: source.groovy
Most of this appears to work as intended. (Always hard to know with RegEx.) My problem is that the third capture. I want to apply all the rules contained in 'source.groovy' to the text between the parentheses and what I have above is not getting the job done.
To clarify: the text is "captured" and tagged as source.groovy, but that's not actually quite what I want. I don't want it simply tagged as source.groovy, I want the rules from source.groovy to be used when evaluating the text. The last line of my example successfully does this to the "content" section (text in between the braces) but simply putting include does not work.
'3': {include: source.groovy} # This gets an error.
If there is a syntax to apply include directly to a capture I can't find it, and I can't figure out another technique. Maybe something that has nested begin and end tags?
If I am understanding this correctly you would like the third capture group source.groovy to match the group: 'Install NGA - deploy', type: Copy, dependsOn: 'whoCares' part of your example.
In that case you would just need to alter you expression to capture more of the string like so:
begin: '\s*(task)\s+(\w+)\s*\((.*type: Copy.*?)\)\s*{'