3 # Copyright 2020 Canonical Ltd.
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
22 from ops
.charm
import CharmBase
23 from ops
.main
import main
24 from ops
.model
import ActiveStatus
26 sys
.path
.append("lib")
29 class MyNativeCharm(CharmBase
):
30 def __init__(self
, framework
, key
):
31 super().__init
__(framework
, key
)
33 # Listen to charm events
34 self
.framework
.observe(self
.on
.config_changed
, self
.on_config_changed
)
35 self
.framework
.observe(self
.on
.install
, self
.on_install
)
36 self
.framework
.observe(self
.on
.start
, self
.on_start
)
38 # Listen to the touch action event
39 self
.framework
.observe(self
.on
.touch_action
, self
.on_touch_action
)
41 def on_config_changed(self
, event
):
42 """Handle changes in configuration"""
43 self
.model
.unit
.status
= ActiveStatus()
45 def on_install(self
, event
):
46 """Called when the charm is being installed"""
47 self
.model
.unit
.status
= ActiveStatus()
49 def on_start(self
, event
):
50 """Called when the charm is being started"""
51 self
.model
.unit
.status
= ActiveStatus()
53 def on_touch_action(self
, event
):
56 filename
= event
.params
["filename"]
58 subprocess
.run(["touch", filename
], check
=True)
59 event
.set_results({"created": True, "filename": filename
})
60 except subprocess
.CalledProcessError
as e
:
61 event
.fail("Action failed: {}".format(e
))
62 self
.model
.unit
.status
= ActiveStatus()
65 if __name__
== "__main__":