def check_status_shutdown_error_test(self):
        (osf, outfile) = tempfile.mkstemp()
        os.close(osf)
        dir = os.path.expanduser("~/.cloudinitd/")
        conf_file = self.plan_basedir + "/terminate/top.conf"
        cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
        cb.start()
        cb.block_until_complete(poll_period=1.0)
        runname = cb.run_name
        svc = cb.get_service("sampleservice")
        p = svc.shutdown()
        rc = p.poll()
        while not rc:
            rc = p.poll()
            time.sleep(0.1)

        if 'CLOUDINITD_TESTENV' in os.environ:
            bkfab = os.environ['CLOUDINITD_FAB']
            bkssh = os.environ['CLOUDINITD_SSH']
            os.environ['CLOUDINITD_FAB'] = "/bin/false"
            os.environ['CLOUDINITD_SSH'] = "/bin/false"

        rc = cloudinitd.cli.boot.main(["-O", outfile, "-v","-v","-v","-v", "status", runname])
        if 'CLOUDINITD_TESTENV' in os.environ:
            os.environ['CLOUDINITD_FAB'] = bkfab
            os.environ['CLOUDINITD_SSH'] = bkssh
        self._dump_output(outfile)
        n = "ERROR"
        line = self._find_str(outfile, n)
        self.assertNotEqual(line, None)

        rc = cloudinitd.cli.boot.main(["-O", outfile, "terminate",  "%s" % (runname)])
        self.assertEqual(rc, 0)
    def check_repair_error_test(self):
        if 'CLOUDINITD_TESTENV' in os.environ:
            # we cannot run this one in fake mode yet
            return
        (osf, outfile) = tempfile.mkstemp()
        os.close(osf)
        dir = os.path.expanduser("~/.cloudinitd/")
        conf_file = "%s/outputdep/top.conf" % (cloudinitd.nosetests.g_plans_dir)
        cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
        cb.start()
        cb.block_until_complete(poll_period=1.0)
        runname = cb.run_name
        svc = cb.get_service("onelvl1")

        secret = svc.get_attr_from_bag('iaas_secret')
        key = svc.get_attr_from_bag('iaas_key')
        iaas_url= svc.get_attr_from_bag('iaas_url')
        instance_id = svc.get_attr_from_bag('instance_id')
        con = iaas_get_con(svc._svc, key=key, secret=secret, iaasurl=iaas_url)
        instance = con.find_instance(instance_id)
        instance.terminate()

        print "start repair"
        rc = cloudinitd.cli.boot.main(["-O", outfile, "-v","-v","-v","repair", runname])
        self._dump_output(outfile)
        n = "ERROR"
        line = self._find_str(outfile, n)
        self.assertNotEqual(line, None)

        print "start terminate"
        rc = cloudinitd.cli.boot.main(["terminate",  "%s" % (runname)])
        self.assertEqual(rc, 0)
    def test_validateiaas(self):
        self.plan_basedir = cloudinitd.nosetests.g_plans_dir
        dir = tempfile.mkdtemp()
        conf_file = self.plan_basedir + "/iaastypevalidate/top.conf"
        cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
        cb.start()

        cb.block_until_complete(poll_period=1.0)

        # check the log for a warning
        fname = os.path.expanduser("~/.cloudinitd/%s/badsvc.log" % (cb.run_name))
        print fname
        self.assertTrue(os.path.exists(fname), "The path %s should exist" % (fname))
        f = open(fname, "r")
        found = False
        for l in f.readlines():
            print l
            ndx = l.find("WARN")
            if ndx >= 0:
                ndx = l.find("2.7")
                if ndx >= 0:
                    found = True
        self.assertTrue(found, "a warning with the key 2.7 should be in the logfile %s" %(fname))
        f.close()

        cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False)
        cb.shutdown()
        cb.block_until_complete(poll_period=1.0)
        fname = cb.get_db_file()
        os.remove(fname)
