import os
import pwd
import shutil
import yaml

from charmhelpers.core import hookenv, host
from charmhelpers.core.templating import render
from charms.reactive import hook, set_state
import gobinary


@hook('install')
def install():
    config = gobinary.config()
    install_workload(config)
    set_state('%s.available' % (config['binary']))


@hook('upgrade-charm')
def upgrade():
    # TODO: get_state("go-binary.config")
    #       and compare with upgraded, remove old service if name has changed.
    config = gobinary.config()
    service = config["binary"]
    need_restart = False
    if host.service_running(service):
        need_restart = True
    if need_restart:
        host.service_stop(service)
    install_workload(config)
    if need_restart:
        host.service_start(service)


def install_workload(config):
    local_binary = os.path.join(hookenv.charm_dir(), "files", config["binary"])
    install_binary = "/usr/bin/" + config["binary"]
    args = config["args"]
    shutil.copyfile(local_binary, install_binary)
    os.chmod(install_binary, 0o755)
    render(source="upstart",
        target="/etc/init/%s.conf" % (config["binary"]),
        owner="root",
        perms=0o644,
        context={
            "ctx": config,
            "install_binary": install_binary,
        })
    set_state('go-binary.config' % (config))

@@ -176,16 +176,46 @@
 et_state
+, when, when_not, remove_state
 %0Aimport 
@@ -826,16 +826,401 @@
 vice)%0A%0A%0A
+@when('gobinary.start')%0A@when_not('gobinary.started')%0Adef start_gobinary():%0A    bin_config = gobinary.config()%0A    host.service_start(bin_config%5B'binary'%5D)%0A    set_state('gobinary.started')%0A%0A%0A@when('gobinary.started')%0A@when_not('gobinary.start')%0Adef stop_gobinary():%0A    bin_config = gobinary.config()%0A    host.service_stop(bin_config%5B'binary'%5D)%0A    remove_state('gobinary.started')%0A%0A%0A
 def inst

Move service starting to gobinary layer