#
# Django Code Deploy
#
# Copyright 2015 - 2018 devops.center
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging
logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
from fabric.api import *
from time import gmtime, strftime
import os
import sys
from git import Repo
class FabricException(Exception):
pass
# Set some global defaults for all operations
env.user = "ubuntu"
env.key_filename = []
ACCESS_KEY_PATH = "~/.ssh/"
env.connection_attempts = 3
TRUTH_VALUES = ['True', 'TRUE', '1', 'true', 't', 'Yes',
'YES', 'yes', 'y'] # arguments in fab are always strings
import boto
import urllib2
from boto.ec2 import connect_to_region
import distutils.sysconfig
# objects
import collections
AWSAddress = collections.namedtuple(
'AWSAddress', 'name publicdns privateip shard')
# set_hosts selects all instances that match the filter criteria.
# type is web, worker, db
# environment is dev, staging, prod
# appname is application name, such as "fresco", "topopps", "mojo", etc.
# action is the deferred action needed, such as "deploy", "security-updates", etc.
# region is aws region
@task
def set_hosts(type, primary=None, appname=None, action=None, region=None,
shard='all', aRole=None):
if appname is None:
local('echo "ERROR: appname option is not set"')
if region is None:
local('echo "ERROR: region option is not set"')
environment = os.environ["AWS_ENVIRONMENT"]
awsaddresses = _get_awsaddress(type, primary, environment, appname,
action, region, shard, aRole)
env.hosts = list(item.publicdns for item in awsaddresses)
env.host_names = list(item.name for item in awsaddresses)
_log_hosts(awsaddresses)
# set_one_host picks a single instance out of the set.
# filters are the same as with set_hosts.
@task
def set_one_host(type, primary=None, appname=None, action=None, region=None,
shard='all', aRole=None):
if appname is None:
local('echo "ERROR: appname option is not set"')
if region is None:
local('echo "ERROR: region option is not set"')
environment = os.environ["AWS_ENVIRONMENT"]
awsaddresses = _get_awsaddress(type, primary, environment, appname,
action, region, shard, aRole)
awsaddresses = [awsaddresses[0]]
env.hosts = [awsaddresses[0].publicdns]
env.host_names = [awsaddresses[0].name]
_log_hosts(awsaddresses)
@task
def set_one_host_per_shard(type, primary=None, appname=None, action=None,
region=None, shard='all', aRole=None):
if appname is None:
local('echo "ERROR: appname option is not set"')
if region is None:
local('echo "ERROR: region option is not set"')
environment = os.environ["AWS_ENVIRONMENT"]
awsaddresses = _get_awsaddress(type, primary, environment, appname,
action, region, shard, aRole)
pruned_list = []
for ahost in awsaddresses:
if not next((True for bhost in pruned_list if ahost.shard == bhost.shard), False):
pruned_list.append(ahost)
env.hosts = list(item.publicdns for item in pruned_list)
env.host_names = list(item.name for item in pruned_list)
_log_hosts(pruned_list)
def _log_hosts(awsaddresses):
logger.info("")
logger.info(
"Instances to operate upon - name, public dns, private ip, shard")
logger.info(
"---------------------------------------------------------------")
for instance in awsaddresses:
logger.info("%s %s %s %s", instance.name,
instance.publicdns, instance.privateip, instance.shard)
logger.info("")
logger.info("")
logger.info("keys: %s", env.key_filename)
logger.info("")
@task
def dev():
os.environ["AWS_ENVIRONMENT"] = "dev"
@task
def staging():
os.environ["AWS_ENVIRONMENT"] = "staging"
@task
def prod():
os.environ["AWS_ENVIRONMENT"] = "prod"
@task
def set_environment(environment):
os.environ["AWS_ENVIRONMENT"] = environment
@task
def set_access_key(accessKeyPath):
env.key_filename = [accessKeyPath]
@task
def set_access_key_path(anAccessKeyPath):
global ACCESS_KEY_PATH
if(anAccessKeyPath.endswith('/')):
ACCESS_KEY_PATH = anAccessKeyPath
else:
ACCESS_KEY_PATH = anAccessKeyPath + "/"
@task
def set_user(loginName):
env.user = loginName
@task
def show_environment():
run('env')
# Private method to get public DNS name for instance with given tag key
# and value pair
def _get_awsaddress(type, primary, environment, appname, action, region, shard,
aRole):
awsaddresses = []
connection = _create_connection(region)
aws_tags = {"tag:Type": type, "tag:Env": environment,
"tag:App": appname, "instance-state-name": "running"}
if action:
aws_tags["tag:ActionNeeded"] = action
if primary:
aws_tags["tag:Primary"] = primary
if aRole:
aws_tags["tag:role"] = aRole
logger.info("Filtering via tags=%s", aws_tags)
instances = connection.get_only_instances(filters=aws_tags)
shards = [e for e in shard.split(' ')]
for instance in instances:
if instance.public_dns_name: # make sure there's really an instance here
shardt = (
"None" if not 'Shard' in instance.tags else instance.tags['Shard'])
awsaddress = AWSAddress(name=instance.tags['Name'], publicdns=instance.public_dns_name,
privateip=instance.private_ip_address, shard=shardt)
if (shard == 'all') or (shardt in shards):
awsaddresses.append(awsaddress)
if instance.key_name not in env.key_filename:
env.key_filename.append(instance.key_name)
# convert any AWS key-pair names to a file path for the actual key pair
# locally
env.key_filename = [key if os.path.isfile(
key) else ACCESS_KEY_PATH + key + ".pem" for key in env.key_filename]
return awsaddresses
# Private method for getting AWS connection
def _create_connection(region):
logger.info("")
logger.info("Connecting to AWS region %s", region)
connection = connect_to_region(
region_name=region
)
logger.info("Connection with AWS established")
return connection
timest = strftime("%Y-%m-%d_%H-%M-%S", gmtime())
# deploy directories have timestamps for names.
UPLOAD_CODE_PATH = os.path.join("/data/deploy", timest)
TAR_NAME = "devops"
# These are tasks for building on jenkins (or other build box)
# Initally support a yarn-based workflow for node
@task
def build(branch, installPath):
# ensure yarn installs all build tools
with lcd(installPath):
local("pwd")
local('yarn --production=false --no-progress --non-interactive install')
# do the build
local('npm run dist')
# make sure the new files are part of the local git repo
local('echo "devops.tar.*"" >> .gitignore')
local('echo "fabfile.*" >> .gitignore')
local("git add .")
local("git commit -am 'add results of build' --no-verify --quiet")
@task
def tar_from_git(branch):
local('rm -rf %s.tar.gz' % TAR_NAME)
local('gi
没有合适的资源?快使用搜索试试~ 我知道了~
Python库 | django-code-deploy-0.9.138.tar.gz
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 138 浏览量
2022-04-08
01:28:22
上传
评论
收藏 6KB GZ 举报
温馨提示
共5个文件
py:3个
pkg-info:1个
cfg:1个
资源分类:Python库 所属语言:Python 资源全名:django-code-deploy-0.9.138.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源详情
资源评论
资源推荐
收起资源包目录
django-code-deploy-0.9.138.tar.gz (5个子文件)
django-code-deploy-0.9.138
PKG-INFO 485B
django_code_deploy
fabfile.py 17KB
__init__.py 0B
setup.cfg 40B
setup.py 700B
共 5 条
- 1
挣扎的蓝藻
- 粉丝: 12w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0