Skip to content

Commit c6a5f00

Browse files
committed
vpc_idは指定しなくても良いようにした
1 parent fc4e222 commit c6a5f00

File tree

6 files changed

+54
-70
lines changed

6 files changed

+54
-70
lines changed

README.md

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# floccus
22

3-
AWS の CloudFormation が VPC (Virutal Private Cloud) に対応していないので代替として動作するCLIツールです。VPCIDを指定すると標準出力にCloudFormationのJSONファイルを出力します
3+
AWS の CloudFormation が VPC (Virutal Private Cloud) に対応していないので代替として動作するCLIツールです。regionを指定すると標準出力にCloudFormationのJSONファイルを出力します
44

55
## 動作環境
66
Linux/MacOS 10以降で動作します。また必要なライブラリは以下のとおりです。
@@ -33,11 +33,11 @@ ${FLOCCUS_HOME} 以下に bin/ と lib/ ができますので ${FLOCCUS_HOME}/bi
3333

3434
## 使い方
3535

36-
環境変数 AWS_ACCESS_KEY, AWS_SECRET_KEY へEC2でDescribeできる権限のアクセスキー、シークレットキーを設定してCloudFormationのJSONを出力したいVPCのIDを指定します
36+
環境変数 AWS_ACCESS_KEY, AWS_SECRET_KEY へEC2でDescribeできる権限のアクセスキー、シークレットキーを設定し、CloudFormationのJSONを出力したいリージョンを指定します
3737

