Пример #1
0
    def run(self):
        t_start = time.time()
        self.parse_options()

        if len(self.args) != 2:
            print "You must specify an instance id."
            print "For example: %s [options] gpi-37a8bf17" % self.name
            return 1

        inst_id = self.args[1]

        api = API(self.opt.dir)
        (status_code, message, topology_json) = api.instance(inst_id)

        if status_code != API.STATUS_SUCCESS:
            self._print_error("Could not access instance.", message)
            return 1
        else:
            t = Topology.from_json_string(topology_json)

            if not t.domains.has_key(self.opt.domain):
                self._print_error("Could not add user", "Domain '%s' does not exist" % self.opt.domain)
                return 1

            domain = t.domains[self.opt.domain]

            user = User()
            user.set_property("id", self.opt.login)
            user.set_property("password_hash", self.opt.passwd)
            user.set_property("ssh_pkey", self.opt.ssh)
            if self.opt.admin != None:
                user.set_property("admin", self.opt.admin)
            else:
                user.set_property("admin", False)
            user.set_property("certificate", self.opt.certificate)

            domain.add_to_array("users", user)

            topology_json = t.to_json_string()

            print "Adding new user to",
            print Fore.WHITE + Style.BRIGHT + inst_id + Fore.RESET + Style.RESET_ALL + "...",
            status_code, message = api.instance_update(inst_id, topology_json, [], [])

            if status_code == API.STATUS_SUCCESS:
                print Fore.GREEN + Style.BRIGHT + "done!"

                self._set_last_gpi(inst_id)

                t_end = time.time()

                delta = t_end - t_start
                minutes = int(delta / 60)
                seconds = int(delta - (minutes * 60))
                print "Added user in " + Fore.WHITE + Style.BRIGHT + "%i minutes and %s seconds" % (minutes, seconds)
                return 0
            elif status_code == API.STATUS_FAIL:
                self._print_error("Could not update topology.", message)
                return 1
Пример #2
0
    def run(self):    
        SIGINTWatcher(self.cleanup_after_kill) 
                
        t_start = time.time()        
        self.parse_options()
                
        if len(self.args) <= 2:
            print "You must specify an instance id and at least one host."
            print "For example: %s [options] gpi-37a8bf17 simple-wn3" % self.name
            exit(1)        
        inst_id = self.args[1]
        hosts = self.args[2:]
        
        api = API(self.opt.dir)
        (status_code, message, topology_json) = api.instance(inst_id)
        
        if status_code != API.STATUS_SUCCESS:
            self._print_error("Could not access instance.", message)
            exit(1) 
        else:
            t = Topology.from_json_string(topology_json)
            
            if not t.domains.has_key(self.opt.domain):
                self._print_error("Could not remove hosts", "Domain '%s' does not exist" % self.opt.domain)
                exit(1) 
                            
            removed = []
            nodes = t.domains[self.opt.domain].nodes
            
            for host in hosts:
                if host in nodes:
                    nodes.pop(host)
                    removed.append(host)
                        
            remaining = set(removed) ^ set(hosts)
            for r in remaining:
                print Fore.YELLOW + Style.BRIGHT + "Warning" + Fore.RESET + Style.RESET_ALL + ":",
                print "Host %s does not exist." % r

            topology_json = t.to_json_string()

            if len(removed) > 0:
                print "Removing hosts %s from" % list(removed),
                print Fore.WHITE + Style.BRIGHT + inst_id + Fore.RESET + Style.RESET_ALL + "...",
                status_code, message = api.instance_update(inst_id, topology_json, [], [])
    
                if status_code == API.STATUS_SUCCESS:
                    print Fore.GREEN + Style.BRIGHT + "done!"
                    t_end = time.time()
                    
                    delta = t_end - t_start
                    minutes = int(delta / 60)
                    seconds = int(delta - (minutes * 60))
                    print "Removed hosts in " + Fore.WHITE + Style.BRIGHT + "%i minutes and %s seconds" % (minutes, seconds)
                elif status_code == API.STATUS_FAIL:
                    self._print_error("Could not update topology.", message)
                    exit(1) 
