<div class="gmail_quote">2009/5/11 Jean-Paul Calderone <span dir="ltr">&lt;<a href="mailto:exarkun@divmod.com">exarkun@divmod.com</a>&gt;</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 &lt;<a href="mailto:luigiandcosolutions@gmail.com">luigiandcosolutions@gmail.com</a>&gt; wrote:<br>
&gt;I&#39;m sure you&#39;re saying only good things but I&#39;m so inexpert in twisted that<br>
&gt;I can&#39;t understand very well what to do.<br>
&gt;I&#39;ll try to explain all that I want to do and how I did it wrongly:<br>
&gt;first I have to call the connect method from the api and it returns a<br>
&gt;deferred so I do:<br>
&gt;in conn_to_ctrl i have:<br>
&gt;d = api.connect(...)<br>
&gt;return d<br>
&gt;<br>
&gt;now I do some operations with config files to call many times the start<br>
&gt;method from the api (I have to start many virtual machines) and the<br>
&gt;api.start returns a deferred so what have I to do now?<br>
&gt;I do so:<br>
&gt;in examinecfg i have:<br>
&gt;# tha same d that i used to add api.connect!<br>
&gt;self.d.addCallback(api.start, (method_args))<br>
&gt;return d<br>
&gt;<br>
&gt;Then I have to call the disconnect method from the api. But i tried to print<br>
&gt;out something to fallow the executing but I only see printing the first<br>
&gt;method that call the connection. after that the process ends.<br>
&gt;<br>
&gt;In the main I have:<br>
&gt;defer = conn_to_ctrl()<br>
&gt;or have I to add to defer also the examinecfg?<br>
&gt;<br>
&gt;thank you very much<br>
<br>
</div>I&#39;m not sure what you&#39;re doing wrong.  It&#39;s hard to understand mixed up<br>
snippets and fragments of code.  Please:<br>
<br>
  - don&#39;t top post<br>
  - share complete, runnable examples (preferably minimal, too)<br>
  - share the failure you&#39;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&#39;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>        &quot;&quot;&quot;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>        &quot;&quot;&quot;<br>
        # serialize access to connect<br>        if not self.connect_lock.acquire(False):<br>            raise UsherEx, &quot;Connection Pending&quot;<br>        if self.factory:<br>            self.disconnect()<br>        # reset vms<br>
        self.vms = {}<br>        # reset passwd to None until we&#39;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=&#39;&#39;, vmlist=None, count=None, first=None, last=None, dregex=None,  dlist=None, dryrun=False, eid=None, **kw):<br>
        &quot;&quot;&quot;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 &#39;vm_&#39; are added to VM&#39;s uargs parameter.  Note, the controller actually checks for two kw arguments: ram and ip_addrs.  Though these are, strictly speaking, VM parameters, they&#39;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>
       &quot;&quot;&quot;<br><br>        # make sure we&#39;re connected<br>        if not self.aref:<br>            raise UsherNotConnectedEx<br>        # combine these to reduce params<br>        if ((first and first &lt; 0) or (last and last &lt; 0) or (count and count &lt;<br>
            0)):<br>            raise UsherInvalidInputEx(&quot;first, last, and count must be non-negative&quot;)<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&#39;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[&#39;eid&#39;] = eid<br>        if dryrun:<br>
            d = UsherResult(&#39;dryrun&#39;, msg = &#39;started:&#39; + linesep +<br>                    linesep.join(vmlist))<br>            return d<br>        try:<br>            if vmlist:<br>                d = self.aref.callRemote(&quot;start&quot;, vmlist, dlist, **kw)<br>
                d.addErrback(self._handle_err, misc.whoami())<br>            else:<br>                raise UsherInvalidInputEx( &quot;No VMs started.  No unique VMs specified in command&quot;)<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 &quot;Connection added&quot;<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 &quot;started vm %s&quot;%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 &quot;connection added&quot;.<br>Thank you for your help!!<br>