Example of hostname #1

Merged
merged 2 commits into from Jan 3, 2017
Jump to file or symbol
Failed to load files and symbols.
+22 −2
Split
View
@@ -0,0 +1,6 @@
+options:
+ hostname:
+ type: string
+ default:
+ description: Override hostname of machine, when empty uses default machine hostname
+
View
@@ -1,6 +1,8 @@
-from charms.reactive import when_not, set_state
-from charmhelpers.core.hookenv import status_set, application_version_set
+import subprocess
+
+from charms.reactive import when, when_not, set_state
+from charmhelpers.core.hookenv import status_set, application_version_set, config
from charmhelpers.core.host import lsb_release
@@ -9,3 +11,15 @@ def status_ubuntu():
status_set('active', 'ready')
application_version_set(lsb_release()['DISTRIB_RELEASE'])
set_state('ubuntu.ready')
+
+
+@when('config.changed.hostname')
+def update_hostname():
+ hostname = config().get('hostname')
+ if not hostname:
+ return
+
+ with open('/etc/hostname', 'w') as f:
+ f.write(hostname)
+
+ subprocess.check_call(['hostname', hostname])