X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2Fcommon.git;a=blobdiff_plain;f=osm_common%2Ftests%2Fpackages%2Fnative_charm_with_metadata_dir_vnf%2FScripts%2Fcharms%2Fsimple%2Fhooks%2Finstall;fp=osm_common%2Ftests%2Fpackages%2Fnative_charm_with_metadata_dir_vnf%2FScripts%2Fcharms%2Fsimple%2Fhooks%2Finstall;h=9ef7d07286257338ea1ef940fb34f67a9052f998;hp=0000000000000000000000000000000000000000;hb=0839503b0c0fb61f256f387bfdf07419e673bbe3;hpb=277c931191194490c5595307c22b721d94294254 diff --git a/osm_common/tests/packages/native_charm_with_metadata_dir_vnf/Scripts/charms/simple/hooks/install b/osm_common/tests/packages/native_charm_with_metadata_dir_vnf/Scripts/charms/simple/hooks/install new file mode 100755 index 0000000..9ef7d07 --- /dev/null +++ b/osm_common/tests/packages/native_charm_with_metadata_dir_vnf/Scripts/charms/simple/hooks/install @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +## +# Copyright 2020 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## + +import sys +import subprocess + +sys.path.append("lib") + +from ops.charm import CharmBase +from ops.main import main +from ops.model import ActiveStatus + +class MyNativeCharm(CharmBase): + + def __init__(self, framework, key): + super().__init__(framework, key) + + # Listen to charm events + self.framework.observe(self.on.config_changed, self.on_config_changed) + self.framework.observe(self.on.install, self.on_install) + self.framework.observe(self.on.start, self.on_start) + + # Listen to the touch action event + self.framework.observe(self.on.touch_action, self.on_touch_action) + + def on_config_changed(self, event): + """Handle changes in configuration""" + self.model.unit.status = ActiveStatus() + + def on_install(self, event): + """Called when the charm is being installed""" + self.model.unit.status = ActiveStatus() + + def on_start(self, event): + """Called when the charm is being started""" + self.model.unit.status = ActiveStatus() + + def on_touch_action(self, event): + """Touch a file.""" + + filename = event.params["filename"] + try: + subprocess.run(["touch", filename], check=True) + event.set_results({"created": True, "filename": filename}) + except subprocess.CalledProcessError as e: + event.fail("Action failed: {}".format(e)) + self.model.unit.status = ActiveStatus() + + +if __name__ == "__main__": + main(MyNativeCharm) +