I recently read the source code of saltstack, understand the process, and view the execution process of the salt command. Think about whether you can transform the source code, so you need it. Later I thought about whether I could specify the jid when executing the salt.
Let's talk about the following requirements:
Jid of the salt command
Similar to this:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/48/6D/wKioL1QIDsTRoHWYAADPA53FEF4308.jpg "Title =" qq20140904150311.png "alt =" wkiol1qidstrohwyaadpa53fef00008.jpg "/>
Start implementation.
Step 1
Modify the salt. utils. parsers. saltcmdoptionparser class and add the command line option.
self.add_option( ‘-j‘,‘--jid‘, dest=‘jid‘, default=‘‘, help=(‘Set the job\‘s id,‘ ‘like 20140903143821048459‘) )
Step 2
Modify the salt. utils. parsers. optionparser class to determine the validity of jid.
# Call this method if hasattr (options, 'jid'): Self. _ check_jid (options. jid) return options, argS # Check jid validity def _ check_jid (self, jid = ''): Try: If jid ='': Pass Elif Len (jid )! = 20: Raise optparse. optionvalueerror ('jid needs 20 length and is numberic STR, like "20140903143821048459" ') else: Import re if not re. match (R' \ D {20} ', jid): Raise optparse. optionvalueerror ('jid needs 20 length and is numberic STR, like "20140903143821048459" ') Does T optparse. optionvalueerror, E: Print (e) sys. exit (1)
Step 3
Modify salt. cli. saltcmd class
Try: Local = salt. client. localclient (self. get_config_file_path (), self. options. jid) # modify counter t saltclienterror as exc: Self. exit (2, '{0} \ n '. format (EXC) returnif self. options. batch: Batch = salt. CLI. batch. batch (self. config, jid = self. options. jid) # modify
Step 4
Modify salt. Client. localclient class
Def _ init _ (self, c_path = OS. path. join (syspaths. config_dir, 'master'), jid = '', mopts = none): # Add the jid parameter ...... self. jid = jid # Add the property self. serial = salt. payload. serial (self. opts) def run_job (self, TGT, fun ,....): # jid = ''jid = self. jid
Step 5
Modify salt. cli. batch. Batch class
Def _ init _ (self, opts, quiet = false, jid = ''): # Add jid self. opts = opts self. quiet = quiet self. local = salt. client. localclient (OPTs ['conf _ file'], jid) # Add jid self. minions = self. _ gather_minions ()
After all the steps are completed, restart master and Minion.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/48/6C/wKiom1QIFLnBc3fIAAJi78OjehQ794.jpg "Title =" qq20140904252845.png "alt =" wkiom1qiflnbc3fiaaji78ojehq794.jpg "/>
Dangdang...
Modified successfully.
Why should we add this function, because the background management system needs to generate a jid on the Web page before executing tasks. Use this jid to query some information.
This article from the "fly World" blog, please be sure to keep this source http://liuping0906.blog.51cto.com/2516248/1548765
ID of a saltstack task