Nomad job constraint: run on list of nodes - nomad

I'm trying to find a way to run a Nomad job on a specific set of machines identified by IP address. I have looked at the documentation on the constraint block, but I can't find a way to achieve this. Ideally, it would look something like:
job "myjob" {
type = "sysbatch"
constraint {
attribute = "${attr.unique.network.ip-address}"
operator = "attribute_included_in"
value = "<ip1>,<ip2>,<ip3>"
}
group "sys_batch_job" {
# ...
}
}
The operators set_contains and set_contains_any are both intended for scenarios where the attribute itself contains a list of values. I tried using the regexp operator, but it also doesn't work. Any idea how to go about this?

Invert your thinking.
constraint {
attribute = "<ip1>,<ip2>,<ip3>"
operator = "set_contains"
value = "${attr.unique.network.ip-address}"
}

Related

terrraform get yaml values

I need to get the values from the yaml snippet into 1. the Route53 Zones where I need the apex_nme to be the zone names and 2. the records to be added as Route53 records into the specific zones. I have no clue how to do this. Any help is highly appreciated.
resource "aws_route53_zone" "this" {
for_each = {
for apex in var.source_domains : apex => {
name = apex.name
}
}
}
resource "aws_route53_record" "this" {
for_each = {
for records in var.source_domains : records => {
zone_id = aws_route53_zone.this[each.key].zone_id
name = subdomains.records
type = "A"
records = ["192.168.0.1"]
}
}
}
source_domains:
- apax_name: elastic2ls.com
records:
- elastic2ls.com
- www.elastic2ls.com
- apax_name: elastic2ls.ch
records:
- elastic2ls.ch
- www.elastic2ls.ch
- image.elastic2ls.ch
- m.elastic2ls.ch
- static.elastic2ls.ch
Your question is presented as if it's about YAML parsing, but I suspect you're really asking about how to write the for_each expression for aws_route53_record.this to create all of the records across all of the domains.
For completeness, I'll note that you could get var.source_domains to be populated from that YAML by making the calling module define that variable with an expression like this:
module "example" {
source = "../modules/example"
source_domains = yamldecode(file("${path.module}/example.yaml")).source_domains
# ...
}
Inside the module itself then, I'd first declare that module with a suitable type constraint to make it clear what data structure we're expecting, like this:
variable "source_domains" {
type = set(object({
apax_name = string
records = set(string)
}))
}
Here I defined both the top-level structure and the nested record as set types, because the way we're going to use them means that the ordering isn't important and we're expecting them all to be unique.
With all of that in place we can start to write out the resource definitions. Let's start with the zones, which are the simpler case because var.source_domains already meets the main requirement of having one element per resource instance we want to declare:
resource "aws_route53_zone" "example" {
for_each = {
for d in var.source_domains : d.apax_name => d
}
name = each.value.apax_name
}
With the example YAML input you shared, this block will declare two instances of this resource:
aws_route53_zone.example["elastic2ls.com"]
aws_route53_zone.example["elastic2ls.ch"]
The aws_route53_record declaration is a little trickier because we need to project the input data structure into a new structure where there's one element per record we want to declare, rather than one element per zone. Flattening nested data structures for for_each is a common use for the flatten function, and we can adapt the networks and subnets example from the documentation to work with zones and records instead:
locals {
zone_records = flatten([
for d in var.source_domains : [
for r in d.records : {
zone_name = d.apax_name
zone_id = aws_route53_zone.example[d.apax_name].id
record = r
}
]
])
}
This local value is constructing a list of objects where each object represents one valid pairing of zone and record. That means that the number of elements matches the number of aws_route53_record instances we need to declare, and so we can use this data structure in for_each:
resource "aws_route53_record" "example" {
for_each = {
for zr in local.zone_records : zr.record => zr
}
zone_id = each.value.zone_id
name = each.value.record
# ...
}
This example diverges a little from the typical flatten/for_each pattern because all of your record names already have the zone name embedded in them anyway, and so we don't need the usual expression to generate a compound unique key with multiple parts, like "${subnet.network_key}.${subnet.subnet_key}" in the documentation's example. The record name alone is sufficient for a unique key across all pairs in this case.
This then, again based on your example YAML, will declare the following instances:
aws_route53_record.example["elastic2ls.com"]
aws_route53_record.example["www.elastic2ls.com"]
aws_route53_record.example["elastic2ls.ch"]
aws_route53_record.example["www.elastic2ls.ch"]
aws_route53_record.example["image.elastic2ls.ch"]
aws_route53_record.example["m.elastic2ls.ch"]
aws_route53_record.example["static.elastic2ls.ch"]

How to use auto increment index in Tarantool?

