def create_git_test_repo(root_dir, repo_name): git_repo = repo_name + ".git" git_dir = os.path.join(root_dir, git_repo) os.mkdir(git_dir) cmds = [] cmds.append("cd " + git_dir + ";git init --bare") cmds.append("cd " + root_dir + ";git clone " + git_dir + " " + repo_name) utils.run_commands(cmds) test_dir = os.path.join(root_dir, repo_name) os.chdir(test_dir) cmds = [] cmds.append("echo test > testfile") cmds.append("git add testfile") cmds.append("git commit -m \"first commmit\"") cmds.append("git push origin master") utils.run_commands(cmds)
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 deploy_workflow_start(self): """Method to perform actions in start state: - checkout the deploy branch - prepare for deploying state """ log.info("deploy workflow ==> enter [%s] state", self.current_context['state']) deploy_branch = self.opts['branch'] cmds = [] cmds.append("git checkout -B " + deploy_branch) cmds.append("git tag -a deploy_start -m \"jungar: start deploy workflow\"") cmds.append("git merge master -m \'jungar: merge from master branch\'") utils.run_commands(cmds) # prepare to move into next state: deploying self.current_context['state'] = STATE_DEPLOYING self.current_context['bmt_phase'] = 0 return self.current_context
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
def setUp(self): super(TestDeployWorkflow, self).setUp() log.info("enter setup") self.cur_dir = os.getcwd() # clone the gittesting repo and make the recovery tag create_git_test_repo('/tmp', 'git_test') self.work_dir = "/tmp/git_test" os.chdir(self.work_dir) cmds = [] cmds.append("git tag -a test_snapshot -m \"test start snapshot\"") cmds.append("git config user.email [email protected]") cmds.append("git config user.name logstream-ci") cmds.append("git checkout master") utils.run_commands(cmds) self.file_dir = os.path.join(self.work_dir) shutil.copy(lsb_file, self.file_dir) shutil.copy(mlb_file, self.file_dir) shutil.copy(pmt_file, self.file_dir) shutil.copy(bmt_file, self.file_dir) cmds = [] cmds.append("git add " + self.file_dir + "/*.yaml") cmds.append("git commit -m \'add test yaml files\'") utils.run_commands(cmds) self.pmt_file = os.path.join(self.file_dir, "pmt.yaml") self.bmt_file = os.path.join(self.file_dir, "bmt.yaml") self.lsb_file = os.path.join(self.file_dir, "lsb.yaml") self.mlb_file = os.path.join(self.file_dir, "mlb.yaml") self.xmt_mgr = xmt.XmtManager(pmt_path = self.pmt_file, bmt_path = self.bmt_file, lsb_path = self.lsb_file, mlb_path = self.mlb_file) self.xmt_mgr.load(xmt.XMT_TYPE_PMT) self.xmt_mgr.load(xmt.XMT_TYPE_BMT) self.xmt_mgr.load(xmt.XMT_TYPE_LSB) self.xmt_mgr.load(xmt.XMT_TYPE_MLB)
def setUp(self): super(TestDeployWorkflow, self).setUp() log.info("enter setup") self.cur_dir = os.getcwd() # clone the gittesting repo and make the recovery tag create_git_test_repo('/tmp', 'git_test') self.work_dir = "/tmp/git_test" os.chdir(self.work_dir) cmds = [] cmds.append("git tag -a test_snapshot -m \"test start snapshot\"") cmds.append("git config user.email [email protected]") cmds.append("git config user.name logstream-ci") cmds.append("git checkout master") utils.run_commands(cmds) self.file_dir = os.path.join(self.work_dir) shutil.copy(lsb_file, self.file_dir) shutil.copy(mlb_file, self.file_dir) shutil.copy(pmt_file, self.file_dir) shutil.copy(bmt_file, self.file_dir) cmds = [] cmds.append("git add " + self.file_dir + "/*.yaml") cmds.append("git commit -m \'add test yaml files\'") utils.run_commands(cmds) self.pmt_file = os.path.join(self.file_dir, "pmt.yaml") self.bmt_file = os.path.join(self.file_dir, "bmt.yaml") self.lsb_file = os.path.join(self.file_dir, "lsb.yaml") self.mlb_file = os.path.join(self.file_dir, "mlb.yaml") self.xmt_mgr = xmt.XmtManager(pmt_path=self.pmt_file, bmt_path=self.bmt_file, lsb_path=self.lsb_file, mlb_path=self.mlb_file) self.xmt_mgr.load(xmt.XMT_TYPE_PMT) self.xmt_mgr.load(xmt.XMT_TYPE_BMT) self.xmt_mgr.load(xmt.XMT_TYPE_LSB) self.xmt_mgr.load(xmt.XMT_TYPE_MLB)