def render_vhost_conf(self): template = self.env.get_template('vhost.conf.j2') for virtual_host in self.virtual_hosts: nginx_config = template.render(virtual_host=virtual_host) virtual_host_conf = os.path.join(self.nginx.files_dir, virtual_host.conf) with open(virtual_host_conf, 'w+') as dest: Logger.put("{}を生成したよー".format(virtual_host_conf)) dest.write(nginx_config.encode('utf-8'))
def process_do_maintenance(self): """ メンテナンスの設定を行う """ if self.operation.maintenance_mode: self.operation.render_maintenance_config() self.operation.cp_maintenance_config() self.operation.nginx.restart() Logger.put('メンテナンスを{}にしたよ'.format(self.operation.maintenance_mode))
def parse_json(self): init_data = {} if not isinstance(self.item['data'], str): return False try: init_data = json.loads(self.item['data']) except: Logger.put("JSONにできないやつ送られてきた!. {}".format(self.item['data'])) return False self.operation = Operation(init_data) return True
def cp(self, _from, _to): """ cp _from _to @param _from str file path @param _to str file path @return Popen#returncode, res, err """ cp = 'cp -rf {} {}'.format(_from, _to), # DEBUG Logger.put(cp) ret_code, res, err = shell_command(cp) return ret_code, res, err
def run(self): while True: try: for self.item in self.pubsub.listen(): if self.parse_json(): self.process_do_maintenance() self.process_do_changed_enable() except: import traceback Logger.put(traceback.format_exc()) finally: # コケたら1秒sleep Logger.put('Failed SUBSCRIBE') # 再度 self.pubsub.subscribe(self.channels) Logger.put('SUBSCRIBE channels {}'.format(self.channels)) time.sleep(1)
def restart(self): Logger.put("restart nginx...") return self.initd('restart')
def reload(self): Logger.put("reload nginx...") return self.initd('reload')
def render_maintenance_config(self): template = self.env.get_template('nginx.conf.j2') nginx_config = template.render(maintenance=self.maintenance_mode) with open(self.nginx.maintenance_conf, 'w+') as dest: dest.write(nginx_config.encode('utf-8')) Logger.put("maintenance.conf作成したよー")