def stop_kafka_waiting_for_zookeeper_ready(): hookenv.status_set('maintenance', 'zookeeper not ready, stopping kafka') kafka = Kafka() hookenv.close_port(hookenv.config()['port']) kafka.stop() remove_state('kafka.started') hookenv.status_set('waiting', 'waiting for zookeeper to become ready')
def configure_kafka_zookeepers(zk): """Configure ready zookeepers and restart kafka if needed. As zks come and go, server.properties will be updated. When that file changes, restart Kafka and set appropriate status messages. This method also handles the restart if our network_interface config has changed. """ zks = zk.zookeepers() network_interface = hookenv.config().get('network_interface') log_dir = unitdata.kv().get('kafka.storage.log_dir') if not(any(( data_changed('zookeepers', zks), data_changed('kafka.network_interface', network_interface), data_changed('kafka.storage.log_dir', log_dir)))): return hookenv.log('Checking Zookeeper configuration') hookenv.status_set('maintenance', 'updating zookeeper instances') kafka = Kafka() kafka.configure_kafka(zks, network_interface=network_interface, log_dir=log_dir) hookenv.status_set('active', 'ready')
def reconfigure_kafka_zk_instances_leaving(zkdeparting, zkavailable): try: zk_units = zkavailable.get_zookeeper_units() hookenv.status_set('maintenance', 'Updating Kafka with departing Zookeeper instances ') kafka = Kafka(DistConfig()) kafka.configure_kafka(zk_units) kafka.restart() zkdeparting.dismiss_departing() hookenv.status_set('active', 'Ready') except: hookenv.log("Relation with Zookeeper not established. Stopping Kafka.") kafka = Kafka(DistConfig()) kafka.stop() remove_state('kafka.started') hookenv.status_set('blocked', 'Waiting for connection to Zookeeper')
def configure_kafka(zk): hookenv.status_set('maintenance', 'setting up kafka') if hookenv.config()['log_dir']: log_dir = hookenv.config()['log_dir'] else: log_dir = unitdata.kv().get('kafka.storage.log_dir') kafka = Kafka() zks = zk.zookeepers() data_changed('kafka.storage.log_dir', log_dir) data_changed('zookeepers', zks), if log_dir: kafka.install(zk_units=zks, log_dir=log_dir) else: hookenv.status_set('blocked', 'unable to get storage dir') charms.coordinator.acquire('restart')
def configure_kafka_zookeepers(zk): """Configure ready zookeepers and restart kafka if needed. As zks come and go, server.properties will be updated. When that file changes, restart Kafka and set appropriate status messages. """ zks = zk.zookeepers() log_dir = unitdata.kv().get('kafka.storage.log_dir') if not (any( (data_changed('zookeepers', zks), data_changed('kafka.storage.log_dir', log_dir)))): return hookenv.log('Checking Zookeeper configuration') hookenv.status_set('maintenance', 'updating zookeeper instances') kafka = Kafka() kafka.install(zk_units=zks, log_dir=log_dir) hookenv.status_set('active', 'ready')
def install_kafka(*args): kafka = Kafka(DistConfig()) if kafka.verify_resources(): hookenv.status_set('maintenance', 'Installing Kafka') kafka.install() kafka.open_ports() set_state('kafka.installed')
def restart(): hookenv.status_set('maintenance', 'Rolling restart') kafka = Kafka() if not kafka.is_running(): kafka.start() else: kafka.restart() hookenv.open_port(hookenv.config()['port']) # set app version string for juju status output kafka_version = kafka.version() hookenv.application_version_set(kafka_version) hookenv.status_set('active', 'ready') set_state('kafka.started')
def storage_detaching(): unitdata.kv().unset('kafka.storage.log_dir') Kafka().stop() log('log storage detatched, reconfiguring to use temporary storage') remove_state('kafka.configured') set_flag('kafka.force-reconfigure') remove_state('kafka.started') remove_state('kafka.storage.logs.attached')
def storage_detaching(): unitdata.kv().unset('kafka.storage.log_dir') kafka = Kafka() kafka.close_ports() kafka.stop() remove_state('kafka.started') hookenv.status_set('waiting', 'reconfiguring to use temporary storage') remove_state('kafka.storage.logs.attached')
def reconfigure_kafka_new_zk_instances(zkjoining, zkavailable): try: zk_units = zkavailable.get_zookeeper_units() hookenv.status_set('maintenance', 'Updating Kafka with new Zookeeper instances') kafka = Kafka(DistConfig()) kafka.configure_kafka(zk_units) kafka.restart() zkjoining.dismiss_joining() hookenv.status_set('active', 'Ready') except: hookenv.log("Relation with Zookeeper not established")
def configure_kafka(zkjoining, zkavailable): try: zk_units = zkavailable.get_zookeeper_units() hookenv.status_set('maintenance', 'Setting up Kafka') kafka = Kafka(DistConfig()) kafka.configure_kafka(zk_units) kafka.start() zkjoining.dismiss_joining() hookenv.status_set('active', 'Ready') set_state('kafka.started') except: hookenv.log("Relation with Zookeeper not established")
def configure_kafka(zk): hookenv.status_set('maintenance', 'setting up kafka') log_dir = unitdata.kv().get('kafka.storage.log_dir') data_changed('kafka.storage.log_dir', log_dir) kafka = Kafka() zks = zk.zookeepers() if log_dir: kafka.install(zk_units=zks, log_dir=log_dir) else: hookenv.status_set('blocked', 'unable to get storage dir') kafka.open_ports() set_state('kafka.started') hookenv.status_set('active', 'ready') # set app version string for juju status output kafka_version = kafka.version() hookenv.application_version_set(kafka_version)
def configure_kafka(zk): hookenv.status_set('maintenance', 'setting up kafka') data_changed( # Prime data changed for network interface 'kafka.network_interface', hookenv.config().get('network_interface')) log_dir = unitdata.kv().get('kafka.storage.log_dir') data_changed('kafka.storage.log_dir', log_dir) kafka = Kafka() zks = zk.zookeepers() kafka.configure_kafka(zks, log_dir=log_dir) kafka.open_ports() set_state('kafka.started') hookenv.status_set('active', 'ready') # set app version string for juju status output kafka_version = get_package_version(KAFKA_SNAP) or 'unknown' hookenv.application_version_set(kafka_version)
def storage_attach(): storageids = hookenv.storage_list('logs') if not storageids: hookenv.status_set('blocked', 'cannot locate attached storage') return storageid = storageids[0] mount = hookenv.storage_get('location', storageid) if not mount: hookenv.status_set('blocked', 'cannot locate attached storage mount') return log_dir = os.path.join(mount, "logs") unitdata.kv().set('kafka.storage.log_dir', log_dir) hookenv.log('Kafka logs storage attached at {}'.format(log_dir)) # Stop Kafka; removing the kafka.started state will trigger a reconfigure if/when it's ready kafka = Kafka() kafka.close_ports() kafka.stop() remove_state('kafka.started') hookenv.status_set('waiting', 'reconfiguring to use attached storage') set_state('kafka.storage.logs.attached')
def autostart_service(): ''' Attempt to restart the service if it is not running. ''' kafka = Kafka() if kafka.is_running(): hookenv.status_set('active', 'ready') return for i in range(3): hookenv.status_set( 'maintenance', 'attempting to restart kafka, ' 'attempt: {}'.format(i+1) ) kafka.restart() if kafka.is_running(): hookenv.status_set('active', 'ready') return hookenv.status_set('blocked', 'failed to start kafka; check syslog')