def install():
    juju_log('**********install.real')
    rsync(
        charm_dir() + '/packages/vsm-dep-repo',
        '/opt'
    )
    rsync(
        charm_dir() + '/packages/vsmrepo',
        '/opt'
    )
    rsync(
        charm_dir() + '/files/apt.conf',
        '/etc/apt'
    )
    rsync(
        charm_dir() + '/files/vsm.list',
        '/etc/apt/sources.list.d'
    )
    rsync(
        charm_dir() + '/files/vsm-dep.list',
        '/etc/apt/sources.list.d'
    )
    apt_update()
    apt_install(VSM_PACKAGES)
    juju_log('**********finished to install vsm')
    add_source(config('ceph-source'), config('ceph-key'))
    apt_update(fatal=True)
    apt_install(packages=PRE_INSTALL_PACKAGES, fatal=True)
def db_joined(relation_id=None):
    juju_log('**********shared-db-relation-joined')
    try:
        # NOTE: try to use network spaces
        host = network_get_primary_address('shared-db')
    except NotImplementedError:
        # NOTE: fallback to private-address
        host = unit_get('private-address')
    conf = config()
    relation_set(database=conf['database'],
                 username=conf['database-user'],
                 hostname=host,
                 relation_id=relation_id)
def db_changed():
    juju_log('**********shared-db-relation-changed')
    juju_log('**********CONFIGS.complete_contexts(): %s' % str(CONFIGS.complete_contexts()))
    if 'shared-db' not in CONFIGS.complete_contexts():
        juju_log('shared-db relation incomplete. Peer not ready?')
        return
    juju_log('**********CONFIGS is %s' % str(CONFIGS))
    CONFIGS.write(VSM_CONF)

    with open('/etc/manifest/server.manifest') as server_manifest:
        flag = 'token-tenant' in server_manifest.read()
    if not flag:
        subprocess.check_call(['sudo', 'service', 'vsm-agent', 'start'])
        juju_log("**********start vsm-agent")
def amqp_changed():
    juju_log('**********amqp-relation-changed')
    if 'amqp' not in CONFIGS.complete_contexts():
        juju_log('amqp relation incomplete. Peer not ready?')
        return
    juju_log('**********CONFIGS is %s' % str(CONFIGS))
    CONFIGS.write(VSM_CONF)
def agent_changed(rid=None, unit=None):
    if 'shared-db' not in CONFIGS.complete_contexts():
        juju_log('shared-db relation incomplete. Peer not ready?')
        return
    if 'amqp' not in CONFIGS.complete_contexts():
        juju_log('amqp relation incomplete. Peer not ready?')
        return

    with open('/etc/manifest/server.manifest') as server_manifest:
        flag = 'token-tenant' in server_manifest.read()

    if flag:
        rel_settings = relation_get(rid=rid, unit=unit)
        key = rel_settings.get('ssh_public_key')
        juju_log("**********key is %s" % str(key))
        if not key:
            juju_log('peer did not publish key?')
            return
        ssh_controller_key_add(key, rid=rid, unit=unit)
        host = unit_get('private-address')
        hostname = get_hostname(host)
        hostaddress = get_host_ip(host)
        juju_log("**********host is %s" % host)
        juju_log("**********hostname is %s" % hostname)
        juju_log("**********hostaddress is %s" % hostaddress)
        with open('/etc/hosts', 'a') as hosts:
            hosts.write('%s  %s' % (hostaddress, hostname) + '\n')

        token_tenant = rel_settings.get('token_tenant')
        juju_log("**********token_tenant is %s" % token_tenant)
        rsync(
            charm_dir() + '/files/server.manifest',
            '/etc/manifest/server.manifest'
        )
        c_hostaddress = rel_settings.get('hostaddress')
        juju_log("**********controller_hostaddress is %s" % c_hostaddress)
        subprocess.check_call(['sudo', 'sed', '-i', 's/^controller_ip/%s/g' % c_hostaddress,
                               '/etc/manifest/server.manifest'])
        subprocess.check_call(['sudo', 'sed', '-i', 's/token-tenant/%s/g' % token_tenant,
                               '/etc/manifest/server.manifest'])
        subprocess.check_call(['sudo', 'service', 'vsm-agent', 'stop'])
        subprocess.check_call(['sudo', 'service', 'vsm-agent', 'start'])
        subprocess.check_call(['sudo', 'service', 'vsm-physical', 'stop'])
        subprocess.check_call(['sudo', 'service', 'vsm-physical', 'start'])
        juju_log("**********start vsm-agent")
        juju_log("**********start vsm-physical")
def amqp_joined(relation_id=None):
    juju_log('**********amqp-relation-joined')
    juju_log('**********relation_id is %s' % str(relation_id))
    conf = config()
    relation_set(relation_id=relation_id,
                 username=conf['rabbit-user'], vhost=conf['rabbit-vhost'])