Beispiel #4
0
def reboot(options, args):
    """
    Reboot an already booted plan.  You must supply the run name of the booted plan.
    """
    if len(args) < 2:
        print "The reboot command requires a run name.  See --help"
        return 1
    dbname = args[1]
    cb = CloudInitD(options.database, db_name=dbname, log_level=options.loglevel, level_callback=level_callback, service_callback=service_callback, logdir=options.logdir, terminate=True, boot=False, ready=False, continue_on_error=True)
    print_chars(1, "Rebooting %s\n" % (cb.run_name))
    cb.shutdown()
    try:
        try:
            print_chars(1, "Terminating all services %s\n" % (cb.run_name))
            options.logger.info("Terminating all services")
            cb.block_until_complete(poll_period=0.1)
            options.logger.info("Starting services back up")
            cb = CloudInitD(options.database, db_name=dbname, log_level=options.loglevel, level_callback=level_callback, service_callback=service_callback, logdir=options.logdir, terminate=False, boot=True, ready=True, continue_on_error=False)
            print_chars(1, "Booting all services %s\n" % (cb.run_name))
            cb.start()
            cb.block_until_complete(poll_period=0.1)
            return 0
        except CloudServiceException, svcex:
            print svcex
        except MultilevelException, mex:
            print mex
    def _start_one(self, conf_file):

        dir = tempfile.mkdtemp()
        conf_file = self.plan_basedir + "/" + conf_file + "/top.conf"
        cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
        cb.start()
        cb.block_until_complete(poll_period=1.0)
        return (dir, cb)
 def test_poll_to_soon_error(self):
     self.plan_basedir = self.plan_basedir = cloudinitd.nosetests.g_plans_dir
     dir = tempfile.mkdtemp()
     conf_file = self.plan_basedir + "/simplelevels/top.conf"
     cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
     try:
         cb.block_until_complete(poll_period=1.0)
         self.fail("exception should have been thrown")
     except APIUsageException, ex:
         pass
def load(p, c, m, run_name, cloudinitd_dbdir, silent=False, terminate=False, wholerun=True):
    """Load any EPU related instances from a local cloudinit.d launch with the same run name.
    """
    
    try:
        cb = CloudInitD(cloudinitd_dbdir, db_name=run_name, terminate=terminate, boot=False, ready=False)
        cb.start()
        cb.block_until_complete()
    except APIUsageException, e:
        raise IncompatibleEnvironment("Problem loading records from cloudinit.d: %s" % str(e))
 def test_manyservices_one_vm_simple(self):
     self.plan_basedir = cloudinitd.nosetests.g_plans_dir
     dir = tempfile.mkdtemp()
     conf_file = self.plan_basedir + "/singlevmmanyservice/top.conf"
     cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
     cb.start()
     cb.block_until_complete(poll_period=1.0)
     cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False)
     cb.shutdown()
     cb.block_until_complete(poll_period=1.0)
 def test_badlevel_bootpgm(self):
     ilist_1 = self._get_running_vms()
     count1 = len(ilist_1)
     self.plan_basedir = cloudinitd.nosetests.g_plans_dir
     dir = tempfile.mkdtemp()
     conf_file = self.plan_basedir + "/badlevel2/top.conf"
     pass_ex = True
     cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
     try:
         cb.start()
         cb.block_until_complete(poll_period=1.0)
     except Exception, ex:
         pass_ex = True
Beispiel #10
0
    def _start_one(self, conf_file):
        self.plan_basedir = cloudinitd.nosetests.g_plans_dir
        dir = tempfile.mkdtemp()
        conf_file = self.plan_basedir + "/" + conf_file + "/top.conf"
        cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
        cb.start()
        cb.block_until_complete(poll_period=1.0)

        cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False)
        cb.shutdown()
        cb.block_until_complete(poll_period=1.0)
        fname = cb.get_db_file()
        os.remove(fname)
Beispiel #11
0
    def get_status_test(self):
        self.plan_basedir = cloudinitd.nosetests.g_plans_dir
        dir = tempfile.mkdtemp()
        conf_file = self.plan_basedir + "/oneservice/top.conf"
        cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
        cb.start()
        cb.block_until_complete(poll_period=1.0)

        svc = cb.get_service("sampleservice")
        status = svc.get_iaas_status()
        self.assertEqual("running", status, "status is %s" % (status))

        cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False)
        cb.shutdown()
        cb.block_until_complete(poll_period=1.0)
        fname = cb.get_db_file()
        os.remove(fname)
Beispiel #12
0
def _status(options, args):
    global g_repair

    dbname = args[1]
    c_on_e = not g_repair
    options.name = dbname

    cb = CloudInitD(options.database, db_name=dbname, log_level=options.loglevel, level_callback=level_callback, service_callback=service_callback, logdir=options.logdir, terminate=False, boot=False, ready=True, continue_on_error=c_on_e)
    print_chars(1, "Checking status on %s\n" % (cb.run_name))
    cb.start()
    try:
        try:
            cb.block_until_complete(poll_period=0.1)
        except CloudServiceException, svcex:
            print svcex
            return 1
        except MultilevelException, mex:
            print mex
            return 1
