一、我是如何獲知我有多少種系統?html
cmd和state只要一執行就會自動去提取主機python
一、註冊admin組件shell
from django.contrib import admin # Register your models here. from Arya import models admin.site.register(models.Host) admin.site.register(models.HostGroup)
二、建立超級用戶進入後臺建立測試數據數據庫
python manage.py createsuperuser
三、默認操做系統類型apache
from django.db import models # Create your models here. class Host(models.Model): hostname = models.CharField(max_length=128,unique=True) key = models.TextField() status_choices = ((0,'Waiting Approval'), (1,'Accepted'), (2,'Rejected')) os_type_choices =( ('redhat','Redhat\CentOS'), ('ubuntu','Ubuntu'), ('suse','Suse'), ('windows','Windows'), )
四、判斷輸入的命令有沒有超過邊界django
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:luoahong class BaseSaltModule(object): def __init__(self,sys_argvs,db_models,settings): self.db_models = db_models self.settings = settings self.sys_argvs = sys_argvs def get_selected_os_types(self): data = {} for host in self.host_list: data[host.os_type] = [] print('--->data',data) return data def process(self): self.fetch_hosts() self.config_data_dic = self.get_selected_os_types() def require(self,*args,**kwargs): pass def fetch_hosts(self): print('--fetching hosts---') if '-h' in self.sys_argvs or '-g' in self.sys_argvs: host_list = [] if '-h' in self.sys_argvs: host_str_index = self.sys_argvs.index('-h') +1 if len(self.sys_argvs) <= host_str_index: exit("host argument must be provided after -h") else: #get the host str host_str = self.sys_argvs[host_str_index] host_str_list = host_str.split(',') host_list += self.db_models.Host.objects.filter(hostname__in=host_str_list) if '-g' in self.sys_argvs: group_str_index = self.sys_argvs.index('-g') +1 if len(self.sys_argvs) <= group_str_index: exit("group argument must be provided after -g") else: #get the group str group_str = self.sys_argvs[group_str_index] group_str_list = group_str.split(',') group_list = self.db_models.HostGroup.objects.filter(name__in=group_str_list) for group in group_list: host_list += group.hosts.select_related() self.host_list = set(host_list) return True print('----host list:', host_list) else: exit("host [-h] or group[-g] argument must be provided") def syntax_parser(self,section_name,mod_name,mod_data): print("-going to parser state data:",section_name,mod_name) for state_item in mod_data: print("\t",state_item) for key,val in state_item.items(): if hasattr(self,key): state_func = getattr(self,key) state_func(val) else: exit("Error:module [%s] has no argument [%s]" %( mod_name,key ))
apache: pkg.installed: [] service.running: - reload: True - watch: - file: /etc/httpd/conf/httpd.conf user.present: - uid: 87 #- username: alex - gid: 87 - home: /var/www/html - shell: /bin/nologin - require: - group: apache group.present: - gid: 87 - require: - pkg: apache /etc/httpd/conf/httpd.conf: file.managed: - source: salt://apache/httpd.conf - user: root - group: root - mode: 644
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:luoahong class BaseSaltModule(object): def __init__(self,sys_argvs,db_models,settings): self.db_models = db_models self.settings = settings self.sys_argvs = sys_argvs def get_selected_os_types(self): data = {} for host in self.host_list: data[host.os_type] = [] print('--->data',data) return data def process(self): self.fetch_hosts() self.config_data_dic = self.get_selected_os_types() def require(self,*args,**kwargs): pass def fetch_hosts(self): print('--fetching hosts---') if '-h' in self.sys_argvs or '-g' in self.sys_argvs: host_list = [] if '-h' in self.sys_argvs: host_str_index = self.sys_argvs.index('-h') +1 if len(self.sys_argvs) <= host_str_index: exit("host argument must be provided after -h") else: #get the host str host_str = self.sys_argvs[host_str_index] host_str_list = host_str.split(',') host_list += self.db_models.Host.objects.filter(hostname__in=host_str_list) if '-g' in self.sys_argvs: group_str_index = self.sys_argvs.index('-g') +1 if len(self.sys_argvs) <= group_str_index: exit("group argument must be provided after -g") else: #get the group str group_str = self.sys_argvs[group_str_index] group_str_list = group_str.split(',') group_list = self.db_models.HostGroup.objects.filter(name__in=group_str_list) for group in group_list: host_list += group.hosts.select_related() self.host_list = set(host_list) return True print('----host list:', host_list) else: exit("host [-h] or group[-g] argument must be provided") def syntax_parser(self,section_name,mod_name,mod_data): print("-going to parser state data:",section_name,mod_name) for state_item in mod_data: print("\t",state_item) for key,val in state_item.items(): if hasattr(self,key): state_func = getattr(self,key) state_func(val) else: exit("Error:module [%s] has no argument [%s]" %( mod_name,key ))
一、獲主機信息ubuntu
二、打印模塊詳細信息windows
打印結果截圖ide