I made auto increment index:
box.space.metric:create_index('primary', {
parts = {{'id', 'unsigned'}},
sequence = true,
})
Then I try to pass nil in id field:
metric.id = nil
When I try insert this values, I catch error:
Tuple field 1 type does not match one required by operation: expected unsigned
What value do I have to pass for autoincrement field?
Second questions. If I use tarantool-cluster with few instances (for ex. cartridge-application based), is it prove use autoincrement indexes? Will there be a cases that there will be duplicate keys on different instances?
It is not possible to pass nil. When you assign nil, you erase field. Use box.NULL instead.
But better, use some kind of cluster id, which perform well across cluster, instead of autoincrement, which works only inside one node.
For cluster-wide ids I could propose UUID or something like ULID (for ex from https://github.com/moonlibs/id)

How do I get unique field values using rethinkdb javascript?

I have a field which has similar values. For eg {country : 'US'} occurs multiple times in the table. Similar for other countries too. I want to return an array which contains non-redundant values of 'country' field. I am new to creating Databases so likely this is a trivial question but I couldn't find anything useful in rethinkdb api.[SOLVED]
Thanks
You can use distinct, but the distinct command was created for short sequences only.
If you have a lot of data, you can use map/reduce
r.table("data").map(function(doc) {
return r.object(doc("country"), true) // return { <country>: true}
}).reduce(function(left, right) {
return left.merge(right)
}).keys() // return all the keys of the final document

Rails 4 and Mongoid: programmatically build query to search for different conditions on the same field

I'm building a advanced search functionality and, thanks to the help of some ruby fellows on SO, I've been already able to combine AND and OR conditions programmatically on different fields of the same class.
I ended up writing something similar to the accepted answer mentioned above, which I report here:
query = criteria.each_with_object({}) do |(field, values), query|
field = field.in if(values.is_a?(Array))
query[field] = values
end
MyClass.where(query)
Now, what might happen is that someone wants to search on a certain field with multiple criteria, something like:
"all the users where names contains 'abc' but not contains 'def'"
How would you write the query above?
Please note that I already have the regexes to do what I want to (see below), my question is mainly on how to combine them together.
#contains
Regex.new('.*' + val + '.*')
#not contains
Regex.new('^((?!'+ val +').)*$')
Thanks for your time!
* UPDATE *
I was playing with the console and this is working:
MyClass.where(name: /.*abc.*/).and(name: /^((?!def).)*$/)
My question remains: how do I do that programmatically? I shouldn't end up with more than two conditions on the same field but it's something I can't be sure of.
You could use an :$and operator to combine the individual queries:
MyClass.where(:$and => [
{ name: /.*abc.*/ },
{ name: /^((?!def).)*$/ }
])
That would change the overall query builder to something like this:
components = criteria.map do |field, value|
field = field.in if(value.is_a?(Array))
{ field => value }
end
query = components.length > 1 ? { :$and => components } : components.first
You build a list of the individual components and then, at the end, either combine them with :$and or, if there aren't enough components for :$and, just unwrap the single component and call that your query.

Rearranging active record elements in Yii

I am using a CDbCriteria with its own conditions, with & order clauses. However, the order i want to give to the elements in the array is way too complex to specify in the order clause.
The solution i have in mind consists of obtaining the active records with the defined criteria like this
$theModelsINeed = MyModel::model()->findAll($criteria);
and then rearrange the order from my php code. How can i do this? I mean, i know how to iterate through its elements, but i donĀ“t know if it is possible to actually change them.
I have been looking into this link about populating active records, but it seems quite complicated and maybe someone could have some better advice.
Thanks
There is nothing special about Yii's active records. The find family of methods will return an array of objects, and you can sort this array like any other array in PHP.
If you have complex sort criteria, this means that probably the best tool for this is usort. Since you will be dealing with objects, your user-defined comparison functions will look something like this:
function compare($x, $y)
{
// First sort criterion: $obj->Name
if ($x->Name != $y->Name) {
return $x->Name < $y->Name ? -1 : 1; // this is an ascending sort
}
// Second sort criterion: $obj->Age
if ($x->Age != $y->Age) {
return $x->Age < $y->Age ? 1 : -1; // this is a descending sort
}
// Add more criteria here
return 0; // if we get this far, the items are equal
}
If you do want to get an array as a result, you can use this method for fetching data that supports dbCriteria:
$model = MyModel::model()->myScope();
$model->dbCriteria->condition .= " AND date BETWEEN :d1 AND :d2";
$model->dbCriteria->order = 'field1 ASC, field2 DESC';
$model->dbCriteria->params = array(':d1'=>$d1, ':d2'=>$d2);
$theModelsINeed = $model->getCommandBuilder()
->createFindCommand($model->tableSchema, $model->dbCriteria)
->queryAll();
The above example shows using a defined scope and modifying the condition with named parameters.
If you don't need Active Record, you could also look into Query Builder, but the above method has worked pretty well for me when I want to use AR but need an array for my result.

Resources