Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the fabric code like in a production environment?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What this article shares with you is about what the fabric code is like in the production environment. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

PHP code update:

#! / usr/bin/env python

#-*-coding:utf-8-*-

From fabric.api import *

From fabric.context_managers import *

From fabric.contrib.console import confirm

From fabric.colors import *

Import time,sys,os

# = Login remote host variable setting =

Env.hosts= ['opuser@10.0.0.111:22','opuser@10.0.0.112:22','opuser@10.0.0.115:22']

# env.password='1234'

# = update program variable settings =

Env.dev_dir='/app/webapps/'

Env.pro_backup_date=time.strftime ("% Y%m%d%H%M")

Env.deploy_dir='/opt/app/webapps/'

Env.pre_deploy_dir='/opt/app/prewebapps/'

Env.deploy_tmp_dir='/home/opuser/upload/'

Env.deploy_backup_dir='/data01/backup/php'

Env.pro_name=prompt ("please input programe name:", default= "opuser.test.com")

Env.pro_versionID=prompt ("please input versionID:", default= "0.0.0.0")

Env.sourceProdRoot='/opt/app/webapps2/'

@ task

Def backup_task ():

Print red ("Start to backup programe...")

Env.pro_backup_name=prompt ("Please input backup project name:", default= "opuser.test.com")

With settings (warn_only=True):

Run ("cd% s & & tar-czf% s.%s.backup.tar.gz-- exclude% s/logs% s" (env.deploy_dir,env.pro_backup_name,env.pro_backup_date,env.pro_backup_name,env.pro_backup_name))

Run ("cd s & & mv s.%s.backup.tar.gz s" (env.deploy_dir,env.pro_backup_name,env.pro_backup_date,env.deploy_backup_dir))

Print green ("Backuped project packages success!")

# @ runs_once

@ task

Def tar_task ():

Print yellow ("Start to tar source package...")

Local ("cd% s% s & & rm-rf% s/index.php & & tar-czf% s.tar.gz-- exclude=logs-- exclude .svn% s"% (env.dev_dir,env.pro_name,env.pro_versionID,env.pro_name,env.pro_versionID))

Print green ("Creating source packages success!")

@ task

Def put_update_task ():

Run ("mkdir-p / home/opuser/upload/%s"% (env.pro_name))

Env.deploy_full_path=env.deploy_tmp_dir + env.pro_name

With settings (warn_only=True):

Result = put (env.dev_dir + env.pro_name + "/" + env.pro_name + ".tar.gz", env.deploy_full_path)

If result.failed and not confirm ("put file failed,Continue [Ygamma N]?"):

Abort ("Aborting file put task!")

With settings (warn_only=True):

Lmd5=local ("md5sum / app/webapps/%s/%s.tar.gz"% (env.pro_name,env.pro_name), capture=True) .split ('') [0]

Rmd5=run ("md5sum / home/opuser/upload/%s/%s.tar.gz"% (env.pro_name,env.pro_name)) .split ('') [0]

If lmd5==rmd5:

Print "OK"

Else:

Print "ERROR"

With cd (env.deploy_full_path):

Run ("tar xf% s.tar.gz & & cd% s & &\ cp-rf *% s%s/"% (env.pro_name,env.pro_versionID,env.deploy_dir,env.pro_name))

Run ("tar xf% s.tar.gz & & cd% s & &\ cp-rf *% s%s/"% (env.pro_name,env.pro_versionID,env.pre_deploy_dir,env.pro_name))

Run ("mv s s" (env.pro_versionID,env.pro_backup_date))

Run ("rm-rf% s*"% (env.pro_name))

Print red ("delete source tar file....")

# time.sleep (2)

With cd (env.dev_dir):

Local ("cd / app/webapps/%s & & rm-rf s.tar.gz"% (env.pro_name,env.pro_name))

@ runs_once

@ task

Def rollback ():

Confirm ("you are sure to rollback this project? Continue")

Print yellow ("Start to rollback project...")

With cd ("/ data/backup/php"):

Run ("ls-l")

Env.rollback_pro_name=prompt ("rollback project name:")

Run ("cd% s & & tar-xf% s-C% s" (env.deploy_backup_dir,env.rollback_pro_name,env.deploy_dir))

Print green ("rollback project success!")

@ task

Def checkcode ():

# env.sourceLatestVersion=run ("ls-lt / home/opuser/upload/%s/ | awk'{print $9}'| grep-v ^ $| head-n 1"% (env.pro_name)

# env.staticLatestVersion=run ("ls-lt / home/opuser/upload/res.spp.com | awk'{print $9}'| grep-v ^ $| head-n 1")

With cd ("/ server/scripts"):

# run (". / deploy-alsologtostderr=true-bakLimit=7-sourceProdRoot=%s%s-sourceDevRoot=%s%s-staticDevRoot=%s/-staticProdRoot=%s-staticRelativeDirs=%s"% (env.sourceProdRoot,env.pro_name,env.sourceDevRoot,env.pro_name,env.staticDevRoot,env.staticProdRoot,env.staticRelativeDirs))

Run (". / deploy-alsologtostderr=true")

@ task

Def go ():

# backup_task ()

Tar_task ()

Put_update_task ()

# checkcode ()

JAVA code update:

#! / usr/bin/env python

From fabric.api import *

From fabric.colors import *

From fabric.context_managers import *

From fabric.contrib.console import confirm

