Align with shell type: implement onchange

This commit is contained in:
Darko Poljak 2019-10-01 08:15:01 +02:00
parent 1588476a91
commit b7af305636
2 changed files with 11 additions and 1 deletions

View file

@ -52,6 +52,7 @@ class FileType(PythonType):
create_file = False create_file = False
upload_file = False upload_file = False
set_attributes = False set_attributes = False
fire_onchange = False
code = [] code = []
if state_should == 'present' or state_should == 'exists': if state_should == 'present' or state_should == 'exists':
if source is None: if source is None:
@ -72,6 +73,7 @@ class FileType(PythonType):
self.die('Source {} does not exist'.format(source)) self.die('Source {} does not exist'.format(source))
if create_file or upload_file: if create_file or upload_file:
set_attributes = True set_attributes = True
fire_onchange = True
tempfile_template = '{}.cdist.XXXXXXXXXX'.format(destination) tempfile_template = '{}.cdist.XXXXXXXXXX'.format(destination)
destination_upload = self.run_remote( destination_upload = self.run_remote(
["mktemp", tempfile_template, ]) ["mktemp", tempfile_template, ])
@ -90,6 +92,7 @@ class FileType(PythonType):
value_is = self.get_attribute(stat_file, attribute, value_is = self.get_attribute(stat_file, attribute,
value_should) value_should)
if set_attributes or value_should != value_is: if set_attributes or value_should != value_is:
fire_onchange = True
code.append(self.set_attribute(attribute, code.append(self.set_attribute(attribute,
value_should, value_should,
destination)) destination))
@ -97,9 +100,15 @@ class FileType(PythonType):
if typeis == 'file': if typeis == 'file':
code.append('rm -f {}'.format(destination)) code.append('rm -f {}'.format(destination))
self.send_message('remove') self.send_message('remove')
fire_onchange = True
else: else:
self.die('Unknown state {}'.format(state_should)) self.die('Unknown state {}'.format(state_should))
if fire_onchange:
onchange = self.get_parameter('onchange')
if onchange:
code.append(onchange)
return "\n".join(code) return "\n".join(code)
def get_args_parser(self): def get_args_parser(self):
@ -107,7 +116,7 @@ class FileType(PythonType):
argument_default=argparse.SUPPRESS) argument_default=argparse.SUPPRESS)
parser.add_argument('--state', dest='state', action='store', parser.add_argument('--state', dest='state', action='store',
required=False, default='present') required=False, default='present')
for param in ('group', 'mode', 'owner', 'source'): for param in ('group', 'mode', 'owner', 'source', 'onchange'):
parser.add_argument('--' + param, dest=param, action='store', parser.add_argument('--' + param, dest=param, action='store',
required=False, default=None) required=False, default=None)

1
cdist/conf/type/__file_py Symbolic link
View file

@ -0,0 +1 @@
__file