def validate(self): if not self.path: if not self.user: raise Fail("[%s] Either path or user is required" % self) self.path = os.path.join( self.env.cookbooks.ssh.ssh_path_for_user(self.user), "authorized_keys")
def _init_cmd(self, command, expect=None): ret = subprocess.call( ["/usr/sbin/monit", command, self.resource.service_name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) if expect is not None and expect != ret: raise Fail("%r command %s for service %s failed" % (self, command, self.resource.service_name)) return ret
def ssh_path_for_user(user): env = Environment.get_instance() if env.system.os == "linux": if user == "root": return "/root/.ssh/" return "/home/%s/.ssh/" % user elif env.system.platform == "mac_os_x": return "/Users/%s/.ssh/" % user raise Fail( "Unable to determine ssh path for user %s on os %s platform %s" % (user, env.system.os, env.system.platform))
def _determine_volume(self): """Pulls the volume id from the volume_id attribute or the node data and verifies that the volume actually exists""" vol = self._find_volume(self.resource.volume_id, self.resource.name, self.resource.device) if not vol: raise Fail( "volume_id attribute not set or no volume with the given name or device found" ) return vol
def action_attach(self): vol = self._determine_volume() if vol.status == "in-use": if vol.attach_data.instance_id != self.resource.env.config.aws.instance_id: raise Fail( "Volume with id %s exists but is attached to instance %s" % (vol.id, vol.attach_data.instance_id)) else: self.log.debug("Volume is already attached") else: self._attach_volume(vol, self.resource.env.config.aws.instance_id, self.resource.device, self.resource.timeout) self.resource.updated()
def status(self): p = subprocess.Popen([self.supervisorctl_path, "status"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out = p.communicate()[0] for l in out.split('\n'): try: svc, status, info = whitespace_re.split(l.strip(), 2) service, process_name = svc.split(':') except ValueError: continue if service == self.resource.service_name: return status.strip() == "RUNNING" raise Fail("Service %s not managed by supervisor" % self.resource.service_name)
def _attach_volume(self, vol, instance_id, device, timeout): """Attaches the volume and blocks until done (or times out)""" self.log.info("Attaching %s as %s" % (vol.id, device)) vol.attach(instance_id, device) start_time = time.time() end_time = start_time + timeout if timeout else 0 attached = False # block until attached while (not timeout) or (time.time() < end_time): if vol.update() != "deleting": if vol.attachment_state() == "attached": if vol.attach_data.instance_id == instance_id: self.log.info("%s Volume is attached" % self) attached = True break else: raise Fail( "Volume is attached to instance %s instead of %s" % (vol.attach_data.instance_id, instance_id)) else: self.log.debug("Volume is %s" % vol.status) else: raise Fail("%s Volume %s no longer exists" % (self, vol.id)) time.sleep(3) # block until device is available if attached: while (not timeout) or (time.time() < end_time): if os.path.exists(self.resource.device): return time.sleep(1) raise Fail("Timed out waiting for volume attachment after %s seconds" % (time.time() - start_time))
def status(self): p = subprocess.Popen(["/usr/sbin/monit", "summary"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out = p.communicate()[0] for l in out.split('\n'): try: typ, name, status = l.strip().split(' ', 2) except ValueError: continue if typ.strip() == 'Process' and name.strip( ) == "'%s'" % self.resource.service_name: return status.strip() == "running" raise Fail("Service %s not managed by monit" % self.resource.service_name)
def get_current_status(self): p = Popen("%s freeze | grep ^%s==" % (self.pip_binary_path, self.resource.package_name), stdout=PIPE, stderr=STDOUT, shell=True) out = p.communicate()[0] res = p.wait() if res != 0: self.current_version = None else: try: self.current_version = out.split("==", 1)[1].strip() except IndexError: raise Fail( "pip could not determine installed package version.")
def install_package(name, url, creates): import os filename = url.rsplit('/', 1)[-1] dirname = filename while dirname.rsplit('.', 1)[-1] in ('gz', 'tar', 'tgz', 'bz2'): dirname = dirname.rsplit('.', 1)[0] if not dirname: raise Fail( "Unable to figure out directory name of project for URL %s" % url) Script("install-%s" % name, not_if=lambda: os.path.exists(creates), cwd="/usr/local/src", code=("wget %(url)s\n" "tar -zxvf %(filename)s\n" "cd %(dirname)s\n" "./configure && make install\n" "ldconfig\n") % dict(url=url, dirname=dirname, filename=filename))
def action_create(self): if self.resource.volume_id: raise Fail( "Cannot create a volume with a specific id (EC2 chooses volume ids)" ) vol = self._find_volume(self.resource.volume_id, self.resource.name, self.resource.device) if vol: self.log.debug("Volume %s already exists", self.resource) # self.log.debug("There is already a volume attached at device %s" % self.resource.device) # # if not self._volume_compatible_with_resource_definition(attached_volume): # raise Fail("Volume %s attached at %s but does not conform to this resource's specifications" % (attached_volume['aws_id'], attached_volume['aws_device'])) # # self.log.debug("The volume matches the resource's definition, so the volume is assumed to be already created") else: vol = self._create_volume(self.resource.snapshot_id, self.resource.size, self.resource.availability_zone, self.resource.name, self.resource.timeout) self.resource.updated()
def _create_volume(self, snapshot_id, size, availability_zone, name, timeout): """Creates a volume according to specifications and blocks until done (or times out)""" self.log.debug( "Creating volume with attributes: snapshot_id=%s size=%s availability_zone=%s name=%s timeout=%s", snapshot_id, size, availability_zone, name, timeout) if snapshot_id and not snapshot_id.startswith('snap-'): sid = self._find_snapshot(snapshot_id) if not sid and self.resource.snapshot_required: raise Fail("Unable to find snapshot with name %s" % snapshot_id) snapshot_id = sid availability_zone = availability_zone or self.resource.env.config.aws.availability_zone vol = self.ec2.create_volume(size, availability_zone, snapshot_id) self.log.info("Created new volume %s %s%s", name, vol.id, " based on %s" % snapshot_id if snapshot_id else "") start_time = time.time() end_time = start_time + timeout if timeout else 0 while (not timeout) or (time.time() < end_time): if vol.update() in ('in-use', 'available'): break time.sleep(1) if name: vol.add_tag('Name', name) try: del self.resource.env.config.aws.resources._volumes except AttributeError: pass return vol
elif ver == "9.04": apt = "deb http://packages.cloudkick.com/ubuntu jaunty main" elif ver == "8.10": apt = "deb http://packages.cloudkick.com/ubuntu intrepid main" elif ver == "8.04": apt = "deb http://packages.cloudkick.com/ubuntu hardy main" elif ver == "6.04": apt = "deb http://packages.cloudkick.com/ubuntu dapper main" elif env.system.platform == "debian": ver = env.system.lsb['release'] apt = "deb http://packages.cloudkick.com/ubuntu lucid main" # if ver == '5.0': # apt = "deb http://apt.librato.com/debian/ lenny non-free" if not apt: raise Fail("Can't find a cloudkick package for your platform/version") Execute("apt-update-cloudkick", command="apt-get update", action="nothing") Execute( "curl http://packages.cloudkick.com/cloudkick.packages.key | apt-key add -", not_if="(apt-key list | grep 'Cloudkick' > /dev/null)") File(apt_list_path, owner="root", group="root", mode=0644, content=apt + "\n", notifies=[("run", env.resources["Execute"]["apt-update-cloudkick"], True) ])
apt_list_path = '/etc/apt/sources.list.d/serverdensity.list' apt = None if env.system.platform == "ubuntu": ver = env.system.lsb['release'] apt = "deb http://www.serverdensity.com/downloads/linux/debian lenny main" # if ver == "10.04": # apt = "deb http://apt.librato.com/ubuntu/ lucid non-free" elif env.system.platform == "debian": ver = env.system.lsb['release'] apt = "deb http://www.serverdensity.com/downloads/linux/debian lenny main" # if ver == '5.0': # apt = "deb http://apt.librato.com/debian/ lenny non-free" if not apt: raise Fail("Can't find a serverdensity package for your platform/version") Execute("apt-update-serverdensity", command = "apt-get update", action = "nothing") Execute("curl http://www.serverdensity.com/downloads/boxedice-public.key | apt-key add -", not_if = "(apt-key list | grep 'Server Density' > /dev/null)") File(apt_list_path, owner = "root", group ="root", mode = 0644, content = apt+"\n", notifies = [("run", env.resources["Execute"]["apt-update-serverdensity"], True)])
apt_list_path = '/etc/apt/sources.list.d/librato.list' apt = None if env.system.platform == "ubuntu": ver = env.system.lsb['release'] if ver == "10.04": apt = "deb http://apt.librato.com/ubuntu/ lucid non-free" elif ver == "10.10": apt = "deb http://apt.librato.com/ubuntu/ maverick non-free" elif env.system.platform == "debian": ver = env.system.lsb['release'] if ver == '5.0': apt = "deb http://apt.librato.com/debian/ lenny non-free" if not apt: raise Fail("Can't find a librato package for your platform/version") Execute("apt-update-librato", command = "apt-get update", action = "nothing") Execute("curl http://apt.librato.com/packages.librato.key | apt-key add -", not_if = "(apt-key list | grep Librato > /dev/null)") File(apt_list_path, owner = "root", group ="root", mode = 0644, content = apt+"\n", notifies = [("run", env.resources["Execute"]["apt-update-librato"], True)])
from kokki import Package, Execute, File, Fail Package("erlang") apt_list_path = '/etc/apt/sources.list.d/rabbitmq.list' apt = None if env.system.platform in ("ubuntu", "debian"): apt = "deb http://www.rabbitmq.com/debian/ testing main" if not apt: raise Fail("Can't find a rabbitmq package for your platform/version") Execute("apt-update-rabbitmq", command="apt-get update", action="nothing") Execute( "curl http://www.rabbitmq.com/rabbitmq-signing-key-public.asc | apt-key add -", not_if="(apt-key list | grep rabbitmq > /dev/null)") File(apt_list_path, owner="root", group="root", mode=0644, content=apt + "\n", notifies=[("run", env.resources["Execute"]["apt-update-rabbitmq"], True)]) Package("rabbitmq-server")
from kokki import File, Template, Execute, Directory, Service, Fail, Package apt_list_path = '/etc/apt/sources.list.d/mongodb.list' apt = None if env.system.platform == "ubuntu": ver = env.system.lsb['release'] if ver in ('10.10', '10.04', '9.10', '9.04'): ver = ver.replace(".0", ".") apt = 'deb http://downloads.mongodb.org/distros/ubuntu %s 10gen' % ver elif env.system.platform == "debian": ver = env.system.lsb['release'] if ver == '5.0': apt = 'deb http://downloads.mongodb.org/distros/debian 5.0 10gen' if not apt: raise Fail("Can't find a mongodb package for your platform/version") Execute("apt-update-mongo", command="apt-get update", action="nothing") Execute("apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10", not_if="(apt-key list | grep 10gen.com > /dev/null)") File(apt_list_path, owner="root", group="root", mode=0644, content=apt + "\n", notifies=[("run", env.resources["Execute"]["apt-update-mongo"], True)]) ###
from kokki import Package, Fail if env.system.platform in ("ubuntu", "debian"): Package("postgresql-client") elif env.system.platform in ("redhat", "centos", "fedora"): Package("postgresql-devel") else: raise Fail("Unsupported platform %s for recipe postgresql.client" % env.system.platform)