Пример #3
0
    def run(self):
        t_start = time.time()
        self.parse_options()

        if len(self.args) != 2:
            print "You must specify an instance id."
            print "For example: %s [options] gpi-37a8bf17" % self.name
            return 1

        inst_id = self.args[1]

        api = API(self.opt.dir)
        (status_code, message, topology_json) = api.instance(inst_id)

        if status_code != API.STATUS_SUCCESS:
            self._print_error("Could not access instance.", message)
            return 1
        else:
            t = Topology.from_json_string(topology_json)

            if not t.domains.has_key(self.opt.domain):
                self._print_error("Could not add host", "Domain '%s' does not exist" % self.opt.domain)
                return 1

            domain = t.domains[self.opt.domain]

            node = Node()
            node.set_property("id", self.opt.id)
            node.set_property("run_list", self.opt.runlist.split(","))
            if self.opt.depends != None:
                node.add_to_array("depends", "node:%s" % self.opt.depends)

            domain.add_to_array("nodes", (node))

            topology_json = t.to_json_string()

            print "Adding new host to",
            print Fore.WHITE + Style.BRIGHT + inst_id + Fore.RESET + Style.RESET_ALL + "...",
            status_code, message = api.instance_update(inst_id, topology_json, [], [])

            if status_code == API.STATUS_SUCCESS:
                print Fore.GREEN + Style.BRIGHT + "done!"

                self._set_last_gpi(inst_id)

                t_end = time.time()

                delta = t_end - t_start
                minutes = int(delta / 60)
                seconds = int(delta - (minutes * 60))
                print "Added host in " + Fore.WHITE + Style.BRIGHT + "%i minutes and %s seconds" % (minutes, seconds)
                return 0
            elif status_code == API.STATUS_FAIL:
                self._print_error("Could not update topology.", message)
                return 1
Пример #4
0
    def run(self):
        t_start = time.time()
        self.parse_options()

        if len(self.args) != 2:
            print "You must specify an instance id."
            print "For example: %s [options] gpi-37a8bf17" % self.name
            return 1

        inst_id = self.args[1]

        if self.opt.topology != None:
            self._check_exists_file(self.opt.topology)

            jsonfile = open(self.opt.topology)
            topology_json = jsonfile.read()
            jsonfile.close()
        else:
            topology_json = None

        if self.opt.extra_files != None:
            self._check_exists_file(self.opt.extra_files)
            extra_files = parse_extra_files_files(self.opt.extra_files)
        else:
            extra_files = []

        if self.opt.run != None:
            self._check_exists_file(self.opt.run)
            run_cmds = [l.strip() for l in open(self.opt.run).readlines()]
        else:
            run_cmds = []

        print "Updating topology of",
        print Fore.WHITE + Style.BRIGHT + inst_id + Fore.RESET + Style.RESET_ALL + "...",

        api = API(self.opt.dir)
        status_code, message = api.instance_update(inst_id, topology_json, extra_files, run_cmds)

        if status_code == API.STATUS_SUCCESS:
            print Fore.GREEN + Style.BRIGHT + "done!"

            self._set_last_gpi(inst_id)

            t_end = time.time()

            delta = t_end - t_start
            minutes = int(delta / 60)
            seconds = int(delta - (minutes * 60))
            print "Updated topology in " + Fore.WHITE + Style.BRIGHT + "%i minutes and %s seconds" % (minutes, seconds)
            return 0
        elif status_code == API.STATUS_FAIL:
            self._print_error("Could not update topology.", message)
            return 1