Import time,os

Env.hosts= ['10.0.0.13,10.0.0.11,10.0.0.12']

Env.user= "opuser"

# env.passwordcards Passw0dwords words W'

Env.port= "52113"

Env.app_local_dir= "/ app/webapps/app/java/"

Env.backup_date=time.strftime ("% Y%m%d%H%M")

Env.pro_backup_dir= "/ data01/backup/java"

Env.pro_name=prompt ("please input app name:")

Env.remote_app_dir= "/ opt/app/server/"

Env.upload_temp_dir= "/ home/opuser/upload/"

@ task

Def backup_task ():

Run ("cd% s%s/webapps & & tar-czf% s.%s.tar.gz * .war & & mv% s.%s.tar.gz% s"% (env.remote_app_dir,env.pro_name,env.pro_name,env.backup_date,\)

Env.pro_name,env.backup_date,env.pro_backup_dir))

Print "backup app the kids!"

Time.sleep (3)

Print red ("Ready to upload Java update package.")

Time.sleep (3)

@ task

Def upload_task ():

Run ("mkdir-p / home/opuser/upload/%s"% (env.pro_name))

Env.deploy_full_path=env.upload_temp_dir + env.pro_name

With settings (warn_only=True):

Result = put (env.app_local_dir + env.pro_name + "/" + "* .war", env.deploy_full_path)

Lmd5=local ("md5sum / app/webapps/app/java/%s/*.war"% (env.pro_name), capture=True) .split ('') [0]

Rmd5=run ("md5sum / home/opuser/upload/%s/*.war"% (env.pro_name)) .split ('') [0]

If lmd5==rmd5:

Print "OK"

Else:

Print "ERROR"

# time.sleep (5)

Print green ("Start off Tomcat service and update service packs.")

Time.sleep (5)

@ task

Def shutdown_and_update ():

# Close Tomcat

Print red ("start shutdown the% s...."% (env.pro_name))

Time.sleep (3)

Env.remote_app_pid=run ("ps xua | grep% s | grep-v grep | awk'{print $2}'| tail-1"% (env.pro_name)

If len (env.remote_app_pid) = = 0:

# Delete the old version

Print red ("start delete war packge.")

Time.sleep (3)

Run ("cd% s%s/webapps & & rm-rf *"% (env.remote_app_dir,env.pro_name))

# Update the new version

Print green ("Start update app....")

Run ("cd / home/opuser/upload/%s & & cp * .war s%s/webapps"% (env.pro_name,env.remote_app_dir,env.pro_name))

Time.sleep (3)

Run ("cd / home/opuser/upload/%s & & mv * .war s-%s-backup"% (env.pro_name,env.pro_name,env.backup_date))

Time.sleep (5)

Print green ("Update successfull.please run to the app")

Else:

Run ("cd% s% s & &. / bin/shutdown.sh"% (env.remote_app_dir,env.pro_name))

Env.remote_app_pid=run ("ps xua | grep% s | grep-v grep | awk'{print $2}'| tail-1"% (env.pro_name)

If len (env.remote_app_pid) = = 0:

Print "s is shutdown (env.pro_name)"

Run ("cd% s%s/webapps & & rm-rf *"% (env.remote_app_dir,env.pro_name))

# Update the new version

Print green ("Start update app....")

Run ("cd / home/opuser/upload/%s & & cp * .war s%s/webapps"% (env.pro_name,env.remote_app_dir,env.pro_name))

Time.sleep (3)

Run ("cd / home/opuser/upload/%s & & mv * .war s-%s-backup"% (env.pro_name,env.pro_name,env.backup_date))

Time.sleep (5)

Print green ("Update successfull.please run to the app")

Else:

Env.remote_app_pid=run ("ps xua | grep% s | grep-v grep | awk'{print $2}'| tail-1"% (env.pro_name)

Print "% s pid will be killed next...."% (env.pro_name)

Run ("/ bin/kill-9% s" (env.remote_app_pid))

Time.sleep (5)

Print green ("the app s has been shutdown"% (env.pro_name))

Run ("cd% s%s/webapps & & rm-rf *"% (env.remote_app_dir,env.pro_name))

# Update the new version

Print green ("Start update app....")

Run ("cd / home/opuser/upload/%s & & cp * .war s%s/webapps"% (env.pro_name,env.remote_app_dir,env.pro_name))

Time.sleep (3)

Run ("cd / home/opuser/upload/%s & & mv * .war s-%s-backup"% (env.pro_name,env.pro_name,env.backup_date))

Time.sleep (5)

Print green ("Update successfull.please run to the app")

Print yellow ("# #")

Print green ("\ n")

@ task

Def start_task ():

Print green ("starting app....")

Time.sleep (3)

Run ("set-m office% s% s bind startup.sh"% (env.remote_app_dir,env.pro_name))

Time.sleep (5)

Env.remote_app_pid=run ("ps xua | grep% s | grep-v grep | awk'{print $2}'| tail-1"% (env.pro_name)

# print env.remote_app_pid

If len (env.remote_app_pid)! = 0:

Print blue ("% s is startup successfull the pid is s."% (env.pro_name,env.remote_app_pid))

@ task

Def go ():

Backup_task ()

Upload_task ()

Shutdown_and_update ()

Start_task ()

Note: ssh-key key authentication is recommended in production, but password is not recommended.

The above is what the fabric code looks like in the production environment, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report