Git repository of Karesansui.
Revisión | e13cbe977e145c8707335992f94522a6f767681f (tree) |
---|---|
Tiempo | 2011-06-01 18:43:38 |
Autor | Junichi Shinohara <junichi@kare...> |
Commiter | Junichi Shinohara |
bugfix - rollback is not working
@@ -1,4 +1,4 @@ | ||
1 | -#!/usr/bin/env python | |
1 | +#!/usr/bin/python | |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | # |
4 | 4 | # This file is part of Karesansui. |
@@ -19,6 +19,7 @@ import time | ||
19 | 19 | import signal |
20 | 20 | import logging |
21 | 21 | from optparse import OptionParser |
22 | +import tempfile | |
22 | 23 | |
23 | 24 | from ksscommand import KssCommand, KssCommandException, KssCommandOptException |
24 | 25 |
@@ -99,8 +100,6 @@ class WriteConf(KssCommand): | ||
99 | 100 | chkopts(opts) |
100 | 101 | self.up_progress(1) |
101 | 102 | |
102 | - uniq_id = time.strftime("%Y%m%d%H%M%S", time.localtime()) | |
103 | - | |
104 | 103 | if opts.pre_command is not None: |
105 | 104 | if opts.pre_command[0:4] == "b64:": |
106 | 105 | command = base64_decode(opts.pre_command[4:]) |
@@ -120,68 +119,72 @@ class WriteConf(KssCommand): | ||
120 | 119 | source_files = [] |
121 | 120 | retval = True |
122 | 121 | cnt = 0 |
123 | - for _mod in modules: | |
124 | - _file = files[cnt] | |
122 | + try: | |
125 | 123 | try: |
126 | - exec("from karesansui.lib.parser.%s import %sParser as Parser" % (_mod,_mod,)) | |
127 | - | |
128 | - self.up_progress(5) | |
129 | - parser = Parser() | |
130 | - | |
131 | - # 辞書オペレータに追加 | |
132 | - self.up_progress(5) | |
133 | - if opts.php is True: | |
134 | - conf_arr = php_array_to_python_dict(open(_file).read()) | |
135 | - else: | |
136 | - exec("conf_arr = %s" % open(_file).read()) | |
137 | - dop.addconf(_mod,conf_arr) | |
138 | - | |
139 | - """ | |
140 | - 必要ならここで配列操作 | |
141 | - 通常は、配列操作後の辞書が_fileに書き込まれているので必要ない | |
142 | - dop.add (_mod,"foo","bar") | |
143 | - dop.delete(_mod,"foo") | |
144 | - """ | |
145 | - | |
146 | - # 設定ファイル一覧に作成(バックアップ用) | |
147 | - self.up_progress(5) | |
148 | - source_file = parser.source_file() | |
149 | - for _afile in source_file: | |
150 | - _bak_afile = "%s.%s" % (_afile,uniq_id) | |
151 | - copy_file(_afile,_bak_afile) | |
152 | - source_files = source_files + source_file | |
153 | - | |
154 | - # 辞書に戻す | |
155 | - self.up_progress(5) | |
156 | - conf_arr = dop.getconf(_mod) | |
157 | - #dop.preprint_r(_mod) | |
158 | - | |
159 | - # 設定ファイルに書き込み | |
160 | - self.up_progress(5) | |
161 | - extra_args = {} | |
162 | - extra_args["include"] = opts.include | |
163 | - if opts.early_exit is True: | |
164 | - retval = retval and parser.write_conf(conf_arr,extra_args=extra_args) | |
165 | - else: | |
166 | - retval = parser.write_conf(conf_arr,extra_args=extra_args) and retval | |
167 | - | |
168 | - if opts.delete is True: | |
169 | - os.unlink(_file) | |
170 | - | |
171 | - finally: | |
172 | - cnt = cnt + 1 | |
173 | - | |
174 | - if retval is False: | |
175 | - for _afile in source_files: | |
176 | - _bak_afile = "%s.%s" % (_afile,uniq_id) | |
177 | - os.unlink(_afile) | |
178 | - copy_file(_bak_afile,_afile) | |
124 | + for _mod in modules: | |
125 | + _file = files[cnt] | |
126 | + exec("from karesansui.lib.parser.%s import %sParser as Parser" % (_mod,_mod,)) | |
127 | + | |
128 | + self.up_progress(5) | |
129 | + parser = Parser() | |
130 | + | |
131 | + # 辞書オペレータに追加 | |
132 | + self.up_progress(5) | |
133 | + if opts.php is True: | |
134 | + conf_arr = php_array_to_python_dict(open(_file).read()) | |
135 | + else: | |
136 | + exec("conf_arr = %s" % open(_file).read()) | |
137 | + dop.addconf(_mod,conf_arr) | |
138 | + | |
139 | + """ | |
140 | + 必要ならここで配列操作 | |
141 | + 通常は、配列操作後の辞書が_fileに書き込まれているので必要ない | |
142 | + dop.add (_mod,"foo","bar") | |
143 | + dop.delete(_mod,"foo") | |
144 | + """ | |
145 | + | |
146 | + # 設定ファイル一覧に作成(バックアップ用) | |
147 | + self.up_progress(5) | |
148 | + source_file = parser.source_file() | |
149 | + for _afile in source_file: | |
150 | + file_prefix = "%s." % _afile | |
151 | + file_dirname = os.path.dirname(_afile) | |
152 | + _bak_afile = tempfile.mkstemp(prefix=file_prefix, dir=file_dirname)[1] | |
153 | + copy_file(_afile,_bak_afile) | |
154 | + source_files.append((_afile, _bak_afile)) | |
155 | + | |
156 | + # 辞書に戻す | |
157 | + self.up_progress(5) | |
158 | + conf_arr = dop.getconf(_mod) | |
159 | + #dop.preprint_r(_mod) | |
160 | + | |
161 | + # 設定ファイルに書き込み | |
162 | + self.up_progress(5) | |
163 | + extra_args = {} | |
164 | + extra_args["include"] = opts.include | |
165 | + if opts.early_exit is True: | |
166 | + retval = retval and parser.write_conf(conf_arr,extra_args=extra_args) | |
167 | + else: | |
168 | + retval = parser.write_conf(conf_arr,extra_args=extra_args) and retval | |
169 | + | |
170 | + if retval is False: | |
171 | + raise KssCommandOptException("ERROR: %s write configure failure" % _mod) | |
172 | + | |
173 | + if opts.delete is True: | |
174 | + os.unlink(_file) | |
175 | + | |
176 | + cnt = cnt + 1 | |
177 | + except: | |
178 | + # 例外ありで、バックアップファイルを使って設定の切り戻し | |
179 | + for _afile, _bak_afile in source_files: | |
180 | + os.unlink(_afile) | |
181 | + copy_file(_bak_afile,_afile) | |
182 | + self.logger.warning("rollback write conf - %s to %s" % (_bak_afile, _afile)) | |
183 | + raise | |
184 | + finally: | |
185 | + # 例外なしで、バックアップファイルを全て削除 | |
186 | + for _afile, _bak_afile in source_files: | |
179 | 187 | os.unlink(_bak_afile) |
180 | - raise KssCommandOptException("ERROR: write configure failure") | |
181 | - | |
182 | - for _afile in source_files: | |
183 | - _bak_afile = "%s.%s" % (_afile,uniq_id) | |
184 | - os.unlink(_bak_afile) | |
185 | 188 | |
186 | 189 | if opts.post_command is not None: |
187 | 190 | if opts.post_command[0:4] == "b64:": |