Пример #5
0
def check_sample_api(s):
    instances_dir = create_instances_dir()
    
    config_txt, topology_json = load_config_file(s, dummy = True)
    
    api = API(instances_dir)

    (status_code, message, inst_id) = api.instance_create(topology_json, config_txt)
    assert status_code == API.STATUS_SUCCESS
    check_instance_state(api, inst_id, Topology.STATE_NEW)
    
    (status_code, message, topologies_json) = api.instance_list(None)
    assert status_code == API.STATUS_SUCCESS
    insts = json.loads(topologies_json)
    assert len(insts) == 1
    assert insts[0]["id"] == inst_id
            
    (status_code, message, topologies_json) = api.instance_list([inst_id])
    assert status_code == API.STATUS_SUCCESS, message
    insts = json.loads(topologies_json)
    assert len(insts) == 1
    assert insts[0]["id"] == inst_id
    
    (status_code, message) = api.instance_start(inst_id, [], [])
    assert status_code == API.STATUS_SUCCESS, message
    check_instance_state(api, inst_id, Topology.STATE_RUNNING)

    (status_code, message) = api.instance_stop(inst_id)
    assert status_code == API.STATUS_SUCCESS, message
    check_instance_state(api, inst_id, Topology.STATE_STOPPED)

    (status_code, message) = api.instance_start(inst_id, [], [])
    assert status_code == API.STATUS_SUCCESS, message
    check_instance_state(api, inst_id, Topology.STATE_RUNNING)
    
    (status_code, message) = api.instance_update(inst_id, None, [], [])
    assert status_code == API.STATUS_SUCCESS, message
    check_instance_state(api, inst_id, Topology.STATE_RUNNING)    
    
    (status_code, message) = api.instance_terminate(inst_id)
    assert status_code == API.STATUS_SUCCESS, message
    check_instance_state(api, inst_id, Topology.STATE_TERMINATED)
    
    remove_instances_dir(instances_dir)
Пример #6
0
    def run(self):
        t_start = time.time()
        self.parse_options()

        if len(self.args) <= 2:
            print "You must specify an instance id and at least one host."
            print "For example: %s [options] gpi-37a8bf17 simple-wn3" % self.name
            return 1

        inst_id = self.args[1]
        users = self.args[2:]

        api = API(self.opt.dir)
        (status_code, message, topology_json) = api.instance(inst_id)

        if status_code != API.STATUS_SUCCESS:
            self._print_error("Could not access instance.", message)
            return 1
        else:
            t = Topology.from_json_string(topology_json)

            if not t.domains.has_key(self.opt.domain):
                self._print_error("Could not remove users", "Domain '%s' does not exist" % self.opt.domain)
                return 1

            removed = []
            domain_users = t.domains[self.opt.domain].users

            for user in users:
                if user in domain_users:
                    domain_users.pop(user)
                    removed.append(user)

            remaining = set(removed) ^ set(users)
            for r in remaining:
                print Fore.YELLOW + Style.BRIGHT + "Warning" + Fore.RESET + Style.RESET_ALL + ":",
                print "User %s does not exist." % r

            topology_json = t.to_json_string()

            if len(removed) > 0:
                print "Removing users %s from" % list(removed),
                print Fore.WHITE + Style.BRIGHT + inst_id + Fore.RESET + Style.RESET_ALL + "...",
                status_code, message = api.instance_update(inst_id, topology_json, [], [])

                if status_code == API.STATUS_SUCCESS:
                    print Fore.GREEN + Style.BRIGHT + "done!"

                    self._set_last_gpi(inst_id)

                    t_end = time.time()

                    delta = t_end - t_start
                    minutes = int(delta / 60)
                    seconds = int(delta - (minutes * 60))
                    print "Removed users in " + Fore.WHITE + Style.BRIGHT + "%i minutes and %s seconds" % (
                        minutes,
                        seconds,
                    )
                    return 0
                elif status_code == API.STATUS_FAIL:
                    self._print_error("Could not update topology.", message)
                    return 1