protected Location getLocation(@Nullable Collection<? extends Location> locations) { if (locations==null || locations.isEmpty()) locations = entity().getLocations(); if (locations.isEmpty()) { MachineProvisioningLocation<?> provisioner = entity().getAttribute(SoftwareProcess.PROVISIONING_LOCATION); if (provisioner!=null) locations = Arrays.<Location>asList(provisioner); } if (locations.size() != 1 || Iterables.getOnlyElement(locations)==null) throw new IllegalArgumentException("Expected one non-null location when starting "+entity()+", but given "+locations); return Iterables.getOnlyElement(locations); }
DynamicTasks.queue("stopping (machine)", new Callable<String>() { public String call() { if (entity().getAttribute(SoftwareProcess.SERVICE_STATE)==Lifecycle.STOPPED) { log.debug("Skipping stop of entity "+entity()+" when already stopped"); return "Already stopped"; } return stopAnyProvisionedMachines(); }});
protected boolean tryStopService() { if (serviceName==null) return false; int result = DynamicTasks.queue(SshEffectorTasks.ssh("/etc/init.d/"+serviceName+" stop").runAsRoot()).get(); if (0==result) return true; if (entity().getAttribute(Attributes.SERVICE_STATE)!=Lifecycle.RUNNING) return true; throw new IllegalStateException("The process for "+entity()+" appears could not be stopped (exit code "+result+" to service stop)"); }
protected boolean tryStopWindowsService() { if (windowsServiceName==null) return false; int result = DynamicTasks.queue(SshEffectorTasks.ssh("sc query \""+windowsServiceName+"\"").runAsCommand()).get(); if (0==result) return true; if (entity().getAttribute(Attributes.SERVICE_STATE)!=Lifecycle.RUNNING) return true; throw new IllegalStateException("The process for "+entity()+" appears could not be stopped (exit code "+result+" to service stop)"); }
DynamicTasks.queue("pre-stop", new Callable<String>() { public String call() { if (entity().getAttribute(SoftwareProcess.SERVICE_STATE)==Lifecycle.STOPPED) { log.debug("Skipping stop of entity "+entity()+" when already stopped"); return "Already stopped"; } entity().setAttribute(SoftwareProcess.SERVICE_STATE, Lifecycle.STOPPING); entity().setAttribute(SoftwareProcess.SERVICE_UP, false); preStopCustom(); return null; }});
protected boolean tryStopPid() { Integer pid = entity().getAttribute(Attributes.PID); if (pid==null) { if (entity().getAttribute(Attributes.SERVICE_STATE)==Lifecycle.RUNNING && pidFile==null) log.warn("No PID recorded for "+entity()+" when running, with PID file "+pidFile+"; skipping kill in "+Tasks.current()); else if (log.isDebugEnabled()) log.debug("No PID recorded for "+entity()+"; skipping ("+entity().getAttribute(Attributes.SERVICE_STATE)+" / "+pidFile+")"); return false; } // allow non-zero exit as process may have already been killed DynamicTasks.queue(SshEffectorTasks.ssh( "kill "+pid, "sleep 5", BashCommands.ok("kill -9 "+pid)).allowingNonZeroExitCode().runAsRoot()).block(); if (DynamicTasks.queue(SshEffectorTasks.isPidRunning(pid).runAsRoot()).get()) { throw new IllegalStateException("Process for "+entity()+" in "+pid+" still running after kill"); } entity().setAttribute(Attributes.PID, null); return true; }
DynamicTasks.queue("starting", new Runnable() { public void run() { // startInLocations will look up the location, and provision a machine if necessary // (if it remembered the provisioning location) entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.STARTING); startInLocations(null); DynamicTasks.waitForLast(); if (entity().getAttribute(Attributes.SERVICE_STATE) == Lifecycle.STARTING) entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.RUNNING); }}); }
MachineProvisioningLocation<MachineLocation> provisioner = entity().getAttribute(SoftwareProcess.PROVISIONING_LOCATION);
/** runs the tasks needed to start, wrapped by setting {@link Attributes#SERVICE_STATE} appropriately */ public void start(Collection<? extends Location> locations) { entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.STARTING); try { startInLocations(locations); DynamicTasks.waitForLast(); if (entity().getAttribute(Attributes.SERVICE_STATE) == Lifecycle.STARTING) entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.RUNNING); } catch (Throwable t) { entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.ON_FIRE); throw Exceptions.propagate(t); } }
}}); if (entity().getAttribute(SoftwareProcess.SERVICE_STATE)==Lifecycle.STOPPED) { return;
protected void startWithKnifeAsync() { Entities.warnOnIgnoringConfig(entity(), ChefConfig.CHEF_RUN_LIST); Entities.warnOnIgnoringConfig(entity(), ChefConfig.CHEF_LAUNCH_ATTRIBUTES); DynamicTasks.queue( ChefServerTasks .knifeConvergeRunList("postgresql::server") .knifeAddAttributes(Jsonya .at("postgresql", "config").add( "port", entity().getAttribute(PostgreSqlNode.POSTGRESQL_PORT), "listen_addresses", "*").getRootMap()) .knifeAddAttributes(Jsonya .at("postgresql", "pg_hba").list().map().add( "type", "host", "db", "all", "user", "all", "addr", "0.0.0.0/0", "method", "md5").getRootMap()) // no other arguments currenty supported; chef will pick a password for us ); } protected void postStartCustom() {