def run(self):
        debug("run start")
        ChallengeBase.run(self)
        
        if self.check() is False:
            self.usage()
            sys.exit(-1)

        log("Creating new DNS zone %s" % self.domain_name)
        self.domain=self.dns.create(name=self.domain_name, emailAddress="*****@*****.**", ttl=300, comment="challenge9 domain")

        log("Creating cloud server ...(please wait the build can take a while)")
        self.server=self.cs.servers.create(name=self.fqdn_name, flavor=self.flavor_id, image=self.image_id)

        wait = WaitingForTask(self.check_cs_build, self.server)
        if wait.wait_for_tasks() == False: 
            log("Cloud server build failed, deleting created objects")
            self.server.delete()
            self.domain.delete()
            sys.exit(-1)

        # get all public IPs
        net = [ i["addr"] for i in self.server.addresses["public"] ] 
        self.net = utils.get_ipv4net(net)

        cname_rec = {"type": "A",
                    "name": self.fqdn_name,
                    "data": self.net,
                    "ttl": 300,
                    "comment": "challenge9 rec"}

        self.recs = self.domain.add_record(cname_rec)

        log("DNS zone %s has been udpated and a new A record created" % (self.domain_name))
        log("rec A: %s -> %s" % (self.fqdn_name, self.net))
    def build_lb(self):
        debug("build_lb start")

        for s in self.servers :
            net = [ i["addr"] for i in s.addresses["private"] ] 
            net = utils.get_ipv4net(net)

            node = self.clb.Node(address=net, port=80, condition="ENABLED")
            self.nodes.append(node)

        self.vip = self.clb.VirtualIP(type="PUBLIC")
        self.lb = self.clb.create(self.lb_name, port=80, protocol="HTTP", nodes=self.nodes, virtual_ips=[self.vip])
        self.wait_for_lb_change()

        debug("key")
        debug(self.cert_key)

        debug("crt")
        debug(self.cert)

        self.lb.add_ssl_termination(
            securePort=443,
            enabled=True,
            secureTrafficOnly=False,
            certificate=self.cert,
            privatekey=self.cert_key )

        self.wait_for_lb_change()
    def build_lb(self):
        debug("build_lb start")

        for s in self.servers :
            net = [ i["addr"] for i in s.addresses["private"] ] 
            net = utils.get_ipv4net(net)

            node = self.clb.Node(address=net, port=80, condition="ENABLED")
            self.nodes.append(node)

        self.vip = self.clb.VirtualIP(type="PUBLIC")
        self.lb = self.clb.create(self.lb_name, port=80, protocol="HTTP", nodes=self.nodes, virtual_ips=[self.vip])
    def show(self):
        debug("show start")

        log("-" * 70)
        log("vip name %s and ip %s" % (self.lb_name, self.vip_address) )
        
        for i in xrange(0, self.cs_count) :
            name= "%s-%d" % (self.server_prefix, i)
            log("cloud server %s added to pool as %s" % (name, self.nodes[i].address) )

        log("-" * 70)
        for i, s in enumerate(self.servers) :
            s.change_password(self.server_passord)
            
            net = [ a["addr"] for a in s.addresses["public"] ] 
            net = utils.get_ipv4net(net)

            priv = [ a["addr"] for a in s.addresses[self.network_name] ] 
            priv = utils.get_ipv4net(priv)

            print ("Server #%2d: ID %s name %s pub IP %s priv IP %s password %s" % (i, s.id, 
                s.name, net, priv, self.server_passord) )
    def build_dns(self):
        debug("build_dns start")

        self.domain=self.dns.create(name=self.domain_name, emailAddress="*****@*****.**", ttl=300, comment="challenge10")

        net = self.lb.virtual_ips[0].address
        self.vip_address = utils.get_ipv4net(net)

        cname_rec = {"type": "A",
                    "name": self.fqdn_name,
                    "data": self.vip_address,
                    "ttl": 300,
                    "comment": "challenge10 vip IP"}

        self.recs = self.domain.add_record(cname_rec)
    def build_lb(self):
        debug("build_lb start")

        for s in self.servers :
            net = [ i["addr"] for i in s.addresses["private"] ] 
            net = utils.get_ipv4net(net)

            node = self.clb.Node(address=net, port=80, condition="ENABLED")
            self.nodes.append(node)

        self.vip = self.clb.VirtualIP(type="PUBLIC")
        self.lb = self.clb.create(self.lb_name, port=80, protocol="HTTP", nodes=self.nodes, virtual_ips=[self.vip])
        self.wait_for_lb_change()

        self.clb.add_health_monitor(self.lb, 'CONNECT', delay=3, timeout=10, attemptsBeforeDeactivation=3)
        self.wait_for_lb_change()

        self.clb.set_error_page(self.lb, self.error_page)
        self.wait_for_lb_change()