<div class="gmail_quote">2009/5/11 Jean-Paul Calderone <span dir="ltr"><<a href="mailto:exarkun@divmod.com">exarkun@divmod.com</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">On Mon, 11 May 2009 16:55:40 +0200, Luigi Conte <<a href="mailto:luigiandcosolutions@gmail.com">luigiandcosolutions@gmail.com</a>> wrote:<br>
>I'm sure you're saying only good things but I'm so inexpert in twisted that<br>
>I can't understand very well what to do.<br>
>I'll try to explain all that I want to do and how I did it wrongly:<br>
>first I have to call the connect method from the api and it returns a<br>
>deferred so I do:<br>
>in conn_to_ctrl i have:<br>
>d = api.connect(...)<br>
>return d<br>
><br>
>now I do some operations with config files to call many times the start<br>
>method from the api (I have to start many virtual machines) and the<br>
>api.start returns a deferred so what have I to do now?<br>
>I do so:<br>
>in examinecfg i have:<br>
># tha same d that i used to add api.connect!<br>
>self.d.addCallback(api.start, (method_args))<br>
>return d<br>
><br>
>Then I have to call the disconnect method from the api. But i tried to print<br>
>out something to fallow the executing but I only see printing the first<br>
>method that call the connection. after that the process ends.<br>
><br>
>In the main I have:<br>
>defer = conn_to_ctrl()<br>
>or have I to add to defer also the examinecfg?<br>
><br>
>thank you very much<br>
<br>
</div>I'm not sure what you're doing wrong. It's hard to understand mixed up<br>
snippets and fragments of code. Please:<br>
<br>
- don't top post<br>
- share complete, runnable examples (preferably minimal, too)<br>
- share the failure you're seeing - possibly including a traceback, or a<br>
description of the behavior that the program exhibits when you run it,<br>
and an explanation of how this differs from what you want<br>
<div><div></div><div class="h5"><br>
Jean-Paul<br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a></div></div></blockquote><div><br>Ok I'll try to explain what the api provides to me and what I want to do with it:<br>
mothods provided by api that I want to use:<br>1) connection<br><br> def connect(self, ctrl_host, ctrl_port, user, passwd):<br> """Connect to an Usher control server.<br> @param ctrl_host: fqdn where Usher controller is running<br>
@type ctrl_host: string<br> @param ctrl_port: port number Usher controller is listening on<br> @type ctrl_port: int<br> @param user: username of user connecting to Usher controller<br> @type user: string<br>
@param passwd: password of user connecting to Usher controller<br> @type passwd: string<br> return: Deferred which returns and L{UsherResult}<br> rtype: twisted.internet.defer.Deferred<br> """<br>
# serialize access to connect<br> if not self.connect_lock.acquire(False):<br> raise UsherEx, "Connection Pending"<br> if self.factory:<br> self.disconnect()<br> # reset vms<br>
self.vms = {}<br> # reset passwd to None until we're connected<br> self.passwd = None<br> self.factory = APIFactory()<br> self.factory.api = self<br> # connect to server<br> reactor.connectSSL(ctrl_host, ctrl_port, self.factory,<br>
ssl.ClientContextFactory())<br> d = self.factory.login(credentials.UsernamePassword(user, passwd),<br> client=self)<br> d.addErrback(self._handle_err, misc.whoami())<br> d.addCallback(self._connected_to_ctrl, ctrl_host, ctrl_port, user,<br>
passwd)<br> return d<br><br>2) start single vm<br><br> def start(self, cluster=None, prefix='', vmlist=None, count=None, first=None, last=None, dregex=None, dlist=None, dryrun=False, eid=None, **kw):<br>
"""Start a VM or set of VMs<br> @param cluster: Name of cluster to which VMs should belong<br> @type cluster: string<br> @param prefix: prefix to prepend to VM names<br> @type prefix: string<br>
@param vmlist: list of strings of VMs to start<br> @type vmlist: list of strings<br> @param count: Number of consecutive VMs to start<br> @type count: int<br> @param first: Lowest number of VM to start <br>
@type first: int<br> @param last: Highest number of VM to start<br> @type last: int<br> @param dregex: regular expression to match against destination LNM names<br> @type dregex: list of strings<br>
@param dlist: list of strings to match against destination LNM names<br> @type dlist: list of strings<br> @param dryrun: Show what would have happened if the command had been run without the dryrun command (at the instant that the dryrun command was run of course)<br>
@type dryrun: bool<br> @param eid: mechanism for admin user to be able to specify an alternate username for forming suffix for regex<br> @type eid: string<br> @param kw: Extra arguments passed to controller for use by plugins or LNM. Any keywords starting with 'vm_' are added to VM's uargs parameter. Note, the controller actually checks for two kw arguments: ram and ip_addrs. Though these are, strictly speaking, VM parameters, they're considered important enough that the controller checks for them.<br>
@type kw: dict<br><br> @return: Deferred which returns an L{UsherResult}<br> @rtype: twisted.internet.defer.Deferred<br> @raise UsherNotConnected: Raised when not connected to Usher Controller<br>
@raise UsherPBDeadRefEx: Raised when capability for Usher Controller goes stale. Must reconnect when this happens<br> @raise UsherInvalidInputEx: Raised when an invalid input or combination of parameters is received. <br>
"""<br><br> # make sure we're connected<br> if not self.aref:<br> raise UsherNotConnectedEx<br> # combine these to reduce params<br> if ((first and first < 0) or (last and last < 0) or (count and count <<br>
0)):<br> raise UsherInvalidInputEx("first, last, and count must be non-negative")<br> vmrange = (first,last,count)<br> # arg check<br> self._arg_check(vmlist, vmrange)<br>
# get final destination list<br> dlist = self._namecomplete(dlist, suffix=self.suffix)<br> self._check_lnmlist(dlist)<br> dregex = self._regex_append(dregex, dlist)<br> dlist = self._regex_filter(dregex, self.lnms.keys()) <br>
# complete names (if necessary) in the vmlist<br> vmlist = self._namecomplete(vmlist, cluster, eid)<br> # check that the vms specified don't already exist self._check_vmlist(vmlist, isin=True)<br>
vmlist = self._get_vm_names_to_start(vmlist, cluster, prefix, vmrange, eid)<br> # controller handles eid from kw, so add it<br> if eid:<br> kw['eid'] = eid<br> if dryrun:<br>
d = UsherResult('dryrun', msg = 'started:' + linesep +<br> linesep.join(vmlist))<br> return d<br> try:<br> if vmlist:<br> d = self.aref.callRemote("start", vmlist, dlist, **kw)<br>
d.addErrback(self._handle_err, misc.whoami())<br> else:<br> raise UsherInvalidInputEx( "No VMs started. No unique VMs specified in command")<br> return d<br>
except pb.DeadReferenceError, ex: raise UsherPBDeadRefEx <br></div></div><br>in my script I have to use connect method and then I have to do some operations first of calling the start method<br>1) start connection<br>
<br> def startConnection(self):<br> d = my_api.connect(self.ctrl_ip, self.ctrl_port, self.user, self.pwd)<br> d.addCallback(self.postConnection)<br> d.addErrback(twisted.python.log.err)<br> print "Connection added"<br>
return d<br>2) operation before starting a virtual machine:<br>def newVMCfg(self, new_vms_cfg):<br> #...<br> #some operations<br> #if condition valid I try to start the virtual machine<br> # is this the correct way to pass args to the start method? <br>
d = self.d.addCallback(self.startVM,(new_vm, self.lnms[i])<br> print "started vm %s"%new_vm<br> return d<br><br>in the main process I call them as:<br>d = startConnection()<br>
d.addCallback(newVMCfg, arg)<br><br>Is it correct? Because the process stops at the first method called: I see only "connection added".<br>Thank you for your help!!<br>