Created on July 17, 2017
section 1 textbook festival content 8minutes
Task Orchestration System Development
Introduction to Architectural ideas /implementation methods
Project implementation
Interface authentication
Extended:
What is the Python class?
The essence of template language
Lesson 2 task Orchestration System architecture 46minutes
Send a task to the machine to execute
Gitlab
GitHub
Svn
Git.oschina.net Code Cloud
the Manytomany Append columns, you can add the following in the models field:
Nane = models. Manytomany (through= ' table name ')
Table structure of a task system:
Usertype Userinfo Admin usergroup
Hoststatus Host tasktemplate TaskType
Executetype Task taskhoststatus tasklog
Section 3 Task Description background management function 25minutes
Section 4 task Description background management features 15minutes
Task background System page writing implementation
Task List for 5 task-arranging background management 10minutes
6 lesson Task Creation Background management task 46minutes
From Django import forms, Templatetagsfrom pip._vendor.requests.utils import is_valid_cidrfrom django.shortcuts Import Render_to_responsefrom CGITB Import htmlfrom django.template.backends.django import templatefrom _codecs Import register#---------froms-----------------------------------------------class Taskform (forms. Form): name = forms. Charfield (max_length=30, error_messages={' Required ': U ' task name cannot be empty '}, Widget = f Orms. TextInput (attrs={' class ': ' Form-control no radius ', ' placeholder ': U ' Task Name '})) Task_type = forms. Integerfield (error_messages={' Required ': U ' task type cannot be empty '}, Widget=forms.widgets.select ( Choices=models. TaskType.objects.all (). order_by (' id '). values_list (' id ', ' caption '), attrs={' class ': ' Form-co Ntrol no Radius '})) hosts = forms. Charfield (error_messages={' Required ': U ' task type cannot be empty '}, Widget=forms.widgets.selectmultiple (choices=models. Hosts.objects.all (). order_by (' id '). values_list (' id ', ' hostname '), attrs={' class ': ' Form-control No radius ', ' multiple ': "multiple"})) Kick_off_time = forms. Charfield (max_length=30, error_messages={' Required ': U ' execution time cannot be empty '}, WI Dget=forms. Datetimeinput (attrs={' class ': ' Form-control no radius ', ' pla Ceholder ': U ' execution time ', ' id ': ' kick_off_at '}) def __init__ (self): #每执行一次都从数据库中更新, no Each time it is the original data, the new data is not displayed #静态字段执行后会写入内存 self.fields[' hosts '].widget.choices = models. Hosts.objects.all (). order_by (' id '). values_list (' id ', ' hostname ') #------------- Views------------------------------------------------def add_task (request): Passdef Create (Request): Form_obj = task Form () if Request.method = = ' POST ': form_obj = TaSkform (Request. POST) if Form_obj.is_valid:print form_obj.clean () Raw_data = Form_obj.clean () add _task (Raw_data) #将Form的内容添加到数据库中 else: #显示错误信息 print form_obj.errors.as_data () Return Render_to_response (' index.html ', {' model ', Form_obj, ' Message ': Form_obj.errors.as_data () #--------------custom error template----------------------------------------file directory under App, Module name: templatetags, filename can be defined #form_tag:form Django import templateregister = template. Library () @register. Simple_tagdef error_message (ARG): If Arg:return arg[0][0] Else:return "' Above the module can display the first A parameter HTML using {% load Form_tag%} can then be used: {% error_message variable. Field 1%}
Section 7 Task Creation Background management task 223 minutes
Section 8 Task Creation Background management task 37 minutes
From django.db import transactiondef add_task (Request): Try:with transaction.atomic (): hosts = data[ ' Hosts '] del data[' hosts '] del data[' task_template '] data[' task_type '] = models. TaskType.objects.get (id=data[' task_type ') data[' execute_type '] = models. ExecuteType.objects.get (id=data[' execute_type ') Task_obj = models. Task.objects.create (**data) hosts = Hosts.replace ("'", ' "') hosts = Hosts.replace (' u ', ')" The hosts = ' [u ' 1 ", U" 2 ", U" 3 "] ' JSON cannot load with u characters simultaneously loads note that the table must be double quotes, outside the single quote, otherwise error >>> a ' [u ' 1 ", U" 2 ", U" 3 "] ' >>> a = A.replace (" U "," ') >&G T;> Json.loads (a) [' 1 ', ' 2 ', ' 3 '] >>> a = ' [u ' 1 ", U" 2 ", U" 3 "] ' >>> Json.loads (a) Traceback (most recent): File "<Stdin> ", line 1, in <module> File" C:\Python27\lib\json\__init__.py ", line 310, in loads return _default_decoder.decode (s) File "C:\Python27\lib\json\decoder.py", line 346, in decode obj, end = Self.raw_decode (S, idx=_w (s, 0). End ()) File "C:\Python27\lib\json\decoder. Py ", line 364, in Raw_decode raise ValueError (" No JSON object could be decoded ") Valueer Ror:no JSON object could be decoded>>> "#这里去U改成双引号后才能Loads Host_lis T = models. Host.objects.filter (Id__in=json.loads (Hosts)) for item in Host_list:models. TaskHostStatus.objects.create (Status=0,task=task_obj,host=item)
9 Lesson Task Orchestration Agent implementation Analysis minutes
#--------------------------------------------------------------------------Agentimport Jsonimport Uuidfrom Lib.plugins Import pluginapifrom lib.commons import logfrom log.core import securtyimport configimport commandsclass Prog Ram:def __init__ (self): Self.host = config.configuations[' host ' Self.port = config.configuations[' Port ' ] Self.resource = config.configuations[' resource '] self.timeout = config.configuations[' timeout '] def pro Cess (self): data = Self.get_task () #这里可以判断, the command is a script or a command, if the command is executed directly, do not need to write to the file #如果是脚本, write to the file and then execute file_name = Self.write_file (data) Retsult = Self.execute (file_name) def get_task (self): params = Urllib.urlencode ( {' Data ': json.dumps ({' hostname ': c1.salt.com})}) result = Self.url_request (params, ' GET ') return result def wirte_file (self): file_name = str (UUID.UUID ()) + .‘ Py ' F = file (file_name, ' W ') f.write (data) f.close () return file_name def execute (self,file_name): Shell_command = ' python%s '% (file_name) STATUS,OUTPU t = commands.getstatusoutput (shell_command) pirnt ' output:==========> ' Print output def url_request (s Elf,params,methos): original = None headers = {' Content-type ': "application/x-www/from-urlencoded", "A Ccept ":" Text/json "," Securtykey ": Securty.create_ai_key ()} try:conn = Httplib. Httpconnection (self.host,self.port,self.timeout) conn.request (metod,self.resource,params,headers) re Sponse = Conn.getresponse () original = Response.read () except Exception,e:log.write_error_lo G (' [htp],%s '%e) return original
Section 10 task Orchestration API validation minutes
Client
send MD5 (key+datetime) | datetime
Server
1. Receive the request, split the string (plus password, client time)
2. If the current time-client time >5s, the request fails
3.MD5 (client time +key), Generate plus password
4. Match plus password
#--------------------------------------------------------------#securty-----Generate an encrypted string Import timeimport config Import Hashlibdef Create_api_key (): Hash_obj = HASHLIB.MD5 () key = Config.configration[' key '] Time_span = Time.time () Hash_obj.updata ("%s|%f"% (Key,time_span)) encryption = Hash_obj.hexdigest () result = '%s|%f '% (Encryption,time_span) Return result #----------------views----------------------------------------------------from Django.shortcuts Import Httpresponsedef Api_auth (func): Def wrapper (request): Securty_key = Request. Meta.get (' Http_securtykey ', None) if not Securty_key:return HttpResponse (' authentication failed ') if not auth_api_ Valid (Securty_key): Return HttpResponse (' authentication failed ') return func (Request) return wrapper# MD5 the received data With Def auth_api_valid (data): try:encryption, Time_span = Data.split (' | ') Time_span = Float (time_span) if (Time.time ()-time_span) >5:return False Hash_obj = Hashlib.md5 () hash_obj.update ("%s|%f"% (Key,time_span)) if hash_obj.hexdigest () = Encryptio N:return True Else:return false except Exception,e:pass return FALSE "" here can be K ey,5 is placed in a configuration file, and then directly called, can be easily changed management "@api_auth #使用装饰器来检查是否有KEYdef Handle_server_info (request): ret = {' status ': 0, ' message ': '} return HttpResponse (Json.dumps (ret))
Lesson 11 expansion and the like what is it (top) minutes
What is the extension of lesson 12 ( II ) 2 minutes
Everything in Python is an object, and the class itself is an object, and the class is generated by type. Class Foo pass the following two ways are the same result 1. Bar = Type (' Bar ', (object,), {' name ': 123, ' Func ': Hello}) 2. Class Bar: name = 123def Hello (self): print ' Hello ' in this case, for the defined class, as long as a class is defined, the constructor of the type class is called once, how is it validated? __metaclass__ can specify Class MyClass (type) that is generated by that type: def __init__ (self,name,bases,dicts): Print Name def __call__ (self, *args, **kwargs): return type.__call__ (self, *args, **kwargs) class C1: # is an instance of MyClass __metaclass__ = Myclassclass C2: #是MyClass的实例 __metaclass__ = MYCLASSC1 = C1 () # Execute the Call method class Test (object): def __init__ (self): print ' init ' def __call__ (self): print ' call ' T1 = Test () #执行Initt1 ()
The essence of the template language for the extension of lesson 13 minutes
The template language is implemented in the following way #!usr/bin/env Python#coding:utf-8 the following name is equivalent to the background of the dictionary variable namespace = {' name ': ' Wupeiqi ', ' Data ': [ 18,73,84]}code = " def hellocute (): Return " name%s, age%d "% (Name,data[0],)" #通过以下两个命令执行上面的字符串函数func = Compile (code, ' <string> ', "exec") exec func in namespace result = Namespace[' Hellocute '] () print result
Lesson 14 Summary Minutes
Python Learning day17 Task release