Is there a way to use the service name with wildcard using salt stack? - jinja2

I have a scenario where I have to use service name with wildcard since service name starts with a particular name and it contains the version of software. E.g. in server-1, service name is RunningService14.1.0 and in server-2, service name is RunningService15.2.0.
How can I pass the service name dynamically?
Below syntax is not working.
start_service:
service.start:
- name: RunningServ*
Where as below syntax is working fine.
status_service:
service.status:
- name: RunningServ*

We can get the service status with wildcard because the service.status module supports it. Quoting from documentation:
Return the status for a service. If the name contains globbing, a dict mapping service name to True/False values is returned.
But the same is not true for the service.start module, which requires a specific service name.
To avoid using the wildcard, you could keep the version of software in a pillar or custom grain for each server. Example for server-1:
software_version: "14.1.0"
Then your state file can be simple and just have:
start-service:
module.run:
- service.start:
- name: RunningService{{ software_version }}
This will ensure that the appropriate version is picked up for each server, and the corresponding service will be started.
Otherwise, as #β.εηοιτ.βε suggested in his comment, we can get all services, filter and take action only when service name contains RunningServ. Though this will require writing logic in the state file.
{% set all_services = salt['service.get_all']() %}
{% for service_name in all_services %}
{% if 'RunningServ' in service_name %}
start-service-{{ service_name }}:
module.run:
- service.start:
- name: {{ service_name }}
{% endif %}
{% endfor %}

Related

Dynamic variable name Jekyll / Liquid

I'm trying to use a variable as a name of another variable.
{% assign key = string[0] %}
{% assign {{key}} = string[1] %}
Is this possible? If not, is there any alternative that I could use that would let me achieve similar things as this variable?
I'm not trying to access a variable, I'm trying to assign it, so this isn't a duplicate of Dynamic Variables Jekyll Liquid.

Liquid Tag in Where Filter in Jekyll

The following:
{% assign devices = site.data.equipment | where:"department","sound" | group_by:"servicelocation" %}
Is helping me build a list of sound devices from the equipment data list, grouped by location. The file resides in the _includes folder and is entitled equipment_list.html.
When I include this file in a _page, it displays exactly what I need, but I am attempting to take it one step further.
{% assign departmentname = page.name | remove:"equipment_list_" | remove:".md" %} yields the word sound based upon the _page/equipment_list_sound.md file.
{{ departmentname }} = "sound" as expected.
Can I insert this value into the where filter somehow to be able to re-use the equipment list page over and over again for different departments?
where:"department","{{departmentname}}" fails, and any variation I can imagine also fails. Is it possible?
You can use : {% assign devices = site.data.equipment | where:"department", departmentname | group_by:"servicelocation" %}

Fetch currently logged in user relation with a given model instance

Assume we have a model called Author as such:
class Author(models.Model):
name = models.CharField(max_length=250)
We also have a feature which enables users to follow a certain author:
class UserFollow(models.Model):
author = models.ForeignKey(Author)
user = models.ForeignKey(User)
If you have a button in your UI that enables logged in users to follow / unfollow these authors. One way to check if this user is already following a certain author is to make a query on the existence of a record in UserFollow. However, when you're fetching a list of 10 / 20 .. etc authors, how would you check each author for the currently logged in user?
The usual approach would result in +X number of extra queries depending on how many items you're loading per page. Is there a more efficient way to achieve the same effect?
Thanks in advance
you can fetch all instances of UserFollow in view, pass it as a variable to the template and then you can check if given author is in this list
def my_view(request):
following = list(UserFollow.objects.filter(user=request.user))
template_context = {
'following': following
}
template:
{% for author in authors %}
{% if author in following %}
{# do some magic #}
{% endif %}
{% endfor %}

How do you refer to Config values from the admin interface in Zotonic validators?

I would like to be able to refer to a System Configuration value set in the Zotonic admin interface within a template.
In particular I would like to create a configurable password complexity regex so I can write a validate statement like the following:
{% validate id="new_password" type={format pattern=config.mod_admin_identity.password_regex %}
How do you refer to Config values from the admin interface in Zotonic validators?
The answer is quick to retrieve thanks to Arjan's new search I went from http://zotonic.com/search?q=config to http://zotonic.com/documentation/719/m-config and quickly devised a solution using m_config.
Here is a modification to _action_dialog_set_username_password.tpl that provides password complexity enforcement based on an admin config for module mod_admin_identity with key password_regex that auto-degrades to a simple presence check:
{% if m.config.mod_admin_identity.password_regex.value %}
{% validate id="new_password" type={presence} type={format pattern=m.config.mod_admin_identity.password_regex.value} %}
{% else %}
{% validate id="new_password" type={presence} %}
{% endif %}

Expense of django query

I have two tables: Actor and Images. The Images table is linked to the Actor table through a Foreign Key. I might make a query on Actor that pulls a list of all the actors with first name beginning with the letter A. Let's say I store this query result in actor_list and pass it to the template. I can get a list of all the Images through the template by iterating. Something like:
{% for actor in actor_list %}
{% for image in actor.img_set %}
{{ image }}
{% endfor %}
{% endfor %}
Is this costly to the database? Does the associated img_set come through with the initial actor object list, or does a new query hit the database every time I call for the actor.img_set?
You should install the Django debug toolbar and see what SQL queries are happening under the covers.
If you are getting too many queries, you can try using select_related() to pull in the related data in the initial query.
Better than using the django toolbar is using the connections library. With Debug set to True:
>>> from django.db import connection
>>> for actor in actor_list:
>>> for image in actor.img_set:
>>> image
>>> connection.queries()
This will show all the queries that were executed and the amount of time that each took.
The debug toolbar is cool, though, for many uses. :-)