Repository navigation
Description
When using git as a backend and the environment is not explicitly specified, salt may select an environment/branch other than the base/master. I've specifically seen this with state.show_top and state.highstate.
Here's the (truncated) top file for the main branch:
root@SALT:/srv/salt# cat top.sls
base:
'*':
- salt
- users
- log
- ssh
- snmp
One of the servers (it's affecting all servers)
root@SERVER1:~# salt-call state.show_top
[INFO ] Configuration file path: /etc/salt/minion
[INFO ] Loading fresh modules for state activity
[INFO ] Fetching file 'salt://top.sls'
[INFO ] Fetching file 'salt://top.sls'
[INFO ] Fetching file 'salt://top.sls'
[INFO ] Fetching file 'salt://top.sls'
[INFO ] Fetching file 'salt://top.sls'
[INFO ] Fetching file 'salt://top.sls'
[INFO ] Fetching file 'salt://top.sls'
base:
----------
*:
- salt
- users
- log
[truncated]
It's missing the last two states, ssh and snmp. It seems to be picking up the top.sls file from an env/branch other than the base/master. It matched two branches, so I removed both of them from git and tested it again. It picked a different branch this time, still not the master. It's not picking the newest branch or the earliest in the alphabet, I'm unsure how it decides what to use.
Here are select parts of the salt master conf file:
file_roots:
base:
- /srv/salt
fileserver_backend:
#- roots
- git
gitfs_remotes:
- git://[servername]/salt
pillar_roots:
base:
- /srv/pillar
Running state.sls with env=branchname works just fine and uses the environment specified.
I'm using Salt 0.15.1, but I'm reasonably sure I've seen similar behavior in earlier versions. The master is on Ubuntu 12.04 LTS with git 1.7.9.5 and python-git 0.1.6.
Original mailing list post here
https://groups.google.com/d/msg/salt-users/HMQ1sZxB2sY/Ylc6uFuhpGQJ
Activity
basepi commentedon Jun 7, 2013
Thanks for the report. By any chance do you know which branch it's choosing? Want to see if it's alphabetical or what.
xcorvis commentedon Jun 7, 2013
First it was either choosing "more_users" or "vmi-openstack", and after I deleted those it decided on "db-newiptables'. If it was alphabetical (or reverse) it should have picked different ones. "master" is smack in the middle of the pack. It's also clearly not choosing the most recent or oldest branches.
basepi commentedon Jun 7, 2013
Very strange. Thanks for the report.
xcorvis commentedon Jun 14, 2013
I've been messing with this more. I made sure that I am using python-git 0.3.2-RC1.
On a different minion I've tried totally removing the minion and all config/cache files, and reinstalling, and the top file it reports (show_top) does not match any existing top.sls in any remote branches. I've even cleared /var/cache/salt/master/gitfs on the master just so it wouldn't have old branches. It appears to be mashing separate top files together - I see combinations of states that I know have never existed in the same top.sls file.
Please let me know if there is more troubleshooting I can do or more info I can gather, this is becoming a major issue for us.
xcorvis commentedon Jun 14, 2013
I did also verify that the top file it is generating is the one it appears to be trying to apply - the states actually applied (or skipped) match the ones in the "mashup" top file.
basepi commentedon Jun 14, 2013
Wow, that's definitely no good. We'll try to make this higher priority.
xcorvis commentedon Jun 17, 2013
It looks like the mashup top file is being generated such that every section of the top file that exists in any branch is being included, and one version of the section from one of the branches is being used. For instance, branch A has a section ONE and TWO, and branch B has section TWO and THREE, then the mashup file will have sections ONE, TWO and THREE, and section TWO will either be from branch A or branch B.
We were able to trim the repo down to just the master branch, and the top file did match when there was only one branch. We also determined that it is not pulling the branches alphabetically by name, commit time or hash - we can't tell how it's deciding which branch to use, but it might be because of python lists, which are not guaranteed to be in any particular order. As far as we can tell, the order the top files are pulled down in (salt-call -l debug state.show_top) is not relevant to the way the mashup file is generated.
This may or may not be related, but I notice that on non-env aware states such as show_top or highstate, every branch file is fetched. This doesn't seem correct to me. Shouldn't it only fetch the base branch? Why pull data it doesn't need?
basepi commentedon Jun 18, 2013
So I talked to Tom, and apparently all file backends are designed to combine the top files from all the environments, as top files are not environment-specific. This is expected behavior, as any state calls in the top files go under environment labels.
Does that make sense? It would be the same if you had multiple environments using file_roots -- if you had a top file in each of your file root definitions, it would combine them all, then build the environments based on that combined definition.
xcorvis commentedon Jun 18, 2013
I reviewed the environment documentation and I understand what you're saying. It doesn't work at all how I expected it to. I don't quite follow how this is desirable behavior, but in any case, it looks like we'll have to change how we work. Thanks for the help.
basepi commentedon Jun 19, 2013
Ya, it wasn't entirely intuitive to me, either, though it does make sense that the top file is environment agnostic (we define environments inside of top files).
Sorry for the confusion!
Document that top files are combined from various environments
driskell commentedon Apr 10, 2014
Hi,
Does this not mean that everything in the "How Top Files are Combined" at http://docs.saltstack.com/en/latest/ref/states/top.html is completely wrong?
I've just tried to build an entire system based on the above - and now I find out that my design is completely flawed because the documentation is completely wrong:
"The base environment's top file is processed first."
This issue seems to state this is not the case
"If, for some reason, the base environment is not configured in the base environment's top file, then the other environments will be checked in alphabetical order."
Also seems not to be the case
"For environments other than base, the top file in a given environment will be checked for a section matching the environment's name. If one is found, then it is used. Otherwise, the remaining (non-base) environments will be checked in alphabetical order."
Also seems this is all wrong.
So the question then is... If we have multiple environments that are branches in a git repository. Do we need to store top.sls completely separately in its own repository?
Based on the documentation at the above URL, we should be able to keep top.sls in the repository and the top.sls in the "base" repository will always be authoritative. But this is not happening :(
Will this be fixed so the documentation is correct, or will the documentation be changed to replace the entire section on top.sls compiling with "Only ever have one top.sls, or multiple top.sls containing unique environments. Behaviour when having multiple top.sls where environment data overlaps is undefined."?
"Undefined" just seems really bad to me... one could easily break things by introducing another top.sls in an environment with the wrong name and completely destroy production systems using modifications to a development branch - its not controllable this way :(
basepi commentedon Apr 16, 2014
@driskell Can you tell me what behavior you're seeing? I think that document is right, though there's a chance that it is incorrect for gitfs and the other backends -- the environments there may behave differently, I haven't done any extensive tests.
In general, we recommend that people don't rely on this "overwriting" behavior, as it's hard to maintain and see what's happening at a glance. It's much better to just remember that topfiles are combined across environments and try not to duplicate across environments.
driskell commentedon Apr 28, 2014
Hi @basepi
I have 4 environments, "base", "qa", "dev", "master", in that order defined in file_roots. These are branches in a git and I have my own deployment of this to the salt server (I do not use gitfs).
Each has a version of the top.sls file containing all environments (as you'd expect since it same file, just different revisions different branches)
The documentation describes that if an environment has info in top.sls in "base", it will always override all other top.sls. This is not the case - and checking the code there is no distinction between "base" and any other name, and no specific decision on ordering (and it seems alphabetical) - so in my case, even though in the top.sls and file_roots, "qa" is second down, because it is last alphabetically, that overrides all other top.sls files in all other environments.
Based on the docs, it seems to give the idea that "base" is a special name and is always authoritative. It also explicitly says that if an environment is defined in multiple top.sls files - the definition in the top.sls for that environment will be preferred. But the code seems to say otherwise.
The code seems to simply merge and overwrite, in alphabetical order. Which contradicts entirely the documentation.
My opinion is that the following should be done (I can help if you want me to attempt a PR?):
This seems how I would like it. Though maybe there are better ways of managing this? I'd just like to keep the top.sls in version control, and make it so a modification to top.sls that affects development servers can in no way affect the production servers (which it won't with above, because production will ONLY be defined by the top.sls in the production env, "base" in my case)
Thoughts?
driskell commentedon Apr 28, 2014
For reference, from http://docs.saltstack.com/en/latest/ref/states/top.html:
The following is what lead me to think "base" was a special name (it doesn't say the FIRST environment wins, it says the "base" environment wins.)
The following is the bit saying a top.sls in an environment is authoritative for that environment if it contains config for it:
Although if I'm honest, point 3 does seem to contradict point 2 (below) - I would've expected point 2 to at least make an exception for point 3 - or point 3 come first
I've just edited this multiple times, and finished now - sorry if it spammed or confused anyone
basepi commentedon Apr 28, 2014
Luckily, it only sends an e-mail on your initial submission, not edits, so no spamming occurred. =)
@driskell Could I convince you to open a new issue and insert all of this awesome information and detective work into that new issue? I think what we will want to do is bring the code in line with the documentation (I still haven't tested this myself, so I haven't verified that I see the same thing you do) if possible. I think the documentation's way is a good way, but we definitely need to get them in line with each other one way or another.