• R/O
  • R/O (HTTP)
  • R/W (SSH)
  • R/W (HTTPS)

karesansui: Commit Details

Git repository of Karesansui.


Commit MetaInfo

Revisione13cbe977e145c8707335992f94522a6f767681f (tree)
Time2011-06-01 18:43:38
AuthorJunichi Shinohara <junichi@kare...>
CommiterJunichi Shinohara

Log Message

bugfix - rollback is not working

Change Summary

Diff

--- a/bin/write_conf.py
+++ b/bin/write_conf.py
@@ -1,4 +1,4 @@
1-#!/usr/bin/env python
1+#!/usr/bin/python
22 # -*- coding: utf-8 -*-
33 #
44 # This file is part of Karesansui.
@@ -19,6 +19,7 @@ import time
1919 import signal
2020 import logging
2121 from optparse import OptionParser
22+import tempfile
2223
2324 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
2425
@@ -99,8 +100,6 @@ class WriteConf(KssCommand):
99100 chkopts(opts)
100101 self.up_progress(1)
101102
102- uniq_id = time.strftime("%Y%m%d%H%M%S", time.localtime())
103-
104103 if opts.pre_command is not None:
105104 if opts.pre_command[0:4] == "b64:":
106105 command = base64_decode(opts.pre_command[4:])
@@ -120,68 +119,72 @@ class WriteConf(KssCommand):
120119 source_files = []
121120 retval = True
122121 cnt = 0
123- for _mod in modules:
124- _file = files[cnt]
122+ try:
125123 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:
179187 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)
185188
186189 if opts.post_command is not None:
187190 if opts.post_command[0:4] == "b64:":
旧リポジトリブラウザで表示