Skip to content

Commit

Permalink
Merge pull request #6 from ibejohn818/add-twine-pypi
Browse files Browse the repository at this point in the history
Add twine pypi
  • Loading branch information
ibejohn818 authored Dec 27, 2017
2 parents 36a059e + 5a714b9 commit 0201656
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 21 deletions.
20 changes: 20 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ node {

stage("Build Test Image") {
sh "docker build -f Dockerfile-test -t ${img_tag} ."
echo sh(returnStdout: true, script: 'env')
}

stage("Run Tests") {
Expand All @@ -36,6 +37,25 @@ node {
}
}

if (env.TAG_NAME) {
stage("Publish To PyPi") {

echo "Cleaning"
sh "docker run --rm -v ${env.WORKSPACE}:${env.WORKSPACE} -w ${env.WORKSPACE} ${img_tag} make clean"
echo "Build DIST Package"
sh "docker run --rm -v ${env.WORKSPACE}:${env.WORKSPACE} -w ${env.WORKSPACE} ${img_tag} python3 setup.py sdist"

withCredentials([usernamePassword(credentialsId: 'ibejohn818PyPi', passwordVariable: 'PYPIPASSWD', usernameVariable: 'PYPIUSER')]) {
echo "Send to PyPi"

def dist_name = "jh-stackformation-${env.TAG_NAME}.tar.gz"

sh "docker run --rm -v ${env.WORKSPACE}:${env.WORKSPACE} -w ${env.WORKSPACE} ${img_tag} twine upload dist/${dist_name} -u ${env.PYPIUSER} -p ${env.PYPIPASSWD}"
}

}
}

} catch(Exception err) {
currentBuild.result = "FAILURE"
} finally {
Expand Down
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ PyYAML
pytest
pytest-runner
pytest-cov
twine
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[bumpversion]
current_version = 0.1.2
current_version = 0.1.5
commit = True
tag = True
tag_name = {new_version}

[bumpversion:file:setup.py]
search = version='{current_version}'
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

setup(
name='jh-stackformation',
version='0.1.2',
version='0.1.5',
description="AWS CloudFormation framework",
long_description=readme + '\n\n' + history,
author="John Hardy",
Expand Down
22 changes: 16 additions & 6 deletions stackformation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

__author__ = """John Hardy"""
__email__ = '[email protected]'
__version__ = '0.1.2'
__version__ = '0.1.5'

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -290,6 +290,8 @@ def list_amis(self):

return amis



class SoloStack():
pass

Expand Down Expand Up @@ -370,10 +372,10 @@ def parse_template_components(self, env, context):
def get_stack_name(self):

return "{}{}{}{}".format(
''.join([i.capitalize() for i in self.infra.prefix]),
self.infra.name.capitalize(),
self.stack_name.capitalize(),
self.name.capitalize()
''.join([utils.ucfirst(i) for i in self.infra.prefix]),
utils.ucfirst(self.infra.name),
utils.ucfirst(self.stack_name),
utils.ucfirst(self.name)
)

def get_remote_stack_name(self):
Expand Down Expand Up @@ -656,7 +658,15 @@ def find_class_in_list(self, ls, clazz, name=None):

return None

# def get_dependent_stacks(self, infra):
def review(self, infra):

# get stack dependencies
deps = infra.get_dependent_stacks(self)

cf = self.infra.boto_session.client('cloudformation')

info = cf.describe_stacks(StackName=self.get_remote_stack_name())
print(info['Stacks'][0]['Parameters'])

def build_template(self):
raise NotImplementedError("Must implement method to extend Stack")
Expand Down
Empty file.
Empty file.
27 changes: 27 additions & 0 deletions stackformation/aws/vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ def output_security_group(self):
self.name
)


class SelfReferenceSecurityGroup(SecurityGroup):

def __init__(self):
name = "SelfReferenceSecurityGroup"
super(SelfReferenceSecurityGroup, self).__init__(name)

def _build_security_group(self, t, vpc):

sg = t.add_resource(ec2.SecurityGroup(
'{}'.format(self.name),
GroupDescription="{} Self Reference Security Group".format(self.stack.get_stack_name()),
GroupName="{} {}".format(self.stack.get_stack_name(), self.name),
VpcId=Ref(vpc),
SecurityGroupIngress=[]
))

t.add_resource(ec2.SecurityGroupIngress(
'{}Ingress'.format(self.name),
ToPort='-1',
FromPort='-1',
IpProtocol='-1',
SourceSecurityGroupId=Ref(sg),
GroupId=Ref(sg),
))
return sg

class SSHSecurityGroup(SecurityGroup):

def __init__(self, name="SSH"):
Expand Down
34 changes: 33 additions & 1 deletion stackformation/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,23 @@ def build():


@stacks.command(name='list')
def list_stack():
@click.argument('selector', nargs=-1)
def list_stack(selector):

selector = list(selector)

infra = load_infra_file()

stacks = infra.list_stacks()

results = []

for stack in stacks:
if match_stack(selector, stack):
results.append(stack)

stacks = results

for stack in stacks:
click.echo("{}Stack:{} {} {}[{}]{}".format(
Style.BRIGHT,
Expand All @@ -58,6 +69,25 @@ def list_stack():
Style.RESET_ALL
))

@stacks.command(help='Deploy stacks')
@click.argument('selector', nargs=-1)
def review(selector):

selector = list(selector)

infra = load_infra_file()

stacks = infra.list_stacks()

results = []

for stack in stacks:
if match_stack(selector, stack):
results.append(stack)

for stack in results:
stack.review(infra)


@stacks.command(help='Deploy stacks')
@click.argument('selector', nargs=-1)
Expand Down Expand Up @@ -154,6 +184,8 @@ def load_configuration():
if HOME is None:
raise Exception("$HOME environment variable needs to be set to save configuration")



def jinja_env():

path = os.path.dirname(os.path.realpath(__file__))
Expand Down
29 changes: 27 additions & 2 deletions stackformation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ def match_stack(selector, stack):
if not isinstance(selector, list):
selector = selector.split(' ')

selector = [i.lower() for i in selector]

pos = []
neg = []
sn = stack.get_stack_name()
rn = stack.get_remote_stack_name()
sn = stack.get_stack_name().lower()
rn = stack.get_remote_stack_name().lower()
result = False

for s in selector:
Expand All @@ -57,3 +59,26 @@ def match_stack(selector, stack):
result = False

return result


def ucfirst(word):
"""Uppercase the first letter in a string
and error if string starts with an digit
Args:
word (str): the word
Raises:
Exception
Returns:
(str)
"""
if len(word) <= 0:
return ""
if not re.match('^[a-zA-Z]', word):
raise Exception("{} Cannot begin with a digit".format(word))
ls = list(word)
ls[0] = ls[0].upper()
return ''.join(ls)
8 changes: 4 additions & 4 deletions tests/test_aws_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ def test_ec2_stack(infra):

inst = t.resources['WebEC2Instance'].to_dict()

assert ec2_stack.output_instance() == "ProdTestWebEc2WebEC2Instance"
assert ec2_stack.output_instance() == "ProdTestWebEC2WebEC2Instance"

assert inst['Properties']['KeyName'] == 'testkey'

assert inst['Properties']['NetworkInterfaces'][0]['SubnetId'] == {'Ref': 'ProdTestVpcPublicSubnet1'}
assert inst['Properties']['NetworkInterfaces'][0]['SubnetId'] == {'Ref': 'ProdTestVPCPublicSubnet1'}

assert inst['Properties']['NetworkInterfaces'][0]['GroupSet'][0] == {'Ref': 'ProdTestVpcSSHSecurityGroup'}
assert inst['Properties']['NetworkInterfaces'][0]['GroupSet'][0] == {'Ref': 'ProdTestVPCSSHSecurityGroup'}

ec2_stack.private_subnet = True

t = ec2_stack.build_template()

inst = t.resources['WebEC2Instance'].to_dict()

assert inst['Properties']['NetworkInterfaces'][0]['SubnetId'] == {'Ref': 'ProdTestVpcPrivateSubnet1'}
assert inst['Properties']['NetworkInterfaces'][0]['SubnetId'] == {'Ref': 'ProdTestVPCPrivateSubnet1'}
12 changes: 6 additions & 6 deletions tests/test_aws_vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def test_vpc(prod_infra):
assert len(vpc_stack.output_azs()) == 3
assert len(vpc_stack.output_private_subnets()) == 3
assert len(vpc_stack.output_public_subnets()) == 3
assert vpc_stack.output_vpc() == "ProdTestVpcVpcId"
assert vpc_stack.output_public_routetable() == "ProdTestVpcPublicRouteTable"
assert vpc_stack.output_private_routetable() == "ProdTestVpcPrivateRouteTable"
assert vpc_stack.output_default_acl_table() == "ProdTestVpcDefaultAclTable"
assert vpc_stack.output_vpc() == "ProdTestVPCVpcId"
assert vpc_stack.output_public_routetable() == "ProdTestVPCPublicRouteTable"
assert vpc_stack.output_private_routetable() == "ProdTestVPCPrivateRouteTable"
assert vpc_stack.output_default_acl_table() == "ProdTestVPCDefaultAclTable"

def test_base_sec_group(prod_infra):

Expand Down Expand Up @@ -103,7 +103,7 @@ def test_ssh_sec_group(prod_infra):
assert sg_dict['Properties']['SecurityGroupIngress'][0]['FromPort'] == 22
assert sg_dict['Properties']['SecurityGroupIngress'][0]['CidrIp'] == '1.2.3.4/5'

assert ssh_sg.output_security_group() == "ProdTestVpcSSHSecurityGroup"
assert ssh_sg.output_security_group() == "ProdTestVPCSSHSecurityGroup"


def test_web_sec_group(prod_infra):
Expand All @@ -126,7 +126,7 @@ def test_web_sec_group(prod_infra):
assert sg['Properties']['SecurityGroupIngress'][1]['FromPort'] == 443
assert sg['Properties']['SecurityGroupIngress'][1]['CidrIp'] == '0.0.0.0/0'

assert web_sg.output_security_group() == "ProdTestVpcWebSecurityGroup"
assert web_sg.output_security_group() == "ProdTestVPCWebSecurityGroup"

def test_all_ports_sec_group(prod_infra):

Expand Down

0 comments on commit 0201656

Please sign in to comment.