Beispiel #13
0
    def dep_keys_test(self):
        self.plan_basedir = cloudinitd.nosetests.g_plans_dir
        dir = tempfile.mkdtemp()
        conf_file = self.plan_basedir + "/oneservice/top.conf"
        cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
        cb.start()
        cb.block_until_complete(poll_period=1.0)

        svc = cb.get_service("sampleservice")
        attr_keys = svc.get_keys_from_bag()
        # until we figure out how to get __dict__ values from the sqlalchemy objects this will be in complete
        expectations = [
            "hostname",
            "instance_id",
#            "name",
#            "level_id",
#            "image",
#            "iaas",
#            "allocation",
#            "keyname",
#            "localkey",
#            "username",
#            "scp_username",
#            "readypgm",
#            "hostname",
#            "bootconf",
#            "bootpgm",
#            "instance_id",
#            "iaas_url",
#            "iaas_key",
#            "iaas_secret",
#            "contextualized",
#            "securitygroups",
            "webmessage"
            ]
        for e in expectations:
            self.assertTrue(e in attr_keys, "The key %s should exist in %s" % (e, str(attr_keys)))

        cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False)
        cb.shutdown()
        cb.block_until_complete(poll_period=1.0)
        fname = cb.get_db_file()
        os.remove(fname)
Beispiel #14
0
    def test_only_one_launched(self):
        if 'CLOUDINITD_CLEAN_ACCOUNT' not in os.environ:
            raise SkipTest()
        ilist_1 = self._get_running_vms()
        count1 = len(ilist_1)
        self.plan_basedir = cloudinitd.nosetests.g_plans_dir
        dir = tempfile.mkdtemp()
        conf_file = self.plan_basedir + "/singlevmmanyservice/top.conf"
        cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
        cb.start()
        cb.block_until_complete(poll_period=1.0)

        ilist_2 = self._get_running_vms()
        count2 = len(ilist_2)

        self.assertEqual(count1, count2 - 1, "there should be exactly 1 more VM in count 2: %d %d" % (count1, count2))

        cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False)
        cb.shutdown()
        cb.block_until_complete(poll_period=1.0)
    def test_env_set(self):
        if cloudinitd.nosetests.is_a_test():
            raise SkipTest()
        dir = tempfile.mkdtemp()
        conf_file = self.plan_basedir + "/oneservice/top.conf"
        cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
        cb.start()
        cb.block_until_complete(poll_period=1.0)
        svc = cb.get_service("sampleservice")
        ssh_cmd = svc.get_ssh_command() + " ls -l %s" % (self.remote_dir)

        # form test directory command
        print ssh_cmd

        rc = os.system(ssh_cmd)
        self.assertEquals(rc, 0)

        cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False)
        cb.shutdown()
        cb.block_until_complete(poll_period=1.0)
