4
\$\begingroup\$

Simple task - deploy war-file from TeamCity agent to remote web-server.

I realized it with Fabric, because of it make readable output, and give opportunity to login via SSH with RSA-key. Also - during deploy - build agent must make some additional steps - create backup, stop-start Tomcat service etc.

URL to deploy to can be passed via variable on build-server.

import os
import sys
import time
from fabric.api import run, env, put
VAR_PREFIX = 'bamboo_'
"""Cloudlibrary functions"""
def deploy(logger, rds_basedir):
 """Deploy cloudlibrary.war to remote box, passed via BASE_URL"""
 logger.logger.info('Running cloudlibrary.war deploy')
 # file to deploy
 local_file = 'target/cloudlibrary.war'
 # local_file = 'd:\\RDS\\rdsmanager\\file.txt'
 # file will be created during backup command
 back_file = '/home/ec2-user/backups/cloudlibrary/cloudlibrary_%s.war' % time.strftime('%Y-%m-%d')
 # commands list
 status = 'sudo service tomcat7 status'
 backup = 'cp /var/lib/tomcat7/webapps/cloudlibrary.war %s' % back_file
 check_bkp = 'file %s' % back_file
 tomcat_kill = 'sudo kill -9 $(cat /var/run/tomcat7.pid)'
 tomcat_start = 'sudo /etc/init.d/tomcat7 start'
 try:
 base_url = os.environ[VAR_PREFIX + 'BASE_URL']
 logger.logger.info('\nURL found %s' % base_url)
 except KeyError:
 base_url = 'www.dev.domain.com'
 logger.logger.info('URL variable not found, will default - %s' % base_url)
 # Fabric connection settings
 env.host_string = base_url
 env.key_filename = [os.path.join(rds_basedir, '.ssh', 'rdsmanager_priv.openssh')]
 env.user = 'user'
 env.project_root = '/var/lib/tomcat7/webapps'
 logger.logger.info('Using URL: %s, user: %s, RDS-key: %s, Tomcat webapps: %s' % (env.host_string, env.user, env.key_filename[0], env.project_root))
 if os.path.isfile(local_file):
 # make backup of current cloudlibrary.war to ~/backup/cloulibrary
 run(backup)
 # if new file doesn't found - Fabric will exit
 run(check_bkp)
 # stop Omcat before put new war-file
 run(tomcat_kill)
 # put new builded war-file
 put(local_file, env.project_root)
 # start Tomcat
 run(tomcat_start)
 # cgeck it's status
 run(status)
 else:
 logger.logger.error('ERROR: file %s not found. Exit.' % local_file)
 sys.exit(1)
asked Jun 16, 2015 at 11:07
\$\endgroup\$
2
  • \$\begingroup\$ Are you aware of the existence of ftplib? \$\endgroup\$ Commented Jun 16, 2015 at 11:14
  • \$\begingroup\$ @Mast um... no... but - there is also tasks to start/stop Tomcat, not only copy war-file... plus - authorization possible with RSA key only; \$\endgroup\$ Commented Jun 16, 2015 at 11:18

1 Answer 1

3
\$\begingroup\$
  1. Using % for string formatting is deprecated. You should be using str.format() instead. Here's how one would use str.format(): print "Hello {0}.".format("world"). str.format() also supports using keyword arguments. Here's an example of that: print "Hello {word}.".format(word="world").
  2. Near the end of the function deploy, underneath the if block with the condition os.path.isfile(local_file), you have comments above every line. These are unnecessary and can be removed.
  3. Finally, I'm not sure why you have square brackets, [], around the value of the variable env.key_filename. If these aren't needed, they can be removed.
answered Jun 27, 2015 at 3:13
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.