3838
$ export AWS_ACCESS_KEY=アクセスキー
3939
$ export AWS_SECRET_KEY=シークレットキー
40-
$ flcs VPCID
40+
$ flcs REGION
4141
{
4242
"AWSTemplateFormatVersion": "2010-09-09",
4343
"Description": "This is auto generated cloudformation file.",
@@ -46,20 +46,16 @@ ${FLOCCUS_HOME} 以下に bin/ と lib/ ができますので ${FLOCCUS_HOME}/bi
4646

4747
アクセスキー、シークレットキーは引数で指定することもできます。
4848

49-
$ flcs --aws-access-key アクセスキー --aws-secret-key シークレットキー VPCID
49+
$ flcs --aws-access-key アクセスキー --aws-secret-key シークレットキー
5050

51-
対象のVPCが us-east-1 以外のリージョンに存在する場合は --region 引数で指定する必要があります。
51+
us-east-1 以外のリージョンを使用する場合は引数で指定できます
5252

53-
$ flcs --region 'ap-northeast-1' VPCID
53+
$ flcs --region 'ap-northeast-1'
5454

5555
--help で簡単なヘルプを見ることができます。
5656

5757
bin/flcs --help
5858
usage: flcs [-h] [-O AWS_ACCESS_KEY] [-W AWS_SECRET_KEY] [--region REGION]
59-
vpcid
60-
61-
positional arguments:
62-
vpcid
6359

6460
optional arguments:
6561
-h, --help show this help message and exit
@@ -69,7 +65,6 @@ ${FLOCCUS_HOME} 以下に bin/ と lib/ ができますので ${FLOCCUS_HOME}/bi
6965

7066
## TODO
7167
* 一部のResourceにしか対応していないので拡充
72-
* Tagへの対応
7368
* cloudformerのようなWebアプリケーション化
7469

7570
## LICENCE

bin/flcs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import sys
44
sys.path.insert(0, sys.path[0] + '/../lib')
5-
from floccus import main
6-
7-
main.main()
8-
5+
from floccus import cli
96

7+
cli.main()

lib/floccus/main.py lib/floccus/cli.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
def main():
1212
# parse arguments
1313
parser = argparse.ArgumentParser()
14-
parser.add_argument('vpcid')
14+
parser.add_argument('--region', default='us-east-1')
1515
parser.add_argument('-O','--aws-access-key', default=None)
1616
parser.add_argument('-W','--aws-secret-key', default=None)
17-
parser.add_argument('--region')
1817
parsed = parser.parse_args()
1918
access_key, secret_key = utils.get_aws_key(parsed.aws_access_key, parsed.aws_secret_key)
2019

2120
# do form
22-
former = CloudFormer(vpc_id=parsed.vpcid, access_key=access_key, secret_key=secret_key, region_name=parsed.region)
21+
former = CloudFormer(access_key=access_key, secret_key=secret_key, region_name=parsed.region)
2322
model = former.form()
2423

2524
# output

lib/floccus/former.py

+35-39
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
from models import *
99

1010
class CloudFormer:
11-
def __init__(self, access_key, secret_key, vpc_id, region_name='us-east-1'):
11+
def __init__(self, access_key, secret_key, region_name='us-east-1'):
1212
self.access_key = access_key if access_key is not None else ''
1313
self.secret_key = secret_key if secret_key is not None else ''
1414
self.region = boto.ec2.get_region(
1515
region_name,
1616
aws_access_key_id=self.access_key,
1717
aws_secret_access_key=self.secret_key
1818
)
19-
self.vpc_id = vpc_id
2019

2120
def form(self):
2221
self.vpcconn = boto.connect_vpc(
@@ -25,62 +24,63 @@ def form(self):
2524
aws_secret_access_key=self.secret_key
2625
)
2726
context = {}
28-
self._form_vpc(context)
29-
self._form_internet_gateway(context)
30-
self._form_gateway_attachments(context)
31-
self._form_subnets(context)
32-
self._form_instances(context)
33-
self._form_route_tables(context)
34-
self._form_route(context)
35-
self._form_subnet_route_table_association(context)
27+
vpcs = self._form_vpc(context)
28+
for vpc in vpcs:
29+
internet_gateways = self._form_internet_gateway(context, vpc)
30+
subnets = self._form_subnets(context, vpc)
31+
instances = self._form_instances(context, vpc, subnets)
32+
route_tables = self._form_route_tables(context, vpc)
33+
self._form_gateway_attachments(context, vpc, internet_gateways)
34+
self._form_route(context, route_tables, internet_gateways, instances)
35+
self._form_subnet_route_table_association(context, route_tables, subnets)
3636
return context
3737

3838
def _form_vpc(self, context):
39-
vpcs = self.vpcconn.get_all_vpcs(
40-
filters=[('vpc-id', self.vpc_id)]
41-
)
42-
context['vpc'] = CfnVpc(vpcs[0])
39+
vpcs = [CfnVpc(vpc) for vpc in self.vpcconn.get_all_vpcs()]
40+
context['vpcs'] = vpcs
41+
return vpcs
4342

44-
def _form_internet_gateway(self, context):
45-
context['internet_gateways'] = [
43+
def _form_internet_gateway(self, context, vpc):
44+
internet_gateways = [
4645
CfnInternetGateWay(igw)
4746
for igw in self.vpcconn.get_all_internet_gateways(
48-
filters=[('attachment.vpc-id',self.vpc_id)]
47+
filters=[('attachment.vpc-id', vpc.id)]
4948
)
5049
]
50+
context['internet_gateways'] = internet_gateways
51+
return internet_gateways
5152

52-
def _form_gateway_attachments(self, context):
53-
vpc = context['vpc']
54-
internet_gateways = context['internet_gateways'] if 'internet_gateways' in context else []
53+
def _form_gateway_attachments(self, context, vpc, internet_gateways):
5554
attachments = []
5655
for internet_gateway in internet_gateways:
5756
attachments.extend([
5857
CfnVpcGatewayAttachment(att, cfn_vpc=vpc, cfn_gateway=internet_gateway)
5958
for att in internet_gateway.attachments
6059
])
6160
context['gateway_attachments'] = attachments
61+
return attachments
6262

63-
def _form_subnets(self, context):
64-
vpc = context['vpc']
65-
context['subnets'] = [
63+
def _form_subnets(self, context, vpc):
64+
subnets = [
6665
CfnSubnet(s, vpc)
6766
for s in self.vpcconn.get_all_subnets(
6867
filters=[('vpc-id', vpc.id)]
6968
)
7069
]
70+
context['subnets'] = subnets
71+
return subnets
7172

72-
def _form_route_tables(self, context):
73-
vpc = context['vpc']
74-
context['route_tables'] = [
73+
def _form_route_tables(self, context, vpc):
74+
route_tables = [
7575
CfnRouteTable(rtb, cfn_vpc=vpc)
7676
for rtb in self.vpcconn.get_all_route_tables(
7777
filters=[('vpc-id', vpc.id)]
7878
)
7979
]
80+
context['route_tables'] = route_tables
81+
return route_tables
8082

81-
def _form_instances(self, context):
82-
vpc = context['vpc']
83-
subnets = context['subnets'] if 'subnets' in context else []
83+
def _form_instances(self, context, vpc, subnets):
8484
instances = []
8585
for reservation in self.vpcconn.get_all_instances(filters={'vpc-id': vpc.id}):
8686
for instance in reservation.instances:
@@ -90,15 +90,12 @@ def _form_instances(self, context):
9090
if subnet.id == instance.subnet_id
9191
])
9292
context['instances'] = instances
93+
return instances
9394

94-
def _form_route(self, context):
95-
route_tables = context['route_tables'] if 'route_tables' in context else []
96-
gateways = context['internet_gateways'] if 'internet_gateways' in context else []
97-
instances = context['instances'] if 'instances' in context else []
98-
network_interfaces = context['network_interfaces'] if 'network_interfaces' in context else []
95+
def _form_route(self, context, route_tables=[], internet_gateways=[], instances=[], network_interfaces=[]):
9996
routes = []
10097
for route_table in route_tables:
101-
for gateway in gateways:
98+
for gateway in internet_gateways:
10299
routes.extend([
103100
CfnRoute(route, cfn_route_table=route_table, cfn_gateway=gateway)
104101
for route in route_table.routes
@@ -117,10 +114,9 @@ def _form_route(self, context):
117114
if network_interfaces.id == route.network_interface_id
118115
])
119116
context['routes'] = routes
117+
return routes
120118

121-
def _form_subnet_route_table_association(self, context):
122-
route_tables = context['route_tables']
123-
subnets = context['subnets']
119+
def _form_subnet_route_table_association(self, context, route_tables, subnets):
124120
associations = []
125121
for subnet in subnets:
126122
for route_table in route_tables:
@@ -130,4 +126,4 @@ def _form_subnet_route_table_association(self, context):
130126
if assoc.subnet_id == subnet.id
131127
])
132128
context['subnet_route_table_associations'] = associations
133-
129+
return associations