Beispiel #16
0
def terminate(options, args):
    """
    Terminate an already booted plan.  You must supply the run name of the booted plan.
    """
    if len(args) < 2:
        print "The terminate command requires a run name.  See --help"
        return 1

    for dbname in args[1:]:
        options.name = dbname
        rc = 0
        try:
            cb = CloudInitD(options.database, log_level=options.loglevel, db_name=dbname, level_callback=level_callback, service_callback=service_callback, logdir=options.logdir, terminate=True, boot=False, ready=False, continue_on_error=True)
            print_chars(1, "Terminating %s\n" % (cb.run_name))
            cb.shutdown()

            cb.block_until_complete(poll_period=0.1)
            if not options.noclean:
                path = "%s/cloudinitd-%s.db" % (options.database, dbname)
                if not os.path.exists(path):
                    raise Exception("That DB does not seem to exist: %s" % (path))
                if not options.safeclean or (cb.get_exception() is None and not cb.get_all_exceptions()):
                    print_chars(1, "Deleting the db file %s\n" % (path))
                    os.remove(path)
                else:
                    print_chars(4, "There were errors when terminating %s, keeping db\n" % (cb.run_name))

            ex = cb.get_exception()
            if ex is None:
                ex_list = cb.get_all_exceptions()
                if ex_list:
                    ex = ex_list[-1]
            if ex is not None:
                print_chars(4, "An error occured %s" % (str(ex)))
                raise ex
        except CloudServiceException, svcex:
            print svcex
            rc = 1
        except Exception, mex:
            rc = 1
    def check_status_error_test(self):
        (osf, outfile) = tempfile.mkstemp()
        os.close(osf)
        dir = os.path.expanduser("~/.cloudinitd/")
        conf_file = self.plan_basedir + "/terminate/top.conf"
        cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
        cb.start()
        cb.block_until_complete(poll_period=1.0)
        runname = cb.run_name
        svc = cb.get_service("sampleservice")

        secret = svc.get_attr_from_bag('iaas_secret')
        key = svc.get_attr_from_bag('iaas_key')
        iaas_url = svc.get_attr_from_bag('iaas_url')
        instance_id = svc.get_attr_from_bag('instance_id')
        con = iaas_get_con(None, key=key, secret=secret, iaasurl=iaas_url)
        instance = con.find_instance(instance_id)
        instance.terminate()

        if 'CLOUDINITD_TESTENV' in os.environ:
            bkfab = os.environ['CLOUDINITD_FAB']
            bkssh = os.environ['CLOUDINITD_SSH']
            os.environ['CLOUDINITD_FAB'] = "/bin/false"
            os.environ['CLOUDINITD_SSH'] = "/bin/false"

        rc = cloudinitd.cli.boot.main(["-O", outfile, "-v","-v","-v","-v", "status", runname])
        if 'CLOUDINITD_TESTENV' in os.environ:
            os.environ['CLOUDINITD_FAB'] = bkfab
            os.environ['CLOUDINITD_SSH'] = bkssh
        self._dump_output(outfile)
        n = "ERROR"
        line = self._find_str(outfile, n)
        self.assertNotEqual(line, None)

        rc = cloudinitd.cli.boot.main(["-O", outfile, "terminate",  "%s" % (runname)])
        if 'CLOUDINITD_TESTENV' in os.environ:
            # in fake mode we cannot detect that an instance was killed
            self.assertEqual(rc, 0)
        else:
            self.assertNotEqual(rc, 0)
    def test_prelaunch(self):

        key = None
        secret = None
        url = None
        try:
            key = os.environ['CLOUDINITD_IAAS_ACCESS_KEY']
            secret = os.environ['CLOUDINITD_IAAS_SECRET_KEY']
            url = os.environ['CLOUDINITD_IAAS_URL']
        except:
            pass

        # XXX this test may fail for nimbus
        con = cloudinitd.cb_iaas.iaas_get_con(None, key=key, secret=secret, iaasurl=url)
        i_list = con.get_all_instances()
        conf_file = "multilevelsimple"
        self.plan_basedir = cloudinitd.nosetests.g_plans_dir
        dir = tempfile.mkdtemp()
        conf_file = self.plan_basedir + "/" + conf_file + "/top.conf"
        cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
        cb.pre_start_iaas()

        post_boot_list = con.get_all_instances()

        self.assertNotEqual(len(i_list), len(post_boot_list), "The list of instances should have grown")
        self.assertTrue(len(i_list)+3 < len(post_boot_list), "The list of instances should have grown by more than the number of services in the first level")

        cb.start()
        post_start_list = con.get_all_instances()
        self.assertEqual(len(post_boot_list), len(post_start_list), "The list should not have grown")
        cb.block_until_complete(poll_period=1.0)

        cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False)
        cb.shutdown()
        cb.block_until_complete(poll_period=1.0)
        fname = cb.get_db_file()
        os.remove(fname)
 def _status(self, dir, run_name):
     cb = CloudInitD(dir, db_name=run_name, terminate=False, boot=False, ready=True, continue_on_error=True)
     cb.start()
     cb.block_until_complete(poll_period=1.0)
    def test_badlevel_creds(self):
        ilist_1 = self._get_running_vms()
        count1 = len(ilist_1)
        self.plan_basedir = cloudinitd.nosetests.g_plans_dir
        dir = tempfile.mkdtemp()
        conf_file = self.plan_basedir + "/badlevel2.2/top.conf"

        pass_ex = True
        cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True)
        try:
            cb.start()
            cb.block_until_complete(poll_period=1.0)
        except Exception, ex:
            pass_ex = True
        self.assertTrue(pass_ex, "An exception should have happened and didn't")

        cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False)
        cb.shutdown()
        cb.block_until_complete(poll_period=1.0)

        time.sleep(5)
        ilist_2 = self._get_running_vms()
        count2 = len(ilist_2)

        self.assertEqual(count1, count2, "the vm count before and after should be the same: %d %d" % (count1, count2))


if __name__ == '__main__':
    unittest.main()
 def _terminate(self, dir, run_name):
     cb = CloudInitD(dir, db_name=run_name, terminate=True, boot=False, ready=False, continue_on_error=True)
     cb.shutdown()
     cb.block_until_complete(poll_period=1.0)
     fname = cb.get_db_file()
     os.remove(fname)