def main(): try: # prepare the working environment args = prepare() # load PMT/BMT table xmt_mgr = xmt.XmtManager() xmt_mgr.load(xmt.XMT_TYPE_PMT, args.pmt) hostname = socket.gethostname() pmt_entry = xmt_mgr.get_entry(xmt.XMT_TYPE_PMT, hostname) log.debug("Get the PMT entry: %s", pmt_entry) # modify puppet config with proper environment value cmd = "sed -i \'s/environment=production$/environment=" + \ pmt_entry['environment'] + "/g\' /etc/puppet/puppet.conf" utils.run_command(cmd) # start deployment with the specified build pbt = load_yaml(args.pbt) deploy_build(args, pmt_entry, pbt) except Exception as e: log.error("Error happens during post boot: %s", e) raise return 0
def tearDown(self): super(TestDeployWorkflow, self).tearDown() # Remove the test yaml files to clean up # the test environment on remote gittesting # repository utils.run_command("git reset --hard test_snapshot") os.chdir(self.cur_dir) remove_git_test_repo('/tmp', 'git_test')
def set_hostname(args, xmt_mgr): """setup the hostname in boot mode """ try: pmt_entries = xmt_mgr.get_entries(xmt.XMT_TYPE_PMT) pmt_entry = [x for x in pmt_entries if args.ecs_name.find(x['ecs_name']) != -1][0] # pmt_entry = xmt_mgr.get_entries(xmt.XMT_TYPE_PMT, filter={'ecs_name': args.ecs_name})[0] except ValueError: log.error("Didn't find the host give ecs_name [%s]!", args.ecs_name) raise hostname = pmt_entry['host'] cmd = "echo " + hostname + " > /etc/hostname" utils.run_command(cmd) utils.run_command('hostname ' + hostname) log.info("set hostname to [%s]", hostname)
def deploy_workflow_exit(self): """Method to complete the deployment workflow and update BMT and LSB on master branch accordingly """ log.info("deploy workflow ==> enter [%s] state", self.current_context['state']) if self.current_context['prestate'] == STATE_ROLLBACK: # there is nothing to do if it moves from rollback # state to exit state. LSB and BMT is always rolled # back return lsb_file = self.xmt_mgr.get_xmt_file(xmt.XMT_TYPE_LSB) # update lsb table lsb_entries = self.xmt_mgr.construct_lsb_entries() self.xmt_mgr.save(xmt.XMT_TYPE_LSB, lsb_entries) msg = "jungar: update lsb on deploy branch" cmds = [] cmds.append("git add " + lsb_file) cmds.append("git commit -m \'" + msg + "\'") utils.run_commands(cmds) # update the lsb and bmt table on master branch msg = "deploy: update bmt and lsb at the end of workflow" cmds = [] cmds.append('git checkout master') cmds.append('git pull') cmds.append('git merge ' + self.opts['branch'] + " -m \"jungar: merge lsb and bmt to master\"") utils.run_commands(cmds) retries = 5 while retries: try: utils.run_command("git push origin master") break except exp.CommandFailure: log.warn("Push changes to remote failed. Retry [%d/5]", retries) retries -= 1 time.sleep(5) return
def set_hostname(args, xmt_mgr): """setup the hostname in boot mode """ try: pmt_entries = xmt_mgr.get_entries(xmt.XMT_TYPE_PMT) pmt_entry = [ x for x in pmt_entries if args.ecs_name.find(x['ecs_name']) != -1 ][0] # pmt_entry = xmt_mgr.get_entries(xmt.XMT_TYPE_PMT, filter={'ecs_name': args.ecs_name})[0] except ValueError: log.error("Didn't find the host give ecs_name [%s]!", args.ecs_name) raise hostname = pmt_entry['host'] cmd = "echo " + hostname + " > /etc/hostname" utils.run_command(cmd) utils.run_command('hostname ' + hostname) log.info("set hostname to [%s]", hostname)
def deploy_build(args, pmt_entry, pbt): """ Deploy the build specified in pbt table """ host = pmt_entry['host'] phase = pmt_entry['phase'] log.info(" == start deploying the build for host %s at phase %s", host, phase) # checkout the branch d_branch = pbt[phase]['branch'] utils.run_command("cd " + args.shoowo_dir + ";git checkout " + d_branch) utils.run_command("cd " + args.shoowo_dir + ";git pull") # if tag is speicifed, checkout that tag if 'tag' in pbt[phase]: utils.run_command("cd " + args.shoowo_dir + ";git reset --hard " + pbt[phase]['tag']) log.info(" -- deployed the build at TAG %s", pbt[phase]['tag']) else: utils.run_command('cd ' + args.shoowo_dir + ';git pull') log.info(" -- deployed the lastest build")
def deploy_workflow_deploying(self): """Method to perform deploying at given phase """ log.info("deploy workflow ==> enter [%s] state", self.current_context['state']) bmt_phase = self.current_context['bmt_phase'] assert self.current_context['mlb_phase'] == -1 or \ bmt_phase <= self.current_context['mlb_phase'] # construct BMT table bmt_entries = self.xmt_mgr.construct_bmt_entries(bmt_phase) self.xmt_mgr.save(xmt.XMT_TYPE_BMT, bmt_entries) msg = "deploy: update bmt to phase %d" % (bmt_phase) bmt_file = self.xmt_mgr.get_xmt_file(xmt.XMT_TYPE_BMT) output = utils.run_command('git status') if output.find("nothing to commit") == -1: cmds = [] cmds.append("git add " + bmt_file) cmds.append("git commit -m \'" + msg + "\'") utils.run_commands(cmds) # TODO: interact with Tarim system to ensure the deployment # is successfully on the sichuan machines time.sleep(self.opts['wait']) # prepare to move into next state if self.current_context['mlb_phase'] == -1 and bmt_phase < self.max_phase: self.current_context['state'] = STATE_DEPLOYING self.current_context['bmt_phase'] = bmt_phase + 1 return self.current_context if bmt_phase < self.max_phase and \ bmt_phase < self.current_context['mlb_phase']: self.current_context['state'] = STATE_DEPLOYING self.current_context['bmt_phase'] = bmt_phase + 1 return self.current_context self.current_context['state'] = STATE_EXIT self.current_context['prestate'] = STATE_DEPLOYING return self.current_context