class DockerExecutor(): #Instantiating the docker client. def __init__(self, parsed_data): self.pack_info = parsed_data self.db =Data(constants.PACKAGE_TABLE_NAME) self.package = self.db.find(self.pack_info["id"]) self.container = None try: self.docker_cli = docker.Client(base_url="tcp://192.168.99.100:2375", version="1.18", timeout=600) except: print "Docker connection Error." print "Setting the Client for docker...." #Creating the container for executing the test suites. def create_container(self): try: print "Creating the container...." self.container = self.docker_cli.create_container(image="pyhold:1.0", tty=True) except: print "Error creating & starting the container." #Starting the container def start_container(self): try: print "Starting the container...." self.docker_cli.start(self.container) except Exception,err: print Exception, err print "Error while starting container."
class MainExecutor: def main_loop(self): def listen(ch, method, properties, body): self.datasql = Data(constants.PACKAGE_TABLE_NAME) self.rwriter = BuildLogWriter() print colored("[x] Received -- ", 'magenta') , body self.data = body self.parsed_data = json.loads(self.data) print self.parsed_data self.pack = self.datasql.find(self.parsed_data["id"]) print "PACKAGE => ",self.pack self.file_name = str(self.datasql.find(self.parsed_data["id"],select="file_name")[0][0]) self.lib_name = str(self.datasql.find(self.parsed_data["id"],select="lib_name")[0][0]) dock = DockerExecutor(self.parsed_data) dock.create_container() dock.start_container() print dock.exec_command("mkdir testex") package_endpoint = constants.PACKAGE_ENDPT + str(self.file_name) print dock.exec_command('bash -c "cd testex && wget %s"' % str(package_endpoint)) print dock.exec_command('bash -c "cd testex && tar -xvf %s"' % self.file_name) #Requirements Installation self.req_data = dock.exec_command('bash -c "cd / && cd testex && cd %s && pip install -r requirements.txt"' % self.lib_name) print self.req_data self.rwriter.write(self.lib_name, "requirements_installation.log", self.req_data) #Custom build commands self.build_commands = self.datasql.find(self.parsed_data["id"],select="commands") if(not(self.build_commands[0][0] == "")): print "BUILD COMMANDS => ", self.build_commands[0][0] commands = self.build_commands[0][0].split(",") for com in enumerate(commands): self.req_data = dock.exec_command('bash -c "cd / && cd testex && cd %s && %s"' % (self.lib_name, com[1])) print self.req_data self.rwriter.write(self.lib_name,"custom_build_" + str(com[0]) + ".log", self.req_data) #Building from source self.build_data = dock.exec_command('bash -c "cd / && cd testex && cd %s && python setup.py install"' % self.lib_name) print self.build_data self.rwriter.write(self.lib_name, "build.log", self.build_data) #Stopping the container dock.stop_container() try: config = assets.Asset() credentials = config.get_rabbit_credentials() rabbit_credential = pika.credentials.PlainCredentials(credentials["user_name"], credentials["password"]) connection = pika.BlockingConnection(pika.ConnectionParameters( host=constants.PYHOLD_HOST,credentials=rabbit_credential)) self.channel = connection.channel() self.channel.queue_declare(queue='pyhold_mq_stream') self.channel.basic_consume(listen, queue='pyhold_mq_stream', no_ack=True) od.oline() print ">> Starting Rabbit (AMQP) Listener.", "\t", colored("[SUCCESS]","green") print ">> Listening..." od.oline() self.channel.start_consuming() except: print colored("Error while binding/connecting to Rabbit Server / " "Keyboard Interrupt.", "red") print colored("Please ensure that Rabbit Server is running", "red")