lib/floccus/models.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def cfn_resource_name(self):
2929
return self.tag_name
3030

3131
def default_cfn_resource_name(self):
32-
return None
32+
return (self.id).replace('-','')
3333

3434
class CfnVpc(CfnAWSResource):
3535
def __init__(self, vpc):
@@ -46,7 +46,7 @@ def __init__(self, attachment, cfn_vpc, cfn_gateway):
4646
self.gateway = cfn_gateway
4747

4848
def cfn_resource_name(self):
49-
return self.vpc.cfn_resource_name() + self.gateway.cfn_resource_name() + "GateWayAttachment"
49+
return self.vpc.cfn_resource_name() + self.gateway.cfn_resource_name() + "GatewayAttachment"
5050

5151
class CfnSubnet(CfnAWSResource):
5252
def __init__(self, subnet, cfn_vpc):
@@ -61,9 +61,6 @@ def __init__(self, route_table, cfn_vpc):
6161
CfnTaggedResource.__init__(self, route_table)
6262
self.vpc = cfn_vpc
6363

64-
def default_cfn_resource_name(self):
65-
return (self.id).replace('-','')
66-
6764
class CfnSubnetRouteTableAssociation(CfnAWSResource):
6865
def __init__(self, route_table_association, cfn_route_table, cfn_subnet):
6966
CfnAWSResource.__init__(self, route_table_association)
@@ -85,7 +82,3 @@ class CfnEC2Instance(CfnTaggedResource):
8582
def __init__(self, instance, cfn_subnet):
8683
CfnTaggedResource.__init__(self, instance)
8784
self.subnet = cfn_subnet
88-
89-
def default_cfn_resource_name(self):
90-
return (self.id).replace('-','')
91-

lib/floccus/templates/metatemplate.jinja2

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
{
22
"AWSTemplateFormatVersion": "2010-09-09",
3-
"Description": "This is auto generated cloudformation file. VPC:{{ model.vpc.id }}",
3+
"Description": "This is auto generated cloudformation file.",
44
"Resources": {
5-
"{{ model.vpc.cfn_resource_name() }}" : {
5+
6+
{% for vpc in model.vpcs %}
7+
"{{ vpc.cfn_resource_name() }}" : {
68
"Type" : "AWS::EC2::VPC",
79
"Properties" : {
8-
"CidrBlock" : "{{ model.vpc.cidr_block }}",
9-
"InstanceTenancy" : "{{ model.vpc.instanceTenancy }}"
10+
"CidrBlock" : "{{ vpc.cidr_block }}",
11+
"InstanceTenancy" : "{{ vpc.instanceTenancy }}"
1012
}
1113
},
14+
{% endfor %}
1215
{% for gateway in model.internet_gateways %}
1316
"{{ gateway.cfn_resource_name() }}" : {
1417
"Type" : "AWS::EC2::InternetGateway"

0 commit comments

Comments
 (0)