<gh_stars>0
from charmhelpers.core import hookenv
from charmhelpers.core.templating import render
from charms.reactive import hook, when, set_flag, clear_flag


@hook('config-changed')
def config():
    cfg = hookenv.config()
    if cfg.changed('port') and cfg.previous('port'):
        hookenv.close_port(cfg.previous('port'))
    hookenv.open_port(cfg['port'])
    set_flag('tor.configured')


@when('tor.configured')
def update_torrc():
    cfg = hookenv.config()
    render(
        source='torrc',
        target='/etc/tor/torrc',
        owner='root',
        perms=0o644,
        context={
            'cfg': cfg,
            'public_address': hookenv.unit_public_ip(),
            'private_address': hookenv.unit_private_ip(),
        },
    )
    clear_flag('tor.configured')
    set_flag('tor.start')
    hookenv.status_set('active', 'tor bridge ready')

from charmhelpers.core import hookenv
from charmhelpers.core.templating import render
from charms.reactive import hook, when, set_flag, clear_flag


@hook('config-changed')
def config():
    cfg = hookenv.config()
    if cfg.changed('port') and cfg.previous('port'):
        hookenv.close_port(cfg.previous('port'))
    hookenv.open_port(cfg['port'])
    set_flag('tor.configured')


@when('tor.configured')
def update_torrc():
    cfg = hookenv.config()
    render(
        source='torrc',
        target='/etc/tor/torrc',
        owner='root',
        perms=0o644,
        context={
            'cfg': cfg,
            'public_address': hookenv.unit_public_ip(),
            'private_address': hookenv.unit_private_ip(),
        },
    )
    clear_flag('tor.configured')
    set_flag('tor.start')
    hookenv.status_set('active', 'tor bridge ready')