def accept_key(self): self.__log(module='accept_key', action='start') result = self.__salt_api(path='/key/accept') if result[RETCODE] == 0 and result[STDOUT] == self.host: m_kernel = self.__get_kernel() m_ip = self.__get_ip() if len(Minion.objects.filter(name=self.host)) == 0: minion = Minion(ip=m_ip, kernel=m_kernel, name=self.host) minion.save() minion.master.add(self.master) else: masters = [ x.ip for x in Minion.objects.get(name=self.host).master.all() ] if self.master.ip in masters: Minion.objects.filter(name=self.host).update( kernel=m_kernel, ip=m_ip) else: Minion.objects.get(name=self.host).master.add( self.master_ip) result = format_result(0, 'done') else: result = format_result(1, 'failed') self.__log(module='accept_key', action='end', result=result) return result
def get(self, tgt, cmd): result = None try: self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh.connect(self.host, 22, 'root', key_filename=self.key) std_in, std_out, std_err = self.ssh.exec_command(cmd) data = std_out.read() self.ssh.close() try: tmp_result = json.loads(data) result = format_result(0, tmp_result) logger.debug('get result: %s ' % result) except Exception, e: result = format_result(1, e.message + ' ' + str(data)) except Exception, e: result = format_result(2, e.message)
def delete_key(self): self.init() self.__log(module='delete_key', action='start') if self.__salt_api(path='/key/delete')[RETCODE] == 0: if self.minion: self.minion.master.remove(self.master) self.minion.delete() self.__log(module='delete_key', action='end') return format_result(0, 'all done')
def run(self): """ exec salt command by salt :return: """ hosts = Minion.objects.filter(ip__contains=self.host_name + '#') if len(hosts) == 0: return format_result(1, 'no this ip %s' % self.host_name) if Module.objects.filter(name=self.salt_name): salt_name = Module.objects.get(name=self.salt_name).salt_name else: salt_name = self.salt_name self.__log('start', salt_name=salt_name) result = self.__handle(hosts[0].name, salt_name) if result[RETCODE] == 0: result[STDOUT] = {self.host_name: result[STDOUT][hosts[0].name]} self.__log('end', salt_name=salt_name, result=str(result)) logger.debug('result type:%s ' % type(result)) logger.debug('result:%s ' % result) return result
def get_module_name(): return format_result(0, [{ "name": x.name, "describe": x.describe } for x in Module.objects.filter(private_status=False)])