def validate_hooks(client, unit_state, hook_names): # Assemble a list of valid hooks for the charm. valid_hooks = ["start", "stop", "install", "config-changed"] service_manager = ServiceStateManager(client) endpoints = yield service_manager.get_relation_endpoints( unit_state.service_name) endpoint_names = [ep.relation_name for ep in endpoints] for endpoint_name in endpoint_names: valid_hooks.extend([ endpoint_name + "-relation-joined", endpoint_name + "-relation-changed", endpoint_name + "-relation-departed", endpoint_name + "-relation-broken", ]) # Verify the debug names. for hook_name in hook_names: if hook_name in valid_hooks: continue break else: returnValue(True) # We dereference to the charm to give a fully qualified error # message. I wish this was a little easier to dereference, the # service_manager.get_relation_endpoints effectively does this # already. service_manager = ServiceStateManager(client) service_state = yield service_manager.get_service_state( unit_state.service_name) charm_id = yield service_state.get_charm_id() charm_manager = CharmStateManager(client) charm = yield charm_manager.get_charm_state(charm_id) raise InvalidCharmHook(charm.id, hook_name)
def get_local_service(self): """Return ServiceState for the local service.""" if self._service is None: service_state_manager = ServiceStateManager(self._client) self._service = yield (service_state_manager.get_service_state( parse_service_name(self._unit_name))) returnValue(self._service)
def add_relation(env_config, environment, verbose, log, *descriptors): """Add relation between relation endpoints described by `descriptors`""" provider = environment.get_machine_provider() client = yield provider.connect() relation_state_manager = RelationStateManager(client) service_state_manager = ServiceStateManager(client) endpoint_pairs = yield service_state_manager.join_descriptors(*descriptors) if verbose: log.info("Endpoint pairs: %s", endpoint_pairs) if len(endpoint_pairs) == 0: raise NoMatchingEndpoints() elif len(endpoint_pairs) > 1: raise AmbiguousRelation(descriptors, endpoint_pairs) # At this point we just have one endpoint pair. We need to pick # just one of the endpoints if it's a peer endpoint, since that's # our current API - join descriptors takes two descriptors, but # add_relation_state takes one or two endpoints. TODO consider # refactoring. endpoints = endpoint_pairs[0] if endpoints[0] == endpoints[1]: endpoints = endpoints[0:1] yield relation_state_manager.add_relation_state(*endpoints) yield client.close() log.info("Added %s relation to all service units.", endpoints[0].relation_type)
def config_set(environment, service_name, service_options): """Set service options. """ provider = environment.get_machine_provider() client = yield provider.connect() # Get the service and the charm # service_manager = ServiceStateManager(client) service = yield service_manager.get_service_state(service_name) charm = yield service.get_charm_state() # Use the charm's ConfigOptions instance to validate the # arguments to config_set. Invalid options passed to this method # will thrown an exception. options = parse_keyvalue_pairs(service_options) config = yield charm.get_config() # ignore the output of validate, we run it so it might throw an exception config.validate(options) # Apply the change state = yield service.get_config() state.update(options) yield state.write()
def __init__(self, client, is_running, provider): """Initialize a Firewall Manager. :param client: A connected zookeeper client. :param is_running: A function (usually a bound method) that returns whether the associated agent is still running or not. :param provider: A machine provider, used for making the actual changes in the environment to firewall settings. """ self.machine_state_manager = MachineStateManager(client) self.service_state_manager = ServiceStateManager(client) self.is_running = is_running self.provider = provider # Track all currently watched machines, using machine ID. self._watched_machines = set() # Map service name to either NotExposed or set of exposed unit names. # If a service name is present in the dictionary, it means its # respective expose node is being watched. self._watched_services = {} # Machines to retry open_close_ports because of earlier errors self._retry_machines_on_port_error = set() # Registration of observers for corresponding actions self._open_close_ports_observers = set() self._open_close_ports_on_machine_observers = set()
def setUp(self): yield super(MachineStateManagerTest, self).setUp() self.charm_state_manager = CharmStateManager(self.client) self.machine_state_manager = MachineStateManager(self.client) self.service_state_manager = ServiceStateManager(self.client) self.charm_state = yield self.charm_state_manager.add_charm_state( local_charm_id(self.charm), self.charm, "")
def destroy_service(config, environment, verbose, log, service_name): provider = environment.get_machine_provider() client = yield provider.connect() service_manager = ServiceStateManager(client) service_state = yield service_manager.get_service_state(service_name) yield service_manager.remove_service_state(service_state) log.info("Service %r destroyed.", service_state.service_name)
def setUp(self): yield super(MachineAgentTest, self).setUp() self.output = self.capture_logging("juju.agents.machine", level=logging.DEBUG) config = self.get_test_environment_config() environment = config.get_default() # Store the environment to zookeeper environment_state_manager = EnvironmentStateManager(self.client) yield environment_state_manager.set_config_state(config, "myfirstenv") # Load the environment with the charm state and charm binary self.provider = environment.get_machine_provider() self.storage = self.provider.get_file_storage() self.charm = CharmDirectory(self.sample_dir1) self.publisher = CharmPublisher(self.client, self.storage) yield self.publisher.add_charm(local_charm_id(self.charm), self.charm) charm_states = yield self.publisher.publish() self.charm_state = charm_states[0] # Create a service from the charm from which we can create units for # the machine. self.service_state_manager = ServiceStateManager(self.client) self.service = yield self.service_state_manager.add_service_state( "fatality-blog", self.charm_state)
def test_deploy_no_service_name_short_charm_name(self): """Uses charm name as service name if possible.""" environment = self.config.get("firstenv") yield deploy.deploy(self.config, environment, self.unbundled_repo_path, "local:sample", None, logging.getLogger("deploy")) service = yield ServiceStateManager( self.client).get_service_state("sample") self.assertEqual(service.service_name, "sample")
def setUp(self): yield super(FirewallTestBase, self).setUp() self._running = True self.environment = yield self.get_test_environment() self.provider = self.environment.get_machine_provider() self.firewall_manager = FirewallManager(self.client, self.is_running, self.provider) self.service_state_manager = ServiceStateManager(self.client) self.output = self.capture_logging(level=logging.DEBUG)
def start(self): """Start the unit agent process.""" self.service_state_manager = ServiceStateManager(self.client) self.executor.start() # Retrieve our unit and configure working directories. service_name = self.unit_name.split("/")[0] service_state = yield self.service_state_manager.get_service_state( service_name) self.unit_state = yield service_state.get_unit_state(self.unit_name) self.unit_directory = os.path.join( self.config["juju_directory"], "units", self.unit_state.unit_name.replace("/", "-")) self.state_directory = os.path.join(self.config["juju_directory"], "state") # Setup the server portion of the cli api exposed to hooks. from twisted.internet import reactor self.api_socket = reactor.listenUNIX( os.path.join(self.unit_directory, HOOK_SOCKET_FILE), self.api_factory) # Setup the unit state's address address = yield get_unit_address(self.client) yield self.unit_state.set_public_address( (yield address.get_public_address())) yield self.unit_state.set_private_address( (yield address.get_private_address())) # Inform the system, we're alive. yield self.unit_state.connect_agent() self.lifecycle = UnitLifecycle(self.client, self.unit_state, service_state, self.unit_directory, self.executor) self.workflow = UnitWorkflowState(self.client, self.unit_state, self.lifecycle, self.state_directory) if self.get_watch_enabled(): yield self.unit_state.watch_resolved(self.cb_watch_resolved) yield self.unit_state.watch_hook_debug(self.cb_watch_hook_debug) yield service_state.watch_config_state( self.cb_watch_config_changed) # Fire initial transitions, only if successful if (yield self.workflow.transition_state("installed")): yield self.workflow.transition_state("started") # Upgrade can only be processed if we're in a running state so # for case of a newly started unit, do it after the unit is started. if self.get_watch_enabled(): yield self.unit_state.watch_upgrade_flag( self.cb_watch_upgrade_flag)
def test_deploy_adds_peer_relations(self): """Deploy automatically adds a peer relations.""" environment = self.config.get("firstenv") yield deploy.deploy(self.config, environment, self.unbundled_repo_path, "local:riak", None, logging.getLogger("deploy")) service_manager = ServiceStateManager(self.client) service_state = yield service_manager.get_service_state("riak") relation_manager = RelationStateManager(self.client) relations = yield relation_manager.get_relations_for_service( service_state) self.assertEqual(len(relations), 1) self.assertEqual(relations[0].relation_name, "ring")
def get_service_states(self): """Get all the services associated with this relation. @return: list of ServiceState instance associated with this relation. """ from juju.state.service import ServiceStateManager, ServiceState service_manager = ServiceStateManager(self._client) services = [] topology = yield service_manager._read_topology() for service_id in topology.get_relation_services( self.internal_relation_id): service_name = topology.get_service_name(service_id) service = ServiceState(self._client, service_id, service_name) services.append(service) returnValue(services)
def remove_unit(config, environment, verbose, log, unit_names): provider = environment.get_machine_provider() client = yield provider.connect() try: service_manager = ServiceStateManager(client) for unit_name in unit_names: service_name = parse_service_name(unit_name) service_state = yield service_manager.get_service_state( service_name) unit_state = yield service_state.get_unit_state(unit_name) yield service_state.remove_unit_state(unit_state) log.info("Unit %r removed from service %r", unit_state.unit_name, service_state.service_name) finally: yield client.close()
def expose(config, environment, verbose, log, service_name): """Expose a service.""" provider = environment.get_machine_provider() client = yield provider.connect() try: service_manager = ServiceStateManager(client) service_state = yield service_manager.get_service_state(service_name) already_exposed = yield service_state.get_exposed_flag() if not already_exposed: yield service_state.set_exposed_flag() log.info("Service %r was exposed.", service_name) else: log.info("Service %r was already exposed.", service_name) finally: yield client.close()
def get_ip_address_for_unit(client, provider, unit_name): """Returns public DNS name and unit state for the service unit. :param client: a connected zookeeper client. :param provider: the `MachineProvider` in charge of the juju. :param unit_name: service unit running on a machine to connect to. :return: tuple of the DNS name and a `MachineState`. :raises: :class:`juju.state.errors.ServiceUnitStateMachineNotAssigned` """ manager = ServiceStateManager(client) service_unit = yield manager.get_unit_state(unit_name) machine_id = yield service_unit.get_assigned_machine_id() if machine_id is None: raise ServiceUnitStateMachineNotAssigned(unit_name) returnValue( ((yield service_unit.get_public_address()), service_unit))
def add_unit(config, environment, verbose, log, service_name, num_units): """Add a unit of a service to the environment. """ provider = environment.get_machine_provider() placement_policy = provider.get_placement_policy() client = yield provider.connect() try: service_manager = ServiceStateManager(client) service_state = yield service_manager.get_service_state(service_name) for i in range(num_units): unit_state = yield service_state.add_unit_state() yield place_unit(client, placement_policy, unit_state) log.info("Unit %r added to service %r", unit_state.unit_name, service_state.service_name) finally: yield client.close()
def test_deploy_with_default_config(self): """Valid config options should be available to the deployed service.""" environment = self.config.get("firstenv") # Here we explictly pass no config file but the services # associated config.yaml defines default which we expect to # find anyway. yield deploy.deploy(self.config, environment, self.unbundled_repo_path, "local:dummy", "myblog", logging.getLogger("deploy"), None) # Verify that options in the yaml are available as state after # the deploy call (successfully applied) service = yield ServiceStateManager( self.client).get_service_state("myblog") config = yield service.get_config() self.assertEqual(config["title"], "My Title")
def config_get(environment, service_name, display_schema): """Get service settings. """ provider = environment.get_machine_provider() client = yield provider.connect() # Get the service service_manager = ServiceStateManager(client) service = yield service_manager.get_service_state(service_name) # Retrieve schema charm = yield service.get_charm_state() schema = yield charm.get_config() schema_dict = schema.as_dict() display_dict = { "service": service.service_name, "charm": (yield service.get_charm_id()), "settings": schema_dict } # Get current settings settings = yield service.get_config() settings = dict(settings.items()) # Merge current settings into schema/display dict for k, v in schema_dict.items(): if k in settings: v['value'] = settings[k] # Defaults are confusing inline display to the value for longer # strings. And there already the current value, if one wasn't set. # We do defaults for upgraded config values, which haven't been # set yet. if 'default' in v: del v['default'] else: v['value'] = "-Not set-" print yaml.safe_dump(display_dict, indent=4, default_flow_style=False, width=80, allow_unicode=True) client.close()
def test_deploy_with_config(self): """Valid config options should be available to the deployed service.""" config_file = self.makeFile( yaml.dump(dict(myblog=dict(outlook="sunny", username="******")))) environment = self.config.get("firstenv") yield deploy.deploy(self.config, environment, self.unbundled_repo_path, "local:dummy", "myblog", logging.getLogger("deploy"), config_file) # Verify that options in the yaml are available as state after # the deploy call (successfully applied) service = yield ServiceStateManager( self.client).get_service_state("myblog") config = yield service.get_config() self.assertEqual(config["outlook"], "sunny") self.assertEqual(config["username"], "tester01") # a default value from the config.yaml self.assertEqual(config["title"], "My Title")
def start(self): """Start the machine agent. Creates state directories on the machine, retrieves the machine state, and enables watch on assigned units. """ # Initialize directory paths. if not os.path.exists(self.charms_directory): os.makedirs(self.charms_directory) if not os.path.exists(self.units_directory): os.makedirs(self.units_directory) if not os.path.exists(self.unit_state_directory): os.makedirs(self.unit_state_directory) # Get state managers we'll be utilizing. self.service_state_manager = ServiceStateManager(self.client) self.charm_state_manager = CharmStateManager(self.client) # Retrieve the machine state for the machine we represent. machine_manager = MachineStateManager(self.client) self.machine_state = yield machine_manager.get_machine_state( self.get_machine_id()) # Watch assigned units for the machine. if self.get_watch_enabled(): self.machine_state.watch_assigned_units(self.watch_service_units) # Find out what provided the machine, and how to deploy units. settings = GlobalSettingsStateManager(self.client) self.provider_type = yield settings.get_provider_type() self.deploy_factory = get_deploy_factory(self.provider_type) # Connect the machine agent, broadcasting presence to the world. yield self.machine_state.connect_agent() log.info( "Machine agent started id:%s deploy:%r provider:%r" % (self.get_machine_id(), self.deploy_factory, self.provider_type))
def collect(scope, machine_provider, client, log): """Extract status information into nested dicts for rendering. `scope`: an optional list of name specifiers. Globbing based wildcards supported. Defaults to all units, services and relations. `machine_provider`: machine provider for the environment `client`: ZK client connection `log`: a Python stdlib logger. """ service_manager = ServiceStateManager(client) relation_manager = RelationStateManager(client) machine_manager = MachineStateManager(client) charm_manager = CharmStateManager(client) service_data = {} machine_data = {} state = dict(services=service_data, machines=machine_data) seen_machines = set() filter_services, filter_units = digest_scope(scope) services = yield service_manager.get_all_service_states() for service in services: if len(filter_services): found = False for filter_service in filter_services: if fnmatch(service.service_name, filter_service): found = True break if not found: continue unit_data = {} relation_data = {} charm_id = yield service.get_charm_id() charm = yield charm_manager.get_charm_state(charm_id) service_data[service.service_name] = dict(units=unit_data, charm=charm.id, relations=relation_data) exposed = yield service.get_exposed_flag() if exposed: service_data[service.service_name].update(exposed=exposed) units = yield service.get_all_unit_states() unit_matched = False relations = yield relation_manager.get_relations_for_service(service) for unit in units: if len(filter_units): found = False for filter_unit in filter_units: if fnmatch(unit.unit_name, filter_unit): found = True break if not found: continue u = unit_data[unit.unit_name] = dict() machine_id = yield unit.get_assigned_machine_id() u["machine"] = machine_id unit_workflow_client = WorkflowStateClient(client, unit) unit_state = yield unit_workflow_client.get_state() if not unit_state: u["state"] = "pending" else: unit_connected = yield unit.has_agent() u["state"] = unit_state if unit_connected else "down" if exposed: open_ports = yield unit.get_open_ports() u["open-ports"] = [ "{port}/{proto}".format(**port_info) for port_info in open_ports ] u["public-address"] = yield unit.get_public_address() # indicate we should include information about this # machine later seen_machines.add(machine_id) unit_matched = True # collect info on each relation for the service unit relation_status = {} for relation in relations: try: relation_unit = yield relation.get_unit_state(unit) except UnitRelationStateNotFound: # This exception will occur when relations are # established between services without service # units, and therefore never have any # corresponding service relation units. This # scenario does not occur in actual deployments, # but can happen in test circumstances. In # particular, it will happen with a misconfigured # provider, which exercises this codepath. continue # should not occur, but status should not fail relation_workflow_client = WorkflowStateClient( client, relation_unit) relation_workflow_state = \ yield relation_workflow_client.get_state() relation_status[relation.relation_name] = dict( state=relation_workflow_state) u["relations"] = relation_status # after filtering units check if any matched or remove the # service from the output if filter_units and not unit_matched: del service_data[service.service_name] continue for relation in relations: rel_services = yield relation.get_service_states() # A single related service implies a peer relation. More # imply a bi-directional provides/requires relationship. # In the later case we omit the local side of the relation # when reporting. if len(rel_services) > 1: # Filter out self from multi-service relations. rel_services = [ rsn for rsn in rel_services if rsn.service_name != service.service_name ] if len(rel_services) > 1: raise ValueError("Unexpected relationship with more " "than 2 endpoints") rel_service = rel_services[0] relation_data[relation.relation_name] = rel_service.service_name machines = yield machine_manager.get_all_machine_states() for machine_state in machines: if (filter_services or filter_units) and \ machine_state.id not in seen_machines: continue instance_id = yield machine_state.get_instance_id() m = {"instance-id": instance_id \ if instance_id is not None else "pending"} if instance_id is not None: try: pm = yield machine_provider.get_machine(instance_id) m["dns-name"] = pm.dns_name m["instance-state"] = pm.state if (yield machine_state.has_agent()): # if the agent's connected, we're fine m["state"] = "running" else: units = (yield machine_state.get_all_service_unit_states()) for unit in units: unit_workflow_client = WorkflowStateClient( client, unit) if (yield unit_workflow_client.get_state()): # for unit to have a state, its agent must have # run, which implies the machine agent must have # been running correctly at some point in the past m["state"] = "down" break else: # otherwise we're probably just still waiting m["state"] = "not-started" except ProviderError: # The provider doesn't have machine information log.error("Machine provider information missing: machine %s" % (machine_state.id)) machine_data[machine_state.id] = m returnValue(state)
def upgrade_charm( config, environment, verbose, log, repository_path, service_name, dry_run): """Upgrades a service's charm. First determines if an upgrade is available, then updates the service charm reference, and marks the units as needing upgrades. """ provider = environment.get_machine_provider() client = yield provider.connect() service_manager = ServiceStateManager(client) service_state = yield service_manager.get_service_state(service_name) old_charm_id = yield service_state.get_charm_id() old_charm_url = CharmURL.parse(old_charm_id) old_charm_url.assert_revision() repo, charm_url = resolve( str(old_charm_url.with_revision(None)), repository_path, environment.default_series) new_charm_url = charm_url.with_revision( (yield repo.latest(charm_url))) if charm_url.collection.schema == "local": if old_charm_url.revision >= new_charm_url.revision: new_revision = old_charm_url.revision + 1 charm = yield repo.find(new_charm_url) if isinstance(charm, CharmDirectory): if dry_run: log.info("%s would be set to revision %s", charm.path, new_revision) else: log.info("Setting %s to revision %s", charm.path, new_revision) charm.set_revision(new_revision) new_charm_url.revision = new_revision new_charm_id = str(new_charm_url) # Verify its newer than what's deployed if not new_charm_url.revision > old_charm_url.revision: if dry_run: log.info("Service already running latest charm %r", old_charm_id) else: raise NewerCharmNotFound(old_charm_id) elif dry_run: log.info("Service would be upgraded from charm %r to %r", old_charm_id, new_charm_id) # On dry run, stop before modifying state. if not dry_run: # Publish the new charm storage = provider.get_file_storage() publisher = CharmPublisher(client, storage) charm = yield repo.find(new_charm_url) yield publisher.add_charm(new_charm_id, charm) result = yield publisher.publish() charm_state = result[0] # Update the service charm reference yield service_state.set_charm_id(charm_state.id) # Mark the units for upgrades units = yield service_state.get_all_unit_states() for unit in units: running, state = yield is_unit_running(client, unit) if not running: log.info( "Unit %r is not in a running state (state: %r), won't upgrade", unit.unit_name, state or "uninitialized") continue if not dry_run: yield unit.set_upgrade_flag()
def resolved(config, environment, verbose, log, unit_name, relation_name, retry): """Mark an error as resolved in a unit or unit relation. If one of a unit's charm non-relation hooks returns a non-zero exit status, the entire unit can be considered to be in a non-running state. As a resolution, the the unit can be manually returned a running state via the juju resolved command. Optionally this command can also rerun the failed hook. This resolution also applies separately to each of the unit's relations. If one of the relation-hooks failed. In that case there is no notion of retrying (the change is gone), but resolving will allow additional relation hooks for that relation to proceed. """ provider = environment.get_machine_provider() client = yield provider.connect() service_manager = ServiceStateManager(client) relation_manager = RelationStateManager(client) unit_state = yield service_manager.get_unit_state(unit_name) service_state = yield service_manager.get_service_state( unit_name.split("/")[0]) retry = retry and RETRY_HOOKS or NO_HOOKS if not relation_name: running, workflow_state = yield is_unit_running(client, unit_state) if running: log.info("Unit %r already running: %s", unit_name, workflow_state) client.close() returnValue(False) yield unit_state.set_resolved(retry) log.info("Marked unit %r as resolved", unit_name) returnValue(True) # Check for the matching relations service_relations = yield relation_manager.get_relations_for_service( service_state) service_relations = [ sr for sr in service_relations if sr.relation_name == relation_name ] if not service_relations: raise RelationStateNotFound() # Verify the relations are in need of resolution. resolved_relations = {} for service_relation in service_relations: unit_relation = yield service_relation.get_unit_state(unit_state) running, state = yield is_relation_running(client, unit_relation) if not running: resolved_relations[unit_relation.internal_relation_id] = retry if not resolved_relations: log.warning("Matched relations are all running") client.close() returnValue(False) # Mark the relations as resolved. yield unit_state.set_relation_resolved(resolved_relations) log.info("Marked unit %r relation %r as resolved", unit_name, relation_name) client.close()
def _run(self): self._log.info("Starting charm upgrade...") # Verify the workflow state workflow_state = yield self._agent.workflow.get_state() if not workflow_state in ("started", ): self._log.warning("Unit not in an upgradeable state: %s", workflow_state) # Upgrades can only be supported while the unit is # running, we clear the flag because we don't support # persistent upgrade requests across unit starts. The # upgrade request will need to be reissued, after # resolving or restarting the unit. yield self._agent.unit_state.clear_upgrade_flag() returnValue(False) # Get, check, and clear the flag. Do it first so a second upgrade # will restablish the upgrade request. upgrade_flag = yield self._agent.unit_state.get_upgrade_flag() if not upgrade_flag: self._log.warning("No upgrade flag set.") returnValue(False) self._log.debug("Clearing upgrade flag.") yield self._agent.unit_state.clear_upgrade_flag() # Retrieve the service state service_state_manager = ServiceStateManager(self._agent.client) service_state = yield service_state_manager.get_service_state( self._agent.unit_name.split("/")[0]) # Verify unit state, upgrade flag, and newer version requested. service_charm_id = yield service_state.get_charm_id() unit_charm_id = yield self._agent.unit_state.get_charm_id() if service_charm_id == unit_charm_id: self._log.debug("Unit already running latest charm") yield self._agent.unit_state.clear_upgrade_flag() returnValue(True) # Retrieve charm self._log.debug("Retrieving charm %s", service_charm_id) charm = yield self.retrieve_charm(service_charm_id) # Stop hook executions self._log.debug("Stopping hook execution.") yield self._agent.executor.stop() # Note the current charm version self._log.debug("Setting unit charm id to %s", service_charm_id) yield self._agent.unit_state.set_charm_id(service_charm_id) # Extract charm self._log.debug("Extracting new charm.") charm.extract_to(os.path.join(self._agent.unit_directory, "charm")) # Upgrade self._log.debug("Invoking upgrade transition.") success = yield self._agent.workflow.fire_transition("upgrade_charm") if success: self._log.debug("Unit upgraded.") else: self._log.warning("Upgrade failed.") returnValue(success)
def get_local_unit_state(self): """Return ServiceUnitState for the local service unit.""" service_state_manager = ServiceStateManager(self._client) unit_state = yield service_state_manager.get_unit_state( self._unit_name) returnValue(unit_state)