def run(self): if self.app_result['count'] == 1: res = self.app_result['results'][0] try: port = int(res.get("port", 80)) except Exception, e: logger.error(str(e)) port = 80 warmup_urls = res['warmup_urls'] if not warmup_urls: all_status = True warm_url = '' else: warm_url = '' all_status = True for m in warmup_urls: logger.debug(m) t_result = self.check( self.host, port, 'http://%s:%s%s' % (self.item, port, m["warmup_url"]), m["expected_codes"], m["expected_text"]) # if t_result: # pass # else: if not t_result: warm_url = m all_status = False break if all_status: return get_result(0, 'done') else: raise ReleaseError('warm this url %s failed' % warm_url)
def rebuild(self): self.__valid_executing() env = self.mission.env status = Status.objects.get(content=ReleaseStatus.Undo) cmdb_list = self.__get_list(self.mission.item, env) logger.debug('cmdb_list: %s' % cmdb_list) # fixme: host location change cause rebuild bug for idc in cmdb_list: existed_list = list( set([ x.host for x in Progress.objects.filter( mission=self.mission, location=idc['location']).all() ])) for ip in [x for x in existed_list if x not in idc['list']]: Progress.objects.filter(host=ip, mission=self.mission).delete() step_list = Type_step.objects.filter(type=self.mission.type) for step in step_list: for ip in [x for x in idc['list'] if x not in existed_list]: Progress.objects.filter(~Q(location=idc['location']), host=ip, mission=self.mission).delete() Progress.objects.create(host=ip, step=step.step, dep=self.mission.dep, item=self.mission.item, item_type=self.mission.item_type, type=self.mission.type, env=env, location=idc['location'], step_order=step.order, mission=self.mission, status=status) return get_result(0, 'done!')
def post(self, data): content = urlencode(data) req = urllib2.Request(self.cmd_path, content, self.auth_header) result = urllib2.urlopen(req) result_content = result.read() logger.debug('path:%s data:%s' % (self.cmd_path, content)) logger.debug(result_content) return json.loads(result_content)
def run(self): logger.debug('%s' % self.cmd_name) self.set_ftp_info(ftp_flag=self.ftp_flag) self.set_send_data(arg_format=self.arg_format) self.set_rs_method() logger.info('send_data:%s' % self.send_data) try: result = CmdControlApi().post(self.send_data) except Exception, e: logger.error('run error: %s' % str(e)) result = e.message
def __request(self, method, path, data=None): conn = httplib.HTTPConnection(self.host, self.port) conn.connect() send_data = '' if data: send_data = json.dumps(data) print send_data conn.request(method, path, send_data, self.auth_header) logger.debug('access: %s%s data :%s %s' % (self.host, path, data, self.auth_header)) result = conn.getresponse().read() conn.close() print result return json.loads(result)
def run(self, instance_id, command): docker_instance = Docker_list.objects.get(id=instance_id) tech = docker_instance.tech.content if command == 'create': result = self._host_config(instance_id, tech) elif command == 'start': result = self._start(docker_instance.docker_id) elif command == 'stop': result = self._stop(docker_instance.docker_id) elif command == 'rm': result = self._rm_docker(docker_instance.docker_id) else: result = 'not exist command' logger.debug(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 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 _create_docker(self, instance_id, tech): ports, host_config, ports_detail = self._host_config(instance_id, tech) result = self.cli.create_container('lemonbar/centos6-ssh', ports=ports, dns='', working_dir='', host_config=host_config, name='docker-shqs-{0}'.format( instance_id, )) docker_id = result.get('Id') logger.debug(result) if self._start_docker(docker_id): aLL_docker = self.cli.containers(all=True) docker_new = [x for x in aLL_docker if x.get('Id') == docker_id][0] status = docker_new.get('status') else: status = 'unknown' Docker_list.objects.filter(id=instance_id).update(docker_id=docker_id, status=status, ports=ports_detail) return result
def __create_mission(self, roll=None): self.__get_item_and_version() if self.item_result['count'] != 0: dep = self.item_result['results'][0]['department'] item_type = self.item_result['results'][0]['type'].lower() self.__create_mission_before(item_type, roll) if roll: env = ReleaseEnv.Production else: env = self.env self.mission_id = Mission.objects.create(version=self.version, dep=dep, env=env, item=self.item, item_type=item_type, type=self.mission_type, creator=self.creator) logger.debug('mission %s created', self.mission_id) else: raise ReleaseError( 'application group does not contain this item %s' % self.item) return self.__create_mission_after()
def check(host, port, url, expected_codes, expected_text, method='GET'): conn = httplib.HTTPConnection(host, port) conn.connect() conn.request(method, url) resp = conn.getresponse() code = resp.status logger.debug('url%s ,expect_code type %s, code %s' % (url, expected_codes, code)) flag = False if expected_codes: if str(code) in [str(x) for x in expected_codes]: flag = True else: raise ReleaseError( '%s code not in %s ' % (str(code), [str(x) for x in expected_codes])) if expected_text: if str(expected_text) in resp.read(): flag = True else: raise ReleaseError('%s not found %s ' % (str(expected_text), url)) return flag