def checkout_tag(self, git_tag): """切换分支""" git_fetch_cmd = 'cd {} && git fetch -t -p -f && git fetch --all'.format( self.code_dir) # 更新代码 git_checkout_cmd = 'cd {} && git clean -df && git checkout {}'.format( self.code_dir, git_tag) # 切换分支 try: git_fetch_status, git_fetch_output = exec_shell(git_fetch_cmd) if git_fetch_status == 0: git_check_status, git_check_output = exec_shell( git_checkout_cmd) if git_check_status == 0: print('[Success]: git checkout tag: {} successfully...'. format(git_tag)) else: print('[Error]: git checkout tag: {} failed ...'.format( git_tag)) exit(402) else: print('[Error]: git fetch failed ...') exit(403) except Exception as e: print(e) exit(-500)
def checkout_tag(self, git_tag): '''切换分支''' git_fetch_cmd = 'cd {}/{} && git fetch -t -p -f && git fetch --all'.format(self.clone_dir, self.repo_name) # 更新代码 git_checkout_cmd = 'cd {}/{} && git clean -df && git checkout {}'.format( self.clone_dir, self.repo_name, git_tag) # 切换分支 # print('[CMD:]', git_fetch_cmd) # print('[CMD:]', git_checkout_cmd) try: git_fetch_status, git_fetch_output = exec_shell(git_fetch_cmd) if git_fetch_status == 0: git_check_status, git_check_output = exec_shell(git_checkout_cmd) if git_check_status == 0: print('[Success]: git checkout tag: {} successfully...'.format(git_tag)) else: print('[Error]: git checkout tag: {} faild...'.format(git_tag)) exit(402) else: print('[Error]: git fetch faild...') exit(403) except Exception as e: print(e) exit(-500)
def code_deploy(self, host): if not isinstance(host, dict): raise ValueError() '''/tmp下的代码是upload_code脚本上传过来的''' tmp_code_path = '/tmp/{}'.format(self.repo_name) # if not os.path.exists(tmp_code_path): # print('[Error]: No code found') # sys.exit(-100) ip = host.get('ip') port = host.get('port', 22) user = host.get('user', 'root') password = host.get('password') # code_path = self.publish_path + self.repo_name # depoly_cmd = "sshpass -p {} rsync -ahqzt --delete -e 'ssh -p {} -o StrictHostKeyChecking=no ' {} {}@{}:{}".format( # password, port, tmp_code_path, user, ip, self.publish_path) rsync_cmd = 'rsync -ahqzt --delete {} {}'.format( tmp_code_path, self.publish_path) depoly_cmd = "sshpass -p {} ssh -p {} -o StrictHostKeyChecking=no {}@{} '{}'".format( password, port, user, ip, rsync_cmd) # print('[CMD:]', depoly_cmd) try: depoly_status, depoly_output = exec_shell(depoly_cmd) if depoly_status == 0: print('[Success]: Host:{} 发布成功'.format(ip)) else: os.mknod(self.uuid_file) print('[Error]: Host:{} 失败,错误信息: {}'.format(ip, depoly_output)) exit(-3) except Exception as e: print(e) exit(-500)
def git_clone(self): '''检测发布目录没有代码则进行git clone''' code_dir = self.publish_path + self.repo_name try: if not os.path.exists(code_dir): print('[INFO]: Start pulling a new codebase to {}'.format( code_dir)) git_clone_cmd = '[ ! -d "{}" ] && mkdir {} ;cd {} && git clone {}'.format( self.publish_path, self.publish_path, self.publish_path, self.repository) # 克隆代码 print('[CMD:] ', git_clone_cmd) git_clone_status, git_clone_output = exec_shell(git_clone_cmd) if git_clone_status == 0: print('[Success]: git clone {} sucess...'.format( self.repository)) else: print('[Error]: git clone {} faild...'.format( self.repository)) exit(404) else: print( '[PASS]: The repository already exists, skip the clone and update directly' ) except Exception as e: print(e)
def code_backup(self, host): """ :param host: 主机信息,IP端口用户密码 :return: """ if not isinstance(host, dict): raise ValueError() ip = host.get('ip') port = host.get('port', 22) user = host.get('user', 'root') password = host.get('password') code_path = self.publish_path + self.repo_name try: backup_cmd = '[ ! -d "{}" ] && mkdir {} ; cp -aR {} {} && echo success'.format( self.backup_dir, self.backup_dir, code_path, self.backup_dir) ssh_cmd = "sshpass -p {} ssh -p {} -o StrictHostKeyChecking=no {}@{} '{}'".format( password, port, user, ip, backup_cmd) ssh_status, ssh_output = exec_shell(ssh_cmd) if ssh_status == 0: print('[Success]: Host: {} 备份路径: {}'.format( ip, self.backup_dir)) else: print('[Error]: Host: {} 备份失败,信息: {} faild'.format( ip, ssh_output)) print(ssh_output) except Exception as e: print(e) exit(-500)
def code_process(self, exclude_file): """ 代码处理,如过滤,处理完放到编译主机/tmp :param exclude_file: 过滤文件名称 :return: """ try: if os.path.exists(self.clone_dir): rsync_local_cmd = 'cd {} && rsync -ahqzt --delete --exclude-from="{}" {}/{} {}'.format(self.clone_dir, exclude_file, self.clone_dir, self.repo_name, '/tmp') # print('[CMD:]', rsync_local_cmd) recode, stdout = exec_shell(rsync_local_cmd) if recode == 0: print('[Success]: 构建机器代码处理(如:exclude)到临时路径:/tmp/{} 完成 '.format(self.repo_name)) else: print('[Error]: Rsync bulid host:/tmp failed') exit(405) else: print('[Error]: Not fount git repo dir:{} '.format(self.clone_dir)) exit(404) except Exception as e: print(e) exit(406)
def rsync_tmp(self, host): """ 发布代码到目标主机的/tmp目录 :return: """ if not isinstance(host, dict): raise ValueError() ip = host.get('ip') port = host.get('port', 22) user = host.get('user', 'root') password = host.get('password') local_code_path = self.local_dir + self.repo_name # 处理后的本地代码路径 rsync_tmp_cmd = "sshpass -p {} rsync -ahqzt --delete -e 'ssh -p {} -o StrictHostKeyChecking=no ' {} {}@{}:{}".format( password, port, local_code_path, user, ip, '/tmp/') # print('[CMD:] ', rsync_tmp_cmd) rsync_status, rsync_output = exec_shell(rsync_tmp_cmd) if rsync_status == 0: # 同步完成删除/tmp/repo_name目录 print('[Success]: rsync host:{} to /tmp/{} sucess...'.format( ip, self.repo_name)) else: print( '[Error]: rsync host:{} to /tmp/{} faild, please check your ip,port,user,password...' .format(ip, self.repo_name)) exit(-407)
def check(self): cmd = "ssh %s kubectl get pod -n %s -o wide -l app=%s |grep -v 'NAME' |awk '{print($3)}'" % ( self.k8s_host, self.namespace, self.deploy_name) exec_status, exec_output = exec_shell(cmd) if exec_status == 0 and exec_output[0] == 'Running': print('[Success]: The pod status is running...') else: print('[Error]: The pod status is not running...\n[Error]: {}'. format(exec_output)) print('[Warning] rollout undo deployment start !!!!') rollback_cmd = "ssh {} kubectl rollout undo -n {} deployment {}".format( self.k8s_host, self.namespace, self.deploy_name) exec_status1, exec_output1 = exec_shell(rollback_cmd) print("{} {}".format(exec_status1, exec_output1)) print("Roll back to the last version end") exit(-2)
def build_image(self): cmd = "cd {} && docker build . -t {}".format(self.code_dir, self.image_name) exec_status, exec_output = exec_shell(cmd) if exec_status == 0: print('[Success]: docker build {} OK...'.format(self.image_name)) else: print('[Error]: docker build {} failed ...\n[Error]: {}'.format( self.image_name, exec_output)) exit(-1)
def push_image(self): self.login() cmd = "docker push {}".format(self.image_name) exec_status, exec_output = exec_shell(cmd) if exec_status == 0: print('[Success]: docker push {} successfully...'.format( self.image_name)) else: print('[Error]: docker push {} failed ...\n[Error]: {}'.format( self.image_name, exec_output)) exit(-1)
def login_cos(self): """ 登陆COS :return: """ print('[INFO:] 开始登陆COS') login_cos = 'coscmd config -a %s -s %s -b %s -r %s -m 20 >/dev/null' % ( self.access_id, self.access_key, self.bucket_name, self.region) login_cos_status, login_cos_output = exec_shell(login_cos) if login_cos_status == 0: print('[Success:] COS登陆成功') else: print('[Error:] COS登陆失败,错误信息:{}'.format(login_cos_output))
def upload_file(self): """ 将资源文件上传到AWS S3,使用AWSCLI的 AWS S3 Sync方式 :return: """ source_dir = '/tmp/%s/' % (self.repo_name) upload_s3_cmd = 'aws s3 sync %s s3://%s --exclude *.git* --quiet' % (source_dir, self.bucket_name) print('[INFO]:Start upload for bucket: %s' % self.repo_name) upload_s3_status, upload_s3_ouput = exec_shell(upload_s3_cmd) if upload_s3_status == 0: print('[Success:] AWS S3 上传成功!') else: print('[Error:] AWS S3 上传失败!')
def run(self): cmd = "kubectl set image deployment/{} {}={} -n {}".format( self.deploy_name, self.deploy_name, self.image_name, self.namespace) exec_status, exec_output = exec_shell("ssh {} '{}'".format( self.k8s_host, cmd)) if exec_status == 0: print('[Success]: kubernetes rolling-update {} successfully...'. format(self.image_name)) else: print( '[Error]: kubernetes rolling-update {} failed ...\n[Error]: {}' .format(self.image_name, exec_output)) exit(-1)
def cos_upload(self): """ 上传资源到COS :return: """ # 判断有没有权限 print('[INFO:] 正在检查权限') check_cos = "coscmd getbucketacl" check_cos_status, check_cos_output = exec_shell(check_cos) if check_cos_status != 0: print('[Error:]Access Denied,请确认密钥是否有权限访问Bucket: {}'.format(self.bucket_name)) exit(-403) source_dir = '/tmp/%s' % (self.repo_name) # cmd = 'aws s3 sync %s s3://%s --exclude *.git* --profile %s --quiet'%(source_dir,BucketDict[self.app_name][self.tag_type],BucketDict[self.app_name]['s3_key']) upload_cmd = 'coscmd upload -rs %s/ %s >/dev/null 2>&1' % (source_dir, self.bucket_path) upload_cmd_status, upload_cmd_output = exec_shell(upload_cmd) if upload_cmd_status == 0: print('[Success:] 文件上传成功') else: print('[Error:] 上传失败,错误信息:{}'.format(upload_cmd_output)) exit(-404)
def login(self): obj = API() result = obj.get_api_info(api_settings['docker_registry_info_api']) for i in result: if i['registry_url'] == self.docker_registry: login_cmd = "docker login -u {} -p {} {}".format( i['user_name'], i['password'], i['registry_url']) print(login_cmd) exec_status, exec_output = exec_shell(login_cmd) if exec_status == 0: print('[Success]: docker login {} OK...'.format( self.docker_registry)) else: print('[Error]: docker login {} failed ...\n[Error]: {}'. format(self.docker_registry, exec_output)) exit(-1)
def git_clone(self): """检测发布目录没有代码则进行git clone""" try: if not os.path.exists(self.code_dir): print('[INFO]: Start pulling a new codebase to {}'.format(self.code_dir)) ### 克隆代码 git_clone_cmd = 'mkdir -p {} ;cd {} && git clone {}'.format(self.version_dir, self.version_dir, self.repository) print('[CMD:] ', git_clone_cmd) git_clone_status, git_clone_output = exec_shell(git_clone_cmd) if git_clone_status == 0: print('[Success]: git clone {} successfully...'.format(self.repository)) else: print('[Error]: git clone {} failed ...'.format(self.repository)) exit(404) else: print('[PASS]: The repository already exists, skip the clone and update directly') except Exception as e: print(e)
def code_process(self, exclude_file): '''代码处理,如过滤,处理完放到编译主机/tmp''' code_dir = self.publish_path + self.repo_name try: if os.path.exists(code_dir): rsync_local_cmd = 'cd {} && rsync -ahqzt --delete --exclude-from="{}" {} {}'.format( code_dir, exclude_file, code_dir, self.local_dir) # print('[CMD:]', rsync_local_cmd) recode, stdout = exec_shell(rsync_local_cmd) if recode == 0: print('[Success]: 构建机器代码处理(如:exclude)到临时路径:/tmp/{} 完成 '. format(self.repo_name)) else: print('[Error]: Rsync bulid host:/tmp failed') exit(405) else: print('[Error]: Not fount git repo dir:{} '.format(code_dir)) exit(404) except Exception as e: print(e)